Merge pull request #4882 from colemanw/CRM-15789
[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 the valid context set and the titles
170 * @static
171 */
172 public static function &validContext() {
173 if (!(self::$_validContext)) {
174 self::$_validContext = array(
175 'smog' => 'Show members of group',
176 'amtg' => 'Add members to group',
177 'basic' => 'Basic Search',
178 'search' => 'Search',
179 'builder' => 'Search Builder',
180 'advanced' => 'Advanced Search',
181 'custom' => 'Custom Search',
182 );
183 }
184 return self::$_validContext;
185 }
186
187 /**
188 * @param $context
189 *
190 * @return bool
191 */
192 public static function isSearchContext($context) {
193 $searchContext = CRM_Utils_Array::value($context, self::validContext());
194 return $searchContext ? TRUE : FALSE;
195 }
196
197 public static function setModeValues() {
198 if (!self::$_modeValues) {
199 self::$_modeValues = array(
200 1 => array(
201 'selectorName' => self::$_selectorName,
202 'selectorLabel' => ts('Contacts'),
203 'taskFile' => 'CRM/Contact/Form/Search/ResultTasks.tpl',
204 'taskContext' => NULL,
205 'resultFile' => 'CRM/Contact/Form/Selector.tpl',
206 'resultContext' => NULL,
207 'taskClassName' => 'CRM_Contact_Task',
208 ),
209 2 => array(
210 'selectorName' => 'CRM_Contribute_Selector_Search',
211 'selectorLabel' => ts('Contributions'),
212 'taskFile' => 'CRM/common/searchResultTasks.tpl',
213 'taskContext' => 'Contribution',
214 'resultFile' => 'CRM/Contribute/Form/Selector.tpl',
215 'resultContext' => 'Search',
216 'taskClassName' => 'CRM_Contribute_Task',
217 ),
218 3 => array(
219 'selectorName' => 'CRM_Event_Selector_Search',
220 'selectorLabel' => ts('Event Participants'),
221 'taskFile' => 'CRM/common/searchResultTasks.tpl',
222 'taskContext' => NULL,
223 'resultFile' => 'CRM/Event/Form/Selector.tpl',
224 'resultContext' => 'Search',
225 'taskClassName' => 'CRM_Event_Task',
226 ),
227 4 => array(
228 'selectorName' => 'CRM_Activity_Selector_Search',
229 'selectorLabel' => ts('Activities'),
230 'taskFile' => 'CRM/common/searchResultTasks.tpl',
231 'taskContext' => NULL,
232 'resultFile' => 'CRM/Activity/Form/Selector.tpl',
233 'resultContext' => 'Search',
234 'taskClassName' => 'CRM_Activity_Task',
235 ),
236 5 => array(
237 'selectorName' => 'CRM_Member_Selector_Search',
238 'selectorLabel' => ts('Memberships'),
239 'taskFile' => "CRM/common/searchResultTasks.tpl",
240 'taskContext' => NULL,
241 'resultFile' => 'CRM/Member/Form/Selector.tpl',
242 'resultContext' => 'Search',
243 'taskClassName' => 'CRM_Member_Task',
244 ),
245 6 => array(
246 'selectorName' => 'CRM_Case_Selector_Search',
247 'selectorLabel' => ts('Cases'),
248 'taskFile' => "CRM/common/searchResultTasks.tpl",
249 'taskContext' => NULL,
250 'resultFile' => 'CRM/Case/Form/Selector.tpl',
251 'resultContext' => 'Search',
252 'taskClassName' => 'CRM_Case_Task',
253 ),
254 7 => array(
255 'selectorName' => self::$_selectorName,
256 'selectorLabel' => ts('Related Contacts'),
257 'taskFile' => 'CRM/Contact/Form/Search/ResultTasks.tpl',
258 'taskContext' => NULL,
259 'resultFile' => 'CRM/Contact/Form/Selector.tpl',
260 'resultContext' => NULL,
261 'taskClassName' => 'CRM_Contact_Task',
262 ),
263 8 => array(
264 'selectorName' => 'CRM_Mailing_Selector_Search',
265 'selectorLabel' => ts('Mailings'),
266 'taskFile' => "CRM/common/searchResultTasks.tpl",
267 'taskContext' => NULL,
268 'resultFile' => 'CRM/Mailing/Form/Selector.tpl',
269 'resultContext' => 'Search',
270 'taskClassName' => 'CRM_Mailing_Task',
271 ),
272 );
273 }
274 }
275
276 /**
277 * @param int $mode
278 *
279 * @return mixed
280 */
281 public static function getModeValue($mode = 1) {
282 self::setModeValues();
283
284 if (!array_key_exists($mode, self::$_modeValues)) {
285 $mode = 1;
286 }
287
288 return self::$_modeValues[$mode];
289 }
290
291 /**
292 * @return array
293 */
294 public static function getModeSelect() {
295 self::setModeValues();
296
297 $select = array();
298 foreach (self::$_modeValues as $id => & $value) {
299 $select[$id] = $value['selectorLabel'];
300 }
301
302 // unset contributions or participants if user does not have
303 // permission on them
304 if (!CRM_Core_Permission::access('CiviContribute')) {
305 unset($select['2']);
306 }
307
308 if (!CRM_Core_Permission::access('CiviEvent')) {
309 unset($select['3']);
310 }
311
312 if (!CRM_Core_Permission::check('view all activities')) {
313 unset($select['4']);
314 }
315 return $select;
316 }
317
318 /**
319 * Build the common elements between the search/advanced form
320 *
321 *
322 * @return void
323 */
324 public function buildQuickForm() {
325 parent::buildQuickForm();
326 CRM_Core_Resources::singleton()
327 // jsTree is needed for tags popup
328 ->addScriptFile('civicrm', 'packages/jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE)
329 ->addStyleFile('civicrm', 'packages/jquery/plugins/jstree/themes/default/style.css', 0, 'html-header');
330 $permission = CRM_Core_Permission::getPermission();
331 // some tasks.. what do we want to do with the selected contacts ?
332 $tasks = array();
333 if ($this->_componentMode == 1 || $this->_componentMode == 7) {
334 $tasks += CRM_Contact_Task::permissionedTaskTitles($permission,
335 CRM_Utils_Array::value('deleted_contacts', $this->_formValues)
336 );
337 }
338 else {
339 $className = $this->_modeValue['taskClassName'];
340 $tasks += $className::permissionedTaskTitles($permission, FALSE);
341 }
342
343 if (isset($this->_ssID)) {
344 if ($permission == CRM_Core_Permission::EDIT) {
345 $tasks = $tasks + CRM_Contact_Task::optionalTaskTitle();
346 }
347
348 $search_custom_id =
349 CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'search_custom_id');
350
351 $savedSearchValues = array(
352 'id' => $this->_ssID,
353 'name' => CRM_Contact_BAO_SavedSearch::getName($this->_ssID, 'title'),
354 'search_custom_id' => $search_custom_id,
355 );
356 $this->assign_by_ref('savedSearch', $savedSearchValues);
357 $this->assign('ssID', $this->_ssID);
358 }
359
360 if ($this->_context === 'smog') {
361 // CRM-11788, we might want to do this for all of search where force=1
362 $formQFKey = CRM_Utils_Array::value('qfKey', $this->_formValues);
363 $getQFKey = CRM_Utils_Array::value('qfKey', $_GET);
364 $postQFKey = CRM_Utils_Array::value('qfKey', $_POST);
365 if ($formQFKey && empty($getQFKey) && empty($postQFKey)) {
366 $url = CRM_Utils_System::makeURL('qfKey') . $formQFKey;
367 CRM_Utils_System::redirect($url);
368 }
369 $permissionForGroup = FALSE;
370
371 if (!empty($this->_groupID)) {
372 // check if user has permission to edit members of this group
373 $permission = CRM_Contact_BAO_Group::checkPermission($this->_groupID);
374 if ($permission && in_array(CRM_Core_Permission::EDIT, $permission)) {
375 $permissionForGroup = TRUE;
376 }
377
378 // check if _groupID exists, it might not if
379 // we are displaying a hidden group
380 if (!isset($this->_group[$this->_groupID])) {
381 $this->_group[$this->_groupID] =
382 CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_groupID, 'title');
383 }
384
385 // set the group title
386 $groupValues = array('id' => $this->_groupID, 'title' => $this->_group[$this->_groupID]);
387 $this->assign_by_ref('group', $groupValues);
388
389 // also set ssID if this is a saved search
390 $ssID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_groupID, 'saved_search_id');
391 $this->assign('ssID', $ssID);
392
393 //get the saved search mapping id
394 if ($ssID) {
395 $this->_ssID = $ssID;
396 $ssMappingId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $ssID, 'mapping_id');
397 $this->assign('ssMappingID', $ssMappingId);
398 }
399
400 // Set dynamic page title for 'Show Members of Group'
401 CRM_Utils_System::setTitle(ts('Contacts in Group: %1', array(1 => $this->_group[$this->_groupID])));
402 }
403
404 $group_contact_status = array();
405 foreach (CRM_Core_SelectValues::groupContactStatus() as $k => $v) {
406 if (!empty($k)) {
407 $group_contact_status[] = $this->createElement('checkbox', $k, NULL, $v);
408 }
409 }
410 $this->addGroup($group_contact_status,
411 'group_contact_status', ts('Group Status')
412 );
413
414 $this->assign('permissionedForGroup', $permissionForGroup);
415 }
416
417 // add the go button for the action form, note it is of type 'next' rather than of type 'submit'
418 if ($this->_context === 'amtg') {
419 // check if _groupID exists, it might not if
420 // we are displaying a hidden group
421 if (!isset($this->_group[$this->_amtgID])) {
422 $this->assign('permissionedForGroup', FALSE);
423 $this->_group[$this->_amtgID] =
424 CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_amtgID, 'title');
425 }
426
427 // Set dynamic page title for 'Add Members Group'
428 CRM_Utils_System::setTitle(ts('Add to Group: %1', array(1 => $this->_group[$this->_amtgID])));
429 // also set the group title and freeze the action task with Add Members to Group
430 $groupValues = array('id' => $this->_amtgID, 'title' => $this->_group[$this->_amtgID]);
431 $this->assign_by_ref('group', $groupValues);
432 $this->add('submit', $this->_actionButtonName, ts('Add Contacts to %1', array(1 => $this->_group[$this->_amtgID])),
433 array(
434 'class' => 'crm-form-submit',
435 )
436 );
437 $this->add('hidden', 'task', CRM_Contact_Task::GROUP_CONTACTS);
438 $selectedRowsRadio = $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked'));
439 $allRowsRadio = $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all');
440 $this->assign('ts_sel_id', $selectedRowsRadio->_attributes['id']);
441 $this->assign('ts_all_id', $allRowsRadio->_attributes['id']);
442 }
443 else {
444 $this->addTaskMenu($tasks);
445 }
446
447 $selectedContactIds = array();
448 $qfKeyParam = CRM_Utils_Array::value('qfKey', $this->_formValues);
449 // We use ajax to handle selections only if the search results component_mode is set to "contacts"
450 if ($qfKeyParam && ($this->get('component_mode') <= 1 || $this->get('component_mode') == 7)) {
451 $this->addClass('crm-ajax-selection-form');
452 $qfKeyParam = "civicrm search {$qfKeyParam}";
453 $selectedContactIdsArr = CRM_Core_BAO_PrevNextCache::getSelection($qfKeyParam);
454 $selectedContactIds = array_keys($selectedContactIdsArr[$qfKeyParam]);
455 }
456
457 $this->assign_by_ref('selectedContactIds', $selectedContactIds);
458
459 $rows = $this->get('rows');
460
461 if (is_array($rows)) {
462 $this->addRowSelectors($rows);
463 }
464
465 }
466
467 /**
468 * Processing needed for buildForm and later
469 *
470 * @return void
471 */
472 public function preProcess() {
473 // set the various class variables
474
475 $this->_group = CRM_Core_PseudoConstant::group();
476
477 $this->_groupIterator = CRM_Core_PseudoConstant::groupIterator();
478 $this->_tag = CRM_Core_BAO_Tag::getTags();
479 $this->_done = FALSE;
480
481 /*
482 * we allow the controller to set force/reset externally, useful when we are being
483 * driven by the wizard framework
484 */
485
486 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean',
487 CRM_Core_DAO::$_nullObject
488 );
489
490 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', CRM_Core_DAO::$_nullObject);
491 $this->_groupID = CRM_Utils_Request::retrieve('gid', 'Positive', $this);
492 $this->_amtgID = CRM_Utils_Request::retrieve('amtgID', 'Positive', $this);
493 $this->_ssID = CRM_Utils_Request::retrieve('ssID', 'Positive', $this);
494 $this->_sortByCharacter = CRM_Utils_Request::retrieve('sortByCharacter', 'String', $this);
495 $this->_ufGroupID = CRM_Utils_Request::retrieve('id', 'Positive', $this);
496 $this->_componentMode = CRM_Utils_Request::retrieve('component_mode', 'Positive', $this, FALSE, 1, $_REQUEST);
497 $this->_operator = CRM_Utils_Request::retrieve('operator', 'String', $this, FALSE, 1, $_REQUEST, 'AND');
498
499 /**
500 * set the button names
501 */
502 $this->_searchButtonName = $this->getButtonName('refresh');
503 $this->_actionButtonName = $this->getButtonName('next', 'action');
504
505 $this->assign('actionButtonName', $this->_actionButtonName);
506
507 // reset from session, CRM-3526
508 $session = CRM_Core_Session::singleton();
509 if ($this->_force && $session->get('selectedSearchContactIds')) {
510 $session->resetScope('selectedSearchContactIds');
511 }
512
513 // if we dont get this from the url, use default if one exsts
514 $config = CRM_Core_Config::singleton();
515 if ($this->_ufGroupID == NULL &&
516 $config->defaultSearchProfileID != NULL
517 ) {
518 $this->_ufGroupID = $config->defaultSearchProfileID;
519 }
520
521 // assign context to drive the template display, make sure context is valid
522 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
523 if (!CRM_Utils_Array::value($this->_context, self::validContext())) {
524 $this->_context = 'search';
525 }
526 $this->set('context', $this->_context);
527 $this->assign('context', $this->_context);
528
529 $this->_modeValue = self::getModeValue($this->_componentMode);
530 $this->assign($this->_modeValue);
531
532 $this->set('selectorName', self::$_selectorName);
533
534 // get user submitted values
535 // get it from controller only if form has been submitted, else preProcess has set this
536 // $this->controller->isModal( ) returns TRUE if page is
537 // valid, i.e all the validations are TRUE
538
539 if (!empty($_POST) && !$this->controller->isModal()) {
540 $this->_formValues = $this->controller->exportValues($this->_name);
541
542 $this->normalizeFormValues();
543 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
544 $this->_returnProperties = &$this->returnProperties();
545
546 // also get the uf group id directly from the post value
547 $this->_ufGroupID = CRM_Utils_Array::value('uf_group_id', $_POST, $this->_ufGroupID);
548 $this->_formValues['uf_group_id'] = $this->_ufGroupID;
549 $this->set('id', $this->_ufGroupID);
550
551 // also get the object mode directly from the post value
552 $this->_componentMode = CRM_Utils_Array::value('component_mode', $_POST, $this->_componentMode);
553
554 // also get the operator from the post value if set
555 $this->_operator = CRM_Utils_Array::value('operator', $_POST, $this->_operator);
556 $this->_formValues['operator'] = $this->_operator;
557 $this->set('operator', $this->_operator);
558 }
559 else {
560 $this->_formValues = $this->get('formValues');
561 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
562 $this->_returnProperties = &$this->returnProperties();
563 if (!empty($this->_ufGroupID)) {
564 $this->set('id', $this->_ufGroupID);
565 }
566 }
567
568 if (empty($this->_formValues)) {
569 //check if group is a smart group (fix for CRM-1255)
570 if ($this->_groupID) {
571 if ($ssId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_groupID, 'saved_search_id')) {
572 $this->_ssID = $ssId;
573 }
574 }
575
576 // fix for CRM-1907
577 if (isset($this->_ssID) && $this->_context != 'smog') {
578 // we only retrieve the saved search values if out current values are null
579 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
580
581 //fix for CRM-1505
582 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'mapping_id')) {
583 $this->_params = CRM_Contact_BAO_SavedSearch::getSearchParams($this->_ssID);
584 }
585 else {
586 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
587 }
588 $this->_returnProperties = &$this->returnProperties();
589 }
590 else {
591 if (isset($this->_ufGroupID)) {
592 // also set the uf group id if not already present
593 $this->_formValues['uf_group_id'] = $this->_ufGroupID;
594 }
595 if (isset($this->_componentMode)) {
596 $this->_formValues['component_mode'] = $this->_componentMode;
597 }
598 if (isset($this->_operator)) {
599 $this->_formValues['operator'] = $this->_operator;
600 }
601
602 // FIXME: we should generalise in a way that components could inject url-filters
603 // just like they build their own form elements
604 foreach (array(
605 'mailing_id', 'mailing_delivery_status', 'mailing_open_status',
606 'mailing_click_status', 'mailing_reply_status', 'mailing_optout',
607 'mailing_forward', 'mailing_unsubscribe', 'mailing_date_low',
608 'mailing_date_high',
609 ) as $mailingFilter) {
610 $type = 'String';
611 if ($mailingFilter == 'mailing_id' &&
612 $filterVal = CRM_Utils_Request::retrieve('mailing_id', 'Positive', $this)
613 ) {
614 $this->_formValues[$mailingFilter] = array($filterVal);
615 }
616 elseif ($filterVal = CRM_Utils_Request::retrieve($mailingFilter, $type, $this)) {
617 $this->_formValues[$mailingFilter] = $filterVal;
618 }
619 if ($filterVal) {
620 $this->_openedPanes['Mailings'] = 1;
621 $this->_formValues['hidden_CiviMail'] = 1;
622 }
623 }
624 }
625 }
626 $this->assign('id',
627 CRM_Utils_Array::value('uf_group_id', $this->_formValues)
628 );
629 $operator = CRM_Utils_Array::value('operator', $this->_formValues, 'AND');
630 $this->set('queryOperator', $operator);
631 if ($operator == 'OR') {
632 $this->assign('operator', ts('OR'));
633 }
634 else {
635 $this->assign('operator', ts('AND'));
636 }
637
638 // show the context menu only when we’re not searching for deleted contacts; CRM-5673
639 if (empty($this->_formValues['deleted_contacts'])) {
640 $menuItems = CRM_Contact_BAO_Contact::contextMenu();
641 $primaryActions = CRM_Utils_Array::value('primaryActions', $menuItems, array());
642 $this->_contextMenu = CRM_Utils_Array::value('moreActions', $menuItems, array());
643 $this->assign('contextMenu', $primaryActions + $this->_contextMenu);
644 }
645
646 if (!isset($this->_componentMode)) {
647 $this->_componentMode = CRM_Contact_BAO_Query::MODE_CONTACTS;
648 }
649 $modeValues = self::getModeValue($this->_componentMode);
650
651 self::$_selectorName = $this->_modeValue['selectorName'];
652
653 $setDynamic = FALSE;
654 if (strpos(self::$_selectorName, 'CRM_Contact_Selector') !== FALSE) {
655 $selector = new self::$_selectorName(
656 $this->_customSearchClass,
657 $this->_formValues,
658 $this->_params,
659 $this->_returnProperties,
660 $this->_action,
661 FALSE, TRUE,
662 $this->_context,
663 $this->_contextMenu
664 );
665 $setDynamic = TRUE;
666 }
667 else {
668 $selector = new self::$_selectorName(
669 $this->_params,
670 $this->_action,
671 NULL, FALSE, NULL,
672 "search", "advanced"
673 );
674 }
675
676 $selector->setKey($this->controller->_key);
677
678 $controller = new CRM_Contact_Selector_Controller($selector,
679 $this->get(CRM_Utils_Pager::PAGE_ID),
680 $this->get(CRM_Utils_Sort::SORT_ID),
681 CRM_Core_Action::VIEW,
682 $this,
683 CRM_Core_Selector_Controller::TRANSFER
684 );
685 $controller->setEmbedded(TRUE);
686 $controller->setDynamicAction($setDynamic);
687
688 if ($this->_force) {
689
690 $this->postProcess();
691
692 /*
693 * Note that we repeat this, since the search creates and stores
694 * values that potentially change the controller behavior. i.e. things
695 * like totalCount etc
696 */
697 $sortID = NULL;
698 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
699 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
700 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
701 );
702 }
703 $controller = new CRM_Contact_Selector_Controller($selector,
704 $this->get(CRM_Utils_Pager::PAGE_ID),
705 $sortID,
706 CRM_Core_Action::VIEW, $this, CRM_Core_Selector_Controller::TRANSFER
707 );
708 $controller->setEmbedded(TRUE);
709 $controller->setDynamicAction($setDynamic);
710 }
711
712 $controller->moveFromSessionToTemplate();
713 }
714
715 /**
716 * @return array
717 */
718 public function &getFormValues() {
719 return $this->_formValues;
720 }
721
722 /**
723 * Common post processing
724 *
725 * @return void
726 */
727 public function postProcess() {
728 /*
729 * sometime we do a postProcess early on, so we dont need to repeat it
730 * this will most likely introduce some more bugs :(
731 */
732
733 if ($this->_done) {
734 return;
735 }
736 $this->_done = TRUE;
737
738 //for prev/next pagination
739 $crmPID = CRM_Utils_Request::retrieve('crmPID', 'Integer', CRM_Core_DAO::$_nullObject);
740
741 if (array_key_exists($this->_searchButtonName, $_POST) ||
742 ($this->_force && !$crmPID)) {
743 //reset the cache table for new search
744 $cacheKey = "civicrm search {$this->controller->_key}";
745 CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey);
746 }
747
748 //get the button name
749 $buttonName = $this->controller->getButtonName();
750
751 if (isset($this->_ufGroupID) && empty($this->_formValues['uf_group_id'])) {
752 $this->_formValues['uf_group_id'] = $this->_ufGroupID;
753 }
754
755 if (isset($this->_componentMode) && empty($this->_formValues['component_mode'])) {
756 $this->_formValues['component_mode'] = $this->_componentMode;
757 }
758
759 if (isset($this->_operator) && empty($this->_formValues['operator'])) {
760 $this->_formValues['operator'] = $this->_operator;
761 }
762
763 if (empty($this->_formValues['qfKey'])) {
764 $this->_formValues['qfKey'] = $this->controller->_key;
765 }
766
767 if (!CRM_Core_Permission::check('access deleted contacts')) {
768 unset($this->_formValues['deleted_contacts']);
769 }
770
771 $this->set('type', $this->_action);
772 $this->set('formValues', $this->_formValues);
773 $this->set('queryParams', $this->_params);
774 $this->set('returnProperties', $this->_returnProperties);
775
776 if ($buttonName == $this->_actionButtonName) {
777 // check actionName and if next, then do not repeat a search, since we are going to the next page
778 // hack, make sure we reset the task values
779 $stateMachine = $this->controller->getStateMachine();
780 $formName = $stateMachine->getTaskFormName();
781 $this->controller->resetPage($formName);
782 return;
783 }
784 else {
785 $output = CRM_Core_Selector_Controller::SESSION;
786
787 // create the selector, controller and run - store results in session
788 $searchChildGroups = TRUE;
789 if ($this->get('isAdvanced')) {
790 $searchChildGroups = FALSE;
791 }
792
793 $setDynamic = FALSE;
794
795 if (strpos(self::$_selectorName, 'CRM_Contact_Selector') !== FALSE) {
796 $selector = new self::$_selectorName (
797 $this->_customSearchClass,
798 $this->_formValues,
799 $this->_params,
800 $this->_returnProperties,
801 $this->_action,
802 FALSE,
803 $searchChildGroups,
804 $this->_context,
805 $this->_contextMenu
806 );
807 $setDynamic = TRUE;
808 }
809 else {
810 $selector = new self::$_selectorName(
811 $this->_params,
812 $this->_action,
813 NULL,
814 FALSE,
815 NULL,
816 "search",
817 "advanced"
818 );
819 }
820
821 $selector->setKey($this->controller->_key);
822
823 // added the sorting character to the form array
824 $config = CRM_Core_Config::singleton();
825 // do this only for contact search
826 if ($setDynamic && $config->includeAlphabeticalPager) {
827 // Don't recompute if we are just paging/sorting
828 if ($this->_reset || (empty($_GET['crmPID']) && empty($_GET['crmSID']) && !$this->_sortByCharacter)) {
829 $aToZBar = CRM_Utils_PagerAToZ::getAToZBar($selector, $this->_sortByCharacter);
830 $this->set('AToZBar', $aToZBar);
831 }
832 }
833
834 $sortID = NULL;
835 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
836 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
837 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
838 );
839 }
840 $controller = new CRM_Contact_Selector_Controller($selector,
841 $this->get(CRM_Utils_Pager::PAGE_ID),
842 $sortID,
843 CRM_Core_Action::VIEW,
844 $this,
845 $output
846 );
847 $controller->setEmbedded(TRUE);
848 $controller->setDynamicAction($setDynamic);
849 $controller->run();
850 }
851 }
852
853 /**
854 * @return NULL
855 */
856 public function &returnProperties() {
857 return CRM_Core_DAO::$_nullObject;
858 }
859
860 /**
861 * Return a descriptive name for the page, used in wizard header
862 *
863 * @return string
864 */
865 public function getTitle() {
866 return ts('Search');
867 }
868 }