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