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