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