From 78d0090bf8b4b909564b806e40d4b3dd78720572 Mon Sep 17 00:00:00 2001 From: Monish Deb Date: Thu, 23 Jul 2020 14:35:27 +0530 Subject: [PATCH] Revert "revert PR 13958" This reverts commit ba2ad46f76db57c8f9bea064e7ba4c5b5adefc83. --- CRM/Contact/BAO/Contact.php | 2 +- CRM/Contact/BAO/Group.php | 11 ++++++----- CRM/Contact/Form/Contact.php | 6 ++++-- CRM/Contact/Form/Edit/TagsAndGroups.php | 18 +++++++++--------- CRM/Contact/Form/GroupContact.php | 2 +- CRM/Contact/Form/Search/Basic.php | 3 ++- CRM/Contact/Form/Search/Criteria.php | 2 +- ang/crmMailing/Recipients.js | 13 +++++++++---- .../View/UserDashboard/GroupContactTest.php | 6 +++--- 9 files changed, 36 insertions(+), 27 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index b97aa37b1295..86bbf890b1d5 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -304,7 +304,7 @@ public static function &create(&$params, $fixAddress = TRUE, $invokeHooks = TRUE } } - if (array_key_exists('group', $params)) { + if (!empty($params['group'])) { $contactIds = [$params['contact_id']]; foreach ($params['group'] as $groupId => $flag) { if ($flag == 1) { diff --git a/CRM/Contact/BAO/Group.php b/CRM/Contact/BAO/Group.php index aac84aa42428..fae22f03b844 100644 --- a/CRM/Contact/BAO/Group.php +++ b/CRM/Contact/BAO/Group.php @@ -1055,7 +1055,7 @@ public static function getGroupsHierarchy( $groups = []; $args = [1 => [$groupIdString, 'String']]; $query = " -SELECT id, title, description, visibility, parents +SELECT id, title, description, visibility, parents, saved_search_id FROM civicrm_group WHERE id IN $groupIdString "; @@ -1081,7 +1081,7 @@ public static function getGroupsHierarchy( $parent = self::filterActiveGroups($parentArray); $tree[$parent][] = [ 'id' => $dao->id, - 'title' => $dao->title, + 'title' => empty($dao->saved_search_id) ? $dao->title : '* ' . $dao->title, 'visibility' => $dao->visibility, 'description' => $dao->description, ]; @@ -1089,7 +1089,7 @@ public static function getGroupsHierarchy( else { $roots[] = [ 'id' => $dao->id, - 'title' => $dao->title, + 'title' => empty($dao->saved_search_id) ? $dao->title : '* ' . $dao->title, 'visibility' => $dao->visibility, 'description' => $dao->description, ]; @@ -1123,8 +1123,9 @@ private static function buildGroupHierarchy(&$hierarchy, $group, $tree, $titleOn $hierarchy[$group['id']] = $spaces . $group['title']; } else { - $hierarchy[$group['id']] = [ - 'title' => $spaces . $group['title'], + $hierarchy[] = [ + 'id' => $group['id'], + 'text' => $spaces . $group['title'], 'description' => $group['description'], 'visibility' => $group['visibility'], ]; diff --git a/CRM/Contact/Form/Contact.php b/CRM/Contact/Form/Contact.php index 20d115f28dc1..04e3c8a897e7 100644 --- a/CRM/Contact/Form/Contact.php +++ b/CRM/Contact/Form/Contact.php @@ -870,8 +870,10 @@ public function postProcess() { } $group = $params['group'] ?? NULL; - if (!empty($group) && is_array($group)) { - unset($params['group']); + $params['group'] = ($params['group'] == '') ? [] : $params['group']; + if (!empty($group)) { + $group = is_array($group) ? $group : explode(',', $group); + $params['group'] = []; foreach ($group as $key => $value) { $params['group'][$value] = 1; } diff --git a/CRM/Contact/Form/Edit/TagsAndGroups.php b/CRM/Contact/Form/Edit/TagsAndGroups.php index c7d26927153d..9299246d9beb 100644 --- a/CRM/Contact/Form/Edit/TagsAndGroups.php +++ b/CRM/Contact/Form/Edit/TagsAndGroups.php @@ -102,17 +102,17 @@ public static function buildQuickForm( } if ($groupElementType == 'select') { - $groupsOptions[$id] = $group['title']; + $groupsOptions[$id] = $group; } else { $form->_tagGroup[$fName][$id]['description'] = $group['description']; - $elements[] = &$form->addElement('advcheckbox', $id, NULL, $group['title'], $attributes); + $elements[] = &$form->addElement('advcheckbox', $id, NULL, $group['text'], $attributes); } } if ($groupElementType == 'select' && !empty($groupsOptions)) { - $form->add('select', $fName, $groupName, $groupsOptions, FALSE, - ['id' => $fName, 'multiple' => 'multiple', 'class' => 'crm-select2 twenty'] + $form->add('select2', $fName, $groupName, $groupsOptions, FALSE, + ['placeholder' => '- select -', 'multiple' => TRUE, 'class' => 'twenty'] ); $form->assign('groupCount', count($groupsOptions)); } @@ -166,11 +166,11 @@ public static function setDefaults($id, &$defaults, $type = self::ALL, $fieldNam $contactGroup = CRM_Contact_BAO_GroupContact::getContactGroup($id, 'Added', NULL, FALSE, TRUE); if ($contactGroup) { - foreach ($contactGroup as $group) { - if ($groupElementType == 'select') { - $defaults[$fName][] = $group['group_id']; - } - else { + if ($groupElementType == 'select') { + $defaults[$fName] = implode(',', CRM_Utils_Array::collect('group_id', $contactGroup)); + } + else { + foreach ($contactGroup as $group) { $defaults[$fName . '[' . $group['group_id'] . ']'] = 1; } } diff --git a/CRM/Contact/Form/GroupContact.php b/CRM/Contact/Form/GroupContact.php index b2e326e960f7..79d4c29c1d62 100644 --- a/CRM/Contact/Form/GroupContact.php +++ b/CRM/Contact/Form/GroupContact.php @@ -73,7 +73,7 @@ public function buildQuickForm() { if ($onlyPublicGroups && $group['visibility'] == 'User and User Admin Only') { continue; } - $allGroups[$id] = $group; + $allGroups[$group['id']] = $group; } } else { diff --git a/CRM/Contact/Form/Search/Basic.php b/CRM/Contact/Form/Search/Basic.php index 1b6ae423fa4d..c1e84382319f 100644 --- a/CRM/Contact/Form/Search/Basic.php +++ b/CRM/Contact/Form/Search/Basic.php @@ -49,13 +49,14 @@ public function buildQuickForm() { // add select for groups // Get hierarchical listing of groups, respecting ACLs for CRM-16836. - $groupHierarchy = CRM_Contact_BAO_Group::getGroupsHierarchy($this->_group, NULL, '  ', TRUE); + $groupHierarchy = CRM_Contact_BAO_Group::getGroupsHierarchy($this->_group, NULL, '  '); if (!empty($searchOptions['groups'])) { $this->addField('group', [ 'entity' => 'group_contact', 'label' => ts('in'), 'placeholder' => ts('- any group -'), 'options' => $groupHierarchy, + 'type' => 'Select2', ]); } diff --git a/CRM/Contact/Form/Search/Criteria.php b/CRM/Contact/Form/Search/Criteria.php index dbfb6ebe6a34..5385e3a565d6 100644 --- a/CRM/Contact/Form/Search/Criteria.php +++ b/CRM/Contact/Form/Search/Criteria.php @@ -45,7 +45,7 @@ public static function basic(&$form) { $groupHierarchy = CRM_Contact_BAO_Group::getGroupsHierarchy($form->_group, NULL, '  ', TRUE); $form->add('select', 'group', ts('Groups'), $groupHierarchy, FALSE, - ['id' => 'group', 'multiple' => 'multiple', 'class' => 'crm-select2'] + ['id' => 'group', 'multiple' => 'multiple', 'class' => 'crm-select2'] ); $groupOptions = CRM_Core_BAO_OptionValue::getOptionValuesAssocArrayFromName('group_type'); $form->add('select', 'group_type', ts('Group Types'), $groupOptions, FALSE, diff --git a/ang/crmMailing/Recipients.js b/ang/crmMailing/Recipients.js index 2d0d16020972..1867e9582f70 100644 --- a/ang/crmMailing/Recipients.js +++ b/ang/crmMailing/Recipients.js @@ -95,11 +95,12 @@ } var option = convertValueToObj(item.id); var icon = (option.entity_type === 'civicrm_mailing') ? 'fa-envelope' : 'fa-users'; + var smartGroupMarker = item.is_smart ? '* ' : ''; var spanClass = (option.mode == 'exclude') ? 'crmMailing-exclude' : 'crmMailing-include'; if (option.entity_type != 'civicrm_mailing' && isMandatory(option.entity_id)) { spanClass = 'crmMailing-mandatory'; } - return ' ' + item.text + ''; + return ' ' + smartGroupMarker + item.text + ''; } function validate() { @@ -154,7 +155,7 @@ mids.push(0); } - CRM.api3('Group', 'getlist', { params: { id: { IN: gids }, options: { limit: 0 } }, extra: ["is_hidden"] } ).then( + CRM.api3('Group', 'getlist', { params: { id: { IN: gids }, options: { limit: 0 } }, extra: ["is_hidden"] }).then( function(glist) { CRM.api3('Mailing', 'getlist', { params: { id: { IN: mids }, options: { limit: 0 } } }).then( function(mlist) { @@ -165,6 +166,7 @@ $(glist.values).each(function (idx, group) { var key = group.id + ' civicrm_group include'; + groupNames.push({id: parseInt(group.id), title: group.label, is_hidden: group.extra.is_hidden}); if (values.indexOf(key) >= 0) { datamap.push({id: key, text: group.label}); @@ -232,6 +234,9 @@ if('civicrm_mailing' === rcpAjaxState.entity) { params["api.MailingRecipients.getcount"] = {}; } + else if ('civicrm_group' === rcpAjaxState.entity) { + params.extra = ["saved_search_id"]; + } return params; }, @@ -255,8 +260,8 @@ text: obj.label } : ''; } else { - return { id: obj.id + ' ' + rcpAjaxState.entity + ' ' + rcpAjaxState.type, - text: obj.label }; + return { id: obj.id + ' ' + rcpAjaxState.entity + ' ' + rcpAjaxState.type, text: obj.label, + is_smart: (!_.isEmpty(obj.extra.saved_search_id)) }; } }) }; diff --git a/tests/phpunit/CRM/Contact/Page/View/UserDashboard/GroupContactTest.php b/tests/phpunit/CRM/Contact/Page/View/UserDashboard/GroupContactTest.php index a88e0774a6f6..8876fccf800c 100644 --- a/tests/phpunit/CRM/Contact/Page/View/UserDashboard/GroupContactTest.php +++ b/tests/phpunit/CRM/Contact/Page/View/UserDashboard/GroupContactTest.php @@ -177,7 +177,7 @@ public function testBrowseDisplaysCorrectListOfAVailableGroups() { $group_id_field_html = $form['group_id']['html']; $this->assertContains($publicSmartGroupTitle, $group_id_field_html, "Group '$publicSmartGroupTitle' should be in listed available groups, but is not."); $this->assertContains($publicStdGroupTitle, $group_id_field_html, "Group '$publicStdGroupTitle' should be in listed available groups, but is not."); - $this->assertNotContains($adminSmartGroupTitle, $group_id_field_html, "Group '$adminSmartGroupTitle' should not be in listed available groups, but is."); + $this->assertNotContains('* ' . $adminSmartGroupTitle, $group_id_field_html, "Group '$adminSmartGroupTitle' should not be in listed available groups, but is."); $this->assertNotContains($adminStdGroupTitle, $group_id_field_html, "Group '$adminStdGroupTitle' should not be in listed available groups, but is."); // Add current user to the test groups. @@ -199,9 +199,9 @@ public function testBrowseDisplaysCorrectListOfAVailableGroups() { $form = CRM_Core_Smarty::singleton()->get_template_vars('form'); $group_id_field_html = $form['group_id']['html']; - $this->assertNotContains($publicSmartGroupTitle, $group_id_field_html, "Group '$publicSmartGroupTitle' should not be in listed available groups, but is."); + $this->assertNotContains('* ' . $publicSmartGroupTitle, $group_id_field_html, "Group '$publicSmartGroupTitle' should not be in listed available groups, but is."); $this->assertNotContains($publicStdGroupTitle, $group_id_field_html, "Group '$publicStdGroupTitle' should not be in listed available groups, but is."); - $this->assertNotContains($adminSmartGroupTitle, $group_id_field_html, "Group '$adminSmartGroupTitle' should not be in listed available groups, but is."); + $this->assertNotContains('* ' . $adminSmartGroupTitle, $group_id_field_html, "Group '$adminSmartGroupTitle' should not be in listed available groups, but is."); $this->assertNotContains($adminStdGroupTitle, $group_id_field_html, "Group '$adminStdGroupTitle' should not be in listed available groups, but is."); }