Merge pull request #13915 from colemanw/shortCRM
[civicrm-core.git] / CRM / Contact / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
32 */
33
34 /**
35 * Base Search / View form for *all* listing of multiple
36 * contacts
37 */
38 class CRM_Contact_Form_Search extends CRM_Core_Form_Search {
39
40 /**
41 * list of valid contexts.
42 *
43 * @var array
44 */
45 static $_validContext = NULL;
46
47 /**
48 * List of values used when we want to display other objects.
49 *
50 * @var array
51 */
52 static $_modeValues = NULL;
53
54 /**
55 * The contextMenu.
56 *
57 * @var array
58 */
59 protected $_contextMenu;
60
61 /**
62 * The groupId retrieved from the GET vars.
63 *
64 * @var int
65 */
66 public $_groupID;
67
68 /**
69 * The Group ID belonging to Add Member to group ID.
70 * retrieved from the GET vars
71 *
72 * @var int
73 */
74 protected $_amtgID;
75
76 /**
77 * The saved search ID retrieved from the GET vars.
78 *
79 * @var int
80 */
81 protected $_ssID;
82
83 /**
84 * The group elements.
85 *
86 * @var array
87 */
88 public $_group;
89 public $_groupElement;
90
91 /**
92 * The tag elements.
93 *
94 * @var array
95 */
96 public $_tag;
97 public $_tagElement;
98
99 /**
100 * The params used for search.
101 *
102 * @var array
103 */
104 protected $_params;
105
106 /**
107 * The return properties used for search.
108 *
109 * @var array
110 */
111 protected $_returnProperties;
112
113 /**
114 * The sort by character.
115 *
116 * @var string
117 */
118 protected $_sortByCharacter;
119
120 /**
121 * The profile group id used for display.
122 *
123 * @var integer
124 */
125 protected $_ufGroupID;
126
127 /**
128 * Csv - common search values
129 *
130 * @var array
131 */
132 static $csv = ['contact_type', 'group', 'tag'];
133
134 /**
135 * @var string how to display the results. Should we display as
136 * contributons, members, cases etc
137 */
138 protected $_componentMode;
139
140 /**
141 * @var string what operator should we use, AND or OR
142 */
143 protected $_operator;
144
145 protected $_modeValue;
146
147 /**
148 * Declare entity reference fields as they will need to be converted to using 'IN'.
149 *
150 * @var array
151 */
152 protected $entityReferenceFields = ['event_id', 'membership_type_id'];
153
154 /**
155 * Name of the selector to use.
156 */
157 static $_selectorName = 'CRM_Contact_Selector';
158 protected $_customSearchID = NULL;
159 protected $_customSearchClass = NULL;
160
161 protected $_openedPanes = [];
162
163 /**
164 * Explicitly declare the entity api name.
165 */
166 public function getDefaultEntity() {
167 return 'Contact';
168 }
169
170 /**
171 * Define the set of valid contexts that the search form operates on.
172 *
173 * @return array
174 * the valid context set and the titles
175 */
176 public static function &validContext() {
177 if (!(self::$_validContext)) {
178 self::$_validContext = [
179 'smog' => 'Show members of group',
180 'amtg' => 'Add members to group',
181 'basic' => 'Basic Search',
182 'search' => 'Search',
183 'builder' => 'Search Builder',
184 'advanced' => 'Advanced Search',
185 'custom' => 'Custom Search',
186 ];
187 }
188 return self::$_validContext;
189 }
190
191 /**
192 * @param $context
193 *
194 * @return bool
195 */
196 public static function isSearchContext($context) {
197 $searchContext = CRM_Utils_Array::value($context, self::validContext());
198 return $searchContext ? TRUE : FALSE;
199 }
200
201 public static function setModeValues() {
202 self::$_modeValues = [
203 CRM_Contact_BAO_Query::MODE_CONTACTS => [
204 'selectorName' => self::$_selectorName,
205 'selectorLabel' => ts('Contacts'),
206 'taskFile' => 'CRM/Contact/Form/Search/ResultTasks.tpl',
207 'taskContext' => NULL,
208 'resultFile' => 'CRM/Contact/Form/Selector.tpl',
209 'resultContext' => NULL,
210 'taskClassName' => 'CRM_Contact_Task',
211 'component' => '',
212 ],
213 CRM_Contact_BAO_Query::MODE_CONTRIBUTE => [
214 'selectorName' => 'CRM_Contribute_Selector_Search',
215 'selectorLabel' => ts('Contributions'),
216 'taskFile' => 'CRM/common/searchResultTasks.tpl',
217 'taskContext' => 'Contribution',
218 'resultFile' => 'CRM/Contribute/Form/Selector.tpl',
219 'resultContext' => 'Search',
220 'taskClassName' => 'CRM_Contribute_Task',
221 'component' => 'CiviContribute',
222 ],
223 CRM_Contact_BAO_Query::MODE_EVENT => [
224 'selectorName' => 'CRM_Event_Selector_Search',
225 'selectorLabel' => ts('Event Participants'),
226 'taskFile' => 'CRM/common/searchResultTasks.tpl',
227 'taskContext' => NULL,
228 'resultFile' => 'CRM/Event/Form/Selector.tpl',
229 'resultContext' => 'Search',
230 'taskClassName' => 'CRM_Event_Task',
231 'component' => 'CiviEvent',
232 ],
233 CRM_Contact_BAO_Query::MODE_ACTIVITY => [
234 'selectorName' => 'CRM_Activity_Selector_Search',
235 'selectorLabel' => ts('Activities'),
236 'taskFile' => 'CRM/common/searchResultTasks.tpl',
237 'taskContext' => NULL,
238 'resultFile' => 'CRM/Activity/Form/Selector.tpl',
239 'resultContext' => 'Search',
240 'taskClassName' => 'CRM_Activity_Task',
241 'component' => 'activity',
242 ],
243 CRM_Contact_BAO_Query::MODE_MEMBER => [
244 'selectorName' => 'CRM_Member_Selector_Search',
245 'selectorLabel' => ts('Memberships'),
246 'taskFile' => "CRM/common/searchResultTasks.tpl",
247 'taskContext' => NULL,
248 'resultFile' => 'CRM/Member/Form/Selector.tpl',
249 'resultContext' => 'Search',
250 'taskClassName' => 'CRM_Member_Task',
251 'component' => 'CiviMember',
252 ],
253 CRM_Contact_BAO_Query::MODE_CASE => [
254 'selectorName' => 'CRM_Case_Selector_Search',
255 'selectorLabel' => ts('Cases'),
256 'taskFile' => "CRM/common/searchResultTasks.tpl",
257 'taskContext' => NULL,
258 'resultFile' => 'CRM/Case/Form/Selector.tpl',
259 'resultContext' => 'Search',
260 'taskClassName' => 'CRM_Case_Task',
261 'component' => 'CiviCase',
262 ],
263 CRM_Contact_BAO_Query::MODE_CONTACTSRELATED => [
264 'selectorName' => self::$_selectorName,
265 'selectorLabel' => ts('Related Contacts'),
266 'taskFile' => 'CRM/Contact/Form/Search/ResultTasks.tpl',
267 'taskContext' => NULL,
268 'resultFile' => 'CRM/Contact/Form/Selector.tpl',
269 'resultContext' => NULL,
270 'taskClassName' => 'CRM_Contact_Task',
271 'component' => 'related_contact',
272 ],
273 CRM_Contact_BAO_Query::MODE_MAILING => [
274 'selectorName' => 'CRM_Mailing_Selector_Search',
275 'selectorLabel' => ts('Mailings'),
276 'taskFile' => "CRM/common/searchResultTasks.tpl",
277 'taskContext' => NULL,
278 'resultFile' => 'CRM/Mailing/Form/Selector.tpl',
279 'resultContext' => 'Search',
280 'taskClassName' => 'CRM_Mailing_Task',
281 'component' => 'CiviMail',
282 ],
283 ];
284 }
285
286 /**
287 * Get the metadata for the query mode (this includes task class names)
288 *
289 * @param int $mode
290 *
291 * @return array
292 * @throws \CRM_Core_Exception
293 */
294 public static function getModeValue($mode = CRM_Contact_BAO_Query::MODE_CONTACTS) {
295 $searchPane = CRM_Utils_Request::retrieve('searchPane', 'String');
296 if (!empty($searchPane)) {
297 $mode = array_search($searchPane, self::getModeToComponentMapping());
298 }
299
300 self::setModeValues();
301 if (!array_key_exists($mode, self::$_modeValues)) {
302 $mode = CRM_Contact_BAO_Query::MODE_CONTACTS;
303 }
304
305 return self::$_modeValues[$mode];
306 }
307
308 /**
309 * Get a mapping of modes to components.
310 *
311 * This will map the integers to the components. Contact has an empty component
312 * an pseudo-components exist for activity & related_contact.
313 *
314 * @return array
315 */
316 public static function getModeToComponentMapping() {
317 $mapping = [];
318 self::setModeValues();
319
320 foreach (self::$_modeValues as $id => $metadata) {
321 $mapping[$id] = $metadata['component'];
322 }
323 return $mapping;
324 }
325
326 /**
327 * @return array
328 */
329 public static function getModeSelect() {
330 self::setModeValues();
331
332 $enabledComponents = CRM_Core_Component::getEnabledComponents();
333 $componentModes = [];
334 foreach (self::$_modeValues as $id => & $value) {
335 if (strpos($value['component'], 'Civi') !== FALSE
336 && !array_key_exists($value['component'], $enabledComponents)
337 ) {
338 continue;
339 }
340 $componentModes[$id] = $value['selectorLabel'];
341 }
342
343 // unset disabled components
344 if (!array_key_exists('CiviMail', $enabledComponents)) {
345 unset($componentModes[CRM_Contact_BAO_Query::MODE_MAILING]);
346 }
347
348 // unset contributions or participants if user does not have permission on them
349 if (!CRM_Core_Permission::access('CiviContribute')) {
350 unset($componentModes[CRM_Contact_BAO_Query::MODE_CONTRIBUTE]);
351 }
352
353 if (!CRM_Core_Permission::access('CiviEvent')) {
354 unset($componentModes[CRM_Contact_BAO_Query::MODE_EVENT]);
355 }
356
357 if (!CRM_Core_Permission::access('CiviMember')) {
358 unset($componentModes[CRM_Contact_BAO_Query::MODE_MEMBER]);
359 }
360
361 if (!CRM_Core_Permission::check('view all activities')) {
362 unset($componentModes[CRM_Contact_BAO_Query::MODE_ACTIVITY]);
363 }
364
365 return $componentModes;
366 }
367
368 /**
369 * Builds the list of tasks or actions that a searcher can perform on a result set.
370 *
371 * @return array
372 */
373 public function buildTaskList() {
374 // amtg = 'Add members to group'
375 if ($this->_context !== 'amtg') {
376 $taskParams['deletedContacts'] = FALSE;
377 if ($this->_componentMode == CRM_Contact_BAO_Query::MODE_CONTACTS || $this->_componentMode == CRM_Contact_BAO_Query::MODE_CONTACTSRELATED) {
378 $taskParams['deletedContacts'] = CRM_Utils_Array::value('deleted_contacts', $this->_formValues);
379 }
380 $className = $this->_modeValue['taskClassName'];
381 $taskParams['ssID'] = isset($this->_ssID) ? $this->_ssID : NULL;
382 $this->_taskList += $className::permissionedTaskTitles(CRM_Core_Permission::getPermission(), $taskParams);
383 }
384
385 return $this->_taskList;
386 }
387
388 /**
389 * Build the common elements between the search/advanced form.
390 */
391 public function buildQuickForm() {
392 parent::buildQuickForm();
393
394 // some tasks.. what do we want to do with the selected contacts ?
395 $this->_taskList = $this->buildTaskList();
396
397 if (isset($this->_ssID)) {
398 $search_custom_id
399 = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'search_custom_id');
400
401 $savedSearchValues = [
402 'id' => $this->_ssID,
403 'name' => CRM_Contact_BAO_SavedSearch::getName($this->_ssID, 'title'),
404 'search_custom_id' => $search_custom_id,
405 ];
406 $this->assign_by_ref('savedSearch', $savedSearchValues);
407 $this->assign('ssID', $this->_ssID);
408 }
409
410 if ($this->_context === 'smog') {
411 // CRM-11788, we might want to do this for all of search where force=1
412 $formQFKey = CRM_Utils_Array::value('qfKey', $this->_formValues);
413 $getQFKey = CRM_Utils_Array::value('qfKey', $_GET);
414 $postQFKey = CRM_Utils_Array::value('qfKey', $_POST);
415 if ($formQFKey && empty($getQFKey) && empty($postQFKey)) {
416 $url = CRM_Utils_System::makeURL('qfKey') . $formQFKey;
417 CRM_Utils_System::redirect($url);
418 }
419 $permissionForGroup = FALSE;
420
421 if (!empty($this->_groupID)) {
422 // check if user has permission to edit members of this group
423 $permission = CRM_Contact_BAO_Group::checkPermission($this->_groupID);
424 if ($permission && in_array(CRM_Core_Permission::EDIT, $permission)) {
425 $permissionForGroup = TRUE;
426 }
427
428 // check if _groupID exists, it might not if
429 // we are displaying a hidden group
430 if (!isset($this->_group[$this->_groupID])) {
431 $this->_group[$this->_groupID]
432 = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_groupID, 'title');
433 }
434
435 // set the group title
436 $groupValues = ['id' => $this->_groupID, 'title' => $this->_group[$this->_groupID]];
437 $this->assign_by_ref('group', $groupValues);
438
439 // also set ssID if this is a saved search
440 $ssID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_groupID, 'saved_search_id');
441 $this->assign('ssID', $ssID);
442
443 //get the saved search mapping id
444 if ($ssID) {
445 $this->_ssID = $ssID;
446 $ssMappingId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $ssID, 'mapping_id');
447 $this->assign('ssMappingID', $ssMappingId);
448 }
449
450 // Set dynamic page title for 'Show Members of Group'
451 CRM_Utils_System::setTitle(ts('Contacts in Group: %1', [1 => $this->_group[$this->_groupID]]));
452 }
453
454 $group_contact_status = [];
455 foreach (CRM_Core_SelectValues::groupContactStatus() as $k => $v) {
456 if (!empty($k)) {
457 $group_contact_status[] = $this->createElement('checkbox', $k, NULL, $v);
458 }
459 }
460 $this->addGroup($group_contact_status,
461 'group_contact_status', ts('Group Status')
462 );
463
464 $this->assign('permissionedForGroup', $permissionForGroup);
465 }
466
467 // add the go button for the action form, note it is of type 'next' rather than of type 'submit'
468 if ($this->_context === 'amtg') {
469 // check if _groupID exists, it might not if
470 // we are displaying a hidden group
471 if (!isset($this->_group[$this->_amtgID])) {
472 $this->assign('permissionedForGroup', FALSE);
473 $this->_group[$this->_amtgID]
474 = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_amtgID, 'title');
475 }
476
477 // Set dynamic page title for 'Add Members Group'
478 CRM_Utils_System::setTitle(ts('Add to Group: %1', [1 => $this->_group[$this->_amtgID]]));
479 // also set the group title and freeze the action task with Add Members to Group
480 $groupValues = ['id' => $this->_amtgID, 'title' => $this->_group[$this->_amtgID]];
481 $this->assign_by_ref('group', $groupValues);
482 $this->add('submit', $this->_actionButtonName, ts('Add Contacts to %1', [1 => $this->_group[$this->_amtgID]]),
483 [
484 'class' => 'crm-form-submit',
485 ]
486 );
487 $this->add('hidden', 'task', CRM_Contact_Task::GROUP_ADD);
488 $selectedRowsRadio = $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', ['checked' => 'checked']);
489 $allRowsRadio = $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all');
490 $this->assign('ts_sel_id', $selectedRowsRadio->_attributes['id']);
491 $this->assign('ts_all_id', $allRowsRadio->_attributes['id']);
492 }
493
494 $selectedContactIds = [];
495 $qfKeyParam = CRM_Utils_Array::value('qfKey', $this->_formValues);
496 // We use ajax to handle selections only if the search results component_mode is set to "contacts"
497 if ($qfKeyParam && ($this->get('component_mode') <= CRM_Contact_BAO_Query::MODE_CONTACTS || $this->get('component_mode') == CRM_Contact_BAO_Query::MODE_CONTACTSRELATED)) {
498 $this->addClass('crm-ajax-selection-form');
499 $qfKeyParam = "civicrm search {$qfKeyParam}";
500 $selectedContactIdsArr = Civi::service('prevnext')->getSelection($qfKeyParam);
501 $selectedContactIds = array_keys($selectedContactIdsArr[$qfKeyParam]);
502 }
503
504 $this->assign_by_ref('selectedContactIds', $selectedContactIds);
505
506 $rows = $this->get('rows');
507
508 if (is_array($rows)) {
509 $this->addRowSelectors($rows);
510 }
511
512 }
513
514 /**
515 * Processing needed for buildForm and later.
516 */
517 public function preProcess() {
518 // set the various class variables
519
520 $this->_group = CRM_Core_PseudoConstant::group();
521
522 $this->_tag = CRM_Core_BAO_Tag::getTags();
523 $this->_done = FALSE;
524
525 /*
526 * we allow the controller to set force/reset externally, useful when we are being
527 * driven by the wizard framework
528 */
529
530 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean');
531
532 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean');
533 $this->_groupID = CRM_Utils_Request::retrieve('gid', 'Positive', $this);
534 $this->_amtgID = CRM_Utils_Request::retrieve('amtgID', 'Positive', $this);
535 $this->_ssID = CRM_Utils_Request::retrieve('ssID', 'Positive', $this);
536 $this->_sortByCharacter = CRM_Utils_Request::retrieve('sortByCharacter', 'String', $this);
537 $this->_ufGroupID = CRM_Utils_Request::retrieve('id', 'Positive', $this);
538 $this->_componentMode = CRM_Utils_Request::retrieve('component_mode', 'Positive', $this, FALSE, CRM_Contact_BAO_Query::MODE_CONTACTS, $_REQUEST);
539 $this->_operator = CRM_Utils_Request::retrieve('operator', 'String', $this, FALSE, CRM_Contact_BAO_Query::SEARCH_OPERATOR_AND, 'REQUEST');
540
541 /**
542 * set the button names
543 */
544 $this->_searchButtonName = $this->getButtonName('refresh');
545 $this->_actionButtonName = $this->getButtonName('next', 'action');
546
547 $this->assign('actionButtonName', $this->_actionButtonName);
548
549 // if we dont get this from the url, use default if one exsts
550 $config = CRM_Core_Config::singleton();
551 if ($this->_ufGroupID == NULL &&
552 $config->defaultSearchProfileID != NULL
553 ) {
554 $this->_ufGroupID = $config->defaultSearchProfileID;
555 }
556
557 // assign context to drive the template display, make sure context is valid
558 $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, 'search');
559 if (!CRM_Utils_Array::value($this->_context, self::validContext())) {
560 $this->_context = 'search';
561 }
562 $this->set('context', $this->_context);
563 $this->assign('context', $this->_context);
564
565 $this->_modeValue = self::getModeValue($this->_componentMode);
566 $this->assign($this->_modeValue);
567
568 $this->set('selectorName', self::$_selectorName);
569
570 // get user submitted values
571 // get it from controller only if form has been submitted, else preProcess has set this
572 // $this->controller->isModal( ) returns TRUE if page is
573 // valid, i.e all the validations are TRUE
574
575 if (!empty($_POST) && !$this->controller->isModal()) {
576 $this->_formValues = $this->controller->exportValues($this->_name);
577
578 $this->normalizeFormValues();
579 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
580 $this->_returnProperties = &$this->returnProperties();
581
582 // also get the uf group id directly from the post value
583 $this->_ufGroupID = CRM_Utils_Array::value('uf_group_id', $_POST, $this->_ufGroupID);
584 $this->_formValues['uf_group_id'] = $this->_ufGroupID;
585 $this->set('id', $this->_ufGroupID);
586
587 // also get the object mode directly from the post value
588 $this->_componentMode = CRM_Utils_Array::value('component_mode', $_POST, $this->_componentMode);
589
590 // also get the operator from the post value if set
591 $this->_operator = CRM_Utils_Array::value('operator', $_POST, $this->_operator);
592 $this->_formValues['operator'] = $this->_operator;
593 $this->set('operator', $this->_operator);
594 }
595 else {
596 $this->_formValues = $this->get('formValues');
597 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
598 $this->_returnProperties = &$this->returnProperties();
599 if (!empty($this->_ufGroupID)) {
600 $this->set('id', $this->_ufGroupID);
601 }
602 }
603
604 if (empty($this->_formValues)) {
605 //check if group is a smart group (fix for CRM-1255)
606 if ($this->_groupID) {
607 if ($ssId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_groupID, 'saved_search_id')) {
608 $this->_ssID = $ssId;
609 }
610 }
611
612 // fix for CRM-1907
613 if (isset($this->_ssID) && $this->_context != 'smog') {
614 // we only retrieve the saved search values if out current values are null
615 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
616
617 //fix for CRM-1505
618 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'mapping_id')) {
619 $this->_params = CRM_Contact_BAO_SavedSearch::getSearchParams($this->_ssID);
620 }
621 else {
622 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
623 }
624 $this->_returnProperties = &$this->returnProperties();
625 }
626 else {
627 if (isset($this->_ufGroupID)) {
628 // also set the uf group id if not already present
629 $this->_formValues['uf_group_id'] = $this->_ufGroupID;
630 }
631 if (isset($this->_componentMode)) {
632 $this->_formValues['component_mode'] = $this->_componentMode;
633 }
634 if (isset($this->_operator)) {
635 $this->_formValues['operator'] = $this->_operator;
636 }
637
638 // FIXME: we should generalise in a way that components could inject url-filters
639 // just like they build their own form elements
640 foreach ([
641 'mailing_id',
642 'mailing_delivery_status',
643 'mailing_open_status',
644 'mailing_click_status',
645 'mailing_reply_status',
646 'mailing_optout',
647 'mailing_forward',
648 'mailing_unsubscribe',
649 'mailing_date_low',
650 'mailing_date_high',
651 ] as $mailingFilter) {
652 $type = 'String';
653 if ($mailingFilter == 'mailing_id' &&
654 $filterVal = CRM_Utils_Request::retrieve('mailing_id', 'Positive', $this)
655 ) {
656 $this->_formValues[$mailingFilter] = [$filterVal];
657 }
658 elseif ($filterVal = CRM_Utils_Request::retrieve($mailingFilter, $type, $this)) {
659 $this->_formValues[$mailingFilter] = $filterVal;
660 }
661 if ($filterVal) {
662 $this->_openedPanes['Mailings'] = 1;
663 $this->_formValues['hidden_CiviMail'] = 1;
664 }
665 }
666 }
667 }
668 $this->assign('id',
669 CRM_Utils_Array::value('uf_group_id', $this->_formValues)
670 );
671 $operator = CRM_Utils_Array::value('operator', $this->_formValues, CRM_Contact_BAO_Query::SEARCH_OPERATOR_AND);
672 $this->set('queryOperator', $operator);
673 if ($operator == CRM_Contact_BAO_Query::SEARCH_OPERATOR_OR) {
674 $this->assign('operator', ts('OR'));
675 }
676 else {
677 $this->assign('operator', ts('AND'));
678 }
679
680 // show the context menu only when we’re not searching for deleted contacts; CRM-5673
681 if (empty($this->_formValues['deleted_contacts'])) {
682 $menuItems = CRM_Contact_BAO_Contact::contextMenu();
683 $primaryActions = CRM_Utils_Array::value('primaryActions', $menuItems, []);
684 $this->_contextMenu = CRM_Utils_Array::value('moreActions', $menuItems, []);
685 $this->assign('contextMenu', $primaryActions + $this->_contextMenu);
686 }
687
688 if (!isset($this->_componentMode)) {
689 $this->_componentMode = CRM_Contact_BAO_Query::MODE_CONTACTS;
690 }
691 self::$_selectorName = $this->_modeValue['selectorName'];
692 self::setModeValues();
693
694 $setDynamic = FALSE;
695 if (strpos(self::$_selectorName, 'CRM_Contact_Selector') !== FALSE) {
696 $selector = new self::$_selectorName(
697 $this->_customSearchClass,
698 $this->_formValues,
699 $this->_params,
700 $this->_returnProperties,
701 $this->_action,
702 FALSE, TRUE,
703 $this->_context,
704 $this->_contextMenu
705 );
706 $setDynamic = TRUE;
707 }
708 else {
709 $selector = new self::$_selectorName(
710 $this->_params,
711 $this->_action,
712 NULL, FALSE, NULL,
713 "search", "advanced"
714 );
715 }
716
717 $selector->setKey($this->controller->_key);
718
719 $controller = new CRM_Contact_Selector_Controller($selector,
720 $this->get(CRM_Utils_Pager::PAGE_ID),
721 $this->get(CRM_Utils_Sort::SORT_ID),
722 CRM_Core_Action::VIEW,
723 $this,
724 CRM_Core_Selector_Controller::TRANSFER
725 );
726 $controller->setEmbedded(TRUE);
727 $controller->setDynamicAction($setDynamic);
728
729 if ($this->_force) {
730
731 $this->postProcess();
732
733 /*
734 * Note that we repeat this, since the search creates and stores
735 * values that potentially change the controller behavior. i.e. things
736 * like totalCount etc
737 */
738 $sortID = NULL;
739 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
740 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
741 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
742 );
743 }
744 $controller = new CRM_Contact_Selector_Controller($selector,
745 $this->get(CRM_Utils_Pager::PAGE_ID),
746 $sortID,
747 CRM_Core_Action::VIEW, $this, CRM_Core_Selector_Controller::TRANSFER
748 );
749 $controller->setEmbedded(TRUE);
750 $controller->setDynamicAction($setDynamic);
751 }
752
753 $controller->moveFromSessionToTemplate();
754 }
755
756 /**
757 * @return array
758 */
759 public function &getFormValues() {
760 return $this->_formValues;
761 }
762
763 /**
764 * Common post processing.
765 */
766 public function postProcess() {
767 /*
768 * sometime we do a postProcess early on, so we dont need to repeat it
769 * this will most likely introduce some more bugs :(
770 */
771
772 if ($this->_done) {
773 return;
774 }
775 $this->_done = TRUE;
776
777 //for prev/next pagination
778 $crmPID = CRM_Utils_Request::retrieve('crmPID', 'Integer');
779
780 if (array_key_exists($this->_searchButtonName, $_POST) ||
781 ($this->_force && !$crmPID)
782 ) {
783 //reset the cache table for new search
784 $cacheKey = "civicrm search {$this->controller->_key}";
785 Civi::service('prevnext')->deleteItem(NULL, $cacheKey);
786 }
787
788 //get the button name
789 $buttonName = $this->controller->getButtonName();
790
791 if (isset($this->_ufGroupID) && empty($this->_formValues['uf_group_id'])) {
792 $this->_formValues['uf_group_id'] = $this->_ufGroupID;
793 }
794
795 if (isset($this->_componentMode) && empty($this->_formValues['component_mode'])) {
796 $this->_formValues['component_mode'] = $this->_componentMode;
797 }
798
799 if (isset($this->_operator) && empty($this->_formValues['operator'])) {
800 $this->_formValues['operator'] = $this->_operator;
801 }
802
803 if (empty($this->_formValues['qfKey'])) {
804 $this->_formValues['qfKey'] = $this->controller->_key;
805 }
806
807 if (!CRM_Core_Permission::check('access deleted contacts')) {
808 unset($this->_formValues['deleted_contacts']);
809 }
810
811 $this->set('type', $this->_action);
812 $this->set('formValues', $this->_formValues);
813 $this->set('queryParams', $this->_params);
814 $this->set('returnProperties', $this->_returnProperties);
815
816 if ($buttonName == $this->_actionButtonName) {
817 // check actionName and if next, then do not repeat a search, since we are going to the next page
818 // hack, make sure we reset the task values
819 $stateMachine = $this->controller->getStateMachine();
820 $formName = $stateMachine->getTaskFormName();
821 $this->controller->resetPage($formName);
822 return;
823 }
824 else {
825 $output = CRM_Core_Selector_Controller::SESSION;
826
827 // create the selector, controller and run - store results in session
828 $searchChildGroups = TRUE;
829 if ($this->get('isAdvanced')) {
830 $searchChildGroups = FALSE;
831 }
832
833 $setDynamic = FALSE;
834
835 if (strpos(self::$_selectorName, 'CRM_Contact_Selector') !== FALSE) {
836 $selector = new self::$_selectorName(
837 $this->_customSearchClass,
838 $this->_formValues,
839 $this->_params,
840 $this->_returnProperties,
841 $this->_action,
842 FALSE,
843 $searchChildGroups,
844 $this->_context,
845 $this->_contextMenu
846 );
847 $setDynamic = TRUE;
848 }
849 else {
850 $selector = new self::$_selectorName(
851 $this->_params,
852 $this->_action,
853 NULL,
854 FALSE,
855 NULL,
856 "search",
857 "advanced"
858 );
859 }
860
861 $selector->setKey($this->controller->_key);
862
863 // added the sorting character to the form array
864 $config = CRM_Core_Config::singleton();
865 // do this only for contact search
866 if ($setDynamic && $config->includeAlphabeticalPager) {
867 // Don't recompute if we are just paging/sorting
868 if ($this->_reset || (empty($_GET['crmPID']) && empty($_GET['crmSID']) && !$this->_sortByCharacter)) {
869 $aToZBar = CRM_Utils_PagerAToZ::getAToZBar($selector, $this->_sortByCharacter);
870 $this->set('AToZBar', $aToZBar);
871 }
872 }
873
874 $sortID = NULL;
875 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
876 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
877 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
878 );
879 }
880 $controller = new CRM_Contact_Selector_Controller($selector,
881 $this->get(CRM_Utils_Pager::PAGE_ID),
882 $sortID,
883 CRM_Core_Action::VIEW,
884 $this,
885 $output
886 );
887 $controller->setEmbedded(TRUE);
888 $controller->setDynamicAction($setDynamic);
889 $controller->run();
890 }
891 }
892
893 /**
894 * @return NULL
895 */
896 public function &returnProperties() {
897 return CRM_Core_DAO::$_nullObject;
898 }
899
900 /**
901 * Return a descriptive name for the page, used in wizard header
902 *
903 * @return string
904 */
905 public function getTitle() {
906 return ts('Search');
907 }
908
909 }