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(); 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, 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; 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;