Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
150f50c1 | 35 | class CRM_Contact_Form_Edit_TagsAndGroups { |
6a488035 TO |
36 | |
37 | /** | |
38 | * constant to determine which forms we are generating | |
39 | * | |
40 | * Used by both profile and edit contact | |
41 | */ | |
42 | CONST GROUP = 1, TAG = 2, ALL = 3; | |
43 | ||
44 | /** | |
45 | * This function is to build form elements | |
46 | * params object $form object of the form | |
47 | * | |
77b97be7 EM |
48 | * @param Object $form the form object that we are operating on |
49 | * @param int $contactId contact id | |
50 | * @param int $type what components are we interested in | |
51 | * @param boolean $visibility visibility of the field | |
52 | * @param null $isRequired | |
53 | * @param string $groupName if used for building group block | |
54 | * @param string $tagName if used for building tag block | |
55 | * @param string $fieldName this is used in batch profile(i.e to build multiple blocks) | |
56 | * | |
57 | * @param string $groupElementType | |
6a488035 TO |
58 | * |
59 | * @static | |
60 | * @access public | |
61 | */ | |
62 | static function buildQuickForm(&$form, | |
150f50c1 CW |
63 | $contactId = 0, |
64 | $type = self::ALL, | |
6a488035 TO |
65 | $visibility = FALSE, |
66 | $isRequired = NULL, | |
150f50c1 CW |
67 | $groupName = 'Group(s)', |
68 | $tagName = 'Tag(s)', | |
c18f95b7 PJ |
69 | $fieldName = NULL, |
70 | $groupElementType = 'checkbox' | |
6a488035 TO |
71 | ) { |
72 | if (!isset($form->_tagGroup)) { | |
73 | $form->_tagGroup = array(); | |
74 | } | |
75 | ||
76 | // NYSS 5670 | |
77 | if (!$contactId && !empty($form->_contactId)) { | |
78 | $contactId = $form->_contactId; | |
79 | } | |
80 | ||
150f50c1 CW |
81 | $type = (int) $type; |
82 | if ($type & self::GROUP) { | |
6a488035 TO |
83 | |
84 | $fName = 'group'; | |
85 | if ($fieldName) { | |
86 | $fName = $fieldName; | |
87 | } | |
88 | ||
6a488035 TO |
89 | $groupID = isset($form->_grid) ? $form->_grid : NULL; |
90 | if ($groupID && $visibility) { | |
f828fa2c | 91 | $ids = array($groupID => $groupID); |
6a488035 TO |
92 | } |
93 | else { | |
94 | if ($visibility) { | |
95 | $group = CRM_Core_PseudoConstant::allGroup(); | |
96 | } | |
97 | else { | |
98 | $group = CRM_Core_PseudoConstant::group(); | |
99 | } | |
f828fa2c | 100 | $ids = $group; |
6a488035 TO |
101 | } |
102 | ||
103 | if ($groupID || !empty($group)) { | |
104 | $groups = CRM_Contact_BAO_Group::getGroupsHierarchy($ids); | |
105 | ||
106 | $attributes['skiplabel'] = TRUE; | |
c18f95b7 PJ |
107 | $elements = array(); |
108 | $groupsOptions = array(); | |
6a488035 TO |
109 | foreach ($groups as $id => $group) { |
110 | // make sure that this group has public visibility | |
111 | if ($visibility && | |
112 | $group['visibility'] == 'User and User Admin Only' | |
113 | ) { | |
114 | continue; | |
115 | } | |
77b97be7 | 116 | |
ab345ca5 | 117 | if ($groupElementType == 'select') { |
c18f95b7 PJ |
118 | $groupsOptions[$id] = $group['title']; |
119 | } | |
120 | else { | |
121 | $form->_tagGroup[$fName][$id]['description'] = $group['description']; | |
122 | $elements[] = &$form->addElement('advcheckbox', $id, NULL, $group['title'], $attributes); | |
123 | } | |
124 | } | |
125 | ||
ab345ca5 | 126 | if ($groupElementType == 'select' && !empty($groupsOptions)) { |
c18f95b7 | 127 | $form->add('select', $fName, ts('%1', array(1 => $groupName)), $groupsOptions, FALSE, |
ab345ca5 | 128 | array('id' => $fName, 'multiple' => 'multiple', 'class' => 'crm-select2') |
c18f95b7 PJ |
129 | ); |
130 | $form->assign('groupCount', count($groupsOptions)); | |
6a488035 TO |
131 | } |
132 | ||
c18f95b7 | 133 | if ($groupElementType == 'checkbox' && !empty($elements)) { |
6a488035 TO |
134 | $form->addGroup($elements, $fName, $groupName, ' <br />'); |
135 | $form->assign('groupCount', count($elements)); | |
136 | if ($isRequired) { | |
137 | $form->addRule($fName, ts('%1 is a required field.', array(1 => $groupName)), 'required'); | |
138 | } | |
139 | } | |
c18f95b7 | 140 | $form->assign('groupElementType', $groupElementType); |
6a488035 TO |
141 | } |
142 | } | |
143 | ||
150f50c1 | 144 | if ($type & self::TAG) { |
6a488035 TO |
145 | $fName = 'tag'; |
146 | if ($fieldName) { | |
147 | $fName = $fieldName; | |
148 | } | |
149 | $form->_tagGroup[$fName] = 1; | |
150 | $elements = array(); | |
151 | $tag = CRM_Core_BAO_Tag::getTags(); | |
152 | ||
153 | foreach ($tag as $id => $name) { | |
154 | $elements[] = $form->createElement('checkbox', $id, NULL, $name); | |
155 | } | |
156 | if (!empty($elements)) { | |
157 | $form->addGroup($elements, $fName, $tagName, '<br />'); | |
158 | $form->assign('tagCount', count($elements)); | |
159 | } | |
160 | ||
161 | if ($isRequired) { | |
162 | $form->addRule($fName, ts('%1 is a required field.', array(1 => $tagName)), 'required'); | |
163 | } | |
164 | ||
165 | // build tag widget | |
166 | $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact'); | |
167 | ||
0cc0faa9 | 168 | CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_contact', $contactId, TRUE, TRUE); |
6a488035 TO |
169 | } |
170 | $form->assign('tagGroup', $form->_tagGroup); | |
171 | } | |
172 | ||
173 | /** | |
174 | * set defaults for relevant form elements | |
175 | * | |
77b97be7 EM |
176 | * @param int $id the contact id |
177 | * @param array $defaults the defaults array to store the values in | |
178 | * @param int $type what components are we interested in | |
6a488035 TO |
179 | * @param string $fieldName this is used in batch profile(i.e to build multiple blocks) |
180 | * | |
77b97be7 EM |
181 | * @param string $groupElementType |
182 | * | |
6a488035 TO |
183 | * @return void |
184 | * @access public | |
185 | * @static | |
186 | */ | |
c18f95b7 | 187 | static function setDefaults($id, &$defaults, $type = self::ALL, $fieldName = NULL, $groupElementType = 'checkbox') { |
6a488035 TO |
188 | $type = (int ) $type; |
189 | if ($type & self::GROUP) { | |
190 | $fName = 'group'; | |
191 | if ($fieldName) { | |
192 | $fName = $fieldName; | |
193 | } | |
194 | ||
195 | $contactGroup = CRM_Contact_BAO_GroupContact::getContactGroup($id, 'Added', NULL, FALSE, TRUE); | |
196 | if ($contactGroup) { | |
197 | foreach ($contactGroup as $group) { | |
ab345ca5 | 198 | if ($groupElementType == 'select') { |
c18f95b7 PJ |
199 | $defaults[$fName][] = $group['group_id']; |
200 | } | |
201 | else { | |
202 | $defaults[$fName . '[' . $group['group_id'] . ']'] = 1; | |
203 | } | |
6a488035 TO |
204 | } |
205 | } | |
206 | } | |
207 | ||
208 | if ($type & self::TAG) { | |
209 | $fName = 'tag'; | |
210 | if ($fieldName) { | |
211 | $fName = $fieldName; | |
212 | } | |
213 | ||
214 | $contactTag = CRM_Core_BAO_EntityTag::getTag($id); | |
215 | if ($contactTag) { | |
216 | foreach ($contactTag as $tag) { | |
217 | $defaults[$fName . '[' . $tag . ']'] = 1; | |
218 | } | |
219 | } | |
220 | } | |
221 | } | |
222 | ||
223 | /** | |
224 | * This function sets the default values for the form. Note that in edit/view mode | |
225 | * the default values are retrieved from the database | |
226 | * | |
227 | * @access public | |
228 | * | |
77b97be7 EM |
229 | * @param $form |
230 | * @param $defaults | |
231 | * | |
355ba699 | 232 | * @return void |
6a488035 TO |
233 | */ |
234 | public static function setDefaultValues(&$form, &$defaults) { | |
235 | $contactEditOptions = $form->get('contactEditOptions'); | |
c18f95b7 | 236 | |
6a488035 TO |
237 | if ($form->_action & CRM_Core_Action::ADD) { |
238 | if (array_key_exists('TagsAndGroups', $contactEditOptions)) { | |
239 | // set group and tag defaults if any | |
240 | if ($form->_gid) { | |
241 | $defaults['group'][$form->_gid] = 1; | |
242 | } | |
243 | if ($form->_tid) { | |
244 | $defaults['tag'][$form->_tid] = 1; | |
245 | } | |
246 | } | |
247 | } | |
248 | else { | |
249 | if (array_key_exists('TagsAndGroups', $contactEditOptions)) { | |
250 | // set the group and tag ids | |
c18f95b7 PJ |
251 | $groupElementType = 'checkbox'; |
252 | if (CRM_Utils_System::getClassName($form) == 'CRM_Contact_Form_Contact') { | |
ab345ca5 | 253 | $groupElementType = 'select'; |
c18f95b7 PJ |
254 | } |
255 | self::setDefaults($form->_contactId, $defaults, self::ALL, NULL, $groupElementType); | |
6a488035 TO |
256 | } |
257 | } | |
258 | } | |
232624b1 | 259 | } |