From 9857487f5169909cebf51743adf2afd83fccabc5 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Wed, 16 Jun 2021 02:45:23 +0000 Subject: [PATCH] [REF] Shift special handling of extends param of the customGroup create into apiv3 and change Custom Group form to use apiv3 --- CRM/Core/BAO/CustomGroup.php | 35 ++++--------------- CRM/Custom/Form/Group.php | 9 ++--- .../CustomGroupPreCreationSubscriber.php | 5 --- api/v3/CustomGroup.php | 23 ++++++++++++ .../CRMTraits/Custom/CustomDataTrait.php | 2 +- 5 files changed, 36 insertions(+), 38 deletions(-) diff --git a/CRM/Core/BAO/CustomGroup.php b/CRM/Core/BAO/CustomGroup.php index 44c4e958c0..9f18bfde45 100644 --- a/CRM/Core/BAO/CustomGroup.php +++ b/CRM/Core/BAO/CustomGroup.php @@ -45,36 +45,13 @@ class CRM_Core_BAO_CustomGroup extends CRM_Core_DAO_CustomGroup { $group->title = $params['title']; } - $extends = CRM_Utils_Array::value('extends', $params, []); - $extendsEntity = $extends[0] ?? NULL; - - $participantEntities = [ - 'ParticipantRole', - 'ParticipantEventName', - 'ParticipantEventType', - ]; - - if (in_array($extendsEntity, $participantEntities)) { - $group->extends = 'Participant'; - } - else { - $group->extends = $extendsEntity; - } - - $group->extends_entity_column_id = 'null'; - if (in_array($extendsEntity, $participantEntities) - ) { - $group->extends_entity_column_id = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $extendsEntity, 'value', 'name'); - } - - // this is format when form get submit. - $extendsChildType = $extends[1] ?? NULL; + $extendsChildType = NULL; // lets allow user to pass direct child type value, CRM-6893 if (!empty($params['extends_entity_column_value'])) { $extendsChildType = $params['extends_entity_column_value']; } if (!CRM_Utils_System::isNull($extendsChildType)) { - $registeredSubTypes = self::getSubTypes()[$extendsEntity]; + $registeredSubTypes = self::getSubTypes()[$params['extends']]; if (is_array($extendsChildType)) { foreach ($extendsChildType as $childType) { if (!array_key_exists($childType, $registeredSubTypes) && !in_array($childType, $registeredSubTypes, TRUE)) { @@ -88,8 +65,8 @@ class CRM_Core_BAO_CustomGroup extends CRM_Core_DAO_CustomGroup { } $extendsChildType = [$extendsChildType]; } - $extendsChildType = implode(CRM_Core_DAO::VALUE_SEPARATOR, (array) $extendsChildType); - if (CRM_Utils_Array::value(0, $extends) == 'Relationship') { + $extendsChildType = implode(CRM_Core_DAO::VALUE_SEPARATOR, $extendsChildType); + if ($params['extends'] == 'Relationship') { $extendsChildType = str_replace(['_a_b', '_b_a'], [ '', '', @@ -121,6 +98,8 @@ class CRM_Core_BAO_CustomGroup extends CRM_Core_DAO_CustomGroup { 'is_active', 'is_multiple', 'icon', + 'extends_entity_column_id', + 'extends', ]; $current_db_version = CRM_Core_BAO_Domain::version(); $is_public_version = version_compare($current_db_version, '4.7.19', '>='); @@ -214,7 +193,7 @@ class CRM_Core_BAO_CustomGroup extends CRM_Core_DAO_CustomGroup { $params['id'], 'table_name' ); - CRM_Core_BAO_SchemaHandler::changeFKConstraint($table, self::mapTableName($extendsEntity)); + CRM_Core_BAO_SchemaHandler::changeFKConstraint($table, self::mapTableName($params['extends'])); } $transaction->commit(); diff --git a/CRM/Custom/Form/Group.php b/CRM/Custom/Form/Group.php index 2b1cf9158e..ddf49e1c3a 100644 --- a/CRM/Custom/Form/Group.php +++ b/CRM/Custom/Form/Group.php @@ -413,7 +413,8 @@ class CRM_Custom_Form_Group extends CRM_Core_Form { $params['created_date'] = date('YmdHis'); } - $group = CRM_Core_BAO_CustomGroup::create($params); + $result = civicrm_api3('CustomGroup', 'create', $params); + $group = $result['values'][$result['id']]; // reset the cache Civi::cache('fields')->flush(); @@ -421,14 +422,14 @@ class CRM_Custom_Form_Group extends CRM_Core_Form { CRM_Core_BAO_Cache::resetCaches(); if ($this->_action & CRM_Core_Action::UPDATE) { - CRM_Core_Session::setStatus(ts('Your custom field set \'%1 \' has been saved.', [1 => $group->title]), ts('Saved'), 'success'); + CRM_Core_Session::setStatus(ts('Your custom field set \'%1 \' has been saved.', [1 => $group['title']]), ts('Saved'), 'success'); } else { // Jump directly to adding a field if popups are disabled $action = CRM_Core_Resources::singleton()->ajaxPopupsEnabled ? '' : '/add'; - $url = CRM_Utils_System::url("civicrm/admin/custom/group/field$action", 'reset=1&new=1&gid=' . $group->id . '&action=' . ($action ? 'add' : 'browse')); + $url = CRM_Utils_System::url("civicrm/admin/custom/group/field$action", 'reset=1&new=1&gid=' . $group['id'] . '&action=' . ($action ? 'add' : 'browse')); CRM_Core_Session::setStatus(ts("Your custom field set '%1' has been added. You can add custom fields now.", - [1 => $group->title] + [1 => $group['title']] ), ts('Saved'), 'success'); $session = CRM_Core_Session::singleton(); $session->replaceUserContext($url); diff --git a/Civi/Api4/Event/Subscriber/CustomGroupPreCreationSubscriber.php b/Civi/Api4/Event/Subscriber/CustomGroupPreCreationSubscriber.php index e0615aae14..625a09575c 100644 --- a/Civi/Api4/Event/Subscriber/CustomGroupPreCreationSubscriber.php +++ b/Civi/Api4/Event/Subscriber/CustomGroupPreCreationSubscriber.php @@ -27,14 +27,9 @@ class CustomGroupPreCreationSubscriber extends Generic\PreCreationSubscriber { * @param \Civi\Api4\Generic\DAOCreateAction $request */ protected function modify(DAOCreateAction $request) { - $extends = $request->getValue('extends'); $title = $request->getValue('title'); $name = $request->getValue('name'); - if (is_string($extends)) { - $request->addValue('extends', [$extends]); - } - if (NULL === $title && $name) { $request->addValue('title', $name); } diff --git a/api/v3/CustomGroup.php b/api/v3/CustomGroup.php index 3c5519fc70..f45c9ca302 100644 --- a/api/v3/CustomGroup.php +++ b/api/v3/CustomGroup.php @@ -40,6 +40,29 @@ function civicrm_api3_custom_group_create($params) { return civicrm_api3_create_error("First item in params['extends'] must be a class name (e.g. 'Contact')."); } + if (!isset($params['extends_entity_column_value']) && isset($params['extends'][1])) { + $extendsEntity = $params['extends'][0] ?? NULL; + $participantEntities = [ + 'ParticipantRole', + 'ParticipantEventName', + 'ParticipantEventType', + ]; + $params['extends_entity_column_id'] = 'null'; + if (in_array($extendsEntity, $participantEntities) + ) { + $params['extends_entity_column_id'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $extendsEntity, 'value', 'name'); + } + $params['extends_entity_column_value'] = $params['extends'][1]; + if (in_array($extendsEntity, $participantEntities)) { + $params['extends'] = 'Participant'; + } + else { + $params['extends'] = $extendsEntity; + } + } + elseif (isset($params['extends']) && (!isset($params['extends'][1]) || empty($params['extends'][1]))) { + $params['extends'] = $params['extends'][0]; + } if (isset($params['extends_entity_column_value']) && !is_array($params['extends_entity_column_value'])) { // BAO fails if this is a string, but API getFields says this must be a string, so we'll do a double backflip $params['extends_entity_column_value'] = CRM_Utils_Array::explodePadded($params['extends_entity_column_value']); diff --git a/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php b/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php index 95cbb922f5..ef904881c6 100644 --- a/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php +++ b/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php @@ -46,7 +46,7 @@ trait CRMTraits_Custom_CustomDataTrait { public function createCustomGroup($params = []) { $params = array_merge([ 'title' => 'Custom Group', - 'extends' => [$this->entity ?? 'Contact'], + 'extends' => $this->entity ?? 'Contact', 'weight' => 5, 'style' => 'Inline', 'max_multiple' => 0, -- 2.25.1