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