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