Merge pull request #3391 from eileenmcnaughton/CRM-14768
[civicrm-core.git] / CRM / Contact / Form / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
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 */
3efb5b86 44class CRM_Contact_Form_Search extends CRM_Core_Form_Search {
6a488035
TO
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
6a488035
TO
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
86538308
EM
248 /**
249 * @param $context
250 *
251 * @return bool
252 */
6a488035
TO
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 ),
2cc569f2
PJ
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 ),
6a488035
TO
333 );
334 }
335 }
336
86538308
EM
337 /**
338 * @param int $mode
339 *
340 * @return mixed
341 */
6a488035
TO
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
86538308
EM
352 /**
353 * @return array
354 */
6a488035
TO
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() {
3efb5b86 387 parent::buildQuickForm();
8e4ec1f5 388 CRM_Core_Resources::singleton()
8e4ec1f5
CW
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');
6a488035
TO
392 $permission = CRM_Core_Permission::getPermission();
393 // some tasks.. what do we want to do with the selected contacts ?
394 $tasks = array('' => ts('- actions -'));
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->add('select', 'task', ts('Actions:') . ' ', $tasks);
508 $this->add('submit', $this->_actionButtonName, ts('Go'),
509 array(
510 'class' => 'form-submit',
511 'id' => 'Go',
512 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 0, 1);",
513 )
514 );
515 }
516
517 // need to perform tasks on all or selected items ? using radio_ts(task selection) for it
518 $selectedRowsRadio = $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array(
519 'checked' => 'checked',
520 'onclick' => 'toggleTaskAction( true );',
521 ));
522 $this->assign('ts_sel_id', $selectedRowsRadio->_attributes['id']);
523
524
525 if ($qfKeyParam = CRM_Utils_Array::value('qfKey', $this->_formValues)) {
526 $qfKeyParam = "civicrm search {$qfKeyParam}";
527 $selectedContactIdsArr = CRM_Core_BAO_PrevNextCache::getSelection($qfKeyParam);
528 $selectedContactIds = array_keys($selectedContactIdsArr[$qfKeyParam]);
529 }
530
531 $this->assign_by_ref('selectedContactIds', $selectedContactIds);
532
d664f648 533 $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' );"));
6a488035
TO
534 $this->assign('ts_all_id', $allRowsRadio->_attributes['id']);
535
536 /*
b9599ed0
CW
537 * add form checkboxes for each row. This is needed out here to conform to QF protocol
538 * of all elements being declared in builQuickForm
539 */
6a488035
TO
540
541 $rows = $this->get('rows');
542
543 if (is_array($rows)) {
d664f648 544 $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('class' => 'select-rows', 'onclick' => "toggleTaskAction( true ); toggleContactSelection( 'toggleSelect', '" . $qfKeyParam . "' , 'multiple' );"));
6a488035
TO
545
546 $unselectedContactIds = array();
547 foreach ($rows as $row) {
548 $this->addElement('checkbox', $row['checkbox'],
549 NULL, NULL,
d664f648 550 array('onclick' => "toggleContactSelection( '" . $row['checkbox'] . "', '" . $qfKeyParam . "' , 'single' );toggleTaskAction( true );", 'class' => 'select-row')
6a488035
TO
551 );
552
553 if (!in_array($row['contact_id'], $selectedContactIds)) {
554 $unselectedContactIds[] = $row['contact_id'];
555 }
556 }
557 $this->assign_by_ref('unselectedContactIds', $unselectedContactIds);
558 }
559
6a488035
TO
560 }
561
562 /**
563 * processing needed for buildForm and later
564 *
565 * @return void
566 * @access public
567 */
568 function preProcess() {
569 // set the various class variables
570
571 $this->_group = CRM_Core_PseudoConstant::group();
572
573 $this->_groupIterator = CRM_Core_PseudoConstant::groupIterator();
574 $this->_tag = CRM_Core_BAO_Tag::getTags();
575 $this->_done = FALSE;
576
577 /*
deb50ba3
CW
578 * we allow the controller to set force/reset externally, useful when we are being
579 * driven by the wizard framework
580 */
6a488035
TO
581
582 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean',
583 CRM_Core_DAO::$_nullObject
584 );
585
586 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', CRM_Core_DAO::$_nullObject);
587 $this->_groupID = CRM_Utils_Request::retrieve('gid', 'Positive', $this);
588 $this->_amtgID = CRM_Utils_Request::retrieve('amtgID', 'Positive', $this);
589 $this->_ssID = CRM_Utils_Request::retrieve('ssID', 'Positive', $this);
590 $this->_sortByCharacter = CRM_Utils_Request::retrieve('sortByCharacter', 'String', $this);
591 $this->_ufGroupID = CRM_Utils_Request::retrieve('id', 'Positive', $this);
592 $this->_componentMode = CRM_Utils_Request::retrieve('component_mode', 'Positive', $this, FALSE, 1, $_REQUEST);
593 $this->_operator = CRM_Utils_Request::retrieve('operator', 'String', $this, FALSE, 1, $_REQUEST, 'AND');
594
595 /**
596 * set the button names
597 */
598 $this->_searchButtonName = $this->getButtonName('refresh');
6a488035
TO
599 $this->_actionButtonName = $this->getButtonName('next', 'action');
600
6a488035
TO
601 $this->assign('actionButtonName', $this->_actionButtonName);
602
603 // reset from session, CRM-3526
604 $session = CRM_Core_Session::singleton();
605 if ($this->_force && $session->get('selectedSearchContactIds')) {
606 $session->resetScope('selectedSearchContactIds');
607 }
608
609 // if we dont get this from the url, use default if one exsts
610 $config = CRM_Core_Config::singleton();
611 if ($this->_ufGroupID == NULL &&
612 $config->defaultSearchProfileID != NULL
613 ) {
614 $this->_ufGroupID = $config->defaultSearchProfileID;
615 }
616
617 // assign context to drive the template display, make sure context is valid
618 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
619 if (!CRM_Utils_Array::value($this->_context, self::validContext())) {
620 $this->_context = 'search';
621 }
622 $this->set('context', $this->_context);
623 $this->assign('context', $this->_context);
624
625 $this->_modeValue = self::getModeValue($this->_componentMode);
626 $this->assign($this->_modeValue);
627
628 $this->set('selectorName', self::$_selectorName);
629
630 // get user submitted values
631 // get it from controller only if form has been submitted, else preProcess has set this
632 // $this->controller->isModal( ) returns true if page is
633 // valid, i.e all the validations are true
634
635 if (!empty($_POST) && !$this->controller->isModal()) {
636 $this->_formValues = $this->controller->exportValues($this->_name);
637
638 $this->normalizeFormValues();
639 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
640 $this->_returnProperties = &$this->returnProperties();
641
642 // also get the uf group id directly from the post value
643 $this->_ufGroupID = CRM_Utils_Array::value('uf_group_id', $_POST, $this->_ufGroupID);
644 $this->_formValues['uf_group_id'] = $this->_ufGroupID;
645 $this->set('id', $this->_ufGroupID);
646
647 // also get the object mode directly from the post value
648 $this->_componentMode = CRM_Utils_Array::value('component_mode', $_POST, $this->_componentMode);
649
650 // also get the operator from the post value if set
651 $this->_operator = CRM_Utils_Array::value('operator', $_POST, $this->_operator);
652 $this->_formValues['operator'] = $this->_operator;
653 $this->set('operator', $this->_operator);
654 }
655 else {
656 $this->_formValues = $this->get('formValues');
657 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
658 $this->_returnProperties = &$this->returnProperties();
659 if (!empty($this->_ufGroupID)) {
660 $this->set('id', $this->_ufGroupID);
661 }
662 }
663
664 if (empty($this->_formValues)) {
665 //check if group is a smart group (fix for CRM-1255)
666 if ($this->_groupID) {
667 if ($ssId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_groupID, 'saved_search_id')) {
668 $this->_ssID = $ssId;
669 }
670 }
671
672 // fix for CRM-1907
673 if (isset($this->_ssID) && $this->_context != 'smog') {
674 // we only retrieve the saved search values if out current values are null
675 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
676
677 //fix for CRM-1505
678 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'mapping_id')) {
679 $this->_params = CRM_Contact_BAO_SavedSearch::getSearchParams($this->_ssID);
680 }
681 else {
682 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
683 }
684 $this->_returnProperties = &$this->returnProperties();
685 }
686 else {
687 if (isset($this->_ufGroupID)) {
688 // also set the uf group id if not already present
689 $this->_formValues['uf_group_id'] = $this->_ufGroupID;
690 }
691 if (isset($this->_componentMode)) {
692 $this->_formValues['component_mode'] = $this->_componentMode;
693 }
694 if (isset($this->_operator)) {
695 $this->_formValues['operator'] = $this->_operator;
696 }
697
698 // FIXME: we should generalise in a way that components could inject url-filters
699 // just like they build their own form elements
700 foreach (array(
701 'mailing_id', 'mailing_delivery_status', 'mailing_open_status',
702 'mailing_click_status', 'mailing_reply_status', 'mailing_optout',
703 'mailing_forward', 'mailing_unsubscribe', 'mailing_date_low',
704 'mailing_date_high',
705 ) as $mailingFilter) {
706 $type = 'String';
707 if ($mailingFilter == 'mailing_id' &&
708 $filterVal = CRM_Utils_Request::retrieve('mailing_id', 'Positive', $this)
709 ) {
710 $this->_formValues[$mailingFilter] = array($filterVal);
711 }
712 elseif ($filterVal = CRM_Utils_Request::retrieve($mailingFilter, $type, $this)) {
713 $this->_formValues[$mailingFilter] = $filterVal;
714 }
715 if ($filterVal) {
716 $this->_openedPanes['Mailings'] = 1;
717 $this->_formValues['hidden_CiviMail'] = 1;
718 }
719 }
720 }
721 }
722 $this->assign('id',
723 CRM_Utils_Array::value('uf_group_id', $this->_formValues)
724 );
725 $operator = CRM_Utils_Array::value('operator', $this->_formValues, 'AND');
726 $this->set('queryOperator', $operator);
727 if ($operator == 'OR') {
728 $this->assign('operator', ts('OR'));
729 }
730 else {
731 $this->assign('operator', ts('AND'));
732 }
733
734 // show the context menu only when we’re not searching for deleted contacts; CRM-5673
a7488080 735 if (empty($this->_formValues['deleted_contacts'])) {
6a488035
TO
736 $menuItems = CRM_Contact_BAO_Contact::contextMenu();
737 $primaryActions = CRM_Utils_Array::value('primaryActions', $menuItems, array());
738 $this->_contextMenu = CRM_Utils_Array::value('moreActions', $menuItems, array());
739 $this->assign('contextMenu', $primaryActions + $this->_contextMenu);
740 }
741
742 if (!isset($this->_componentMode)) {
743 $this->_componentMode = CRM_Contact_BAO_Query::MODE_CONTACTS;
744 }
745 $modeValues = self::getModeValue($this->_componentMode);
746
747 self::$_selectorName = $this->_modeValue['selectorName'];
748
749 $setDynamic = FALSE;
750 if (strpos(self::$_selectorName, 'CRM_Contact_Selector') !== FALSE) {
751 $selector = new self::$_selectorName(
752 $this->_customSearchClass,
753 $this->_formValues,
754 $this->_params,
755 $this->_returnProperties,
756 $this->_action,
757 false, true,
758 $this->_context,
759 $this->_contextMenu
760 );
761 $setDynamic = TRUE;
762 }
763 else {
764 $selector = new self::$_selectorName(
765 $this->_params,
766 $this->_action,
767 null, false, null,
768 "search", "advanced"
769 );
770 }
771
772 $selector->setKey($this->controller->_key);
773
774 $controller = new CRM_Contact_Selector_Controller($selector,
775 $this->get(CRM_Utils_Pager::PAGE_ID),
776 $this->get(CRM_Utils_Sort::SORT_ID),
777 CRM_Core_Action::VIEW,
778 $this,
779 CRM_Core_Selector_Controller::TRANSFER
780 );
781 $controller->setEmbedded(TRUE);
782 $controller->setDynamicAction($setDynamic);
783
784 if ($this->_force) {
785
786 $this->postProcess();
787
788 /*
deb50ba3
CW
789 * Note that we repeat this, since the search creates and stores
790 * values that potentially change the controller behavior. i.e. things
791 * like totalCount etc
792 */
6a488035
TO
793 $sortID = NULL;
794 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
795 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
796 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
797 );
798 }
799 $controller = new CRM_Contact_Selector_Controller($selector,
800 $this->get(CRM_Utils_Pager::PAGE_ID),
801 $sortID,
802 CRM_Core_Action::VIEW, $this, CRM_Core_Selector_Controller::TRANSFER
803 );
804 $controller->setEmbedded(TRUE);
805 $controller->setDynamicAction($setDynamic);
806 }
807
808 $controller->moveFromSessionToTemplate();
809 }
810
86538308
EM
811 /**
812 * @return array
813 */
6a488035
TO
814 function &getFormValues() {
815 return $this->_formValues;
816 }
817
818 /**
819 * Common post processing
820 *
821 * @return void
822 * @access public
823 */
824 function postProcess() {
825 /*
826 * sometime we do a postProcess early on, so we dont need to repeat it
827 * this will most likely introduce some more bugs :(
828 */
829
830 if ($this->_done) {
831 return;
832 }
833 $this->_done = TRUE;
834
835 //for prev/next pagination
836 $crmPID = CRM_Utils_Request::retrieve('crmPID', 'Integer', CRM_Core_DAO::$_nullObject);
837
838 if (array_key_exists($this->_searchButtonName, $_POST) ||
839 ($this->_force && !$crmPID)) {
840 //reset the cache table for new search
841 $cacheKey = "civicrm search {$this->controller->_key}";
842 CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey);
843 }
844
845 //get the button name
846 $buttonName = $this->controller->getButtonName();
847
8cc574cf 848 if (isset($this->_ufGroupID) && empty($this->_formValues['uf_group_id'])) {
6a488035
TO
849 $this->_formValues['uf_group_id'] = $this->_ufGroupID;
850 }
851
8cc574cf 852 if (isset($this->_componentMode) && empty($this->_formValues['component_mode'])) {
6a488035
TO
853 $this->_formValues['component_mode'] = $this->_componentMode;
854 }
855
8cc574cf 856 if (isset($this->_operator) && empty($this->_formValues['operator'])) {
6a488035
TO
857 $this->_formValues['operator'] = $this->_operator;
858 }
859
a7488080 860 if (empty($this->_formValues['qfKey'])) {
6a488035
TO
861 $this->_formValues['qfKey'] = $this->controller->_key;
862 }
863
864 if (!CRM_Core_Permission::check('access deleted contacts')) {
865 unset($this->_formValues['deleted_contacts']);
866 }
867
868 $this->set('type', $this->_action);
869 $this->set('formValues', $this->_formValues);
870 $this->set('queryParams', $this->_params);
871 $this->set('returnProperties', $this->_returnProperties);
872
e341bbee 873 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
874 // check actionName and if next, then do not repeat a search, since we are going to the next page
875 // hack, make sure we reset the task values
876 $stateMachine = $this->controller->getStateMachine();
877 $formName = $stateMachine->getTaskFormName();
878 $this->controller->resetPage($formName);
879 return;
880 }
881 else {
882 $output = CRM_Core_Selector_Controller::SESSION;
883
884 // create the selector, controller and run - store results in session
885 $searchChildGroups = TRUE;
886 if ($this->get('isAdvanced')) {
887 $searchChildGroups = FALSE;
888 }
889
890 $setDynamic = FALSE;
891
892 if (strpos(self::$_selectorName, 'CRM_Contact_Selector') !== FALSE) {
893 $selector = new self::$_selectorName (
894 $this->_customSearchClass,
895 $this->_formValues,
896 $this->_params,
897 $this->_returnProperties,
898 $this->_action,
899 false,
900 $searchChildGroups,
901 $this->_context,
902 $this->_contextMenu
903 );
904 $setDynamic = TRUE;
905 }
906 else {
907 $selector = new self::$_selectorName (
908 $this->_params,
909 $this->_action,
910 null,
911 false,
912 null,
913 "search",
914 "advanced"
915 );
916 }
917
918 $selector->setKey($this->controller->_key);
919
920 // added the sorting character to the form array
6a488035
TO
921 $config = CRM_Core_Config::singleton();
922 // do this only for contact search
923 if ($setDynamic && $config->includeAlphabeticalPager) {
e166ff79
CW
924 // Don't recompute if we are just paging/sorting
925 if ($this->_reset || (empty($_GET['crmPID']) && empty($_GET['crmSID']) && !$this->_sortByCharacter)) {
6a488035
TO
926 $aToZBar = CRM_Utils_PagerAToZ::getAToZBar($selector, $this->_sortByCharacter);
927 $this->set('AToZBar', $aToZBar);
928 }
929 }
930
931 $sortID = NULL;
932 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
933 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
934 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
935 );
936 }
937 $controller = new CRM_Contact_Selector_Controller($selector,
938 $this->get(CRM_Utils_Pager::PAGE_ID),
939 $sortID,
940 CRM_Core_Action::VIEW,
941 $this,
942 $output
943 );
944 $controller->setEmbedded(TRUE);
945 $controller->setDynamicAction($setDynamic);
946 $controller->run();
947 }
948 }
949
86538308
EM
950 /**
951 * @return null
952 */
6a488035
TO
953 function &returnProperties() {
954 return CRM_Core_DAO::$_nullObject;
955 }
956
957 /**
958 * Return a descriptive name for the page, used in wizard header
959 *
960 * @return string
961 * @access public
962 */
963 function getTitle() {
964 return ts('Search');
965 }
966}
967