Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] extract portion that creates the custom field record #14725

Merged
merged 3 commits into from
Jul 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually @pradpnayak the net effect here is to set the op AFTER the pre hook has been called. I'm now on the fence about that - I guess it's a good thing to set afterwards even though other BAO don't but are also kinda inconsistent.

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