Merge pull request #11197 from agileware/CRM-21104
[civicrm-core.git] / CRM / Case / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
31 * @copyright CiviCRM LLC (c) 2004-2018
32 */
33
34 /**
35 * This file is for Case search.
36 */
37 class CRM_Case_Form_Search extends CRM_Core_Form_Search {
38
39 /**
40 * The params that are sent to the query
41 *
42 * @var array
43 */
44 protected $_queryParams;
45
46 /**
47 * Are we restricting ourselves to a single contact
48 *
49 * @var boolean
50 */
51 protected $_single = FALSE;
52
53 /**
54 * Are we restricting ourselves to a single contact
55 *
56 * @var boolean
57 */
58 protected $_limit = NULL;
59
60 /**
61 * Prefix for the controller
62 */
63 protected $_prefix = 'case_';
64
65 /**
66 * Processing needed for buildForm and later.
67 */
68 public function preProcess() {
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');
87 $this->_actionButtonName = $this->getButtonName('next', 'action');
88
89 $this->_done = FALSE;
90 $this->defaults = array();
91
92 /*
93 * we allow the controller to set force/reset externally, useful when we are being
94 * driven by the wizard framework
95 */
96
97 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean');
98 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
99 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
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
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 /**
163 * Build the form object.
164 */
165 public function buildQuickForm() {
166 parent::buildQuickForm();
167 $this->addSortNameField();
168
169 CRM_Case_BAO_Query::buildSearchForm($this);
170
171 $rows = $this->get('rows');
172 if (is_array($rows)) {
173 if (!$this->_single) {
174 $this->addRowSelectors($rows);
175 }
176
177 $tasks = CRM_Case_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission());
178
179 if (!empty($this->_formValues['case_deleted'])) {
180 unset($tasks[CRM_Case_Task::TASK_DELETE]);
181 }
182 else {
183 unset($tasks[CRM_Case_Task::RESTORE_CASES]);
184 }
185
186 $this->addTaskMenu($tasks);
187 }
188
189 }
190
191 /**
192 * Get the label for the sortName field if email searching is on.
193 *
194 * (email searching is a setting under search preferences).
195 *
196 * @return string
197 */
198 protected function getSortNameLabelWithEmail() {
199 return ts('Client Name or Email');
200 }
201
202 /**
203 * Get the label for the sortName field if email searching is off.
204 *
205 * (email searching is a setting under search preferences).
206 *
207 * @return string
208 */
209 protected function getSortNameLabelWithOutEmail() {
210 return ts('Client Name');
211 }
212
213 /**
214 * The post processing of the form gets done here.
215 *
216 * Key things done during post processing are
217 * - check for reset or next request. if present, skip post procesing.
218 * - now check if user requested running a saved search, if so, then
219 * the form values associated with the saved search are used for searching.
220 * - if user has done a submit with new values the regular post submissing is
221 * done.
222 * The processing consists of using a Selector / Controller framework for getting the
223 * search results.
224 */
225 public function postProcess() {
226 if ($this->_done) {
227 return;
228 }
229
230 $this->_done = TRUE;
231 $this->_formValues = $this->controller->exportValues($this->_name);
232 $this->fixFormValues();
233
234 if (isset($this->_ssID) && empty($_POST)) {
235 // if we are editing / running a saved search and the form has not been posted
236 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
237 }
238
239 //search for civicase
240 if (!$this->_force) {
241 if (array_key_exists('case_owner', $this->_formValues) && !$this->_formValues['case_owner']) {
242 $this->_formValues['case_owner'] = 0;
243 }
244 }
245
246 if (empty($this->_formValues['case_deleted'])) {
247 $this->_formValues['case_deleted'] = 0;
248 }
249 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
250
251 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
252
253 $this->set('formValues', $this->_formValues);
254 $this->set('queryParams', $this->_queryParams);
255
256 $buttonName = $this->controller->getButtonName();
257 if ($buttonName == $this->_actionButtonName) {
258 // check actionName and if next, then do not repeat a search, since we are going to the next page
259
260 // hack, make sure we reset the task values
261 $stateMachine = $this->controller->getStateMachine();
262 $formName = $stateMachine->getTaskFormName();
263 $this->controller->resetPage($formName);
264 return;
265 }
266
267 $sortID = NULL;
268 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
269 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
270 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
271 );
272 }
273
274 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
275
276 $selector = new CRM_Case_Selector_Search($this->_queryParams,
277 $this->_action,
278 NULL,
279 $this->_single,
280 $this->_limit,
281 $this->_context
282 );
283 $selector->setKey($this->controller->_key);
284
285 $prefix = NULL;
286 if ($this->_context == 'user') {
287 $prefix = $this->_prefix;
288 }
289
290 $this->assign("{$prefix}limit", $this->_limit);
291 $this->assign("{$prefix}single", $this->_single);
292
293 $controller = new CRM_Core_Selector_Controller($selector,
294 $this->get(CRM_Utils_Pager::PAGE_ID),
295 $sortID,
296 CRM_Core_Action::VIEW,
297 $this,
298 CRM_Core_Selector_Controller::SESSION,
299 $prefix
300 );
301 $controller->setEmbedded(TRUE);
302
303 $query = &$selector->getQuery();
304 if ($this->_context == 'user') {
305 $query->setSkipPermission(TRUE);
306 }
307 $controller->run();
308 }
309
310 /**
311 * Add the rules (mainly global rules) for form.
312 *
313 * All local rules are added near the element
314 *
315 * @see valid_date
316 */
317 public function addRules() {
318 $this->addFormRule(array('CRM_Case_Form_Search', 'formRule'));
319 }
320
321 /**
322 * Global validation rules for the form.
323 *
324 * @param array $fields
325 * Posted values of the form.
326 *
327 * @return array|bool
328 */
329 public static function formRule($fields) {
330 $errors = array();
331
332 if (!empty($errors)) {
333 return $errors;
334 }
335
336 return TRUE;
337 }
338
339 /**
340 * Set the default form values.
341 *
342 *
343 * @return array
344 * the default array reference
345 */
346 public function setDefaultValues() {
347 $defaults = array();
348 $defaults = $this->_formValues;
349 return $defaults;
350 }
351
352 public function fixFormValues() {
353 if (!$this->_force) {
354 return;
355 }
356
357 $caseStatus = CRM_Utils_Request::retrieve('status', 'Positive');
358 if ($caseStatus) {
359 $this->_formValues['case_status_id'] = $caseStatus;
360 $this->_defaults['case_status_id'] = $caseStatus;
361 }
362 $caseType = CRM_Utils_Request::retrieve('type', 'Positive');
363 if ($caseType) {
364 $this->_formValues['case_type_id'] = (array) $caseType;
365 $this->_defaults['case_type_id'] = (array) $caseType;
366 }
367
368 $caseFromDate = CRM_Utils_Request::retrieve('pstart', 'Date');
369 if ($caseFromDate) {
370 list($date) = CRM_Utils_Date::setDateDefaults($caseFromDate);
371 $this->_formValues['case_start_date_low'] = $date;
372 $this->_defaults['case_start_date_low'] = $date;
373 }
374
375 $caseToDate = CRM_Utils_Request::retrieve('pend', 'Date');
376 if ($caseToDate) {
377 list($date) = CRM_Utils_Date::setDateDefaults($caseToDate);
378 $this->_formValues['case_start_date_high'] = $date;
379 $this->_defaults['case_start_date_high'] = $date;
380 }
381
382 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
383 if ($cid) {
384 $cid = CRM_Utils_Type::escape($cid, 'Integer');
385 if ($cid > 0) {
386 $this->_formValues['contact_id'] = $cid;
387 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
388 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
389 'sort_name'
390 );
391 // also assign individual mode to the template
392 $this->_single = TRUE;
393 }
394 }
395 else {
396 // First, if "all" is stored in the session, default to all cases, otherwise default to no selection.
397 $session = CRM_Core_Session::singleton();
398 if (CRM_Utils_Request::retrieve('all', 'Positive', $session)) {
399 $this->_formValues['case_owner'] = 1;
400 $this->_defaults['case_owner'] = 1;
401 }
402 else {
403 $this->_formValues['case_owner'] = 0;
404 $this->_defaults['case_owner'] = 0;
405 }
406
407 // Now if case_owner is set in the url/post, use that instead.
408 $caseOwner = CRM_Utils_Request::retrieve('case_owner', 'Positive');
409 if ($caseOwner) {
410 $this->_formValues['case_owner'] = $caseOwner;
411 $this->_defaults['case_owner'] = $caseOwner;
412 }
413 }
414 }
415
416 /**
417 * @return null
418 */
419 public function getFormValues() {
420 return NULL;
421 }
422
423 /**
424 * Return a descriptive name for the page, used in wizard header
425 *
426 * @return string
427 */
428 public function getTitle() {
429 return ts('Find Cases');
430 }
431
432 }