Clean up docblock
[civicrm-core.git] / CRM / Contact / Form / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
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
6a488035
TO
529 // if we dont get this from the url, use default if one exsts
530 $config = CRM_Core_Config::singleton();
531 if ($this->_ufGroupID == NULL &&
532 $config->defaultSearchProfileID != NULL
533 ) {
534 $this->_ufGroupID = $config->defaultSearchProfileID;
535 }
536
537 // assign context to drive the template display, make sure context is valid
538 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
539 if (!CRM_Utils_Array::value($this->_context, self::validContext())) {
540 $this->_context = 'search';
541 }
542 $this->set('context', $this->_context);
543 $this->assign('context', $this->_context);
544
545 $this->_modeValue = self::getModeValue($this->_componentMode);
546 $this->assign($this->_modeValue);
547
548 $this->set('selectorName', self::$_selectorName);
549
550 // get user submitted values
551 // get it from controller only if form has been submitted, else preProcess has set this
4eeb9a5b
TO
552 // $this->controller->isModal( ) returns TRUE if page is
553 // valid, i.e all the validations are TRUE
6a488035
TO
554
555 if (!empty($_POST) && !$this->controller->isModal()) {
556 $this->_formValues = $this->controller->exportValues($this->_name);
557
558 $this->normalizeFormValues();
90c13aa2 559 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
6a488035
TO
560 $this->_returnProperties = &$this->returnProperties();
561
562 // also get the uf group id directly from the post value
563 $this->_ufGroupID = CRM_Utils_Array::value('uf_group_id', $_POST, $this->_ufGroupID);
564 $this->_formValues['uf_group_id'] = $this->_ufGroupID;
565 $this->set('id', $this->_ufGroupID);
566
567 // also get the object mode directly from the post value
568 $this->_componentMode = CRM_Utils_Array::value('component_mode', $_POST, $this->_componentMode);
569
570 // also get the operator from the post value if set
571 $this->_operator = CRM_Utils_Array::value('operator', $_POST, $this->_operator);
572 $this->_formValues['operator'] = $this->_operator;
573 $this->set('operator', $this->_operator);
574 }
575 else {
576 $this->_formValues = $this->get('formValues');
90c13aa2 577 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
6a488035
TO
578 $this->_returnProperties = &$this->returnProperties();
579 if (!empty($this->_ufGroupID)) {
580 $this->set('id', $this->_ufGroupID);
581 }
582 }
583
584 if (empty($this->_formValues)) {
585 //check if group is a smart group (fix for CRM-1255)
586 if ($this->_groupID) {
587 if ($ssId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_groupID, 'saved_search_id')) {
588 $this->_ssID = $ssId;
589 }
590 }
591
592 // fix for CRM-1907
593 if (isset($this->_ssID) && $this->_context != 'smog') {
594 // we only retrieve the saved search values if out current values are null
595 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
596
597 //fix for CRM-1505
598 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'mapping_id')) {
599 $this->_params = CRM_Contact_BAO_SavedSearch::getSearchParams($this->_ssID);
600 }
601 else {
602 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
603 }
604 $this->_returnProperties = &$this->returnProperties();
605 }
606 else {
607 if (isset($this->_ufGroupID)) {
608 // also set the uf group id if not already present
609 $this->_formValues['uf_group_id'] = $this->_ufGroupID;
610 }
611 if (isset($this->_componentMode)) {
612 $this->_formValues['component_mode'] = $this->_componentMode;
613 }
614 if (isset($this->_operator)) {
615 $this->_formValues['operator'] = $this->_operator;
616 }
617
618 // FIXME: we should generalise in a way that components could inject url-filters
619 // just like they build their own form elements
620 foreach (array(
353ffa53
TO
621 'mailing_id',
622 'mailing_delivery_status',
623 'mailing_open_status',
624 'mailing_click_status',
625 'mailing_reply_status',
626 'mailing_optout',
627 'mailing_forward',
628 'mailing_unsubscribe',
629 'mailing_date_low',
630 'mailing_date_high',
631 ) as $mailingFilter) {
6a488035
TO
632 $type = 'String';
633 if ($mailingFilter == 'mailing_id' &&
634 $filterVal = CRM_Utils_Request::retrieve('mailing_id', 'Positive', $this)
635 ) {
636 $this->_formValues[$mailingFilter] = array($filterVal);
637 }
638 elseif ($filterVal = CRM_Utils_Request::retrieve($mailingFilter, $type, $this)) {
639 $this->_formValues[$mailingFilter] = $filterVal;
640 }
641 if ($filterVal) {
642 $this->_openedPanes['Mailings'] = 1;
643 $this->_formValues['hidden_CiviMail'] = 1;
644 }
645 }
646 }
647 }
648 $this->assign('id',
649 CRM_Utils_Array::value('uf_group_id', $this->_formValues)
650 );
651 $operator = CRM_Utils_Array::value('operator', $this->_formValues, 'AND');
652 $this->set('queryOperator', $operator);
653 if ($operator == 'OR') {
654 $this->assign('operator', ts('OR'));
655 }
656 else {
657 $this->assign('operator', ts('AND'));
658 }
659
660 // show the context menu only when we’re not searching for deleted contacts; CRM-5673
a7488080 661 if (empty($this->_formValues['deleted_contacts'])) {
6a488035
TO
662 $menuItems = CRM_Contact_BAO_Contact::contextMenu();
663 $primaryActions = CRM_Utils_Array::value('primaryActions', $menuItems, array());
664 $this->_contextMenu = CRM_Utils_Array::value('moreActions', $menuItems, array());
665 $this->assign('contextMenu', $primaryActions + $this->_contextMenu);
666 }
667
668 if (!isset($this->_componentMode)) {
669 $this->_componentMode = CRM_Contact_BAO_Query::MODE_CONTACTS;
670 }
836eb043 671 self::setModeValues();
6a488035
TO
672
673 self::$_selectorName = $this->_modeValue['selectorName'];
674
675 $setDynamic = FALSE;
676 if (strpos(self::$_selectorName, 'CRM_Contact_Selector') !== FALSE) {
677 $selector = new self::$_selectorName(
678 $this->_customSearchClass,
679 $this->_formValues,
680 $this->_params,
681 $this->_returnProperties,
682 $this->_action,
ab8a593e 683 FALSE, TRUE,
6a488035
TO
684 $this->_context,
685 $this->_contextMenu
686 );
687 $setDynamic = TRUE;
688 }
689 else {
690 $selector = new self::$_selectorName(
691 $this->_params,
692 $this->_action,
ab8a593e 693 NULL, FALSE, NULL,
6a488035
TO
694 "search", "advanced"
695 );
696 }
697
698 $selector->setKey($this->controller->_key);
699
700 $controller = new CRM_Contact_Selector_Controller($selector,
701 $this->get(CRM_Utils_Pager::PAGE_ID),
702 $this->get(CRM_Utils_Sort::SORT_ID),
703 CRM_Core_Action::VIEW,
704 $this,
705 CRM_Core_Selector_Controller::TRANSFER
706 );
707 $controller->setEmbedded(TRUE);
708 $controller->setDynamicAction($setDynamic);
709
710 if ($this->_force) {
711
712 $this->postProcess();
713
714 /*
deb50ba3
CW
715 * Note that we repeat this, since the search creates and stores
716 * values that potentially change the controller behavior. i.e. things
717 * like totalCount etc
718 */
6a488035
TO
719 $sortID = NULL;
720 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
721 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
722 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
723 );
724 }
725 $controller = new CRM_Contact_Selector_Controller($selector,
726 $this->get(CRM_Utils_Pager::PAGE_ID),
727 $sortID,
728 CRM_Core_Action::VIEW, $this, CRM_Core_Selector_Controller::TRANSFER
729 );
730 $controller->setEmbedded(TRUE);
731 $controller->setDynamicAction($setDynamic);
732 }
733
734 $controller->moveFromSessionToTemplate();
735 }
736
86538308
EM
737 /**
738 * @return array
739 */
00be9182 740 public function &getFormValues() {
6a488035
TO
741 return $this->_formValues;
742 }
743
744 /**
fe482240 745 * Common post processing.
6a488035 746 */
00be9182 747 public function postProcess() {
6a488035
TO
748 /*
749 * sometime we do a postProcess early on, so we dont need to repeat it
750 * this will most likely introduce some more bugs :(
751 */
752
753 if ($this->_done) {
754 return;
755 }
756 $this->_done = TRUE;
757
758 //for prev/next pagination
a3d827a7 759 $crmPID = CRM_Utils_Request::retrieve('crmPID', 'Integer');
6a488035
TO
760
761 if (array_key_exists($this->_searchButtonName, $_POST) ||
353ffa53
TO
762 ($this->_force && !$crmPID)
763 ) {
6a488035
TO
764 //reset the cache table for new search
765 $cacheKey = "civicrm search {$this->controller->_key}";
766 CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey);
767 }
768
769 //get the button name
770 $buttonName = $this->controller->getButtonName();
771
8cc574cf 772 if (isset($this->_ufGroupID) && empty($this->_formValues['uf_group_id'])) {
6a488035
TO
773 $this->_formValues['uf_group_id'] = $this->_ufGroupID;
774 }
775
8cc574cf 776 if (isset($this->_componentMode) && empty($this->_formValues['component_mode'])) {
6a488035
TO
777 $this->_formValues['component_mode'] = $this->_componentMode;
778 }
779
8cc574cf 780 if (isset($this->_operator) && empty($this->_formValues['operator'])) {
6a488035
TO
781 $this->_formValues['operator'] = $this->_operator;
782 }
783
a7488080 784 if (empty($this->_formValues['qfKey'])) {
6a488035
TO
785 $this->_formValues['qfKey'] = $this->controller->_key;
786 }
787
788 if (!CRM_Core_Permission::check('access deleted contacts')) {
789 unset($this->_formValues['deleted_contacts']);
790 }
791
792 $this->set('type', $this->_action);
793 $this->set('formValues', $this->_formValues);
794 $this->set('queryParams', $this->_params);
795 $this->set('returnProperties', $this->_returnProperties);
796
e341bbee 797 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
798 // check actionName and if next, then do not repeat a search, since we are going to the next page
799 // hack, make sure we reset the task values
800 $stateMachine = $this->controller->getStateMachine();
801 $formName = $stateMachine->getTaskFormName();
802 $this->controller->resetPage($formName);
803 return;
804 }
805 else {
806 $output = CRM_Core_Selector_Controller::SESSION;
807
808 // create the selector, controller and run - store results in session
809 $searchChildGroups = TRUE;
810 if ($this->get('isAdvanced')) {
811 $searchChildGroups = FALSE;
812 }
813
814 $setDynamic = FALSE;
815
816 if (strpos(self::$_selectorName, 'CRM_Contact_Selector') !== FALSE) {
79d7553f 817 $selector = new self::$_selectorName(
6a488035
TO
818 $this->_customSearchClass,
819 $this->_formValues,
820 $this->_params,
821 $this->_returnProperties,
822 $this->_action,
ab8a593e 823 FALSE,
6a488035
TO
824 $searchChildGroups,
825 $this->_context,
826 $this->_contextMenu
827 );
828 $setDynamic = TRUE;
829 }
830 else {
d3e86119 831 $selector = new self::$_selectorName(
6a488035
TO
832 $this->_params,
833 $this->_action,
e60f24eb 834 NULL,
ab8a593e 835 FALSE,
e60f24eb 836 NULL,
6a488035
TO
837 "search",
838 "advanced"
839 );
840 }
841
842 $selector->setKey($this->controller->_key);
843
844 // added the sorting character to the form array
6a488035
TO
845 $config = CRM_Core_Config::singleton();
846 // do this only for contact search
847 if ($setDynamic && $config->includeAlphabeticalPager) {
e166ff79
CW
848 // Don't recompute if we are just paging/sorting
849 if ($this->_reset || (empty($_GET['crmPID']) && empty($_GET['crmSID']) && !$this->_sortByCharacter)) {
6a488035
TO
850 $aToZBar = CRM_Utils_PagerAToZ::getAToZBar($selector, $this->_sortByCharacter);
851 $this->set('AToZBar', $aToZBar);
852 }
853 }
854
855 $sortID = NULL;
856 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
857 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
858 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
859 );
860 }
861 $controller = new CRM_Contact_Selector_Controller($selector,
862 $this->get(CRM_Utils_Pager::PAGE_ID),
863 $sortID,
864 CRM_Core_Action::VIEW,
865 $this,
866 $output
867 );
868 $controller->setEmbedded(TRUE);
869 $controller->setDynamicAction($setDynamic);
870 $controller->run();
871 }
872 }
873
86538308 874 /**
e60f24eb 875 * @return NULL
86538308 876 */
00be9182 877 public function &returnProperties() {
6a488035
TO
878 return CRM_Core_DAO::$_nullObject;
879 }
880
881 /**
882 * Return a descriptive name for the page, used in wizard header
883 *
884 * @return string
6a488035 885 */
00be9182 886 public function getTitle() {
6a488035
TO
887 return ts('Search');
888 }
96025800 889
6a488035 890}