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