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