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