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