Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
5a409b50 | 35 | * This class generates form components for search-result tasks. |
6a488035 | 36 | */ |
31aaf096 | 37 | class CRM_Contact_Form_Task extends CRM_Core_Form_Task { |
6a488035 TO |
38 | |
39 | /** | |
100fef9d | 40 | * The task being performed |
6a488035 TO |
41 | * |
42 | * @var int | |
43 | */ | |
44 | protected $_task; | |
45 | ||
46 | /** | |
47 | * The array that holds all the contact ids | |
48 | * | |
49 | * @var array | |
50 | */ | |
51 | public $_contactIds; | |
52 | ||
53 | /** | |
54 | * The array that holds all the contact types | |
55 | * | |
56 | * @var array | |
57 | */ | |
58 | public $_contactTypes; | |
59 | ||
60 | /** | |
61 | * The additional clause that we restrict the search with | |
62 | * | |
63 | * @var string | |
64 | */ | |
65 | protected $_componentClause = NULL; | |
66 | ||
67 | /** | |
68 | * The name of the temp table where we store the contact IDs | |
69 | * | |
70 | * @var string | |
71 | */ | |
72 | protected $_componentTable = NULL; | |
73 | ||
74 | /** | |
75 | * The array that holds all the component ids | |
76 | * | |
77 | * @var array | |
78 | */ | |
79 | protected $_componentIds; | |
80 | ||
81 | /** | |
82 | * This includes the submitted values of the search form | |
83 | */ | |
84 | static protected $_searchFormValues; | |
85 | ||
86 | /** | |
d8689418 | 87 | * Build all the data structures needed to build the form. |
8ef12e64 | 88 | */ |
00be9182 | 89 | public function preProcess() { |
6a488035 TO |
90 | self::preProcessCommon($this); |
91 | } | |
92 | ||
86538308 | 93 | /** |
d8689418 EM |
94 | * Common pre-processing function. |
95 | * | |
c490a46a | 96 | * @param CRM_Core_Form $form |
a0174743 MW |
97 | * |
98 | * @throws \CRM_Core_Exception | |
86538308 | 99 | */ |
2b089ce1 | 100 | public static function preProcessCommon(&$form) { |
6a488035 TO |
101 | $form->_contactIds = array(); |
102 | $form->_contactTypes = array(); | |
103 | ||
fe9b66c1 | 104 | $useTable = (CRM_Utils_System::getClassName($form->controller->getStateMachine()) == 'CRM_Export_StateMachine_Standalone'); |
2b089ce1 | 105 | |
78b22ce9 | 106 | $isStandAlone = in_array('task', $form->urlPath) || in_array('standalone', $form->urlPath); |
061ff68c | 107 | if ($isStandAlone) { |
108 | list($form->_task, $title) = CRM_Contact_Task::getTaskAndTitleByClass(get_class($form)); | |
109 | if (!array_key_exists($form->_task, CRM_Contact_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission()))) { | |
f2774867 | 110 | CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.')); |
061ff68c | 111 | } |
78b22ce9 | 112 | $form->_contactIds = explode(',', CRM_Utils_Request::retrieve('cids', 'CommaSeparatedIntegers', $form, TRUE)); |
061ff68c | 113 | if (empty($form->_contactIds)) { |
6bc234fa | 114 | CRM_Core_Error::statusBounce(ts('No Contacts Selected')); |
061ff68c | 115 | } |
116 | $form->setTitle($title); | |
117 | } | |
118 | ||
6a488035 TO |
119 | // get the submitted values of the search form |
120 | // we'll need to get fv from either search or adv search in the future | |
121 | $fragment = 'search'; | |
122 | if ($form->_action == CRM_Core_Action::ADVANCED) { | |
123 | self::$_searchFormValues = $form->controller->exportValues('Advanced'); | |
124 | $fragment .= '/advanced'; | |
125 | } | |
126 | elseif ($form->_action == CRM_Core_Action::PROFILE) { | |
127 | self::$_searchFormValues = $form->controller->exportValues('Builder'); | |
128 | $fragment .= '/builder'; | |
129 | } | |
130 | elseif ($form->_action == CRM_Core_Action::COPY) { | |
131 | self::$_searchFormValues = $form->controller->exportValues('Custom'); | |
132 | $fragment .= '/custom'; | |
133 | } | |
061ff68c | 134 | elseif (!$isStandAlone) { |
6a488035 TO |
135 | self::$_searchFormValues = $form->controller->exportValues('Basic'); |
136 | } | |
137 | ||
138 | //set the user context for redirection of task actions | |
139 | $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form); | |
140 | $urlParams = 'force=1'; | |
141 | if (CRM_Utils_Rule::qfKey($qfKey)) { | |
142 | $urlParams .= "&qfKey=$qfKey"; | |
143 | } | |
144 | ||
145 | $cacheKey = "civicrm search {$qfKey}"; | |
146 | ||
147 | $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams); | |
148 | $session = CRM_Core_Session::singleton(); | |
149 | $session->replaceUserContext($url); | |
150 | ||
151 | $form->_task = CRM_Utils_Array::value('task', self::$_searchFormValues); | |
152 | $crmContactTaskTasks = CRM_Contact_Task::taskTitles(); | |
153 | $form->assign('taskName', CRM_Utils_Array::value($form->_task, $crmContactTaskTasks)); | |
154 | ||
155 | if ($useTable) { | |
90e5222f | 156 | $tempTable = CRM_Utils_SQL_TempTable::build()->setCategory('tskact')->setDurable()->setId($qfKey)->setUtf8(); |
157 | $form->_componentTable = $tempTable->getName(); | |
158 | $tempTable->drop(); | |
159 | $tempTable->createWithColumns('contact_id int primary key'); | |
6a488035 TO |
160 | } |
161 | ||
162 | // all contacts or action = save a search | |
163 | if ((CRM_Utils_Array::value('radio_ts', self::$_searchFormValues) == 'ts_all') || | |
164 | ($form->_task == CRM_Contact_Task::SAVE_SEARCH) | |
165 | ) { | |
66ceb5d9 | 166 | // since we don't store all contacts in prevnextcache, when user selects "all" use query to retrieve contacts |
8e1a7c71 | 167 | // rather than prevnext cache table for most of the task actions except export where we rebuild query to fetch |
168 | // final result set | |
169 | if ($useTable) { | |
b7994703 | 170 | $allCids = Civi::service('prevnext')->getSelection($cacheKey, "getall"); |
8e1a7c71 | 171 | } |
172 | else { | |
fe9b66c1 | 173 | $allCids[$cacheKey] = self::getContactIds($form); |
8e1a7c71 | 174 | } |
6a488035 TO |
175 | |
176 | $form->_contactIds = array(); | |
177 | if ($useTable) { | |
178 | $count = 0; | |
179 | $insertString = array(); | |
180 | foreach ($allCids[$cacheKey] as $cid => $ignore) { | |
181 | $count++; | |
182 | $insertString[] = " ( {$cid} ) "; | |
183 | if ($count % 200 == 0) { | |
184 | $string = implode(',', $insertString); | |
185 | $sql = "REPLACE INTO {$form->_componentTable} ( contact_id ) VALUES $string"; | |
186 | CRM_Core_DAO::executeQuery($sql); | |
187 | $insertString = array(); | |
188 | } | |
189 | } | |
190 | if (!empty($insertString)) { | |
191 | $string = implode(',', $insertString); | |
192 | $sql = "REPLACE INTO {$form->_componentTable} ( contact_id ) VALUES $string"; | |
193 | CRM_Core_DAO::executeQuery($sql); | |
194 | } | |
195 | } | |
061ff68c | 196 | elseif (empty($form->_contactIds)) { |
6a488035 TO |
197 | // filter duplicates here |
198 | // CRM-7058 | |
199 | // might be better to do this in the query, but that logic is a bit complex | |
200 | // and it decides when to use distinct based on input criteria, which needs | |
201 | // to be fixed and optimized. | |
202 | ||
203 | foreach ($allCids[$cacheKey] as $cid => $ignore) { | |
204 | $form->_contactIds[] = $cid; | |
205 | } | |
206 | } | |
207 | } | |
208 | elseif (CRM_Utils_Array::value('radio_ts', self::$_searchFormValues) == 'ts_sel') { | |
209 | // selected contacts only | |
210 | // need to perform action on only selected contacts | |
211 | $insertString = array(); | |
212 | ||
b44e3f84 | 213 | // refire sql in case of custom search |
6a488035 TO |
214 | if ($form->_action == CRM_Core_Action::COPY) { |
215 | // selected contacts only | |
216 | // need to perform action on only selected contacts | |
217 | foreach (self::$_searchFormValues as $name => $value) { | |
218 | if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) { | |
219 | $contactID = substr($name, CRM_Core_Form::CB_PREFIX_LEN); | |
220 | if ($useTable) { | |
221 | $insertString[] = " ( {$contactID} ) "; | |
222 | } | |
223 | else { | |
224 | $form->_contactIds[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN); | |
225 | } | |
226 | } | |
227 | } | |
228 | } | |
229 | else { | |
5d272ea2 | 230 | // fetching selected contact ids of passed cache key |
b7994703 | 231 | $selectedCids = Civi::service('prevnext')->getSelection($cacheKey); |
5d272ea2 | 232 | foreach ($selectedCids[$cacheKey] as $selectedCid => $ignore) { |
233 | if ($useTable) { | |
234 | $insertString[] = " ( {$selectedCid} ) "; | |
235 | } | |
236 | else { | |
237 | $form->_contactIds[] = $selectedCid; | |
238 | } | |
6a488035 TO |
239 | } |
240 | } | |
8ef12e64 | 241 | |
6a488035 TO |
242 | if (!empty($insertString)) { |
243 | $string = implode(',', $insertString); | |
244 | $sql = "REPLACE INTO {$form->_componentTable} ( contact_id ) VALUES $string"; | |
245 | CRM_Core_DAO::executeQuery($sql); | |
246 | } | |
247 | } | |
248 | ||
249 | //contact type for pick up profiles as per selected contact types with subtypes | |
250 | //CRM-5521 | |
251 | if ($selectedTypes = CRM_Utils_Array::value('contact_type', self::$_searchFormValues)) { | |
252 | if (!is_array($selectedTypes)) { | |
253 | $selectedTypes = explode(' ', $selectedTypes); | |
254 | } | |
255 | foreach ($selectedTypes as $ct => $dontcare) { | |
256 | if (strpos($ct, CRM_Core_DAO::VALUE_SEPARATOR) === FALSE) { | |
257 | $form->_contactTypes[] = $ct; | |
258 | } | |
259 | else { | |
260 | $separator = strpos($ct, CRM_Core_DAO::VALUE_SEPARATOR); | |
261 | $form->_contactTypes[] = substr($ct, $separator + 1); | |
262 | } | |
263 | } | |
264 | } | |
265 | ||
6a488035 | 266 | if (CRM_Utils_Array::value('radio_ts', self::$_searchFormValues) == 'ts_sel' |
5d272ea2 | 267 | && ($form->_action != CRM_Core_Action::COPY) |
268 | ) { | |
6a488035 | 269 | $sel = CRM_Utils_Array::value('radio_ts', self::$_searchFormValues); |
5d272ea2 | 270 | $form->assign('searchtype', $sel); |
0dbe04b1 | 271 | $result = self::getSelectedContactNames(); |
6a488035 TO |
272 | $form->assign("value", $result); |
273 | } | |
8ef12e64 | 274 | |
6a488035 TO |
275 | if (!empty($form->_contactIds)) { |
276 | $form->_componentClause = ' contact_a.id IN ( ' . implode(',', $form->_contactIds) . ' ) '; | |
277 | $form->assign('totalSelectedContacts', count($form->_contactIds)); | |
278 | ||
279 | $form->_componentIds = $form->_contactIds; | |
280 | } | |
281 | } | |
282 | ||
283 | /** | |
fe9b66c1 MW |
284 | * Get the contact ids for: |
285 | * - "Select Records: All xx records" | |
286 | * - custom search (FIXME: does this still apply to custom search?). | |
287 | * When we call this function we are not using the prev/next cache | |
d8689418 | 288 | * |
fe9b66c1 MW |
289 | * @param $form CRM_Core_Form |
290 | * | |
291 | * @return array $contactIds | |
6a488035 | 292 | */ |
fe9b66c1 | 293 | public static function getContactIds($form) { |
6a488035 TO |
294 | // need to perform action on all contacts |
295 | // fire the query again and get the contact id's + display name | |
296 | $sortID = NULL; | |
fe9b66c1 MW |
297 | if ($form->get(CRM_Utils_Sort::SORT_ID)) { |
298 | $sortID = CRM_Utils_Sort::sortIDValue($form->get(CRM_Utils_Sort::SORT_ID), | |
299 | $form->get(CRM_Utils_Sort::SORT_DIRECTION) | |
6a488035 TO |
300 | ); |
301 | } | |
302 | ||
fe9b66c1 | 303 | $selectorName = $form->controller->selectorName(); |
6a488035 | 304 | |
fe9b66c1 MW |
305 | $fv = $form->get('formValues'); |
306 | $customClass = $form->get('customSearchClass'); | |
6a488035 TO |
307 | $returnProperties = CRM_Core_BAO_Mapping::returnProperties(self::$_searchFormValues); |
308 | ||
5d272ea2 | 309 | $selector = new $selectorName($customClass, $fv, NULL, $returnProperties); |
6a488035 | 310 | |
fe9b66c1 | 311 | $params = $form->get('queryParams'); |
6a488035 TO |
312 | |
313 | // fix for CRM-5165 | |
fe9b66c1 | 314 | $sortByCharacter = $form->get('sortByCharacter'); |
a7cddb8c | 315 | if ($sortByCharacter && $sortByCharacter != 1) { |
6a488035 TO |
316 | $params[] = array('sortByCharacter', '=', $sortByCharacter, 0, 0); |
317 | } | |
fe9b66c1 | 318 | $queryOperator = $form->get('queryOperator'); |
6a488035 TO |
319 | if (!$queryOperator) { |
320 | $queryOperator = 'AND'; | |
321 | } | |
ae066a2b | 322 | $dao = $selector->contactIDQuery($params, $sortID, |
5d272ea2 | 323 | CRM_Utils_Array::value('display_relationship_type', $fv), |
6a488035 TO |
324 | $queryOperator |
325 | ); | |
326 | ||
327 | $contactIds = array(); | |
5d272ea2 | 328 | while ($dao->fetch()) { |
6a488035 TO |
329 | $contactIds[$dao->contact_id] = $dao->contact_id; |
330 | } | |
331 | ||
332 | return $contactIds; | |
333 | } | |
334 | ||
335 | ||
336 | /** | |
ee0ce2ef | 337 | * Set default values for the form. Relationship that in edit/view action. |
6a488035 | 338 | * |
ee0ce2ef | 339 | * The default values are retrieved from the database. |
6a488035 | 340 | * |
e2046b33 | 341 | * @return array |
6a488035 | 342 | */ |
00be9182 | 343 | public function setDefaultValues() { |
6a488035 TO |
344 | $defaults = array(); |
345 | return $defaults; | |
346 | } | |
347 | ||
348 | /** | |
57507ae6 | 349 | * Add the rules for form. |
6a488035 | 350 | */ |
00be9182 | 351 | public function addRules() { |
5d272ea2 | 352 | } |
6a488035 TO |
353 | |
354 | /** | |
5c9ff055 | 355 | * Build the form object. |
6a488035 TO |
356 | */ |
357 | public function buildQuickForm() { | |
358 | $this->addDefaultButtons(ts('Confirm Action')); | |
359 | } | |
360 | ||
361 | /** | |
ee0ce2ef | 362 | * Process the form after the input has been submitted and validated. |
6a488035 | 363 | */ |
5d272ea2 | 364 | public function postProcess() { |
365 | } | |
366 | ||
6a488035 | 367 | /** |
57507ae6 EM |
368 | * Simple shell that derived classes can call to add form buttons. |
369 | * | |
370 | * Allows customized title for the main Submit | |
6a488035 | 371 | * |
77c5b619 TO |
372 | * @param string $title |
373 | * Title of the main button. | |
374 | * @param string $nextType | |
375 | * Button type for the form after processing. | |
77b97be7 EM |
376 | * @param string $backType |
377 | * @param bool $submitOnce | |
6a488035 | 378 | */ |
00be9182 | 379 | public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) { |
6a488035 TO |
380 | $this->addButtons(array( |
381 | array( | |
382 | 'type' => $nextType, | |
383 | 'name' => $title, | |
384 | 'isDefault' => TRUE, | |
385 | ), | |
386 | array( | |
387 | 'type' => $backType, | |
388 | 'name' => ts('Cancel'), | |
0291a521 | 389 | 'icon' => 'fa-times', |
6a488035 TO |
390 | ), |
391 | ) | |
392 | ); | |
393 | } | |
57884c26 DJ |
394 | |
395 | /** | |
100fef9d | 396 | * Replace ids of household members in $this->_contactIds with the id of their household. |
57884c26 | 397 | * |
5c9ff055 | 398 | * CRM-8338 |
57884c26 DJ |
399 | */ |
400 | public function mergeContactIdsByHousehold() { | |
401 | if (empty($this->_contactIds)) { | |
402 | return; | |
403 | } | |
404 | ||
405 | $contactRelationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType( | |
406 | NULL, | |
407 | NULL, | |
408 | NULL, | |
409 | NULL, | |
410 | TRUE, | |
411 | 'name', | |
412 | FALSE | |
413 | ); | |
414 | ||
415 | // Get Head of Household & Household Member relationships | |
416 | $relationKeyMOH = CRM_Utils_Array::key('Household Member of', $contactRelationshipTypes); | |
417 | $relationKeyHOH = CRM_Utils_Array::key('Head of Household for', $contactRelationshipTypes); | |
418 | $householdRelationshipTypes = array( | |
419 | $relationKeyMOH => $contactRelationshipTypes[$relationKeyMOH], | |
420 | $relationKeyHOH => $contactRelationshipTypes[$relationKeyHOH], | |
421 | ); | |
422 | ||
423 | $relID = implode(',', $this->_contactIds); | |
424 | ||
425 | foreach ($householdRelationshipTypes as $rel => $dnt) { | |
426 | list($id, $direction) = explode('_', $rel, 2); | |
427 | // identify the relationship direction | |
428 | $contactA = 'contact_id_a'; | |
429 | $contactB = 'contact_id_b'; | |
430 | if ($direction == 'b_a') { | |
431 | $contactA = 'contact_id_b'; | |
432 | $contactB = 'contact_id_a'; | |
433 | } | |
434 | ||
435 | // Find related households. | |
353ffa53 | 436 | $relationSelect = "SELECT contact_household.id as household_id, {$contactA} as refContact "; |
57884c26 DJ |
437 | $relationFrom = " FROM civicrm_contact contact_household |
438 | INNER JOIN civicrm_relationship crel ON crel.{$contactB} = contact_household.id AND crel.relationship_type_id = {$id} "; | |
439 | ||
440 | // Check for active relationship status only. | |
353ffa53 TO |
441 | $today = date('Ymd'); |
442 | $relationActive = " AND (crel.is_active = 1 AND ( crel.end_date is NULL OR crel.end_date >= {$today} ) )"; | |
443 | $relationWhere = " WHERE contact_household.is_deleted = 0 AND crel.{$contactA} IN ( {$relID} ) {$relationActive}"; | |
0ee5581e | 444 | $relationGroupBy = " GROUP BY crel.{$contactA}, contact_household.id"; |
57884c26 DJ |
445 | $relationQueryString = "$relationSelect $relationFrom $relationWhere $relationGroupBy"; |
446 | ||
447 | $householdsDAO = CRM_Core_DAO::executeQuery($relationQueryString); | |
448 | while ($householdsDAO->fetch()) { | |
449 | // Remove contact's id from $this->_contactIds and replace with their household's id. | |
450 | foreach (array_keys($this->_contactIds, $householdsDAO->refContact) as $idKey) { | |
451 | unset($this->_contactIds[$idKey]); | |
452 | } | |
453 | if (!in_array($householdsDAO->household_id, $this->_contactIds)) { | |
454 | $this->_contactIds[] = $householdsDAO->household_id; | |
455 | } | |
456 | } | |
457 | $householdsDAO->free(); | |
458 | } | |
31fd348d AS |
459 | |
460 | // If contact list has changed, households will probably be at the end of | |
461 | // the list. Sort it again by sort_name. | |
462 | if (implode(',', $this->_contactIds) != $relID) { | |
31fd348d AS |
463 | $result = civicrm_api3('Contact', 'get', array( |
464 | 'return' => array('id'), | |
465 | 'id' => array('IN' => $this->_contactIds), | |
466 | 'options' => array( | |
467 | 'limit' => 0, | |
351b2dd9 | 468 | 'sort' => "sort_name", |
31fd348d AS |
469 | ), |
470 | )); | |
471 | $this->_contactIds = array_keys($result['values']); | |
472 | } | |
57884c26 | 473 | } |
96025800 | 474 | |
0dbe04b1 TO |
475 | /** |
476 | * @return array | |
477 | * List of contact names. | |
478 | * NOTE: These are raw values from the DB. In current data-model, that means | |
479 | * they are pre-encoded HTML. | |
480 | */ | |
481 | private static function getSelectedContactNames() { | |
482 | $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String'); | |
483 | $cacheKey = "civicrm search {$qfKey}"; | |
484 | ||
485 | $cids = array(); | |
486 | // Gymanstic time! | |
487 | foreach (Civi::service('prevnext')->getSelection($cacheKey) as $cacheKey => $values) { | |
488 | $cids = array_unique(array_merge($cids, array_keys($values))); | |
489 | } | |
490 | ||
491 | $result = CRM_Utils_SQL_Select::from('civicrm_contact') | |
492 | ->where('id IN (#cids)', ['cids' => $cids]) | |
493 | ->execute() | |
494 | ->fetchMap('id', 'sort_name'); | |
495 | return $result; | |
496 | } | |
497 | ||
36c74cd6 TO |
498 | /** |
499 | * Given this task's list of targets, produce a hidden group. | |
500 | * | |
501 | * @return array | |
502 | * Array(0 => int $groupID, 1 => int|NULL $ssID). | |
503 | * @throws Exception | |
504 | */ | |
505 | public function createHiddenGroup() { | |
506 | // Did the user select "All" matches or cherry-pick a few records? | |
507 | $searchParams = $this->controller->exportValues(); | |
508 | if ($searchParams['radio_ts'] == 'ts_sel') { | |
509 | // Create a static group. | |
36c74cd6 TO |
510 | $randID = md5(time() . rand(1, 1000)); // groups require a unique name |
511 | $grpTitle = "Hidden Group {$randID}"; | |
512 | $grpID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $grpTitle, 'id', 'title'); | |
513 | ||
514 | if (!$grpID) { | |
515 | $groupParams = array( | |
516 | 'title' => $grpTitle, | |
517 | 'is_active' => 1, | |
518 | 'is_hidden' => 1, | |
519 | 'group_type' => array('2' => 1), | |
520 | ); | |
521 | ||
522 | $group = CRM_Contact_BAO_Group::create($groupParams); | |
523 | $grpID = $group->id; | |
524 | ||
525 | CRM_Contact_BAO_GroupContact::addContactsToGroup($this->_contactIds, $group->id); | |
526 | ||
527 | $newGroupTitle = "Hidden Group {$grpID}"; | |
528 | $groupParams = array( | |
529 | 'id' => $grpID, | |
530 | 'name' => CRM_Utils_String::titleToVar($newGroupTitle), | |
531 | 'title' => $newGroupTitle, | |
532 | 'group_type' => array('2' => 1), | |
533 | ); | |
5ab8fe4a | 534 | CRM_Contact_BAO_Group::create($groupParams); |
36c74cd6 TO |
535 | } |
536 | ||
537 | // note at this point its a static group | |
538 | return array($grpID, NULL); | |
539 | } | |
540 | else { | |
541 | // Create a smart group. | |
36c74cd6 TO |
542 | $ssId = $this->get('ssID'); |
543 | $hiddenSmartParams = array( | |
544 | 'group_type' => array('2' => 1), | |
d1249113 | 545 | // queryParams have been preprocessed esp WRT any entity reference fields - see + |
546 | // https://github.com/civicrm/civicrm-core/pull/13250 | |
547 | 'form_values' => $this->get('queryParams'), | |
36c74cd6 TO |
548 | 'saved_search_id' => $ssId, |
549 | 'search_custom_id' => $this->get('customSearchID'), | |
550 | 'search_context' => $this->get('context'), | |
551 | ); | |
552 | ||
553 | list($smartGroupId, $savedSearchId) = CRM_Contact_BAO_Group::createHiddenSmartGroup($hiddenSmartParams); | |
554 | return array($smartGroupId, $savedSearchId); | |
555 | } | |
556 | ||
557 | } | |
558 | ||
6a488035 | 559 | } |