From edd71ef9e6e98446da1332259edb7f7713a0a92b Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 2 Jul 2019 11:43:45 +1200 Subject: [PATCH 1/3] [REF] further simplification of custom field create action --- CRM/Core/BAO/CustomField.php | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 88732fddc302..6011f179c270 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -160,35 +160,25 @@ public static function create($params) { $customField->copyValues($params); $customField->save(); - // make sure all values are present in the object for further processing - $customField->find(TRUE); + $indexExist = empty($params['id']) ? FALSE : CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $params['id'], 'is_searchable'); - $triggerRebuild = CRM_Utils_Array::value('triggerRebuild', $params, TRUE); //create/drop the index when we toggle the is_searchable flag $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 ($op !== 'modify') { 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 + // 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); + self::createField($customField, $op, $indexExist, CRM_Utils_Array::value('triggerRebuild', $params, TRUE)); + CRM_Utils_Hook::post(($op === 'add' ? 'create' : 'edit'), 'CustomField', $customField->id, $customField); CRM_Utils_System::flushCache(); From b5c280fc1b9574377b6d4b930ace3eafd0b97ac4 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 4 Jul 2019 11:27:22 +1200 Subject: [PATCH 2/3] Clarify appending field to column_name --- CRM/Core/BAO/CustomField.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 6011f179c270..e28e0fcd90ed 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -153,7 +153,6 @@ public static function dataToHtml() { */ public static function create($params) { $transaction = new CRM_Core_Transaction(); - $origParams = array_merge([], $params); $params = self::prepareCreate($params); $customField = new CRM_Core_DAO_CustomField(); @@ -165,7 +164,7 @@ public static function create($params) { //create/drop the index when we toggle the is_searchable flag $op = empty($params['id']) ? 'add' : 'modify'; if ($op !== 'modify') { - if (!isset($origParams['column_name'])) { + if ($params['is_append_field_id_to_column_name']) { $params['column_name'] .= "_{$customField->id}"; } $customField->column_name = $params['column_name']; @@ -1858,6 +1857,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'])) { From f45a42ba1547d9489c208d9dd210682f26d7af4a Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 4 Jul 2019 11:33:51 +1200 Subject: [PATCH 3/3] [REF] extract portion that creates the custom field record --- CRM/Core/BAO/CustomField.php | 58 ++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index e28e0fcd90ed..8d232803944e 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -152,30 +152,9 @@ public static function dataToHtml() { * @return CRM_Core_DAO_CustomField */ public static function create($params) { - $transaction = new CRM_Core_Transaction(); - $params = self::prepareCreate($params); - - $customField = new CRM_Core_DAO_CustomField(); - $customField->copyValues($params); - $customField->save(); - - $indexExist = empty($params['id']) ? FALSE : CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $params['id'], 'is_searchable'); - - //create/drop the index when we toggle the is_searchable flag + $customField = self::createCustomFieldRecord($params); $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); + $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); @@ -1965,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. *