Merge pull request #4764 from rohankatkar/CRM-15615
[civicrm-core.git] / CRM / Contact / Form / Edit / TagsAndGroups.php
CommitLineData
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 35class CRM_Contact_Form_Edit_TagsAndGroups {
6a488035
TO
36
37 /**
100fef9d 38 * Constant to determine which forms we are generating
6a488035
TO
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 *
c490a46a 48 * @param CRM_Core_Form $form the form object that we are operating on
77b97be7
EM
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, '&nbsp;<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) {
6dac2504
T
145 // CODE FROM CRM/Tag/Form/Tag.php //
146 CRM_Core_Resources::singleton()
147 ->addScriptFile('civicrm', 'packages/jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE)
148 ->addStyleFile('civicrm', 'packages/jquery/plugins/jstree/themes/default/style.css', 0, 'html-header');
149
6a488035
TO
150 $fName = 'tag';
151 if ($fieldName) {
152 $fName = $fieldName;
153 }
154 $form->_tagGroup[$fName] = 1;
6dac2504
T
155
156 // get the list of all the categories
157 $tags = new CRM_Core_BAO_Tag();
158 $tree = $tags->getTree('civicrm_contact', TRUE);
159
6a488035 160 $elements = array();
6dac2504 161 self::climbtree($form, $tree, $elements);
6a488035 162
6dac2504
T
163 $form->addGroup($elements, $fName, $tagName, '<br />');
164 $form->assign('tagCount', count($elements));
165 $form->assign('tree', $tree);
166 $form->assign('tag', $tree);
167 $form->assign('entityID', $contactId);
168 $form->assign('entityTable', 'civicrm_contact');
6a488035
TO
169
170 if ($isRequired) {
171 $form->addRule($fName, ts('%1 is a required field.', array(1 => $tagName)), 'required');
172 }
173
174 // build tag widget
175 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact');
47bc9cec 176 CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_contact', $contactId, FALSE, TRUE);
6a488035
TO
177 }
178 $form->assign('tagGroup', $form->_tagGroup);
179 }
4a679824 180
6dac2504
T
181 static function climbtree($form, $tree, &$elements) {
182 foreach ($tree as $tagID => $varValue) {
183 $tagAttribute = array(
184 'onclick' => "return changeRowColor(\"rowidtag_$tagID\")",
185 'id' => "tag_{$tagID}",
186 );
187
188 $elements[$tagID] = $form->createElement('checkbox', $tagID, '', '', $tagAttribute);
189
190 if (array_key_exists('children', $varValue)) {
191 self::climbtree($form, $varValue['children'], $elements);
192 }
193 }
194 return $elements;
195 }
6a488035
TO
196
197 /**
100fef9d 198 * Set defaults for relevant form elements
6a488035 199 *
77b97be7
EM
200 * @param int $id the contact id
201 * @param array $defaults the defaults array to store the values in
202 * @param int $type what components are we interested in
6a488035
TO
203 * @param string $fieldName this is used in batch profile(i.e to build multiple blocks)
204 *
77b97be7
EM
205 * @param string $groupElementType
206 *
6a488035
TO
207 * @return void
208 * @access public
209 * @static
210 */
c18f95b7 211 static function setDefaults($id, &$defaults, $type = self::ALL, $fieldName = NULL, $groupElementType = 'checkbox') {
6a488035
TO
212 $type = (int ) $type;
213 if ($type & self::GROUP) {
214 $fName = 'group';
215 if ($fieldName) {
216 $fName = $fieldName;
217 }
218
219 $contactGroup = CRM_Contact_BAO_GroupContact::getContactGroup($id, 'Added', NULL, FALSE, TRUE);
220 if ($contactGroup) {
221 foreach ($contactGroup as $group) {
ab345ca5 222 if ($groupElementType == 'select') {
c18f95b7
PJ
223 $defaults[$fName][] = $group['group_id'];
224 }
225 else {
226 $defaults[$fName . '[' . $group['group_id'] . ']'] = 1;
227 }
6a488035
TO
228 }
229 }
230 }
231
232 if ($type & self::TAG) {
233 $fName = 'tag';
234 if ($fieldName) {
235 $fName = $fieldName;
236 }
237
238 $contactTag = CRM_Core_BAO_EntityTag::getTag($id);
239 if ($contactTag) {
240 foreach ($contactTag as $tag) {
241 $defaults[$fName . '[' . $tag . ']'] = 1;
242 }
243 }
244 }
245 }
246
247 /**
c490a46a 248 * Set default values for the form. Note that in edit/view mode
6a488035
TO
249 * the default values are retrieved from the database
250 *
251 * @access public
252 *
c490a46a 253 * @param CRM_Core_Form $form
77b97be7
EM
254 * @param $defaults
255 *
355ba699 256 * @return void
6a488035
TO
257 */
258 public static function setDefaultValues(&$form, &$defaults) {
259 $contactEditOptions = $form->get('contactEditOptions');
c18f95b7 260
6a488035
TO
261 if ($form->_action & CRM_Core_Action::ADD) {
262 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
263 // set group and tag defaults if any
264 if ($form->_gid) {
265 $defaults['group'][$form->_gid] = 1;
266 }
267 if ($form->_tid) {
268 $defaults['tag'][$form->_tid] = 1;
269 }
270 }
271 }
272 else {
273 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
274 // set the group and tag ids
c18f95b7
PJ
275 $groupElementType = 'checkbox';
276 if (CRM_Utils_System::getClassName($form) == 'CRM_Contact_Form_Contact') {
ab345ca5 277 $groupElementType = 'select';
c18f95b7
PJ
278 }
279 self::setDefaults($form->_contactId, $defaults, self::ALL, NULL, $groupElementType);
6a488035
TO
280 }
281 }
282 }
232624b1 283}