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