CRM-16119: Fix incorrect usages of ts().
[civicrm-core.git] / CRM / Contact / Form / Edit / TagsAndGroups.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
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 /**
fe482240 38 * Constant to determine which forms we are generating.
6a488035
TO
39 *
40 * Used by both profile and edit contact
41 */
7da04cde 42 const GROUP = 1, TAG = 2, ALL = 3;
6a488035
TO
43
44 /**
fe482240 45 * build form elements.
6a488035
TO
46 * params object $form object of the form
47 *
77c5b619
TO
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.
77b97be7 56 * @param null $isRequired
77c5b619
TO
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).
77b97be7
EM
63 *
64 * @param string $groupElementType
6a488035 65 *
6a488035 66 */
ae5ffbb7 67 public static function buildQuickForm(
51ccfbbe 68 &$form,
150f50c1
CW
69 $contactId = 0,
70 $type = self::ALL,
6a488035
TO
71 $visibility = FALSE,
72 $isRequired = NULL,
58438e5b
ML
73 $groupName = ts('Group(s)'),
74 $tagName = ts('Tag(s)'),
c18f95b7
PJ
75 $fieldName = NULL,
76 $groupElementType = 'checkbox'
6a488035
TO
77 ) {
78 if (!isset($form->_tagGroup)) {
79 $form->_tagGroup = array();
80 }
81
82 // NYSS 5670
83 if (!$contactId && !empty($form->_contactId)) {
84 $contactId = $form->_contactId;
85 }
86
150f50c1
CW
87 $type = (int) $type;
88 if ($type & self::GROUP) {
6a488035
TO
89
90 $fName = 'group';
91 if ($fieldName) {
92 $fName = $fieldName;
93 }
94
6a488035
TO
95 $groupID = isset($form->_grid) ? $form->_grid : NULL;
96 if ($groupID && $visibility) {
f828fa2c 97 $ids = array($groupID => $groupID);
6a488035
TO
98 }
99 else {
100 if ($visibility) {
101 $group = CRM_Core_PseudoConstant::allGroup();
102 }
103 else {
104 $group = CRM_Core_PseudoConstant::group();
105 }
f828fa2c 106 $ids = $group;
6a488035
TO
107 }
108
109 if ($groupID || !empty($group)) {
110 $groups = CRM_Contact_BAO_Group::getGroupsHierarchy($ids);
111
112 $attributes['skiplabel'] = TRUE;
c18f95b7
PJ
113 $elements = array();
114 $groupsOptions = array();
6a488035
TO
115 foreach ($groups as $id => $group) {
116 // make sure that this group has public visibility
117 if ($visibility &&
118 $group['visibility'] == 'User and User Admin Only'
119 ) {
120 continue;
121 }
77b97be7 122
ab345ca5 123 if ($groupElementType == 'select') {
c18f95b7
PJ
124 $groupsOptions[$id] = $group['title'];
125 }
126 else {
127 $form->_tagGroup[$fName][$id]['description'] = $group['description'];
128 $elements[] = &$form->addElement('advcheckbox', $id, NULL, $group['title'], $attributes);
129 }
130 }
131
ab345ca5 132 if ($groupElementType == 'select' && !empty($groupsOptions)) {
58438e5b 133 $form->add('select', $fName, $groupName, $groupsOptions, FALSE,
ab345ca5 134 array('id' => $fName, 'multiple' => 'multiple', 'class' => 'crm-select2')
c18f95b7
PJ
135 );
136 $form->assign('groupCount', count($groupsOptions));
6a488035
TO
137 }
138
c18f95b7 139 if ($groupElementType == 'checkbox' && !empty($elements)) {
6a488035
TO
140 $form->addGroup($elements, $fName, $groupName, '&nbsp;<br />');
141 $form->assign('groupCount', count($elements));
142 if ($isRequired) {
143 $form->addRule($fName, ts('%1 is a required field.', array(1 => $groupName)), 'required');
144 }
145 }
c18f95b7 146 $form->assign('groupElementType', $groupElementType);
6a488035
TO
147 }
148 }
149
150f50c1 150 if ($type & self::TAG) {
6a488035
TO
151 $fName = 'tag';
152 if ($fieldName) {
153 $fName = $fieldName;
154 }
155 $form->_tagGroup[$fName] = 1;
6dac2504
T
156
157 // get the list of all the categories
158 $tags = new CRM_Core_BAO_Tag();
159 $tree = $tags->getTree('civicrm_contact', TRUE);
d768b780
WA
160 // let's not load jstree if there are not children. This also fixes blank
161 // display at the beginning of checkboxes
a4a56e95 162 $loadJsTree = CRM_Utils_Array::retrieveValueRecursive($tree, 'children');
d768b780 163 $form->assign('loadjsTree', FALSE);
a4a56e95 164 if (!empty($loadJsTree)) {
d768b780
WA
165 // CODE FROM CRM/Tag/Form/Tag.php //
166 CRM_Core_Resources::singleton()
167 ->addScriptFile('civicrm', 'packages/jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE)
168 ->addStyleFile('civicrm', 'packages/jquery/plugins/jstree/themes/default/style.css', 0, 'html-header');
169 $form->assign('loadjsTree', TRUE);
170 }
6dac2504 171
6a488035 172 $elements = array();
6dac2504 173 self::climbtree($form, $tree, $elements);
6a488035 174
6dac2504
T
175 $form->addGroup($elements, $fName, $tagName, '<br />');
176 $form->assign('tagCount', count($elements));
177 $form->assign('tree', $tree);
178 $form->assign('tag', $tree);
179 $form->assign('entityID', $contactId);
180 $form->assign('entityTable', 'civicrm_contact');
6a488035
TO
181
182 if ($isRequired) {
183 $form->addRule($fName, ts('%1 is a required field.', array(1 => $tagName)), 'required');
184 }
185
186 // build tag widget
187 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact');
47bc9cec 188 CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_contact', $contactId, FALSE, TRUE);
6a488035
TO
189 }
190 $form->assign('tagGroup', $form->_tagGroup);
191 }
4a679824 192
2e2605fe
EM
193 /**
194 * Climb tree.
195 *
196 * @param $form
197 * @param $tree
198 * @param $elements
199 *
200 * @return mixed
201 */
00be9182 202 public static function climbtree($form, $tree, &$elements) {
6dac2504
T
203 foreach ($tree as $tagID => $varValue) {
204 $tagAttribute = array(
353ffa53
TO
205 'onclick' => "return changeRowColor(\"rowidtag_$tagID\")",
206 'id' => "tag_{$tagID}",
6dac2504
T
207 );
208
209 $elements[$tagID] = $form->createElement('checkbox', $tagID, '', '', $tagAttribute);
210
211 if (array_key_exists('children', $varValue)) {
212 self::climbtree($form, $varValue['children'], $elements);
213 }
214 }
51ccfbbe 215 return $elements;
6dac2504 216 }
6a488035
TO
217
218 /**
fe482240 219 * Set defaults for relevant form elements.
6a488035 220 *
77c5b619
TO
221 * @param int $id
222 * The contact id.
223 * @param array $defaults
224 * The defaults array to store the values in.
225 * @param int $type
226 * What components are we interested in.
227 * @param string $fieldName
228 * This is used in batch profile(i.e to build multiple blocks).
6a488035 229 *
77b97be7
EM
230 * @param string $groupElementType
231 *
6a488035 232 * @return void
6a488035 233 */
00be9182 234 public static function setDefaults($id, &$defaults, $type = self::ALL, $fieldName = NULL, $groupElementType = 'checkbox') {
6a488035
TO
235 $type = (int ) $type;
236 if ($type & self::GROUP) {
237 $fName = 'group';
238 if ($fieldName) {
239 $fName = $fieldName;
240 }
241
242 $contactGroup = CRM_Contact_BAO_GroupContact::getContactGroup($id, 'Added', NULL, FALSE, TRUE);
243 if ($contactGroup) {
244 foreach ($contactGroup as $group) {
ab345ca5 245 if ($groupElementType == 'select') {
c18f95b7
PJ
246 $defaults[$fName][] = $group['group_id'];
247 }
248 else {
249 $defaults[$fName . '[' . $group['group_id'] . ']'] = 1;
250 }
6a488035
TO
251 }
252 }
253 }
254
255 if ($type & self::TAG) {
256 $fName = 'tag';
257 if ($fieldName) {
258 $fName = $fieldName;
259 }
260
261 $contactTag = CRM_Core_BAO_EntityTag::getTag($id);
262 if ($contactTag) {
263 foreach ($contactTag as $tag) {
264 $defaults[$fName . '[' . $tag . ']'] = 1;
265 }
266 }
267 }
268 }
269
270 /**
c490a46a 271 * Set default values for the form. Note that in edit/view mode
6a488035
TO
272 * the default values are retrieved from the database
273 *
6a488035 274 *
c490a46a 275 * @param CRM_Core_Form $form
77b97be7
EM
276 * @param $defaults
277 *
355ba699 278 * @return void
6a488035
TO
279 */
280 public static function setDefaultValues(&$form, &$defaults) {
281 $contactEditOptions = $form->get('contactEditOptions');
c18f95b7 282
6a488035
TO
283 if ($form->_action & CRM_Core_Action::ADD) {
284 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
285 // set group and tag defaults if any
286 if ($form->_gid) {
287 $defaults['group'][$form->_gid] = 1;
288 }
289 if ($form->_tid) {
290 $defaults['tag'][$form->_tid] = 1;
291 }
292 }
293 }
294 else {
295 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
296 // set the group and tag ids
c18f95b7
PJ
297 $groupElementType = 'checkbox';
298 if (CRM_Utils_System::getClassName($form) == 'CRM_Contact_Form_Contact') {
ab345ca5 299 $groupElementType = 'select';
c18f95b7
PJ
300 }
301 self::setDefaults($form->_contactId, $defaults, self::ALL, NULL, $groupElementType);
6a488035
TO
302 }
303 }
304 }
96025800 305
232624b1 306}