3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This file is for Case search.
21 class CRM_Case_Form_Search
extends CRM_Core_Form_Search
{
24 * The params that are sent to the query
28 protected $_queryParams;
31 * Are we restricting ourselves to a single contact
35 protected $_single = FALSE;
38 * Are we restricting ourselves to a single contact
42 protected $_limit = NULL;
45 * Prefix for the controller
48 protected $_prefix = 'case_';
53 public function getDefaultEntity() {
58 * Processing needed for buildForm and later.
60 public function preProcess() {
61 $this->set('searchFormName', 'Search');
63 //check for civicase access.
64 if (!CRM_Case_BAO_Case
::accessCiviCase()) {
65 CRM_Core_Error
::statusBounce(ts('You are not authorized to access this page.'));
68 //validate case configuration.
69 $configured = CRM_Case_BAO_Case
::isCaseConfigured();
70 $this->assign('notConfigured', !$configured['configured']);
71 if (!$configured['configured']) {
76 * set the button names
78 $this->_actionButtonName
= $this->getButtonName('next', 'action');
81 $this->sortNameOnly
= TRUE;
85 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
86 $selector = new CRM_Case_Selector_Search($this->_queryParams
,
95 if ($this->_context
== 'user') {
96 $prefix = $this->_prefix
;
99 $this->assign("{$prefix}limit", $this->_limit
);
100 $this->assign("{$prefix}single", $this->_single
);
102 $controller = new CRM_Core_Selector_Controller($selector,
103 $this->get(CRM_Utils_Pager
::PAGE_ID
),
105 CRM_Core_Action
::VIEW
,
107 CRM_Core_Selector_Controller
::TRANSFER
,
110 $controller->setEmbedded(TRUE);
111 $controller->moveFromSessionToTemplate();
113 $this->assign('summary', $this->get('summary'));
117 * Build the form object.
119 public function buildQuickForm() {
120 parent
::buildQuickForm();
121 $this->addSortNameField();
123 CRM_Case_BAO_Query
::buildSearchForm($this);
125 $rows = $this->get('rows');
126 if (is_array($rows)) {
127 if (!$this->_single
) {
128 $this->addRowSelectors($rows);
131 $tasks = CRM_Case_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission());
133 if (!empty($this->_formValues
['case_deleted'])) {
134 unset($tasks[CRM_Case_Task
::TASK_DELETE
]);
137 unset($tasks[CRM_Case_Task
::RESTORE_CASES
]);
140 $this->addTaskMenu($tasks);
146 * Get the label for the sortName field if email searching is on.
148 * (email searching is a setting under search preferences).
152 protected function getSortNameLabelWithEmail() {
153 return ts('Client Name or Email');
157 * Get the label for the sortName field if email searching is off.
159 * (email searching is a setting under search preferences).
163 protected function getSortNameLabelWithOutEmail() {
164 return ts('Client Name');
168 * The post processing of the form gets done here.
170 * Key things done during post processing are
171 * - check for reset or next request. if present, skip post procesing.
172 * - now check if user requested running a saved search, if so, then
173 * the form values associated with the saved search are used for searching.
174 * - if user has done a submit with new values the regular post submissing is
176 * The processing consists of using a Selector / Controller framework for getting the
179 public function postProcess() {
185 $this->setFormValues();
186 // @todo - stop changing formValues - respect submitted form values, change a working array.
187 $this->fixFormValues();
188 if (isset($this->_ssID
) && empty($_POST)) {
189 // if we are editing / running a saved search and the form has not been posted
190 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
193 //search for civicase
194 if (!$this->_force
) {
195 // @todo - stop changing formValues - respect submitted form values, change a working array.
196 if (array_key_exists('case_owner', $this->_formValues
) && !$this->_formValues
['case_owner']) {
197 $this->_formValues
['case_owner'] = 0;
201 // @todo - stop changing formValues - respect submitted form values, change a working array.
202 if (empty($this->_formValues
['case_deleted'])) {
203 $this->_formValues
['case_deleted'] = 0;
205 // @todo - stop changing formValues - respect submitted form values, change a working array.
206 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
208 // @todo - stop changing formValues - respect submitted form values, change a working array.
209 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
211 $this->set('queryParams', $this->_queryParams
);
213 $buttonName = $this->controller
->getButtonName();
214 if ($buttonName == $this->_actionButtonName
) {
215 // check actionName and if next, then do not repeat a search, since we are going to the next page
217 // hack, make sure we reset the task values
218 $stateMachine = $this->controller
->getStateMachine();
219 $formName = $stateMachine->getTaskFormName();
220 $this->controller
->resetPage($formName);
224 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
226 $selector = new CRM_Case_Selector_Search($this->_queryParams
,
233 $selector->setKey($this->controller
->_key
);
236 if ($this->_context
== 'user') {
237 $prefix = $this->_prefix
;
240 $this->assign("{$prefix}limit", $this->_limit
);
241 $this->assign("{$prefix}single", $this->_single
);
243 $controller = new CRM_Core_Selector_Controller($selector,
244 $this->get(CRM_Utils_Pager
::PAGE_ID
),
246 CRM_Core_Action
::VIEW
,
248 CRM_Core_Selector_Controller
::SESSION
,
251 $controller->setEmbedded(TRUE);
253 $query = &$selector->getQuery();
254 if ($this->_context
== 'user') {
255 $query->setSkipPermission(TRUE);
260 public function fixFormValues() {
261 if (!$this->_force
) {
265 $caseStatus = CRM_Utils_Request
::retrieve('status', 'Positive');
267 $this->_formValues
['case_status_id'] = $caseStatus;
268 $this->_defaults
['case_status_id'] = $caseStatus;
270 $caseType = CRM_Utils_Request
::retrieve('type', 'Positive');
272 $this->_formValues
['case_type_id'] = (array) $caseType;
273 $this->_defaults
['case_type_id'] = (array) $caseType;
276 $caseFromDate = CRM_Utils_Request
::retrieve('pstart', 'Date');
278 list($date) = CRM_Utils_Date
::setDateDefaults($caseFromDate);
279 $this->_formValues
['case_start_date_low'] = $date;
280 $this->_defaults
['case_start_date_low'] = $date;
283 $caseToDate = CRM_Utils_Request
::retrieve('pend', 'Date');
285 list($date) = CRM_Utils_Date
::setDateDefaults($caseToDate);
286 $this->_formValues
['case_start_date_high'] = $date;
287 $this->_defaults
['case_start_date_high'] = $date;
290 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
292 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
294 $this->_formValues
['contact_id'] = $cid;
295 list($display, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($cid);
296 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid,
299 // also assign individual mode to the template
300 $this->_single
= TRUE;
304 // First, if "all" is stored in the session, default to all cases, otherwise default to no selection.
305 $session = CRM_Core_Session
::singleton();
306 if (CRM_Utils_Request
::retrieve('all', 'Positive', $session)) {
307 $this->_formValues
['case_owner'] = 1;
308 $this->_defaults
['case_owner'] = 1;
311 $this->_formValues
['case_owner'] = 0;
312 $this->_defaults
['case_owner'] = 0;
315 // Now if case_owner is set in the url/post, use that instead.
316 $caseOwner = CRM_Utils_Request
::retrieve('case_owner', 'Positive');
318 $this->_formValues
['case_owner'] = $caseOwner;
319 $this->_defaults
['case_owner'] = $caseOwner;
325 * Return a descriptive name for the page, used in wizard header
329 public function getTitle() {
330 return ts('Find Cases');
334 * Set the metadata for the form.
336 * @throws \CiviCRM_API3_Exception
338 protected function setSearchMetadata() {
339 $this->addSearchFieldMetadata(['Case' => CRM_Case_BAO_Query
::getSearchFieldMetadata()]);