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