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