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