Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-06-25-23-42-42
[civicrm-core.git] / CRM / Contact / Form / Edit / TagsAndGroups.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 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
58 *
59 * @static
60 * @access public
61 */
62 static function buildQuickForm(&$form,
63 $contactId = 0,
64 $type = self::ALL,
65 $visibility = FALSE,
66 $isRequired = NULL,
67 $groupName = 'Group(s)',
68 $tagName = 'Tag(s)',
69 $fieldName = NULL,
70 $groupElementType = 'checkbox'
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
81 $type = (int) $type;
82 if ($type & self::GROUP) {
83
84 $fName = 'group';
85 if ($fieldName) {
86 $fName = $fieldName;
87 }
88
89 $groupID = isset($form->_grid) ? $form->_grid : NULL;
90 if ($groupID && $visibility) {
91 $ids = array($groupID => $groupID);
92 }
93 else {
94 if ($visibility) {
95 $group = CRM_Core_PseudoConstant::allGroup();
96 }
97 else {
98 $group = CRM_Core_PseudoConstant::group();
99 }
100 $ids = $group;
101 }
102
103 if ($groupID || !empty($group)) {
104 $groups = CRM_Contact_BAO_Group::getGroupsHierarchy($ids);
105
106 $attributes['skiplabel'] = TRUE;
107 $elements = array();
108 $groupsOptions = array();
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 }
116
117 if ($groupElementType == 'select') {
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
126 if ($groupElementType == 'select' && !empty($groupsOptions)) {
127 $form->add('select', $fName, ts('%1', array(1 => $groupName)), $groupsOptions, FALSE,
128 array('id' => $fName, 'multiple' => 'multiple', 'class' => 'crm-select2')
129 );
130 $form->assign('groupCount', count($groupsOptions));
131 }
132
133 if ($groupElementType == 'checkbox' && !empty($elements)) {
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 }
140 $form->assign('groupElementType', $groupElementType);
141 }
142 }
143
144 if ($type & self::TAG) {
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
168 CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_contact', $contactId, TRUE, TRUE);
169 }
170 $form->assign('tagGroup', $form->_tagGroup);
171 }
172
173 /**
174 * set defaults for relevant form elements
175 *
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
179 * @param string $fieldName this is used in batch profile(i.e to build multiple blocks)
180 *
181 * @param string $groupElementType
182 *
183 * @return void
184 * @access public
185 * @static
186 */
187 static function setDefaults($id, &$defaults, $type = self::ALL, $fieldName = NULL, $groupElementType = 'checkbox') {
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) {
198 if ($groupElementType == 'select') {
199 $defaults[$fName][] = $group['group_id'];
200 }
201 else {
202 $defaults[$fName . '[' . $group['group_id'] . ']'] = 1;
203 }
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 *
229 * @param $form
230 * @param $defaults
231 *
232 * @return void
233 */
234 public static function setDefaultValues(&$form, &$defaults) {
235 $contactEditOptions = $form->get('contactEditOptions');
236
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
251 $groupElementType = 'checkbox';
252 if (CRM_Utils_System::getClassName($form) == 'CRM_Contact_Form_Contact') {
253 $groupElementType = 'select';
254 }
255 self::setDefaults($form->_contactId, $defaults, self::ALL, NULL, $groupElementType);
256 }
257 }
258 }
259 }