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