Merge pull request #7182 from totten/4.6-gencode-mem
[civicrm-core.git] / CRM / Case / Form / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 */
33
34/**
d76e5b23 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 *
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
65 protected $_defaults;
66
67 /**
fe482240 68 * Processing needed for buildForm and later.
6a488035
TO
69 *
70 * @return void
6a488035 71 */
00be9182 72 public function preProcess() {
6a488035
TO
73 $this->set('searchFormName', 'Search');
74
75 //check for civicase access.
76 if (!CRM_Case_BAO_Case::accessCiviCase()) {
77 CRM_Core_Error::fatal(ts('You are not authorized to access this page.'));
78 }
79
80 //validate case configuration.
81 $configured = CRM_Case_BAO_Case::isCaseConfigured();
82 $this->assign('notConfigured', !$configured['configured']);
83 if (!$configured['configured']) {
84 return;
85 }
86
87 /**
88 * set the button names
89 */
90 $this->_searchButtonName = $this->getButtonName('refresh');
6a488035
TO
91 $this->_actionButtonName = $this->getButtonName('next', 'action');
92
93 $this->_done = FALSE;
94 $this->defaults = array();
95
96 /*
e70a7fc0
TO
97 * we allow the controller to set force/reset externally, useful when we are being
98 * driven by the wizard framework
99 */
6a488035 100
353ffa53
TO
101 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject);
102 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
103 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
6a488035
TO
104 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
105
106 $this->assign('context', $this->_context);
107
108 // get user submitted values
109 // get it from controller only if form has been submitted, else preProcess has set this
110 if (!empty($_POST) && !$this->controller->isModal()) {
111 $this->_formValues = $this->controller->exportValues($this->_name);
112 }
113 else {
114 $this->_formValues = $this->get('formValues');
115 }
116
117 if (empty($this->_formValues)) {
118 if (isset($this->_ssID)) {
119 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
120 }
121 }
122
123 if ($this->_force) {
124 $this->postProcess();
125 $this->set('force', 0);
126 }
127
128 $sortID = NULL;
129 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
130 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
131 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
132 );
133 }
134
6a488035
TO
135 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
136 $selector = new CRM_Case_Selector_Search($this->_queryParams,
137 $this->_action,
138 NULL,
139 $this->_single,
140 $this->_limit,
141 $this->_context
142 );
143
144 $prefix = NULL;
145 if ($this->_context == 'user') {
146 $prefix = $this->_prefix;
147 }
148
149 $this->assign("{$prefix}limit", $this->_limit);
150 $this->assign("{$prefix}single", $this->_single);
151
152 $controller = new CRM_Core_Selector_Controller($selector,
153 $this->get(CRM_Utils_Pager::PAGE_ID),
154 $sortID,
155 CRM_Core_Action::VIEW,
156 $this,
157 CRM_Core_Selector_Controller::TRANSFER,
158 $prefix
159 );
160 $controller->setEmbedded(TRUE);
161 $controller->moveFromSessionToTemplate();
162
163 $this->assign('summary', $this->get('summary'));
164 }
165
166 /**
fe482240 167 * Build the form object.
6a488035 168 *
6a488035
TO
169 *
170 * @return void
171 */
00be9182 172 public function buildQuickForm() {
3efb5b86 173 parent::buildQuickForm();
6a488035
TO
174 $this->addElement('text',
175 'sort_name',
176 ts('Client Name or Email'),
177 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')
178 );
179
180 CRM_Case_BAO_Query::buildSearchForm($this);
181
6a488035
TO
182 $rows = $this->get('rows');
183 if (is_array($rows)) {
6a488035 184 if (!$this->_single) {
8d36b801 185 $this->addRowSelectors($rows);
6a488035
TO
186 }
187
6a488035
TO
188 $permission = CRM_Core_Permission::getPermission();
189
34197a55 190 $tasks = CRM_Case_Task::permissionedTaskTitles($permission);
6a488035 191
a7488080 192 if (!empty($this->_formValues['case_deleted'])) {
6a488035
TO
193 unset($tasks[1]);
194 }
195 else {
196 unset($tasks[4]);
197 }
198
34197a55 199 $this->addTaskMenu($tasks);
6a488035
TO
200 }
201
6a488035
TO
202 }
203
204 /**
205 * The post processing of the form gets done here.
206 *
207 * Key things done during post processing are
208 * - check for reset or next request. if present, skip post procesing.
209 * - now check if user requested running a saved search, if so, then
210 * the form values associated with the saved search are used for searching.
211 * - if user has done a submit with new values the regular post submissing is
212 * done.
213 * The processing consists of using a Selector / Controller framework for getting the
214 * search results.
215 *
216 * @param
217 *
218 * @return void
6a488035 219 */
00be9182 220 public function postProcess() {
6a488035
TO
221 if ($this->_done) {
222 return;
223 }
224
225 $this->_done = TRUE;
226 $this->_formValues = $this->controller->exportValues($this->_name);
227 $this->fixFormValues();
228
229 if (isset($this->_ssID) && empty($_POST)) {
230 // if we are editing / running a saved search and the form has not been posted
231 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
232 }
233
234 //search for civicase
235 if (!$this->_force) {
236 if (array_key_exists('case_owner', $this->_formValues) && !$this->_formValues['case_owner']) {
237 $this->_formValues['case_owner'] = 0;
238 }
239 }
240
241 //only fetch own cases.
242 if (!CRM_Core_Permission::check('access all cases and activities')) {
243 $this->_formValues['case_owner'] = 2;
244 }
245
a7488080 246 if (empty($this->_formValues['case_deleted'])) {
6a488035
TO
247 $this->_formValues['case_deleted'] = 0;
248 }
c94d39fd 249 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
6a488035
TO
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();
e341bbee 257 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
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
95ea77ec 261 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
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 /**
dc195289 311 * add the rules (mainly global rules) for form.
6a488035
TO
312 * All local rules are added near the element
313 *
355ba699 314 * @return void
6a488035
TO
315 * @see valid_date
316 */
00be9182 317 public function addRules() {
6a488035
TO
318 $this->addFormRule(array('CRM_Case_Form_Search', 'formRule'));
319 }
320
321 /**
fe482240 322 * Global validation rules for the form.
6a488035 323 *
64bd5a0e
TO
324 * @param array $fields
325 * Posted values of the form.
2a6da8d7 326 *
6a488035 327 * @return void
6a488035 328 */
00be9182 329 public static function formRule($fields) {
6a488035
TO
330 $errors = array();
331
332 if (!empty($errors)) {
333 return $errors;
334 }
335
336 return TRUE;
337 }
338
339 /**
fe482240 340 * Set the default form values.
6a488035 341 *
6a488035 342 *
a6c01b45
CW
343 * @return array
344 * the default array reference
6a488035 345 */
00be9182 346 public function setDefaultValues() {
6a488035
TO
347 $defaults = array();
348 $defaults = $this->_formValues;
349 return $defaults;
350 }
351
00be9182 352 public function fixFormValues() {
6a488035
TO
353 if (!$this->_force) {
354 return;
355 }
356
357 $caseStatus = CRM_Utils_Request::retrieve('status', 'Positive',
358 CRM_Core_DAO::$_nullObject
359 );
360 if ($caseStatus) {
361 $this->_formValues['case_status_id'] = $caseStatus;
362 $this->_defaults['case_status_id'] = $caseStatus;
363 }
364 $caseType = CRM_Utils_Request::retrieve('type', 'Positive',
365 CRM_Core_DAO::$_nullObject
366 );
367 if ($caseType) {
368 $this->_formValues['case_type_id'][$caseType] = 1;
369 $this->_defaults['case_type_id'][$caseType] = 1;
370 }
371
372 $caseFromDate = CRM_Utils_Request::retrieve('pstart', 'Date',
373 CRM_Core_DAO::$_nullObject
374 );
375 if ($caseFromDate) {
376 list($date) = CRM_Utils_Date::setDateDefaults($caseFromDate);
377 $this->_formValues['case_start_date_low'] = $date;
378 $this->_defaults['case_start_date_low'] = $date;
379 }
380
381 $caseToDate = CRM_Utils_Request::retrieve('pend', 'Date',
382 CRM_Core_DAO::$_nullObject
383 );
384 if ($caseToDate) {
385 list($date) = CRM_Utils_Date::setDateDefaults($caseToDate);
386 $this->_formValues['case_start_date_high'] = $date;
387 $this->_defaults['case_start_date_high'] = $date;
388 }
389
390 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
391 if ($cid) {
392 $cid = CRM_Utils_Type::escape($cid, 'Integer');
393 if ($cid > 0) {
394 $this->_formValues['contact_id'] = $cid;
395 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
396 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
397 'sort_name'
398 );
399 // also assign individual mode to the template
400 $this->_single = TRUE;
401 }
402 }
403 else {
e547f744 404 // First, if "all" is stored in the session, default to all cases, otherwise default to no selection.
6a488035
TO
405 $session = CRM_Core_Session::singleton();
406 if (CRM_Utils_Request::retrieve('all', 'Positive', $session)) {
407 $this->_formValues['case_owner'] = 1;
408 $this->_defaults['case_owner'] = 1;
409 }
410 else {
411 $this->_formValues['case_owner'] = 0;
412 $this->_defaults['case_owner'] = 0;
413 }
414
415 // Now if case_owner is set in the url/post, use that instead.
416 $caseOwner = CRM_Utils_Request::retrieve('case_owner', 'Positive',
417 CRM_Core_DAO::$_nullObject
418 );
419 if ($caseOwner) {
420 $this->_formValues['case_owner'] = $caseOwner;
421 $this->_defaults['case_owner'] = $caseOwner;
422 }
423 }
424 }
425
4c6ce474
EM
426 /**
427 * @return null
428 */
00be9182 429 public function getFormValues() {
6a488035
TO
430 return NULL;
431 }
432
433 /**
434 * Return a descriptive name for the page, used in wizard header
435 *
436 * @return string
6a488035
TO
437 */
438 public function getTitle() {
439 return ts('Find Cases');
440 }
96025800 441
6a488035 442}