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