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