From 2f1e5721c140af46e32ffd1b8bdc154e96d9256b Mon Sep 17 00:00:00 2001 From: Marc Michalsky forumZFD Date: Mon, 12 Apr 2021 11:26:29 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20code=20cleanup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CRM/TwingleCampaign/BAO/Campaign.php | 2 -- CRM/TwingleCampaign/BAO/CampaignType.php | 9 +++++---- CRM/TwingleCampaign/BAO/CustomField.php | 18 +++--------------- CRM/TwingleCampaign/BAO/OptionValue.php | 5 ----- CRM/TwingleCampaign/BAO/TwingleProject.php | 7 ++----- CRM/TwingleCampaign/Upgrader.php | 11 +++++++---- api/v3/TwingleProject/Get.php | 2 +- 7 files changed, 18 insertions(+), 36 deletions(-) diff --git a/CRM/TwingleCampaign/BAO/Campaign.php b/CRM/TwingleCampaign/BAO/Campaign.php index 6a5e9cc..86b0bdc 100644 --- a/CRM/TwingleCampaign/BAO/Campaign.php +++ b/CRM/TwingleCampaign/BAO/Campaign.php @@ -26,7 +26,6 @@ abstract class CRM_TwingleCampaign_BAO_Campaign { protected $formattedValues; - /** * ## Campaign constructor. * @@ -55,7 +54,6 @@ abstract class CRM_TwingleCampaign_BAO_Campaign { * @throws \Exception */ public function create(bool $no_hook = FALSE): bool { - // Prepare project values for import into database $values_prepared_for_import = $this->values; $this->formatValues( diff --git a/CRM/TwingleCampaign/BAO/CampaignType.php b/CRM/TwingleCampaign/BAO/CampaignType.php index 8e9f686..5957f86 100644 --- a/CRM/TwingleCampaign/BAO/CampaignType.php +++ b/CRM/TwingleCampaign/BAO/CampaignType.php @@ -27,6 +27,9 @@ class CRM_TwingleCampaign_BAO_CampaignType { } /** + * @param bool $upgrade + * If true: Does not show UF message if campaign type already exists + * * @throws \CiviCRM_API3_Exception */ public function create(bool $upgrade = false) { @@ -41,8 +44,7 @@ class CRM_TwingleCampaign_BAO_CampaignType { ] ); - if ($field['count'] == 0) - { + if ($field['count'] == 0) { $this->results = civicrm_api3('OptionValue', 'create', $this->getSetAttributes()); $this->value = array_column($this->results['values'], 'value')[0]; @@ -55,8 +57,7 @@ class CRM_TwingleCampaign_BAO_CampaignType { } else { $error_message = $this->results['error_message']; - Civi::log()->error("Twingle Extension could not create new campaign type - for \"$this->label\": $error_message"); + Civi::log()->error("Twingle Extension could not create new campaign type for \"$this->label\": $error_message"); } } elseif (!$upgrade) { diff --git a/CRM/TwingleCampaign/BAO/CustomField.php b/CRM/TwingleCampaign/BAO/CustomField.php index b28642f..dea24f9 100644 --- a/CRM/TwingleCampaign/BAO/CustomField.php +++ b/CRM/TwingleCampaign/BAO/CustomField.php @@ -53,11 +53,6 @@ class CRM_TwingleCampaign_BAO_CustomField { if (array_key_exists($var, $attributes)) { $this->$var = $attributes[$var]; } - - // translate help_post - if ($this->help_post) { - $this->help_post = E::ts($this->help_post); - } } } @@ -162,16 +157,9 @@ class CRM_TwingleCampaign_BAO_CustomField { if (!$name) { $result = []; - // Get json file with all custom fields for this extension - $json_file = file_get_contents(E::path() . - '/CRM/TwingleCampaign/resources/campaigns.json'); - $campaign_info = json_decode($json_file, TRUE); - - // Log an error and throw an exception if the file cannot get read - if (!$campaign_info) { - Civi::log()->error("Could not read json file"); - throw new Exception('Could not read json file'); - } + // Get array with all custom fields for this extension + $campaign_info = + require E::path() . '/CRM/TwingleCampaign/resources/campaigns.php'; // Recursive method call with all custom field names from the json file foreach ($campaign_info['custom_fields'] as $customField) { diff --git a/CRM/TwingleCampaign/BAO/OptionValue.php b/CRM/TwingleCampaign/BAO/OptionValue.php index 38251f7..9de6594 100644 --- a/CRM/TwingleCampaign/BAO/OptionValue.php +++ b/CRM/TwingleCampaign/BAO/OptionValue.php @@ -33,11 +33,6 @@ class CRM_TwingleCampaign_BAO_OptionValue { if (array_key_exists($var, $attributes)) { $this->$var = $attributes[$var]; } - - // translate description - if ($this->description) { - $this->description = E::ts($this->description); - } } } diff --git a/CRM/TwingleCampaign/BAO/TwingleProject.php b/CRM/TwingleCampaign/BAO/TwingleProject.php index dd82cef..b92313e 100644 --- a/CRM/TwingleCampaign/BAO/TwingleProject.php +++ b/CRM/TwingleCampaign/BAO/TwingleProject.php @@ -677,19 +677,16 @@ class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign { return $response; } - /** * ## Last update * Returns a timestamp of the last update of the TwingleProject campaign. * - * @return int|string|null + * @return int|null */ - public - function lastUpdate(): ?int { + public function lastUpdate(): ?int { return self::getTimestamp($this->values['last_update']); } - /** * ## Get project id * Returns the **project_id** of this TwingleProject. diff --git a/CRM/TwingleCampaign/Upgrader.php b/CRM/TwingleCampaign/Upgrader.php index b87e152..6cecff9 100644 --- a/CRM/TwingleCampaign/Upgrader.php +++ b/CRM/TwingleCampaign/Upgrader.php @@ -65,10 +65,12 @@ class CRM_TwingleCampaign_Upgrader extends CRM_TwingleCampaign_Upgrader_Base { */ public function install() { // Create campaign types, custom fields and custom groups by the contents - // of the json file "campaigns.json" + // of the campaigns.php file - $campaign_info = Cache::getInstance()->getCampaigns(); - $option_values = Cache::getInstance()->getOptionValues(); + $campaign_info = require E::path() . + '/CRM/TwingleCampaign/resources/campaigns.php'; + $option_values = require E::path() . + '/CRM/TwingleCampaign/resources/option_values.php'; // Create campaign types foreach ($campaign_info['campaign_types'] as $campaign_type) { @@ -132,7 +134,8 @@ class CRM_TwingleCampaign_Upgrader extends CRM_TwingleCampaign_Upgrader_Base { */ public function uninstall() { - $campaign_info = Cache::getInstance()->getCampaigns(); + $campaign_info = require E::path() . + '/CRM/TwingleCampaign/resources/campaigns.php'; $option_values = Cache::getInstance()->getOptionValues(); // Delete campaign types diff --git a/api/v3/TwingleProject/Get.php b/api/v3/TwingleProject/Get.php index da78501..012150a 100644 --- a/api/v3/TwingleProject/Get.php +++ b/api/v3/TwingleProject/Get.php @@ -127,7 +127,7 @@ function civicrm_api3_twingle_project_Get(array $params): array { $query = ['options' => ['limit' => 0]]; foreach ($params as $key => $value) { - if ( $key != 'id' && + if ($key != 'id' && array_key_exists('twingle_project_' . $key, $custom_field_mapping) ) { $query[$custom_field_mapping['twingle_project_' . $key]] = $value;