From da4a07a4bb3c2fccd2cea8f4cc8fed615bc2763e Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 4 Dec 2020 17:22:29 +1300 Subject: [PATCH] dev/core#866, dev/core#1318 Fix failure to import checkboxes for activities This seeks to address a long-standing issue whereby checkboxes cannot be imported onto activities if the name and value match. Since this code was written the create was switched from a BAO create to the activity api create. Simply removing the handling to prepare for the BAO create allows the api to do it correctly --- CRM/Activity/Import/Parser/Activity.php | 29 ------------------- .../CRMTraits/Custom/CustomDataTrait.php | 28 ++++++++++++++++-- 2 files changed, 25 insertions(+), 32 deletions(-) diff --git a/CRM/Activity/Import/Parser/Activity.php b/CRM/Activity/Import/Parser/Activity.php index e38432cff8e2..86186d256cee 100644 --- a/CRM/Activity/Import/Parser/Activity.php +++ b/CRM/Activity/Import/Parser/Activity.php @@ -278,11 +278,6 @@ public function import($onDuplicate, &$values) { return CRM_Import_Parser::ERROR; } - $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, - NULL, - 'Activity' - ); - if ($this->_contactIdIndex < 0) { // Retrieve contact id using contact dedupe rule. @@ -393,36 +388,12 @@ protected function deprecated_activity_formatted_param(&$params, &$values, $crea $fields = CRM_Activity_DAO_Activity::fields(); _civicrm_api3_store_values($fields, $params, $values); - require_once 'CRM/Core/OptionGroup.php'; - $customFields = CRM_Core_BAO_CustomField::getFields('Activity'); - foreach ($params as $key => $value) { // ignore empty values or empty arrays etc if (CRM_Utils_System::isNull($value)) { continue; } - //Handling Custom Data - if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { - $values[$key] = $value; - $type = $customFields[$customFieldID]['html_type']; - if (CRM_Core_BAO_CustomField::isSerialized($customFields[$customFieldID])) { - $values[$key] = CRM_Import_Parser::unserializeCustomValue($customFieldID, $value, $type); - } - elseif ($type == 'Select' || $type == 'Radio') { - $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); - foreach ($customOption as $customFldID => $customValue) { - $val = $customValue['value'] ?? NULL; - $label = $customValue['label'] ?? NULL; - $label = strtolower($label); - $value = strtolower(trim($value)); - if (($value == $label) || ($value == strtolower($val))) { - $values[$key] = $val; - } - } - } - } - if ($key == 'target_contact_id') { if (!CRM_Utils_Rule::integer($value)) { return civicrm_api3_create_error("contact_id not valid: $value"); diff --git a/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php b/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php index 1e9ef5e4bfd3..581478c17335 100644 --- a/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php +++ b/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php @@ -92,10 +92,9 @@ protected function getCustomFieldColumnName($key) { * * @throws \API_Exception * @throws \CRM_Core_Exception - * @throws \Civi\API\Exception\UnauthorizedException */ - public function createCustomGroupWithFieldOfType($groupParams = [], $customFieldType = 'text', $identifier = NULL, $fieldParams = []) { - $supported = ['text', 'select', 'date', 'int', 'contact_reference', 'radio', 'multi_country']; + public function createCustomGroupWithFieldOfType($groupParams = [], $customFieldType = 'text', $identifier = NULL, $fieldParams = []): void { + $supported = ['text', 'select', 'date', 'checkbox', 'int', 'contact_reference', 'radio', 'multi_country']; if (!in_array($customFieldType, $supported, TRUE)) { throw new CRM_Core_Exception('we have not yet extracted other custom field types from createCustomFieldsOfAllTypes, Use consistent syntax when you do'); } @@ -113,6 +112,10 @@ public function createCustomGroupWithFieldOfType($groupParams = [], $customField $reference = $this->createSelectCustomField($fieldParams)['id']; return; + case 'checkbox': + $reference = $this->createStringCheckboxCustomField($fieldParams)['id']; + return; + case 'int': $reference = $this->createIntCustomField($fieldParams)['id']; return; @@ -154,6 +157,7 @@ public function createCustomFieldsOfAllTypes() { $ids['state'] = (int) $this->createStateCustomField(['custom_group_id' => $customGroupID])['id']; $ids['multi_state'] = (int) $this->createMultiStateCustomField(['custom_group_id' => $customGroupID])['id']; $ids['boolean'] = (int) $this->createBooleanCustomField(['custom_group_id' => $customGroupID])['id']; + $ids['checkbox'] = (int) $this->createStringCheckboxCustomField(['custom_group_id' => $customGroupID])['id']; return $ids; } @@ -374,6 +378,18 @@ protected function createDateCustomField($params): array { return $this->callAPISuccess('custom_field', 'create', $params)['values'][0]; } + /** + * Create a custom field of type radio with integer values. + * + * @param array $params + * + * @return array + */ + protected function createStringCheckboxCustomField(array $params): array { + $params = array_merge($this->getFieldsValuesByType('String', 'CheckBox'), $params); + return $this->callAPISuccess('custom_field', 'create', $params)['values'][0]; + } + /** * Create a custom field of type radio with integer values. * @@ -504,6 +520,12 @@ protected function getAvailableFieldCombinations() { 'weight' => 3, 'is_active' => 1, ], + [ + 'label' => 'blue', + 'value' => 'blue', + 'weight' => 4, + 'is_active' => 1, + ], ], ], 'Multi-Select' => [