Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | +--------------------------------------------------------------------+ | |
e70a7fc0 | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group { | |
36 | ||
37 | /** | |
100fef9d | 38 | * Class constructor |
6a488035 | 39 | */ |
00be9182 | 40 | public function __construct() { |
6a488035 TO |
41 | parent::__construct(); |
42 | } | |
43 | ||
44 | /** | |
fe482240 EM |
45 | * Retrieve DB object based on input parameters. |
46 | * | |
47 | * It also stores all the retrieved values in the default array. | |
6a488035 | 48 | * |
77c5b619 TO |
49 | * @param array $params |
50 | * (reference ) an assoc array of name/value pairs. | |
51 | * @param array $defaults | |
52 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 53 | * |
16b10e64 | 54 | * @return CRM_Contact_BAO_Group |
6a488035 | 55 | */ |
00be9182 | 56 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
57 | $group = new CRM_Contact_DAO_Group(); |
58 | $group->copyValues($params); | |
59 | if ($group->find(TRUE)) { | |
60 | CRM_Core_DAO::storeValues($group, $defaults); | |
61 | return $group; | |
62 | } | |
63 | ||
64 | return NULL; | |
65 | } | |
66 | ||
67 | /** | |
100fef9d | 68 | * Delete the group and all the object that connect to |
6a488035 TO |
69 | * this group. Incredibly destructive |
70 | * | |
77c5b619 TO |
71 | * @param int $id |
72 | * Group id. | |
6a488035 | 73 | */ |
00be9182 | 74 | public static function discard($id) { |
6a488035 TO |
75 | CRM_Utils_Hook::pre('delete', 'Group', $id, CRM_Core_DAO::$_nullArray); |
76 | ||
77 | $transaction = new CRM_Core_Transaction(); | |
78 | ||
79 | // added for CRM-1631 and CRM-1794 | |
80 | // delete all subscribed mails with the selected group id | |
81 | $subscribe = new CRM_Mailing_Event_DAO_Subscribe(); | |
82 | $subscribe->group_id = $id; | |
83 | $subscribe->delete(); | |
84 | ||
85 | // delete all Subscription records with the selected group id | |
86 | $subHistory = new CRM_Contact_DAO_SubscriptionHistory(); | |
87 | $subHistory->group_id = $id; | |
88 | $subHistory->delete(); | |
89 | ||
90 | // delete all crm_group_contact records with the selected group id | |
91 | $groupContact = new CRM_Contact_DAO_GroupContact(); | |
92 | $groupContact->group_id = $id; | |
93 | $groupContact->delete(); | |
94 | ||
95 | // make all the 'add_to_group_id' field of 'civicrm_uf_group table', pointing to this group, as null | |
96 | $params = array(1 => array($id, 'Integer')); | |
97 | $query = "UPDATE civicrm_uf_group SET `add_to_group_id`= NULL WHERE `add_to_group_id` = %1"; | |
98 | CRM_Core_DAO::executeQuery($query, $params); | |
99 | ||
100 | $query = "UPDATE civicrm_uf_group SET `limit_listings_group_id`= NULL WHERE `limit_listings_group_id` = %1"; | |
101 | CRM_Core_DAO::executeQuery($query, $params); | |
102 | ||
103 | // make sure u delete all the entries from civicrm_mailing_group and civicrm_campaign_group | |
104 | // CRM-6186 | |
105 | $query = "DELETE FROM civicrm_mailing_group where entity_table = 'civicrm_group' AND entity_id = %1"; | |
106 | CRM_Core_DAO::executeQuery($query, $params); | |
107 | ||
108 | $query = "DELETE FROM civicrm_campaign_group where entity_table = 'civicrm_group' AND entity_id = %1"; | |
109 | CRM_Core_DAO::executeQuery($query, $params); | |
110 | ||
111 | $query = "DELETE FROM civicrm_acl_entity_role where entity_table = 'civicrm_group' AND entity_id = %1"; | |
112 | CRM_Core_DAO::executeQuery($query, $params); | |
113 | ||
114 | if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME, | |
353ffa53 TO |
115 | 'is_enabled' |
116 | ) | |
117 | ) { | |
6a488035 | 118 | // clear any descendant groups cache if exists |
554259a7 | 119 | CRM_Core_BAO_Cache::deleteGroup('descendant groups for an org'); |
6a488035 TO |
120 | } |
121 | ||
122 | // delete from group table | |
123 | $group = new CRM_Contact_DAO_Group(); | |
124 | $group->id = $id; | |
125 | $group->delete(); | |
126 | ||
127 | $transaction->commit(); | |
128 | ||
129 | CRM_Utils_Hook::post('delete', 'Group', $id, $group); | |
130 | ||
131 | // delete the recently created Group | |
132 | $groupRecent = array( | |
133 | 'id' => $id, | |
134 | 'type' => 'Group', | |
135 | ); | |
136 | CRM_Utils_Recent::del($groupRecent); | |
137 | } | |
138 | ||
139 | /** | |
140 | * Returns an array of the contacts in the given group. | |
6a488035 | 141 | */ |
00be9182 | 142 | public static function getGroupContacts($id) { |
6a488035 TO |
143 | $params = array(array('group', 'IN', array($id => 1), 0, 0)); |
144 | list($contacts, $_) = CRM_Contact_BAO_Query::apiQuery($params, array('contact_id')); | |
145 | return $contacts; | |
146 | } | |
147 | ||
148 | /** | |
fe482240 | 149 | * Get the count of a members in a group with the specific status. |
6a488035 | 150 | * |
77c5b619 TO |
151 | * @param int $id |
152 | * Group id. | |
3f8d2862 CW |
153 | * @param string $status |
154 | * status of members in group | |
80ad33e7 | 155 | * @param bool $countChildGroups |
6a488035 | 156 | * |
a6c01b45 CW |
157 | * @return int |
158 | * count of members in the group with above status | |
6a488035 | 159 | */ |
00be9182 | 160 | public static function memberCount($id, $status = 'Added', $countChildGroups = FALSE) { |
6a488035 TO |
161 | $groupContact = new CRM_Contact_DAO_GroupContact(); |
162 | $groupIds = array($id); | |
163 | if ($countChildGroups) { | |
164 | $groupIds = CRM_Contact_BAO_GroupNesting::getDescendentGroupIds($groupIds); | |
165 | } | |
166 | $count = 0; | |
167 | ||
168 | $contacts = self::getGroupContacts($id); | |
169 | ||
170 | foreach ($groupIds as $groupId) { | |
171 | ||
172 | $groupContacts = self::getGroupContacts($groupId); | |
173 | foreach ($groupContacts as $gcontact) { | |
174 | if ($groupId != $id) { | |
175 | // Loop through main group's contacts | |
176 | // and subtract from the count for each contact which | |
177 | // matches one in the present group, if it is not the | |
178 | // main group | |
179 | foreach ($contacts as $contact) { | |
180 | if ($contact['contact_id'] == $gcontact['contact_id']) { | |
181 | $count--; | |
182 | } | |
183 | } | |
184 | } | |
185 | } | |
186 | $groupContact->group_id = $groupId; | |
187 | if (isset($status)) { | |
188 | $groupContact->status = $status; | |
189 | } | |
190 | $groupContact->_query['condition'] = 'WHERE contact_id NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1)'; | |
191 | $count += $groupContact->count(); | |
192 | } | |
193 | return $count; | |
194 | } | |
195 | ||
196 | /** | |
fe482240 | 197 | * Get the list of member for a group id. |
6a488035 | 198 | * |
100fef9d | 199 | * @param int $groupID |
80ad33e7 EM |
200 | * @param bool $useCache |
201 | * | |
a6c01b45 CW |
202 | * @return array |
203 | * this array contains the list of members for this group id | |
6a488035 | 204 | */ |
00be9182 | 205 | public static function &getMember($groupID, $useCache = TRUE) { |
6a488035 TO |
206 | $params = array(array('group', 'IN', array($groupID => 1), 0, 0)); |
207 | $returnProperties = array('contact_id'); | |
208 | list($contacts, $_) = CRM_Contact_BAO_Query::apiQuery($params, $returnProperties, NULL, NULL, 0, 0, $useCache); | |
209 | ||
210 | $aMembers = array(); | |
211 | foreach ($contacts as $contact) { | |
212 | $aMembers[$contact['contact_id']] = 1; | |
213 | } | |
214 | ||
215 | return $aMembers; | |
216 | } | |
217 | ||
218 | /** | |
219 | * Returns array of group object(s) matching a set of one or Group properties. | |
220 | * | |
77c5b619 TO |
221 | * @param array $params |
222 | * Limits the set of groups returned. | |
223 | * @param array $returnProperties | |
224 | * Which properties should be included in the returned group objects. | |
3f8d2862 CW |
225 | * (member_count should be last element.) |
226 | * @param string $sort | |
227 | * @param int $offset | |
228 | * @param int $rowCount | |
80ad33e7 | 229 | * |
a6c01b45 | 230 | * @return array |
16b10e64 | 231 | * Array of group objects. |
6a488035 | 232 | * |
6a488035 TO |
233 | * |
234 | * @todo other BAO functions that use returnProperties (e.g. Query Objects) receive the array flipped & filled with 1s and | |
235 | * add in essential fields (e.g. id). This should follow a regular pattern like the others | |
236 | */ | |
2da40d21 | 237 | public static function getGroups( |
6a488035 TO |
238 | $params = NULL, |
239 | $returnProperties = NULL, | |
240 | $sort = NULL, | |
241 | $offset = NULL, | |
242 | $rowCount = NULL | |
243 | ) { | |
244 | $dao = new CRM_Contact_DAO_Group(); | |
6ce76461 CW |
245 | if (!isset($params['is_active'])) { |
246 | $dao->is_active = 1; | |
247 | } | |
6a488035 TO |
248 | if ($params) { |
249 | foreach ($params as $k => $v) { | |
250 | if ($k == 'name' || $k == 'title') { | |
251 | $dao->whereAdd($k . ' LIKE "' . CRM_Core_DAO::escapeString($v) . '"'); | |
252 | } | |
6ce76461 CW |
253 | elseif ($k == 'group_type') { |
254 | foreach ((array) $v as $type) { | |
255 | $dao->whereAdd($k . " LIKE '%" . CRM_Core_DAO::VALUE_SEPARATOR . (int) $type . CRM_Core_DAO::VALUE_SEPARATOR . "%'"); | |
256 | } | |
257 | } | |
6a488035 | 258 | elseif (is_array($v)) { |
6ce76461 CW |
259 | foreach ($v as &$num) { |
260 | $num = (int) $num; | |
261 | } | |
6a488035 TO |
262 | $dao->whereAdd($k . ' IN (' . implode(',', $v) . ')'); |
263 | } | |
264 | else { | |
265 | $dao->$k = $v; | |
266 | } | |
267 | } | |
268 | } | |
269 | ||
270 | if ($offset || $rowCount) { | |
271 | $offset = ($offset > 0) ? $offset : 0; | |
272 | $rowCount = ($rowCount > 0) ? $rowCount : 25; | |
273 | $dao->limit($offset, $rowCount); | |
274 | } | |
275 | ||
276 | if ($sort) { | |
277 | $dao->orderBy($sort); | |
278 | } | |
279 | ||
280 | // return only specific fields if returnproperties are sent | |
281 | if (!empty($returnProperties)) { | |
282 | $dao->selectAdd(); | |
283 | $dao->selectAdd(implode(',', $returnProperties)); | |
284 | } | |
285 | $dao->find(); | |
286 | ||
287 | $flag = $returnProperties && in_array('member_count', $returnProperties) ? 1 : 0; | |
288 | ||
289 | $groups = array(); | |
290 | while ($dao->fetch()) { | |
291 | $group = new CRM_Contact_DAO_Group(); | |
292 | if ($flag) { | |
293 | $dao->member_count = CRM_Contact_BAO_Group::memberCount($dao->id); | |
294 | } | |
295 | $groups[] = clone($dao); | |
296 | } | |
297 | return $groups; | |
298 | } | |
299 | ||
300 | /** | |
fe482240 | 301 | * Make sure that the user has permission to access this group. |
6a488035 | 302 | * |
77c5b619 TO |
303 | * @param int $id |
304 | * The id of the object. | |
6a488035 | 305 | * |
a6c01b45 CW |
306 | * @return string |
307 | * the permission that the user has (or NULL) | |
6a488035 | 308 | */ |
00be9182 | 309 | public static function checkPermission($id) { |
6a488035 TO |
310 | $allGroups = CRM_Core_PseudoConstant::allGroup(); |
311 | ||
312 | $permissions = NULL; | |
313 | if (CRM_Core_Permission::check('edit all contacts') || | |
314 | CRM_ACL_API::groupPermission(CRM_ACL_API::EDIT, $id, NULL, | |
315 | 'civicrm_saved_search', $allGroups | |
316 | ) | |
317 | ) { | |
318 | $permissions[] = CRM_Core_Permission::EDIT; | |
319 | } | |
320 | ||
321 | if (CRM_Core_Permission::check('view all contacts') || | |
322 | CRM_ACL_API::groupPermission(CRM_ACL_API::VIEW, $id, NULL, | |
323 | 'civicrm_saved_search', $allGroups | |
324 | ) | |
325 | ) { | |
326 | $permissions[] = CRM_Core_Permission::VIEW; | |
327 | } | |
328 | ||
329 | if (!empty($permissions) && CRM_Core_Permission::check('delete contacts')) { | |
330 | // Note: using !empty() in if condition, restricts the scope of delete | |
331 | // permission to groups/contacts that are editable/viewable. | |
332 | // We can remove this !empty condition once we have ACL support for delete functionality. | |
333 | $permissions[] = CRM_Core_Permission::DELETE; | |
334 | } | |
335 | ||
336 | return $permissions; | |
337 | } | |
338 | ||
339 | /** | |
fe482240 | 340 | * Create a new group. |
6a488035 | 341 | * |
77c5b619 | 342 | * @param array $params |
6a488035 | 343 | * |
72b3a70c CW |
344 | * @return CRM_Contact_BAO_Group|NULL |
345 | * The new group BAO (if created) | |
6a488035 TO |
346 | */ |
347 | public static function &create(&$params) { | |
348 | ||
a7488080 | 349 | if (!empty($params['id'])) { |
6a488035 TO |
350 | CRM_Utils_Hook::pre('edit', 'Group', $params['id'], $params); |
351 | } | |
352 | else { | |
353 | CRM_Utils_Hook::pre('create', 'Group', NULL, $params); | |
354 | } | |
355 | ||
356 | // form the name only if missing: CRM-627 | |
e2b7c67d | 357 | $nameParam = CRM_Utils_Array::value('name', $params, NULL); |
8cc574cf | 358 | if (!$nameParam && empty($params['id'])) { |
6a488035 TO |
359 | $params['name'] = CRM_Utils_String::titleToVar($params['title']); |
360 | } | |
361 | ||
362 | // convert params if array type | |
363 | if (isset($params['group_type'])) { | |
364 | if (is_array($params['group_type'])) { | |
365 | $params['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
353ffa53 TO |
366 | array_keys($params['group_type']) |
367 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
6a488035 TO |
368 | } |
369 | } | |
370 | else { | |
371 | $params['group_type'] = ''; | |
372 | } | |
373 | ||
481a74f4 | 374 | $session = CRM_Core_Session::singleton(); |
d0dfb649 PJ |
375 | $cid = $session->get('userID'); |
376 | // this action is add | |
377 | if ($cid && empty($params['id'])) { | |
6a488035 TO |
378 | $params['created_id'] = $cid; |
379 | } | |
d0dfb649 PJ |
380 | // this action is update |
381 | if ($cid && !empty($params['id'])) { | |
382 | $params['modified_id'] = $cid; | |
383 | } | |
6a488035 TO |
384 | |
385 | $group = new CRM_Contact_BAO_Group(); | |
386 | $group->copyValues($params); | |
57c93d72 E |
387 | //@todo very hacky fix for the fact this function wants to receive 'parents' as an array further down but |
388 | // needs it as a separated string for the DB. Preferred approaches are having the copyParams or save fn | |
389 | // use metadata to translate the array to the appropriate DB type or altering the param in the api layer, | |
390 | // or at least altering the param in same section as 'group_type' rather than repeating here. However, further down | |
391 | // we need the $params one to be in it's original form & we are not sure what test coverage we have on that | |
22e263ad | 392 | if (isset($group->parents) && is_array($group->parents)) { |
57c93d72 | 393 | $group->parents = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, |
353ffa53 TO |
394 | array_keys($group->parents) |
395 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
57c93d72 | 396 | } |
a7488080 | 397 | if (empty($params['id']) && |
e2b7c67d | 398 | !$nameParam |
0aab99e7 | 399 | ) { |
6a488035 TO |
400 | $group->name .= "_tmp"; |
401 | } | |
402 | $group->save(); | |
403 | ||
404 | if (!$group->id) { | |
405 | return NULL; | |
406 | } | |
407 | ||
a7488080 | 408 | if (empty($params['id']) && |
e2b7c67d | 409 | !$nameParam |
0aab99e7 | 410 | ) { |
6a488035 TO |
411 | $group->name = substr($group->name, 0, -4) . "_{$group->id}"; |
412 | } | |
413 | ||
414 | $group->buildClause(); | |
415 | $group->save(); | |
416 | ||
417 | // add custom field values | |
a7488080 | 418 | if (!empty($params['custom'])) { |
6a488035 TO |
419 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_group', $group->id); |
420 | } | |
421 | ||
422 | // make the group, child of domain/site group by default. | |
423 | $domainGroupID = CRM_Core_BAO_Domain::getGroupId(); | |
424 | if (CRM_Utils_Array::value('no_parent', $params) !== 1) { | |
425 | if (empty($params['parents']) && | |
426 | $domainGroupID != $group->id && | |
427 | CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME, | |
428 | 'is_enabled' | |
429 | ) && | |
430 | !CRM_Contact_BAO_GroupNesting::hasParentGroups($group->id) | |
431 | ) { | |
432 | // if no parent present and the group doesn't already have any parents, | |
433 | // make sure site group goes as parent | |
434 | $params['parents'] = array($domainGroupID => 1); | |
435 | } | |
436 | elseif (array_key_exists('parents', $params) && !is_array($params['parents'])) { | |
437 | $params['parents'] = array($params['parents'] => 1); | |
438 | } | |
439 | ||
440 | if (!empty($params['parents'])) { | |
441 | foreach ($params['parents'] as $parentId => $dnc) { | |
442 | if ($parentId && !CRM_Contact_BAO_GroupNesting::isParentChild($parentId, $group->id)) { | |
443 | CRM_Contact_BAO_GroupNesting::add($parentId, $group->id); | |
444 | } | |
445 | } | |
446 | } | |
447 | ||
448 | // clear any descendant groups cache if exists | |
449 | $finalGroups = CRM_Core_BAO_Cache::deleteGroup('descendant groups for an org'); | |
450 | ||
451 | // this is always required, since we don't know when a | |
452 | // parent group is removed | |
453 | CRM_Contact_BAO_GroupNestingCache::update(); | |
454 | ||
455 | // update group contact cache for all parent groups | |
456 | $parentIds = CRM_Contact_BAO_GroupNesting::getParentGroupIds($group->id); | |
457 | foreach ($parentIds as $parentId) { | |
458 | CRM_Contact_BAO_GroupContactCache::add($parentId); | |
459 | } | |
460 | } | |
461 | ||
a7488080 | 462 | if (!empty($params['organization_id'])) { |
6a488035 TO |
463 | $groupOrg = array(); |
464 | $groupOrg = $params; | |
465 | $groupOrg['group_id'] = $group->id; | |
466 | CRM_Contact_BAO_GroupOrganization::add($groupOrg); | |
467 | } | |
468 | ||
469 | CRM_Contact_BAO_GroupContactCache::add($group->id); | |
470 | ||
a7488080 | 471 | if (!empty($params['id'])) { |
6a488035 TO |
472 | CRM_Utils_Hook::post('edit', 'Group', $group->id, $group); |
473 | } | |
474 | else { | |
475 | CRM_Utils_Hook::post('create', 'Group', $group->id, $group); | |
476 | } | |
477 | ||
478 | $recentOther = array(); | |
479 | if (CRM_Core_Permission::check('edit groups')) { | |
480 | $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/group', 'reset=1&action=update&id=' . $group->id); | |
481 | // currently same permission we are using for delete a group | |
482 | $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/group', 'reset=1&action=delete&id=' . $group->id); | |
483 | } | |
484 | ||
485 | // add the recently added group (unless hidden: CRM-6432) | |
486 | if (!$group->is_hidden) { | |
487 | CRM_Utils_Recent::add($group->title, | |
488 | CRM_Utils_System::url('civicrm/group/search', 'reset=1&force=1&context=smog&gid=' . $group->id), | |
489 | $group->id, | |
490 | 'Group', | |
491 | NULL, | |
492 | NULL, | |
493 | $recentOther | |
494 | ); | |
495 | } | |
496 | return $group; | |
497 | } | |
498 | ||
499 | /** | |
100fef9d | 500 | * Given a saved search compute the clause and the tables |
6a488035 TO |
501 | * and store it for future use |
502 | */ | |
00be9182 | 503 | public function buildClause() { |
6a488035 TO |
504 | $params = array(array('group', 'IN', array($this->id => 1), 0, 0)); |
505 | ||
506 | if (!empty($params)) { | |
507 | $tables = $whereTables = array(); | |
508 | $this->where_clause = CRM_Contact_BAO_Query::getWhereClause($params, NULL, $tables, $whereTables); | |
509 | if (!empty($tables)) { | |
510 | $this->select_tables = serialize($tables); | |
511 | } | |
512 | if (!empty($whereTables)) { | |
513 | $this->where_tables = serialize($whereTables); | |
514 | } | |
515 | } | |
6a488035 TO |
516 | } |
517 | ||
518 | /** | |
fe482240 | 519 | * Defines a new smart group. |
6a488035 | 520 | * |
77c5b619 TO |
521 | * @param array $params |
522 | * Associative array of parameters. | |
6a488035 | 523 | * |
72b3a70c CW |
524 | * @return CRM_Contact_BAO_Group|NULL |
525 | * The new group BAO (if created) | |
6a488035 TO |
526 | */ |
527 | public static function createSmartGroup(&$params) { | |
a7488080 | 528 | if (!empty($params['formValues'])) { |
6a488035 TO |
529 | $ssParams = $params; |
530 | unset($ssParams['id']); | |
531 | if (isset($ssParams['saved_search_id'])) { | |
532 | $ssParams['id'] = $ssParams['saved_search_id']; | |
533 | } | |
534 | ||
535 | $savedSearch = CRM_Contact_BAO_SavedSearch::create($params); | |
536 | ||
537 | $params['saved_search_id'] = $savedSearch->id; | |
538 | } | |
539 | else { | |
540 | return NULL; | |
541 | } | |
542 | ||
543 | return self::create($params); | |
544 | } | |
545 | ||
546 | /** | |
fe482240 | 547 | * Update the is_active flag in the db. |
6a488035 | 548 | * |
77c5b619 TO |
549 | * @param int $id |
550 | * Id of the database record. | |
551 | * @param bool $isActive | |
552 | * Value we want to set the is_active field. | |
6a488035 | 553 | * |
16b10e64 | 554 | * @return CRM_Core_DAO|null |
3f8d2862 | 555 | * DAO object on success, NULL otherwise |
6a488035 | 556 | */ |
00be9182 | 557 | public static function setIsActive($id, $isActive) { |
6a488035 TO |
558 | return CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_Group', $id, 'is_active', $isActive); |
559 | } | |
560 | ||
561 | /** | |
100fef9d | 562 | * Build the condition to retrieve groups. |
6a488035 | 563 | * |
77c5b619 TO |
564 | * @param string $groupType |
565 | * Type of group(Access/Mailing) OR the key of the group. | |
3f8d2862 | 566 | * @param bool $excludeHidden exclude hidden groups. |
6a488035 | 567 | * |
a6c01b45 | 568 | * @return string |
6a488035 | 569 | */ |
00be9182 | 570 | public static function groupTypeCondition($groupType = NULL, $excludeHidden = TRUE) { |
6a488035 TO |
571 | $value = NULL; |
572 | if ($groupType == 'Mailing') { | |
573 | $value = CRM_Core_DAO::VALUE_SEPARATOR . '2' . CRM_Core_DAO::VALUE_SEPARATOR; | |
574 | } | |
575 | elseif ($groupType == 'Access') { | |
576 | $value = CRM_Core_DAO::VALUE_SEPARATOR . '1' . CRM_Core_DAO::VALUE_SEPARATOR; | |
577 | } | |
9b873358 | 578 | elseif (!empty($groupType)) { |
6a488035 TO |
579 | // ie we have been given the group key |
580 | $value = CRM_Core_DAO::VALUE_SEPARATOR . $groupType . CRM_Core_DAO::VALUE_SEPARATOR; | |
581 | } | |
582 | ||
583 | $condition = NULL; | |
584 | if ($excludeHidden) { | |
585 | $condition = "is_hidden = 0"; | |
586 | } | |
587 | ||
588 | if ($value) { | |
589 | if ($condition) { | |
590 | $condition .= " AND group_type LIKE '%$value%'"; | |
591 | } | |
592 | else { | |
593 | $condition = "group_type LIKE '%$value%'"; | |
594 | } | |
595 | } | |
596 | ||
597 | return $condition; | |
598 | } | |
599 | ||
47c89d6b | 600 | /** |
fe482240 | 601 | * Get permission relevant clauses. |
47c89d6b EM |
602 | * CRM-12209 |
603 | * | |
aaac0e0b EM |
604 | * @param bool $force |
605 | * | |
47c89d6b EM |
606 | * @return array |
607 | */ | |
aaac0e0b EM |
608 | public static function getPermissionClause($force = FALSE) { |
609 | static $clause = 1; | |
47c89d6b | 610 | static $retrieved = FALSE; |
481a74f4 | 611 | if ((!$retrieved || $force) && !CRM_Core_Permission::check('view all contacts') && !CRM_Core_Permission::check('edit all contacts')) { |
47c89d6b EM |
612 | //get the allowed groups for the current user |
613 | $groups = CRM_ACL_API::group(CRM_ACL_API::VIEW); | |
614 | if (!empty($groups)) { | |
615 | $groupList = implode(', ', array_values($groups)); | |
616 | $clause = "groups.id IN ( $groupList ) "; | |
617 | } | |
aaac0e0b EM |
618 | else { |
619 | $clause = '1 = 0'; | |
620 | } | |
47c89d6b EM |
621 | } |
622 | $retrieved = TRUE; | |
623 | return $clause; | |
624 | } | |
625 | ||
86538308 EM |
626 | /** |
627 | * @return string | |
628 | */ | |
6a488035 TO |
629 | public function __toString() { |
630 | return $this->title; | |
631 | } | |
632 | ||
633 | /** | |
634 | * This function create the hidden smart group when user perform | |
b44e3f84 | 635 | * contact search and want to send mailing to search contacts. |
6a488035 | 636 | * |
77c5b619 TO |
637 | * @param array $params |
638 | * ( reference ) an assoc array of name/value pairs. | |
6a488035 | 639 | * |
a6c01b45 CW |
640 | * @return array |
641 | * ( smartGroupId, ssId ) smart group id and saved search id | |
6a488035 | 642 | */ |
00be9182 | 643 | public static function createHiddenSmartGroup($params) { |
6a488035 TO |
644 | $ssId = CRM_Utils_Array::value('saved_search_id', $params); |
645 | ||
646 | //add mapping record only for search builder saved search | |
647 | $mappingId = NULL; | |
648 | if ($params['search_context'] == 'builder') { | |
649 | //save the mapping for search builder | |
650 | if (!$ssId) { | |
651 | //save record in mapping table | |
353ffa53 | 652 | $temp = array(); |
6a488035 | 653 | $mappingParams = array('mapping_type' => 'Search Builder'); |
353ffa53 TO |
654 | $mapping = CRM_Core_BAO_Mapping::add($mappingParams, $temp); |
655 | $mappingId = $mapping->id; | |
6a488035 TO |
656 | } |
657 | else { | |
658 | //get the mapping id from saved search | |
659 | $savedSearch = new CRM_Contact_BAO_SavedSearch(); | |
660 | $savedSearch->id = $ssId; | |
661 | $savedSearch->find(TRUE); | |
662 | $mappingId = $savedSearch->mapping_id; | |
663 | } | |
664 | ||
665 | //save mapping fields | |
666 | CRM_Core_BAO_Mapping::saveMappingFields($params['form_values'], $mappingId); | |
667 | } | |
668 | ||
669 | //create/update saved search record. | |
670 | $savedSearch = new CRM_Contact_BAO_SavedSearch(); | |
671 | $savedSearch->id = $ssId; | |
672 | $savedSearch->form_values = serialize($params['form_values']); | |
673 | $savedSearch->mapping_id = $mappingId; | |
674 | $savedSearch->search_custom_id = CRM_Utils_Array::value('search_custom_id', $params); | |
675 | $savedSearch->save(); | |
676 | ||
677 | $ssId = $savedSearch->id; | |
678 | if (!$ssId) { | |
679 | return NULL; | |
680 | } | |
681 | ||
682 | $smartGroupId = NULL; | |
a7488080 | 683 | if (!empty($params['saved_search_id'])) { |
6a488035 TO |
684 | $smartGroupId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $ssId, 'id', 'saved_search_id'); |
685 | } | |
686 | else { | |
687 | //create group only when new saved search. | |
688 | $groupParams = array( | |
689 | 'title' => "Hidden Smart Group {$ssId}", | |
690 | 'is_active' => CRM_Utils_Array::value('is_active', $params, 1), | |
691 | 'is_hidden' => CRM_Utils_Array::value('is_hidden', $params, 1), | |
692 | 'group_type' => CRM_Utils_Array::value('group_type', $params), | |
693 | 'visibility' => CRM_Utils_Array::value('visibility', $params), | |
694 | 'saved_search_id' => $ssId, | |
695 | ); | |
696 | ||
697 | $smartGroup = self::create($groupParams); | |
698 | $smartGroupId = $smartGroup->id; | |
699 | } | |
700 | ||
701 | return array($smartGroupId, $ssId); | |
702 | } | |
703 | ||
704 | /** | |
fe482240 | 705 | * wrapper for ajax group selector. |
6a488035 | 706 | * |
77c5b619 TO |
707 | * @param array $params |
708 | * Associated array for params record id. | |
6a488035 | 709 | * |
a6c01b45 CW |
710 | * @return array |
711 | * associated array of group list | |
16b10e64 CW |
712 | * -rp = rowcount |
713 | * -page= offset | |
d3e86119 TO |
714 | * @todo there seems little reason for the small number of functions that call this to pass in |
715 | * params that then need to be translated in this function since they are coding them when calling | |
6a488035 | 716 | */ |
bca4d720 | 717 | static public function getGroupListSelector(&$params) { |
6a488035 | 718 | // format the params |
353ffa53 | 719 | $params['offset'] = ($params['page'] - 1) * $params['rp']; |
6a488035 | 720 | $params['rowCount'] = $params['rp']; |
353ffa53 | 721 | $params['sort'] = CRM_Utils_Array::value('sortBy', $params); |
6a488035 TO |
722 | |
723 | // get groups | |
724 | $groups = CRM_Contact_BAO_Group::getGroupList($params); | |
725 | ||
726 | //skip total if we are making call to show only children | |
a7488080 | 727 | if (empty($params['parent_id'])) { |
6a488035 TO |
728 | // add total |
729 | $params['total'] = CRM_Contact_BAO_Group::getGroupCount($params); | |
730 | ||
731 | // get all the groups | |
732 | $allGroups = CRM_Core_PseudoConstant::allGroup(); | |
733 | } | |
734 | ||
735 | // format params and add links | |
736 | $groupList = array(); | |
737 | if (!empty($groups)) { | |
738 | foreach ($groups as $id => $value) { | |
739 | $groupList[$id]['group_id'] = $value['id']; | |
7d1ce072 | 740 | $groupList[$id]['count'] = $value['count']; |
6a488035 | 741 | $groupList[$id]['group_name'] = $value['title']; |
6a488035 TO |
742 | |
743 | // append parent names if in search mode | |
8cc574cf | 744 | if (empty($params['parent_id']) && !empty($value['parents'])) { |
6a488035 TO |
745 | $groupIds = explode(',', $value['parents']); |
746 | $title = array(); | |
22e263ad | 747 | foreach ($groupIds as $gId) { |
6a488035 TO |
748 | $title[] = $allGroups[$gId]; |
749 | } | |
92fcb95f | 750 | $groupList[$id]['group_name'] .= '<div class="crm-row-parent-name"><em>' . ts('Child of') . '</em>: ' . implode(', ', $title) . '</div>'; |
7f4b344c | 751 | $value['class'] = array_diff($value['class'], array('crm-row-parent')); |
6a488035 | 752 | } |
7f4b344c CW |
753 | $value['class'][] = 'crm-entity'; |
754 | $groupList[$id]['class'] = $value['id'] . ',' . implode(' ', $value['class']); | |
6a488035 TO |
755 | |
756 | $groupList[$id]['group_description'] = CRM_Utils_Array::value('description', $value); | |
a7488080 | 757 | if (!empty($value['group_type'])) { |
6a488035 TO |
758 | $groupList[$id]['group_type'] = $value['group_type']; |
759 | } | |
760 | else { | |
761 | $groupList[$id]['group_type'] = ''; | |
762 | } | |
763 | $groupList[$id]['visibility'] = $value['visibility']; | |
764 | $groupList[$id]['links'] = $value['action']; | |
765 | $groupList[$id]['org_info'] = CRM_Utils_Array::value('org_info', $value); | |
766 | $groupList[$id]['created_by'] = CRM_Utils_Array::value('created_by', $value); | |
767 | ||
768 | $groupList[$id]['is_parent'] = $value['is_parent']; | |
769 | } | |
770 | return $groupList; | |
771 | } | |
772 | } | |
773 | ||
774 | /** | |
fe482240 | 775 | * This function to get list of groups. |
6a488035 | 776 | * |
77c5b619 TO |
777 | * @param array $params |
778 | * Associated array for params. | |
80ad33e7 EM |
779 | * |
780 | * @return array | |
6a488035 | 781 | */ |
00be9182 | 782 | public static function getGroupList(&$params) { |
6a488035 TO |
783 | $config = CRM_Core_Config::singleton(); |
784 | ||
785 | $whereClause = self::whereClause($params, FALSE); | |
786 | ||
787 | //$this->pagerAToZ( $whereClause, $params ); | |
788 | ||
789 | if (!empty($params['rowCount']) && | |
790 | $params['rowCount'] > 0 | |
791 | ) { | |
792 | $limit = " LIMIT {$params['offset']}, {$params['rowCount']} "; | |
793 | } | |
794 | ||
795 | $orderBy = ' ORDER BY groups.title asc'; | |
21d32567 DL |
796 | if (!empty($params['sort'])) { |
797 | $orderBy = ' ORDER BY ' . CRM_Utils_Type::escape($params['sort'], 'String'); | |
6a488035 TO |
798 | } |
799 | ||
800 | $select = $from = $where = ""; | |
801 | $groupOrg = FALSE; | |
802 | if (CRM_Core_Permission::check('administer Multiple Organizations') && | |
803 | CRM_Core_Permission::isMultisiteEnabled() | |
804 | ) { | |
805 | $select = ", contact.display_name as org_name, contact.id as org_id"; | |
806 | $from = " LEFT JOIN civicrm_group_organization gOrg | |
807 | ON gOrg.group_id = groups.id | |
808 | LEFT JOIN civicrm_contact contact | |
809 | ON contact.id = gOrg.organization_id "; | |
810 | ||
811 | //get the Organization ID | |
812 | $orgID = CRM_Utils_Request::retrieve('oid', 'Positive', CRM_Core_DAO::$_nullObject); | |
813 | if ($orgID) { | |
814 | $where = " AND gOrg.organization_id = {$orgID}"; | |
815 | } | |
816 | ||
817 | $groupOrg = TRUE; | |
818 | } | |
819 | ||
820 | $query = " | |
cbe32e2f | 821 | SELECT groups.*, createdBy.sort_name as created_by {$select} |
6a488035 | 822 | FROM civicrm_group groups |
7f4b344c CW |
823 | LEFT JOIN civicrm_contact createdBy |
824 | ON createdBy.id = groups.created_id | |
825 | {$from} | |
6a488035 | 826 | WHERE $whereClause {$where} |
7f4b344c | 827 | GROUP BY groups.id |
6a488035 TO |
828 | {$orderBy} |
829 | {$limit}"; | |
830 | ||
831 | $object = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Contact_DAO_Group'); | |
832 | ||
833 | //FIXME CRM-4418, now we are handling delete separately | |
834 | //if we introduce 'delete for group' make sure to handle here. | |
835 | $groupPermissions = array(CRM_Core_Permission::VIEW); | |
836 | if (CRM_Core_Permission::check('edit groups')) { | |
837 | $groupPermissions[] = CRM_Core_Permission::EDIT; | |
838 | $groupPermissions[] = CRM_Core_Permission::DELETE; | |
839 | } | |
840 | ||
841 | // CRM-9936 | |
842 | $reservedPermission = CRM_Core_Permission::check('administer reserved groups'); | |
843 | ||
844 | $links = self::actionLinks(); | |
845 | ||
846 | $allTypes = CRM_Core_OptionGroup::values('group_type'); | |
cbe32e2f | 847 | $values = $groupsToCount = array(); |
6a488035 | 848 | |
e3c75a92 | 849 | $visibility = CRM_Core_SelectValues::ufVisibility(); |
850 | ||
6a488035 TO |
851 | while ($object->fetch()) { |
852 | $permission = CRM_Contact_BAO_Group::checkPermission($object->id, $object->title); | |
0f8844a1 | 853 | //@todo CRM-12209 introduced an ACL check in the whereClause function |
854 | // it may be that this checking is now obsolete - or that what remains | |
855 | // should be removed to the whereClause (which is also accessed by getCount) | |
856 | ||
6a488035 TO |
857 | if ($permission) { |
858 | $newLinks = $links; | |
cbe32e2f CW |
859 | $values[$object->id] = array( |
860 | 'class' => array(), | |
861 | 'count' => '0', | |
862 | ); | |
6a488035 TO |
863 | CRM_Core_DAO::storeValues($object, $values[$object->id]); |
864 | if ($object->saved_search_id) { | |
865 | $values[$object->id]['title'] .= ' (' . ts('Smart Group') . ')'; | |
866 | // check if custom search, if so fix view link | |
867 | $customSearchID = CRM_Core_DAO::getFieldValue( | |
868 | 'CRM_Contact_DAO_SavedSearch', | |
869 | $object->saved_search_id, | |
870 | 'search_custom_id' | |
871 | ); | |
872 | ||
873 | if ($customSearchID) { | |
874 | $newLinks[CRM_Core_Action::VIEW]['url'] = 'civicrm/contact/search/custom'; | |
875 | $newLinks[CRM_Core_Action::VIEW]['qs'] = "reset=1&force=1&ssID={$object->saved_search_id}"; | |
876 | } | |
877 | } | |
878 | ||
879 | $action = array_sum(array_keys($newLinks)); | |
880 | ||
881 | // CRM-9936 | |
882 | if (array_key_exists('is_reserved', $object)) { | |
883 | //if group is reserved and I don't have reserved permission, suppress delete/edit | |
884 | if ($object->is_reserved && !$reservedPermission) { | |
885 | $action -= CRM_Core_Action::DELETE; | |
886 | $action -= CRM_Core_Action::UPDATE; | |
887 | $action -= CRM_Core_Action::DISABLE; | |
888 | } | |
889 | } | |
890 | ||
6a488035 TO |
891 | if (array_key_exists('is_active', $object)) { |
892 | if ($object->is_active) { | |
893 | $action -= CRM_Core_Action::ENABLE; | |
894 | } | |
895 | else { | |
7d644ac8 | 896 | $values[$object->id]['class'][] = 'disabled'; |
6a488035 TO |
897 | $action -= CRM_Core_Action::VIEW; |
898 | $action -= CRM_Core_Action::DISABLE; | |
899 | } | |
900 | } | |
901 | ||
902 | $action = $action & CRM_Core_Action::mask($groupPermissions); | |
903 | ||
e3c75a92 | 904 | $values[$object->id]['visibility'] = $visibility[$values[$object->id]['visibility']]; |
905 | ||
cbe32e2f | 906 | $groupsToCount[$object->saved_search_id ? 'civicrm_group_contact_cache' : 'civicrm_group_contact'][] = $object->id; |
7f4b344c | 907 | |
6a488035 TO |
908 | if (isset($values[$object->id]['group_type'])) { |
909 | $groupTypes = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
910 | substr($values[$object->id]['group_type'], 1, -1) | |
911 | ); | |
912 | $types = array(); | |
913 | foreach ($groupTypes as $type) { | |
914 | $types[] = CRM_Utils_Array::value($type, $allTypes); | |
915 | } | |
916 | $values[$object->id]['group_type'] = implode(', ', $types); | |
917 | } | |
918 | $values[$object->id]['action'] = CRM_Core_Action::formLink($newLinks, | |
919 | $action, | |
920 | array( | |
921 | 'id' => $object->id, | |
922 | 'ssid' => $object->saved_search_id, | |
87dab4a4 AH |
923 | ), |
924 | ts('more'), | |
925 | FALSE, | |
926 | 'group.selector.row', | |
927 | 'Group', | |
928 | $object->id | |
6a488035 TO |
929 | ); |
930 | ||
931 | // If group has children, add class for link to view children | |
ab8a593e | 932 | $values[$object->id]['is_parent'] = FALSE; |
6a488035 | 933 | if (array_key_exists('children', $values[$object->id])) { |
7d644ac8 | 934 | $values[$object->id]['class'][] = "crm-group-parent"; |
4eeb9a5b | 935 | $values[$object->id]['is_parent'] = TRUE; |
6a488035 TO |
936 | } |
937 | ||
938 | // If group is a child, add child class | |
939 | if (array_key_exists('parents', $values[$object->id])) { | |
7d644ac8 | 940 | $values[$object->id]['class'][] = "crm-group-child"; |
6a488035 TO |
941 | } |
942 | ||
943 | if ($groupOrg) { | |
944 | if ($object->org_id) { | |
945 | $contactUrl = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$object->org_id}"); | |
946 | $values[$object->id]['org_info'] = "<a href='{$contactUrl}'>{$object->org_name}</a>"; | |
947 | } | |
948 | else { | |
949 | $values[$object->id]['org_info'] = ''; // Empty cell | |
950 | } | |
951 | } | |
952 | else { | |
953 | $values[$object->id]['org_info'] = NULL; // Collapsed column if all cells are NULL | |
954 | } | |
955 | if ($object->created_id) { | |
956 | $contactUrl = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$object->created_id}"); | |
957 | $values[$object->id]['created_by'] = "<a href='{$contactUrl}'>{$object->created_by}</a>"; | |
958 | } | |
959 | } | |
960 | } | |
961 | ||
e96ee3a2 | 962 | // Get group counts - executes one query for regular groups and another for smart groups |
cbe32e2f | 963 | foreach ($groupsToCount as $table => $groups) { |
e96ee3a2 | 964 | $where = "g.group_id IN (" . implode(',', $groups) . ")"; |
b08455a1 | 965 | if ($table == 'civicrm_group_contact') { |
e96ee3a2 | 966 | $where .= " AND g.status = 'Added'"; |
b08455a1 | 967 | } |
e96ee3a2 CW |
968 | // Exclude deleted contacts |
969 | $where .= " and c.id = g.contact_id AND c.is_deleted = 0"; | |
970 | $dao = CRM_Core_DAO::executeQuery("SELECT g.group_id, COUNT(g.id) as `count` FROM $table g, civicrm_contact c WHERE $where GROUP BY g.group_id"); | |
22e263ad | 971 | while ($dao->fetch()) { |
cbe32e2f CW |
972 | $values[$dao->group_id]['count'] = $dao->count; |
973 | } | |
974 | } | |
975 | ||
6a488035 TO |
976 | return $values; |
977 | } | |
978 | ||
979 | /** | |
980 | * This function to get hierarchical list of groups (parent followed by children) | |
981 | * | |
77c5b619 TO |
982 | * @param array $groupIDs |
983 | * Array of group ids. | |
80ad33e7 | 984 | * |
e60f24eb | 985 | * @param NULL $parents |
80ad33e7 EM |
986 | * @param string $spacer |
987 | * @param bool $titleOnly | |
f828fa2c | 988 | * |
80ad33e7 | 989 | * @return array |
6a488035 | 990 | */ |
2da40d21 | 991 | public static function getGroupsHierarchy( |
f828fa2c | 992 | $groupIDs, |
90d1fee5 | 993 | $parents = NULL, |
6a488035 TO |
994 | $spacer = '<span class="child-indent"></span>', |
995 | $titleOnly = FALSE | |
90d1fee5 | 996 | ) { |
f828fa2c DL |
997 | if (empty($groupIDs)) { |
998 | return array(); | |
999 | } | |
1000 | ||
1001 | $groupIdString = '(' . implode(',', array_keys($groupIDs)) . ')'; | |
90d1fee5 | 1002 | // <span class="child-icon"></span> |
1003 | // need to return id, title (w/ spacer), description, visibility | |
1004 | ||
1005 | // We need to build a list of tags ordered by hierarchy and sorted by | |
b44e3f84 | 1006 | // name. The hierarchy will be communicated by an accumulation of |
90d1fee5 | 1007 | // separators in front of the name to give it a visual offset. |
1008 | // Instead of recursively making mysql queries, we'll make one big | |
b44e3f84 | 1009 | // query and build the hierarchy with the algorithm below. |
90d1fee5 | 1010 | $groups = array(); |
1011 | $args = array(1 => array($groupIdString, 'String')); | |
1012 | $query = " | |
f828fa2c DL |
1013 | SELECT id, title, description, visibility, parents |
1014 | FROM civicrm_group | |
1015 | WHERE id IN $groupIdString | |
1016 | "; | |
90d1fee5 | 1017 | if ($parents) { |
1018 | // group can have > 1 parent so parents may be comma separated list (eg. '1,2,5'). We just grab and match on 1st parent. | |
1019 | $parentArray = explode(',', $parents); | |
1020 | $parent = $parentArray[0]; | |
1021 | $args[2] = array($parent, 'Integer'); | |
1022 | $query .= " AND SUBSTRING_INDEX(parents, ',', 1) = %2"; | |
1023 | } | |
1024 | $query .= " ORDER BY title"; | |
1025 | $dao = CRM_Core_DAO::executeQuery($query, $args); | |
1026 | ||
1027 | // Sort the groups into the correct storage by the parent | |
1028 | // $roots represent the current leaf nodes that need to be checked for | |
1029 | // children. $rows represent the unplaced nodes | |
1030 | $roots = $rows = $allGroups = array(); | |
1031 | while ($dao->fetch()) { | |
1032 | $allGroups[$dao->id] = array( | |
1033 | 'title' => $dao->title, | |
1034 | 'visibility' => $dao->visibility, | |
21dfd5f5 | 1035 | 'description' => $dao->description, |
90d1fee5 | 1036 | ); |
1037 | ||
1038 | if ($dao->parents == $parents) { | |
1039 | $roots[] = array( | |
1040 | 'id' => $dao->id, | |
1041 | 'prefix' => '', | |
21dfd5f5 | 1042 | 'title' => $dao->title, |
90d1fee5 | 1043 | ); |
1044 | } | |
1045 | else { | |
1046 | // group can have > 1 parent so $dao->parents may be comma separated list (eg. '1,2,5'). Grab and match on 1st parent. | |
1047 | $parentArray = explode(',', $dao->parents); | |
1048 | $parent = $parentArray[0]; | |
1049 | $rows[] = array( | |
1050 | 'id' => $dao->id, | |
1051 | 'prefix' => '', | |
1052 | 'title' => $dao->title, | |
21dfd5f5 | 1053 | 'parents' => $parent, |
90d1fee5 | 1054 | ); |
1055 | } | |
1056 | } | |
1057 | $dao->free(); | |
1058 | // While we have nodes left to build, shift the first (alphabetically) | |
1059 | // node of the list, place it in our groups list and loop through the | |
1060 | // list of unplaced nodes to find its children. We make a copy to | |
1061 | // iterate through because we must modify the unplaced nodes list | |
1062 | // during the loop. | |
1063 | while (count($roots)) { | |
1064 | $new_roots = array(); | |
1065 | $current_rows = $rows; | |
1066 | $root = array_shift($roots); | |
1067 | $groups[$root['id']] = array($root['prefix'], $root['title']); | |
1068 | ||
1069 | // As you find the children, append them to the end of the new set | |
1070 | // of roots (maintain alphabetical ordering). Also remove the node | |
1071 | // from the set of unplaced nodes. | |
1072 | if (is_array($current_rows)) { | |
1073 | foreach ($current_rows as $key => $row) { | |
1074 | if ($row['parents'] == $root['id']) { | |
1075 | $new_roots[] = array( | |
1076 | 'id' => $row['id'], | |
1077 | 'prefix' => $groups[$root['id']][0] . $spacer, | |
21dfd5f5 | 1078 | 'title' => $row['title'], |
90d1fee5 | 1079 | ); |
1080 | unset($rows[$key]); | |
1081 | } | |
1082 | } | |
1083 | } | |
1084 | ||
1085 | //As a group, insert the new roots into the beginning of the roots | |
1086 | //list. This maintains the hierarchical ordering of the tags. | |
1087 | $roots = array_merge($new_roots, $roots); | |
1088 | } | |
1089 | ||
1090 | // below is the redundant looping to ensure child groups are populated in the case where user does not have | |
1091 | // access to parent groups ( esp. using ACL permissions and logged in user can assess only child groups ) | |
1092 | foreach ($rows as $value) { | |
1093 | $groups[$value['id']] = array($value['prefix'], $value['title']); | |
1094 | } | |
1095 | // Prefix titles with the calcuated spacing to give the visual | |
1096 | // appearance of ordering when transformed into HTML in the form layer. Add description and visibility. | |
1097 | $groupsReturn = array(); | |
1098 | foreach ($groups as $key => $value) { | |
1099 | if ($titleOnly) { | |
1100 | $groupsReturn[$key] = $value[0] . $value[1]; | |
1101 | } | |
1102 | else { | |
1103 | $groupsReturn[$key] = array( | |
1104 | 'title' => $value[0] . $value[1], | |
1105 | 'description' => $allGroups[$key]['description'], | |
1106 | 'visibility' => $allGroups[$key]['visibility'], | |
1107 | ); | |
1108 | } | |
1109 | } | |
1110 | ||
1111 | return $groupsReturn; | |
6a488035 TO |
1112 | } |
1113 | ||
86538308 | 1114 | /** |
c490a46a | 1115 | * @param array $params |
86538308 | 1116 | * |
e60f24eb | 1117 | * @return NULL|string |
86538308 | 1118 | */ |
00be9182 | 1119 | public static function getGroupCount(&$params) { |
6a488035 TO |
1120 | $whereClause = self::whereClause($params, FALSE); |
1121 | $query = "SELECT COUNT(*) FROM civicrm_group groups"; | |
1122 | ||
a7488080 | 1123 | if (!empty($params['created_by'])) { |
6a488035 TO |
1124 | $query .= " |
1125 | INNER JOIN civicrm_contact createdBy | |
1126 | ON createdBy.id = groups.created_id"; | |
1127 | } | |
1128 | $query .= " | |
1129 | WHERE {$whereClause}"; | |
1130 | return CRM_Core_DAO::singleValueQuery($query, $params); | |
1131 | } | |
1132 | ||
47c89d6b | 1133 | /** |
fe482240 | 1134 | * Generate permissioned where clause for group search. |
c490a46a | 1135 | * @param array $params |
47c89d6b EM |
1136 | * @param bool $sortBy |
1137 | * @param bool $excludeHidden | |
1138 | * | |
1139 | * @return string | |
1140 | */ | |
00be9182 | 1141 | public static function whereClause(&$params, $sortBy = TRUE, $excludeHidden = TRUE) { |
6a488035 | 1142 | $values = array(); |
6a488035 TO |
1143 | $title = CRM_Utils_Array::value('title', $params); |
1144 | if ($title) { | |
1145 | $clauses[] = "groups.title LIKE %1"; | |
1146 | if (strpos($title, '%') !== FALSE) { | |
1147 | $params[1] = array($title, 'String', FALSE); | |
1148 | } | |
1149 | else { | |
1150 | $params[1] = array($title, 'String', TRUE); | |
1151 | } | |
1152 | } | |
1153 | ||
1154 | $groupType = CRM_Utils_Array::value('group_type', $params); | |
1155 | if ($groupType) { | |
1156 | $types = explode(',', $groupType); | |
1157 | if (!empty($types)) { | |
353ffa53 | 1158 | $clauses[] = 'groups.group_type LIKE %2'; |
6a488035 | 1159 | $typeString = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $types) . CRM_Core_DAO::VALUE_SEPARATOR; |
353ffa53 | 1160 | $params[2] = array($typeString, 'String', TRUE); |
6a488035 TO |
1161 | } |
1162 | } | |
1163 | ||
1164 | $visibility = CRM_Utils_Array::value('visibility', $params); | |
1165 | if ($visibility) { | |
1166 | $clauses[] = 'groups.visibility = %3'; | |
1167 | $params[3] = array($visibility, 'String'); | |
1168 | } | |
1169 | ||
1170 | $groupStatus = CRM_Utils_Array::value('status', $params); | |
1171 | if ($groupStatus) { | |
1172 | switch ($groupStatus) { | |
1173 | case 1: | |
1174 | $clauses[] = 'groups.is_active = 1'; | |
1175 | $params[4] = array($groupStatus, 'Integer'); | |
1176 | break; | |
1177 | ||
1178 | case 2: | |
1179 | $clauses[] = 'groups.is_active = 0'; | |
1180 | $params[4] = array($groupStatus, 'Integer'); | |
1181 | break; | |
1182 | ||
1183 | case 3: | |
1184 | $clauses[] = '(groups.is_active = 0 OR groups.is_active = 1 )'; | |
1185 | break; | |
1186 | } | |
1187 | } | |
1188 | ||
1189 | $parentsOnly = CRM_Utils_Array::value('parentsOnly', $params); | |
1190 | if ($parentsOnly) { | |
1191 | $clauses[] = 'groups.parents IS NULL'; | |
1192 | } | |
1193 | ||
1194 | // only show child groups of a specific parent group | |
1195 | $parent_id = CRM_Utils_Array::value('parent_id', $params); | |
1196 | if ($parent_id) { | |
1197 | $clauses[] = 'groups.id IN (SELECT child_group_id FROM civicrm_group_nesting WHERE parent_group_id = %5)'; | |
1198 | $params[5] = array($parent_id, 'Integer'); | |
1199 | } | |
1200 | ||
1201 | if ($createdBy = CRM_Utils_Array::value('created_by', $params)) { | |
1202 | $clauses[] = "createdBy.sort_name LIKE %6"; | |
1203 | if (strpos($createdBy, '%') !== FALSE) { | |
1204 | $params[6] = array($createdBy, 'String', FALSE); | |
1205 | } | |
1206 | else { | |
1207 | $params[6] = array($createdBy, 'String', TRUE); | |
1208 | } | |
1209 | } | |
1210 | ||
6a488035 TO |
1211 | if (empty($clauses)) { |
1212 | $clauses[] = 'groups.is_active = 1'; | |
1213 | } | |
1214 | ||
1215 | if ($excludeHidden) { | |
1216 | $clauses[] = 'groups.is_hidden = 0'; | |
1217 | } | |
33421d01 | 1218 | |
aaac0e0b | 1219 | $clauses[] = self::getPermissionClause(); |
6a488035 TO |
1220 | |
1221 | return implode(' AND ', $clauses); | |
1222 | } | |
1223 | ||
1224 | /** | |
fe482240 | 1225 | * Define action links. |
6a488035 | 1226 | * |
a6c01b45 CW |
1227 | * @return array |
1228 | * array of action links | |
6a488035 | 1229 | */ |
00be9182 | 1230 | public static function actionLinks() { |
6a488035 TO |
1231 | $links = array( |
1232 | CRM_Core_Action::VIEW => array( | |
1233 | 'name' => ts('Contacts'), | |
1234 | 'url' => 'civicrm/group/search', | |
1235 | 'qs' => 'reset=1&force=1&context=smog&gid=%%id%%', | |
1236 | 'title' => ts('Group Contacts'), | |
1237 | ), | |
1238 | CRM_Core_Action::UPDATE => array( | |
1239 | 'name' => ts('Settings'), | |
1240 | 'url' => 'civicrm/group', | |
1241 | 'qs' => 'reset=1&action=update&id=%%id%%', | |
1242 | 'title' => ts('Edit Group'), | |
1243 | ), | |
1244 | CRM_Core_Action::DISABLE => array( | |
1245 | 'name' => ts('Disable'), | |
4d17a233 | 1246 | 'ref' => 'crm-enable-disable', |
6a488035 TO |
1247 | 'title' => ts('Disable Group'), |
1248 | ), | |
1249 | CRM_Core_Action::ENABLE => array( | |
1250 | 'name' => ts('Enable'), | |
4d17a233 | 1251 | 'ref' => 'crm-enable-disable', |
6a488035 TO |
1252 | 'title' => ts('Enable Group'), |
1253 | ), | |
1254 | CRM_Core_Action::DELETE => array( | |
1255 | 'name' => ts('Delete'), | |
1256 | 'url' => 'civicrm/group', | |
1257 | 'qs' => 'reset=1&action=delete&id=%%id%%', | |
1258 | 'title' => ts('Delete Group'), | |
1259 | ), | |
1260 | ); | |
1261 | ||
1262 | return $links; | |
1263 | } | |
1264 | ||
86538308 EM |
1265 | /** |
1266 | * @param $whereClause | |
100fef9d | 1267 | * @param array $whereParams |
86538308 EM |
1268 | * |
1269 | * @return string | |
1270 | */ | |
00be9182 | 1271 | public function pagerAtoZ($whereClause, $whereParams) { |
6a488035 TO |
1272 | $query = " |
1273 | SELECT DISTINCT UPPER(LEFT(groups.title, 1)) as sort_name | |
1274 | FROM civicrm_group groups | |
1275 | WHERE $whereClause | |
1276 | ORDER BY LEFT(groups.title, 1) | |
1277 | "; | |
1278 | $dao = CRM_Core_DAO::executeQuery($query, $whereParams); | |
1279 | ||
1280 | return CRM_Utils_PagerAToZ::getAToZBar($dao, $this->_sortByCharacter, TRUE); | |
1281 | } | |
96025800 | 1282 | |
6a488035 | 1283 | } |