Merge pull request #159 from lcdservices/master
[civicrm-core.git] / CRM / Contact / Form / Edit / TagsAndGroups.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 = CRM_Contact_Form_Edit_TagsandGroups::ALL,
62 $visibility = FALSE,
63 $isRequired = NULL,
64 $groupName = 'Group(s)',
65 $tagName = 'Tag(s)',
66 $fieldName = NULL
67 ) {
68 if (!isset($form->_tagGroup)) {
69 $form->_tagGroup = array();
70 }
71
72 // NYSS 5670
73 if (!$contactId && !empty($form->_contactId)) {
74 $contactId = $form->_contactId;
75 }
76
77 $type = (int ) $type;
78 if ($type & CRM_Contact_Form_Edit_TagsandGroups::GROUP) {
79
80 $fName = 'group';
81 if ($fieldName) {
82 $fName = $fieldName;
83 }
84
85 $elements = array();
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 foreach ($groups as $id => $group) {
105 // make sure that this group has public visibility
106 if ($visibility &&
107 $group['visibility'] == 'User and User Admin Only'
108 ) {
109 continue;
110 }
111 $form->_tagGroup[$fName][$id]['description'] = $group['description'];
112 $elements[] = &$form->addElement('advcheckbox', $id, NULL, $group['title'], $attributes);
113 }
114
115 if (!empty($elements)) {
116 $form->addGroup($elements, $fName, $groupName, '&nbsp;<br />');
117 $form->assign('groupCount', count($elements));
118 if ($isRequired) {
119 $form->addRule($fName, ts('%1 is a required field.', array(1 => $groupName)), 'required');
120 }
121 }
122 }
123 }
124
125 if ($type & CRM_Contact_Form_Edit_TagsandGroups::TAG) {
126 $fName = 'tag';
127 if ($fieldName) {
128 $fName = $fieldName;
129 }
130 $form->_tagGroup[$fName] = 1;
131 $elements = array();
132 $tag = CRM_Core_BAO_Tag::getTags();
133
134 foreach ($tag as $id => $name) {
135 $elements[] = $form->createElement('checkbox', $id, NULL, $name);
136 }
137 if (!empty($elements)) {
138 $form->addGroup($elements, $fName, $tagName, '<br />');
139 $form->assign('tagCount', count($elements));
140 }
141
142 if ($isRequired) {
143 $form->addRule($fName, ts('%1 is a required field.', array(1 => $tagName)), 'required');
144 }
145
146 // build tag widget
147 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact');
148
149 CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_contact', $contactId, FALSE, TRUE);
150 }
151 $form->assign('tagGroup', $form->_tagGroup);
152 }
153
154 /**
155 * set defaults for relevant form elements
156 *
157 * @param int $id the contact id
158 * @param array $defaults the defaults array to store the values in
159 * @param int $type what components are we interested in
160 * @param string $fieldName this is used in batch profile(i.e to build multiple blocks)
161 *
162 * @return void
163 * @access public
164 * @static
165 */
166 static function setDefaults($id, &$defaults, $type = CRM_Contact_Form_Edit_TagsandGroups::ALL, $fieldName = NULL) {
167 $type = (int ) $type;
168 if ($type & self::GROUP) {
169 $fName = 'group';
170 if ($fieldName) {
171 $fName = $fieldName;
172 }
173
174 $contactGroup = CRM_Contact_BAO_GroupContact::getContactGroup($id, 'Added', NULL, FALSE, TRUE);
175 if ($contactGroup) {
176 foreach ($contactGroup as $group) {
177 $defaults[$fName . '[' . $group['group_id'] . ']'] = 1;
178 }
179 }
180 }
181
182 if ($type & self::TAG) {
183 $fName = 'tag';
184 if ($fieldName) {
185 $fName = $fieldName;
186 }
187
188 $contactTag = CRM_Core_BAO_EntityTag::getTag($id);
189 if ($contactTag) {
190 foreach ($contactTag as $tag) {
191 $defaults[$fName . '[' . $tag . ']'] = 1;
192 }
193 }
194 }
195 }
196
197 /**
198 * This function sets the default values for the form. Note that in edit/view mode
199 * the default values are retrieved from the database
200 *
201 * @access public
202 *
203 * @return None
204 */
205 public static function setDefaultValues(&$form, &$defaults) {
206 $contactEditOptions = $form->get('contactEditOptions');
207 if ($form->_action & CRM_Core_Action::ADD) {
208 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
209 // set group and tag defaults if any
210 if ($form->_gid) {
211 $defaults['group'][$form->_gid] = 1;
212 }
213 if ($form->_tid) {
214 $defaults['tag'][$form->_tid] = 1;
215 }
216 }
217 }
218 else {
219 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
220 // set the group and tag ids
221 self::setDefaults($form->_contactId, $defaults, self::ALL);
222 }
223 }
224 }
225 }
226