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