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