3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This file is for Case search
39 class CRM_Case_Form_Search
extends CRM_Core_Form_Search
{
42 * Are we forced to run a search
50 * name of search button
55 protected $_searchButtonName;
58 * name of action button
63 protected $_actionButtonName;
66 * form values that we will be using
74 * the params that are sent to the query
79 protected $_queryParams;
82 * have we already done this search
90 * are we restricting ourselves to a single contact
95 protected $_single = FALSE;
98 * are we restricting ourselves to a single contact
103 protected $_limit = NULL;
106 * what context are we being invoked from
111 protected $_context = NULL;
114 * prefix for the controller
117 protected $_prefix = 'case_';
119 protected $_defaults;
122 * processing needed for buildForm and later
127 function preProcess() {
128 $this->set('searchFormName', 'Search');
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.'));
135 //validate case configuration.
136 $configured = CRM_Case_BAO_Case
::isCaseConfigured();
137 $this->assign('notConfigured', !$configured['configured']);
138 if (!$configured['configured']) {
143 * set the button names
145 $this->_searchButtonName
= $this->getButtonName('refresh');
146 $this->_actionButtonName
= $this->getButtonName('next', 'action');
148 $this->_done
= FALSE;
149 $this->defaults
= array();
152 * we allow the controller to set force/reset externally, useful when we are being
153 * driven by the wizard framework
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');
161 $this->assign('context', $this->_context
);
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
);
169 $this->_formValues
= $this->get('formValues');
172 if (empty($this->_formValues
)) {
173 if (isset($this->_ssID
)) {
174 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
179 $this->postProcess();
180 $this->set('force', 0);
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
)
191 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
192 $selector = new CRM_Case_Selector_Search($this->_queryParams
,
201 if ($this->_context
== 'user') {
202 $prefix = $this->_prefix
;
205 $this->assign("{$prefix}limit", $this->_limit
);
206 $this->assign("{$prefix}single", $this->_single
);
208 $controller = new CRM_Core_Selector_Controller($selector,
209 $this->get(CRM_Utils_Pager
::PAGE_ID
),
211 CRM_Core_Action
::VIEW
,
213 CRM_Core_Selector_Controller
::TRANSFER
,
216 $controller->setEmbedded(TRUE);
217 $controller->moveFromSessionToTemplate();
219 $this->assign('summary', $this->get('summary'));
229 function buildQuickForm() {
230 parent
::buildQuickForm();
231 $this->addElement('text',
233 ts('Client Name or Email'),
234 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')
237 CRM_Case_BAO_Query
::buildSearchForm($this);
240 * add form checkboxes for each row. This is needed out here to conform to QF protocol
241 * of all elements being declared in builQuickForm
243 $rows = $this->get('rows');
244 if (is_array($rows)) {
245 if (!$this->_single
) {
246 $this->addElement('checkbox',
250 array('onclick' => "toggleTaskAction( true );", 'class' => 'select-rows')
253 foreach ($rows as $row) {
254 $this->addElement('checkbox', $row['checkbox'],
256 array('onclick' => "toggleTaskAction( true );", 'class' => 'select-row')
261 $total = $cancel = 0;
263 $permission = CRM_Core_Permission
::getPermission();
265 $tasks = array('' => ts('- actions -')) + CRM_Case_Task
::permissionedTaskTitles($permission);
267 if (!empty($this->_formValues
['case_deleted'])) {
274 $this->add('select', 'task', ts('Actions:') . ' ', $tasks);
275 $this->add('submit', $this->_actionButtonName
, ts('Go'),
277 'class' => 'form-submit',
279 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 0);",
283 // need to perform tasks on all or selected items ? using radio_ts(task selection) for it
284 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked'));
285 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all', array('class' => 'select-rows', 'onclick' => $this->getName() . ".toggleSelect.checked = false; toggleTaskAction( true );"));
291 * The post processing of the form gets done here.
293 * Key things done during post processing are
294 * - check for reset or next request. if present, skip post procesing.
295 * - now check if user requested running a saved search, if so, then
296 * the form values associated with the saved search are used for searching.
297 * - if user has done a submit with new values the regular post submissing is
299 * The processing consists of using a Selector / Controller framework for getting the
307 function postProcess() {
313 $this->_formValues
= $this->controller
->exportValues($this->_name
);
314 $this->fixFormValues();
316 if (isset($this->_ssID
) && empty($_POST)) {
317 // if we are editing / running a saved search and the form has not been posted
318 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
321 //search for civicase
322 if (!$this->_force
) {
323 if (array_key_exists('case_owner', $this->_formValues
) && !$this->_formValues
['case_owner']) {
324 $this->_formValues
['case_owner'] = 0;
328 //only fetch own cases.
329 if (!CRM_Core_Permission
::check('access all cases and activities')) {
330 $this->_formValues
['case_owner'] = 2;
333 if (empty($this->_formValues
['case_deleted'])) {
334 $this->_formValues
['case_deleted'] = 0;
336 CRM_Core_BAO_CustomValue
::fixFieldValueOfTypeMemo($this->_formValues
);
338 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
340 $this->set('formValues', $this->_formValues
);
341 $this->set('queryParams', $this->_queryParams
);
343 $buttonName = $this->controller
->getButtonName();
344 if ($buttonName == $this->_actionButtonName
) {
345 // check actionName and if next, then do not repeat a search, since we are going to the next page
347 // hack, make sure we reset the task values
348 $stateMachine = $this->controller
->getStateMachine();
349 $formName = $stateMachine->getTaskFormName();
350 $this->controller
->resetPage($formName);
355 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
356 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
357 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
361 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
363 $selector = new CRM_Case_Selector_Search($this->_queryParams
,
370 $selector->setKey($this->controller
->_key
);
373 if ($this->_context
== 'user') {
374 $prefix = $this->_prefix
;
377 $this->assign("{$prefix}limit", $this->_limit
);
378 $this->assign("{$prefix}single", $this->_single
);
380 $controller = new CRM_Core_Selector_Controller($selector,
381 $this->get(CRM_Utils_Pager
::PAGE_ID
),
383 CRM_Core_Action
::VIEW
,
385 CRM_Core_Selector_Controller
::SESSION
,
388 $controller->setEmbedded(TRUE);
390 $query = &$selector->getQuery();
391 if ($this->_context
== 'user') {
392 $query->setSkipPermission(TRUE);
398 * This function is used to add the rules (mainly global rules) for form.
399 * All local rules are added near the element
405 function addRules() {
406 $this->addFormRule(array('CRM_Case_Form_Search', 'formRule'));
410 * global validation rules for the form
412 * @param array $fields posted values of the form
414 * @internal param array $errors list of errors to be posted back to the form
420 static function formRule($fields) {
423 if (!empty($errors)) {
431 * Set the default form values
435 * @return array the default array reference
437 function setDefaultValues() {
439 $defaults = $this->_formValues
;
443 function fixFormValues() {
444 if (!$this->_force
) {
448 $caseStatus = CRM_Utils_Request
::retrieve('status', 'Positive',
449 CRM_Core_DAO
::$_nullObject
452 $this->_formValues
['case_status_id'] = $caseStatus;
453 $this->_defaults
['case_status_id'] = $caseStatus;
455 $caseType = CRM_Utils_Request
::retrieve('type', 'Positive',
456 CRM_Core_DAO
::$_nullObject
459 $this->_formValues
['case_type_id'][$caseType] = 1;
460 $this->_defaults
['case_type_id'][$caseType] = 1;
463 $caseFromDate = CRM_Utils_Request
::retrieve('pstart', 'Date',
464 CRM_Core_DAO
::$_nullObject
467 list($date) = CRM_Utils_Date
::setDateDefaults($caseFromDate);
468 $this->_formValues
['case_start_date_low'] = $date;
469 $this->_defaults
['case_start_date_low'] = $date;
472 $caseToDate = CRM_Utils_Request
::retrieve('pend', 'Date',
473 CRM_Core_DAO
::$_nullObject
476 list($date) = CRM_Utils_Date
::setDateDefaults($caseToDate);
477 $this->_formValues
['case_start_date_high'] = $date;
478 $this->_defaults
['case_start_date_high'] = $date;
481 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
483 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
485 $this->_formValues
['contact_id'] = $cid;
486 list($display, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($cid);
487 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid,
490 // also assign individual mode to the template
491 $this->_single
= TRUE;
495 // First, if "all" is stored in the session, default to all cases, otherwise default to no selection.
496 $session = CRM_Core_Session
::singleton();
497 if (CRM_Utils_Request
::retrieve('all', 'Positive', $session)) {
498 $this->_formValues
['case_owner'] = 1;
499 $this->_defaults
['case_owner'] = 1;
502 $this->_formValues
['case_owner'] = 0;
503 $this->_defaults
['case_owner'] = 0;
506 // Now if case_owner is set in the url/post, use that instead.
507 $caseOwner = CRM_Utils_Request
::retrieve('case_owner', 'Positive',
508 CRM_Core_DAO
::$_nullObject
511 $this->_formValues
['case_owner'] = $caseOwner;
512 $this->_defaults
['case_owner'] = $caseOwner;
517 function getFormValues() {
522 * Return a descriptive name for the page, used in wizard header
527 public function getTitle() {
528 return ts('Find Cases');