From 482b8e2d0d1ff4d3c51ae302609da47d2ffc0256 Mon Sep 17 00:00:00 2001 From: Marc Michalsky Date: Fri, 17 Dec 2021 09:05:51 +0100 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=90=9B=20Fix=20bug=20#5:=20Error=20wh?= =?UTF-8?q?en=20altering=20any=20case=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CRM/TwingleCampaign/BAO/CustomField.php | 31 ++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/CRM/TwingleCampaign/BAO/CustomField.php b/CRM/TwingleCampaign/BAO/CustomField.php index 04324f3..a0a9b81 100644 --- a/CRM/TwingleCampaign/BAO/CustomField.php +++ b/CRM/TwingleCampaign/BAO/CustomField.php @@ -205,15 +205,40 @@ class CRM_TwingleCampaign_BAO_CustomField { try { foreach ($options as $key => $value) { - $result[] = civicrm_api3( + + $option_value_exists = civicrm_api3( 'OptionValue', - 'create', + 'get', [ + 'sequential' => 1, 'option_group_id' => $option_group_id, 'value' => $key, - 'label' => $value, ] ); + + // If the option value does not yet exist, create it + if ($option_value_exists['count'] == 0) { + $result[] = civicrm_api3( + 'OptionValue', + 'create', + [ + 'option_group_id' => $option_group_id, + 'value' => $key, + 'label' => $value, + ] + ); + } + // If the option value already exist, update it + else { + $result[] = civicrm_api3( + 'OptionValue', + 'create', + [ + 'id' => $option_value_exists['values'][0]['id'], + 'label' => $value, + ] + ); + } } } catch (CiviCRM_API3_Exception $e) { $errorMessage = $e->getMessage(); From 3e73ed733bb6a753005e368c9fe238e749772982 Mon Sep 17 00:00:00 2001 From: Marc Michalsky Date: Fri, 17 Dec 2021 12:13:13 +0100 Subject: [PATCH 2/4] =?UTF-8?q?=E2=9C=A8=20improve=20hex=20color=20validat?= =?UTF-8?q?ion=20#4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CRM/TwingleCampaign/BAO/TwingleProject.php | 34 +++++++++++++--------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/CRM/TwingleCampaign/BAO/TwingleProject.php b/CRM/TwingleCampaign/BAO/TwingleProject.php index 8f509c0..fc3f779 100644 --- a/CRM/TwingleCampaign/BAO/TwingleProject.php +++ b/CRM/TwingleCampaign/BAO/TwingleProject.php @@ -31,6 +31,13 @@ class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign { 'one_time', ]; + // All fields for hex color codes + const colorFields = [ + 'design_background_color', + 'design_primary_color', + 'design_font_color', + ]; + /** * ## TwingleProject constructor * @@ -143,11 +150,13 @@ class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign { */ private function strToInt(array &$values) { foreach ($values as $key => $value) { - if (ctype_digit($value)) { - $values[$key] = intval($value); - } - elseif (is_array($value)) { - $this->strToInt($values[$key]); + if (!in_array($key, self::colorFields)) { + if (ctype_digit($value)) { + $values[$key] = intval($value); + } + elseif (is_array($value)) { + $this->strToInt($values[$key]); + } } } } @@ -298,21 +307,18 @@ class CRM_TwingleCampaign_BAO_TwingleProject extends Campaign { } // Validate hexadecimal color fields - $colorFields = - [ - 'design_background_color', - 'design_primary_color', - 'design_font_color', - ]; - foreach ($colorFields as $colorField) { + foreach (self::colorFields as $colorField) { if ( - !empty($this->values['project_options'][$colorField]) && + ( + !empty($this->values['project_options'][$colorField]) || + $this->values['project_options'][$colorField] === "0" + ) && ( !( ctype_xdigit($this->values['project_options'][$colorField]) || is_integer($this->values['project_options'][$colorField]) ) || - strlen((string) $this->values['project_options'][$colorField]) > 6 + strlen((string) $this->values['project_options'][$colorField]) != 6 ) ) { $valid = FALSE; From fbe30b0ce582f8e9e49a56e6c5804e0be2cdf917 Mon Sep 17 00:00:00 2001 From: Marc Michalsky Date: Fri, 17 Dec 2021 12:19:46 +0100 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=97=91=EF=B8=8F=20get=20rid=20of=20th?= =?UTF-8?q?e=20warning=20about=20using=20an=20deprecated=20constant?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- twinglecampaign.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/twinglecampaign.php b/twinglecampaign.php index bda4f1d..04ff8ca 100644 --- a/twinglecampaign.php +++ b/twinglecampaign.php @@ -411,8 +411,7 @@ function _validateAndSendInput($id, $campaign_type_id): bool { CRM_Core_Session::setStatus( $errorMessage, E::ts("Input validation failed"), - error, - [unique => TRUE] + error ); // Validation failed return FALSE; From 0ab5bb1fa93d50081ad127697367990418b43d82 Mon Sep 17 00:00:00 2001 From: Marc Michalsky Date: Fri, 17 Dec 2021 12:23:38 +0100 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=A9=B9=20call=20the=20right=20API=20t?= =?UTF-8?q?o=20fetch=20OptionValues?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CRM/TwingleCampaign/BAO/OptionValue.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CRM/TwingleCampaign/BAO/OptionValue.php b/CRM/TwingleCampaign/BAO/OptionValue.php index 9de6594..1d808aa 100644 --- a/CRM/TwingleCampaign/BAO/OptionValue.php +++ b/CRM/TwingleCampaign/BAO/OptionValue.php @@ -158,7 +158,7 @@ class CRM_TwingleCampaign_BAO_OptionValue { // If a specific option value is required try { $option_value = civicrm_api3( - 'CustomValue', + 'OptionValue', 'get', [ 'sequential' => 1,