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