From cf6ed7344f8a6818a475a5555ae1103437fb0048 Mon Sep 17 00:00:00 2001 From: Marc Michalsky forumZFD Date: Thu, 4 Feb 2021 17:23:10 +0100 Subject: [PATCH] cache campaign type ids --- CRM/TwingleCampaign/Utils/ExtensionCache.php | 35 +++++++++++++------- twinglecampaign.php | 8 ++--- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/CRM/TwingleCampaign/Utils/ExtensionCache.php b/CRM/TwingleCampaign/Utils/ExtensionCache.php index 180a753..95517f2 100644 --- a/CRM/TwingleCampaign/Utils/ExtensionCache.php +++ b/CRM/TwingleCampaign/Utils/ExtensionCache.php @@ -20,11 +20,12 @@ class CRM_TwingleCampaign_Utils_ExtensionCache { private $templates; /** - * Get an instance (singleton) - * @return self|null + * ## Get an instance (singleton) + * + * @return CRM_TwingleCampaign_Utils_ExtensionCache */ - public static function getInstance() { - if (null === Civi::cache()->get('twinglecampaign_cache')) { + public static function getInstance(): CRM_TwingleCampaign_Utils_ExtensionCache { + if (NULL === Civi::cache()->get('twinglecampaign_cache')) { Civi::cache()->set('twinglecampaign_cache', new self); } return Civi::cache()->get('twinglecampaign_cache'); @@ -34,6 +35,7 @@ class CRM_TwingleCampaign_Utils_ExtensionCache { * Protected ExtensionCache constructor. * * @throws \CiviCRM_API3_Exception + * @throws \Exception */ protected function __construct() { @@ -67,12 +69,21 @@ class CRM_TwingleCampaign_Utils_ExtensionCache { // Set attribute $this->$key = $array; } - } - protected function __clone() {} + // Get ids for Twingle related campaign types + foreach ($this->campaigns['campaign_types'] as $campaign_type) { + $this->campaigns['campaign_types'][$campaign_type['name']]['id'] = civicrm_api3( + 'OptionValue', + 'get', + ['sequential' => 1, 'name' => $campaign_type['name']] + )['values'][0]['value']; + } + + } /** * Returns a mapping of all custom field of the TwingleCampaign extension + * * @return array */ public function getCustomFieldMapping(): array { @@ -80,23 +91,23 @@ class CRM_TwingleCampaign_Utils_ExtensionCache { } /** - * @return mixed + * @return array */ - public function getTranslations() { + public function getTranslations(): array { return $this->translations; } /** - * @return mixed + * @return array */ - public function getCampaigns() { + public function getCampaigns(): array { return $this->campaigns; } /** - * @return mixed + * @return array */ - public function getTemplates() { + public function getTemplates(): array { return $this->templates; } diff --git a/twinglecampaign.php b/twinglecampaign.php index 95bce26..0a17f8d 100644 --- a/twinglecampaign.php +++ b/twinglecampaign.php @@ -27,11 +27,9 @@ function twinglecampaign_civicrm_postSave_civicrm_campaign($dao) { $hook_campaign_id = $dao->id; // Get campaign type id for TwingleProject - $twingle_project_campaign_type_id = civicrm_api3( - 'OptionValue', - 'get', - ['sequential' => 1, 'name' => 'twingle_project'] - )['values'][0]['value']; + $twingle_project_campaign_type_id = + ExtensionCache::getInstance() + ->getCampaigns()['campaign_types']['twingle_project']['id']; // If $dao is a TwingleProject campaign, synchronize it if ($hook_campaign_type_id == $twingle_project_campaign_type_id) {