INFRA-132 - Docblock formatting fixes
[civicrm-core.git] / CRM / Contact / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
31 * @copyright CiviCRM LLC (c) 2004-2014
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 */
44 class CRM_Contact_Form_Search extends CRM_Core_Form_Search {
45
46 /**
47 * list of valid contexts
48 *
49 * @var array
50 * @static
51 */
52 static $_validContext = NULL;
53
54 /**
55 * List of values used when we want to display other objects
56 *
57 * @var array
58 * @static
59 */
60 static $_modeValues = NULL;
61
62 /**
63 * The contextMenu
64 *
65 * @var array
66 */
67 protected $_contextMenu;
68
69 /**
70 * The groupId retrieved from the GET vars
71 *
72 * @var int
73 */
74 public $_groupID;
75
76 /**
77 * The Group ID belonging to Add Member to group ID
78 * retrieved from the GET vars
79 *
80 * @var int
81 */
82 protected $_amtgID;
83
84 /**
85 * The saved search ID retrieved from the GET vars
86 *
87 * @var int
88 */
89 protected $_ssID;
90
91 /**
92 * The group elements
93 *
94 * @var array
95 */
96 public $_group;
97 public $_groupElement;
98 public $_groupIterator;
99
100 /**
101 * The tag elements
102 *
103 * @var array
104 */
105 public $_tag;
106 public $_tagElement;
107
108 /**
109 * The params used for search
110 *
111 * @var array
112 */
113 protected $_params;
114
115 /**
116 * The return properties used for search
117 *
118 * @var array
119 */
120 protected $_returnProperties;
121
122 /**
123 * The sort by character
124 *
125 * @var string
126 */
127 protected $_sortByCharacter;
128
129 /**
130 * The profile group id used for display
131 *
132 * @var integer
133 */
134 protected $_ufGroupID;
135
136 /**
137 * Csv - common search values
138 *
139 * @var array
140 * @static
141 */
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
157 /**
158 * Name of the selector to use
159 */
160 static $_selectorName = 'CRM_Contact_Selector';
161 protected $_customSearchID = NULL;
162 protected $_customSearchClass = NULL;
163
164 protected $_openedPanes = array();
165
166 /**
167 * Define the set of valid contexts that the search form operates on
168 *
169 * @return array
170 * the valid context set and the titles
171 * @static
172 */
173 public static function &validContext() {
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
188 /**
189 * @param $context
190 *
191 * @return bool
192 */
193 public static function isSearchContext($context) {
194 $searchContext = CRM_Utils_Array::value($context, self::validContext());
195 return $searchContext ? TRUE : FALSE;
196 }
197
198 public static function setModeValues() {
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 ),
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 ),
273 );
274 }
275 }
276
277 /**
278 * @param int $mode
279 *
280 * @return mixed
281 */
282 public static function getModeValue($mode = 1) {
283 self::setModeValues();
284
285 if (!array_key_exists($mode, self::$_modeValues)) {
286 $mode = 1;
287 }
288
289 return self::$_modeValues[$mode];
290 }
291
292 /**
293 * @return array
294 */
295 public static function getModeSelect() {
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 *
322 *
323 * @return void
324 */
325 public function buildQuickForm() {
326 parent::buildQuickForm();
327 CRM_Core_Resources::singleton()
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');
331 $permission = CRM_Core_Permission::getPermission();
332 // some tasks.. what do we want to do with the selected contacts ?
333 $tasks = array();
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'];
341 $tasks += $className::permissionedTaskTitles($permission, FALSE);
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 }
370 $permissionForGroup = FALSE;
371
372 if (!empty($this->_groupID)) {
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])) {
382 $this->_group[$this->_groupID] =
383 CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_groupID, 'title');
384 }
385
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
415 $this->assign('permissionedForGroup', $permissionForGroup);
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(
435 'class' => 'crm-form-submit',
436 )
437 );
438 $this->add('hidden', 'task', CRM_Contact_Task::GROUP_CONTACTS);
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']);
443 }
444 else {
445 $this->addTaskMenu($tasks);
446 }
447
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"
451 if ($qfKeyParam && ($this->get('component_mode') <= 1 || $this->get('component_mode') == 7)) {
452 $this->addClass('crm-ajax-selection-form');
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
460 $rows = $this->get('rows');
461
462 if (is_array($rows)) {
463 $this->addRowSelectors($rows);
464 }
465
466 }
467
468 /**
469 * Processing needed for buildForm and later
470 *
471 * @return void
472 */
473 public function preProcess() {
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 /*
483 * we allow the controller to set force/reset externally, useful when we are being
484 * driven by the wizard framework
485 */
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');
504 $this->_actionButtonName = $this->getButtonName('next', 'action');
505
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
537 // $this->controller->isModal( ) returns TRUE if page is
538 // valid, i.e all the validations are TRUE
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',
607 'mailing_delivery_status',
608 'mailing_open_status',
609 'mailing_click_status',
610 'mailing_reply_status',
611 'mailing_optout',
612 'mailing_forward',
613 'mailing_unsubscribe',
614 'mailing_date_low',
615 'mailing_date_high',
616 ) as $mailingFilter) {
617 $type = 'String';
618 if ($mailingFilter == 'mailing_id' &&
619 $filterVal = CRM_Utils_Request::retrieve('mailing_id', 'Positive', $this)
620 ) {
621 $this->_formValues[$mailingFilter] = array($filterVal);
622 }
623 elseif ($filterVal = CRM_Utils_Request::retrieve($mailingFilter, $type, $this)) {
624 $this->_formValues[$mailingFilter] = $filterVal;
625 }
626 if ($filterVal) {
627 $this->_openedPanes['Mailings'] = 1;
628 $this->_formValues['hidden_CiviMail'] = 1;
629 }
630 }
631 }
632 }
633 $this->assign('id',
634 CRM_Utils_Array::value('uf_group_id', $this->_formValues)
635 );
636 $operator = CRM_Utils_Array::value('operator', $this->_formValues, 'AND');
637 $this->set('queryOperator', $operator);
638 if ($operator == 'OR') {
639 $this->assign('operator', ts('OR'));
640 }
641 else {
642 $this->assign('operator', ts('AND'));
643 }
644
645 // show the context menu only when we’re not searching for deleted contacts; CRM-5673
646 if (empty($this->_formValues['deleted_contacts'])) {
647 $menuItems = CRM_Contact_BAO_Contact::contextMenu();
648 $primaryActions = CRM_Utils_Array::value('primaryActions', $menuItems, array());
649 $this->_contextMenu = CRM_Utils_Array::value('moreActions', $menuItems, array());
650 $this->assign('contextMenu', $primaryActions + $this->_contextMenu);
651 }
652
653 if (!isset($this->_componentMode)) {
654 $this->_componentMode = CRM_Contact_BAO_Query::MODE_CONTACTS;
655 }
656 $modeValues = self::getModeValue($this->_componentMode);
657
658 self::$_selectorName = $this->_modeValue['selectorName'];
659
660 $setDynamic = FALSE;
661 if (strpos(self::$_selectorName, 'CRM_Contact_Selector') !== FALSE) {
662 $selector = new self::$_selectorName(
663 $this->_customSearchClass,
664 $this->_formValues,
665 $this->_params,
666 $this->_returnProperties,
667 $this->_action,
668 FALSE, TRUE,
669 $this->_context,
670 $this->_contextMenu
671 );
672 $setDynamic = TRUE;
673 }
674 else {
675 $selector = new self::$_selectorName(
676 $this->_params,
677 $this->_action,
678 NULL, FALSE, NULL,
679 "search", "advanced"
680 );
681 }
682
683 $selector->setKey($this->controller->_key);
684
685 $controller = new CRM_Contact_Selector_Controller($selector,
686 $this->get(CRM_Utils_Pager::PAGE_ID),
687 $this->get(CRM_Utils_Sort::SORT_ID),
688 CRM_Core_Action::VIEW,
689 $this,
690 CRM_Core_Selector_Controller::TRANSFER
691 );
692 $controller->setEmbedded(TRUE);
693 $controller->setDynamicAction($setDynamic);
694
695 if ($this->_force) {
696
697 $this->postProcess();
698
699 /*
700 * Note that we repeat this, since the search creates and stores
701 * values that potentially change the controller behavior. i.e. things
702 * like totalCount etc
703 */
704 $sortID = NULL;
705 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
706 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
707 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
708 );
709 }
710 $controller = new CRM_Contact_Selector_Controller($selector,
711 $this->get(CRM_Utils_Pager::PAGE_ID),
712 $sortID,
713 CRM_Core_Action::VIEW, $this, CRM_Core_Selector_Controller::TRANSFER
714 );
715 $controller->setEmbedded(TRUE);
716 $controller->setDynamicAction($setDynamic);
717 }
718
719 $controller->moveFromSessionToTemplate();
720 }
721
722 /**
723 * @return array
724 */
725 public function &getFormValues() {
726 return $this->_formValues;
727 }
728
729 /**
730 * Common post processing
731 *
732 * @return void
733 */
734 public function postProcess() {
735 /*
736 * sometime we do a postProcess early on, so we dont need to repeat it
737 * this will most likely introduce some more bugs :(
738 */
739
740 if ($this->_done) {
741 return;
742 }
743 $this->_done = TRUE;
744
745 //for prev/next pagination
746 $crmPID = CRM_Utils_Request::retrieve('crmPID', 'Integer', CRM_Core_DAO::$_nullObject);
747
748 if (array_key_exists($this->_searchButtonName, $_POST) ||
749 ($this->_force && !$crmPID)
750 ) {
751 //reset the cache table for new search
752 $cacheKey = "civicrm search {$this->controller->_key}";
753 CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey);
754 }
755
756 //get the button name
757 $buttonName = $this->controller->getButtonName();
758
759 if (isset($this->_ufGroupID) && empty($this->_formValues['uf_group_id'])) {
760 $this->_formValues['uf_group_id'] = $this->_ufGroupID;
761 }
762
763 if (isset($this->_componentMode) && empty($this->_formValues['component_mode'])) {
764 $this->_formValues['component_mode'] = $this->_componentMode;
765 }
766
767 if (isset($this->_operator) && empty($this->_formValues['operator'])) {
768 $this->_formValues['operator'] = $this->_operator;
769 }
770
771 if (empty($this->_formValues['qfKey'])) {
772 $this->_formValues['qfKey'] = $this->controller->_key;
773 }
774
775 if (!CRM_Core_Permission::check('access deleted contacts')) {
776 unset($this->_formValues['deleted_contacts']);
777 }
778
779 $this->set('type', $this->_action);
780 $this->set('formValues', $this->_formValues);
781 $this->set('queryParams', $this->_params);
782 $this->set('returnProperties', $this->_returnProperties);
783
784 if ($buttonName == $this->_actionButtonName) {
785 // check actionName and if next, then do not repeat a search, since we are going to the next page
786 // hack, make sure we reset the task values
787 $stateMachine = $this->controller->getStateMachine();
788 $formName = $stateMachine->getTaskFormName();
789 $this->controller->resetPage($formName);
790 return;
791 }
792 else {
793 $output = CRM_Core_Selector_Controller::SESSION;
794
795 // create the selector, controller and run - store results in session
796 $searchChildGroups = TRUE;
797 if ($this->get('isAdvanced')) {
798 $searchChildGroups = FALSE;
799 }
800
801 $setDynamic = FALSE;
802
803 if (strpos(self::$_selectorName, 'CRM_Contact_Selector') !== FALSE) {
804 $selector = new self::$_selectorName (
805 $this->_customSearchClass,
806 $this->_formValues,
807 $this->_params,
808 $this->_returnProperties,
809 $this->_action,
810 FALSE,
811 $searchChildGroups,
812 $this->_context,
813 $this->_contextMenu
814 );
815 $setDynamic = TRUE;
816 }
817 else {
818 $selector = new self::$_selectorName(
819 $this->_params,
820 $this->_action,
821 NULL,
822 FALSE,
823 NULL,
824 "search",
825 "advanced"
826 );
827 }
828
829 $selector->setKey($this->controller->_key);
830
831 // added the sorting character to the form array
832 $config = CRM_Core_Config::singleton();
833 // do this only for contact search
834 if ($setDynamic && $config->includeAlphabeticalPager) {
835 // Don't recompute if we are just paging/sorting
836 if ($this->_reset || (empty($_GET['crmPID']) && empty($_GET['crmSID']) && !$this->_sortByCharacter)) {
837 $aToZBar = CRM_Utils_PagerAToZ::getAToZBar($selector, $this->_sortByCharacter);
838 $this->set('AToZBar', $aToZBar);
839 }
840 }
841
842 $sortID = NULL;
843 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
844 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
845 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
846 );
847 }
848 $controller = new CRM_Contact_Selector_Controller($selector,
849 $this->get(CRM_Utils_Pager::PAGE_ID),
850 $sortID,
851 CRM_Core_Action::VIEW,
852 $this,
853 $output
854 );
855 $controller->setEmbedded(TRUE);
856 $controller->setDynamicAction($setDynamic);
857 $controller->run();
858 }
859 }
860
861 /**
862 * @return NULL
863 */
864 public function &returnProperties() {
865 return CRM_Core_DAO::$_nullObject;
866 }
867
868 /**
869 * Return a descriptive name for the page, used in wizard header
870 *
871 * @return string
872 */
873 public function getTitle() {
874 return ts('Search');
875 }
876 }