From c18f95b7c63778e409c827fa12fcaf2cd5c72890 Mon Sep 17 00:00:00 2001 From: Pratik Joshi Date: Mon, 15 Jul 2013 19:06:38 +0530 Subject: [PATCH] CRM-13025 --- CRM/Contact/Form/Contact.php | 33 +++++++++----- CRM/Contact/Form/Edit/TagsAndGroups.php | 45 ++++++++++++++----- .../CRM/Contact/Form/Edit/TagsAndGroups.tpl | 14 ++++++ 3 files changed, 72 insertions(+), 20 deletions(-) diff --git a/CRM/Contact/Form/Contact.php b/CRM/Contact/Form/Contact.php index 00fe65e9a4..184f63bfaa 100644 --- a/CRM/Contact/Form/Contact.php +++ b/CRM/Contact/Form/Contact.php @@ -404,7 +404,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form { if (array_key_exists('TagsAndGroups', $this->_editOptions)) { // set group and tag defaults if any if ($this->_gid) { - $defaults['group'][$this->_gid] = 1; + $defaults['group'][] = $this->_gid; } if ($this->_tid) { $defaults['tag'][$this->_tid] = 1; @@ -799,10 +799,17 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form { $this->_blocks['Address'] = $this->_editOptions['Address']; continue; } + if ($name == 'TagsAndGroups') { + continue; + } $className = 'CRM_Contact_Form_Edit_' . $name; $className::buildQuickForm($this); } + // build tags and groups + CRM_Contact_Form_Edit_TagsAndGroups::buildQuickForm($this, 0, CRM_Contact_Form_Edit_TagsAndGroups::ALL, + FALSE, NULL, 'Group(s)', 'Tag(s)', NULL, 'crmasmSelect'); + // build location blocks. CRM_Contact_Form_Edit_Lock::buildQuickForm($this); CRM_Contact_Form_Location::buildQuickForm($this); @@ -871,6 +878,14 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form { //get the submitted values in an array $params = $this->controller->exportValues($this->_name); + $group = CRM_Utils_Array::value('group', $params); + if ($group && is_array($group)) { + unset($params['group']); + foreach ($group as $key => $value) { + $params['group'][$value] = 1; + } + } + CRM_Contact_BAO_Contact_Optimizer::edit( $params, $this->_preEditValues ); if (CRM_Utils_Array::value('image_URL', $params)) { @@ -959,15 +974,13 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form { if (CRM_Utils_Array::value('contact_id', $params) && ($this->_action & CRM_Core_Action::UPDATE)) { // figure out which all groups are intended to be removed - if (!empty($params['group'])) { - $contactGroupList = CRM_Contact_BAO_GroupContact::getContactGroup($params['contact_id'], 'Added'); - if (is_array($contactGroupList)) { - foreach ($contactGroupList as $key) { - if ($params['group'][$key['group_id']] != 1 && - !CRM_Utils_Array::value('is_hidden', $key) - ) { - $params['group'][$key['group_id']] = -1; - } + $contactGroupList = CRM_Contact_BAO_GroupContact::getContactGroup($params['contact_id'], 'Added'); + if (is_array($contactGroupList)) { + foreach ($contactGroupList as $key) { + if ((!array_key_exists($key['group_id'], $params['group']) || $params['group'][$key['group_id']] != 1) + && !CRM_Utils_Array::value('is_hidden', $key) + ) { + $params['group'][$key['group_id']] = -1; } } } diff --git a/CRM/Contact/Form/Edit/TagsAndGroups.php b/CRM/Contact/Form/Edit/TagsAndGroups.php index a7bb9d466c..f6d22fe806 100644 --- a/CRM/Contact/Form/Edit/TagsAndGroups.php +++ b/CRM/Contact/Form/Edit/TagsAndGroups.php @@ -63,7 +63,8 @@ class CRM_Contact_Form_Edit_TagsAndGroups { $isRequired = NULL, $groupName = 'Group(s)', $tagName = 'Tag(s)', - $fieldName = NULL + $fieldName = NULL, + $groupElementType = 'checkbox' ) { if (!isset($form->_tagGroup)) { $form->_tagGroup = array(); @@ -82,7 +83,6 @@ class CRM_Contact_Form_Edit_TagsAndGroups { $fName = $fieldName; } - $elements = array(); $groupID = isset($form->_grid) ? $form->_grid : NULL; if ($groupID && $visibility) { $ids = array($groupID => $groupID); @@ -101,6 +101,8 @@ class CRM_Contact_Form_Edit_TagsAndGroups { $groups = CRM_Contact_BAO_Group::getGroupsHierarchy($ids); $attributes['skiplabel'] = TRUE; + $elements = array(); + $groupsOptions = array(); foreach ($groups as $id => $group) { // make sure that this group has public visibility if ($visibility && @@ -108,17 +110,31 @@ class CRM_Contact_Form_Edit_TagsAndGroups { ) { continue; } - $form->_tagGroup[$fName][$id]['description'] = $group['description']; - $elements[] = &$form->addElement('advcheckbox', $id, NULL, $group['title'], $attributes); + + if ($groupElementType == 'crmasmSelect') { + $groupsOptions[$id] = $group['title']; + } + else { + $form->_tagGroup[$fName][$id]['description'] = $group['description']; + $elements[] = &$form->addElement('advcheckbox', $id, NULL, $group['title'], $attributes); + } + } + + if ($groupElementType == 'crmasmSelect' && !empty($groupsOptions)) { + $form->add('select', $fName, ts('%1', array(1 => $groupName)), $groupsOptions, FALSE, + array('id' => $fName, 'multiple' => 'multiple', 'title' => ts('- select -')) + ); + $form->assign('groupCount', count($groupsOptions)); } - if (!empty($elements)) { + if ($groupElementType == 'checkbox' && !empty($elements)) { $form->addGroup($elements, $fName, $groupName, ' 
'); $form->assign('groupCount', count($elements)); if ($isRequired) { $form->addRule($fName, ts('%1 is a required field.', array(1 => $groupName)), 'required'); } } + $form->assign('groupElementType', $groupElementType); } } @@ -163,7 +179,7 @@ class CRM_Contact_Form_Edit_TagsAndGroups { * @access public * @static */ - static function setDefaults($id, &$defaults, $type = self::ALL, $fieldName = NULL) { + static function setDefaults($id, &$defaults, $type = self::ALL, $fieldName = NULL, $groupElementType = 'checkbox') { $type = (int ) $type; if ($type & self::GROUP) { $fName = 'group'; @@ -174,7 +190,12 @@ class CRM_Contact_Form_Edit_TagsAndGroups { $contactGroup = CRM_Contact_BAO_GroupContact::getContactGroup($id, 'Added', NULL, FALSE, TRUE); if ($contactGroup) { foreach ($contactGroup as $group) { - $defaults[$fName . '[' . $group['group_id'] . ']'] = 1; + if ($groupElementType == 'crmasmSelect') { + $defaults[$fName][] = $group['group_id']; + } + else { + $defaults[$fName . '[' . $group['group_id'] . ']'] = 1; + } } } } @@ -204,6 +225,7 @@ class CRM_Contact_Form_Edit_TagsAndGroups { */ public static function setDefaultValues(&$form, &$defaults) { $contactEditOptions = $form->get('contactEditOptions'); + if ($form->_action & CRM_Core_Action::ADD) { if (array_key_exists('TagsAndGroups', $contactEditOptions)) { // set group and tag defaults if any @@ -218,9 +240,12 @@ class CRM_Contact_Form_Edit_TagsAndGroups { else { if (array_key_exists('TagsAndGroups', $contactEditOptions)) { // set the group and tag ids - self::setDefaults($form->_contactId, $defaults, self::ALL); + $groupElementType = 'checkbox'; + if (CRM_Utils_System::getClassName($form) == 'CRM_Contact_Form_Contact') { + $groupElementType = 'crmasmSelect'; + } + self::setDefaults($form->_contactId, $defaults, self::ALL, NULL, $groupElementType); } } } -} - +} \ No newline at end of file diff --git a/templates/CRM/Contact/Form/Edit/TagsAndGroups.tpl b/templates/CRM/Contact/Form/Edit/TagsAndGroups.tpl index 553c307a9f..a89210c150 100644 --- a/templates/CRM/Contact/Form/Edit/TagsAndGroups.tpl +++ b/templates/CRM/Contact/Form/Edit/TagsAndGroups.tpl @@ -30,6 +30,20 @@ {/if} + {if $groupElementType eq 'crmasmSelect'} + + {/if} {foreach key=key item=item from=$tagGroup} {* $type assigned from dynamic.tpl *} {if !$type || $type eq $key } -- 2.25.1
{if $title}{$form.group.label}{/if} + {$form.group.html} + {literal} + + {/literal} +