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