7a1003151b17493d85bf5172db89d7ff78a5de9d
[civicrm-core.git] / CRM / Contact / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
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 = array('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 = array('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 = array();
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 = array(
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 = array(
203 CRM_Contact_BAO_Query::MODE_CONTACTS => array(
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 => array(
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 => array(
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 => array(
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 => array(
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 => array(
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 => array(
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 => array(
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 $componentModes = array();
333 foreach (self::$_modeValues as $id => & $value) {
334 $componentModes[$id] = $value['selectorLabel'];
335 }
336
337 $enabledComponents = CRM_Core_Component::getEnabledComponents();
338
339 // unset disabled components
340 if (!array_key_exists('CiviMail', $enabledComponents)) {
341 unset($componentModes[CRM_Contact_BAO_Query::MODE_MAILING]);
342 }
343
344 // unset contributions or participants if user does not have permission on them
345 if (!CRM_Core_Permission::access('CiviContribute')) {
346 unset($componentModes[CRM_Contact_BAO_Query::MODE_CONTRIBUTE]);
347 }
348
349 if (!CRM_Core_Permission::access('CiviEvent')) {
350 unset($componentModes[CRM_Contact_BAO_Query::MODE_EVENT]);
351 }
352
353 if (!CRM_Core_Permission::access('CiviMember')) {
354 unset($componentModes[CRM_Contact_BAO_Query::MODE_MEMBER]);
355 }
356
357 if (!CRM_Core_Permission::check('view all activities')) {
358 unset($componentModes[CRM_Contact_BAO_Query::MODE_ACTIVITY]);
359 }
360
361 return $componentModes;
362 }
363
364 /**
365 * Builds the list of tasks or actions that a searcher can perform on a result set.
366 *
367 * @return array
368 */
369 public function buildTaskList() {
370 // amtg = 'Add members to group'
371 if ($this->_context !== 'amtg') {
372 $taskParams['deletedContacts'] = FALSE;
373 if ($this->_componentMode == CRM_Contact_BAO_Query::MODE_CONTACTS || $this->_componentMode == CRM_Contact_BAO_Query::MODE_CONTACTSRELATED) {
374 $taskParams['deletedContacts'] = CRM_Utils_Array::value('deleted_contacts', $this->_formValues);
375 }
376 $className = $this->_modeValue['taskClassName'];
377 $taskParams['ssID'] = isset($this->_ssID) ? $this->_ssID : NULL;
378 $this->_taskList += $className::permissionedTaskTitles(CRM_Core_Permission::getPermission(), $taskParams);
379 }
380
381 return $this->_taskList;
382 }
383
384 /**
385 * Build the common elements between the search/advanced form.
386 */
387 public function buildQuickForm() {
388 parent::buildQuickForm();
389
390 // some tasks.. what do we want to do with the selected contacts ?
391 $this->_taskList = $this->buildTaskList();
392
393 if (isset($this->_ssID)) {
394 $search_custom_id
395 = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'search_custom_id');
396
397 $savedSearchValues = array(
398 'id' => $this->_ssID,
399 'name' => CRM_Contact_BAO_SavedSearch::getName($this->_ssID, 'title'),
400 'search_custom_id' => $search_custom_id,
401 );
402 $this->assign_by_ref('savedSearch', $savedSearchValues);
403 $this->assign('ssID', $this->_ssID);
404 }
405
406 if ($this->_context === 'smog') {
407 // CRM-11788, we might want to do this for all of search where force=1
408 $formQFKey = CRM_Utils_Array::value('qfKey', $this->_formValues);
409 $getQFKey = CRM_Utils_Array::value('qfKey', $_GET);
410 $postQFKey = CRM_Utils_Array::value('qfKey', $_POST);
411 if ($formQFKey && empty($getQFKey) && empty($postQFKey)) {
412 $url = CRM_Utils_System::makeURL('qfKey') . $formQFKey;
413 CRM_Utils_System::redirect($url);
414 }
415 $permissionForGroup = FALSE;
416
417 if (!empty($this->_groupID)) {
418 // check if user has permission to edit members of this group
419 $permission = CRM_Contact_BAO_Group::checkPermission($this->_groupID);
420 if ($permission && in_array(CRM_Core_Permission::EDIT, $permission)) {
421 $permissionForGroup = TRUE;
422 }
423
424 // check if _groupID exists, it might not if
425 // we are displaying a hidden group
426 if (!isset($this->_group[$this->_groupID])) {
427 $this->_group[$this->_groupID]
428 = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_groupID, 'title');
429 }
430
431 // set the group title
432 $groupValues = array('id' => $this->_groupID, 'title' => $this->_group[$this->_groupID]);
433 $this->assign_by_ref('group', $groupValues);
434
435 // also set ssID if this is a saved search
436 $ssID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_groupID, 'saved_search_id');
437 $this->assign('ssID', $ssID);
438
439 //get the saved search mapping id
440 if ($ssID) {
441 $this->_ssID = $ssID;
442 $ssMappingId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $ssID, 'mapping_id');
443 $this->assign('ssMappingID', $ssMappingId);
444 }
445
446 // Set dynamic page title for 'Show Members of Group'
447 CRM_Utils_System::setTitle(ts('Contacts in Group: %1', array(1 => $this->_group[$this->_groupID])));
448 }
449
450 $group_contact_status = array();
451 foreach (CRM_Core_SelectValues::groupContactStatus() as $k => $v) {
452 if (!empty($k)) {
453 $group_contact_status[] = $this->createElement('checkbox', $k, NULL, $v);
454 }
455 }
456 $this->addGroup($group_contact_status,
457 'group_contact_status', ts('Group Status')
458 );
459
460 $this->assign('permissionedForGroup', $permissionForGroup);
461 }
462
463 // add the go button for the action form, note it is of type 'next' rather than of type 'submit'
464 if ($this->_context === 'amtg') {
465 // check if _groupID exists, it might not if
466 // we are displaying a hidden group
467 if (!isset($this->_group[$this->_amtgID])) {
468 $this->assign('permissionedForGroup', FALSE);
469 $this->_group[$this->_amtgID]
470 = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_amtgID, 'title');
471 }
472
473 // Set dynamic page title for 'Add Members Group'
474 CRM_Utils_System::setTitle(ts('Add to Group: %1', array(1 => $this->_group[$this->_amtgID])));
475 // also set the group title and freeze the action task with Add Members to Group
476 $groupValues = array('id' => $this->_amtgID, 'title' => $this->_group[$this->_amtgID]);
477 $this->assign_by_ref('group', $groupValues);
478 $this->add('submit', $this->_actionButtonName, ts('Add Contacts to %1', array(1 => $this->_group[$this->_amtgID])),
479 array(
480 'class' => 'crm-form-submit',
481 )
482 );
483 $this->add('hidden', 'task', CRM_Contact_Task::GROUP_ADD);
484 $selectedRowsRadio = $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked'));
485 $allRowsRadio = $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all');
486 $this->assign('ts_sel_id', $selectedRowsRadio->_attributes['id']);
487 $this->assign('ts_all_id', $allRowsRadio->_attributes['id']);
488 }
489
490 $selectedContactIds = array();
491 $qfKeyParam = CRM_Utils_Array::value('qfKey', $this->_formValues);
492 // We use ajax to handle selections only if the search results component_mode is set to "contacts"
493 if ($qfKeyParam && ($this->get('component_mode') <= CRM_Contact_BAO_Query::MODE_CONTACTS || $this->get('component_mode') == CRM_Contact_BAO_Query::MODE_CONTACTSRELATED)) {
494 $this->addClass('crm-ajax-selection-form');
495 $qfKeyParam = "civicrm search {$qfKeyParam}";
496 $selectedContactIdsArr = Civi::service('prevnext')->getSelection($qfKeyParam);
497 $selectedContactIds = array_keys($selectedContactIdsArr[$qfKeyParam]);
498 }
499
500 $this->assign_by_ref('selectedContactIds', $selectedContactIds);
501
502 $rows = $this->get('rows');
503
504 if (is_array($rows)) {
505 $this->addRowSelectors($rows);
506 }
507
508 }
509
510 /**
511 * Processing needed for buildForm and later.
512 */
513 public function preProcess() {
514 // set the various class variables
515
516 $this->_group = CRM_Core_PseudoConstant::group();
517
518 $this->_tag = CRM_Core_BAO_Tag::getTags();
519 $this->_done = FALSE;
520
521 /*
522 * we allow the controller to set force/reset externally, useful when we are being
523 * driven by the wizard framework
524 */
525
526 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean');
527
528 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean');
529 $this->_groupID = CRM_Utils_Request::retrieve('gid', 'Positive', $this);
530 $this->_amtgID = CRM_Utils_Request::retrieve('amtgID', 'Positive', $this);
531 $this->_ssID = CRM_Utils_Request::retrieve('ssID', 'Positive', $this);
532 $this->_sortByCharacter = CRM_Utils_Request::retrieve('sortByCharacter', 'String', $this);
533 $this->_ufGroupID = CRM_Utils_Request::retrieve('id', 'Positive', $this);
534 $this->_componentMode = CRM_Utils_Request::retrieve('component_mode', 'Positive', $this, FALSE, CRM_Contact_BAO_Query::MODE_CONTACTS, $_REQUEST);
535 $this->_operator = CRM_Utils_Request::retrieve('operator', 'String', $this, FALSE, CRM_Contact_BAO_Query::SEARCH_OPERATOR_AND, 'REQUEST');
536
537 /**
538 * set the button names
539 */
540 $this->_searchButtonName = $this->getButtonName('refresh');
541 $this->_actionButtonName = $this->getButtonName('next', 'action');
542
543 $this->assign('actionButtonName', $this->_actionButtonName);
544
545 // if we dont get this from the url, use default if one exsts
546 $config = CRM_Core_Config::singleton();
547 if ($this->_ufGroupID == NULL &&
548 $config->defaultSearchProfileID != NULL
549 ) {
550 $this->_ufGroupID = $config->defaultSearchProfileID;
551 }
552
553 // assign context to drive the template display, make sure context is valid
554 $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, 'search');
555 if (!CRM_Utils_Array::value($this->_context, self::validContext())) {
556 $this->_context = 'search';
557 }
558 $this->set('context', $this->_context);
559 $this->assign('context', $this->_context);
560
561 $this->_modeValue = self::getModeValue($this->_componentMode);
562 $this->assign($this->_modeValue);
563
564 $this->set('selectorName', self::$_selectorName);
565
566 // get user submitted values
567 // get it from controller only if form has been submitted, else preProcess has set this
568 // $this->controller->isModal( ) returns TRUE if page is
569 // valid, i.e all the validations are TRUE
570
571 if (!empty($_POST) && !$this->controller->isModal()) {
572 $this->_formValues = $this->controller->exportValues($this->_name);
573
574 $this->normalizeFormValues();
575 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
576 $this->_returnProperties = &$this->returnProperties();
577
578 // also get the uf group id directly from the post value
579 $this->_ufGroupID = CRM_Utils_Array::value('uf_group_id', $_POST, $this->_ufGroupID);
580 $this->_formValues['uf_group_id'] = $this->_ufGroupID;
581 $this->set('id', $this->_ufGroupID);
582
583 // also get the object mode directly from the post value
584 $this->_componentMode = CRM_Utils_Array::value('component_mode', $_POST, $this->_componentMode);
585
586 // also get the operator from the post value if set
587 $this->_operator = CRM_Utils_Array::value('operator', $_POST, $this->_operator);
588 $this->_formValues['operator'] = $this->_operator;
589 $this->set('operator', $this->_operator);
590 }
591 else {
592 $this->_formValues = $this->get('formValues');
593 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
594 $this->_returnProperties = &$this->returnProperties();
595 if (!empty($this->_ufGroupID)) {
596 $this->set('id', $this->_ufGroupID);
597 }
598 }
599
600 if (empty($this->_formValues)) {
601 //check if group is a smart group (fix for CRM-1255)
602 if ($this->_groupID) {
603 if ($ssId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_groupID, 'saved_search_id')) {
604 $this->_ssID = $ssId;
605 }
606 }
607
608 // fix for CRM-1907
609 if (isset($this->_ssID) && $this->_context != 'smog') {
610 // we only retrieve the saved search values if out current values are null
611 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
612
613 //fix for CRM-1505
614 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'mapping_id')) {
615 $this->_params = CRM_Contact_BAO_SavedSearch::getSearchParams($this->_ssID);
616 }
617 else {
618 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
619 }
620 $this->_returnProperties = &$this->returnProperties();
621 }
622 else {
623 if (isset($this->_ufGroupID)) {
624 // also set the uf group id if not already present
625 $this->_formValues['uf_group_id'] = $this->_ufGroupID;
626 }
627 if (isset($this->_componentMode)) {
628 $this->_formValues['component_mode'] = $this->_componentMode;
629 }
630 if (isset($this->_operator)) {
631 $this->_formValues['operator'] = $this->_operator;
632 }
633
634 // FIXME: we should generalise in a way that components could inject url-filters
635 // just like they build their own form elements
636 foreach (array(
637 'mailing_id',
638 'mailing_delivery_status',
639 'mailing_open_status',
640 'mailing_click_status',
641 'mailing_reply_status',
642 'mailing_optout',
643 'mailing_forward',
644 'mailing_unsubscribe',
645 'mailing_date_low',
646 'mailing_date_high',
647 ) as $mailingFilter) {
648 $type = 'String';
649 if ($mailingFilter == 'mailing_id' &&
650 $filterVal = CRM_Utils_Request::retrieve('mailing_id', 'Positive', $this)
651 ) {
652 $this->_formValues[$mailingFilter] = array($filterVal);
653 }
654 elseif ($filterVal = CRM_Utils_Request::retrieve($mailingFilter, $type, $this)) {
655 $this->_formValues[$mailingFilter] = $filterVal;
656 }
657 if ($filterVal) {
658 $this->_openedPanes['Mailings'] = 1;
659 $this->_formValues['hidden_CiviMail'] = 1;
660 }
661 }
662 }
663 }
664 $this->assign('id',
665 CRM_Utils_Array::value('uf_group_id', $this->_formValues)
666 );
667 $operator = CRM_Utils_Array::value('operator', $this->_formValues, CRM_Contact_BAO_Query::SEARCH_OPERATOR_AND);
668 $this->set('queryOperator', $operator);
669 if ($operator == CRM_Contact_BAO_Query::SEARCH_OPERATOR_OR) {
670 $this->assign('operator', ts('OR'));
671 }
672 else {
673 $this->assign('operator', ts('AND'));
674 }
675
676 // show the context menu only when we’re not searching for deleted contacts; CRM-5673
677 if (empty($this->_formValues['deleted_contacts'])) {
678 $menuItems = CRM_Contact_BAO_Contact::contextMenu();
679 $primaryActions = CRM_Utils_Array::value('primaryActions', $menuItems, array());
680 $this->_contextMenu = CRM_Utils_Array::value('moreActions', $menuItems, array());
681 $this->assign('contextMenu', $primaryActions + $this->_contextMenu);
682 }
683
684 if (!isset($this->_componentMode)) {
685 $this->_componentMode = CRM_Contact_BAO_Query::MODE_CONTACTS;
686 }
687 self::$_selectorName = $this->_modeValue['selectorName'];
688 self::setModeValues();
689
690 $setDynamic = FALSE;
691 if (strpos(self::$_selectorName, 'CRM_Contact_Selector') !== FALSE) {
692 $selector = new self::$_selectorName(
693 $this->_customSearchClass,
694 $this->_formValues,
695 $this->_params,
696 $this->_returnProperties,
697 $this->_action,
698 FALSE, TRUE,
699 $this->_context,
700 $this->_contextMenu
701 );
702 $setDynamic = TRUE;
703 }
704 else {
705 $selector = new self::$_selectorName(
706 $this->_params,
707 $this->_action,
708 NULL, FALSE, NULL,
709 "search", "advanced"
710 );
711 }
712
713 $selector->setKey($this->controller->_key);
714
715 $controller = new CRM_Contact_Selector_Controller($selector,
716 $this->get(CRM_Utils_Pager::PAGE_ID),
717 $this->get(CRM_Utils_Sort::SORT_ID),
718 CRM_Core_Action::VIEW,
719 $this,
720 CRM_Core_Selector_Controller::TRANSFER
721 );
722 $controller->setEmbedded(TRUE);
723 $controller->setDynamicAction($setDynamic);
724
725 if ($this->_force) {
726
727 $this->postProcess();
728
729 /*
730 * Note that we repeat this, since the search creates and stores
731 * values that potentially change the controller behavior. i.e. things
732 * like totalCount etc
733 */
734 $sortID = NULL;
735 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
736 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
737 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
738 );
739 }
740 $controller = new CRM_Contact_Selector_Controller($selector,
741 $this->get(CRM_Utils_Pager::PAGE_ID),
742 $sortID,
743 CRM_Core_Action::VIEW, $this, CRM_Core_Selector_Controller::TRANSFER
744 );
745 $controller->setEmbedded(TRUE);
746 $controller->setDynamicAction($setDynamic);
747 }
748
749 $controller->moveFromSessionToTemplate();
750 }
751
752 /**
753 * @return array
754 */
755 public function &getFormValues() {
756 return $this->_formValues;
757 }
758
759 /**
760 * Common post processing.
761 */
762 public function postProcess() {
763 /*
764 * sometime we do a postProcess early on, so we dont need to repeat it
765 * this will most likely introduce some more bugs :(
766 */
767
768 if ($this->_done) {
769 return;
770 }
771 $this->_done = TRUE;
772
773 //for prev/next pagination
774 $crmPID = CRM_Utils_Request::retrieve('crmPID', 'Integer');
775
776 if (array_key_exists($this->_searchButtonName, $_POST) ||
777 ($this->_force && !$crmPID)
778 ) {
779 //reset the cache table for new search
780 $cacheKey = "civicrm search {$this->controller->_key}";
781 Civi::service('prevnext')->deleteItem(NULL, $cacheKey);
782 }
783
784 //get the button name
785 $buttonName = $this->controller->getButtonName();
786
787 if (isset($this->_ufGroupID) && empty($this->_formValues['uf_group_id'])) {
788 $this->_formValues['uf_group_id'] = $this->_ufGroupID;
789 }
790
791 if (isset($this->_componentMode) && empty($this->_formValues['component_mode'])) {
792 $this->_formValues['component_mode'] = $this->_componentMode;
793 }
794
795 if (isset($this->_operator) && empty($this->_formValues['operator'])) {
796 $this->_formValues['operator'] = $this->_operator;
797 }
798
799 if (empty($this->_formValues['qfKey'])) {
800 $this->_formValues['qfKey'] = $this->controller->_key;
801 }
802
803 if (!CRM_Core_Permission::check('access deleted contacts')) {
804 unset($this->_formValues['deleted_contacts']);
805 }
806
807 $this->set('type', $this->_action);
808 $this->set('formValues', $this->_formValues);
809 $this->set('queryParams', $this->_params);
810 $this->set('returnProperties', $this->_returnProperties);
811
812 if ($buttonName == $this->_actionButtonName) {
813 // check actionName and if next, then do not repeat a search, since we are going to the next page
814 // hack, make sure we reset the task values
815 $stateMachine = $this->controller->getStateMachine();
816 $formName = $stateMachine->getTaskFormName();
817 $this->controller->resetPage($formName);
818 return;
819 }
820 else {
821 $output = CRM_Core_Selector_Controller::SESSION;
822
823 // create the selector, controller and run - store results in session
824 $searchChildGroups = TRUE;
825 if ($this->get('isAdvanced')) {
826 $searchChildGroups = FALSE;
827 }
828
829 $setDynamic = FALSE;
830
831 if (strpos(self::$_selectorName, 'CRM_Contact_Selector') !== FALSE) {
832 $selector = new self::$_selectorName(
833 $this->_customSearchClass,
834 $this->_formValues,
835 $this->_params,
836 $this->_returnProperties,
837 $this->_action,
838 FALSE,
839 $searchChildGroups,
840 $this->_context,
841 $this->_contextMenu
842 );
843 $setDynamic = TRUE;
844 }
845 else {
846 $selector = new self::$_selectorName(
847 $this->_params,
848 $this->_action,
849 NULL,
850 FALSE,
851 NULL,
852 "search",
853 "advanced"
854 );
855 }
856
857 $selector->setKey($this->controller->_key);
858
859 // added the sorting character to the form array
860 $config = CRM_Core_Config::singleton();
861 // do this only for contact search
862 if ($setDynamic && $config->includeAlphabeticalPager) {
863 // Don't recompute if we are just paging/sorting
864 if ($this->_reset || (empty($_GET['crmPID']) && empty($_GET['crmSID']) && !$this->_sortByCharacter)) {
865 $aToZBar = CRM_Utils_PagerAToZ::getAToZBar($selector, $this->_sortByCharacter);
866 $this->set('AToZBar', $aToZBar);
867 }
868 }
869
870 $sortID = NULL;
871 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
872 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
873 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
874 );
875 }
876 $controller = new CRM_Contact_Selector_Controller($selector,
877 $this->get(CRM_Utils_Pager::PAGE_ID),
878 $sortID,
879 CRM_Core_Action::VIEW,
880 $this,
881 $output
882 );
883 $controller->setEmbedded(TRUE);
884 $controller->setDynamicAction($setDynamic);
885 $controller->run();
886 }
887 }
888
889 /**
890 * @return NULL
891 */
892 public function &returnProperties() {
893 return CRM_Core_DAO::$_nullObject;
894 }
895
896 /**
897 * Return a descriptive name for the page, used in wizard header
898 *
899 * @return string
900 */
901 public function getTitle() {
902 return ts('Search');
903 }
904
905 }