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 * the params that are sent to the query
47 protected $_queryParams;
50 * are we restricting ourselves to a single contact
55 protected $_single = FALSE;
58 * are we restricting ourselves to a single contact
63 protected $_limit = NULL;
66 * prefix for the controller
69 protected $_prefix = 'case_';
74 * processing needed for buildForm and later
79 function preProcess() {
80 $this->set('searchFormName', 'Search');
82 //check for civicase access.
83 if (!CRM_Case_BAO_Case
::accessCiviCase()) {
84 CRM_Core_Error
::fatal(ts('You are not authorized to access this page.'));
87 //validate case configuration.
88 $configured = CRM_Case_BAO_Case
::isCaseConfigured();
89 $this->assign('notConfigured', !$configured['configured']);
90 if (!$configured['configured']) {
95 * set the button names
97 $this->_searchButtonName
= $this->getButtonName('refresh');
98 $this->_actionButtonName
= $this->getButtonName('next', 'action');
100 $this->_done
= FALSE;
101 $this->defaults
= array();
104 * we allow the controller to set force/reset externally, useful when we are being
105 * driven by the wizard framework
108 $this->_reset
= CRM_Utils_Request
::retrieve('reset', 'Boolean', CRM_Core_DAO
::$_nullObject);
109 $this->_force
= CRM_Utils_Request
::retrieve('force', 'Boolean', $this, FALSE);
110 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive', $this);
111 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this, FALSE, 'search');
113 $this->assign('context', $this->_context
);
115 // get user submitted values
116 // get it from controller only if form has been submitted, else preProcess has set this
117 if (!empty($_POST) && !$this->controller
->isModal()) {
118 $this->_formValues
= $this->controller
->exportValues($this->_name
);
121 $this->_formValues
= $this->get('formValues');
124 if (empty($this->_formValues
)) {
125 if (isset($this->_ssID
)) {
126 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
131 $this->postProcess();
132 $this->set('force', 0);
136 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
137 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
138 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
143 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
144 $selector = new CRM_Case_Selector_Search($this->_queryParams
,
153 if ($this->_context
== 'user') {
154 $prefix = $this->_prefix
;
157 $this->assign("{$prefix}limit", $this->_limit
);
158 $this->assign("{$prefix}single", $this->_single
);
160 $controller = new CRM_Core_Selector_Controller($selector,
161 $this->get(CRM_Utils_Pager
::PAGE_ID
),
163 CRM_Core_Action
::VIEW
,
165 CRM_Core_Selector_Controller
::TRANSFER
,
168 $controller->setEmbedded(TRUE);
169 $controller->moveFromSessionToTemplate();
171 $this->assign('summary', $this->get('summary'));
181 function buildQuickForm() {
182 parent
::buildQuickForm();
183 $this->addElement('text',
185 ts('Client Name or Email'),
186 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')
189 CRM_Case_BAO_Query
::buildSearchForm($this);
191 $rows = $this->get('rows');
192 if (is_array($rows)) {
193 if (!$this->_single
) {
194 $this->addRowSelectors($rows);
197 $permission = CRM_Core_Permission
::getPermission();
199 $tasks = CRM_Case_Task
::permissionedTaskTitles($permission);
201 if (!empty($this->_formValues
['case_deleted'])) {
208 $this->addTaskMenu($tasks);
214 * The post processing of the form gets done here.
216 * Key things done during post processing are
217 * - check for reset or next request. if present, skip post procesing.
218 * - now check if user requested running a saved search, if so, then
219 * the form values associated with the saved search are used for searching.
220 * - if user has done a submit with new values the regular post submissing is
222 * The processing consists of using a Selector / Controller framework for getting the
230 function postProcess() {
236 $this->_formValues
= $this->controller
->exportValues($this->_name
);
237 $this->fixFormValues();
239 if (isset($this->_ssID
) && empty($_POST)) {
240 // if we are editing / running a saved search and the form has not been posted
241 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
244 //search for civicase
245 if (!$this->_force
) {
246 if (array_key_exists('case_owner', $this->_formValues
) && !$this->_formValues
['case_owner']) {
247 $this->_formValues
['case_owner'] = 0;
251 //only fetch own cases.
252 if (!CRM_Core_Permission
::check('access all cases and activities')) {
253 $this->_formValues
['case_owner'] = 2;
256 if (empty($this->_formValues
['case_deleted'])) {
257 $this->_formValues
['case_deleted'] = 0;
259 CRM_Core_BAO_CustomValue
::fixFieldValueOfTypeMemo($this->_formValues
);
261 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
263 $this->set('formValues', $this->_formValues
);
264 $this->set('queryParams', $this->_queryParams
);
266 $buttonName = $this->controller
->getButtonName();
267 if ($buttonName == $this->_actionButtonName
) {
268 // check actionName and if next, then do not repeat a search, since we are going to the next page
270 // hack, make sure we reset the task values
271 $stateMachine = $this->controller
->getStateMachine();
272 $formName = $stateMachine->getTaskFormName();
273 $this->controller
->resetPage($formName);
278 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
279 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
280 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
284 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
286 $selector = new CRM_Case_Selector_Search($this->_queryParams
,
293 $selector->setKey($this->controller
->_key
);
296 if ($this->_context
== 'user') {
297 $prefix = $this->_prefix
;
300 $this->assign("{$prefix}limit", $this->_limit
);
301 $this->assign("{$prefix}single", $this->_single
);
303 $controller = new CRM_Core_Selector_Controller($selector,
304 $this->get(CRM_Utils_Pager
::PAGE_ID
),
306 CRM_Core_Action
::VIEW
,
308 CRM_Core_Selector_Controller
::SESSION
,
311 $controller->setEmbedded(TRUE);
313 $query = &$selector->getQuery();
314 if ($this->_context
== 'user') {
315 $query->setSkipPermission(TRUE);
321 * This function is used to add the rules (mainly global rules) for form.
322 * All local rules are added near the element
328 function addRules() {
329 $this->addFormRule(array('CRM_Case_Form_Search', 'formRule'));
333 * global validation rules for the form
335 * @param array $fields posted values of the form
337 * @internal param array $errors list of errors to be posted back to the form
343 static function formRule($fields) {
346 if (!empty($errors)) {
354 * Set the default form values
358 * @return array the default array reference
360 function setDefaultValues() {
362 $defaults = $this->_formValues
;
366 function fixFormValues() {
367 if (!$this->_force
) {
371 $caseStatus = CRM_Utils_Request
::retrieve('status', 'Positive',
372 CRM_Core_DAO
::$_nullObject
375 $this->_formValues
['case_status_id'] = $caseStatus;
376 $this->_defaults
['case_status_id'] = $caseStatus;
378 $caseType = CRM_Utils_Request
::retrieve('type', 'Positive',
379 CRM_Core_DAO
::$_nullObject
382 $this->_formValues
['case_type_id'][$caseType] = 1;
383 $this->_defaults
['case_type_id'][$caseType] = 1;
386 $caseFromDate = CRM_Utils_Request
::retrieve('pstart', 'Date',
387 CRM_Core_DAO
::$_nullObject
390 list($date) = CRM_Utils_Date
::setDateDefaults($caseFromDate);
391 $this->_formValues
['case_start_date_low'] = $date;
392 $this->_defaults
['case_start_date_low'] = $date;
395 $caseToDate = CRM_Utils_Request
::retrieve('pend', 'Date',
396 CRM_Core_DAO
::$_nullObject
399 list($date) = CRM_Utils_Date
::setDateDefaults($caseToDate);
400 $this->_formValues
['case_start_date_high'] = $date;
401 $this->_defaults
['case_start_date_high'] = $date;
404 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
406 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
408 $this->_formValues
['contact_id'] = $cid;
409 list($display, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($cid);
410 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid,
413 // also assign individual mode to the template
414 $this->_single
= TRUE;
418 // First, if "all" is stored in the session, default to all cases, otherwise default to no selection.
419 $session = CRM_Core_Session
::singleton();
420 if (CRM_Utils_Request
::retrieve('all', 'Positive', $session)) {
421 $this->_formValues
['case_owner'] = 1;
422 $this->_defaults
['case_owner'] = 1;
425 $this->_formValues
['case_owner'] = 0;
426 $this->_defaults
['case_owner'] = 0;
429 // Now if case_owner is set in the url/post, use that instead.
430 $caseOwner = CRM_Utils_Request
::retrieve('case_owner', 'Positive',
431 CRM_Core_DAO
::$_nullObject
434 $this->_formValues
['case_owner'] = $caseOwner;
435 $this->_defaults
['case_owner'] = $caseOwner;
443 function getFormValues() {
448 * Return a descriptive name for the page, used in wizard header
453 public function getTitle() {
454 return ts('Find Cases');