//retain earlier value when Import mode is `Fill`
unset($params[$key]);
}
- else {
- $params[$key] = CRM_Utils_String::strtoboolstr($val);
- }
}
}
}
_civicrm_api3_store_values($fields[$values['contact_type']], $values, $params);
return TRUE;
}
-
- // Check for custom field values
- $customFields = CRM_Core_BAO_CustomField::getFields(CRM_Utils_Array::value('contact_type', $values),
- FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE
- );
-
- foreach ($values as $key => $value) {
- if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
- // check if it's a valid custom field id
-
- if (!array_key_exists($customFieldID, $customFields)) {
- return civicrm_api3_create_error('Invalid custom field ID');
- }
- else {
- $params[$key] = $value;
- }
- }
- }
return TRUE;
}
if ($fieldMetadata['type'] === CRM_Utils_Type::T_BOOLEAN) {
$value = CRM_Utils_String::strtoboolstr($importedValue);
if ($value !== FALSE) {
- return (bool) $value;
+ return (int) $value;
}
return 'invalid_import_value';
}
--- /dev/null
+first_name,Last Name,Street Address,Do Not Email,Contact Custom Field, Address Custom Field
+Joe,1,Main Street,0,0,0
+Joe,2,Main Street,FALSE,FALSE,FALSE
+Joe,3,Main Street,false,false,false
+Joe,4,Main st,no,no,no
+Joe,5,Main Street,NO,NO,NO
+Betty,1,Main Street,1,1,1
+Betty,2,Main Street,TRUE,TRUE,TRUE
+Betty,3,Main Street,true,true,true
+Betty,4,Main Street,yes,yes,yes
+Betty,5,Main Street,YES,YES,YES
}
}
+ /**
+ * Test boolean field handling.
+ *
+ * @throws \CRM_Core_Exception
+ */
+ public function testImportBooleanFields(): void {
+ $this->createCustomGroupWithFieldOfType(['extends' => 'Address', 'name' => 'Address'], 'boolean', 'address_');
+ $this->createCustomGroupWithFieldOfType(['extends' => 'Contact', 'name' => 'Contact'], 'boolean', 'contact_');
+ $this->importCSV('individual_boolean.csv', [
+ ['first_name'],
+ ['last_name'],
+ ['street_address', 1],
+ ['do_not_email'],
+ [$this->getCustomFieldName('address_boolean'), 1],
+ [$this->getCustomFieldName('contact_boolean')],
+ ]);
+ $contacts = Address::get()->addWhere('contact_id.first_name', 'IN', ['Joe', 'Betty'])->setSelect([
+ 'contact_id.first_name',
+ 'contact_id.do_not_email',
+ $this->getCustomFieldName('address_boolean', 4),
+ 'contact_id.' . $this->getCustomFieldName('contact_boolean', 4),
+ ])->execute();
+
+ foreach ($contacts as $contact) {
+ $boolean = !($contact['contact_id.first_name'] === 'Joe');
+ $this->assertSame($boolean, $contact['contact_id.do_not_email']);
+ $this->assertSame($boolean, $contact[$this->getCustomFieldName('address_boolean', 4)]);
+ $this->assertSame($boolean, $contact['contact_id.' . $this->getCustomFieldName('contact_boolean', 4)]);
+ }
+
+ }
+
/**
* @throws \CRM_Core_Exception
*/
*
*/
public function createCustomGroupWithFieldOfType(array $groupParams = [], string $customFieldType = 'text', ?string $identifier = NULL, array $fieldParams = []): void {
- $supported = ['text', 'select', 'date', 'checkbox', 'int', 'contact_reference', 'radio', 'multi_country'];
+ $supported = ['text', 'select', 'date', 'checkbox', 'int', 'contact_reference', 'radio', 'multi_country', 'boolean'];
if (!in_array($customFieldType, $supported, TRUE)) {
$this->fail('we have not yet extracted other custom field types from createCustomFieldsOfAllTypes, Use consistent syntax when you do');
}
$reference = $this->createMultiCountryCustomField($fieldParams)['id'];
return;
+ case 'boolean':
+ $reference = $this->createBooleanCustomField($fieldParams)['id'];
+ return;
}
}