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