Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This file is for Case search | |
38 | */ | |
3efb5b86 | 39 | class CRM_Case_Form_Search extends CRM_Core_Form_Search { |
6a488035 TO |
40 | |
41 | /** | |
42 | * Are we forced to run a search | |
43 | * | |
44 | * @var int | |
45 | * @access protected | |
46 | */ | |
47 | protected $_force; | |
48 | ||
49 | /** | |
50 | * name of search button | |
51 | * | |
52 | * @var string | |
53 | * @access protected | |
54 | */ | |
55 | protected $_searchButtonName; | |
56 | ||
6a488035 TO |
57 | /** |
58 | * name of action button | |
59 | * | |
60 | * @var string | |
61 | * @access protected | |
62 | */ | |
63 | protected $_actionButtonName; | |
64 | ||
65 | /** | |
66 | * form values that we will be using | |
67 | * | |
68 | * @var array | |
69 | * @access protected | |
70 | */ | |
71 | public $_formValues; | |
72 | ||
73 | /** | |
74 | * the params that are sent to the query | |
75 | * | |
76 | * @var array | |
77 | * @access protected | |
78 | */ | |
79 | protected $_queryParams; | |
80 | ||
81 | /** | |
82 | * have we already done this search | |
83 | * | |
84 | * @access protected | |
85 | * @var boolean | |
86 | */ | |
87 | protected $_done; | |
88 | ||
89 | /** | |
90 | * are we restricting ourselves to a single contact | |
91 | * | |
92 | * @access protected | |
93 | * @var boolean | |
94 | */ | |
95 | protected $_single = FALSE; | |
96 | ||
97 | /** | |
98 | * are we restricting ourselves to a single contact | |
99 | * | |
100 | * @access protected | |
101 | * @var boolean | |
102 | */ | |
103 | protected $_limit = NULL; | |
104 | ||
105 | /** | |
106 | * what context are we being invoked from | |
107 | * | |
108 | * @access protected | |
109 | * @var string | |
110 | */ | |
111 | protected $_context = NULL; | |
112 | ||
113 | /** | |
114 | * prefix for the controller | |
115 | * | |
116 | */ | |
117 | protected $_prefix = 'case_'; | |
118 | ||
119 | protected $_defaults; | |
120 | ||
121 | /** | |
122 | * processing needed for buildForm and later | |
123 | * | |
124 | * @return void | |
125 | * @access public | |
126 | */ | |
127 | function preProcess() { | |
128 | $this->set('searchFormName', 'Search'); | |
129 | ||
130 | //check for civicase access. | |
131 | if (!CRM_Case_BAO_Case::accessCiviCase()) { | |
132 | CRM_Core_Error::fatal(ts('You are not authorized to access this page.')); | |
133 | } | |
134 | ||
135 | //validate case configuration. | |
136 | $configured = CRM_Case_BAO_Case::isCaseConfigured(); | |
137 | $this->assign('notConfigured', !$configured['configured']); | |
138 | if (!$configured['configured']) { | |
139 | return; | |
140 | } | |
141 | ||
142 | /** | |
143 | * set the button names | |
144 | */ | |
145 | $this->_searchButtonName = $this->getButtonName('refresh'); | |
6a488035 TO |
146 | $this->_actionButtonName = $this->getButtonName('next', 'action'); |
147 | ||
148 | $this->_done = FALSE; | |
149 | $this->defaults = array(); | |
150 | ||
151 | /* | |
152 | * we allow the controller to set force/reset externally, useful when we are being | |
153 | * driven by the wizard framework | |
154 | */ | |
155 | ||
156 | $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject); | |
157 | $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE); | |
158 | $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this); | |
159 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search'); | |
160 | ||
161 | $this->assign('context', $this->_context); | |
162 | ||
163 | // get user submitted values | |
164 | // get it from controller only if form has been submitted, else preProcess has set this | |
165 | if (!empty($_POST) && !$this->controller->isModal()) { | |
166 | $this->_formValues = $this->controller->exportValues($this->_name); | |
167 | } | |
168 | else { | |
169 | $this->_formValues = $this->get('formValues'); | |
170 | } | |
171 | ||
172 | if (empty($this->_formValues)) { | |
173 | if (isset($this->_ssID)) { | |
174 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); | |
175 | } | |
176 | } | |
177 | ||
178 | if ($this->_force) { | |
179 | $this->postProcess(); | |
180 | $this->set('force', 0); | |
181 | } | |
182 | ||
183 | $sortID = NULL; | |
184 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { | |
185 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), | |
186 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) | |
187 | ); | |
188 | } | |
189 | ||
190 | ||
191 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
192 | $selector = new CRM_Case_Selector_Search($this->_queryParams, | |
193 | $this->_action, | |
194 | NULL, | |
195 | $this->_single, | |
196 | $this->_limit, | |
197 | $this->_context | |
198 | ); | |
199 | ||
200 | $prefix = NULL; | |
201 | if ($this->_context == 'user') { | |
202 | $prefix = $this->_prefix; | |
203 | } | |
204 | ||
205 | $this->assign("{$prefix}limit", $this->_limit); | |
206 | $this->assign("{$prefix}single", $this->_single); | |
207 | ||
208 | $controller = new CRM_Core_Selector_Controller($selector, | |
209 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
210 | $sortID, | |
211 | CRM_Core_Action::VIEW, | |
212 | $this, | |
213 | CRM_Core_Selector_Controller::TRANSFER, | |
214 | $prefix | |
215 | ); | |
216 | $controller->setEmbedded(TRUE); | |
217 | $controller->moveFromSessionToTemplate(); | |
218 | ||
219 | $this->assign('summary', $this->get('summary')); | |
220 | } | |
221 | ||
222 | /** | |
223 | * Build the form | |
224 | * | |
225 | * @access public | |
226 | * | |
227 | * @return void | |
228 | */ | |
229 | function buildQuickForm() { | |
3efb5b86 | 230 | parent::buildQuickForm(); |
6a488035 TO |
231 | $this->addElement('text', |
232 | 'sort_name', | |
233 | ts('Client Name or Email'), | |
234 | CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name') | |
235 | ); | |
236 | ||
237 | CRM_Case_BAO_Query::buildSearchForm($this); | |
238 | ||
239 | /* | |
240 | * add form checkboxes for each row. This is needed out here to conform to QF protocol | |
241 | * of all elements being declared in builQuickForm | |
242 | */ | |
243 | $rows = $this->get('rows'); | |
244 | if (is_array($rows)) { | |
6a488035 TO |
245 | if (!$this->_single) { |
246 | $this->addElement('checkbox', | |
247 | 'toggleSelect', | |
248 | NULL, | |
249 | NULL, | |
d664f648 | 250 | array('onclick' => "toggleTaskAction( true );", 'class' => 'select-rows') |
6a488035 TO |
251 | ); |
252 | ||
253 | foreach ($rows as $row) { | |
254 | $this->addElement('checkbox', $row['checkbox'], | |
255 | NULL, NULL, | |
d664f648 | 256 | array('onclick' => "toggleTaskAction( true );", 'class' => 'select-row') |
6a488035 TO |
257 | ); |
258 | } | |
259 | } | |
260 | ||
261 | $total = $cancel = 0; | |
262 | ||
263 | $permission = CRM_Core_Permission::getPermission(); | |
264 | ||
265 | $tasks = array('' => ts('- actions -')) + CRM_Case_Task::permissionedTaskTitles($permission); | |
266 | ||
a7488080 | 267 | if (!empty($this->_formValues['case_deleted'])) { |
6a488035 TO |
268 | unset($tasks[1]); |
269 | } | |
270 | else { | |
271 | unset($tasks[4]); | |
272 | } | |
273 | ||
274 | $this->add('select', 'task', ts('Actions:') . ' ', $tasks); | |
275 | $this->add('submit', $this->_actionButtonName, ts('Go'), | |
276 | array( | |
277 | 'class' => 'form-submit', | |
278 | 'id' => 'Go', | |
279 | 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 0);", | |
280 | ) | |
281 | ); | |
282 | ||
6a488035 TO |
283 | // need to perform tasks on all or selected items ? using radio_ts(task selection) for it |
284 | $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked')); | |
d664f648 | 285 | $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all', array('class' => 'select-rows', 'onclick' => $this->getName() . ".toggleSelect.checked = false; toggleTaskAction( true );")); |
6a488035 TO |
286 | } |
287 | ||
6a488035 TO |
288 | } |
289 | ||
290 | /** | |
291 | * The post processing of the form gets done here. | |
292 | * | |
293 | * Key things done during post processing are | |
294 | * - check for reset or next request. if present, skip post procesing. | |
295 | * - now check if user requested running a saved search, if so, then | |
296 | * the form values associated with the saved search are used for searching. | |
297 | * - if user has done a submit with new values the regular post submissing is | |
298 | * done. | |
299 | * The processing consists of using a Selector / Controller framework for getting the | |
300 | * search results. | |
301 | * | |
302 | * @param | |
303 | * | |
304 | * @return void | |
305 | * @access public | |
306 | */ | |
307 | function postProcess() { | |
308 | if ($this->_done) { | |
309 | return; | |
310 | } | |
311 | ||
312 | $this->_done = TRUE; | |
313 | $this->_formValues = $this->controller->exportValues($this->_name); | |
314 | $this->fixFormValues(); | |
315 | ||
316 | if (isset($this->_ssID) && empty($_POST)) { | |
317 | // if we are editing / running a saved search and the form has not been posted | |
318 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); | |
319 | } | |
320 | ||
321 | //search for civicase | |
322 | if (!$this->_force) { | |
323 | if (array_key_exists('case_owner', $this->_formValues) && !$this->_formValues['case_owner']) { | |
324 | $this->_formValues['case_owner'] = 0; | |
325 | } | |
326 | } | |
327 | ||
328 | //only fetch own cases. | |
329 | if (!CRM_Core_Permission::check('access all cases and activities')) { | |
330 | $this->_formValues['case_owner'] = 2; | |
331 | } | |
332 | ||
a7488080 | 333 | if (empty($this->_formValues['case_deleted'])) { |
6a488035 TO |
334 | $this->_formValues['case_deleted'] = 0; |
335 | } | |
336 | CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues); | |
337 | ||
338 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
339 | ||
340 | $this->set('formValues', $this->_formValues); | |
341 | $this->set('queryParams', $this->_queryParams); | |
342 | ||
343 | $buttonName = $this->controller->getButtonName(); | |
e341bbee | 344 | if ($buttonName == $this->_actionButtonName) { |
6a488035 TO |
345 | // check actionName and if next, then do not repeat a search, since we are going to the next page |
346 | ||
347 | // hack, make sure we reset the task values | |
95ea77ec | 348 | $stateMachine = $this->controller->getStateMachine(); |
6a488035 TO |
349 | $formName = $stateMachine->getTaskFormName(); |
350 | $this->controller->resetPage($formName); | |
351 | return; | |
352 | } | |
353 | ||
354 | $sortID = NULL; | |
355 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { | |
356 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), | |
357 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) | |
358 | ); | |
359 | } | |
360 | ||
361 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
362 | ||
363 | $selector = new CRM_Case_Selector_Search($this->_queryParams, | |
364 | $this->_action, | |
365 | NULL, | |
366 | $this->_single, | |
367 | $this->_limit, | |
368 | $this->_context | |
369 | ); | |
370 | $selector->setKey($this->controller->_key); | |
371 | ||
372 | $prefix = NULL; | |
373 | if ($this->_context == 'user') { | |
374 | $prefix = $this->_prefix; | |
375 | } | |
376 | ||
377 | $this->assign("{$prefix}limit", $this->_limit); | |
378 | $this->assign("{$prefix}single", $this->_single); | |
379 | ||
380 | $controller = new CRM_Core_Selector_Controller($selector, | |
381 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
382 | $sortID, | |
383 | CRM_Core_Action::VIEW, | |
384 | $this, | |
385 | CRM_Core_Selector_Controller::SESSION, | |
386 | $prefix | |
387 | ); | |
388 | $controller->setEmbedded(TRUE); | |
389 | ||
390 | $query = &$selector->getQuery(); | |
391 | if ($this->_context == 'user') { | |
392 | $query->setSkipPermission(TRUE); | |
393 | } | |
394 | $controller->run(); | |
395 | } | |
396 | ||
397 | /** | |
398 | * This function is used to add the rules (mainly global rules) for form. | |
399 | * All local rules are added near the element | |
400 | * | |
355ba699 | 401 | * @return void |
6a488035 TO |
402 | * @access public |
403 | * @see valid_date | |
404 | */ | |
405 | function addRules() { | |
406 | $this->addFormRule(array('CRM_Case_Form_Search', 'formRule')); | |
407 | } | |
408 | ||
409 | /** | |
410 | * global validation rules for the form | |
411 | * | |
412 | * @param array $fields posted values of the form | |
413 | * @param array $errors list of errors to be posted back to the form | |
414 | * | |
415 | * @return void | |
416 | * @static | |
417 | * @access public | |
418 | */ | |
419 | static function formRule($fields) { | |
420 | $errors = array(); | |
421 | ||
422 | if (!empty($errors)) { | |
423 | return $errors; | |
424 | } | |
425 | ||
426 | return TRUE; | |
427 | } | |
428 | ||
429 | /** | |
430 | * Set the default form values | |
431 | * | |
432 | * @access protected | |
433 | * | |
434 | * @return array the default array reference | |
435 | */ | |
436 | function setDefaultValues() { | |
437 | $defaults = array(); | |
438 | $defaults = $this->_formValues; | |
439 | return $defaults; | |
440 | } | |
441 | ||
442 | function fixFormValues() { | |
443 | if (!$this->_force) { | |
444 | return; | |
445 | } | |
446 | ||
447 | $caseStatus = CRM_Utils_Request::retrieve('status', 'Positive', | |
448 | CRM_Core_DAO::$_nullObject | |
449 | ); | |
450 | if ($caseStatus) { | |
451 | $this->_formValues['case_status_id'] = $caseStatus; | |
452 | $this->_defaults['case_status_id'] = $caseStatus; | |
453 | } | |
454 | $caseType = CRM_Utils_Request::retrieve('type', 'Positive', | |
455 | CRM_Core_DAO::$_nullObject | |
456 | ); | |
457 | if ($caseType) { | |
458 | $this->_formValues['case_type_id'][$caseType] = 1; | |
459 | $this->_defaults['case_type_id'][$caseType] = 1; | |
460 | } | |
461 | ||
462 | $caseFromDate = CRM_Utils_Request::retrieve('pstart', 'Date', | |
463 | CRM_Core_DAO::$_nullObject | |
464 | ); | |
465 | if ($caseFromDate) { | |
466 | list($date) = CRM_Utils_Date::setDateDefaults($caseFromDate); | |
467 | $this->_formValues['case_start_date_low'] = $date; | |
468 | $this->_defaults['case_start_date_low'] = $date; | |
469 | } | |
470 | ||
471 | $caseToDate = CRM_Utils_Request::retrieve('pend', 'Date', | |
472 | CRM_Core_DAO::$_nullObject | |
473 | ); | |
474 | if ($caseToDate) { | |
475 | list($date) = CRM_Utils_Date::setDateDefaults($caseToDate); | |
476 | $this->_formValues['case_start_date_high'] = $date; | |
477 | $this->_defaults['case_start_date_high'] = $date; | |
478 | } | |
479 | ||
480 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
481 | if ($cid) { | |
482 | $cid = CRM_Utils_Type::escape($cid, 'Integer'); | |
483 | if ($cid > 0) { | |
484 | $this->_formValues['contact_id'] = $cid; | |
485 | list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid); | |
486 | $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, | |
487 | 'sort_name' | |
488 | ); | |
489 | // also assign individual mode to the template | |
490 | $this->_single = TRUE; | |
491 | } | |
492 | } | |
493 | else { | |
494 | // First, if "all" is stored in the session, default to all cases, otherwise default to no selection. | |
495 | $session = CRM_Core_Session::singleton(); | |
496 | if (CRM_Utils_Request::retrieve('all', 'Positive', $session)) { | |
497 | $this->_formValues['case_owner'] = 1; | |
498 | $this->_defaults['case_owner'] = 1; | |
499 | } | |
500 | else { | |
501 | $this->_formValues['case_owner'] = 0; | |
502 | $this->_defaults['case_owner'] = 0; | |
503 | } | |
504 | ||
505 | // Now if case_owner is set in the url/post, use that instead. | |
506 | $caseOwner = CRM_Utils_Request::retrieve('case_owner', 'Positive', | |
507 | CRM_Core_DAO::$_nullObject | |
508 | ); | |
509 | if ($caseOwner) { | |
510 | $this->_formValues['case_owner'] = $caseOwner; | |
511 | $this->_defaults['case_owner'] = $caseOwner; | |
512 | } | |
513 | } | |
514 | } | |
515 | ||
516 | function getFormValues() { | |
517 | return NULL; | |
518 | } | |
519 | ||
520 | /** | |
521 | * Return a descriptive name for the page, used in wizard header | |
522 | * | |
523 | * @return string | |
524 | * @access public | |
525 | */ | |
526 | public function getTitle() { | |
527 | return ts('Find Cases'); | |
528 | } | |
529 | } | |
530 |