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