Skip to content

Commit

Permalink
dev/core#866, dev/core#1318 Fix failure to import checkboxes for acti…
Browse files Browse the repository at this point in the history
…vities

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
  • Loading branch information
eileenmcnaughton committed Jan 11, 2021
1 parent 1d2cb41 commit 946054f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 39 deletions.
29 changes: 0 additions & 29 deletions CRM/Activity/Import/Parser/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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");
Expand Down
42 changes: 32 additions & 10 deletions tests/phpunit/CRMTraits/Custom/CustomDataTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -480,30 +496,36 @@ protected function getAvailableFieldCombinations() {
],
],
'CheckBox' => [
'label' => 'Pick Color',
'label' => 'Pick Shade',
'html_type' => 'CheckBox',
'data_type' => 'String',
'text_length' => '',
'default_value' => '',
'option_values' => [
[
'label' => 'Red',
'value' => 'R',
'label' => 'Lilac',
'value' => 'L',
'weight' => 1,
'is_active' => 1,
],
[
'label' => 'Yellow',
'value' => 'Y',
'label' => 'Purple',
'value' => 'P',
'weight' => 2,
'is_active' => 1,
],
[
'label' => 'Green',
'value' => 'G',
'label' => 'Mauve',
'value' => 'M',
'weight' => 3,
'is_active' => 1,
],
[
'label' => 'Violet',
'value' => 'V',
'weight' => 4,
'is_active' => 1,
],
],
],
'Multi-Select' => [
Expand Down

0 comments on commit 946054f

Please sign in to comment.