Skip to content

Commit

Permalink
[REF] Shift special handling of extends param of the customGroup crea…
Browse files Browse the repository at this point in the history
…te into apiv3 and change Custom Group form to use apiv3
  • Loading branch information
seamuslee001 committed Jun 16, 2021
1 parent 54a1943 commit a5ca284
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
36 changes: 11 additions & 25 deletions CRM/Core/BAO/CustomGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,36 +45,20 @@ public static function create(&$params) {
$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';
if (isset($params['extends']) && is_array($params['extends']) && !empty($params['extends'][1])) {
throw new CRM_Core_Exception('Pass in Extends as a single string and the sub type in the extends_entity_column_value param');
}
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');
elseif (isset($params['extends']) && is_array($params['extends'])) {
$params['extends'] = $params['extends'][0];
}

// 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)) {
Expand All @@ -88,8 +72,8 @@ public static function create(&$params) {
}
$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'], [
'',
'',
Expand Down Expand Up @@ -121,6 +105,8 @@ public static function create(&$params) {
'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', '>=');
Expand Down Expand Up @@ -214,7 +200,7 @@ public static function create(&$params) {
$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();

Expand Down
9 changes: 5 additions & 4 deletions CRM/Custom/Form/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,22 +413,23 @@ public function postProcess() {
$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();
// reset ACL and system caches.
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);
Expand Down
23 changes: 23 additions & 0 deletions api/v3/CustomGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down

0 comments on commit a5ca284

Please sign in to comment.