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 // SearchFormName is deprecated & to be removed - the replacement is for the task to
62 // call $this->form->getSearchFormValues()
63 // A couple of extensions use it.
64 $this->set('searchFormName', 'Search');
66 //check for civicase access.
67 if (!CRM_Case_BAO_Case
::accessCiviCase()) {
68 CRM_Core_Error
::statusBounce(ts('You are not authorized to access this page.'));
71 //validate case configuration.
72 $configured = CRM_Case_BAO_Case
::isCaseConfigured();
73 $this->assign('notConfigured', !$configured['configured']);
74 if (!$configured['configured']) {
79 * set the button names
81 $this->_actionButtonName
= $this->getButtonName('next', 'action');
84 $this->sortNameOnly
= TRUE;
88 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
89 $selector = new CRM_Case_Selector_Search($this->_queryParams
,
98 if ($this->_context
== 'user') {
99 $prefix = $this->_prefix
;
102 $this->assign("{$prefix}limit", $this->_limit
);
103 $this->assign("{$prefix}single", $this->_single
);
105 $controller = new CRM_Core_Selector_Controller($selector,
106 $this->get(CRM_Utils_Pager
::PAGE_ID
),
108 CRM_Core_Action
::VIEW
,
110 CRM_Core_Selector_Controller
::TRANSFER
,
113 $controller->setEmbedded(TRUE);
114 $controller->moveFromSessionToTemplate();
116 $this->assign('summary', $this->get('summary'));
120 * Build the form object.
122 public function buildQuickForm() {
123 parent
::buildQuickForm();
124 $this->addSortNameField();
126 CRM_Case_BAO_Query
::buildSearchForm($this);
128 $rows = $this->get('rows');
129 if (is_array($rows)) {
130 if (!$this->_single
) {
131 $this->addRowSelectors($rows);
134 $tasks = CRM_Case_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission());
136 if (!empty($this->_formValues
['case_deleted'])) {
137 unset($tasks[CRM_Case_Task
::TASK_DELETE
]);
140 unset($tasks[CRM_Case_Task
::RESTORE_CASES
]);
143 $this->addTaskMenu($tasks);
149 * Get the label for the sortName field if email searching is on.
151 * (email searching is a setting under search preferences).
155 protected function getSortNameLabelWithEmail() {
156 return ts('Client Name or Email');
160 * Get the label for the sortName field if email searching is off.
162 * (email searching is a setting under search preferences).
166 protected function getSortNameLabelWithOutEmail() {
167 return ts('Client Name');
171 * The post processing of the form gets done here.
173 * Key things done during post processing are
174 * - check for reset or next request. if present, skip post procesing.
175 * - now check if user requested running a saved search, if so, then
176 * the form values associated with the saved search are used for searching.
177 * - if user has done a submit with new values the regular post submissing is
179 * The processing consists of using a Selector / Controller framework for getting the
182 public function postProcess() {
188 $this->setFormValues();
189 // @todo - stop changing formValues - respect submitted form values, change a working array.
190 $this->fixFormValues();
191 if (isset($this->_ssID
) && empty($_POST)) {
192 // if we are editing / running a saved search and the form has not been posted
193 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
196 //search for civicase
197 if (!$this->_force
) {
198 // @todo - stop changing formValues - respect submitted form values, change a working array.
199 if (array_key_exists('case_owner', $this->_formValues
) && !$this->_formValues
['case_owner']) {
200 $this->_formValues
['case_owner'] = 0;
204 // @todo - stop changing formValues - respect submitted form values, change a working array.
205 if (empty($this->_formValues
['case_deleted'])) {
206 $this->_formValues
['case_deleted'] = 0;
209 // @todo - stop changing formValues - respect submitted form values, change a working array.
210 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
212 $this->set('queryParams', $this->_queryParams
);
214 $buttonName = $this->controller
->getButtonName();
215 if ($buttonName == $this->_actionButtonName
) {
216 // check actionName and if next, then do not repeat a search, since we are going to the next page
218 // hack, make sure we reset the task values
219 $stateMachine = $this->controller
->getStateMachine();
220 $formName = $stateMachine->getTaskFormName();
221 $this->controller
->resetPage($formName);
225 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
227 $selector = new CRM_Case_Selector_Search($this->_queryParams
,
234 $selector->setKey($this->controller
->_key
);
237 if ($this->_context
== 'user') {
238 $prefix = $this->_prefix
;
241 $this->assign("{$prefix}limit", $this->_limit
);
242 $this->assign("{$prefix}single", $this->_single
);
244 $controller = new CRM_Core_Selector_Controller($selector,
245 $this->get(CRM_Utils_Pager
::PAGE_ID
),
247 CRM_Core_Action
::VIEW
,
249 CRM_Core_Selector_Controller
::SESSION
,
252 $controller->setEmbedded(TRUE);
254 $query = &$selector->getQuery();
255 if ($this->_context
== 'user') {
256 $query->setSkipPermission(TRUE);
261 public function fixFormValues() {
262 if (!$this->_force
) {
266 $caseStatus = CRM_Utils_Request
::retrieve('status', 'Positive');
268 $this->_formValues
['case_status_id'] = $caseStatus;
269 $this->_defaults
['case_status_id'] = $caseStatus;
271 $caseType = CRM_Utils_Request
::retrieve('type', 'Positive');
273 $this->_formValues
['case_type_id'] = (array) $caseType;
274 $this->_defaults
['case_type_id'] = (array) $caseType;
277 $caseFromDate = CRM_Utils_Request
::retrieve('pstart', 'Date');
279 list($date) = CRM_Utils_Date
::setDateDefaults($caseFromDate);
280 $this->_formValues
['case_start_date_low'] = $date;
281 $this->_defaults
['case_start_date_low'] = $date;
284 $caseToDate = CRM_Utils_Request
::retrieve('pend', 'Date');
286 list($date) = CRM_Utils_Date
::setDateDefaults($caseToDate);
287 $this->_formValues
['case_start_date_high'] = $date;
288 $this->_defaults
['case_start_date_high'] = $date;
291 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
293 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
295 $this->_formValues
['contact_id'] = $cid;
296 list($display, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($cid);
297 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid,
300 // also assign individual mode to the template
301 $this->_single
= TRUE;
305 // First, if "all" is stored in the session, default to all cases, otherwise default to no selection.
306 $session = CRM_Core_Session
::singleton();
307 if (CRM_Utils_Request
::retrieve('all', 'Positive', $session)) {
308 $this->_formValues
['case_owner'] = 1;
309 $this->_defaults
['case_owner'] = 1;
312 $this->_formValues
['case_owner'] = 0;
313 $this->_defaults
['case_owner'] = 0;
316 // Now if case_owner is set in the url/post, use that instead.
317 $caseOwner = CRM_Utils_Request
::retrieve('case_owner', 'Positive');
319 $this->_formValues
['case_owner'] = $caseOwner;
320 $this->_defaults
['case_owner'] = $caseOwner;
326 * Return a descriptive name for the page, used in wizard header
330 public function getTitle() {
331 return ts('Find Cases');
335 * Set the metadata for the form.
337 * @throws \CiviCRM_API3_Exception
339 protected function setSearchMetadata() {
340 $this->addSearchFieldMetadata(['Case' => CRM_Case_BAO_Query
::getSearchFieldMetadata()]);