Merge pull request #8042 from jitendrapurohit/CRM-18250
[civicrm-core.git] / CRM / Contact / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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 * Declare entity reference fields as they will need to be converted to using 'IN'.
150 *
151 * @var array
152 */
153 protected $entityReferenceFields = array('event_id', 'membership_type_id');
154
155 /**
156 * Name of the selector to use.
157 */
158 static $_selectorName = 'CRM_Contact_Selector';
159 protected $_customSearchID = NULL;
160 protected $_customSearchClass = NULL;
161
162 protected $_openedPanes = array();
163
164 /**
165 * Explicitly declare the entity api name.
166 */
167 public function getDefaultEntity() {
168 return 'Contact';
169 }
170
171 /**
172 * Define the set of valid contexts that the search form operates on.
173 *
174 * @return array
175 * the valid context set and the titles
176 */
177 public static function &validContext() {
178 if (!(self::$_validContext)) {
179 self::$_validContext = array(
180 'smog' => 'Show members of group',
181 'amtg' => 'Add members to group',
182 'basic' => 'Basic Search',
183 'search' => 'Search',
184 'builder' => 'Search Builder',
185 'advanced' => 'Advanced Search',
186 'custom' => 'Custom Search',
187 );
188 }
189 return self::$_validContext;
190 }
191
192 /**
193 * @param $context
194 *
195 * @return bool
196 */
197 public static function isSearchContext($context) {
198 $searchContext = CRM_Utils_Array::value($context, self::validContext());
199 return $searchContext ? TRUE : FALSE;
200 }
201
202 public static function setModeValues() {
203 if (!self::$_modeValues) {
204 self::$_modeValues = array(
205 1 => array(
206 'selectorName' => self::$_selectorName,
207 'selectorLabel' => ts('Contacts'),
208 'taskFile' => 'CRM/Contact/Form/Search/ResultTasks.tpl',
209 'taskContext' => NULL,
210 'resultFile' => 'CRM/Contact/Form/Selector.tpl',
211 'resultContext' => NULL,
212 'taskClassName' => 'CRM_Contact_Task',
213 ),
214 2 => array(
215 'selectorName' => 'CRM_Contribute_Selector_Search',
216 'selectorLabel' => ts('Contributions'),
217 'taskFile' => 'CRM/common/searchResultTasks.tpl',
218 'taskContext' => 'Contribution',
219 'resultFile' => 'CRM/Contribute/Form/Selector.tpl',
220 'resultContext' => 'Search',
221 'taskClassName' => 'CRM_Contribute_Task',
222 ),
223 3 => array(
224 'selectorName' => 'CRM_Event_Selector_Search',
225 'selectorLabel' => ts('Event Participants'),
226 'taskFile' => 'CRM/common/searchResultTasks.tpl',
227 'taskContext' => NULL,
228 'resultFile' => 'CRM/Event/Form/Selector.tpl',
229 'resultContext' => 'Search',
230 'taskClassName' => 'CRM_Event_Task',
231 ),
232 4 => array(
233 'selectorName' => 'CRM_Activity_Selector_Search',
234 'selectorLabel' => ts('Activities'),
235 'taskFile' => 'CRM/common/searchResultTasks.tpl',
236 'taskContext' => NULL,
237 'resultFile' => 'CRM/Activity/Form/Selector.tpl',
238 'resultContext' => 'Search',
239 'taskClassName' => 'CRM_Activity_Task',
240 ),
241 5 => array(
242 'selectorName' => 'CRM_Member_Selector_Search',
243 'selectorLabel' => ts('Memberships'),
244 'taskFile' => "CRM/common/searchResultTasks.tpl",
245 'taskContext' => NULL,
246 'resultFile' => 'CRM/Member/Form/Selector.tpl',
247 'resultContext' => 'Search',
248 'taskClassName' => 'CRM_Member_Task',
249 ),
250 6 => array(
251 'selectorName' => 'CRM_Case_Selector_Search',
252 'selectorLabel' => ts('Cases'),
253 'taskFile' => "CRM/common/searchResultTasks.tpl",
254 'taskContext' => NULL,
255 'resultFile' => 'CRM/Case/Form/Selector.tpl',
256 'resultContext' => 'Search',
257 'taskClassName' => 'CRM_Case_Task',
258 ),
259 7 => array(
260 'selectorName' => self::$_selectorName,
261 'selectorLabel' => ts('Related Contacts'),
262 'taskFile' => 'CRM/Contact/Form/Search/ResultTasks.tpl',
263 'taskContext' => NULL,
264 'resultFile' => 'CRM/Contact/Form/Selector.tpl',
265 'resultContext' => NULL,
266 'taskClassName' => 'CRM_Contact_Task',
267 ),
268 8 => array(
269 'selectorName' => 'CRM_Mailing_Selector_Search',
270 'selectorLabel' => ts('Mailings'),
271 'taskFile' => "CRM/common/searchResultTasks.tpl",
272 'taskContext' => NULL,
273 'resultFile' => 'CRM/Mailing/Form/Selector.tpl',
274 'resultContext' => 'Search',
275 'taskClassName' => 'CRM_Mailing_Task',
276 ),
277 );
278 }
279 }
280
281 /**
282 * @param int $mode
283 *
284 * @return mixed
285 */
286 public static function getModeValue($mode = 1) {
287 self::setModeValues();
288
289 if (!array_key_exists($mode, self::$_modeValues)) {
290 $mode = 1;
291 }
292
293 return self::$_modeValues[$mode];
294 }
295
296 /**
297 * @return array
298 */
299 public static function getModeSelect() {
300 self::setModeValues();
301
302 $select = array();
303 foreach (self::$_modeValues as $id => & $value) {
304 $select[$id] = $value['selectorLabel'];
305 }
306
307 // unset contributions or participants if user does not have
308 // permission on them
309 if (!CRM_Core_Permission::access('CiviContribute')) {
310 unset($select['2']);
311 }
312
313 if (!CRM_Core_Permission::access('CiviEvent')) {
314 unset($select['3']);
315 }
316
317 if (!CRM_Core_Permission::check('view all activities')) {
318 unset($select['4']);
319 }
320 return $select;
321 }
322
323 /**
324 * Builds the list of tasks or actions that a searcher can perform on a result set.
325 *
326 * @return array
327 */
328 public function buildTaskList() {
329 if ($this->_context !== 'amtg') {
330 $permission = CRM_Core_Permission::getPermission();
331
332 if ($this->_componentMode == 1 || $this->_componentMode == 7) {
333 $this->_taskList += CRM_Contact_Task::permissionedTaskTitles($permission,
334 CRM_Utils_Array::value('deleted_contacts', $this->_formValues)
335 );
336 }
337 else {
338 $className = $this->_modeValue['taskClassName'];
339 $this->_taskList += $className::permissionedTaskTitles($permission, FALSE);
340 }
341
342 // Only offer the "Update Smart Group" task if a smart group/saved search is already in play
343 if (isset($this->_ssID) && $permission == CRM_Core_Permission::EDIT) {
344 $this->_taskList += CRM_Contact_Task::optionalTaskTitle();
345 }
346 }
347
348 asort($this->_taskList);
349 return $this->_taskList;
350 }
351
352 /**
353 * Build the common elements between the search/advanced form.
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 public function preProcess() {
499 // set the various class variables
500
501 $this->_group = CRM_Core_PseudoConstant::group();
502
503 $this->_groupIterator = CRM_Core_PseudoConstant::groupIterator();
504 $this->_tag = CRM_Core_BAO_Tag::getTags();
505 $this->_done = FALSE;
506
507 /*
508 * we allow the controller to set force/reset externally, useful when we are being
509 * driven by the wizard framework
510 */
511
512 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean',
513 CRM_Core_DAO::$_nullObject
514 );
515
516 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', CRM_Core_DAO::$_nullObject);
517 $this->_groupID = CRM_Utils_Request::retrieve('gid', 'Positive', $this);
518 $this->_amtgID = CRM_Utils_Request::retrieve('amtgID', 'Positive', $this);
519 $this->_ssID = CRM_Utils_Request::retrieve('ssID', 'Positive', $this);
520 $this->_sortByCharacter = CRM_Utils_Request::retrieve('sortByCharacter', 'String', $this);
521 $this->_ufGroupID = CRM_Utils_Request::retrieve('id', 'Positive', $this);
522 $this->_componentMode = CRM_Utils_Request::retrieve('component_mode', 'Positive', $this, FALSE, 1, $_REQUEST);
523 $this->_operator = CRM_Utils_Request::retrieve('operator', 'String', $this, FALSE, 1, $_REQUEST, 'AND');
524
525 /**
526 * set the button names
527 */
528 $this->_searchButtonName = $this->getButtonName('refresh');
529 $this->_actionButtonName = $this->getButtonName('next', 'action');
530
531 $this->assign('actionButtonName', $this->_actionButtonName);
532
533 // reset from session, CRM-3526
534 $session = CRM_Core_Session::singleton();
535 if ($this->_force && $session->get('selectedSearchContactIds')) {
536 $session->resetScope('selectedSearchContactIds');
537 }
538
539 // if we dont get this from the url, use default if one exsts
540 $config = CRM_Core_Config::singleton();
541 if ($this->_ufGroupID == NULL &&
542 $config->defaultSearchProfileID != NULL
543 ) {
544 $this->_ufGroupID = $config->defaultSearchProfileID;
545 }
546
547 // assign context to drive the template display, make sure context is valid
548 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
549 if (!CRM_Utils_Array::value($this->_context, self::validContext())) {
550 $this->_context = 'search';
551 }
552 $this->set('context', $this->_context);
553 $this->assign('context', $this->_context);
554
555 $this->_modeValue = self::getModeValue($this->_componentMode);
556 $this->assign($this->_modeValue);
557
558 $this->set('selectorName', self::$_selectorName);
559
560 // get user submitted values
561 // get it from controller only if form has been submitted, else preProcess has set this
562 // $this->controller->isModal( ) returns TRUE if page is
563 // valid, i.e all the validations are TRUE
564
565 if (!empty($_POST) && !$this->controller->isModal()) {
566 $this->_formValues = $this->controller->exportValues($this->_name);
567
568 $this->normalizeFormValues();
569 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
570 $this->_returnProperties = &$this->returnProperties();
571
572 // also get the uf group id directly from the post value
573 $this->_ufGroupID = CRM_Utils_Array::value('uf_group_id', $_POST, $this->_ufGroupID);
574 $this->_formValues['uf_group_id'] = $this->_ufGroupID;
575 $this->set('id', $this->_ufGroupID);
576
577 // also get the object mode directly from the post value
578 $this->_componentMode = CRM_Utils_Array::value('component_mode', $_POST, $this->_componentMode);
579
580 // also get the operator from the post value if set
581 $this->_operator = CRM_Utils_Array::value('operator', $_POST, $this->_operator);
582 $this->_formValues['operator'] = $this->_operator;
583 $this->set('operator', $this->_operator);
584 }
585 else {
586 $this->_formValues = $this->get('formValues');
587 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
588 $this->_returnProperties = &$this->returnProperties();
589 if (!empty($this->_ufGroupID)) {
590 $this->set('id', $this->_ufGroupID);
591 }
592 }
593
594 if (empty($this->_formValues)) {
595 //check if group is a smart group (fix for CRM-1255)
596 if ($this->_groupID) {
597 if ($ssId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_groupID, 'saved_search_id')) {
598 $this->_ssID = $ssId;
599 }
600 }
601
602 // fix for CRM-1907
603 if (isset($this->_ssID) && $this->_context != 'smog') {
604 // we only retrieve the saved search values if out current values are null
605 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
606
607 //fix for CRM-1505
608 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'mapping_id')) {
609 $this->_params = CRM_Contact_BAO_SavedSearch::getSearchParams($this->_ssID);
610 }
611 else {
612 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
613 }
614 $this->_returnProperties = &$this->returnProperties();
615 }
616 else {
617 if (isset($this->_ufGroupID)) {
618 // also set the uf group id if not already present
619 $this->_formValues['uf_group_id'] = $this->_ufGroupID;
620 }
621 if (isset($this->_componentMode)) {
622 $this->_formValues['component_mode'] = $this->_componentMode;
623 }
624 if (isset($this->_operator)) {
625 $this->_formValues['operator'] = $this->_operator;
626 }
627
628 // FIXME: we should generalise in a way that components could inject url-filters
629 // just like they build their own form elements
630 foreach (array(
631 'mailing_id',
632 'mailing_delivery_status',
633 'mailing_open_status',
634 'mailing_click_status',
635 'mailing_reply_status',
636 'mailing_optout',
637 'mailing_forward',
638 'mailing_unsubscribe',
639 'mailing_date_low',
640 'mailing_date_high',
641 ) as $mailingFilter) {
642 $type = 'String';
643 if ($mailingFilter == 'mailing_id' &&
644 $filterVal = CRM_Utils_Request::retrieve('mailing_id', 'Positive', $this)
645 ) {
646 $this->_formValues[$mailingFilter] = array($filterVal);
647 }
648 elseif ($filterVal = CRM_Utils_Request::retrieve($mailingFilter, $type, $this)) {
649 $this->_formValues[$mailingFilter] = $filterVal;
650 }
651 if ($filterVal) {
652 $this->_openedPanes['Mailings'] = 1;
653 $this->_formValues['hidden_CiviMail'] = 1;
654 }
655 }
656 }
657 }
658 $this->assign('id',
659 CRM_Utils_Array::value('uf_group_id', $this->_formValues)
660 );
661 $operator = CRM_Utils_Array::value('operator', $this->_formValues, 'AND');
662 $this->set('queryOperator', $operator);
663 if ($operator == 'OR') {
664 $this->assign('operator', ts('OR'));
665 }
666 else {
667 $this->assign('operator', ts('AND'));
668 }
669
670 // show the context menu only when we’re not searching for deleted contacts; CRM-5673
671 if (empty($this->_formValues['deleted_contacts'])) {
672 $menuItems = CRM_Contact_BAO_Contact::contextMenu();
673 $primaryActions = CRM_Utils_Array::value('primaryActions', $menuItems, array());
674 $this->_contextMenu = CRM_Utils_Array::value('moreActions', $menuItems, array());
675 $this->assign('contextMenu', $primaryActions + $this->_contextMenu);
676 }
677
678 if (!isset($this->_componentMode)) {
679 $this->_componentMode = CRM_Contact_BAO_Query::MODE_CONTACTS;
680 }
681 self::setModeValues();
682
683 self::$_selectorName = $this->_modeValue['selectorName'];
684
685 $setDynamic = FALSE;
686 if (strpos(self::$_selectorName, 'CRM_Contact_Selector') !== FALSE) {
687 $selector = new self::$_selectorName(
688 $this->_customSearchClass,
689 $this->_formValues,
690 $this->_params,
691 $this->_returnProperties,
692 $this->_action,
693 FALSE, TRUE,
694 $this->_context,
695 $this->_contextMenu
696 );
697 $setDynamic = TRUE;
698 }
699 else {
700 $selector = new self::$_selectorName(
701 $this->_params,
702 $this->_action,
703 NULL, FALSE, NULL,
704 "search", "advanced"
705 );
706 }
707
708 $selector->setKey($this->controller->_key);
709
710 $controller = new CRM_Contact_Selector_Controller($selector,
711 $this->get(CRM_Utils_Pager::PAGE_ID),
712 $this->get(CRM_Utils_Sort::SORT_ID),
713 CRM_Core_Action::VIEW,
714 $this,
715 CRM_Core_Selector_Controller::TRANSFER
716 );
717 $controller->setEmbedded(TRUE);
718 $controller->setDynamicAction($setDynamic);
719
720 if ($this->_force) {
721
722 $this->postProcess();
723
724 /*
725 * Note that we repeat this, since the search creates and stores
726 * values that potentially change the controller behavior. i.e. things
727 * like totalCount etc
728 */
729 $sortID = NULL;
730 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
731 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
732 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
733 );
734 }
735 $controller = new CRM_Contact_Selector_Controller($selector,
736 $this->get(CRM_Utils_Pager::PAGE_ID),
737 $sortID,
738 CRM_Core_Action::VIEW, $this, CRM_Core_Selector_Controller::TRANSFER
739 );
740 $controller->setEmbedded(TRUE);
741 $controller->setDynamicAction($setDynamic);
742 }
743
744 $controller->moveFromSessionToTemplate();
745 }
746
747 /**
748 * @return array
749 */
750 public function &getFormValues() {
751 return $this->_formValues;
752 }
753
754 /**
755 * Common post processing.
756 */
757 public function postProcess() {
758 /*
759 * sometime we do a postProcess early on, so we dont need to repeat it
760 * this will most likely introduce some more bugs :(
761 */
762
763 if ($this->_done) {
764 return;
765 }
766 $this->_done = TRUE;
767
768 //for prev/next pagination
769 $crmPID = CRM_Utils_Request::retrieve('crmPID', 'Integer', CRM_Core_DAO::$_nullObject);
770
771 if (array_key_exists($this->_searchButtonName, $_POST) ||
772 ($this->_force && !$crmPID)
773 ) {
774 //reset the cache table for new search
775 $cacheKey = "civicrm search {$this->controller->_key}";
776 CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey);
777 }
778
779 //get the button name
780 $buttonName = $this->controller->getButtonName();
781
782 if (isset($this->_ufGroupID) && empty($this->_formValues['uf_group_id'])) {
783 $this->_formValues['uf_group_id'] = $this->_ufGroupID;
784 }
785
786 if (isset($this->_componentMode) && empty($this->_formValues['component_mode'])) {
787 $this->_formValues['component_mode'] = $this->_componentMode;
788 }
789
790 if (isset($this->_operator) && empty($this->_formValues['operator'])) {
791 $this->_formValues['operator'] = $this->_operator;
792 }
793
794 if (empty($this->_formValues['qfKey'])) {
795 $this->_formValues['qfKey'] = $this->controller->_key;
796 }
797
798 if (!CRM_Core_Permission::check('access deleted contacts')) {
799 unset($this->_formValues['deleted_contacts']);
800 }
801
802 $this->set('type', $this->_action);
803 $this->set('formValues', $this->_formValues);
804 $this->set('queryParams', $this->_params);
805 $this->set('returnProperties', $this->_returnProperties);
806
807 if ($buttonName == $this->_actionButtonName) {
808 // check actionName and if next, then do not repeat a search, since we are going to the next page
809 // hack, make sure we reset the task values
810 $stateMachine = $this->controller->getStateMachine();
811 $formName = $stateMachine->getTaskFormName();
812 $this->controller->resetPage($formName);
813 return;
814 }
815 else {
816 $output = CRM_Core_Selector_Controller::SESSION;
817
818 // create the selector, controller and run - store results in session
819 $searchChildGroups = TRUE;
820 if ($this->get('isAdvanced')) {
821 $searchChildGroups = FALSE;
822 }
823
824 $setDynamic = FALSE;
825
826 if (strpos(self::$_selectorName, 'CRM_Contact_Selector') !== FALSE) {
827 $selector = new self::$_selectorName(
828 $this->_customSearchClass,
829 $this->_formValues,
830 $this->_params,
831 $this->_returnProperties,
832 $this->_action,
833 FALSE,
834 $searchChildGroups,
835 $this->_context,
836 $this->_contextMenu
837 );
838 $setDynamic = TRUE;
839 }
840 else {
841 $selector = new self::$_selectorName(
842 $this->_params,
843 $this->_action,
844 NULL,
845 FALSE,
846 NULL,
847 "search",
848 "advanced"
849 );
850 }
851
852 $selector->setKey($this->controller->_key);
853
854 // added the sorting character to the form array
855 $config = CRM_Core_Config::singleton();
856 // do this only for contact search
857 if ($setDynamic && $config->includeAlphabeticalPager) {
858 // Don't recompute if we are just paging/sorting
859 if ($this->_reset || (empty($_GET['crmPID']) && empty($_GET['crmSID']) && !$this->_sortByCharacter)) {
860 $aToZBar = CRM_Utils_PagerAToZ::getAToZBar($selector, $this->_sortByCharacter);
861 $this->set('AToZBar', $aToZBar);
862 }
863 }
864
865 $sortID = NULL;
866 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
867 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
868 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
869 );
870 }
871 $controller = new CRM_Contact_Selector_Controller($selector,
872 $this->get(CRM_Utils_Pager::PAGE_ID),
873 $sortID,
874 CRM_Core_Action::VIEW,
875 $this,
876 $output
877 );
878 $controller->setEmbedded(TRUE);
879 $controller->setDynamicAction($setDynamic);
880 $controller->run();
881 }
882 }
883
884 /**
885 * @return NULL
886 */
887 public function &returnProperties() {
888 return CRM_Core_DAO::$_nullObject;
889 }
890
891 /**
892 * Return a descriptive name for the page, used in wizard header
893 *
894 * @return string
895 */
896 public function getTitle() {
897 return ts('Search');
898 }
899
900 }