Skip to content

Commit

Permalink
Merge pull request #18146 from eileenmcnaughton/export_custom
Browse files Browse the repository at this point in the history
Export fix on long custom fields
  • Loading branch information
seamuslee001 authored Sep 9, 2020
2 parents ef2f892 + 71cc06b commit 620928b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 32 deletions.
49 changes: 17 additions & 32 deletions CRM/Export/BAO/ExportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1456,11 +1456,22 @@ public function getSqlColumnDefinition($fieldName, $columnName) {
return "`$fieldName` varchar(16)";

case CRM_Utils_Type::T_STRING:
if (isset($queryFields[$columnName]['maxlength'])) {
return "`$fieldName` varchar({$queryFields[$columnName]['maxlength']})";
if (isset($fieldSpec['maxlength'])) {
return "`$fieldName` varchar({$fieldSpec['maxlength']})";
}
else {
return "`$fieldName` varchar(255)";
$dataType = $fieldSpec['data_type'] ?? '';
// set the sql columns for custom data
switch ($dataType) {
case 'String':
// May be option labels, which could be up to 512 characters
$length = max(512, CRM_Utils_Array::value('text_length', $fieldSpec));
return "`$fieldName` varchar($length)";

case 'Memo':
return "`$fieldName` text";

default:
return "`$fieldName` varchar(255)";
}

case CRM_Utils_Type::T_TEXT:
Expand All @@ -1483,36 +1494,10 @@ public function getSqlColumnDefinition($fieldName, $columnName) {
}
}
else {
if (substr($fieldName, -3, 3) == '_id') {
if (substr($fieldName, -3, 3) === '_id') {
return "`$fieldName` varchar(255)";
}
elseif (substr($fieldName, -5, 5) == '_note') {
return "`$fieldName` text";
}
else {
// set the sql columns for custom data
if (isset($queryFields[$columnName]['data_type'])) {

switch ($queryFields[$columnName]['data_type']) {
case 'String':
// May be option labels, which could be up to 512 characters
$length = max(512, CRM_Utils_Array::value('text_length', $queryFields[$columnName]));
return "`$fieldName` varchar($length)";

case 'Link':
return "`$fieldName` varchar(255)";

case 'Memo':
return "`$fieldName` text";

default:
return "`$fieldName` varchar(255)";
}
}
else {
return "`$fieldName` text";
}
}
return "`$fieldName` text";
}
}

Expand Down
6 changes: 6 additions & 0 deletions tests/phpunit/CRM/Export/BAO/ExportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,9 @@ public function testExportRelationshipsMergeToHouseholdAllFields() {
/**
* Test custom data exporting.
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
* @throws \Civi\API\Exception\UnauthorizedException
* @throws \League\Csv\Exception
*/
public function testExportCustomData() {
Expand All @@ -690,17 +692,20 @@ public function testExportCustomData() {
for ($i = 0; $i < 70; $i++) {
$longString .= 'Blah';
}
$this->addOptionToCustomField('select_string', ['label' => $longString, 'name' => 'blah']);

$this->callAPISuccess('Contact', 'create', [
'id' => $this->contactIDs[1],
$this->getCustomFieldName('text') => $longString,
$this->getCustomFieldName('country') => 'LA',
$this->getCustomFieldName('select_string') => 'blah',
'api.Address.create' => ['location_type_id' => 'Billing', 'city' => 'Waipu'],
]);
$selectedFields = [
['name' => 'city', 'location_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Address', 'location_type_id', 'Billing')],
['name' => $this->getCustomFieldName('text')],
['name' => $this->getCustomFieldName('country')],
['name' => $this->getCustomFieldName('select_string')],
];

$this->doExportTest([
Expand All @@ -711,6 +716,7 @@ public function testExportCustomData() {
$this->assertEquals($longString, $row['Enter text here']);
$this->assertEquals('Waipu', $row['Billing-City']);
$this->assertEquals("Lao People's Democratic Republic", $row['Country']);
$this->assertEquals($longString, $row['Pick Color']);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/phpunit/CRMTraits/Custom/CustomDataTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/

use Civi\Api4\CustomGroup;
use Civi\Api4\CustomField;
use Civi\Api4\OptionValue;

/**
* Trait Custom Data trait.
Expand Down Expand Up @@ -164,6 +166,25 @@ protected function getCustomFieldName($key) {
return 'custom_' . $this->getCustomFieldID($key);
}

/**
* Add another option to the custom field.
*
* @param string $key
* @param array $values
*
* @return int
* @throws \API_Exception
*/
protected function addOptionToCustomField($key, $values) {
$optionGroupID = CustomField::get(FALSE)
->addWhere('id', '=', $this->getCustomFieldID($key))
->addSelect('option_group_id')
->execute()->first()['option_group_id'];
return (int) OptionValue::create(FALSE)
->setValues(array_merge(['option_group_id' => $optionGroupID], $values))
->execute()->first()['value'];
}

/**
* Get the custom field name for the relevant key.
*
Expand Down

0 comments on commit 620928b

Please sign in to comment.