Skip to content

Commit

Permalink
Merge pull request #14725 from eileenmcnaughton/cust_field4
Browse files Browse the repository at this point in the history
[REF] extract portion that creates the custom field record
  • Loading branch information
eileenmcnaughton authored Jul 5, 2019
2 parents 5a28b9c + f45a42b commit 6c58c9a
Showing 1 changed file with 37 additions and 35 deletions.
72 changes: 37 additions & 35 deletions CRM/Core/BAO/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,42 +152,10 @@ public static function dataToHtml() {
* @return CRM_Core_DAO_CustomField
*/
public static function create($params) {
$transaction = new CRM_Core_Transaction();
$origParams = array_merge([], $params);
$params = self::prepareCreate($params);

$customField = new CRM_Core_DAO_CustomField();
$customField->copyValues($params);
$customField->save();

// make sure all values are present in the object for further processing
$customField->find(TRUE);

$triggerRebuild = CRM_Utils_Array::value('triggerRebuild', $params, TRUE);
//create/drop the index when we toggle the is_searchable flag
$customField = self::createCustomFieldRecord($params);
$op = empty($params['id']) ? 'add' : 'modify';
if ($op == 'modify') {
$indexExist = FALSE;
//as during create if field is_searchable we had created index.
if (!empty($params['id'])) {
$indexExist = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $params['id'], 'is_searchable');
}
self::createField($customField, $op, $indexExist, $triggerRebuild);
}
else {
if (!isset($origParams['column_name'])) {
$params['column_name'] .= "_{$customField->id}";
}
$customField->column_name = $params['column_name'];
$customField->save();
// make sure all values are present in the object
$customField->find(TRUE);

self::createField($customField, $op, FALSE, $triggerRebuild);
}

// complete transaction
$transaction->commit();
$indexExist = empty($params['id']) ? FALSE : CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $params['id'], 'is_searchable');
self::createField($customField, $op, $indexExist, CRM_Utils_Array::value('triggerRebuild', $params, TRUE));

CRM_Utils_Hook::post(($op === 'add' ? 'create' : 'edit'), 'CustomField', $customField->id, $customField);

Expand Down Expand Up @@ -1868,6 +1836,7 @@ protected static function createOptionValue(&$params, $value, CRM_Core_DAO_Optio
protected static function prepareCreate($params) {
$op = empty($params['id']) ? 'create' : 'edit';
CRM_Utils_Hook::pre($op, 'CustomField', CRM_Utils_Array::value('id', $params), $params);
$params['is_append_field_id_to_column_name'] = !isset($params['column_name']);
if ($op === 'create') {
CRM_Core_DAO::setCreateDefaults($params, self::getDefaults());
if (!isset($params['column_name'])) {
Expand Down Expand Up @@ -1975,6 +1944,39 @@ protected static function prepareCreate($params) {
return $params;
}

/**
* Create database entry for custom field and related option groups.
*
* @param array $params
*
* @return CRM_Core_DAO_CustomField
*/
protected static function createCustomFieldRecord($params) {
$transaction = new CRM_Core_Transaction();
$params = self::prepareCreate($params);

$customField = new CRM_Core_DAO_CustomField();
$customField->copyValues($params);
$customField->save();

//create/drop the index when we toggle the is_searchable flag
$op = empty($params['id']) ? 'add' : 'modify';
if ($op !== 'modify') {
if ($params['is_append_field_id_to_column_name']) {
$params['column_name'] .= "_{$customField->id}";
}
$customField->column_name = $params['column_name'];
$customField->save();
}

// complete transaction - note that any table alterations include an implicit commit so this is largely meaningless.
$transaction->commit();

// make sure all values are present in the object for further processing
$customField->find(TRUE);
return $customField;
}

/**
* Move custom data from one contact to another.
*
Expand Down

0 comments on commit 6c58c9a

Please sign in to comment.