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