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