Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
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; | |
6a488035 | 90 | |
64ffcefd SL |
91 | $this->loadStandardSearchOptionsFromUrl(); |
92 | $this->loadFormValues(); | |
6a488035 TO |
93 | |
94 | if ($this->_force) { | |
95 | $this->postProcess(); | |
96 | $this->set('force', 0); | |
97 | } | |
98 | ||
99 | $sortID = NULL; | |
100 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { | |
101 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), | |
102 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) | |
103 | ); | |
104 | } | |
105 | ||
6a488035 TO |
106 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
107 | $selector = new CRM_Case_Selector_Search($this->_queryParams, | |
108 | $this->_action, | |
109 | NULL, | |
110 | $this->_single, | |
111 | $this->_limit, | |
112 | $this->_context | |
113 | ); | |
114 | ||
115 | $prefix = NULL; | |
116 | if ($this->_context == 'user') { | |
117 | $prefix = $this->_prefix; | |
118 | } | |
119 | ||
120 | $this->assign("{$prefix}limit", $this->_limit); | |
121 | $this->assign("{$prefix}single", $this->_single); | |
122 | ||
123 | $controller = new CRM_Core_Selector_Controller($selector, | |
124 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
125 | $sortID, | |
126 | CRM_Core_Action::VIEW, | |
127 | $this, | |
128 | CRM_Core_Selector_Controller::TRANSFER, | |
129 | $prefix | |
130 | ); | |
131 | $controller->setEmbedded(TRUE); | |
132 | $controller->moveFromSessionToTemplate(); | |
133 | ||
134 | $this->assign('summary', $this->get('summary')); | |
135 | } | |
136 | ||
137 | /** | |
fe482240 | 138 | * Build the form object. |
6a488035 | 139 | */ |
00be9182 | 140 | public function buildQuickForm() { |
3efb5b86 | 141 | parent::buildQuickForm(); |
e597fc33 | 142 | $this->addSortNameField(); |
6a488035 TO |
143 | |
144 | CRM_Case_BAO_Query::buildSearchForm($this); | |
145 | ||
6a488035 TO |
146 | $rows = $this->get('rows'); |
147 | if (is_array($rows)) { | |
6a488035 | 148 | if (!$this->_single) { |
8d36b801 | 149 | $this->addRowSelectors($rows); |
6a488035 TO |
150 | } |
151 | ||
047838bc | 152 | $tasks = CRM_Case_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission()); |
6a488035 | 153 | |
a7488080 | 154 | if (!empty($this->_formValues['case_deleted'])) { |
7388b48a | 155 | unset($tasks[CRM_Case_Task::TASK_DELETE]); |
6a488035 TO |
156 | } |
157 | else { | |
047838bc | 158 | unset($tasks[CRM_Case_Task::RESTORE_CASES]); |
6a488035 TO |
159 | } |
160 | ||
34197a55 | 161 | $this->addTaskMenu($tasks); |
6a488035 TO |
162 | } |
163 | ||
6a488035 TO |
164 | } |
165 | ||
e597fc33 DG |
166 | /** |
167 | * Get the label for the sortName field if email searching is on. | |
168 | * | |
169 | * (email searching is a setting under search preferences). | |
170 | * | |
171 | * @return string | |
172 | */ | |
173 | protected function getSortNameLabelWithEmail() { | |
174 | return ts('Client Name or Email'); | |
175 | } | |
176 | ||
177 | /** | |
178 | * Get the label for the sortName field if email searching is off. | |
179 | * | |
180 | * (email searching is a setting under search preferences). | |
181 | * | |
182 | * @return string | |
183 | */ | |
184 | protected function getSortNameLabelWithOutEmail() { | |
185 | return ts('Client Name'); | |
186 | } | |
187 | ||
6a488035 TO |
188 | /** |
189 | * The post processing of the form gets done here. | |
190 | * | |
191 | * Key things done during post processing are | |
192 | * - check for reset or next request. if present, skip post procesing. | |
193 | * - now check if user requested running a saved search, if so, then | |
194 | * the form values associated with the saved search are used for searching. | |
195 | * - if user has done a submit with new values the regular post submissing is | |
196 | * done. | |
197 | * The processing consists of using a Selector / Controller framework for getting the | |
198 | * search results. | |
6a488035 | 199 | */ |
00be9182 | 200 | public function postProcess() { |
6a488035 TO |
201 | if ($this->_done) { |
202 | return; | |
203 | } | |
204 | ||
205 | $this->_done = TRUE; | |
206 | $this->_formValues = $this->controller->exportValues($this->_name); | |
207 | $this->fixFormValues(); | |
208 | ||
209 | if (isset($this->_ssID) && empty($_POST)) { | |
210 | // if we are editing / running a saved search and the form has not been posted | |
211 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); | |
212 | } | |
213 | ||
214 | //search for civicase | |
215 | if (!$this->_force) { | |
216 | if (array_key_exists('case_owner', $this->_formValues) && !$this->_formValues['case_owner']) { | |
217 | $this->_formValues['case_owner'] = 0; | |
218 | } | |
219 | } | |
220 | ||
a7488080 | 221 | if (empty($this->_formValues['case_deleted'])) { |
6a488035 TO |
222 | $this->_formValues['case_deleted'] = 0; |
223 | } | |
c94d39fd | 224 | CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); |
6a488035 TO |
225 | |
226 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
227 | ||
228 | $this->set('formValues', $this->_formValues); | |
229 | $this->set('queryParams', $this->_queryParams); | |
230 | ||
231 | $buttonName = $this->controller->getButtonName(); | |
e341bbee | 232 | if ($buttonName == $this->_actionButtonName) { |
6a488035 TO |
233 | // check actionName and if next, then do not repeat a search, since we are going to the next page |
234 | ||
235 | // hack, make sure we reset the task values | |
95ea77ec | 236 | $stateMachine = $this->controller->getStateMachine(); |
6a488035 TO |
237 | $formName = $stateMachine->getTaskFormName(); |
238 | $this->controller->resetPage($formName); | |
239 | return; | |
240 | } | |
241 | ||
242 | $sortID = NULL; | |
243 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { | |
244 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), | |
245 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) | |
246 | ); | |
247 | } | |
248 | ||
249 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
250 | ||
251 | $selector = new CRM_Case_Selector_Search($this->_queryParams, | |
252 | $this->_action, | |
253 | NULL, | |
254 | $this->_single, | |
255 | $this->_limit, | |
256 | $this->_context | |
257 | ); | |
258 | $selector->setKey($this->controller->_key); | |
259 | ||
260 | $prefix = NULL; | |
261 | if ($this->_context == 'user') { | |
262 | $prefix = $this->_prefix; | |
263 | } | |
264 | ||
265 | $this->assign("{$prefix}limit", $this->_limit); | |
266 | $this->assign("{$prefix}single", $this->_single); | |
267 | ||
268 | $controller = new CRM_Core_Selector_Controller($selector, | |
269 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
270 | $sortID, | |
271 | CRM_Core_Action::VIEW, | |
272 | $this, | |
273 | CRM_Core_Selector_Controller::SESSION, | |
274 | $prefix | |
275 | ); | |
276 | $controller->setEmbedded(TRUE); | |
277 | ||
278 | $query = &$selector->getQuery(); | |
279 | if ($this->_context == 'user') { | |
280 | $query->setSkipPermission(TRUE); | |
281 | } | |
282 | $controller->run(); | |
283 | } | |
284 | ||
285 | /** | |
df371444 | 286 | * Add the rules (mainly global rules) for form. |
287 | * | |
6a488035 TO |
288 | * All local rules are added near the element |
289 | * | |
6a488035 TO |
290 | * @see valid_date |
291 | */ | |
00be9182 | 292 | public function addRules() { |
6a488035 TO |
293 | $this->addFormRule(array('CRM_Case_Form_Search', 'formRule')); |
294 | } | |
295 | ||
296 | /** | |
fe482240 | 297 | * Global validation rules for the form. |
6a488035 | 298 | * |
64bd5a0e TO |
299 | * @param array $fields |
300 | * Posted values of the form. | |
2a6da8d7 | 301 | * |
df371444 | 302 | * @return array|bool |
6a488035 | 303 | */ |
00be9182 | 304 | public static function formRule($fields) { |
6a488035 TO |
305 | $errors = array(); |
306 | ||
307 | if (!empty($errors)) { | |
308 | return $errors; | |
309 | } | |
310 | ||
311 | return TRUE; | |
312 | } | |
313 | ||
314 | /** | |
fe482240 | 315 | * Set the default form values. |
6a488035 | 316 | * |
6a488035 | 317 | * |
a6c01b45 CW |
318 | * @return array |
319 | * the default array reference | |
6a488035 | 320 | */ |
00be9182 | 321 | public function setDefaultValues() { |
6a488035 TO |
322 | $defaults = array(); |
323 | $defaults = $this->_formValues; | |
324 | return $defaults; | |
325 | } | |
326 | ||
00be9182 | 327 | public function fixFormValues() { |
6a488035 TO |
328 | if (!$this->_force) { |
329 | return; | |
330 | } | |
331 | ||
1273d77c | 332 | $caseStatus = CRM_Utils_Request::retrieve('status', 'Positive'); |
6a488035 TO |
333 | if ($caseStatus) { |
334 | $this->_formValues['case_status_id'] = $caseStatus; | |
335 | $this->_defaults['case_status_id'] = $caseStatus; | |
336 | } | |
1273d77c | 337 | $caseType = CRM_Utils_Request::retrieve('type', 'Positive'); |
6a488035 | 338 | if ($caseType) { |
d5e5f843 CW |
339 | $this->_formValues['case_type_id'] = (array) $caseType; |
340 | $this->_defaults['case_type_id'] = (array) $caseType; | |
6a488035 TO |
341 | } |
342 | ||
1273d77c | 343 | $caseFromDate = CRM_Utils_Request::retrieve('pstart', 'Date'); |
6a488035 TO |
344 | if ($caseFromDate) { |
345 | list($date) = CRM_Utils_Date::setDateDefaults($caseFromDate); | |
346 | $this->_formValues['case_start_date_low'] = $date; | |
347 | $this->_defaults['case_start_date_low'] = $date; | |
348 | } | |
349 | ||
1273d77c | 350 | $caseToDate = CRM_Utils_Request::retrieve('pend', 'Date'); |
6a488035 TO |
351 | if ($caseToDate) { |
352 | list($date) = CRM_Utils_Date::setDateDefaults($caseToDate); | |
353 | $this->_formValues['case_start_date_high'] = $date; | |
354 | $this->_defaults['case_start_date_high'] = $date; | |
355 | } | |
356 | ||
357 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
358 | if ($cid) { | |
359 | $cid = CRM_Utils_Type::escape($cid, 'Integer'); | |
360 | if ($cid > 0) { | |
361 | $this->_formValues['contact_id'] = $cid; | |
362 | list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid); | |
363 | $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, | |
364 | 'sort_name' | |
365 | ); | |
366 | // also assign individual mode to the template | |
367 | $this->_single = TRUE; | |
368 | } | |
369 | } | |
370 | else { | |
e547f744 | 371 | // First, if "all" is stored in the session, default to all cases, otherwise default to no selection. |
6a488035 TO |
372 | $session = CRM_Core_Session::singleton(); |
373 | if (CRM_Utils_Request::retrieve('all', 'Positive', $session)) { | |
374 | $this->_formValues['case_owner'] = 1; | |
375 | $this->_defaults['case_owner'] = 1; | |
376 | } | |
377 | else { | |
378 | $this->_formValues['case_owner'] = 0; | |
379 | $this->_defaults['case_owner'] = 0; | |
380 | } | |
381 | ||
382 | // Now if case_owner is set in the url/post, use that instead. | |
1273d77c | 383 | $caseOwner = CRM_Utils_Request::retrieve('case_owner', 'Positive'); |
6a488035 TO |
384 | if ($caseOwner) { |
385 | $this->_formValues['case_owner'] = $caseOwner; | |
386 | $this->_defaults['case_owner'] = $caseOwner; | |
387 | } | |
388 | } | |
389 | } | |
390 | ||
4c6ce474 EM |
391 | /** |
392 | * @return null | |
393 | */ | |
00be9182 | 394 | public function getFormValues() { |
6a488035 TO |
395 | return NULL; |
396 | } | |
397 | ||
398 | /** | |
399 | * Return a descriptive name for the page, used in wizard header | |
400 | * | |
401 | * @return string | |
6a488035 TO |
402 | */ |
403 | public function getTitle() { | |
404 | return ts('Find Cases'); | |
405 | } | |
96025800 | 406 | |
6a488035 | 407 | } |