From 51a3717f8af2e28034966079a8f0e8e372201ef2 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 15 Dec 2015 16:06:23 -0500 Subject: [PATCH] CRM-17646 - Refactor out CRM_Core_BAO_CustomGroup::formatCustomValues --- .../Form/Search/Custom/MultipleValues.php | 3 +- CRM/Core/BAO/CustomField.php | 70 ++--- CRM/Core/BAO/CustomGroup.php | 244 +----------------- CRM/Dedupe/Merger.php | 8 +- CRM/Event/Form/Participant.php | 3 +- 5 files changed, 49 insertions(+), 279 deletions(-) diff --git a/CRM/Contact/Form/Search/Custom/MultipleValues.php b/CRM/Contact/Form/Search/Custom/MultipleValues.php index cad65fca08..67c053dd66 100644 --- a/CRM/Contact/Form/Search/Custom/MultipleValues.php +++ b/CRM/Contact/Form/Search/Custom/MultipleValues.php @@ -290,8 +290,7 @@ contact_a.sort_name as sort_name, public function alterRow(&$row) { foreach ($row as $fieldName => &$field) { if (strpos($fieldName, 'custom_') === 0) { - list(, $id) = explode('_', $fieldName); - $field = CRM_Core_BAO_CustomGroup::formatCustomValues(array('data' => $field), $this->fieldInfo[$id]); + $field = CRM_Core_BAO_CustomField::displayValue($field, $fieldName); } } } diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 9f5560d9ac..6914096be7 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -1147,13 +1147,18 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { /** * @param string|int|array|null $value - * @param CRM_Core_BAO_CustomField|int $field + * @param CRM_Core_BAO_CustomField|int|string $field * @param $contactId * - * @return array|int|null|string + * @return string + * @throws \Exception */ public static function displayValue($value, $field, $contactId = NULL) { - $fieldId = is_object($field) ? $field->id : $field; + $fieldId = is_object($field) ? $field->id : (int) str_replace('custom_', '', $field); + + if (!$fieldId) { + throw new Exception('CRM_Core_BAO_CustomField::displayValue requires a field id'); + } if (!isset(self::$displayInfoCache[$fieldId])) { if (!is_a($field, 'CRM_Core_BAO_CustomField')) { @@ -1165,6 +1170,8 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { 'id' => $fieldId, 'html_type' => $field->html_type, 'data_type' => $field->data_type, + 'date_format' => $field->date_format, + 'time_format' => $field->time_format, 'options' => $options, ); } @@ -1236,11 +1243,10 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { * @param array $field * @param int|null $contactID * - * @return string|array|int|null + * @return string */ private static function formatDisplayValue($value, $field, $contactID = NULL) { - $display = $value; - + if (self::isSerialized($field) && !is_array($value)) { $value = (array) CRM_Utils_Array::explodePadded($value); } @@ -1248,7 +1254,9 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { if ($field['html_type'] == 'CheckBox') { CRM_Utils_Array::formatArrayKeys($value); } - + + $display = is_array($value) ? implode(', ', $value) : (string) $value; + switch ($field['html_type']) { case 'Select': @@ -1272,23 +1280,36 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { $display = implode(', ', $v); } else { - $display = CRM_Utils_Array::value($value, $field['options']); + $display = CRM_Utils_Array::value($value, $field['options'], ''); } break; case 'Select Date': - if (is_array($value)) { - foreach ($value as $key => $val) { - $display[$key] = CRM_Utils_Date::customFormat($val); - } - } - else { - // remove time element display if time is not set - if (empty($field['options']['attributes']['time_format'])) { - $value = substr($value, 0, 10); + $customFormat = NULL; + + $actualPHPFormats = CRM_Core_SelectValues::datePluginToPHPFormats(); + $format = CRM_Utils_Array::value('date_format', $field); + + if ($format) { + if (array_key_exists($format, $actualPHPFormats)) { + $customTimeFormat = (array) $actualPHPFormats[$format]; + switch (CRM_Utils_Array::value('time_format', $field)) { + case 1: + $customTimeFormat[] = 'g:iA'; + break; + + case 2: + $customTimeFormat[] = 'G:i'; + break; + + default: + // if time is not selected remove time from value + $value = substr($value, 0, 10); + } + $customFormat = implode(" ", $customTimeFormat); } - $display = CRM_Utils_Date::customFormat($value); } + $display = CRM_Utils_Date::processDate($value, NULL, FALSE, $customFormat); break; case 'File': @@ -1307,19 +1328,10 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { break; case 'TextArea': - if (empty($value)) { - $display = ''; - } - else { - $display = is_array($value) ? nl2br(implode(', ', $value)) : nl2br($value); - } + $display = nl2br($display); break; - - case 'Link': - case 'Text': - $display = is_array($value) ? implode(', ', $value) : $value; } - return $display ? $display : $value; + return $display; } /** diff --git a/CRM/Core/BAO/CustomGroup.php b/CRM/Core/BAO/CustomGroup.php index b529b91b46..f27a456341 100644 --- a/CRM/Core/BAO/CustomGroup.php +++ b/CRM/Core/BAO/CustomGroup.php @@ -1909,18 +1909,10 @@ SELECT IF( EXISTS(SELECT name FROM civicrm_contact_type WHERE name like %1), 1, $details[$groupID][$values['id']]['collapse_adv_display'] = CRM_Utils_Array::value('collapse_adv_display', $group); $details[$groupID][$values['id']]['fields'][$k] = array( 'field_title' => CRM_Utils_Array::value('label', $properties), - 'field_type' => CRM_Utils_Array::value('html_type', - $properties - ), - 'field_data_type' => CRM_Utils_Array::value('data_type', - $properties - ), - 'field_value' => self::formatCustomValues($values, - $properties - ), - 'options_per_line' => CRM_Utils_Array::value('options_per_line', - $properties - ), + 'field_type' => CRM_Utils_Array::value('html_type', $properties), + 'field_data_type' => CRM_Utils_Array::value('data_type', $properties), + 'field_value' => CRM_Core_BAO_CustomField::displayValue($values['data'], $properties['id']), + 'options_per_line' => CRM_Utils_Array::value('options_per_line', $properties), ); // also return contact reference contact id if user has view all or edit all contacts perm if ((CRM_Core_Permission::check('view all contacts') || @@ -1965,234 +1957,6 @@ SELECT IF( EXISTS(SELECT name FROM civicrm_contact_type WHERE name like %1), 1, } } - /** - * Format custom value according to data, view mode - * - * @param array $values - * Associated array of custom values. - * @param array $field - * @param bool $dncOptionPerLine - * True if optionPerLine should not be consider. - * - * @return array|null|string - */ - public static function formatCustomValues($values, $field, $dncOptionPerLine = FALSE) { - $value = $values['data']; - - //changed isset CRM-4601 - if (CRM_Utils_System::isNull($value)) { - return NULL; - } - - $htmlType = CRM_Utils_Array::value('html_type', $field); - $dataType = CRM_Utils_Array::value('data_type', $field); - $option_group_id = CRM_Utils_Array::value('option_group_id', $field); - $timeFormat = CRM_Utils_Array::value('time_format', $field); - $optionPerLine = CRM_Utils_Array::value('options_per_line', $field); - - $freezeString = ""; - $freezeStringChecked = ""; - - switch ($dataType) { - case 'Date': - $customFormat = NULL; - - $actualPHPFormats = CRM_Core_SelectValues::datePluginToPHPFormats(); - - if ($format = CRM_Utils_Array::value('date_format', $field)) { - if (array_key_exists($format, $actualPHPFormats)) { - $customTimeFormat = (array) CRM_Utils_Array::value($format, $actualPHPFormats); - switch ($timeFormat) { - case 1: - $customTimeFormat[] = 'g:iA'; - break; - - case 2: - $customTimeFormat[] = 'G:i'; - break; - - default: - // if time is not selected remove time from value - $value = substr($value, 0, 10); - } - $customFormat = implode(" ", $customTimeFormat); - } - } - $retValue = CRM_Utils_Date::processDate($value, NULL, FALSE, $customFormat); - break; - - case 'Boolean': - if ($value == '1') { - $retValue = $freezeStringChecked . ts('Yes') . "\n"; - } - else { - $retValue = $freezeStringChecked . ts('No') . "\n"; - } - break; - - case 'Link': - if ($value) { - $retValue = CRM_Utils_System::formatWikiURL($value); - } - break; - - case 'File': - $retValue = $values; - break; - - case 'ContactReference': - if ($value) { - $retValue = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $values['data'], 'display_name'); - } - break; - - case 'Memo': - $retValue = $value; - break; - - case 'Float': - if ($htmlType == 'Text') { - $retValue = (float) $value; - break; - } - case 'Money': - if ($htmlType == 'Text') { - $retValue = CRM_Utils_Money::format($value, NULL, '%a'); - break; - } - case 'String': - case 'Int': - if (in_array($htmlType, array('Text', 'TextArea'))) { - $retValue = $value; - break; - } - // note that if its not text / textarea, the code falls thru and executes - // the below case also - case 'StateProvince': - case 'Country': - $options = array(); - $coDAO = NULL; - - //added check for Multi-Select in the below if-statement - $customData[] = $value; - - //form custom data for multiple-valued custom data - switch ($htmlType) { - case 'Multi-Select Country': - case 'Select Country': - $customData = $value; - if (!is_array($value)) { - $customData = CRM_Utils_Array::explodePadded($value); - } - $query = " - SELECT id as value, name as label - FROM civicrm_country"; - $coDAO = CRM_Core_DAO::executeQuery($query); - break; - - case 'Select State/Province': - case 'Multi-Select State/Province': - $customData = $value; - if (!is_array($value)) { - $customData = CRM_Utils_Array::explodePadded($value); - } - - $query = " - SELECT id as value, name as label - FROM civicrm_state_province"; - $coDAO = CRM_Core_DAO::executeQuery($query); - break; - - case 'Select': - $customData = CRM_Utils_Array::explodePadded($value); - if ($option_group_id) { - $options = CRM_Core_BAO_OptionValue::getOptionValuesAssocArray($option_group_id); - } - break; - - case 'CheckBox': - case 'AdvMulti-Select': - case 'Multi-Select': - $customData = CRM_Utils_Array::explodePadded($value); - default: - if ($option_group_id) { - $options = CRM_Core_BAO_OptionValue::getOptionValuesAssocArray($option_group_id); - } - } - - if (is_object($coDAO)) { - while ($coDAO->fetch()) { - if ($dataType == 'Country') { - // NB: using ts() on a variable here is OK, since the value is pre-determined, not variable - // and already extracted to .pot files. - $options[$coDAO->value] = ts($coDAO->label, array('context' => 'country')); - } - elseif ($dataType == 'StateProvince') { - $options[$coDAO->value] = ts($coDAO->label, array('context' => 'province')); - } - else { - $options[$coDAO->value] = $coDAO->label; - } - } - } - - CRM_Utils_Hook::customFieldOptions($field['id'], $options, FALSE); - - $retValue = NULL; - foreach ($options as $optionValue => $optionLabel) { - if ($dataType == 'Money') { - foreach ($customData as $k => $v) { - $customData[] = CRM_Utils_Money::format($v, NULL, '%a'); - } - } - - //to show only values that are checked - if (in_array((string) $optionValue, $customData)) { - $checked = in_array($optionValue, $customData) ? $freezeStringChecked : $freezeString; - if (!$optionPerLine || $dncOptionPerLine) { - if ($retValue) { - $retValue .= ", "; - } - $retValue .= $checked . $optionLabel; - } - else { - $retValue[] = $checked . $optionLabel; - } - } - } - break; - } - - //special case for option per line formatting - if ($optionPerLine > 1 && is_array($retValue)) { - $rowCounter = 0; - $fieldCounter = 0; - $displayValues = array(); - $displayString = ''; - foreach ($retValue as $val) { - if ($displayString) { - $displayString .= ", "; - } - - $displayString .= $val; - $rowCounter++; - $fieldCounter++; - - if (($rowCounter == $optionPerLine) || - ($fieldCounter == count($retValue)) - ) { - $displayValues[] = $displayString; - $displayString = ''; - $rowCounter = 0; - } - } - $retValue = $displayValues; - } - - $retValue = isset($retValue) ? $retValue : NULL; - return $retValue; - } - /** * Get the custom group titles by custom field ids. * diff --git a/CRM/Dedupe/Merger.php b/CRM/Dedupe/Merger.php index 2933c41535..457f12096e 100644 --- a/CRM/Dedupe/Merger.php +++ b/CRM/Dedupe/Merger.php @@ -1406,17 +1406,13 @@ INNER JOIN civicrm_membership membership2 ON membership1.membership_type_id = m } if (!empty($mainTree[$gid]['fields'][$fid]['customValue'])) { foreach ($mainTree[$gid]['fields'][$fid]['customValue'] as $valueId => $values) { - $rows["move_custom_$fid"]['main'] = CRM_Core_BAO_CustomGroup::formatCustomValues($values, - $field, TRUE - ); + $rows["move_custom_$fid"]['main'] = CRM_Core_BAO_CustomField::displayValue($values['data'], $fid); } } $value = "null"; if (!empty($otherTree[$gid]['fields'][$fid]['customValue'])) { foreach ($otherTree[$gid]['fields'][$fid]['customValue'] as $valueId => $values) { - $rows["move_custom_$fid"]['other'] = CRM_Core_BAO_CustomGroup::formatCustomValues($values, - $field, TRUE - ); + $rows["move_custom_$fid"]['other'] = CRM_Core_BAO_CustomField::displayValue($values['data'], $fid); if ($values['data'] === 0 || $values['data'] === '0') { $values['data'] = $qfZeroBug; } diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index 373da816fb..1c0f840f11 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -1596,9 +1596,8 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment //format submitted data foreach ($params['custom'] as $fieldID => $values) { foreach ($values as $fieldValue) { - $customValue = array('data' => $fieldValue['value']); $customFields[$fieldID]['id'] = $fieldID; - $formattedValue = CRM_Core_BAO_CustomGroup::formatCustomValues($customValue, $customFields[$fieldID], TRUE); + $formattedValue = CRM_Core_BAO_CustomField::displayValue($fieldValue['value'], $fieldID); $customGroup[$customFields[$fieldID]['groupTitle']][$customFields[$fieldID]['label']] = str_replace(' ', '', $formattedValue); } } -- 2.25.1