3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
37 * This file is for Case search
39 class CRM_Case_Form_Search
extends CRM_Core_Form
{
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 // js for changing activity status
131 CRM_Core_Resources
::singleton()->addScriptFile('civicrm', 'templates/CRM/Case/Form/ActivityChangeStatus.js');
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.'));
138 //validate case configuration.
139 $configured = CRM_Case_BAO_Case
::isCaseConfigured();
140 $this->assign('notConfigured', !$configured['configured']);
141 if (!$configured['configured']) {
146 * set the button names
148 $this->_searchButtonName
= $this->getButtonName('refresh');
149 $this->_actionButtonName
= $this->getButtonName('next', 'action');
151 $this->_done
= FALSE;
152 $this->defaults
= array();
155 * we allow the controller to set force/reset externally, useful when we are being
156 * driven by the wizard framework
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');
164 $this->assign('context', $this->_context
);
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
);
172 $this->_formValues
= $this->get('formValues');
175 if (empty($this->_formValues
)) {
176 if (isset($this->_ssID
)) {
177 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
182 $this->postProcess();
183 $this->set('force', 0);
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
)
194 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
195 $selector = new CRM_Case_Selector_Search($this->_queryParams
,
204 if ($this->_context
== 'user') {
205 $prefix = $this->_prefix
;
208 $this->assign("{$prefix}limit", $this->_limit
);
209 $this->assign("{$prefix}single", $this->_single
);
211 $controller = new CRM_Core_Selector_Controller($selector,
212 $this->get(CRM_Utils_Pager
::PAGE_ID
),
214 CRM_Core_Action
::VIEW
,
216 CRM_Core_Selector_Controller
::TRANSFER
,
219 $controller->setEmbedded(TRUE);
220 $controller->moveFromSessionToTemplate();
222 $this->assign('summary', $this->get('summary'));
232 function buildQuickForm() {
233 $this->addElement('text',
235 ts('Client Name or Email'),
236 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')
239 CRM_Case_BAO_Query
::buildSearchForm($this);
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
245 $rows = $this->get('rows');
246 if (is_array($rows)) {
247 CRM_Core_Resources
::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js');
248 CRM_Core_Resources
::singleton()->addScriptFile('civicrm', 'js/crm.searchForm.js');
249 if (!$this->_single
) {
250 $this->addElement('checkbox',
254 array('onclick' => "toggleTaskAction( true );", 'class' => 'select-rows')
257 foreach ($rows as $row) {
258 $this->addElement('checkbox', $row['checkbox'],
260 array('onclick' => "toggleTaskAction( true );", 'class' => 'select-row')
265 $total = $cancel = 0;
267 $permission = CRM_Core_Permission
::getPermission();
269 $tasks = array('' => ts('- actions -')) + CRM_Case_Task
::permissionedTaskTitles($permission);
271 if (!empty($this->_formValues
['case_deleted'])) {
278 $this->add('select', 'task', ts('Actions:') . ' ', $tasks);
279 $this->add('submit', $this->_actionButtonName
, ts('Go'),
281 'class' => 'form-submit',
283 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 0);",
287 // need to perform tasks on all or selected items ? using radio_ts(task selection) for it
288 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked'));
289 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all', array('class' => 'select-rows', 'onclick' => $this->getName() . ".toggleSelect.checked = false; toggleTaskAction( true );"));
293 $this->addButtons(array(
296 'name' => ts('Search'),
303 * The post processing of the form gets done here.
305 * Key things done during post processing are
306 * - check for reset or next request. if present, skip post procesing.
307 * - now check if user requested running a saved search, if so, then
308 * the form values associated with the saved search are used for searching.
309 * - if user has done a submit with new values the regular post submissing is
311 * The processing consists of using a Selector / Controller framework for getting the
319 function postProcess() {
325 $this->_formValues
= $this->controller
->exportValues($this->_name
);
326 $this->fixFormValues();
328 if (isset($this->_ssID
) && empty($_POST)) {
329 // if we are editing / running a saved search and the form has not been posted
330 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
333 //search for civicase
334 if (!$this->_force
) {
335 if (array_key_exists('case_owner', $this->_formValues
) && !$this->_formValues
['case_owner']) {
336 $this->_formValues
['case_owner'] = 0;
340 //only fetch own cases.
341 if (!CRM_Core_Permission
::check('access all cases and activities')) {
342 $this->_formValues
['case_owner'] = 2;
345 if (empty($this->_formValues
['case_deleted'])) {
346 $this->_formValues
['case_deleted'] = 0;
348 CRM_Core_BAO_CustomValue
::fixFieldValueOfTypeMemo($this->_formValues
);
350 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
352 $this->set('formValues', $this->_formValues
);
353 $this->set('queryParams', $this->_queryParams
);
355 $buttonName = $this->controller
->getButtonName();
356 if ($buttonName == $this->_actionButtonName
) {
357 // check actionName and if next, then do not repeat a search, since we are going to the next page
359 // hack, make sure we reset the task values
360 $stateMachine = $this->controller
->getStateMachine();
361 $formName = $stateMachine->getTaskFormName();
362 $this->controller
->resetPage($formName);
367 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
368 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
369 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
373 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
375 $selector = new CRM_Case_Selector_Search($this->_queryParams
,
382 $selector->setKey($this->controller
->_key
);
385 if ($this->_context
== 'user') {
386 $prefix = $this->_prefix
;
389 $this->assign("{$prefix}limit", $this->_limit
);
390 $this->assign("{$prefix}single", $this->_single
);
392 $controller = new CRM_Core_Selector_Controller($selector,
393 $this->get(CRM_Utils_Pager
::PAGE_ID
),
395 CRM_Core_Action
::VIEW
,
397 CRM_Core_Selector_Controller
::SESSION
,
400 $controller->setEmbedded(TRUE);
402 $query = &$selector->getQuery();
403 if ($this->_context
== 'user') {
404 $query->setSkipPermission(TRUE);
410 * This function is used to add the rules (mainly global rules) for form.
411 * All local rules are added near the element
417 function addRules() {
418 $this->addFormRule(array('CRM_Case_Form_Search', 'formRule'));
422 * global validation rules for the form
424 * @param array $fields posted values of the form
425 * @param array $errors list of errors to be posted back to the form
431 static function formRule($fields) {
434 if (!empty($errors)) {
442 * Set the default form values
446 * @return array the default array reference
448 function setDefaultValues() {
450 $defaults = $this->_formValues
;
454 function fixFormValues() {
455 if (!$this->_force
) {
459 $caseStatus = CRM_Utils_Request
::retrieve('status', 'Positive',
460 CRM_Core_DAO
::$_nullObject
463 $this->_formValues
['case_status_id'] = $caseStatus;
464 $this->_defaults
['case_status_id'] = $caseStatus;
466 $caseType = CRM_Utils_Request
::retrieve('type', 'Positive',
467 CRM_Core_DAO
::$_nullObject
470 $this->_formValues
['case_type_id'][$caseType] = 1;
471 $this->_defaults
['case_type_id'][$caseType] = 1;
474 $caseFromDate = CRM_Utils_Request
::retrieve('pstart', 'Date',
475 CRM_Core_DAO
::$_nullObject
478 list($date) = CRM_Utils_Date
::setDateDefaults($caseFromDate);
479 $this->_formValues
['case_start_date_low'] = $date;
480 $this->_defaults
['case_start_date_low'] = $date;
483 $caseToDate = CRM_Utils_Request
::retrieve('pend', 'Date',
484 CRM_Core_DAO
::$_nullObject
487 list($date) = CRM_Utils_Date
::setDateDefaults($caseToDate);
488 $this->_formValues
['case_start_date_high'] = $date;
489 $this->_defaults
['case_start_date_high'] = $date;
492 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
494 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
496 $this->_formValues
['contact_id'] = $cid;
497 list($display, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($cid);
498 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid,
501 // also assign individual mode to the template
502 $this->_single
= TRUE;
506 // First, if "all" is stored in the session, default to all cases, otherwise default to no selection.
507 $session = CRM_Core_Session
::singleton();
508 if (CRM_Utils_Request
::retrieve('all', 'Positive', $session)) {
509 $this->_formValues
['case_owner'] = 1;
510 $this->_defaults
['case_owner'] = 1;
513 $this->_formValues
['case_owner'] = 0;
514 $this->_defaults
['case_owner'] = 0;
517 // Now if case_owner is set in the url/post, use that instead.
518 $caseOwner = CRM_Utils_Request
::retrieve('case_owner', 'Positive',
519 CRM_Core_DAO
::$_nullObject
522 $this->_formValues
['case_owner'] = $caseOwner;
523 $this->_defaults
['case_owner'] = $caseOwner;
528 function getFormValues() {
533 * Return a descriptive name for the page, used in wizard header
538 public function getTitle() {
539 return ts('Find Cases');