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 print button
63 protected $_printButtonName;
66 * name of action button
71 protected $_actionButtonName;
74 * form values that we will be using
82 * the params that are sent to the query
87 protected $_queryParams;
90 * have we already done this search
98 * are we restricting ourselves to a single contact
103 protected $_single = FALSE;
106 * are we restricting ourselves to a single contact
111 protected $_limit = NULL;
114 * what context are we being invoked from
119 protected $_context = NULL;
122 * prefix for the controller
125 protected $_prefix = 'case_';
127 protected $_defaults;
130 * processing needed for buildForm and later
135 function preProcess() {
136 $this->set('searchFormName', 'Search');
138 // js for changing activity status
139 CRM_Core_Resources
::singleton()->addScriptFile('civicrm', 'templates/CRM/Case/Form/ActivityChangeStatus.js');
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.'));
146 //validate case configuration.
147 $configured = CRM_Case_BAO_Case
::isCaseConfigured();
148 $this->assign('notConfigured', !$configured['configured']);
149 if (!$configured['configured']) {
154 * set the button names
156 $this->_searchButtonName
= $this->getButtonName('refresh');
157 $this->_printButtonName
= $this->getButtonName('next', 'print');
158 $this->_actionButtonName
= $this->getButtonName('next', 'action');
160 $this->_done
= FALSE;
161 $this->defaults
= array();
164 * we allow the controller to set force/reset externally, useful when we are being
165 * driven by the wizard framework
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');
173 $this->assign('context', $this->_context
);
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
);
181 $this->_formValues
= $this->get('formValues');
184 if (empty($this->_formValues
)) {
185 if (isset($this->_ssID
)) {
186 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
191 $this->postProcess();
192 $this->set('force', 0);
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
)
203 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
204 $selector = new CRM_Case_Selector_Search($this->_queryParams
,
213 if ($this->_context
== 'user') {
214 $prefix = $this->_prefix
;
217 $this->assign("{$prefix}limit", $this->_limit
);
218 $this->assign("{$prefix}single", $this->_single
);
220 $controller = new CRM_Core_Selector_Controller($selector,
221 $this->get(CRM_Utils_Pager
::PAGE_ID
),
223 CRM_Core_Action
::VIEW
,
225 CRM_Core_Selector_Controller
::TRANSFER
,
228 $controller->setEmbedded(TRUE);
229 $controller->moveFromSessionToTemplate();
231 $this->assign('summary', $this->get('summary'));
241 function buildQuickForm() {
242 $this->addElement('text',
244 ts('Client Name or Email'),
245 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')
248 CRM_Case_BAO_Query
::buildSearchForm($this);
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
254 $rows = $this->get('rows');
255 if (is_array($rows)) {
257 if (!$this->_single
) {
258 $this->addElement('checkbox',
262 array('onclick' => "toggleTaskAction( true ); return toggleCheckboxVals('mark_x_',this);")
265 foreach ($rows as $row) {
266 $this->addElement('checkbox', $row['checkbox'],
268 array('onclick' => "toggleTaskAction( true ); return checkSelectedBox('" . $row['checkbox'] . "');")
273 $total = $cancel = 0;
275 $permission = CRM_Core_Permission
::getPermission();
277 $tasks = array('' => ts('- actions -')) + CRM_Case_Task
::permissionedTaskTitles($permission);
279 if (CRM_Utils_Array
::value('case_deleted', $this->_formValues
)) {
286 $this->add('select', 'task', ts('Actions:') . ' ', $tasks);
287 $this->add('submit', $this->_actionButtonName
, ts('Go'),
289 'class' => 'form-submit',
291 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 0);",
295 $this->add('submit', $this->_printButtonName
, ts('Print'),
297 'class' => 'form-submit',
298 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 1);",
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 );"));
308 $this->addButtons(array(
311 'name' => ts('Search'),
318 * The post processing of the form gets done here.
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
326 * The processing consists of using a Selector / Controller framework for getting the
334 function postProcess() {
340 $this->_formValues
= $this->controller
->exportValues($this->_name
);
341 $this->fixFormValues();
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
);
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;
355 //only fetch own cases.
356 if (!CRM_Core_Permission
::check('access all cases and activities')) {
357 $this->_formValues
['case_owner'] = 2;
360 if (!CRM_Utils_Array
::value('case_deleted', $this->_formValues
)) {
361 $this->_formValues
['case_deleted'] = 0;
363 CRM_Core_BAO_CustomValue
::fixFieldValueOfTypeMemo($this->_formValues
);
365 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
367 $this->set('formValues', $this->_formValues
);
368 $this->set('queryParams', $this->_queryParams
);
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
374 // hack, make sure we reset the task values
375 $stateMachine = $this->controller
->getStateMachine();
376 $formName = $stateMachine->getTaskFormName();
377 $this->controller
->resetPage($formName);
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
)
388 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
390 $selector = new CRM_Case_Selector_Search($this->_queryParams
,
397 $selector->setKey($this->controller
->_key
);
400 if ($this->_context
== 'user') {
401 $prefix = $this->_prefix
;
404 $this->assign("{$prefix}limit", $this->_limit
);
405 $this->assign("{$prefix}single", $this->_single
);
407 $controller = new CRM_Core_Selector_Controller($selector,
408 $this->get(CRM_Utils_Pager
::PAGE_ID
),
410 CRM_Core_Action
::VIEW
,
412 CRM_Core_Selector_Controller
::SESSION
,
415 $controller->setEmbedded(TRUE);
417 $query = &$selector->getQuery();
418 if ($this->_context
== 'user') {
419 $query->setSkipPermission(TRUE);
425 * This function is used to add the rules (mainly global rules) for form.
426 * All local rules are added near the element
432 function addRules() {
433 $this->addFormRule(array('CRM_Case_Form_Search', 'formRule'));
437 * global validation rules for the form
439 * @param array $fields posted values of the form
440 * @param array $errors list of errors to be posted back to the form
446 static function formRule($fields) {
449 if (!empty($errors)) {
457 * Set the default form values
461 * @return array the default array reference
463 function setDefaultValues() {
465 $defaults = $this->_formValues
;
469 function fixFormValues() {
470 if (!$this->_force
) {
474 $caseStatus = CRM_Utils_Request
::retrieve('status', 'Positive',
475 CRM_Core_DAO
::$_nullObject
478 $this->_formValues
['case_status_id'] = $caseStatus;
479 $this->_defaults
['case_status_id'] = $caseStatus;
481 $caseType = CRM_Utils_Request
::retrieve('type', 'Positive',
482 CRM_Core_DAO
::$_nullObject
485 $this->_formValues
['case_type_id'][$caseType] = 1;
486 $this->_defaults
['case_type_id'][$caseType] = 1;
489 $caseFromDate = CRM_Utils_Request
::retrieve('pstart', 'Date',
490 CRM_Core_DAO
::$_nullObject
493 list($date) = CRM_Utils_Date
::setDateDefaults($caseFromDate);
494 $this->_formValues
['case_start_date_low'] = $date;
495 $this->_defaults
['case_start_date_low'] = $date;
498 $caseToDate = CRM_Utils_Request
::retrieve('pend', 'Date',
499 CRM_Core_DAO
::$_nullObject
502 list($date) = CRM_Utils_Date
::setDateDefaults($caseToDate);
503 $this->_formValues
['case_start_date_high'] = $date;
504 $this->_defaults
['case_start_date_high'] = $date;
507 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
509 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
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,
516 // also assign individual mode to the template
517 $this->_single
= TRUE;
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;
528 $this->_formValues
['case_owner'] = 0;
529 $this->_defaults
['case_owner'] = 0;
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
537 $this->_formValues
['case_owner'] = $caseOwner;
538 $this->_defaults
['case_owner'] = $caseOwner;
543 function getFormValues() {
548 * Return a descriptive name for the page, used in wizard header
553 public function getTitle() {
554 return ts('Find Cases');