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