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