3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
46 protected $_queryParams;
49 * Are we restricting ourselves to a single contact
53 protected $_single = FALSE;
56 * Are we restricting ourselves to a single contact
60 protected $_limit = NULL;
63 * Prefix for the controller
65 protected $_prefix = 'case_';
70 * Processing needed for buildForm and later.
74 public function preProcess() {
75 $this->set('searchFormName', 'Search');
77 //check for civicase access.
78 if (!CRM_Case_BAO_Case
::accessCiviCase()) {
79 CRM_Core_Error
::fatal(ts('You are not authorized to access this page.'));
82 //validate case configuration.
83 $configured = CRM_Case_BAO_Case
::isCaseConfigured();
84 $this->assign('notConfigured', !$configured['configured']);
85 if (!$configured['configured']) {
90 * set the button names
92 $this->_searchButtonName
= $this->getButtonName('refresh');
93 $this->_actionButtonName
= $this->getButtonName('next', 'action');
96 $this->defaults
= array();
99 * we allow the controller to set force/reset externally, useful when we are being
100 * driven by the wizard framework
103 $this->_reset
= CRM_Utils_Request
::retrieve('reset', 'Boolean', CRM_Core_DAO
::$_nullObject);
104 $this->_force
= CRM_Utils_Request
::retrieve('force', 'Boolean', $this, FALSE);
105 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive', $this);
106 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this, FALSE, 'search');
108 $this->assign('context', $this->_context
);
110 // get user submitted values
111 // get it from controller only if form has been submitted, else preProcess has set this
112 if (!empty($_POST) && !$this->controller
->isModal()) {
113 $this->_formValues
= $this->controller
->exportValues($this->_name
);
116 $this->_formValues
= $this->get('formValues');
119 if (empty($this->_formValues
)) {
120 if (isset($this->_ssID
)) {
121 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
126 $this->postProcess();
127 $this->set('force', 0);
131 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
132 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
133 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
137 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
138 $selector = new CRM_Case_Selector_Search($this->_queryParams
,
147 if ($this->_context
== 'user') {
148 $prefix = $this->_prefix
;
151 $this->assign("{$prefix}limit", $this->_limit
);
152 $this->assign("{$prefix}single", $this->_single
);
154 $controller = new CRM_Core_Selector_Controller($selector,
155 $this->get(CRM_Utils_Pager
::PAGE_ID
),
157 CRM_Core_Action
::VIEW
,
159 CRM_Core_Selector_Controller
::TRANSFER
,
162 $controller->setEmbedded(TRUE);
163 $controller->moveFromSessionToTemplate();
165 $this->assign('summary', $this->get('summary'));
169 * Build the form object.
174 public function buildQuickForm() {
175 parent
::buildQuickForm();
176 $this->addElement('text',
178 ts('Client Name or Email'),
179 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')
182 CRM_Case_BAO_Query
::buildSearchForm($this);
184 $rows = $this->get('rows');
185 if (is_array($rows)) {
186 if (!$this->_single
) {
187 $this->addRowSelectors($rows);
190 $permission = CRM_Core_Permission
::getPermission();
192 $tasks = CRM_Case_Task
::permissionedTaskTitles($permission);
194 if (!empty($this->_formValues
['case_deleted'])) {
201 $this->addTaskMenu($tasks);
207 * The post processing of the form gets done here.
209 * Key things done during post processing are
210 * - check for reset or next request. if present, skip post procesing.
211 * - now check if user requested running a saved search, if so, then
212 * the form values associated with the saved search are used for searching.
213 * - if user has done a submit with new values the regular post submissing is
215 * The processing consists of using a Selector / Controller framework for getting the
222 public function postProcess() {
228 $this->_formValues
= $this->controller
->exportValues($this->_name
);
229 $this->fixFormValues();
231 if (isset($this->_ssID
) && empty($_POST)) {
232 // if we are editing / running a saved search and the form has not been posted
233 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
236 //search for civicase
237 if (!$this->_force
) {
238 if (array_key_exists('case_owner', $this->_formValues
) && !$this->_formValues
['case_owner']) {
239 $this->_formValues
['case_owner'] = 0;
243 //only fetch own cases.
244 if (!CRM_Core_Permission
::check('access all cases and activities')) {
245 $this->_formValues
['case_owner'] = 2;
248 if (empty($this->_formValues
['case_deleted'])) {
249 $this->_formValues
['case_deleted'] = 0;
251 CRM_Core_BAO_CustomValue
::fixFieldValueOfTypeMemo($this->_formValues
);
253 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
255 $this->set('formValues', $this->_formValues
);
256 $this->set('queryParams', $this->_queryParams
);
258 $buttonName = $this->controller
->getButtonName();
259 if ($buttonName == $this->_actionButtonName
) {
260 // check actionName and if next, then do not repeat a search, since we are going to the next page
262 // hack, make sure we reset the task values
263 $stateMachine = $this->controller
->getStateMachine();
264 $formName = $stateMachine->getTaskFormName();
265 $this->controller
->resetPage($formName);
270 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
271 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
272 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
276 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
278 $selector = new CRM_Case_Selector_Search($this->_queryParams
,
285 $selector->setKey($this->controller
->_key
);
288 if ($this->_context
== 'user') {
289 $prefix = $this->_prefix
;
292 $this->assign("{$prefix}limit", $this->_limit
);
293 $this->assign("{$prefix}single", $this->_single
);
295 $controller = new CRM_Core_Selector_Controller($selector,
296 $this->get(CRM_Utils_Pager
::PAGE_ID
),
298 CRM_Core_Action
::VIEW
,
300 CRM_Core_Selector_Controller
::SESSION
,
303 $controller->setEmbedded(TRUE);
305 $query = &$selector->getQuery();
306 if ($this->_context
== 'user') {
307 $query->setSkipPermission(TRUE);
313 * add the rules (mainly global rules) for form.
314 * All local rules are added near the element
319 public function addRules() {
320 $this->addFormRule(array('CRM_Case_Form_Search', 'formRule'));
324 * Global validation rules for the form.
326 * @param array $fields
327 * Posted values of the form.
331 public static function formRule($fields) {
334 if (!empty($errors)) {
342 * Set the default form values.
346 * the default array reference
348 public function setDefaultValues() {
350 $defaults = $this->_formValues
;
354 public function fixFormValues() {
355 if (!$this->_force
) {
359 $caseStatus = CRM_Utils_Request
::retrieve('status', 'Positive',
360 CRM_Core_DAO
::$_nullObject
363 $this->_formValues
['case_status_id'] = $caseStatus;
364 $this->_defaults
['case_status_id'] = $caseStatus;
366 $caseType = CRM_Utils_Request
::retrieve('type', 'Positive',
367 CRM_Core_DAO
::$_nullObject
370 $this->_formValues
['case_type_id'][$caseType] = 1;
371 $this->_defaults
['case_type_id'][$caseType] = 1;
374 $caseFromDate = CRM_Utils_Request
::retrieve('pstart', 'Date',
375 CRM_Core_DAO
::$_nullObject
378 list($date) = CRM_Utils_Date
::setDateDefaults($caseFromDate);
379 $this->_formValues
['case_start_date_low'] = $date;
380 $this->_defaults
['case_start_date_low'] = $date;
383 $caseToDate = CRM_Utils_Request
::retrieve('pend', 'Date',
384 CRM_Core_DAO
::$_nullObject
387 list($date) = CRM_Utils_Date
::setDateDefaults($caseToDate);
388 $this->_formValues
['case_start_date_high'] = $date;
389 $this->_defaults
['case_start_date_high'] = $date;
392 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
394 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
396 $this->_formValues
['contact_id'] = $cid;
397 list($display, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($cid);
398 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid,
401 // also assign individual mode to the template
402 $this->_single
= TRUE;
406 // First, if "all" is stored in the session, default to all cases, otherwise default to no selection.
407 $session = CRM_Core_Session
::singleton();
408 if (CRM_Utils_Request
::retrieve('all', 'Positive', $session)) {
409 $this->_formValues
['case_owner'] = 1;
410 $this->_defaults
['case_owner'] = 1;
413 $this->_formValues
['case_owner'] = 0;
414 $this->_defaults
['case_owner'] = 0;
417 // Now if case_owner is set in the url/post, use that instead.
418 $caseOwner = CRM_Utils_Request
::retrieve('case_owner', 'Positive',
419 CRM_Core_DAO
::$_nullObject
422 $this->_formValues
['case_owner'] = $caseOwner;
423 $this->_defaults
['case_owner'] = $caseOwner;
431 public function getFormValues() {
436 * Return a descriptive name for the page, used in wizard header
440 public function getTitle() {
441 return ts('Find Cases');