Merge pull request #353 from eileenmcnaughton/trunk-kill-eval
[civicrm-core.git] / CRM / Case / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 * $Id$
33 *
34 */
35
36 /**
37 * This file is for Case search
38 */
39 class CRM_Case_Form_Search extends CRM_Core_Form {
40
41 /**
42 * Are we forced to run a search
43 *
44 * @var int
45 * @access protected
46 */
47 protected $_force;
48
49 /**
50 * name of search button
51 *
52 * @var string
53 * @access protected
54 */
55 protected $_searchButtonName;
56
57 /**
58 * name of print button
59 *
60 * @var string
61 * @access protected
62 */
63 protected $_printButtonName;
64
65 /**
66 * name of action button
67 *
68 * @var string
69 * @access protected
70 */
71 protected $_actionButtonName;
72
73 /**
74 * form values that we will be using
75 *
76 * @var array
77 * @access protected
78 */
79 public $_formValues;
80
81 /**
82 * the params that are sent to the query
83 *
84 * @var array
85 * @access protected
86 */
87 protected $_queryParams;
88
89 /**
90 * have we already done this search
91 *
92 * @access protected
93 * @var boolean
94 */
95 protected $_done;
96
97 /**
98 * are we restricting ourselves to a single contact
99 *
100 * @access protected
101 * @var boolean
102 */
103 protected $_single = FALSE;
104
105 /**
106 * are we restricting ourselves to a single contact
107 *
108 * @access protected
109 * @var boolean
110 */
111 protected $_limit = NULL;
112
113 /**
114 * what context are we being invoked from
115 *
116 * @access protected
117 * @var string
118 */
119 protected $_context = NULL;
120
121 /**
122 * prefix for the controller
123 *
124 */
125 protected $_prefix = 'case_';
126
127 protected $_defaults;
128
129 /**
130 * processing needed for buildForm and later
131 *
132 * @return void
133 * @access public
134 */
135 function preProcess() {
136 $this->set('searchFormName', 'Search');
137
138 // js for changing activity status
139 CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'templates/CRM/Case/Form/ActivityChangeStatus.js');
140
141 //check for civicase access.
142 if (!CRM_Case_BAO_Case::accessCiviCase()) {
143 CRM_Core_Error::fatal(ts('You are not authorized to access this page.'));
144 }
145
146 //validate case configuration.
147 $configured = CRM_Case_BAO_Case::isCaseConfigured();
148 $this->assign('notConfigured', !$configured['configured']);
149 if (!$configured['configured']) {
150 return;
151 }
152
153 /**
154 * set the button names
155 */
156 $this->_searchButtonName = $this->getButtonName('refresh');
157 $this->_printButtonName = $this->getButtonName('next', 'print');
158 $this->_actionButtonName = $this->getButtonName('next', 'action');
159
160 $this->_done = FALSE;
161 $this->defaults = array();
162
163 /*
164 * we allow the controller to set force/reset externally, useful when we are being
165 * driven by the wizard framework
166 */
167
168 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject);
169 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
170 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
171 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
172
173 $this->assign('context', $this->_context);
174
175 // get user submitted values
176 // get it from controller only if form has been submitted, else preProcess has set this
177 if (!empty($_POST) && !$this->controller->isModal()) {
178 $this->_formValues = $this->controller->exportValues($this->_name);
179 }
180 else {
181 $this->_formValues = $this->get('formValues');
182 }
183
184 if (empty($this->_formValues)) {
185 if (isset($this->_ssID)) {
186 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
187 }
188 }
189
190 if ($this->_force) {
191 $this->postProcess();
192 $this->set('force', 0);
193 }
194
195 $sortID = NULL;
196 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
197 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
198 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
199 );
200 }
201
202
203 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
204 $selector = new CRM_Case_Selector_Search($this->_queryParams,
205 $this->_action,
206 NULL,
207 $this->_single,
208 $this->_limit,
209 $this->_context
210 );
211
212 $prefix = NULL;
213 if ($this->_context == 'user') {
214 $prefix = $this->_prefix;
215 }
216
217 $this->assign("{$prefix}limit", $this->_limit);
218 $this->assign("{$prefix}single", $this->_single);
219
220 $controller = new CRM_Core_Selector_Controller($selector,
221 $this->get(CRM_Utils_Pager::PAGE_ID),
222 $sortID,
223 CRM_Core_Action::VIEW,
224 $this,
225 CRM_Core_Selector_Controller::TRANSFER,
226 $prefix
227 );
228 $controller->setEmbedded(TRUE);
229 $controller->moveFromSessionToTemplate();
230
231 $this->assign('summary', $this->get('summary'));
232 }
233
234 /**
235 * Build the form
236 *
237 * @access public
238 *
239 * @return void
240 */
241 function buildQuickForm() {
242 $this->addElement('text',
243 'sort_name',
244 ts('Client Name or Email'),
245 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')
246 );
247
248 CRM_Case_BAO_Query::buildSearchForm($this);
249
250 /*
251 * add form checkboxes for each row. This is needed out here to conform to QF protocol
252 * of all elements being declared in builQuickForm
253 */
254 $rows = $this->get('rows');
255 if (is_array($rows)) {
256
257 if (!$this->_single) {
258 $this->addElement('checkbox',
259 'toggleSelect',
260 NULL,
261 NULL,
262 array('onclick' => "toggleTaskAction( true ); return toggleCheckboxVals('mark_x_',this);")
263 );
264
265 foreach ($rows as $row) {
266 $this->addElement('checkbox', $row['checkbox'],
267 NULL, NULL,
268 array('onclick' => "toggleTaskAction( true ); return checkSelectedBox('" . $row['checkbox'] . "');")
269 );
270 }
271 }
272
273 $total = $cancel = 0;
274
275 $permission = CRM_Core_Permission::getPermission();
276
277 $tasks = array('' => ts('- actions -')) + CRM_Case_Task::permissionedTaskTitles($permission);
278
279 if (CRM_Utils_Array::value('case_deleted', $this->_formValues)) {
280 unset($tasks[1]);
281 }
282 else {
283 unset($tasks[4]);
284 }
285
286 $this->add('select', 'task', ts('Actions:') . ' ', $tasks);
287 $this->add('submit', $this->_actionButtonName, ts('Go'),
288 array(
289 'class' => 'form-submit',
290 'id' => 'Go',
291 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 0);",
292 )
293 );
294
295 $this->add('submit', $this->_printButtonName, ts('Print'),
296 array(
297 'class' => 'form-submit',
298 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 1);",
299 )
300 );
301
302 // need to perform tasks on all or selected items ? using radio_ts(task selection) for it
303 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked'));
304 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all', array('onclick' => $this->getName() . ".toggleSelect.checked = false; toggleCheckboxVals('mark_x_',this); toggleTaskAction( true );"));
305 }
306
307 // add buttons
308 $this->addButtons(array(
309 array(
310 'type' => 'refresh',
311 'name' => ts('Search'),
312 'isDefault' => TRUE,
313 ),
314 ));
315 }
316
317 /**
318 * The post processing of the form gets done here.
319 *
320 * Key things done during post processing are
321 * - check for reset or next request. if present, skip post procesing.
322 * - now check if user requested running a saved search, if so, then
323 * the form values associated with the saved search are used for searching.
324 * - if user has done a submit with new values the regular post submissing is
325 * done.
326 * The processing consists of using a Selector / Controller framework for getting the
327 * search results.
328 *
329 * @param
330 *
331 * @return void
332 * @access public
333 */
334 function postProcess() {
335 if ($this->_done) {
336 return;
337 }
338
339 $this->_done = TRUE;
340 $this->_formValues = $this->controller->exportValues($this->_name);
341 $this->fixFormValues();
342
343 if (isset($this->_ssID) && empty($_POST)) {
344 // if we are editing / running a saved search and the form has not been posted
345 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
346 }
347
348 //search for civicase
349 if (!$this->_force) {
350 if (array_key_exists('case_owner', $this->_formValues) && !$this->_formValues['case_owner']) {
351 $this->_formValues['case_owner'] = 0;
352 }
353 }
354
355 //only fetch own cases.
356 if (!CRM_Core_Permission::check('access all cases and activities')) {
357 $this->_formValues['case_owner'] = 2;
358 }
359
360 if (!CRM_Utils_Array::value('case_deleted', $this->_formValues)) {
361 $this->_formValues['case_deleted'] = 0;
362 }
363 CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
364
365 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
366
367 $this->set('formValues', $this->_formValues);
368 $this->set('queryParams', $this->_queryParams);
369
370 $buttonName = $this->controller->getButtonName();
371 if ($buttonName == $this->_actionButtonName || $buttonName == $this->_printButtonName) {
372 // check actionName and if next, then do not repeat a search, since we are going to the next page
373
374 // hack, make sure we reset the task values
375 $stateMachine = &$this->controller->getStateMachine();
376 $formName = $stateMachine->getTaskFormName();
377 $this->controller->resetPage($formName);
378 return;
379 }
380
381 $sortID = NULL;
382 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
383 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
384 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
385 );
386 }
387
388 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
389
390 $selector = new CRM_Case_Selector_Search($this->_queryParams,
391 $this->_action,
392 NULL,
393 $this->_single,
394 $this->_limit,
395 $this->_context
396 );
397 $selector->setKey($this->controller->_key);
398
399 $prefix = NULL;
400 if ($this->_context == 'user') {
401 $prefix = $this->_prefix;
402 }
403
404 $this->assign("{$prefix}limit", $this->_limit);
405 $this->assign("{$prefix}single", $this->_single);
406
407 $controller = new CRM_Core_Selector_Controller($selector,
408 $this->get(CRM_Utils_Pager::PAGE_ID),
409 $sortID,
410 CRM_Core_Action::VIEW,
411 $this,
412 CRM_Core_Selector_Controller::SESSION,
413 $prefix
414 );
415 $controller->setEmbedded(TRUE);
416
417 $query = &$selector->getQuery();
418 if ($this->_context == 'user') {
419 $query->setSkipPermission(TRUE);
420 }
421 $controller->run();
422 }
423
424 /**
425 * This function is used to add the rules (mainly global rules) for form.
426 * All local rules are added near the element
427 *
428 * @return None
429 * @access public
430 * @see valid_date
431 */
432 function addRules() {
433 $this->addFormRule(array('CRM_Case_Form_Search', 'formRule'));
434 }
435
436 /**
437 * global validation rules for the form
438 *
439 * @param array $fields posted values of the form
440 * @param array $errors list of errors to be posted back to the form
441 *
442 * @return void
443 * @static
444 * @access public
445 */
446 static function formRule($fields) {
447 $errors = array();
448
449 if (!empty($errors)) {
450 return $errors;
451 }
452
453 return TRUE;
454 }
455
456 /**
457 * Set the default form values
458 *
459 * @access protected
460 *
461 * @return array the default array reference
462 */
463 function setDefaultValues() {
464 $defaults = array();
465 $defaults = $this->_formValues;
466 return $defaults;
467 }
468
469 function fixFormValues() {
470 if (!$this->_force) {
471 return;
472 }
473
474 $caseStatus = CRM_Utils_Request::retrieve('status', 'Positive',
475 CRM_Core_DAO::$_nullObject
476 );
477 if ($caseStatus) {
478 $this->_formValues['case_status_id'] = $caseStatus;
479 $this->_defaults['case_status_id'] = $caseStatus;
480 }
481 $caseType = CRM_Utils_Request::retrieve('type', 'Positive',
482 CRM_Core_DAO::$_nullObject
483 );
484 if ($caseType) {
485 $this->_formValues['case_type_id'][$caseType] = 1;
486 $this->_defaults['case_type_id'][$caseType] = 1;
487 }
488
489 $caseFromDate = CRM_Utils_Request::retrieve('pstart', 'Date',
490 CRM_Core_DAO::$_nullObject
491 );
492 if ($caseFromDate) {
493 list($date) = CRM_Utils_Date::setDateDefaults($caseFromDate);
494 $this->_formValues['case_start_date_low'] = $date;
495 $this->_defaults['case_start_date_low'] = $date;
496 }
497
498 $caseToDate = CRM_Utils_Request::retrieve('pend', 'Date',
499 CRM_Core_DAO::$_nullObject
500 );
501 if ($caseToDate) {
502 list($date) = CRM_Utils_Date::setDateDefaults($caseToDate);
503 $this->_formValues['case_start_date_high'] = $date;
504 $this->_defaults['case_start_date_high'] = $date;
505 }
506
507 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
508 if ($cid) {
509 $cid = CRM_Utils_Type::escape($cid, 'Integer');
510 if ($cid > 0) {
511 $this->_formValues['contact_id'] = $cid;
512 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
513 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
514 'sort_name'
515 );
516 // also assign individual mode to the template
517 $this->_single = TRUE;
518 }
519 }
520 else {
521 // First, if "all" is stored in the session, default to all cases, otherwise default to no selection.
522 $session = CRM_Core_Session::singleton();
523 if (CRM_Utils_Request::retrieve('all', 'Positive', $session)) {
524 $this->_formValues['case_owner'] = 1;
525 $this->_defaults['case_owner'] = 1;
526 }
527 else {
528 $this->_formValues['case_owner'] = 0;
529 $this->_defaults['case_owner'] = 0;
530 }
531
532 // Now if case_owner is set in the url/post, use that instead.
533 $caseOwner = CRM_Utils_Request::retrieve('case_owner', 'Positive',
534 CRM_Core_DAO::$_nullObject
535 );
536 if ($caseOwner) {
537 $this->_formValues['case_owner'] = $caseOwner;
538 $this->_defaults['case_owner'] = $caseOwner;
539 }
540 }
541 }
542
543 function getFormValues() {
544 return NULL;
545 }
546
547 /**
548 * Return a descriptive name for the page, used in wizard header
549 *
550 * @return string
551 * @access public
552 */
553 public function getTitle() {
554 return ts('Find Cases');
555 }
556 }
557