3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Contact_Form_Edit_TagsAndGroups
{
20 * Constant to determine which forms we are generating.
22 * Used by both profile and edit contact
24 const GROUP
= 1, TAG
= 2, ALL
= 3;
27 * Build form elements.
29 * @param CRM_Core_Form $form
30 * The form object that we are operating on.
31 * @param int $contactId
34 * What components are we interested in.
35 * @param bool $visibility
36 * Visibility of the field.
37 * @param null $isRequired
38 * @param string $groupName
39 * If used for building group block.
40 * @param string $tagName
41 * If used for building tag block.
42 * @param string $fieldName
43 * This is used in batch profile(i.e to build multiple blocks).
45 * @param string $groupElementType
48 public static function buildQuickForm(
54 $groupName = 'Group(s)',
57 $groupElementType = 'checkbox'
59 if (!isset($form->_tagGroup
)) {
60 $form->_tagGroup
= [];
64 if (!$contactId && !empty($form->_contactId
)) {
65 $contactId = $form->_contactId
;
69 if ($type & self
::GROUP
) {
76 $groupID = $form->_grid ??
NULL;
77 if ($groupID && $visibility) {
78 $ids = [$groupID => $groupID];
82 $group = CRM_Core_PseudoConstant
::allGroup();
85 $group = CRM_Core_PseudoConstant
::group();
90 if ($groupID ||
!empty($group)) {
91 $groups = CRM_Contact_BAO_Group
::getGroupsHierarchy($ids, NULL, '- ');
93 $attributes['skiplabel'] = TRUE;
96 foreach ($groups as $id => $group) {
97 // make sure that this group has public visibility
99 $group['visibility'] == 'User and User Admin Only'
104 if ($groupElementType == 'select') {
105 $groupsOptions[$id] = $group;
108 $form->_tagGroup
[$fName][$id]['description'] = $group['description'];
109 $elements[] = &$form->addElement('advcheckbox', $id, NULL, $group['text'], $attributes);
113 if ($groupElementType == 'select' && !empty($groupsOptions)) {
114 $form->add('select2', $fName, $groupName, $groupsOptions, FALSE,
115 ['placeholder' => '- select -', 'multiple' => TRUE, 'class' => 'twenty']
117 $form->assign('groupCount', count($groupsOptions));
120 if ($groupElementType == 'checkbox' && !empty($elements)) {
121 $form->addGroup($elements, $fName, $groupName, ' <br />');
122 $form->assign('groupCount', count($elements));
124 $form->addRule($fName, ts('%1 is a required field.', [1 => $groupName]), 'required');
127 $form->assign('groupElementType', $groupElementType);
131 if ($type & self
::TAG
) {
132 $tags = CRM_Core_BAO_Tag
::getColorTags('civicrm_contact');
135 $form->add('select2', 'tag', ts('Tag(s)'), $tags, FALSE, ['class' => 'huge', 'placeholder' => ts('- select -'), 'multiple' => TRUE]);
139 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_contact');
140 CRM_Core_Form_Tag
::buildQuickForm($form, $parentNames, 'civicrm_contact', $contactId, FALSE, TRUE);
142 $form->assign('tagGroup', $form->_tagGroup
);
146 * Set defaults for relevant form elements.
150 * @param array $defaults
151 * The defaults array to store the values in.
153 * What components are we interested in.
154 * @param string $fieldName
155 * This is used in batch profile(i.e to build multiple blocks).
157 * @param string $groupElementType
159 public static function setDefaults($id, &$defaults, $type = self
::ALL
, $fieldName = NULL, $groupElementType = 'checkbox') {
160 $type = (int ) $type;
161 if ($type & self
::GROUP
) {
167 $contactGroup = CRM_Contact_BAO_GroupContact
::getContactGroup($id, 'Added', NULL, FALSE, TRUE);
169 if ($groupElementType == 'select') {
170 $defaults[$fName] = implode(',', CRM_Utils_Array
::collect('group_id', $contactGroup));
173 foreach ($contactGroup as $group) {
174 $defaults[$fName . '[' . $group['group_id'] . ']'] = 1;
180 if ($type & self
::TAG
) {
181 $defaults['tag'] = implode(',', CRM_Core_BAO_EntityTag
::getTag($id, 'civicrm_contact'));
186 * Set default values for the form. Note that in edit/view mode
187 * the default values are retrieved from the database
190 * @param CRM_Core_Form $form
191 * @param array $defaults
193 public static function setDefaultValues(&$form, &$defaults) {
194 $contactEditOptions = $form->get('contactEditOptions');
196 if ($form->_action
& CRM_Core_Action
::ADD
) {
197 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
198 // set group and tag defaults if any
200 $defaults['group'][$form->_gid
] = 1;
203 $defaults['tag'][$form->_tid
] = 1;
208 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
209 // set the group and tag ids
210 $groupElementType = 'checkbox';
211 if (CRM_Utils_System
::getClassName($form) == 'CRM_Contact_Form_Contact') {
212 $groupElementType = 'select';
214 self
::setDefaults($form->_contactId
, $defaults, self
::ALL
, NULL, $groupElementType);