f06a3e55cae79c574fc8cd99ed28548ab9dde4e5
[civicrm-core.git] / CRM / Contact / Form / Edit / TagsAndGroups.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Contact_Form_Edit_TagsAndGroups {
18
19 /**
20 * Constant to determine which forms we are generating.
21 *
22 * Used by both profile and edit contact
23 */
24 const GROUP = 1, TAG = 2, ALL = 3;
25
26 /**
27 * Build form elements.
28 *
29 * @param CRM_Core_Form $form
30 * The form object that we are operating on.
31 * @param int $contactId
32 * Contact id.
33 * @param int $type
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).
44 * @param string $groupElementType
45 * The html type of the element we are adding e.g. checkbox, select
46 * @param bool $public
47 * Is this being used in a public form e.g. Profile.
48 */
49 public static function buildQuickForm(
50 &$form,
51 $contactId = 0,
52 $type = self::ALL,
53 $visibility = FALSE,
54 $isRequired = NULL,
55 $groupName = 'Group(s)',
56 $tagName = 'Tag(s)',
57 $fieldName = NULL,
58 $groupElementType = 'checkbox',
59 $public = FALSE
60 ) {
61 if (!isset($form->_tagGroup)) {
62 $form->_tagGroup = [];
63 }
64 $form->addExpectedSmartyVariable('type');
65 $form->addOptionalQuickFormElement('group');
66 // NYSS 5670
67 if (!$contactId && !empty($form->_contactId)) {
68 $contactId = $form->_contactId;
69 }
70
71 $type = (int) $type;
72 if ($type & self::GROUP) {
73
74 $fName = 'group';
75 if ($fieldName) {
76 $fName = $fieldName;
77 }
78
79 $groupID = $form->_grid ?? NULL;
80 if ($groupID && $visibility) {
81 $ids = [$groupID => $groupID];
82 }
83 else {
84 if ($visibility) {
85 $group = CRM_Core_PseudoConstant::allGroup();
86 }
87 else {
88 $group = CRM_Core_PseudoConstant::group();
89 }
90 $ids = $group;
91 }
92
93 if ($groupID || !empty($group)) {
94 $groups = CRM_Contact_BAO_Group::getGroupsHierarchy($ids, NULL, '- ', FALSE, $public);
95
96 $attributes['skiplabel'] = TRUE;
97 $elements = [];
98 $groupsOptions = [];
99 foreach ($groups as $key => $group) {
100 $id = $group['id'];
101 // make sure that this group has public visibility
102 if ($visibility &&
103 $group['visibility'] === 'User and User Admin Only'
104 ) {
105 continue;
106 }
107
108 if ($groupElementType === 'select') {
109 $groupsOptions[$key] = $group;
110 }
111 else {
112 $form->_tagGroup[$fName][$id]['description'] = $group['description'];
113 $elements[] = &$form->addElement('advcheckbox', $id, NULL, $group['text'], $attributes);
114 }
115 }
116
117 if ($groupElementType === 'select' && !empty($groupsOptions)) {
118 $form->add('select2', $fName, $groupName, $groupsOptions, FALSE,
119 ['placeholder' => ts('- select -'), 'multiple' => TRUE, 'class' => 'twenty']
120 );
121 $form->assign('groupCount', count($groupsOptions));
122 }
123
124 if ($groupElementType === 'checkbox' && !empty($elements)) {
125 $form->addGroup($elements, $fName, $groupName, '&nbsp;<br />');
126 $form->assign('groupCount', count($elements));
127 if ($isRequired) {
128 $form->addRule($fName, ts('%1 is a required field.', [1 => $groupName]), 'required');
129 }
130 }
131 }
132 }
133 $form->assign('groupElementType', $groupElementType ?? NULL);
134
135 if ($type & self::TAG) {
136 $tags = CRM_Core_BAO_Tag::getColorTags('civicrm_contact');
137
138 if (!empty($tags)) {
139 $form->add('select2', 'tag', ts('Tag(s)'), $tags, $isRequired, ['class' => 'huge', 'placeholder' => ts('- select -'), 'multiple' => TRUE]);
140 }
141
142 // build tag widget
143 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact');
144 CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_contact', $contactId, FALSE, TRUE);
145 }
146 $form->assign('tagGroup', $form->_tagGroup);
147 }
148
149 /**
150 * Set defaults for relevant form elements.
151 *
152 * @param int $id
153 * The contact id.
154 * @param array $defaults
155 * The defaults array to store the values in.
156 * @param int $type
157 * What components are we interested in.
158 * @param string $fieldName
159 * This is used in batch profile(i.e to build multiple blocks).
160 *
161 * @param string $groupElementType
162 */
163 public static function setDefaults($id, &$defaults, $type = self::ALL, $fieldName = NULL, $groupElementType = 'checkbox') {
164 $type = (int ) $type;
165 if ($type & self::GROUP) {
166 $fName = 'group';
167 if ($fieldName) {
168 $fName = $fieldName;
169 }
170
171 $contactGroup = CRM_Contact_BAO_GroupContact::getContactGroup($id, 'Added', NULL, FALSE, TRUE);
172 if ($contactGroup) {
173 if ($groupElementType == 'select') {
174 $defaults[$fName] = implode(',', CRM_Utils_Array::collect('group_id', $contactGroup));
175 }
176 else {
177 foreach ($contactGroup as $group) {
178 $defaults[$fName . '[' . $group['group_id'] . ']'] = 1;
179 }
180 }
181 }
182 }
183
184 if ($type & self::TAG) {
185 $defaults['tag'] = implode(',', CRM_Core_BAO_EntityTag::getTag($id, 'civicrm_contact'));
186 }
187 }
188
189 /**
190 * Set default values for the form. Note that in edit/view mode
191 * the default values are retrieved from the database
192 *
193 *
194 * @param CRM_Core_Form $form
195 * @param array $defaults
196 */
197 public static function setDefaultValues(&$form, &$defaults) {
198 $contactEditOptions = $form->get('contactEditOptions');
199
200 if ($form->_action & CRM_Core_Action::ADD) {
201 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
202 // set group and tag defaults if any
203 if ($form->_gid) {
204 $defaults['group'][$form->_gid] = 1;
205 }
206 if ($form->_tid) {
207 $defaults['tag'][$form->_tid] = 1;
208 }
209 }
210 }
211 else {
212 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
213 // set the group and tag ids
214 $groupElementType = 'checkbox';
215 if (CRM_Utils_System::getClassName($form) == 'CRM_Contact_Form_Contact') {
216 $groupElementType = 'select';
217 }
218 self::setDefaults($form->_contactId, $defaults, self::ALL, NULL, $groupElementType);
219 }
220 }
221 }
222
223 }