Display public title and description on profiles and unsubscribe/subscribe forms...
[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
65 // NYSS 5670
66 if (!$contactId && !empty($form->_contactId)) {
67 $contactId = $form->_contactId;
68 }
69
70 $type = (int) $type;
71 if ($type & self::GROUP) {
72
73 $fName = 'group';
74 if ($fieldName) {
75 $fName = $fieldName;
76 }
77
78 $groupID = $form->_grid ?? NULL;
79 if ($groupID && $visibility) {
80 $ids = [$groupID => $groupID];
81 }
82 else {
83 if ($visibility) {
84 $group = CRM_Core_PseudoConstant::allGroup();
85 }
86 else {
87 $group = CRM_Core_PseudoConstant::group();
88 }
89 $ids = $group;
90 }
91
92 if ($groupID || !empty($group)) {
93 $groups = CRM_Contact_BAO_Group::getGroupsHierarchy($ids, NULL, '- ', FALSE, $public);
94
95 $attributes['skiplabel'] = TRUE;
96 $elements = [];
97 $groupsOptions = [];
98 foreach ($groups as $id => $group) {
99 // make sure that this group has public visibility
100 if ($visibility &&
101 $group['visibility'] == 'User and User Admin Only'
102 ) {
103 continue;
104 }
105
106 if ($groupElementType == 'select') {
107 $groupsOptions[$id] = $group;
108 }
109 else {
110 $form->_tagGroup[$fName][$id]['description'] = $group['description'];
111 $elements[] = &$form->addElement('advcheckbox', $id, NULL, $group['text'], $attributes);
112 }
113 }
114
115 if ($groupElementType == 'select' && !empty($groupsOptions)) {
116 $form->add('select2', $fName, $groupName, $groupsOptions, FALSE,
117 ['placeholder' => '- select -', 'multiple' => TRUE, 'class' => 'twenty']
118 );
119 $form->assign('groupCount', count($groupsOptions));
120 }
121
122 if ($groupElementType == 'checkbox' && !empty($elements)) {
123 $form->addGroup($elements, $fName, $groupName, '&nbsp;<br />');
124 $form->assign('groupCount', count($elements));
125 if ($isRequired) {
126 $form->addRule($fName, ts('%1 is a required field.', [1 => $groupName]), 'required');
127 }
128 }
129 $form->assign('groupElementType', $groupElementType);
130 }
131 }
132
133 if ($type & self::TAG) {
134 $tags = CRM_Core_BAO_Tag::getColorTags('civicrm_contact');
135
136 if (!empty($tags)) {
137 $form->add('select2', 'tag', ts('Tag(s)'), $tags, FALSE, ['class' => 'huge', 'placeholder' => ts('- select -'), 'multiple' => TRUE]);
138 }
139
140 // build tag widget
141 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact');
142 CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_contact', $contactId, FALSE, TRUE);
143 }
144 $form->assign('tagGroup', $form->_tagGroup);
145 }
146
147 /**
148 * Set defaults for relevant form elements.
149 *
150 * @param int $id
151 * The contact id.
152 * @param array $defaults
153 * The defaults array to store the values in.
154 * @param int $type
155 * What components are we interested in.
156 * @param string $fieldName
157 * This is used in batch profile(i.e to build multiple blocks).
158 *
159 * @param string $groupElementType
160 */
161 public static function setDefaults($id, &$defaults, $type = self::ALL, $fieldName = NULL, $groupElementType = 'checkbox') {
162 $type = (int ) $type;
163 if ($type & self::GROUP) {
164 $fName = 'group';
165 if ($fieldName) {
166 $fName = $fieldName;
167 }
168
169 $contactGroup = CRM_Contact_BAO_GroupContact::getContactGroup($id, 'Added', NULL, FALSE, TRUE);
170 if ($contactGroup) {
171 if ($groupElementType == 'select') {
172 $defaults[$fName] = implode(',', CRM_Utils_Array::collect('group_id', $contactGroup));
173 }
174 else {
175 foreach ($contactGroup as $group) {
176 $defaults[$fName . '[' . $group['group_id'] . ']'] = 1;
177 }
178 }
179 }
180 }
181
182 if ($type & self::TAG) {
183 $defaults['tag'] = implode(',', CRM_Core_BAO_EntityTag::getTag($id, 'civicrm_contact'));
184 }
185 }
186
187 /**
188 * Set default values for the form. Note that in edit/view mode
189 * the default values are retrieved from the database
190 *
191 *
192 * @param CRM_Core_Form $form
193 * @param array $defaults
194 */
195 public static function setDefaultValues(&$form, &$defaults) {
196 $contactEditOptions = $form->get('contactEditOptions');
197
198 if ($form->_action & CRM_Core_Action::ADD) {
199 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
200 // set group and tag defaults if any
201 if ($form->_gid) {
202 $defaults['group'][$form->_gid] = 1;
203 }
204 if ($form->_tid) {
205 $defaults['tag'][$form->_tid] = 1;
206 }
207 }
208 }
209 else {
210 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
211 // set the group and tag ids
212 $groupElementType = 'checkbox';
213 if (CRM_Utils_System::getClassName($form) == 'CRM_Contact_Form_Contact') {
214 $groupElementType = 'select';
215 }
216 self::setDefaults($form->_contactId, $defaults, self::ALL, NULL, $groupElementType);
217 }
218 }
219 }
220
221 }