Update copyright date in comments
[civicrm-core.git] / CRM / Contact / Form / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
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 45
d424ffde 46 /**
fe482240 47 * list of valid contexts.
6a488035
TO
48 *
49 * @var array
6a488035
TO
50 */
51 static $_validContext = NULL;
52
53 /**
fe482240 54 * List of values used when we want to display other objects.
6a488035
TO
55 *
56 * @var array
6a488035
TO
57 */
58 static $_modeValues = NULL;
59
6a488035 60 /**
fe482240 61 * The contextMenu.
6a488035
TO
62 *
63 * @var array
6a488035
TO
64 */
65 protected $_contextMenu;
66
67 /**
fe482240 68 * The groupId retrieved from the GET vars.
6a488035
TO
69 *
70 * @var int
6a488035
TO
71 */
72 public $_groupID;
73
74 /**
fe482240 75 * The Group ID belonging to Add Member to group ID.
6a488035
TO
76 * retrieved from the GET vars
77 *
78 * @var int
6a488035
TO
79 */
80 protected $_amtgID;
81
82 /**
fe482240 83 * The saved search ID retrieved from the GET vars.
6a488035
TO
84 *
85 * @var int
6a488035
TO
86 */
87 protected $_ssID;
88
6a488035 89 /**
fe482240 90 * The group elements.
6a488035
TO
91 *
92 * @var array
6a488035
TO
93 */
94 public $_group;
95 public $_groupElement;
96 public $_groupIterator;
97
98 /**
fe482240 99 * The tag elements.
6a488035
TO
100 *
101 * @var array
6a488035
TO
102 */
103 public $_tag;
104 public $_tagElement;
105
6a488035 106 /**
fe482240 107 * The params used for search.
6a488035
TO
108 *
109 * @var array
6a488035
TO
110 */
111 protected $_params;
112
113 /**
fe482240 114 * The return properties used for search.
6a488035
TO
115 *
116 * @var array
6a488035
TO
117 */
118 protected $_returnProperties;
119
120 /**
fe482240 121 * The sort by character.
6a488035
TO
122 *
123 * @var string
6a488035
TO
124 */
125 protected $_sortByCharacter;
126
127 /**
fe482240 128 * The profile group id used for display.
6a488035
TO
129 *
130 * @var integer
6a488035
TO
131 */
132 protected $_ufGroupID;
133
c490a46a 134 /**
100fef9d 135 * Csv - common search values
c490a46a
CW
136 *
137 * @var array
c490a46a 138 */
6a488035
TO
139 static $csv = array('contact_type', 'group', 'tag');
140
141 /**
142 * @var string how to display the results. Should we display as
143 * contributons, members, cases etc
144 */
145 protected $_componentMode;
146
147 /**
148 * @var string what operator should we use, AND or OR
149 */
150 protected $_operator;
151
152 protected $_modeValue;
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();
162
163 /**
fe482240 164 * Define the set of valid contexts that the search form operates on.
6a488035 165 *
a6c01b45
CW
166 * @return array
167 * the valid context set and the titles
6a488035 168 */
00be9182 169 public static function &validContext() {
6a488035
TO
170 if (!(self::$_validContext)) {
171 self::$_validContext = array(
172 'smog' => 'Show members of group',
173 'amtg' => 'Add members to group',
174 'basic' => 'Basic Search',
175 'search' => 'Search',
176 'builder' => 'Search Builder',
177 'advanced' => 'Advanced Search',
178 'custom' => 'Custom Search',
179 );
180 }
181 return self::$_validContext;
182 }
183
86538308
EM
184 /**
185 * @param $context
186 *
187 * @return bool
188 */
00be9182 189 public static function isSearchContext($context) {
6a488035
TO
190 $searchContext = CRM_Utils_Array::value($context, self::validContext());
191 return $searchContext ? TRUE : FALSE;
192 }
193
00be9182 194 public static function setModeValues() {
6a488035
TO
195 if (!self::$_modeValues) {
196 self::$_modeValues = array(
197 1 => array(
198 'selectorName' => self::$_selectorName,
199 'selectorLabel' => ts('Contacts'),
200 'taskFile' => 'CRM/Contact/Form/Search/ResultTasks.tpl',
201 'taskContext' => NULL,
202 'resultFile' => 'CRM/Contact/Form/Selector.tpl',
203 'resultContext' => NULL,
204 'taskClassName' => 'CRM_Contact_Task',
205 ),
206 2 => array(
207 'selectorName' => 'CRM_Contribute_Selector_Search',
208 'selectorLabel' => ts('Contributions'),
209 'taskFile' => 'CRM/common/searchResultTasks.tpl',
210 'taskContext' => 'Contribution',
211 'resultFile' => 'CRM/Contribute/Form/Selector.tpl',
212 'resultContext' => 'Search',
213 'taskClassName' => 'CRM_Contribute_Task',
214 ),
215 3 => array(
216 'selectorName' => 'CRM_Event_Selector_Search',
217 'selectorLabel' => ts('Event Participants'),
218 'taskFile' => 'CRM/common/searchResultTasks.tpl',
219 'taskContext' => NULL,
220 'resultFile' => 'CRM/Event/Form/Selector.tpl',
221 'resultContext' => 'Search',
222 'taskClassName' => 'CRM_Event_Task',
223 ),
224 4 => array(
225 'selectorName' => 'CRM_Activity_Selector_Search',
226 'selectorLabel' => ts('Activities'),
227 'taskFile' => 'CRM/common/searchResultTasks.tpl',
228 'taskContext' => NULL,
229 'resultFile' => 'CRM/Activity/Form/Selector.tpl',
230 'resultContext' => 'Search',
231 'taskClassName' => 'CRM_Activity_Task',
232 ),
233 5 => array(
234 'selectorName' => 'CRM_Member_Selector_Search',
235 'selectorLabel' => ts('Memberships'),
236 'taskFile' => "CRM/common/searchResultTasks.tpl",
237 'taskContext' => NULL,
238 'resultFile' => 'CRM/Member/Form/Selector.tpl',
239 'resultContext' => 'Search',
240 'taskClassName' => 'CRM_Member_Task',
241 ),
242 6 => array(
243 'selectorName' => 'CRM_Case_Selector_Search',
244 'selectorLabel' => ts('Cases'),
245 'taskFile' => "CRM/common/searchResultTasks.tpl",
246 'taskContext' => NULL,
247 'resultFile' => 'CRM/Case/Form/Selector.tpl',
248 'resultContext' => 'Search',
249 'taskClassName' => 'CRM_Case_Task',
250 ),
251 7 => array(
252 'selectorName' => self::$_selectorName,
253 'selectorLabel' => ts('Related Contacts'),
254 'taskFile' => 'CRM/Contact/Form/Search/ResultTasks.tpl',
255 'taskContext' => NULL,
256 'resultFile' => 'CRM/Contact/Form/Selector.tpl',
257 'resultContext' => NULL,
258 'taskClassName' => 'CRM_Contact_Task',
259 ),
2cc569f2
PJ
260 8 => array(
261 'selectorName' => 'CRM_Mailing_Selector_Search',
262 'selectorLabel' => ts('Mailings'),
263 'taskFile' => "CRM/common/searchResultTasks.tpl",
264 'taskContext' => NULL,
265 'resultFile' => 'CRM/Mailing/Form/Selector.tpl',
266 'resultContext' => 'Search',
267 'taskClassName' => 'CRM_Mailing_Task',
268 ),
6a488035
TO
269 );
270 }
271 }
272
86538308
EM
273 /**
274 * @param int $mode
275 *
276 * @return mixed
277 */
00be9182 278 public static function getModeValue($mode = 1) {
6a488035
TO
279 self::setModeValues();
280
281 if (!array_key_exists($mode, self::$_modeValues)) {
282 $mode = 1;
283 }
284
285 return self::$_modeValues[$mode];
286 }
287
86538308
EM
288 /**
289 * @return array
290 */
00be9182 291 public static function getModeSelect() {
6a488035
TO
292 self::setModeValues();
293
294 $select = array();
295 foreach (self::$_modeValues as $id => & $value) {
296 $select[$id] = $value['selectorLabel'];
297 }
298
299 // unset contributions or participants if user does not have
300 // permission on them
301 if (!CRM_Core_Permission::access('CiviContribute')) {
302 unset($select['2']);
303 }
304
305 if (!CRM_Core_Permission::access('CiviEvent')) {
306 unset($select['3']);
307 }
308
309 if (!CRM_Core_Permission::check('view all activities')) {
310 unset($select['4']);
311 }
312 return $select;
313 }
314
315 /**
32795e82 316 * Builds the list of tasks or actions that a searcher can perform on a result set.
6a488035 317 *
32795e82 318 * @return array
6a488035 319 */
e98a9804 320 public function buildTaskList() {
7a3978aa
FG
321 if ($this->_context !== 'amtg') {
322 $permission = CRM_Core_Permission::getPermission();
323
324 if ($this->_componentMode == 1 || $this->_componentMode == 7) {
325 $this->_taskList += CRM_Contact_Task::permissionedTaskTitles($permission,
326 CRM_Utils_Array::value('deleted_contacts', $this->_formValues)
327 );
328 } else {
329 $className = $this->_modeValue['taskClassName'];
330 $this->_taskList += $className::permissionedTaskTitles($permission, false);
331 }
6a488035 332
7a3978aa
FG
333 // Only offer the "Update Smart Group" task if a smart group/saved search is already in play
334 if (isset($this->_ssID) && $permission == CRM_Core_Permission::EDIT) {
335 $this->_taskList += CRM_Contact_Task::optionalTaskTitle();
336 }
32795e82
FG
337 }
338
7a3978aa 339 return $this->_taskList;
32795e82 340 }
6a488035 341
32795e82
FG
342 /**
343 * Build the common elements between the search/advanced form.
6a488035
TO
344 *
345 * @return void
346 */
00be9182 347 public function buildQuickForm() {
3efb5b86 348 parent::buildQuickForm();
8e4ec1f5 349 CRM_Core_Resources::singleton()
8e4ec1f5
CW
350 // jsTree is needed for tags popup
351 ->addScriptFile('civicrm', 'packages/jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE)
352 ->addStyleFile('civicrm', 'packages/jquery/plugins/jstree/themes/default/style.css', 0, 'html-header');
6a488035
TO
353 $permission = CRM_Core_Permission::getPermission();
354 // some tasks.. what do we want to do with the selected contacts ?
34197a55 355 $tasks = array();
6a488035
TO
356 if ($this->_componentMode == 1 || $this->_componentMode == 7) {
357 $tasks += CRM_Contact_Task::permissionedTaskTitles($permission,
358 CRM_Utils_Array::value('deleted_contacts', $this->_formValues)
359 );
360 }
361 else {
362 $className = $this->_modeValue['taskClassName'];
ab8a593e 363 $tasks += $className::permissionedTaskTitles($permission, FALSE);
6a488035
TO
364 }
365
366 if (isset($this->_ssID)) {
367 if ($permission == CRM_Core_Permission::EDIT) {
368 $tasks = $tasks + CRM_Contact_Task::optionalTaskTitle();
369 }
370
79d7553f 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])) {
79d7553f 404 $this->_group[$this->_groupID]
405 = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_groupID, 'title');
6a488035
TO
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') {
353ffa53
TO
442 // check if _groupID exists, it might not if
443 // we are displaying a hidden group
6a488035
TO
444 if (!isset($this->_group[$this->_amtgID])) {
445 $this->assign('permissionedForGroup', FALSE);
79d7553f 446 $this->_group[$this->_amtgID]
447 = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_amtgID, 'title');
6a488035
TO
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 /**
fe482240 488 * Processing needed for buildForm and later.
6a488035
TO
489 *
490 * @return void
6a488035 491 */
00be9182 492 public function preProcess() {
6a488035
TO
493 // set the various class variables
494
495 $this->_group = CRM_Core_PseudoConstant::group();
496
497 $this->_groupIterator = CRM_Core_PseudoConstant::groupIterator();
498 $this->_tag = CRM_Core_BAO_Tag::getTags();
499 $this->_done = FALSE;
500
501 /*
deb50ba3
CW
502 * we allow the controller to set force/reset externally, useful when we are being
503 * driven by the wizard framework
504 */
6a488035
TO
505
506 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean',
507 CRM_Core_DAO::$_nullObject
508 );
509
510 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', CRM_Core_DAO::$_nullObject);
511 $this->_groupID = CRM_Utils_Request::retrieve('gid', 'Positive', $this);
512 $this->_amtgID = CRM_Utils_Request::retrieve('amtgID', 'Positive', $this);
513 $this->_ssID = CRM_Utils_Request::retrieve('ssID', 'Positive', $this);
514 $this->_sortByCharacter = CRM_Utils_Request::retrieve('sortByCharacter', 'String', $this);
515 $this->_ufGroupID = CRM_Utils_Request::retrieve('id', 'Positive', $this);
516 $this->_componentMode = CRM_Utils_Request::retrieve('component_mode', 'Positive', $this, FALSE, 1, $_REQUEST);
517 $this->_operator = CRM_Utils_Request::retrieve('operator', 'String', $this, FALSE, 1, $_REQUEST, 'AND');
518
519 /**
520 * set the button names
521 */
522 $this->_searchButtonName = $this->getButtonName('refresh');
6a488035
TO
523 $this->_actionButtonName = $this->getButtonName('next', 'action');
524
6a488035
TO
525 $this->assign('actionButtonName', $this->_actionButtonName);
526
527 // reset from session, CRM-3526
528 $session = CRM_Core_Session::singleton();
529 if ($this->_force && $session->get('selectedSearchContactIds')) {
530 $session->resetScope('selectedSearchContactIds');
531 }
532
533 // if we dont get this from the url, use default if one exsts
534 $config = CRM_Core_Config::singleton();
535 if ($this->_ufGroupID == NULL &&
536 $config->defaultSearchProfileID != NULL
537 ) {
538 $this->_ufGroupID = $config->defaultSearchProfileID;
539 }
540
541 // assign context to drive the template display, make sure context is valid
542 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
543 if (!CRM_Utils_Array::value($this->_context, self::validContext())) {
544 $this->_context = 'search';
545 }
546 $this->set('context', $this->_context);
547 $this->assign('context', $this->_context);
548
549 $this->_modeValue = self::getModeValue($this->_componentMode);
550 $this->assign($this->_modeValue);
551
552 $this->set('selectorName', self::$_selectorName);
553
554 // get user submitted values
555 // get it from controller only if form has been submitted, else preProcess has set this
4eeb9a5b
TO
556 // $this->controller->isModal( ) returns TRUE if page is
557 // valid, i.e all the validations are TRUE
6a488035
TO
558
559 if (!empty($_POST) && !$this->controller->isModal()) {
560 $this->_formValues = $this->controller->exportValues($this->_name);
561
562 $this->normalizeFormValues();
563 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
564 $this->_returnProperties = &$this->returnProperties();
565
566 // also get the uf group id directly from the post value
567 $this->_ufGroupID = CRM_Utils_Array::value('uf_group_id', $_POST, $this->_ufGroupID);
568 $this->_formValues['uf_group_id'] = $this->_ufGroupID;
569 $this->set('id', $this->_ufGroupID);
570
571 // also get the object mode directly from the post value
572 $this->_componentMode = CRM_Utils_Array::value('component_mode', $_POST, $this->_componentMode);
573
574 // also get the operator from the post value if set
575 $this->_operator = CRM_Utils_Array::value('operator', $_POST, $this->_operator);
576 $this->_formValues['operator'] = $this->_operator;
577 $this->set('operator', $this->_operator);
578 }
579 else {
580 $this->_formValues = $this->get('formValues');
581 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
582 $this->_returnProperties = &$this->returnProperties();
583 if (!empty($this->_ufGroupID)) {
584 $this->set('id', $this->_ufGroupID);
585 }
586 }
587
588 if (empty($this->_formValues)) {
589 //check if group is a smart group (fix for CRM-1255)
590 if ($this->_groupID) {
591 if ($ssId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_groupID, 'saved_search_id')) {
592 $this->_ssID = $ssId;
593 }
594 }
595
596 // fix for CRM-1907
597 if (isset($this->_ssID) && $this->_context != 'smog') {
598 // we only retrieve the saved search values if out current values are null
599 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
600
601 //fix for CRM-1505
602 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'mapping_id')) {
603 $this->_params = CRM_Contact_BAO_SavedSearch::getSearchParams($this->_ssID);
604 }
605 else {
606 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
607 }
608 $this->_returnProperties = &$this->returnProperties();
609 }
610 else {
611 if (isset($this->_ufGroupID)) {
612 // also set the uf group id if not already present
613 $this->_formValues['uf_group_id'] = $this->_ufGroupID;
614 }
615 if (isset($this->_componentMode)) {
616 $this->_formValues['component_mode'] = $this->_componentMode;
617 }
618 if (isset($this->_operator)) {
619 $this->_formValues['operator'] = $this->_operator;
620 }
621
622 // FIXME: we should generalise in a way that components could inject url-filters
623 // just like they build their own form elements
624 foreach (array(
353ffa53
TO
625 'mailing_id',
626 'mailing_delivery_status',
627 'mailing_open_status',
628 'mailing_click_status',
629 'mailing_reply_status',
630 'mailing_optout',
631 'mailing_forward',
632 'mailing_unsubscribe',
633 'mailing_date_low',
634 'mailing_date_high',
635 ) as $mailingFilter) {
6a488035
TO
636 $type = 'String';
637 if ($mailingFilter == 'mailing_id' &&
638 $filterVal = CRM_Utils_Request::retrieve('mailing_id', 'Positive', $this)
639 ) {
640 $this->_formValues[$mailingFilter] = array($filterVal);
641 }
642 elseif ($filterVal = CRM_Utils_Request::retrieve($mailingFilter, $type, $this)) {
643 $this->_formValues[$mailingFilter] = $filterVal;
644 }
645 if ($filterVal) {
646 $this->_openedPanes['Mailings'] = 1;
647 $this->_formValues['hidden_CiviMail'] = 1;
648 }
649 }
650 }
651 }
652 $this->assign('id',
653 CRM_Utils_Array::value('uf_group_id', $this->_formValues)
654 );
655 $operator = CRM_Utils_Array::value('operator', $this->_formValues, 'AND');
656 $this->set('queryOperator', $operator);
657 if ($operator == 'OR') {
658 $this->assign('operator', ts('OR'));
659 }
660 else {
661 $this->assign('operator', ts('AND'));
662 }
663
664 // show the context menu only when we’re not searching for deleted contacts; CRM-5673
a7488080 665 if (empty($this->_formValues['deleted_contacts'])) {
6a488035
TO
666 $menuItems = CRM_Contact_BAO_Contact::contextMenu();
667 $primaryActions = CRM_Utils_Array::value('primaryActions', $menuItems, array());
668 $this->_contextMenu = CRM_Utils_Array::value('moreActions', $menuItems, array());
669 $this->assign('contextMenu', $primaryActions + $this->_contextMenu);
670 }
671
672 if (!isset($this->_componentMode)) {
673 $this->_componentMode = CRM_Contact_BAO_Query::MODE_CONTACTS;
674 }
675 $modeValues = self::getModeValue($this->_componentMode);
676
677 self::$_selectorName = $this->_modeValue['selectorName'];
678
679 $setDynamic = FALSE;
680 if (strpos(self::$_selectorName, 'CRM_Contact_Selector') !== FALSE) {
681 $selector = new self::$_selectorName(
682 $this->_customSearchClass,
683 $this->_formValues,
684 $this->_params,
685 $this->_returnProperties,
686 $this->_action,
ab8a593e 687 FALSE, TRUE,
6a488035
TO
688 $this->_context,
689 $this->_contextMenu
690 );
691 $setDynamic = TRUE;
692 }
693 else {
694 $selector = new self::$_selectorName(
695 $this->_params,
696 $this->_action,
ab8a593e 697 NULL, FALSE, NULL,
6a488035
TO
698 "search", "advanced"
699 );
700 }
701
702 $selector->setKey($this->controller->_key);
703
704 $controller = new CRM_Contact_Selector_Controller($selector,
705 $this->get(CRM_Utils_Pager::PAGE_ID),
706 $this->get(CRM_Utils_Sort::SORT_ID),
707 CRM_Core_Action::VIEW,
708 $this,
709 CRM_Core_Selector_Controller::TRANSFER
710 );
711 $controller->setEmbedded(TRUE);
712 $controller->setDynamicAction($setDynamic);
713
714 if ($this->_force) {
715
716 $this->postProcess();
717
718 /*
deb50ba3
CW
719 * Note that we repeat this, since the search creates and stores
720 * values that potentially change the controller behavior. i.e. things
721 * like totalCount etc
722 */
6a488035
TO
723 $sortID = NULL;
724 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
725 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
726 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
727 );
728 }
729 $controller = new CRM_Contact_Selector_Controller($selector,
730 $this->get(CRM_Utils_Pager::PAGE_ID),
731 $sortID,
732 CRM_Core_Action::VIEW, $this, CRM_Core_Selector_Controller::TRANSFER
733 );
734 $controller->setEmbedded(TRUE);
735 $controller->setDynamicAction($setDynamic);
736 }
737
738 $controller->moveFromSessionToTemplate();
739 }
740
86538308
EM
741 /**
742 * @return array
743 */
00be9182 744 public function &getFormValues() {
6a488035
TO
745 return $this->_formValues;
746 }
747
748 /**
fe482240 749 * Common post processing.
6a488035
TO
750 *
751 * @return void
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}