INFRA-132 - CRM/Contact - Misc
[civicrm-core.git] / CRM / Contact / Form / Edit / TagsAndGroups.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35 class CRM_Contact_Form_Edit_TagsAndGroups {
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 *
48 * @param CRM_Core_Form $form
49 * The form object that we are operating on.
50 * @param int $contactId
51 * Contact id.
52 * @param int $type
53 * What components are we interested in.
54 * @param bool $visibility
55 * Visibility of the field.
56 * @param null $isRequired
57 * @param string $groupName
58 * If used for building group block.
59 * @param string $tagName
60 * If used for building tag block.
61 * @param string $fieldName
62 * This is used in batch profile(i.e to build multiple blocks).
63 *
64 * @param string $groupElementType
65 *
66 * @static
67 */
68 static function buildQuickForm(
69 &$form,
70 $contactId = 0,
71 $type = self::ALL,
72 $visibility = FALSE,
73 $isRequired = NULL,
74 $groupName = 'Group(s)',
75 $tagName = 'Tag(s)',
76 $fieldName = NULL,
77 $groupElementType = 'checkbox'
78 ) {
79 if (!isset($form->_tagGroup)) {
80 $form->_tagGroup = array();
81 }
82
83 // NYSS 5670
84 if (!$contactId && !empty($form->_contactId)) {
85 $contactId = $form->_contactId;
86 }
87
88 $type = (int) $type;
89 if ($type & self::GROUP) {
90
91 $fName = 'group';
92 if ($fieldName) {
93 $fName = $fieldName;
94 }
95
96 $groupID = isset($form->_grid) ? $form->_grid : NULL;
97 if ($groupID && $visibility) {
98 $ids = array($groupID => $groupID);
99 }
100 else {
101 if ($visibility) {
102 $group = CRM_Core_PseudoConstant::allGroup();
103 }
104 else {
105 $group = CRM_Core_PseudoConstant::group();
106 }
107 $ids = $group;
108 }
109
110 if ($groupID || !empty($group)) {
111 $groups = CRM_Contact_BAO_Group::getGroupsHierarchy($ids);
112
113 $attributes['skiplabel'] = TRUE;
114 $elements = array();
115 $groupsOptions = array();
116 foreach ($groups as $id => $group) {
117 // make sure that this group has public visibility
118 if ($visibility &&
119 $group['visibility'] == 'User and User Admin Only'
120 ) {
121 continue;
122 }
123
124 if ($groupElementType == 'select') {
125 $groupsOptions[$id] = $group['title'];
126 }
127 else {
128 $form->_tagGroup[$fName][$id]['description'] = $group['description'];
129 $elements[] = &$form->addElement('advcheckbox', $id, NULL, $group['title'], $attributes);
130 }
131 }
132
133 if ($groupElementType == 'select' && !empty($groupsOptions)) {
134 $form->add('select', $fName, ts('%1', array(1 => $groupName)), $groupsOptions, FALSE,
135 array('id' => $fName, 'multiple' => 'multiple', 'class' => 'crm-select2')
136 );
137 $form->assign('groupCount', count($groupsOptions));
138 }
139
140 if ($groupElementType == 'checkbox' && !empty($elements)) {
141 $form->addGroup($elements, $fName, $groupName, '&nbsp;<br />');
142 $form->assign('groupCount', count($elements));
143 if ($isRequired) {
144 $form->addRule($fName, ts('%1 is a required field.', array(1 => $groupName)), 'required');
145 }
146 }
147 $form->assign('groupElementType', $groupElementType);
148 }
149 }
150
151 if ($type & self::TAG) {
152 // CODE FROM CRM/Tag/Form/Tag.php //
153 CRM_Core_Resources::singleton()
154 ->addScriptFile('civicrm', 'packages/jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE)
155 ->addStyleFile('civicrm', 'packages/jquery/plugins/jstree/themes/default/style.css', 0, 'html-header');
156
157 $fName = 'tag';
158 if ($fieldName) {
159 $fName = $fieldName;
160 }
161 $form->_tagGroup[$fName] = 1;
162
163 // get the list of all the categories
164 $tags = new CRM_Core_BAO_Tag();
165 $tree = $tags->getTree('civicrm_contact', TRUE);
166
167 $elements = array();
168 self::climbtree($form, $tree, $elements);
169
170 $form->addGroup($elements, $fName, $tagName, '<br />');
171 $form->assign('tagCount', count($elements));
172 $form->assign('tree', $tree);
173 $form->assign('tag', $tree);
174 $form->assign('entityID', $contactId);
175 $form->assign('entityTable', 'civicrm_contact');
176
177 if ($isRequired) {
178 $form->addRule($fName, ts('%1 is a required field.', array(1 => $tagName)), 'required');
179 }
180
181 // build tag widget
182 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact');
183 CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_contact', $contactId, FALSE, TRUE);
184 }
185 $form->assign('tagGroup', $form->_tagGroup);
186 }
187
188 public static function climbtree($form, $tree, &$elements) {
189 foreach ($tree as $tagID => $varValue) {
190 $tagAttribute = array(
191 'onclick' => "return changeRowColor(\"rowidtag_$tagID\")",
192 'id' => "tag_{$tagID}",
193 );
194
195 $elements[$tagID] = $form->createElement('checkbox', $tagID, '', '', $tagAttribute);
196
197 if (array_key_exists('children', $varValue)) {
198 self::climbtree($form, $varValue['children'], $elements);
199 }
200 }
201 return $elements;
202 }
203
204 /**
205 * Set defaults for relevant form elements
206 *
207 * @param int $id
208 * The contact id.
209 * @param array $defaults
210 * The defaults array to store the values in.
211 * @param int $type
212 * What components are we interested in.
213 * @param string $fieldName
214 * This is used in batch profile(i.e to build multiple blocks).
215 *
216 * @param string $groupElementType
217 *
218 * @return void
219 * @static
220 */
221 public static function setDefaults($id, &$defaults, $type = self::ALL, $fieldName = NULL, $groupElementType = 'checkbox') {
222 $type = (int ) $type;
223 if ($type & self::GROUP) {
224 $fName = 'group';
225 if ($fieldName) {
226 $fName = $fieldName;
227 }
228
229 $contactGroup = CRM_Contact_BAO_GroupContact::getContactGroup($id, 'Added', NULL, FALSE, TRUE);
230 if ($contactGroup) {
231 foreach ($contactGroup as $group) {
232 if ($groupElementType == 'select') {
233 $defaults[$fName][] = $group['group_id'];
234 }
235 else {
236 $defaults[$fName . '[' . $group['group_id'] . ']'] = 1;
237 }
238 }
239 }
240 }
241
242 if ($type & self::TAG) {
243 $fName = 'tag';
244 if ($fieldName) {
245 $fName = $fieldName;
246 }
247
248 $contactTag = CRM_Core_BAO_EntityTag::getTag($id);
249 if ($contactTag) {
250 foreach ($contactTag as $tag) {
251 $defaults[$fName . '[' . $tag . ']'] = 1;
252 }
253 }
254 }
255 }
256
257 /**
258 * Set default values for the form. Note that in edit/view mode
259 * the default values are retrieved from the database
260 *
261 *
262 * @param CRM_Core_Form $form
263 * @param $defaults
264 *
265 * @return void
266 */
267 public static function setDefaultValues(&$form, &$defaults) {
268 $contactEditOptions = $form->get('contactEditOptions');
269
270 if ($form->_action & CRM_Core_Action::ADD) {
271 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
272 // set group and tag defaults if any
273 if ($form->_gid) {
274 $defaults['group'][$form->_gid] = 1;
275 }
276 if ($form->_tid) {
277 $defaults['tag'][$form->_tid] = 1;
278 }
279 }
280 }
281 else {
282 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
283 // set the group and tag ids
284 $groupElementType = 'checkbox';
285 if (CRM_Utils_System::getClassName($form) == 'CRM_Contact_Form_Contact') {
286 $groupElementType = 'select';
287 }
288 self::setDefaults($form->_contactId, $defaults, self::ALL, NULL, $groupElementType);
289 }
290 }
291 }
292 }