Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2014-12-09-14-48-51
[civicrm-core.git] / CRM / Case / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This file is for Case search
38 */
39 class CRM_Case_Form_Search extends CRM_Core_Form_Search {
40
41 /**
42 * The params that are sent to the query
43 *
44 * @var array
45 * @access protected
46 */
47 protected $_queryParams;
48
49 /**
50 * Are we restricting ourselves to a single contact
51 *
52 * @access protected
53 * @var boolean
54 */
55 protected $_single = FALSE;
56
57 /**
58 * Are we restricting ourselves to a single contact
59 *
60 * @access protected
61 * @var boolean
62 */
63 protected $_limit = NULL;
64
65 /**
66 * Prefix for the controller
67 *
68 */
69 protected $_prefix = 'case_';
70
71 protected $_defaults;
72
73 /**
74 * Processing needed for buildForm and later
75 *
76 * @return void
77 * @access public
78 */
79 function preProcess() {
80 $this->set('searchFormName', 'Search');
81
82 //check for civicase access.
83 if (!CRM_Case_BAO_Case::accessCiviCase()) {
84 CRM_Core_Error::fatal(ts('You are not authorized to access this page.'));
85 }
86
87 //validate case configuration.
88 $configured = CRM_Case_BAO_Case::isCaseConfigured();
89 $this->assign('notConfigured', !$configured['configured']);
90 if (!$configured['configured']) {
91 return;
92 }
93
94 /**
95 * set the button names
96 */
97 $this->_searchButtonName = $this->getButtonName('refresh');
98 $this->_actionButtonName = $this->getButtonName('next', 'action');
99
100 $this->_done = FALSE;
101 $this->defaults = array();
102
103 /*
104 * we allow the controller to set force/reset externally, useful when we are being
105 * driven by the wizard framework
106 */
107
108 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject);
109 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
110 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
111 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
112
113 $this->assign('context', $this->_context);
114
115 // get user submitted values
116 // get it from controller only if form has been submitted, else preProcess has set this
117 if (!empty($_POST) && !$this->controller->isModal()) {
118 $this->_formValues = $this->controller->exportValues($this->_name);
119 }
120 else {
121 $this->_formValues = $this->get('formValues');
122 }
123
124 if (empty($this->_formValues)) {
125 if (isset($this->_ssID)) {
126 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
127 }
128 }
129
130 if ($this->_force) {
131 $this->postProcess();
132 $this->set('force', 0);
133 }
134
135 $sortID = NULL;
136 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
137 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
138 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
139 );
140 }
141
142
143 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
144 $selector = new CRM_Case_Selector_Search($this->_queryParams,
145 $this->_action,
146 NULL,
147 $this->_single,
148 $this->_limit,
149 $this->_context
150 );
151
152 $prefix = NULL;
153 if ($this->_context == 'user') {
154 $prefix = $this->_prefix;
155 }
156
157 $this->assign("{$prefix}limit", $this->_limit);
158 $this->assign("{$prefix}single", $this->_single);
159
160 $controller = new CRM_Core_Selector_Controller($selector,
161 $this->get(CRM_Utils_Pager::PAGE_ID),
162 $sortID,
163 CRM_Core_Action::VIEW,
164 $this,
165 CRM_Core_Selector_Controller::TRANSFER,
166 $prefix
167 );
168 $controller->setEmbedded(TRUE);
169 $controller->moveFromSessionToTemplate();
170
171 $this->assign('summary', $this->get('summary'));
172 }
173
174 /**
175 * Build the form object
176 *
177 * @access public
178 *
179 * @return void
180 */
181 function buildQuickForm() {
182 parent::buildQuickForm();
183 $this->addElement('text',
184 'sort_name',
185 ts('Client Name or Email'),
186 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')
187 );
188
189 CRM_Case_BAO_Query::buildSearchForm($this);
190
191 $rows = $this->get('rows');
192 if (is_array($rows)) {
193 if (!$this->_single) {
194 $this->addRowSelectors($rows);
195 }
196
197 $permission = CRM_Core_Permission::getPermission();
198
199 $tasks = CRM_Case_Task::permissionedTaskTitles($permission);
200
201 if (!empty($this->_formValues['case_deleted'])) {
202 unset($tasks[1]);
203 }
204 else {
205 unset($tasks[4]);
206 }
207
208 $this->addTaskMenu($tasks);
209 }
210
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 * @param
226 *
227 * @return void
228 * @access public
229 */
230 function postProcess() {
231 if ($this->_done) {
232 return;
233 }
234
235 $this->_done = TRUE;
236 $this->_formValues = $this->controller->exportValues($this->_name);
237 $this->fixFormValues();
238
239 if (isset($this->_ssID) && empty($_POST)) {
240 // if we are editing / running a saved search and the form has not been posted
241 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
242 }
243
244 //search for civicase
245 if (!$this->_force) {
246 if (array_key_exists('case_owner', $this->_formValues) && !$this->_formValues['case_owner']) {
247 $this->_formValues['case_owner'] = 0;
248 }
249 }
250
251 //only fetch own cases.
252 if (!CRM_Core_Permission::check('access all cases and activities')) {
253 $this->_formValues['case_owner'] = 2;
254 }
255
256 if (empty($this->_formValues['case_deleted'])) {
257 $this->_formValues['case_deleted'] = 0;
258 }
259 CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
260
261 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
262
263 $this->set('formValues', $this->_formValues);
264 $this->set('queryParams', $this->_queryParams);
265
266 $buttonName = $this->controller->getButtonName();
267 if ($buttonName == $this->_actionButtonName) {
268 // check actionName and if next, then do not repeat a search, since we are going to the next page
269
270 // hack, make sure we reset the task values
271 $stateMachine = $this->controller->getStateMachine();
272 $formName = $stateMachine->getTaskFormName();
273 $this->controller->resetPage($formName);
274 return;
275 }
276
277 $sortID = NULL;
278 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
279 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
280 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
281 );
282 }
283
284 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
285
286 $selector = new CRM_Case_Selector_Search($this->_queryParams,
287 $this->_action,
288 NULL,
289 $this->_single,
290 $this->_limit,
291 $this->_context
292 );
293 $selector->setKey($this->controller->_key);
294
295 $prefix = NULL;
296 if ($this->_context == 'user') {
297 $prefix = $this->_prefix;
298 }
299
300 $this->assign("{$prefix}limit", $this->_limit);
301 $this->assign("{$prefix}single", $this->_single);
302
303 $controller = new CRM_Core_Selector_Controller($selector,
304 $this->get(CRM_Utils_Pager::PAGE_ID),
305 $sortID,
306 CRM_Core_Action::VIEW,
307 $this,
308 CRM_Core_Selector_Controller::SESSION,
309 $prefix
310 );
311 $controller->setEmbedded(TRUE);
312
313 $query = &$selector->getQuery();
314 if ($this->_context == 'user') {
315 $query->setSkipPermission(TRUE);
316 }
317 $controller->run();
318 }
319
320 /**
321 * This function is used to add the rules (mainly global rules) for form.
322 * All local rules are added near the element
323 *
324 * @return void
325 * @access public
326 * @see valid_date
327 */
328 function addRules() {
329 $this->addFormRule(array('CRM_Case_Form_Search', 'formRule'));
330 }
331
332 /**
333 * Global validation rules for the form
334 *
335 * @param array $fields posted values of the form
336 *
337 * @return void
338 * @static
339 * @access public
340 */
341 static function formRule($fields) {
342 $errors = array();
343
344 if (!empty($errors)) {
345 return $errors;
346 }
347
348 return TRUE;
349 }
350
351 /**
352 * Set the default form values
353 *
354 * @access protected
355 *
356 * @return array the default array reference
357 */
358 function setDefaultValues() {
359 $defaults = array();
360 $defaults = $this->_formValues;
361 return $defaults;
362 }
363
364 function fixFormValues() {
365 if (!$this->_force) {
366 return;
367 }
368
369 $caseStatus = CRM_Utils_Request::retrieve('status', 'Positive',
370 CRM_Core_DAO::$_nullObject
371 );
372 if ($caseStatus) {
373 $this->_formValues['case_status_id'] = $caseStatus;
374 $this->_defaults['case_status_id'] = $caseStatus;
375 }
376 $caseType = CRM_Utils_Request::retrieve('type', 'Positive',
377 CRM_Core_DAO::$_nullObject
378 );
379 if ($caseType) {
380 $this->_formValues['case_type_id'][$caseType] = 1;
381 $this->_defaults['case_type_id'][$caseType] = 1;
382 }
383
384 $caseFromDate = CRM_Utils_Request::retrieve('pstart', 'Date',
385 CRM_Core_DAO::$_nullObject
386 );
387 if ($caseFromDate) {
388 list($date) = CRM_Utils_Date::setDateDefaults($caseFromDate);
389 $this->_formValues['case_start_date_low'] = $date;
390 $this->_defaults['case_start_date_low'] = $date;
391 }
392
393 $caseToDate = CRM_Utils_Request::retrieve('pend', 'Date',
394 CRM_Core_DAO::$_nullObject
395 );
396 if ($caseToDate) {
397 list($date) = CRM_Utils_Date::setDateDefaults($caseToDate);
398 $this->_formValues['case_start_date_high'] = $date;
399 $this->_defaults['case_start_date_high'] = $date;
400 }
401
402 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
403 if ($cid) {
404 $cid = CRM_Utils_Type::escape($cid, 'Integer');
405 if ($cid > 0) {
406 $this->_formValues['contact_id'] = $cid;
407 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
408 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
409 'sort_name'
410 );
411 // also assign individual mode to the template
412 $this->_single = TRUE;
413 }
414 }
415 else {
416 // First, if "all" is stored in the session, default to all cases, otherwise default to no selection.
417 $session = CRM_Core_Session::singleton();
418 if (CRM_Utils_Request::retrieve('all', 'Positive', $session)) {
419 $this->_formValues['case_owner'] = 1;
420 $this->_defaults['case_owner'] = 1;
421 }
422 else {
423 $this->_formValues['case_owner'] = 0;
424 $this->_defaults['case_owner'] = 0;
425 }
426
427 // Now if case_owner is set in the url/post, use that instead.
428 $caseOwner = CRM_Utils_Request::retrieve('case_owner', 'Positive',
429 CRM_Core_DAO::$_nullObject
430 );
431 if ($caseOwner) {
432 $this->_formValues['case_owner'] = $caseOwner;
433 $this->_defaults['case_owner'] = $caseOwner;
434 }
435 }
436 }
437
438 /**
439 * @return null
440 */
441 function getFormValues() {
442 return NULL;
443 }
444
445 /**
446 * Return a descriptive name for the page, used in wizard header
447 *
448 * @return string
449 * @access public
450 */
451 public function getTitle() {
452 return ts('Find Cases');
453 }
454 }
455