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