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