Skip to content

Commit

Permalink
Merge pull request #14689 from eileenmcnaughton/cust_field_create
Browse files Browse the repository at this point in the history
[REF] extract prepareCreate from CustomField.create
  • Loading branch information
monishdeb authored Jul 2, 2019
2 parents 84c8491 + 4f166ae commit 526b880
Showing 1 changed file with 124 additions and 108 deletions.
232 changes: 124 additions & 108 deletions CRM/Core/BAO/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,115 +153,9 @@ public static function dataToHtml() {
*/
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);
Expand Down Expand Up @@ -2013,6 +1907,128 @@ protected static function createOptionValue(&$params, $value, CRM_Core_DAO_Optio
}
}

/**
* 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.
*
Expand Down

0 comments on commit 526b880

Please sign in to comment.