From 86a718273e4b817c88b83ab3f00827ae8f8289ee Mon Sep 17 00:00:00 2001 From: Marc Michalsky forumZFD Date: Mon, 22 Feb 2021 18:23:57 +0100 Subject: [PATCH] create OptionValue BAO class --- CRM/TwingleCampaign/BAO/OptionValue.php | 228 ++++++++++++++++++ CRM/TwingleCampaign/Upgrader.php | 17 ++ CRM/TwingleCampaign/Utils/ExtensionCache.php | 15 +- .../resources/option_values.json | 8 + 4 files changed, 265 insertions(+), 3 deletions(-) create mode 100644 CRM/TwingleCampaign/resources/option_values.json diff --git a/CRM/TwingleCampaign/BAO/OptionValue.php b/CRM/TwingleCampaign/BAO/OptionValue.php index 1f13bc3..c057541 100644 --- a/CRM/TwingleCampaign/BAO/OptionValue.php +++ b/CRM/TwingleCampaign/BAO/OptionValue.php @@ -1,5 +1,233 @@ extensionName = E::LONG_NAME; + + foreach ($this as $var => $value) { + + // put array items into attributes + if (array_key_exists($var, $attributes)) { + $this->$var = $attributes[$var]; + } + + // translate description + if ($this->description) { + $this->description = E::ts($this->description); + } + } + } + + /** + * Creates a OptionValue by calling CiviCRM API v.3 + * + * @throws \CiviCRM_API3_Exception + */ + public function create() { + + // Check if the option value already exists + $ov = civicrm_api3( + 'OptionValue', + 'get', + [ + 'sequential' => 1, + 'name' => $this->getName(), + ] + ); + + // If the option value does not exist, create it + if ($ov['count'] == 0) { + $this->result = civicrm_api3( + 'OptionValue', + 'create', + $this->getSetAttributes()); + + // Set option value id + $this->id = $this->result['id']; + + // Log custom value creation + if ($this->result['is_error'] == 0) { + Civi::log()->info("$this->extensionName has created a new option value. + label: $this->label + name: $this->name + id: $this->id + group: $this->option_group_id" + ); + } + // If the option value could not get created: log error + else { + if ($this->label && $this->option_group_id) { + Civi::log() + ->error("$this->extensionName could not create new option value + \"$this->label\" for option group \"$this->option_group_id\": + $this->result['error_message']"); + } + // If there is not enough information: log simple error message + else { + Civi::log() + ->error("$this->extensionName could not create new option value: + $this->result['error_message']"); + } + } + } + } + + + /** + * Gets all the set attributes of the object and returns them as an array. + * + * @return array + */ + private + function getSetAttributes(): array { + $setAttributes = []; + foreach ($this as $var => $value) { + if (isset($value)) { + $setAttributes[$var] = $value; + } + } + return $setAttributes; + } + + + /** + * Get an instance of a OptionValue by its name or get an array with all + * option values by leaving parameters empty. + * + * @param string|null $name + * The name of the option value you wish to instantiate. + * + * @return array|CRM_TwingleCampaign_BAO_OptionValue + * The required OptionValue or an array with all OptionValues. + * + * @throws \CiviCRM_API3_Exception + * @throws \Exception + */ + public static function fetch(string $name = NULL) { + + // If no specific option value is requested + if (!$name) { + $result = []; + $optionValues = ExtensionCache::getInstance()->getOptionValues(); + + // Recursive method call with all custom field names from the json file + foreach ($optionValues as $optionValue) { + $result[] = self::fetch($optionValue['name']); + } + return $result; + } + // If a specific option value is required + try { + $option_value = civicrm_api3( + 'CustomValue', + 'get', + [ + 'sequential' => 1, + 'name' => $name, + ] + ); + return new self(array_shift($option_value['values'])); + } catch (CiviCRM_API3_Exception $e) { + return NULL; + } + } + + /** + * Delete an OptionValue + * + * @throws \CiviCRM_API3_Exception + */ + public function delete() { + + // Delete this OptionValue by API call + $this->result = civicrm_api3( + 'OptionValue', + 'delete', + ['id' => $this->id] + ); + + // Check if custom field was deleted successfully + if ($this->result['is_error'] == 0) { + Civi::log()->info("$this->extensionName has deleted OptionValue. + label: $this->label + name: $this->name + id: $this->id + group: $this->option_group_id" + ); + } + // ... else: log error + else { + if ($this->label && $this->option_group_id) { + Civi::log() + ->error("$this->extensionName could not delete OptionValue + \"$this->label\" for group \"$this->option_group_id\": + $this->result['error_message']"); + } + else { + Civi::log() + ->error("$this->extensionName could not delete OptionValue: + $this->result['error_message']"); + } + } + } + + /** + * @return mixed + */ + public function getName() { + return $this->name; + } + + /** + * @return mixed + */ + public function getId() { + return $this->id; + } + + /** + * @return mixed + */ + public function getLabel() { + return $this->label; + } + + /** + * @return string + */ + public function getDescription(): string { + return $this->description; + } + + /** + * @return mixed + */ + public function getOptionGroupId() { + return $this->option_group_id; + } + } \ No newline at end of file diff --git a/CRM/TwingleCampaign/Upgrader.php b/CRM/TwingleCampaign/Upgrader.php index 6651460..8d04b9c 100644 --- a/CRM/TwingleCampaign/Upgrader.php +++ b/CRM/TwingleCampaign/Upgrader.php @@ -23,6 +23,7 @@ class CRM_TwingleCampaign_Upgrader extends CRM_TwingleCampaign_Upgrader_Base { // of the json file "campaigns.json" $campaign_info = Cache::getInstance()->getCampaigns(); + $option_values = Cache::getInstance()->getOptionValues(); // Create campaign types foreach ($campaign_info['campaign_types'] as $campaign_type) { @@ -48,6 +49,13 @@ class CRM_TwingleCampaign_Upgrader extends CRM_TwingleCampaign_Upgrader_Base { $cf = new CustomField($custom_field); $cf->create(); } + + // Create option values + foreach ($option_values as $option_value) { + $ov = new OptionValue($option_value); + $ov->create(); + } + } /** @@ -57,6 +65,7 @@ class CRM_TwingleCampaign_Upgrader extends CRM_TwingleCampaign_Upgrader_Base { public function uninstall() { $campaign_info = Cache::getInstance()->getCampaigns(); + $option_values = Cache::getInstance()->getOptionValues(); // Delete campaign types foreach ($campaign_info['campaign_types'] as $campaign_type) { @@ -74,6 +83,14 @@ class CRM_TwingleCampaign_Upgrader extends CRM_TwingleCampaign_Upgrader_Base { } } + // Delete option values + foreach ($option_values as $option_value) { + $result = OptionValue::fetch($option_value['name']); + if ($result) { + $result->delete(); + } + } + // Delete all settings for this extension Configuration::deleteAll(); diff --git a/CRM/TwingleCampaign/Utils/ExtensionCache.php b/CRM/TwingleCampaign/Utils/ExtensionCache.php index 1d3cf5c..20bbb05 100644 --- a/CRM/TwingleCampaign/Utils/ExtensionCache.php +++ b/CRM/TwingleCampaign/Utils/ExtensionCache.php @@ -19,6 +19,8 @@ class CRM_TwingleCampaign_Utils_ExtensionCache { private $templates; + private $option_values; + /** * ## Get an instance (singleton) * @@ -44,9 +46,10 @@ class CRM_TwingleCampaign_Utils_ExtensionCache { // Initialize json files as arrays $file_paths = [ - 'translations' => '/CRM/TwingleCampaign/resources/dictionary.json', - 'templates' => '/CRM/TwingleCampaign/resources/twingle_api_templates.json', - 'campaigns' => '/CRM/TwingleCampaign/resources/campaigns.json', + 'translations' => '/CRM/TwingleCampaign/resources/dictionary.json', + 'templates' => '/CRM/TwingleCampaign/resources/twingle_api_templates.json', + 'campaigns' => '/CRM/TwingleCampaign/resources/campaigns.json', + 'option_values' => '/CRM/TwingleCampaign/resources/option_values.json', ]; foreach ($file_paths as $key => $file_path) { @@ -124,5 +127,11 @@ class CRM_TwingleCampaign_Utils_ExtensionCache { return $this->templates; } + /** + * @return mixed + */ + public function getOptionValues() { + return $this->option_values; + } } \ No newline at end of file diff --git a/CRM/TwingleCampaign/resources/option_values.json b/CRM/TwingleCampaign/resources/option_values.json new file mode 100644 index 0000000..a51121e --- /dev/null +++ b/CRM/TwingleCampaign/resources/option_values.json @@ -0,0 +1,8 @@ +{ + "twingle_event": { + "name": "twingle_event", + "label": "Twingle Event", + "description": "A soft credit type for TwingleEvent initiators.", + "option_group_id": "soft_credit_type" + } +} \ No newline at end of file