From 4f166aecc3a659aceed0e684923c9a3ef392499b Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 1 Jul 2019 23:38:00 +1200 Subject: [PATCH] [REF] extract prepareCreate from CustomField.create Straight forward extraction - cleaning up code with a view to offering a more efficient wrapping function --- CRM/Core/BAO/CustomField.php | 232 +++++++++++++++++++---------------- 1 file changed, 124 insertions(+), 108 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index c8f6585345..061271ba2c 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -153,115 +153,9 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { */ public static function create($params) { $transaction = new CRM_Core_Transaction(); - $origParams = array_merge([], $params); - $op = empty($params['id']) ? 'create' : 'edit'; - - CRM_Utils_Hook::pre($op, 'CustomField', CRM_Utils_Array::value('id', $params), $params); - if ($op === 'create') { - CRM_Core_DAO::setCreateDefaults($params, self::getDefaults()); - if (!isset($params['column_name'])) { - // if add mode & column_name not present, calculate it. - $params['column_name'] = strtolower(CRM_Utils_String::munge($params['label'], '_', 32)); - } - if (!isset($params['name'])) { - $params['name'] = CRM_Utils_String::munge($params['label'], '_', 64); - } - } - else { - $params['column_name'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', - $params['id'], - 'column_name' - ); - } - - switch (CRM_Utils_Array::value('html_type', $params)) { - case 'Select Date': - if (empty($params['date_format'])) { - $config = CRM_Core_Config::singleton(); - $params['date_format'] = $config->dateInputFormat; - } - break; - - case 'CheckBox': - case 'Multi-Select': - if (isset($params['default_checkbox_option'])) { - $tempArray = array_keys($params['default_checkbox_option']); - $defaultArray = array(); - foreach ($tempArray as $k => $v) { - if ($params['option_value'][$v]) { - $defaultArray[] = $params['option_value'][$v]; - } - } - - if (!empty($defaultArray)) { - // also add the separator before and after the value per new convention (CRM-1604) - $params['default_value'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $defaultArray) . CRM_Core_DAO::VALUE_SEPARATOR; - } - } - else { - if (!empty($params['default_option']) && isset($params['option_value'][$params['default_option']]) - ) { - $params['default_value'] = $params['option_value'][$params['default_option']]; - } - } - break; - } - - $htmlType = CRM_Utils_Array::value('html_type', $params); - $dataType = CRM_Utils_Array::value('data_type', $params); - $allowedOptionTypes = array('String', 'Int', 'Float', 'Money'); - - // create any option group & values if required - if ($htmlType != 'Text' && in_array($dataType, $allowedOptionTypes) - ) { - //CRM-16659: if option_value then create an option group for this custom field. - if ($params['option_type'] == 1 && (empty($params['option_group_id']) || !empty($params['option_value']))) { - // first create an option group for this custom group - $optionGroup = new CRM_Core_DAO_OptionGroup(); - $optionGroup->name = "{$params['column_name']}_" . date('YmdHis'); - $optionGroup->title = $params['label']; - $optionGroup->is_active = 1; - // Don't set reserved as it's not a built-in option group and may be useful for other custom fields. - $optionGroup->is_reserved = 0; - $optionGroup->data_type = $dataType; - $optionGroup->save(); - $params['option_group_id'] = $optionGroup->id; - if (!empty($params['option_value']) && is_array($params['option_value'])) { - foreach ($params['option_value'] as $k => $v) { - self::createOptionValue($params, $v, $optionGroup, $k, $dataType); - } - } - } - } - - // check for orphan option groups - if (!empty($params['option_group_id'])) { - if (!empty($params['id'])) { - self::fixOptionGroups($params['id'], $params['option_group_id']); - } - - // if we do not have a default value - // retrieve it from one of the other custom fields which use this option group - if (empty($params['default_value'])) { - //don't insert only value separator as default value, CRM-4579 - $defaultValue = self::getOptionGroupDefault($params['option_group_id'], - $htmlType - ); - - if (!CRM_Utils_System::isNull(explode(CRM_Core_DAO::VALUE_SEPARATOR, - $defaultValue - )) - ) { - $params['default_value'] = $defaultValue; - } - } - } - - // since we need to save option group id :) - if (!isset($params['attributes']) && strtolower($htmlType) == 'textarea') { - $params['attributes'] = 'rows=4, cols=60'; - } + $origParams = array_merge([], $params); + $params = self::prepareCreate($params, $op); $customField = new CRM_Core_DAO_CustomField(); $customField->copyValues($params); @@ -2013,6 +1907,128 @@ WHERE id IN ( %1, %2 ) } } + /** + * Prepare for the create operation. + * + * Munge params, create the option values if needed. + * + * This could be called by a single create or a batchCreate. + * + * @param array $params + * @param string $op + * + * @return array + */ + protected static function prepareCreate($params, $op) { + + CRM_Utils_Hook::pre($op, 'CustomField', CRM_Utils_Array::value('id', $params), $params); + if ($op === 'create') { + CRM_Core_DAO::setCreateDefaults($params, self::getDefaults()); + if (!isset($params['column_name'])) { + // if add mode & column_name not present, calculate it. + $params['column_name'] = strtolower(CRM_Utils_String::munge($params['label'], '_', 32)); + } + if (!isset($params['name'])) { + $params['name'] = CRM_Utils_String::munge($params['label'], '_', 64); + } + } + else { + $params['column_name'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', + $params['id'], + 'column_name' + ); + } + + switch (CRM_Utils_Array::value('html_type', $params)) { + case 'Select Date': + if (empty($params['date_format'])) { + $config = CRM_Core_Config::singleton(); + $params['date_format'] = $config->dateInputFormat; + } + break; + + case 'CheckBox': + case 'Multi-Select': + if (isset($params['default_checkbox_option'])) { + $tempArray = array_keys($params['default_checkbox_option']); + $defaultArray = []; + foreach ($tempArray as $k => $v) { + if ($params['option_value'][$v]) { + $defaultArray[] = $params['option_value'][$v]; + } + } + + if (!empty($defaultArray)) { + // also add the separator before and after the value per new convention (CRM-1604) + $params['default_value'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $defaultArray) . CRM_Core_DAO::VALUE_SEPARATOR; + } + } + else { + if (!empty($params['default_option']) && isset($params['option_value'][$params['default_option']]) + ) { + $params['default_value'] = $params['option_value'][$params['default_option']]; + } + } + break; + } + + $htmlType = CRM_Utils_Array::value('html_type', $params); + $dataType = CRM_Utils_Array::value('data_type', $params); + $allowedOptionTypes = ['String', 'Int', 'Float', 'Money']; + + // create any option group & values if required + if ($htmlType != 'Text' && in_array($dataType, $allowedOptionTypes) + ) { + //CRM-16659: if option_value then create an option group for this custom field. + if ($params['option_type'] == 1 && (empty($params['option_group_id']) || !empty($params['option_value']))) { + // first create an option group for this custom group + $optionGroup = new CRM_Core_DAO_OptionGroup(); + $optionGroup->name = "{$params['column_name']}_" . date('YmdHis'); + $optionGroup->title = $params['label']; + $optionGroup->is_active = 1; + // Don't set reserved as it's not a built-in option group and may be useful for other custom fields. + $optionGroup->is_reserved = 0; + $optionGroup->data_type = $dataType; + $optionGroup->save(); + $params['option_group_id'] = $optionGroup->id; + if (!empty($params['option_value']) && is_array($params['option_value'])) { + foreach ($params['option_value'] as $k => $v) { + self::createOptionValue($params, $v, $optionGroup, $k, $dataType); + } + } + } + } + + // check for orphan option groups + if (!empty($params['option_group_id'])) { + if (!empty($params['id'])) { + self::fixOptionGroups($params['id'], $params['option_group_id']); + } + + // if we do not have a default value + // retrieve it from one of the other custom fields which use this option group + if (empty($params['default_value'])) { + //don't insert only value separator as default value, CRM-4579 + $defaultValue = self::getOptionGroupDefault($params['option_group_id'], + $htmlType + ); + + if (!CRM_Utils_System::isNull(explode(CRM_Core_DAO::VALUE_SEPARATOR, + $defaultValue + )) + ) { + $params['default_value'] = $defaultValue; + } + } + } + + // since we need to save option group id :) + if (!isset($params['attributes']) && strtolower($htmlType) == 'textarea') { + $params['attributes'] = 'rows=4, cols=60'; + } + return $params; + } + /** * Move custom data from one contact to another. * -- 2.25.1