Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
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) { |
6a488035 TO |
133 | $params = array(array('group', 'IN', array($id => 1), 0, 0)); |
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 | ||
359 | // convert params if array type | |
360 | if (isset($params['group_type'])) { | |
361 | if (is_array($params['group_type'])) { | |
362 | $params['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
af3e0975 | 363 | $params['group_type'] |
353ffa53 | 364 | ) . CRM_Core_DAO::VALUE_SEPARATOR; |
6a488035 | 365 | } |
568fcb22 | 366 | else { |
367 | $params['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . $params['group_type'] . CRM_Core_DAO::VALUE_SEPARATOR; | |
368 | } | |
6a488035 TO |
369 | } |
370 | else { | |
82d7eb3d | 371 | $params['group_type'] = NULL; |
6a488035 TO |
372 | } |
373 | ||
481a74f4 | 374 | $session = CRM_Core_Session::singleton(); |
d0dfb649 PJ |
375 | $cid = $session->get('userID'); |
376 | // this action is add | |
377 | if ($cid && empty($params['id'])) { | |
6a488035 TO |
378 | $params['created_id'] = $cid; |
379 | } | |
d0dfb649 PJ |
380 | // this action is update |
381 | if ($cid && !empty($params['id'])) { | |
382 | $params['modified_id'] = $cid; | |
383 | } | |
6a488035 | 384 | |
d4fba42a SL |
385 | // CRM-19068. |
386 | // Validate parents parameter when creating group. | |
387 | if (isset($params['parents'])) { | |
388 | if (is_array($params['parents'])) { | |
389 | foreach ($params['parents'] as $parent => $dc) { | |
52202a20 | 390 | CRM_Utils_Type::validate($parent, 'Integer'); |
d4fba42a SL |
391 | } |
392 | } | |
393 | else { | |
52202a20 | 394 | CRM_Utils_Type::validate($params['parents'], 'Integer'); |
d4fba42a SL |
395 | } |
396 | } | |
6a488035 TO |
397 | $group = new CRM_Contact_BAO_Group(); |
398 | $group->copyValues($params); | |
57c93d72 E |
399 | //@todo very hacky fix for the fact this function wants to receive 'parents' as an array further down but |
400 | // needs it as a separated string for the DB. Preferred approaches are having the copyParams or save fn | |
401 | // use metadata to translate the array to the appropriate DB type or altering the param in the api layer, | |
402 | // or at least altering the param in same section as 'group_type' rather than repeating here. However, further down | |
403 | // we need the $params one to be in it's original form & we are not sure what test coverage we have on that | |
22e263ad | 404 | if (isset($group->parents) && is_array($group->parents)) { |
57c93d72 | 405 | $group->parents = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, |
353ffa53 TO |
406 | array_keys($group->parents) |
407 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
57c93d72 | 408 | } |
a7488080 | 409 | if (empty($params['id']) && |
e2b7c67d | 410 | !$nameParam |
0aab99e7 | 411 | ) { |
6a488035 TO |
412 | $group->name .= "_tmp"; |
413 | } | |
414 | $group->save(); | |
415 | ||
416 | if (!$group->id) { | |
417 | return NULL; | |
418 | } | |
419 | ||
a7488080 | 420 | if (empty($params['id']) && |
e2b7c67d | 421 | !$nameParam |
0aab99e7 | 422 | ) { |
6a488035 TO |
423 | $group->name = substr($group->name, 0, -4) . "_{$group->id}"; |
424 | } | |
425 | ||
426 | $group->buildClause(); | |
427 | $group->save(); | |
428 | ||
429 | // add custom field values | |
a7488080 | 430 | if (!empty($params['custom'])) { |
6a488035 TO |
431 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_group', $group->id); |
432 | } | |
433 | ||
434 | // make the group, child of domain/site group by default. | |
435 | $domainGroupID = CRM_Core_BAO_Domain::getGroupId(); | |
436 | if (CRM_Utils_Array::value('no_parent', $params) !== 1) { | |
437 | if (empty($params['parents']) && | |
438 | $domainGroupID != $group->id && | |
aaffa79f | 439 | Civi::settings()->get('is_enabled') && |
6a488035 TO |
440 | !CRM_Contact_BAO_GroupNesting::hasParentGroups($group->id) |
441 | ) { | |
442 | // if no parent present and the group doesn't already have any parents, | |
443 | // make sure site group goes as parent | |
444 | $params['parents'] = array($domainGroupID => 1); | |
445 | } | |
446 | elseif (array_key_exists('parents', $params) && !is_array($params['parents'])) { | |
447 | $params['parents'] = array($params['parents'] => 1); | |
448 | } | |
449 | ||
450 | if (!empty($params['parents'])) { | |
451 | foreach ($params['parents'] as $parentId => $dnc) { | |
52202a20 | 452 | if (CRM_Utils_Type::validate($parentId, 'Integer')) { |
d4fba42a SL |
453 | if ($parentId && !CRM_Contact_BAO_GroupNesting::isParentChild($parentId, $group->id)) { |
454 | CRM_Contact_BAO_GroupNesting::add($parentId, $group->id); | |
455 | } | |
6a488035 TO |
456 | } |
457 | } | |
458 | } | |
459 | ||
6a488035 TO |
460 | // this is always required, since we don't know when a |
461 | // parent group is removed | |
462 | CRM_Contact_BAO_GroupNestingCache::update(); | |
463 | ||
464 | // update group contact cache for all parent groups | |
465 | $parentIds = CRM_Contact_BAO_GroupNesting::getParentGroupIds($group->id); | |
466 | foreach ($parentIds as $parentId) { | |
467 | CRM_Contact_BAO_GroupContactCache::add($parentId); | |
468 | } | |
469 | } | |
470 | ||
a7488080 | 471 | if (!empty($params['organization_id'])) { |
6a488035 TO |
472 | $groupOrg = $params; |
473 | $groupOrg['group_id'] = $group->id; | |
474 | CRM_Contact_BAO_GroupOrganization::add($groupOrg); | |
475 | } | |
476 | ||
430bf241 | 477 | CRM_Utils_System::flushCache(); |
6a488035 TO |
478 | CRM_Contact_BAO_GroupContactCache::add($group->id); |
479 | ||
a7488080 | 480 | if (!empty($params['id'])) { |
6a488035 TO |
481 | CRM_Utils_Hook::post('edit', 'Group', $group->id, $group); |
482 | } | |
483 | else { | |
484 | CRM_Utils_Hook::post('create', 'Group', $group->id, $group); | |
485 | } | |
486 | ||
487 | $recentOther = array(); | |
488 | if (CRM_Core_Permission::check('edit groups')) { | |
489 | $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/group', 'reset=1&action=update&id=' . $group->id); | |
490 | // currently same permission we are using for delete a group | |
491 | $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/group', 'reset=1&action=delete&id=' . $group->id); | |
492 | } | |
493 | ||
494 | // add the recently added group (unless hidden: CRM-6432) | |
495 | if (!$group->is_hidden) { | |
496 | CRM_Utils_Recent::add($group->title, | |
497 | CRM_Utils_System::url('civicrm/group/search', 'reset=1&force=1&context=smog&gid=' . $group->id), | |
498 | $group->id, | |
499 | 'Group', | |
500 | NULL, | |
501 | NULL, | |
502 | $recentOther | |
503 | ); | |
504 | } | |
505 | return $group; | |
506 | } | |
507 | ||
508 | /** | |
100fef9d | 509 | * Given a saved search compute the clause and the tables |
6a488035 TO |
510 | * and store it for future use |
511 | */ | |
00be9182 | 512 | public function buildClause() { |
1dbdc161 | 513 | $params = array(array('group', 'IN', array($this->id), 0, 0)); |
6a488035 TO |
514 | |
515 | if (!empty($params)) { | |
516 | $tables = $whereTables = array(); | |
517 | $this->where_clause = CRM_Contact_BAO_Query::getWhereClause($params, NULL, $tables, $whereTables); | |
518 | if (!empty($tables)) { | |
519 | $this->select_tables = serialize($tables); | |
520 | } | |
521 | if (!empty($whereTables)) { | |
522 | $this->where_tables = serialize($whereTables); | |
523 | } | |
524 | } | |
6a488035 TO |
525 | } |
526 | ||
527 | /** | |
fe482240 | 528 | * Defines a new smart group. |
6a488035 | 529 | * |
77c5b619 TO |
530 | * @param array $params |
531 | * Associative array of parameters. | |
6a488035 | 532 | * |
72b3a70c CW |
533 | * @return CRM_Contact_BAO_Group|NULL |
534 | * The new group BAO (if created) | |
6a488035 TO |
535 | */ |
536 | public static function createSmartGroup(&$params) { | |
a7488080 | 537 | if (!empty($params['formValues'])) { |
6a488035 TO |
538 | $ssParams = $params; |
539 | unset($ssParams['id']); | |
540 | if (isset($ssParams['saved_search_id'])) { | |
541 | $ssParams['id'] = $ssParams['saved_search_id']; | |
542 | } | |
543 | ||
544 | $savedSearch = CRM_Contact_BAO_SavedSearch::create($params); | |
545 | ||
546 | $params['saved_search_id'] = $savedSearch->id; | |
547 | } | |
548 | else { | |
549 | return NULL; | |
550 | } | |
551 | ||
552 | return self::create($params); | |
553 | } | |
554 | ||
555 | /** | |
fe482240 | 556 | * Update the is_active flag in the db. |
6a488035 | 557 | * |
77c5b619 TO |
558 | * @param int $id |
559 | * Id of the database record. | |
560 | * @param bool $isActive | |
561 | * Value we want to set the is_active field. | |
6a488035 | 562 | * |
16b10e64 | 563 | * @return CRM_Core_DAO|null |
3f8d2862 | 564 | * DAO object on success, NULL otherwise |
6a488035 | 565 | */ |
00be9182 | 566 | public static function setIsActive($id, $isActive) { |
6a488035 TO |
567 | return CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_Group', $id, 'is_active', $isActive); |
568 | } | |
569 | ||
570 | /** | |
100fef9d | 571 | * Build the condition to retrieve groups. |
6a488035 | 572 | * |
77c5b619 TO |
573 | * @param string $groupType |
574 | * Type of group(Access/Mailing) OR the key of the group. | |
3f8d2862 | 575 | * @param bool $excludeHidden exclude hidden groups. |
6a488035 | 576 | * |
a6c01b45 | 577 | * @return string |
6a488035 | 578 | */ |
00be9182 | 579 | public static function groupTypeCondition($groupType = NULL, $excludeHidden = TRUE) { |
6a488035 TO |
580 | $value = NULL; |
581 | if ($groupType == 'Mailing') { | |
582 | $value = CRM_Core_DAO::VALUE_SEPARATOR . '2' . CRM_Core_DAO::VALUE_SEPARATOR; | |
583 | } | |
584 | elseif ($groupType == 'Access') { | |
585 | $value = CRM_Core_DAO::VALUE_SEPARATOR . '1' . CRM_Core_DAO::VALUE_SEPARATOR; | |
586 | } | |
9b873358 | 587 | elseif (!empty($groupType)) { |
6a488035 TO |
588 | // ie we have been given the group key |
589 | $value = CRM_Core_DAO::VALUE_SEPARATOR . $groupType . CRM_Core_DAO::VALUE_SEPARATOR; | |
590 | } | |
591 | ||
592 | $condition = NULL; | |
593 | if ($excludeHidden) { | |
594 | $condition = "is_hidden = 0"; | |
595 | } | |
596 | ||
597 | if ($value) { | |
598 | if ($condition) { | |
599 | $condition .= " AND group_type LIKE '%$value%'"; | |
600 | } | |
601 | else { | |
602 | $condition = "group_type LIKE '%$value%'"; | |
603 | } | |
604 | } | |
605 | ||
606 | return $condition; | |
607 | } | |
608 | ||
47c89d6b | 609 | /** |
fe482240 | 610 | * Get permission relevant clauses. |
47c89d6b EM |
611 | * CRM-12209 |
612 | * | |
aaac0e0b EM |
613 | * @param bool $force |
614 | * | |
47c89d6b EM |
615 | * @return array |
616 | */ | |
aaac0e0b EM |
617 | public static function getPermissionClause($force = FALSE) { |
618 | static $clause = 1; | |
47c89d6b | 619 | static $retrieved = FALSE; |
9486be34 | 620 | if (!$retrieved || $force) { |
621 | if (CRM_Core_Permission::check('view all contacts') || CRM_Core_Permission::check('edit all contacts')) { | |
622 | $clause = 1; | |
47c89d6b | 623 | } |
aaac0e0b | 624 | else { |
9486be34 | 625 | //get the allowed groups for the current user |
626 | $groups = CRM_ACL_API::group(CRM_ACL_API::VIEW); | |
627 | if (!empty($groups)) { | |
628 | $groupList = implode(', ', array_values($groups)); | |
629 | $clause = "groups.id IN ( $groupList ) "; | |
630 | } | |
631 | else { | |
632 | $clause = '1 = 0'; | |
633 | } | |
aaac0e0b | 634 | } |
47c89d6b EM |
635 | } |
636 | $retrieved = TRUE; | |
637 | return $clause; | |
638 | } | |
639 | ||
86538308 EM |
640 | /** |
641 | * @return string | |
642 | */ | |
6a488035 TO |
643 | public function __toString() { |
644 | return $this->title; | |
645 | } | |
646 | ||
647 | /** | |
648 | * This function create the hidden smart group when user perform | |
b44e3f84 | 649 | * contact search and want to send mailing to search contacts. |
6a488035 | 650 | * |
77c5b619 TO |
651 | * @param array $params |
652 | * ( reference ) an assoc array of name/value pairs. | |
6a488035 | 653 | * |
a6c01b45 CW |
654 | * @return array |
655 | * ( smartGroupId, ssId ) smart group id and saved search id | |
6a488035 | 656 | */ |
00be9182 | 657 | public static function createHiddenSmartGroup($params) { |
6a488035 TO |
658 | $ssId = CRM_Utils_Array::value('saved_search_id', $params); |
659 | ||
660 | //add mapping record only for search builder saved search | |
661 | $mappingId = NULL; | |
662 | if ($params['search_context'] == 'builder') { | |
663 | //save the mapping for search builder | |
664 | if (!$ssId) { | |
665 | //save record in mapping table | |
353ffa53 | 666 | $temp = array(); |
6a488035 | 667 | $mappingParams = array('mapping_type' => 'Search Builder'); |
353ffa53 TO |
668 | $mapping = CRM_Core_BAO_Mapping::add($mappingParams, $temp); |
669 | $mappingId = $mapping->id; | |
6a488035 TO |
670 | } |
671 | else { | |
672 | //get the mapping id from saved search | |
673 | $savedSearch = new CRM_Contact_BAO_SavedSearch(); | |
674 | $savedSearch->id = $ssId; | |
675 | $savedSearch->find(TRUE); | |
676 | $mappingId = $savedSearch->mapping_id; | |
677 | } | |
678 | ||
679 | //save mapping fields | |
680 | CRM_Core_BAO_Mapping::saveMappingFields($params['form_values'], $mappingId); | |
681 | } | |
682 | ||
683 | //create/update saved search record. | |
684 | $savedSearch = new CRM_Contact_BAO_SavedSearch(); | |
685 | $savedSearch->id = $ssId; | |
686 | $savedSearch->form_values = serialize($params['form_values']); | |
687 | $savedSearch->mapping_id = $mappingId; | |
688 | $savedSearch->search_custom_id = CRM_Utils_Array::value('search_custom_id', $params); | |
689 | $savedSearch->save(); | |
690 | ||
691 | $ssId = $savedSearch->id; | |
692 | if (!$ssId) { | |
693 | return NULL; | |
694 | } | |
695 | ||
696 | $smartGroupId = NULL; | |
a7488080 | 697 | if (!empty($params['saved_search_id'])) { |
6a488035 TO |
698 | $smartGroupId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $ssId, 'id', 'saved_search_id'); |
699 | } | |
700 | else { | |
701 | //create group only when new saved search. | |
702 | $groupParams = array( | |
703 | 'title' => "Hidden Smart Group {$ssId}", | |
704 | 'is_active' => CRM_Utils_Array::value('is_active', $params, 1), | |
705 | 'is_hidden' => CRM_Utils_Array::value('is_hidden', $params, 1), | |
706 | 'group_type' => CRM_Utils_Array::value('group_type', $params), | |
707 | 'visibility' => CRM_Utils_Array::value('visibility', $params), | |
708 | 'saved_search_id' => $ssId, | |
709 | ); | |
710 | ||
711 | $smartGroup = self::create($groupParams); | |
712 | $smartGroupId = $smartGroup->id; | |
713 | } | |
714 | ||
715 | return array($smartGroupId, $ssId); | |
716 | } | |
717 | ||
718 | /** | |
fe482240 | 719 | * wrapper for ajax group selector. |
6a488035 | 720 | * |
77c5b619 TO |
721 | * @param array $params |
722 | * Associated array for params record id. | |
6a488035 | 723 | * |
a6c01b45 CW |
724 | * @return array |
725 | * associated array of group list | |
16b10e64 CW |
726 | * -rp = rowcount |
727 | * -page= offset | |
d3e86119 TO |
728 | * @todo there seems little reason for the small number of functions that call this to pass in |
729 | * params that then need to be translated in this function since they are coding them when calling | |
6a488035 | 730 | */ |
bca4d720 | 731 | static public function getGroupListSelector(&$params) { |
6a488035 | 732 | // format the params |
353ffa53 | 733 | $params['offset'] = ($params['page'] - 1) * $params['rp']; |
6a488035 | 734 | $params['rowCount'] = $params['rp']; |
353ffa53 | 735 | $params['sort'] = CRM_Utils_Array::value('sortBy', $params); |
6a488035 TO |
736 | |
737 | // get groups | |
738 | $groups = CRM_Contact_BAO_Group::getGroupList($params); | |
739 | ||
740 | //skip total if we are making call to show only children | |
a7488080 | 741 | if (empty($params['parent_id'])) { |
6a488035 TO |
742 | // add total |
743 | $params['total'] = CRM_Contact_BAO_Group::getGroupCount($params); | |
744 | ||
745 | // get all the groups | |
746 | $allGroups = CRM_Core_PseudoConstant::allGroup(); | |
747 | } | |
748 | ||
749 | // format params and add links | |
750 | $groupList = array(); | |
3b448eca JL |
751 | foreach ($groups as $id => $value) { |
752 | $group = array(); | |
753 | $group['group_id'] = $value['id']; | |
754 | $group['count'] = $value['count']; | |
755 | $group['title'] = $value['title']; | |
756 | ||
757 | // append parent names if in search mode | |
758 | if (empty($params['parent_id']) && !empty($value['parents'])) { | |
759 | $group['parent_id'] = $value['parents']; | |
760 | $groupIds = explode(',', $value['parents']); | |
761 | $title = array(); | |
762 | foreach ($groupIds as $gId) { | |
763 | $title[] = $allGroups[$gId]; | |
764 | } | |
765 | $group['title'] .= '<div class="crm-row-parent-name"><em>' . ts('Child of') . '</em>: ' . implode(', ', $title) . '</div>'; | |
766 | $value['class'] = array_diff($value['class'], array('crm-row-parent')); | |
767 | } | |
768 | $group['DT_RowId'] = 'row_' . $value['id']; | |
46d33c7a | 769 | if (empty($params['parentsOnly'])) { |
fe6e4e4b | 770 | foreach ($value['class'] as $id => $class) { |
be632a8a | 771 | if ($class == 'crm-group-parent') { |
3b448eca | 772 | unset($value['class'][$id]); |
6a488035 | 773 | } |
6a488035 | 774 | } |
3b448eca JL |
775 | } |
776 | $group['DT_RowClass'] = 'crm-entity ' . implode(' ', $value['class']); | |
777 | $group['DT_RowAttr'] = array(); | |
778 | $group['DT_RowAttr']['data-id'] = $value['id']; | |
779 | $group['DT_RowAttr']['data-entity'] = 'group'; | |
6a488035 | 780 | |
3b448eca | 781 | $group['description'] = CRM_Utils_Array::value('description', $value); |
6a488035 | 782 | |
3b448eca JL |
783 | if (!empty($value['group_type'])) { |
784 | $group['group_type'] = $value['group_type']; | |
785 | } | |
786 | else { | |
787 | $group['group_type'] = ''; | |
6a488035 | 788 | } |
3b448eca JL |
789 | |
790 | $group['visibility'] = $value['visibility']; | |
791 | $group['links'] = $value['action']; | |
792 | $group['org_info'] = CRM_Utils_Array::value('org_info', $value); | |
793 | $group['created_by'] = CRM_Utils_Array::value('created_by', $value); | |
794 | ||
795 | $group['is_parent'] = $value['is_parent']; | |
796 | ||
1e223804 | 797 | array_push($groupList, $group); |
6a488035 | 798 | } |
3b448eca JL |
799 | |
800 | $groupsDT = array(); | |
801 | $groupsDT['data'] = $groupList; | |
802 | $groupsDT['recordsTotal'] = $params['total']; | |
803 | $groupsDT['recordsFiltered'] = $params['total']; | |
804 | ||
805 | return $groupsDT; | |
6a488035 TO |
806 | } |
807 | ||
808 | /** | |
fe482240 | 809 | * This function to get list of groups. |
6a488035 | 810 | * |
77c5b619 TO |
811 | * @param array $params |
812 | * Associated array for params. | |
80ad33e7 EM |
813 | * |
814 | * @return array | |
6a488035 | 815 | */ |
00be9182 | 816 | public static function getGroupList(&$params) { |
6a488035 TO |
817 | $config = CRM_Core_Config::singleton(); |
818 | ||
819 | $whereClause = self::whereClause($params, FALSE); | |
f90c40b9 | 820 | |
6a488035 TO |
821 | //$this->pagerAToZ( $whereClause, $params ); |
822 | ||
823 | if (!empty($params['rowCount']) && | |
824 | $params['rowCount'] > 0 | |
825 | ) { | |
826 | $limit = " LIMIT {$params['offset']}, {$params['rowCount']} "; | |
827 | } | |
828 | ||
829 | $orderBy = ' ORDER BY groups.title asc'; | |
21d32567 DL |
830 | if (!empty($params['sort'])) { |
831 | $orderBy = ' ORDER BY ' . CRM_Utils_Type::escape($params['sort'], 'String'); | |
1d19a8f2 CW |
832 | |
833 | // CRM-16905 - Sort by count cannot be done with sql | |
834 | if (strpos($params['sort'], 'count') === 0) { | |
835 | $orderBy = $limit = ''; | |
836 | } | |
6a488035 TO |
837 | } |
838 | ||
839 | $select = $from = $where = ""; | |
840 | $groupOrg = FALSE; | |
841 | if (CRM_Core_Permission::check('administer Multiple Organizations') && | |
842 | CRM_Core_Permission::isMultisiteEnabled() | |
843 | ) { | |
844 | $select = ", contact.display_name as org_name, contact.id as org_id"; | |
845 | $from = " LEFT JOIN civicrm_group_organization gOrg | |
846 | ON gOrg.group_id = groups.id | |
847 | LEFT JOIN civicrm_contact contact | |
848 | ON contact.id = gOrg.organization_id "; | |
849 | ||
850 | //get the Organization ID | |
851 | $orgID = CRM_Utils_Request::retrieve('oid', 'Positive', CRM_Core_DAO::$_nullObject); | |
852 | if ($orgID) { | |
853 | $where = " AND gOrg.organization_id = {$orgID}"; | |
854 | } | |
855 | ||
856 | $groupOrg = TRUE; | |
857 | } | |
858 | ||
859 | $query = " | |
cbe32e2f | 860 | SELECT groups.*, createdBy.sort_name as created_by {$select} |
6a488035 | 861 | FROM civicrm_group groups |
7f4b344c CW |
862 | LEFT JOIN civicrm_contact createdBy |
863 | ON createdBy.id = groups.created_id | |
864 | {$from} | |
6a488035 TO |
865 | WHERE $whereClause {$where} |
866 | {$orderBy} | |
867 | {$limit}"; | |
868 | ||
869 | $object = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Contact_DAO_Group'); | |
870 | ||
871 | //FIXME CRM-4418, now we are handling delete separately | |
872 | //if we introduce 'delete for group' make sure to handle here. | |
873 | $groupPermissions = array(CRM_Core_Permission::VIEW); | |
874 | if (CRM_Core_Permission::check('edit groups')) { | |
875 | $groupPermissions[] = CRM_Core_Permission::EDIT; | |
876 | $groupPermissions[] = CRM_Core_Permission::DELETE; | |
877 | } | |
878 | ||
879 | // CRM-9936 | |
880 | $reservedPermission = CRM_Core_Permission::check('administer reserved groups'); | |
881 | ||
882 | $links = self::actionLinks(); | |
883 | ||
884 | $allTypes = CRM_Core_OptionGroup::values('group_type'); | |
cbe32e2f | 885 | $values = $groupsToCount = array(); |
6a488035 | 886 | |
e3c75a92 | 887 | $visibility = CRM_Core_SelectValues::ufVisibility(); |
888 | ||
6a488035 TO |
889 | while ($object->fetch()) { |
890 | $permission = CRM_Contact_BAO_Group::checkPermission($object->id, $object->title); | |
0f8844a1 | 891 | //@todo CRM-12209 introduced an ACL check in the whereClause function |
892 | // it may be that this checking is now obsolete - or that what remains | |
893 | // should be removed to the whereClause (which is also accessed by getCount) | |
894 | ||
6a488035 TO |
895 | if ($permission) { |
896 | $newLinks = $links; | |
cbe32e2f CW |
897 | $values[$object->id] = array( |
898 | 'class' => array(), | |
899 | 'count' => '0', | |
900 | ); | |
6a488035 | 901 | CRM_Core_DAO::storeValues($object, $values[$object->id]); |
93abd831 | 902 | |
6a488035 TO |
903 | if ($object->saved_search_id) { |
904 | $values[$object->id]['title'] .= ' (' . ts('Smart Group') . ')'; | |
905 | // check if custom search, if so fix view link | |
906 | $customSearchID = CRM_Core_DAO::getFieldValue( | |
907 | 'CRM_Contact_DAO_SavedSearch', | |
908 | $object->saved_search_id, | |
909 | 'search_custom_id' | |
910 | ); | |
911 | ||
912 | if ($customSearchID) { | |
913 | $newLinks[CRM_Core_Action::VIEW]['url'] = 'civicrm/contact/search/custom'; | |
914 | $newLinks[CRM_Core_Action::VIEW]['qs'] = "reset=1&force=1&ssID={$object->saved_search_id}"; | |
915 | } | |
916 | } | |
917 | ||
918 | $action = array_sum(array_keys($newLinks)); | |
919 | ||
920 | // CRM-9936 | |
921 | if (array_key_exists('is_reserved', $object)) { | |
922 | //if group is reserved and I don't have reserved permission, suppress delete/edit | |
923 | if ($object->is_reserved && !$reservedPermission) { | |
924 | $action -= CRM_Core_Action::DELETE; | |
925 | $action -= CRM_Core_Action::UPDATE; | |
926 | $action -= CRM_Core_Action::DISABLE; | |
927 | } | |
928 | } | |
929 | ||
6a488035 TO |
930 | if (array_key_exists('is_active', $object)) { |
931 | if ($object->is_active) { | |
932 | $action -= CRM_Core_Action::ENABLE; | |
933 | } | |
934 | else { | |
7d644ac8 | 935 | $values[$object->id]['class'][] = 'disabled'; |
6a488035 TO |
936 | $action -= CRM_Core_Action::VIEW; |
937 | $action -= CRM_Core_Action::DISABLE; | |
938 | } | |
939 | } | |
940 | ||
941 | $action = $action & CRM_Core_Action::mask($groupPermissions); | |
942 | ||
e3c75a92 | 943 | $values[$object->id]['visibility'] = $visibility[$values[$object->id]['visibility']]; |
944 | ||
cbe32e2f | 945 | $groupsToCount[$object->saved_search_id ? 'civicrm_group_contact_cache' : 'civicrm_group_contact'][] = $object->id; |
7f4b344c | 946 | |
6a488035 TO |
947 | if (isset($values[$object->id]['group_type'])) { |
948 | $groupTypes = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
949 | substr($values[$object->id]['group_type'], 1, -1) | |
950 | ); | |
951 | $types = array(); | |
952 | foreach ($groupTypes as $type) { | |
953 | $types[] = CRM_Utils_Array::value($type, $allTypes); | |
954 | } | |
955 | $values[$object->id]['group_type'] = implode(', ', $types); | |
956 | } | |
957 | $values[$object->id]['action'] = CRM_Core_Action::formLink($newLinks, | |
958 | $action, | |
959 | array( | |
960 | 'id' => $object->id, | |
961 | 'ssid' => $object->saved_search_id, | |
87dab4a4 AH |
962 | ), |
963 | ts('more'), | |
964 | FALSE, | |
965 | 'group.selector.row', | |
966 | 'Group', | |
967 | $object->id | |
6a488035 TO |
968 | ); |
969 | ||
970 | // If group has children, add class for link to view children | |
ab8a593e | 971 | $values[$object->id]['is_parent'] = FALSE; |
6a488035 | 972 | if (array_key_exists('children', $values[$object->id])) { |
7d644ac8 | 973 | $values[$object->id]['class'][] = "crm-group-parent"; |
4eeb9a5b | 974 | $values[$object->id]['is_parent'] = TRUE; |
6a488035 TO |
975 | } |
976 | ||
977 | // If group is a child, add child class | |
978 | if (array_key_exists('parents', $values[$object->id])) { | |
7d644ac8 | 979 | $values[$object->id]['class'][] = "crm-group-child"; |
6a488035 TO |
980 | } |
981 | ||
982 | if ($groupOrg) { | |
983 | if ($object->org_id) { | |
984 | $contactUrl = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$object->org_id}"); | |
985 | $values[$object->id]['org_info'] = "<a href='{$contactUrl}'>{$object->org_name}</a>"; | |
986 | } | |
987 | else { | |
988 | $values[$object->id]['org_info'] = ''; // Empty cell | |
989 | } | |
990 | } | |
991 | else { | |
992 | $values[$object->id]['org_info'] = NULL; // Collapsed column if all cells are NULL | |
993 | } | |
994 | if ($object->created_id) { | |
995 | $contactUrl = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$object->created_id}"); | |
996 | $values[$object->id]['created_by'] = "<a href='{$contactUrl}'>{$object->created_by}</a>"; | |
997 | } | |
998 | } | |
999 | } | |
1000 | ||
e96ee3a2 | 1001 | // Get group counts - executes one query for regular groups and another for smart groups |
cbe32e2f | 1002 | foreach ($groupsToCount as $table => $groups) { |
e96ee3a2 | 1003 | $where = "g.group_id IN (" . implode(',', $groups) . ")"; |
b08455a1 | 1004 | if ($table == 'civicrm_group_contact') { |
e96ee3a2 | 1005 | $where .= " AND g.status = 'Added'"; |
b08455a1 | 1006 | } |
e96ee3a2 CW |
1007 | // Exclude deleted contacts |
1008 | $where .= " and c.id = g.contact_id AND c.is_deleted = 0"; | |
1009 | $dao = CRM_Core_DAO::executeQuery("SELECT g.group_id, COUNT(g.id) as `count` FROM $table g, civicrm_contact c WHERE $where GROUP BY g.group_id"); | |
22e263ad | 1010 | while ($dao->fetch()) { |
cbe32e2f CW |
1011 | $values[$dao->group_id]['count'] = $dao->count; |
1012 | } | |
1013 | } | |
1014 | ||
1d19a8f2 CW |
1015 | // CRM-16905 - Sort by count cannot be done with sql |
1016 | if (!empty($params['sort']) && strpos($params['sort'], 'count') === 0) { | |
1017 | usort($values, function($a, $b) { | |
1018 | return $a['count'] - $b['count']; | |
1019 | }); | |
1020 | if (strpos($params['sort'], 'desc')) { | |
1021 | $values = array_reverse($values, TRUE); | |
1022 | } | |
1023 | return array_slice($values, $params['offset'], $params['rowCount']); | |
1024 | } | |
1025 | ||
6a488035 TO |
1026 | return $values; |
1027 | } | |
1028 | ||
1029 | /** | |
1030 | * This function to get hierarchical list of groups (parent followed by children) | |
1031 | * | |
77c5b619 TO |
1032 | * @param array $groupIDs |
1033 | * Array of group ids. | |
80ad33e7 | 1034 | * |
e60f24eb | 1035 | * @param NULL $parents |
80ad33e7 EM |
1036 | * @param string $spacer |
1037 | * @param bool $titleOnly | |
f828fa2c | 1038 | * |
80ad33e7 | 1039 | * @return array |
6a488035 | 1040 | */ |
2da40d21 | 1041 | public static function getGroupsHierarchy( |
f828fa2c | 1042 | $groupIDs, |
90d1fee5 | 1043 | $parents = NULL, |
6a488035 TO |
1044 | $spacer = '<span class="child-indent"></span>', |
1045 | $titleOnly = FALSE | |
90d1fee5 | 1046 | ) { |
f828fa2c DL |
1047 | if (empty($groupIDs)) { |
1048 | return array(); | |
1049 | } | |
1050 | ||
1051 | $groupIdString = '(' . implode(',', array_keys($groupIDs)) . ')'; | |
90d1fee5 | 1052 | // <span class="child-icon"></span> |
1053 | // need to return id, title (w/ spacer), description, visibility | |
1054 | ||
1055 | // We need to build a list of tags ordered by hierarchy and sorted by | |
b44e3f84 | 1056 | // name. The hierarchy will be communicated by an accumulation of |
90d1fee5 | 1057 | // separators in front of the name to give it a visual offset. |
1058 | // Instead of recursively making mysql queries, we'll make one big | |
b44e3f84 | 1059 | // query and build the hierarchy with the algorithm below. |
90d1fee5 | 1060 | $groups = array(); |
1061 | $args = array(1 => array($groupIdString, 'String')); | |
1062 | $query = " | |
f828fa2c DL |
1063 | SELECT id, title, description, visibility, parents |
1064 | FROM civicrm_group | |
1065 | WHERE id IN $groupIdString | |
1066 | "; | |
90d1fee5 | 1067 | if ($parents) { |
1068 | // group can have > 1 parent so parents may be comma separated list (eg. '1,2,5'). We just grab and match on 1st parent. | |
1069 | $parentArray = explode(',', $parents); | |
1070 | $parent = $parentArray[0]; | |
1071 | $args[2] = array($parent, 'Integer'); | |
1072 | $query .= " AND SUBSTRING_INDEX(parents, ',', 1) = %2"; | |
1073 | } | |
1074 | $query .= " ORDER BY title"; | |
1075 | $dao = CRM_Core_DAO::executeQuery($query, $args); | |
1076 | ||
1077 | // Sort the groups into the correct storage by the parent | |
1078 | // $roots represent the current leaf nodes that need to be checked for | |
1079 | // children. $rows represent the unplaced nodes | |
1080 | $roots = $rows = $allGroups = array(); | |
1081 | while ($dao->fetch()) { | |
1082 | $allGroups[$dao->id] = array( | |
1083 | 'title' => $dao->title, | |
1084 | 'visibility' => $dao->visibility, | |
21dfd5f5 | 1085 | 'description' => $dao->description, |
90d1fee5 | 1086 | ); |
1087 | ||
1088 | if ($dao->parents == $parents) { | |
1089 | $roots[] = array( | |
1090 | 'id' => $dao->id, | |
1091 | 'prefix' => '', | |
21dfd5f5 | 1092 | 'title' => $dao->title, |
90d1fee5 | 1093 | ); |
1094 | } | |
1095 | else { | |
1096 | // group can have > 1 parent so $dao->parents may be comma separated list (eg. '1,2,5'). Grab and match on 1st parent. | |
1097 | $parentArray = explode(',', $dao->parents); | |
1098 | $parent = $parentArray[0]; | |
1099 | $rows[] = array( | |
1100 | 'id' => $dao->id, | |
1101 | 'prefix' => '', | |
1102 | 'title' => $dao->title, | |
21dfd5f5 | 1103 | 'parents' => $parent, |
90d1fee5 | 1104 | ); |
1105 | } | |
1106 | } | |
1107 | $dao->free(); | |
1108 | // While we have nodes left to build, shift the first (alphabetically) | |
1109 | // node of the list, place it in our groups list and loop through the | |
1110 | // list of unplaced nodes to find its children. We make a copy to | |
1111 | // iterate through because we must modify the unplaced nodes list | |
1112 | // during the loop. | |
1113 | while (count($roots)) { | |
1114 | $new_roots = array(); | |
1115 | $current_rows = $rows; | |
1116 | $root = array_shift($roots); | |
1117 | $groups[$root['id']] = array($root['prefix'], $root['title']); | |
1118 | ||
1119 | // As you find the children, append them to the end of the new set | |
1120 | // of roots (maintain alphabetical ordering). Also remove the node | |
1121 | // from the set of unplaced nodes. | |
1122 | if (is_array($current_rows)) { | |
1123 | foreach ($current_rows as $key => $row) { | |
1124 | if ($row['parents'] == $root['id']) { | |
1125 | $new_roots[] = array( | |
1126 | 'id' => $row['id'], | |
1127 | 'prefix' => $groups[$root['id']][0] . $spacer, | |
21dfd5f5 | 1128 | 'title' => $row['title'], |
90d1fee5 | 1129 | ); |
1130 | unset($rows[$key]); | |
1131 | } | |
1132 | } | |
1133 | } | |
1134 | ||
1135 | //As a group, insert the new roots into the beginning of the roots | |
1136 | //list. This maintains the hierarchical ordering of the tags. | |
1137 | $roots = array_merge($new_roots, $roots); | |
1138 | } | |
1139 | ||
1140 | // below is the redundant looping to ensure child groups are populated in the case where user does not have | |
1141 | // access to parent groups ( esp. using ACL permissions and logged in user can assess only child groups ) | |
1142 | foreach ($rows as $value) { | |
1143 | $groups[$value['id']] = array($value['prefix'], $value['title']); | |
1144 | } | |
1145 | // Prefix titles with the calcuated spacing to give the visual | |
1146 | // appearance of ordering when transformed into HTML in the form layer. Add description and visibility. | |
1147 | $groupsReturn = array(); | |
1148 | foreach ($groups as $key => $value) { | |
1149 | if ($titleOnly) { | |
1150 | $groupsReturn[$key] = $value[0] . $value[1]; | |
1151 | } | |
1152 | else { | |
1153 | $groupsReturn[$key] = array( | |
1154 | 'title' => $value[0] . $value[1], | |
1155 | 'description' => $allGroups[$key]['description'], | |
1156 | 'visibility' => $allGroups[$key]['visibility'], | |
1157 | ); | |
1158 | } | |
1159 | } | |
1160 | ||
1161 | return $groupsReturn; | |
6a488035 TO |
1162 | } |
1163 | ||
86538308 | 1164 | /** |
c490a46a | 1165 | * @param array $params |
86538308 | 1166 | * |
e60f24eb | 1167 | * @return NULL|string |
86538308 | 1168 | */ |
00be9182 | 1169 | public static function getGroupCount(&$params) { |
6a488035 TO |
1170 | $whereClause = self::whereClause($params, FALSE); |
1171 | $query = "SELECT COUNT(*) FROM civicrm_group groups"; | |
1172 | ||
a7488080 | 1173 | if (!empty($params['created_by'])) { |
6a488035 TO |
1174 | $query .= " |
1175 | INNER JOIN civicrm_contact createdBy | |
1176 | ON createdBy.id = groups.created_id"; | |
1177 | } | |
1178 | $query .= " | |
1179 | WHERE {$whereClause}"; | |
1180 | return CRM_Core_DAO::singleValueQuery($query, $params); | |
1181 | } | |
1182 | ||
47c89d6b | 1183 | /** |
fe482240 | 1184 | * Generate permissioned where clause for group search. |
c490a46a | 1185 | * @param array $params |
47c89d6b EM |
1186 | * @param bool $sortBy |
1187 | * @param bool $excludeHidden | |
1188 | * | |
1189 | * @return string | |
1190 | */ | |
00be9182 | 1191 | public static function whereClause(&$params, $sortBy = TRUE, $excludeHidden = TRUE) { |
6a488035 | 1192 | $values = array(); |
6a488035 TO |
1193 | $title = CRM_Utils_Array::value('title', $params); |
1194 | if ($title) { | |
1195 | $clauses[] = "groups.title LIKE %1"; | |
1196 | if (strpos($title, '%') !== FALSE) { | |
1197 | $params[1] = array($title, 'String', FALSE); | |
1198 | } | |
1199 | else { | |
1200 | $params[1] = array($title, 'String', TRUE); | |
1201 | } | |
1202 | } | |
1203 | ||
1204 | $groupType = CRM_Utils_Array::value('group_type', $params); | |
1205 | if ($groupType) { | |
1206 | $types = explode(',', $groupType); | |
1207 | if (!empty($types)) { | |
353ffa53 | 1208 | $clauses[] = 'groups.group_type LIKE %2'; |
6a488035 | 1209 | $typeString = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $types) . CRM_Core_DAO::VALUE_SEPARATOR; |
353ffa53 | 1210 | $params[2] = array($typeString, 'String', TRUE); |
6a488035 TO |
1211 | } |
1212 | } | |
1213 | ||
1214 | $visibility = CRM_Utils_Array::value('visibility', $params); | |
1215 | if ($visibility) { | |
1216 | $clauses[] = 'groups.visibility = %3'; | |
1217 | $params[3] = array($visibility, 'String'); | |
1218 | } | |
1219 | ||
1220 | $groupStatus = CRM_Utils_Array::value('status', $params); | |
1221 | if ($groupStatus) { | |
1222 | switch ($groupStatus) { | |
1223 | case 1: | |
1224 | $clauses[] = 'groups.is_active = 1'; | |
1225 | $params[4] = array($groupStatus, 'Integer'); | |
1226 | break; | |
1227 | ||
1228 | case 2: | |
1229 | $clauses[] = 'groups.is_active = 0'; | |
1230 | $params[4] = array($groupStatus, 'Integer'); | |
1231 | break; | |
1232 | ||
1233 | case 3: | |
1234 | $clauses[] = '(groups.is_active = 0 OR groups.is_active = 1 )'; | |
1235 | break; | |
1236 | } | |
1237 | } | |
1238 | ||
1239 | $parentsOnly = CRM_Utils_Array::value('parentsOnly', $params); | |
1240 | if ($parentsOnly) { | |
1241 | $clauses[] = 'groups.parents IS NULL'; | |
1242 | } | |
1243 | ||
1244 | // only show child groups of a specific parent group | |
1245 | $parent_id = CRM_Utils_Array::value('parent_id', $params); | |
1246 | if ($parent_id) { | |
1247 | $clauses[] = 'groups.id IN (SELECT child_group_id FROM civicrm_group_nesting WHERE parent_group_id = %5)'; | |
1248 | $params[5] = array($parent_id, 'Integer'); | |
1249 | } | |
1250 | ||
1251 | if ($createdBy = CRM_Utils_Array::value('created_by', $params)) { | |
1252 | $clauses[] = "createdBy.sort_name LIKE %6"; | |
1253 | if (strpos($createdBy, '%') !== FALSE) { | |
1254 | $params[6] = array($createdBy, 'String', FALSE); | |
1255 | } | |
1256 | else { | |
1257 | $params[6] = array($createdBy, 'String', TRUE); | |
1258 | } | |
1259 | } | |
1260 | ||
6a488035 TO |
1261 | if (empty($clauses)) { |
1262 | $clauses[] = 'groups.is_active = 1'; | |
1263 | } | |
1264 | ||
1265 | if ($excludeHidden) { | |
1266 | $clauses[] = 'groups.is_hidden = 0'; | |
1267 | } | |
33421d01 | 1268 | |
aaac0e0b | 1269 | $clauses[] = self::getPermissionClause(); |
6a488035 TO |
1270 | |
1271 | return implode(' AND ', $clauses); | |
1272 | } | |
1273 | ||
1274 | /** | |
fe482240 | 1275 | * Define action links. |
6a488035 | 1276 | * |
a6c01b45 CW |
1277 | * @return array |
1278 | * array of action links | |
6a488035 | 1279 | */ |
00be9182 | 1280 | public static function actionLinks() { |
6a488035 TO |
1281 | $links = array( |
1282 | CRM_Core_Action::VIEW => array( | |
1283 | 'name' => ts('Contacts'), | |
1284 | 'url' => 'civicrm/group/search', | |
1285 | 'qs' => 'reset=1&force=1&context=smog&gid=%%id%%', | |
1286 | 'title' => ts('Group Contacts'), | |
1287 | ), | |
1288 | CRM_Core_Action::UPDATE => array( | |
1289 | 'name' => ts('Settings'), | |
1290 | 'url' => 'civicrm/group', | |
1291 | 'qs' => 'reset=1&action=update&id=%%id%%', | |
1292 | 'title' => ts('Edit Group'), | |
1293 | ), | |
1294 | CRM_Core_Action::DISABLE => array( | |
1295 | 'name' => ts('Disable'), | |
4d17a233 | 1296 | 'ref' => 'crm-enable-disable', |
6a488035 TO |
1297 | 'title' => ts('Disable Group'), |
1298 | ), | |
1299 | CRM_Core_Action::ENABLE => array( | |
1300 | 'name' => ts('Enable'), | |
4d17a233 | 1301 | 'ref' => 'crm-enable-disable', |
6a488035 TO |
1302 | 'title' => ts('Enable Group'), |
1303 | ), | |
1304 | CRM_Core_Action::DELETE => array( | |
1305 | 'name' => ts('Delete'), | |
1306 | 'url' => 'civicrm/group', | |
1307 | 'qs' => 'reset=1&action=delete&id=%%id%%', | |
1308 | 'title' => ts('Delete Group'), | |
1309 | ), | |
1310 | ); | |
1311 | ||
1312 | return $links; | |
1313 | } | |
1314 | ||
86538308 EM |
1315 | /** |
1316 | * @param $whereClause | |
100fef9d | 1317 | * @param array $whereParams |
86538308 EM |
1318 | * |
1319 | * @return string | |
1320 | */ | |
00be9182 | 1321 | public function pagerAtoZ($whereClause, $whereParams) { |
6a488035 TO |
1322 | $query = " |
1323 | SELECT DISTINCT UPPER(LEFT(groups.title, 1)) as sort_name | |
1324 | FROM civicrm_group groups | |
1325 | WHERE $whereClause | |
1326 | ORDER BY LEFT(groups.title, 1) | |
1327 | "; | |
1328 | $dao = CRM_Core_DAO::executeQuery($query, $whereParams); | |
1329 | ||
1330 | return CRM_Utils_PagerAToZ::getAToZBar($dao, $this->_sortByCharacter, TRUE); | |
1331 | } | |
96025800 | 1332 | |
c955c26c SL |
1333 | /** |
1334 | * Assign Test Value. | |
1335 | * | |
1336 | * @param string $fieldName | |
1337 | * @param array $fieldDef | |
1338 | * @param int $counter | |
1339 | */ | |
1340 | protected function assignTestValue($fieldName, &$fieldDef, $counter) { | |
1341 | if ($fieldName == 'children' || $fieldName = 'parents') { | |
1342 | $this->{$fieldName} = "NULL"; | |
1343 | } | |
1344 | else { | |
1345 | parent::assignTestValues($fieldaName, $fieldDef, $counter); | |
1346 | } | |
1347 | } | |
1348 | ||
6a488035 | 1349 | } |