Merge pull request #3196 from JoeMurray/master
[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 string $groupName if used for building group block
53 * @param string $tagName if used for building tag block
54 * @param string $fieldName this is used in batch profile(i.e to build multiple blocks)
55 *
56 * @static
57 * @access public
58 */
59 static function buildQuickForm(&$form,
60 $contactId = 0,
61 $type = self::ALL,
62 $visibility = FALSE,
63 $isRequired = NULL,
64 $groupName = 'Group(s)',
65 $tagName = 'Tag(s)',
66 $fieldName = NULL,
67 $groupElementType = 'checkbox'
68 ) {
69 if (!isset($form->_tagGroup)) {
70 $form->_tagGroup = array();
71 }
72
73 // NYSS 5670
74 if (!$contactId && !empty($form->_contactId)) {
75 $contactId = $form->_contactId;
76 }
77
78 $type = (int) $type;
79 if ($type & self::GROUP) {
80
81 $fName = 'group';
82 if ($fieldName) {
83 $fName = $fieldName;
84 }
85
86 $groupID = isset($form->_grid) ? $form->_grid : NULL;
87 if ($groupID && $visibility) {
88 $ids = array($groupID => $groupID);
89 }
90 else {
91 if ($visibility) {
92 $group = CRM_Core_PseudoConstant::allGroup();
93 }
94 else {
95 $group = CRM_Core_PseudoConstant::group();
96 }
97 $ids = $group;
98 }
99
100 if ($groupID || !empty($group)) {
101 $groups = CRM_Contact_BAO_Group::getGroupsHierarchy($ids);
102
103 $attributes['skiplabel'] = TRUE;
104 $elements = array();
105 $groupsOptions = array();
106 foreach ($groups as $id => $group) {
107 // make sure that this group has public visibility
108 if ($visibility &&
109 $group['visibility'] == 'User and User Admin Only'
110 ) {
111 continue;
112 }
113
114 if ($groupElementType == 'select') {
115 $groupsOptions[$id] = $group['title'];
116 }
117 else {
118 $form->_tagGroup[$fName][$id]['description'] = $group['description'];
119 $elements[] = &$form->addElement('advcheckbox', $id, NULL, $group['title'], $attributes);
120 }
121 }
122
123 if ($groupElementType == 'select' && !empty($groupsOptions)) {
124 $form->add('select', $fName, ts('%1', array(1 => $groupName)), $groupsOptions, FALSE,
125 array('id' => $fName, 'multiple' => 'multiple', 'class' => 'crm-select2')
126 );
127 $form->assign('groupCount', count($groupsOptions));
128 }
129
130 if ($groupElementType == 'checkbox' && !empty($elements)) {
131 $form->addGroup($elements, $fName, $groupName, '&nbsp;<br />');
132 $form->assign('groupCount', count($elements));
133 if ($isRequired) {
134 $form->addRule($fName, ts('%1 is a required field.', array(1 => $groupName)), 'required');
135 }
136 }
137 $form->assign('groupElementType', $groupElementType);
138 }
139 }
140
141 if ($type & self::TAG) {
142 $fName = 'tag';
143 if ($fieldName) {
144 $fName = $fieldName;
145 }
146 $form->_tagGroup[$fName] = 1;
147 $elements = array();
148 $tag = CRM_Core_BAO_Tag::getTags();
149
150 foreach ($tag as $id => $name) {
151 $elements[] = $form->createElement('checkbox', $id, NULL, $name);
152 }
153 if (!empty($elements)) {
154 $form->addGroup($elements, $fName, $tagName, '<br />');
155 $form->assign('tagCount', count($elements));
156 }
157
158 if ($isRequired) {
159 $form->addRule($fName, ts('%1 is a required field.', array(1 => $tagName)), 'required');
160 }
161
162 // build tag widget
163 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact');
164
165 CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_contact', $contactId, TRUE, TRUE);
166 }
167 $form->assign('tagGroup', $form->_tagGroup);
168 }
169
170 /**
171 * set defaults for relevant form elements
172 *
173 * @param int $id the contact id
174 * @param array $defaults the defaults array to store the values in
175 * @param int $type what components are we interested in
176 * @param string $fieldName this is used in batch profile(i.e to build multiple blocks)
177 *
178 * @return void
179 * @access public
180 * @static
181 */
182 static function setDefaults($id, &$defaults, $type = self::ALL, $fieldName = NULL, $groupElementType = 'checkbox') {
183 $type = (int ) $type;
184 if ($type & self::GROUP) {
185 $fName = 'group';
186 if ($fieldName) {
187 $fName = $fieldName;
188 }
189
190 $contactGroup = CRM_Contact_BAO_GroupContact::getContactGroup($id, 'Added', NULL, FALSE, TRUE);
191 if ($contactGroup) {
192 foreach ($contactGroup as $group) {
193 if ($groupElementType == 'select') {
194 $defaults[$fName][] = $group['group_id'];
195 }
196 else {
197 $defaults[$fName . '[' . $group['group_id'] . ']'] = 1;
198 }
199 }
200 }
201 }
202
203 if ($type & self::TAG) {
204 $fName = 'tag';
205 if ($fieldName) {
206 $fName = $fieldName;
207 }
208
209 $contactTag = CRM_Core_BAO_EntityTag::getTag($id);
210 if ($contactTag) {
211 foreach ($contactTag as $tag) {
212 $defaults[$fName . '[' . $tag . ']'] = 1;
213 }
214 }
215 }
216 }
217
218 /**
219 * This function sets the default values for the form. Note that in edit/view mode
220 * the default values are retrieved from the database
221 *
222 * @access public
223 *
224 * @return void
225 */
226 public static function setDefaultValues(&$form, &$defaults) {
227 $contactEditOptions = $form->get('contactEditOptions');
228
229 if ($form->_action & CRM_Core_Action::ADD) {
230 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
231 // set group and tag defaults if any
232 if ($form->_gid) {
233 $defaults['group'][$form->_gid] = 1;
234 }
235 if ($form->_tid) {
236 $defaults['tag'][$form->_tid] = 1;
237 }
238 }
239 }
240 else {
241 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
242 // set the group and tag ids
243 $groupElementType = 'checkbox';
244 if (CRM_Utils_System::getClassName($form) == 'CRM_Contact_Form_Contact') {
245 $groupElementType = 'select';
246 }
247 self::setDefaults($form->_contactId, $defaults, self::ALL, NULL, $groupElementType);
248 }
249 }
250 }
251 }