diff --git a/CRM/TwingleCampaign/BAO/Configuration.php b/CRM/TwingleCampaign/BAO/Configuration.php index 4d26232..6d2614e 100644 --- a/CRM/TwingleCampaign/BAO/Configuration.php +++ b/CRM/TwingleCampaign/BAO/Configuration.php @@ -7,8 +7,7 @@ class CRM_TwingleCampaign_BAO_Configuration { 'twingle_api_key', 'twinglecampaign_xcm_profile', 'twinglecampaign_default_case', - 'twinglecampaign_soft_credits', - 'twinglecampaign_matomo_integration' + 'twinglecampaign_soft_credits' ]; @@ -28,12 +27,6 @@ class CRM_TwingleCampaign_BAO_Configuration { Civi::settings()->set('twinglecampaign_soft_credits', 0); } - // Set twinglecampaign_matomo_integration to '0' if checkbox is unchecked - if (!array_key_exists('twinglecampaign_matomo_integration', $settings)) { - Civi::settings()->set('twinglecampaign_matomo_integration', 0); - } - - Civi::settings()->add($settings); } @@ -70,4 +63,4 @@ class CRM_TwingleCampaign_BAO_Configuration { } } -} +} \ No newline at end of file diff --git a/CRM/TwingleCampaign/BAO/CustomField.php b/CRM/TwingleCampaign/BAO/CustomField.php index da2c824..a0a9b81 100644 --- a/CRM/TwingleCampaign/BAO/CustomField.php +++ b/CRM/TwingleCampaign/BAO/CustomField.php @@ -62,11 +62,10 @@ class CRM_TwingleCampaign_BAO_CustomField { * @param bool $upgrade * If true: Does not show UF message if custom field already exists * - * @returns array|False Result of custom field creation api call. False if - * field already existed und wasn't upgraded. + * @returns array Result of custom field creation api call * @throws \CiviCRM_API3_Exception */ - public function create(bool $upgrade = FALSE) { + public function create(bool $upgrade = FALSE): ?array { // Check if the field already exists $field = civicrm_api3( @@ -130,74 +129,52 @@ class CRM_TwingleCampaign_BAO_CustomField { return NULL; } else { - $this->id = $field['values'][0]['id']; - $this->custom_group_id = $field['values'][0]['custom_group_id']; - return $this->upgrade($field['values'][0]); + return NULL; } } /** - * Upgrade an existing custom field + * Update an existing custom field * - * @param $attributes - * Custom Field data - * @returns array|False Result of custom field creation api call, False if - * field was not upgraded + * @returns array Result of custom field creation api call */ - private function upgrade($attributes) { - $upgrade_necessary = False; + public function update(): array { - if (key_exists('option_group_id', $attributes)) { - $this->addOptions($attributes); - } + try { + $this->result = civicrm_api3( + 'CustomField', + 'create', + $this->getSetAttributes()); - foreach ($this as $var => $value) { - // put array items into attributes - if (array_key_exists($var, $attributes) && $attributes[$var] != $value) { - $this->$var = $attributes[$var]; - $upgrade_necessary = True; - } - } - - if ($upgrade_necessary) { - try { - $this->result = civicrm_api3( - 'CustomField', - 'create', - $this->getSetAttributes()); - - // Log field creation - if ($this->result['is_error'] == 0) { - Civi::log()->info("$this->extensionName has updated a custom field. + // Log field creation + if ($this->result['is_error'] == 0) { + Civi::log()->info("$this->extensionName has updated a custom field. label: $this->label name: $this->name id: $this->id group: $this->custom_group_id" - ); - return $this->result; - } - else { - throw new CiviCRM_API3_Exception($this->result['error_message']); - } - } - catch (CiviCRM_API3_Exception $e) { - // If the field could not get created: log error - $errorMessage = $e->getMessage(); - if ($this->name && $this->custom_group_id) { - Civi::log() - ->error("$this->extensionName could not create new custom field \"$this->name\" for group \"$this->custom_group_id\": $errorMessage"); - CRM_Utils_System::setUFMessage(E::ts('Creation of custom field \'%1\' failed. Find more information in the logs.', [1 => $this->name])); - } - // If there is not enough information: log simple error message - else { - Civi::log() - ->error("$this->extensionName could not create new custom field: $errorMessage"); - CRM_Utils_System::setUFMessage(E::ts("Creation of custom field failed. Find more information in the logs.")); - } + ); return $this->result; } + else { + throw new CiviCRM_API3_Exception($this->result['error_message']); + } + } catch (CiviCRM_API3_Exception $e) { + // If the field could not get created: log error + $errorMessage = $e->getMessage(); + if ($this->name && $this->custom_group_id) { + Civi::log() + ->error("$this->extensionName could not create new custom field \"$this->name\" for group \"$this->custom_group_id\": $errorMessage"); + CRM_Utils_System::setUFMessage(E::ts('Creation of custom field \'%1\' failed. Find more information in the logs.', [1 => $this->name])); + } + // If there is not enough information: log simple error message + else { + Civi::log() + ->error("$this->extensionName could not create new custom field: $errorMessage"); + CRM_Utils_System::setUFMessage(E::ts("Creation of custom field failed. Find more information in the logs.")); + } + return $this->result; } - return False; } /** @@ -211,14 +188,30 @@ class CRM_TwingleCampaign_BAO_CustomField { $result = []; try { - foreach ($this->option_values as $key => $value) { + $option_group_id = civicrm_api3( + 'CustomField', + 'getsingle', + ['id' => $this->id] + )['option_group_id']; + } catch (CiviCRM_API3_Exception $e) { + $errorMessage = $e->getMessage(); + Civi::log() + ->error("$this->extensionName could not get get option group id for custom field \"$this->name\": $errorMessage"); + CRM_Utils_System::setUFMessage( + E::ts('%1 could not get option group id for custom field \'%2\'. Find more information in the logs.', + [1 => $this->extensionName, 2 => $this->name]) + ); + } + + try { + foreach ($options as $key => $value) { $option_value_exists = civicrm_api3( 'OptionValue', 'get', [ 'sequential' => 1, - 'option_group_id' => $options['option_group_id'], + 'option_group_id' => $option_group_id, 'value' => $key, ] ); @@ -229,7 +222,7 @@ class CRM_TwingleCampaign_BAO_CustomField { 'OptionValue', 'create', [ - 'option_group_id' => $options['option_group_id'], + 'option_group_id' => $option_group_id, 'value' => $key, 'label' => $value, ] @@ -243,7 +236,6 @@ class CRM_TwingleCampaign_BAO_CustomField { [ 'id' => $option_value_exists['values'][0]['id'], 'label' => $value, - 'value' => $key, ] ); } @@ -355,12 +347,12 @@ class CRM_TwingleCampaign_BAO_CustomField { if ($this->label && $this->custom_group_id) { Civi::log() ->error("$this->extensionName could not delete custom field - \"$this->label\" for group \"$this->custom_group_id\": + \"$this->label\" for group \"$this->custom_group_id\": $this->result['error_message']"); } else { Civi::log() - ->error("$this->extensionName could not delete custom field: + ->error("$this->extensionName could not delete custom field: $this->result['error_message']"); } } @@ -504,4 +496,4 @@ class CRM_TwingleCampaign_BAO_CustomField { $this->option_values = $option_values; } -} +} \ No newline at end of file diff --git a/CRM/TwingleCampaign/BAO/TwingleEvent.php b/CRM/TwingleCampaign/BAO/TwingleEvent.php index faf609b..4248c1d 100644 --- a/CRM/TwingleCampaign/BAO/TwingleEvent.php +++ b/CRM/TwingleCampaign/BAO/TwingleEvent.php @@ -219,15 +219,13 @@ class CRM_TwingleCampaign_BAO_TwingleEvent extends Campaign { private static function matchContact(string $names, string $email): ?int { $names = StringOps::split_names($names); // Hopefully just a temporary solution - $firstnames = $names['firstnames'] ?? NULL; - $lastname = $names['lastname'] ?? NULL; - $display_name = $names['display_name'] ?? NULL; + $firstnames = $names['firstnames']; + $lastname = $names['lastname']; try { $contact = civicrm_api3('Contact', 'getorcreate', [ 'xcm_profile' => Civi::settings()->get('twinglecampaign_xcm_profile'), 'first_name' => $firstnames, 'last_name' => $lastname, - 'display_name' => $display_name, 'email' => $email, ]); return (int) $contact['id']; diff --git a/CRM/TwingleCampaign/BAO/TwingleProject.php b/CRM/TwingleCampaign/BAO/TwingleProject.php index d63dd42..2e2cd1d 100644 --- a/CRM/TwingleCampaign/BAO/TwingleProject.php +++ b/CRM/TwingleCampaign/BAO/TwingleProject.php @@ -181,8 +181,15 @@ class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign { return FALSE; } - return parent::create($no_hook); + $result = parent::create($no_hook); + // Check if campaign was created successfully + if ($result['is_error'] == 0) { + return TRUE; + } + else { + throw new Exception($result['error_message']); + } } /** diff --git a/CRM/TwingleCampaign/Exceptions/TwingleCampaignException.php b/CRM/TwingleCampaign/Exceptions/TwingleCampaignException.php index 61baf6b..0f99555 100644 --- a/CRM/TwingleCampaign/Exceptions/TwingleCampaignException.php +++ b/CRM/TwingleCampaign/Exceptions/TwingleCampaignException.php @@ -1,8 +1,8 @@ addElement( - 'checkbox', - 'twinglecampaign_matomo_integration', - E::ts('Use Matomo to track user interaction with Twingle forms'), - FALSE - ); - $this->addButtons([ [ 'type' => 'submit', diff --git a/CRM/TwingleCampaign/Upgrader.php b/CRM/TwingleCampaign/Upgrader.php index f103551..5c1a706 100644 --- a/CRM/TwingleCampaign/Upgrader.php +++ b/CRM/TwingleCampaign/Upgrader.php @@ -20,7 +20,7 @@ class CRM_TwingleCampaign_Upgrader extends CRM_TwingleCampaign_Upgrader_Base { * changed campaigns will get pulled from Twingle. * @throws \CiviCRM_API3_Exception */ - public function upgrade_03(): bool { + public function upgrade_02(): bool { $campaign_info = require E::path() . '/CRM/TwingleCampaign/resources/campaigns.php'; @@ -96,7 +96,7 @@ class CRM_TwingleCampaign_Upgrader extends CRM_TwingleCampaign_Upgrader_Base { [1 => $e->getMessage()] ), E::ts('Scheduled Job'), - 'error' + error ); } @@ -294,4 +294,83 @@ class CRM_TwingleCampaign_Upgrader extends CRM_TwingleCampaign_Upgrader_Base { Civi::settings()->revert('twingle_api_key'); } + + /** + * Example: Run a couple simple queries. + * + * @return TRUE on success + * @throws Exception + * + * public function upgrade_4200() { + * $this->ctx->log->info('Applying update 4200'); + * CRM_Core_DAO::executeQuery('UPDATE foo SET bar = "whiz"'); + * CRM_Core_DAO::executeQuery('DELETE FROM bang WHERE willy = wonka(2)'); + * return TRUE; + * } // */ + + + /** + * Example: Run an external SQL script. + * + * @return TRUE on success + * @throws Exception + * public function upgrade_4201() { + * $this->ctx->log->info('Applying update 4201'); + * // this path is relative to the extension base dir + * $this->executeSqlFile('sql/upgrade_4201.sql'); + * return TRUE; + * } // */ + + + /** + * Example: Run a slow upgrade process by breaking it up into smaller chunk. + * + * @return TRUE on success + * @throws Exception + * public function upgrade_4202() { + * $this->ctx->log->info('Planning update 4202'); // PEAR Log interface + * + * $this->addTask(E::ts('Process first step'), 'processPart1', $arg1, $arg2); + * $this->addTask(E::ts('Process second step'), 'processPart2', $arg3, $arg4); + * $this->addTask(E::ts('Process second step'), 'processPart3', $arg5); + * return TRUE; + * } + * public function processPart1($arg1, $arg2) { sleep(10); return TRUE; } + * public function processPart2($arg3, $arg4) { sleep(10); return TRUE; } + * public function processPart3($arg5) { sleep(10); return TRUE; } + * // */ + + + /** + * Example: Run an upgrade with a query that touches many (potentially + * millions) of records by breaking it up into smaller chunks. + * + * @return TRUE on success + * @throws Exception + * public function upgrade_4203() { + * $this->ctx->log->info('Planning update 4203'); // PEAR Log interface + * + * $minId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(min(id),0) FROM + * civicrm_contribution'); + * $maxId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(max(id),0) FROM + * civicrm_contribution'); for ($startId = $minId; $startId <= $maxId; + * $startId += self::BATCH_SIZE) { + * $endId = $startId + self::BATCH_SIZE - 1; + * $title = E::ts('Upgrade Batch (%1 => %2)', array( + * 1 => $startId, + * 2 => $endId, + * )); + * $sql = ' + * UPDATE civicrm_contribution SET foobar = whiz(wonky()+wanker) + * WHERE id BETWEEN %1 and %2 + * '; + * $params = array( + * 1 => array($startId, 'Integer'), + * 2 => array($endId, 'Integer'), + * ); + * $this->addTask($title, 'executeSql', $sql, $params); + * } + * return TRUE; + * } // */ + } diff --git a/CRM/TwingleCampaign/Utils/APIWrapper.php b/CRM/TwingleCampaign/Utils/APIWrapper.php index 06ff396..5afe426 100644 --- a/CRM/TwingleCampaign/Utils/APIWrapper.php +++ b/CRM/TwingleCampaign/Utils/APIWrapper.php @@ -40,8 +40,8 @@ class CRM_TwingleCampaign_Utils_APIWrapper { $response_copy = $response; // Create soft credit for contribution - if (array_key_exists('contribution', $response)) { - $contribution = array_shift($response_copy['contribution']); + if (array_key_exists('contribution', $response['values'])) { + $contribution = array_shift($response_copy['values']['contribution']); if (array_key_exists('campaign_id', $contribution)) { try { $twingle_event = civicrm_api3( @@ -49,7 +49,7 @@ class CRM_TwingleCampaign_Utils_APIWrapper { 'getsingle', ['id' => $contribution['campaign_id']] ); - $response['soft_credit'] = + $response['values']['soft_credit'] = self::createSoftCredit($contribution, $twingle_event)['values']; $event->setResponse($response); } catch (CiviCRM_API3_Exception $e) { @@ -58,8 +58,8 @@ class CRM_TwingleCampaign_Utils_APIWrapper { } } // Create soft credit for sepa mandate - elseif (array_key_exists('sepa_mandate', $response)) { - $sepa_mandate = array_pop($response_copy['sepa_mandate']); + elseif (array_key_exists('sepa_mandate', $response['values'])) { + $sepa_mandate = array_pop($response_copy['values']['sepa_mandate']); try { $contribution = civicrm_api3( @@ -84,7 +84,7 @@ class CRM_TwingleCampaign_Utils_APIWrapper { 'getsingle', ['id' => $contribution['contribution_campaign_id']] ); - $response['soft_credit'] = + $response['values']['soft_credit'] = self::createSoftCredit($contribution, $twingle_event)['values']; $event->setResponse($response); } catch (CiviCRM_API3_Exception $e) { @@ -103,12 +103,12 @@ class CRM_TwingleCampaign_Utils_APIWrapper { * the de.systopia.twingle extension can include the campaign into the * contribution which it will create. * - * @param array $apiRequest - * @param callable $callsame + * @param $apiRequest + * @param $callsame * * @return mixed */ - public static function mapDonation(array $apiRequest, callable $callsame) { + public function mapDonation($apiRequest, $callsame) { if (array_key_exists( 'campaign_id', @@ -129,7 +129,11 @@ class CRM_TwingleCampaign_Utils_APIWrapper { } } } - elseif (!empty($apiRequest['params']['custom_fields']['event'])) { + elseif (array_key_exists( + 'event', + $apiRequest['params']['custom_fields']) && + !empty($apiRequest['params']['custom_fields']['event']) + ) { try { $targetCampaign = civicrm_api3( 'TwingleEvent', @@ -200,9 +204,7 @@ class CRM_TwingleCampaign_Utils_APIWrapper { 'contribution_id' => $contribution['id'], ] ); - - throw $e; } } -} +} \ No newline at end of file diff --git a/CRM/TwingleCampaign/Utils/CaseTypes.php b/CRM/TwingleCampaign/Utils/CaseTypes.php index b05282c..312a23f 100644 --- a/CRM/TwingleCampaign/Utils/CaseTypes.php +++ b/CRM/TwingleCampaign/Utils/CaseTypes.php @@ -8,6 +8,7 @@ use CRM_TwingleCampaign_ExtensionUtil as E; * @return array */ function getCaseTypes(): array { + $caseTypes = [NULL => E::ts('none')]; try { $result = civicrm_api3('CaseType', 'get', [ 'sequential' => 1, diff --git a/CRM/TwingleCampaign/Utils/MatomoSnippet.php b/CRM/TwingleCampaign/Utils/MatomoSnippet.php deleted file mode 100644 index f6b62af..0000000 --- a/CRM/TwingleCampaign/Utils/MatomoSnippet.php +++ /dev/null @@ -1,75 +0,0 @@ -", - "", - "", - ]); - } - - /** - * Returns JavaScript snippet to track events in Matomo. - * - * @return string - */ - public static function get_event_tracker() { - $code = "_paq.push(['trackEvent', 'twingle', 'donation', event.data.value.recurringRythm, event.data.value.amount]);"; - return self::embed_in_base_function($code); - } - - /** - * Returns JavaScript snippet to track Matomo goals. - * - * @param $goal_id - * The ID of your Matomo goal. - * - * @return string - */ - public static function get_goal_tracker($goal_id) { - $code = "_paq.push(['trackGoal', $goal_id]);"; - return self::embed_in_base_function($code); - } - - /** - * Returns JavaScript snippet to track ecommerce activity in Matomo. - * - * @return string - */ - public static function get_ecommerce_tracker() { - $code = implode("\n", [ - "_paq.push(['addEcommerceItem', event.data.value.rythm, '', event.data.value.target, event.data.value.amount]);", - "_paq.push(['trackEcommerceOrder', 'anonymizedData', event.data.value.amount]);", - ]); - return self::embed_in_base_function($code); - } - - /** - * Appends the given code to the original code. - * - * @param $original - * The original code. - * @param $appendix - * The code you want to append to the original code. - * - * @return string - * The combined code after appending the appendix. - */ - public static function append_code($original, $appendix) { - return $original . $appendix; - } - -} diff --git a/CRM/TwingleCampaign/Utils/StringOperations.php b/CRM/TwingleCampaign/Utils/StringOperations.php index 98e6657..19c0fd3 100644 --- a/CRM/TwingleCampaign/Utils/StringOperations.php +++ b/CRM/TwingleCampaign/Utils/StringOperations.php @@ -49,7 +49,7 @@ class CRM_TwingleCampaign_Utils_StringOperations { $firstnames = implode(" ", $names); return ['firstnames' => $firstnames, 'lastname' => $lastname]; } - return ['display_name' => $string]; + return $string; } /** @@ -71,4 +71,4 @@ class CRM_TwingleCampaign_Utils_StringOperations { public static function startsWith($haystack, $needle): bool { return substr_compare($haystack, $needle, 0, strlen($needle)) === 0; } -} +} \ No newline at end of file diff --git a/CRM/TwingleCampaign/resources/campaigns.php b/CRM/TwingleCampaign/resources/campaigns.php index 9c59709..ffd097c 100644 --- a/CRM/TwingleCampaign/resources/campaigns.php +++ b/CRM/TwingleCampaign/resources/campaigns.php @@ -113,9 +113,7 @@ return [ "option_values" => [ "default" => E::ts("Default"), "event" => E::ts("Events"), - "membership" => E::ts("Membership"), - "shop" => E::ts("Shop"), - "giftshop" => E::ts("Gift Shop") + "membership" => E::ts("Membership") ], "text_length" => 32, "is_active" => 1, diff --git a/api/v3/TwingleForm/Get.php b/api/v3/TwingleForm/Get.php index 86a5475..6adaf2e 100644 --- a/api/v3/TwingleForm/Get.php +++ b/api/v3/TwingleForm/Get.php @@ -2,7 +2,6 @@ use CRM_TwingleCampaign_ExtensionUtil as E; use CRM_TwingleCampaign_Utils_ExtensionCache as Cache; -use CRM_TwingleCampaign_Utils_MatomoSnippet as MatomoSnippet; /** * TwingleForm.Get API specification (optional) @@ -111,49 +110,16 @@ function civicrm_api3_twingle_form_Get(array $params): array { 'project_type' => $value[$custom_field_mapping['twingle_project_type']], 'counter' => $value[$custom_field_mapping['twingle_project_counter']] ]; - $matomo_integration_enabled = Civi::settings()->get('twinglecampaign_matomo_integration', False); switch ($value[$custom_field_mapping['twingle_project_type']]) { case 'event': - if ($matomo_integration_enabled) { - $returnValues[$value['id']]['embed_code'] = - MatomoSnippet::append_code( - $value[$custom_field_mapping['twingle_project_eventall']], - MatomoSnippet::get_event_tracker() - ); - } - else { - $returnValues[$value['id']]['embed_code'] = - $value[$custom_field_mapping['twingle_project_eventall']]; - } - break; - case 'shop': - if ($matomo_integration_enabled) { - $returnValues[$value['id']]['embed_code'] = - MatomoSnippet::append_code( - $value[$custom_field_mapping['twingle_project_widget']], - MatomoSnippet::get_ecommerce_tracker() - ); - } - else { - $returnValues[$value['id']]['embed_code'] = - $value[$custom_field_mapping['twingle_project_widget']]; - } + $returnValues[$value['id']]['embed_code'] = + $value[$custom_field_mapping['twingle_project_eventall']]; break; default: - if ($matomo_integration_enabled) { - $returnValues[$value['id']]['embed_code'] = - MatomoSnippet::append_code( - $value[$custom_field_mapping['twingle_project_widget']], - MatomoSnippet::get_event_tracker() - ); - } - else { - $returnValues[$value['id']]['embed_code'] = - $value[$custom_field_mapping['twingle_project_widget']]; - } + $returnValues[$value['id']]['embed_code'] = + $value[$custom_field_mapping['twingle_project_widget']]; } } - return civicrm_api3_create_success($returnValues, $query, 'TwingleForm', 'Get'); } else { diff --git a/api/v3/TwingleProject/Sync.php b/api/v3/TwingleProject/Sync.php index ca72d0e..aed3d2a 100644 --- a/api/v3/TwingleProject/Sync.php +++ b/api/v3/TwingleProject/Sync.php @@ -193,8 +193,8 @@ function civicrm_api3_twingle_project_Sync(array $params): array { foreach ($projects_from_civicrm['values'] as $project_from_civicrm) { if ( !in_array($project_from_civicrm['project_id'], - array_column($projects_from_twingle, 'id'), - ) && $project_from_civicrm['is_active'] == 1) { + array_column($projects_from_twingle, 'id') + )) { // store campaign id in $id $id = $project_from_civicrm['id']; unset($project_from_civicrm['id']); diff --git a/info.xml b/info.xml index 7971b85..b7ae3af 100644 --- a/info.xml +++ b/info.xml @@ -14,12 +14,13 @@ https://lab.civicrm.org/Marc_Michalsky/de-forumzfd-twinglecampaign/-/issues http://www.gnu.org/licenses/agpl-3.0.html - 2024-06-15 - 1.0.8 + 2023-02-28 + 1.0.3 stable - 5.74 + 5.14.0 + de.systopia.xcm de.systopia.campaign diff --git a/templates/CRM/TwingleCampaign/Form/Settings.tpl b/templates/CRM/TwingleCampaign/Form/Settings.tpl index 288c13e..a7c7c14 100644 --- a/templates/CRM/TwingleCampaign/Form/Settings.tpl +++ b/templates/CRM/TwingleCampaign/Form/Settings.tpl @@ -26,11 +26,6 @@ {$form.twinglecampaign_soft_credits.html} - - {$form.twinglecampaign_matomo_integration.label} - {$form.twinglecampaign_matomo_integration.html} - -