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