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