Merge pull request #18276 from civicrm/5.29
[civicrm-core.git] / CRM / Case / Form / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
e70a7fc0 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
df371444 19 * This file is for Case search.
6a488035 20 */
3efb5b86 21class CRM_Case_Form_Search extends CRM_Core_Form_Search {
6a488035 22
6a488035 23 /**
100fef9d 24 * The params that are sent to the query
6a488035
TO
25 *
26 * @var array
6a488035
TO
27 */
28 protected $_queryParams;
29
6a488035 30 /**
100fef9d 31 * Are we restricting ourselves to a single contact
6a488035 32 *
b67daa72 33 * @var bool
6a488035
TO
34 */
35 protected $_single = FALSE;
36
37 /**
100fef9d 38 * Are we restricting ourselves to a single contact
6a488035 39 *
b67daa72 40 * @var bool
6a488035
TO
41 */
42 protected $_limit = NULL;
43
6a488035 44 /**
100fef9d 45 * Prefix for the controller
b67daa72 46 * @var string
6a488035
TO
47 */
48 protected $_prefix = 'case_';
49
b62aa188
MD
50 /**
51 * @return string
52 */
53 public function getDefaultEntity() {
54 return 'Case';
55 }
56
6a488035 57 /**
fe482240 58 * Processing needed for buildForm and later.
6a488035 59 */
00be9182 60 public function preProcess() {
6a488035
TO
61 $this->set('searchFormName', 'Search');
62
63 //check for civicase access.
64 if (!CRM_Case_BAO_Case::accessCiviCase()) {
beb414cc 65 CRM_Core_Error::statusBounce(ts('You are not authorized to access this page.'));
6a488035
TO
66 }
67
68 //validate case configuration.
69 $configured = CRM_Case_BAO_Case::isCaseConfigured();
70 $this->assign('notConfigured', !$configured['configured']);
71 if (!$configured['configured']) {
72 return;
73 }
74
75 /**
76 * set the button names
77 */
6a488035
TO
78 $this->_actionButtonName = $this->getButtonName('next', 'action');
79
80 $this->_done = FALSE;
c82067be 81 $this->sortNameOnly = TRUE;
6a488035 82
11ac6a2b 83 parent::preProcess();
6a488035 84
6a488035
TO
85 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
86 $selector = new CRM_Case_Selector_Search($this->_queryParams,
87 $this->_action,
88 NULL,
89 $this->_single,
90 $this->_limit,
91 $this->_context
92 );
93
94 $prefix = NULL;
95 if ($this->_context == 'user') {
96 $prefix = $this->_prefix;
97 }
98
99 $this->assign("{$prefix}limit", $this->_limit);
100 $this->assign("{$prefix}single", $this->_single);
101
102 $controller = new CRM_Core_Selector_Controller($selector,
103 $this->get(CRM_Utils_Pager::PAGE_ID),
b5c63125 104 $this->getSortID(),
6a488035
TO
105 CRM_Core_Action::VIEW,
106 $this,
107 CRM_Core_Selector_Controller::TRANSFER,
108 $prefix
109 );
110 $controller->setEmbedded(TRUE);
111 $controller->moveFromSessionToTemplate();
112
113 $this->assign('summary', $this->get('summary'));
114 }
115
116 /**
fe482240 117 * Build the form object.
6a488035 118 */
00be9182 119 public function buildQuickForm() {
3efb5b86 120 parent::buildQuickForm();
e597fc33 121 $this->addSortNameField();
6a488035
TO
122
123 CRM_Case_BAO_Query::buildSearchForm($this);
124
6a488035
TO
125 $rows = $this->get('rows');
126 if (is_array($rows)) {
6a488035 127 if (!$this->_single) {
8d36b801 128 $this->addRowSelectors($rows);
6a488035
TO
129 }
130
047838bc 131 $tasks = CRM_Case_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission());
6a488035 132
a7488080 133 if (!empty($this->_formValues['case_deleted'])) {
7388b48a 134 unset($tasks[CRM_Case_Task::TASK_DELETE]);
6a488035
TO
135 }
136 else {
047838bc 137 unset($tasks[CRM_Case_Task::RESTORE_CASES]);
6a488035
TO
138 }
139
34197a55 140 $this->addTaskMenu($tasks);
6a488035
TO
141 }
142
6a488035
TO
143 }
144
e597fc33
DG
145 /**
146 * Get the label for the sortName field if email searching is on.
147 *
148 * (email searching is a setting under search preferences).
149 *
150 * @return string
151 */
152 protected function getSortNameLabelWithEmail() {
153 return ts('Client Name or Email');
154 }
155
156 /**
157 * Get the label for the sortName field if email searching is off.
158 *
159 * (email searching is a setting under search preferences).
160 *
161 * @return string
162 */
163 protected function getSortNameLabelWithOutEmail() {
164 return ts('Client Name');
165 }
166
6a488035
TO
167 /**
168 * The post processing of the form gets done here.
169 *
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
175 * done.
176 * The processing consists of using a Selector / Controller framework for getting the
177 * search results.
6a488035 178 */
00be9182 179 public function postProcess() {
6a488035
TO
180 if ($this->_done) {
181 return;
182 }
183
184 $this->_done = TRUE;
b62aa188 185 $this->setFormValues();
11ac6a2b 186 // @todo - stop changing formValues - respect submitted form values, change a working array.
6a488035 187 $this->fixFormValues();
6a488035
TO
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);
191 }
192
193 //search for civicase
194 if (!$this->_force) {
11ac6a2b 195 // @todo - stop changing formValues - respect submitted form values, change a working array.
6a488035
TO
196 if (array_key_exists('case_owner', $this->_formValues) && !$this->_formValues['case_owner']) {
197 $this->_formValues['case_owner'] = 0;
198 }
199 }
200
11ac6a2b 201 // @todo - stop changing formValues - respect submitted form values, change a working array.
a7488080 202 if (empty($this->_formValues['case_deleted'])) {
6a488035
TO
203 $this->_formValues['case_deleted'] = 0;
204 }
6a488035 205
11ac6a2b 206 // @todo - stop changing formValues - respect submitted form values, change a working array.
6a488035
TO
207 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
208
6a488035
TO
209 $this->set('queryParams', $this->_queryParams);
210
211 $buttonName = $this->controller->getButtonName();
e341bbee 212 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
213 // check actionName and if next, then do not repeat a search, since we are going to the next page
214
215 // hack, make sure we reset the task values
95ea77ec 216 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
217 $formName = $stateMachine->getTaskFormName();
218 $this->controller->resetPage($formName);
219 return;
220 }
221
6a488035
TO
222 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
223
224 $selector = new CRM_Case_Selector_Search($this->_queryParams,
225 $this->_action,
226 NULL,
227 $this->_single,
228 $this->_limit,
229 $this->_context
230 );
231 $selector->setKey($this->controller->_key);
232
233 $prefix = NULL;
234 if ($this->_context == 'user') {
235 $prefix = $this->_prefix;
236 }
237
238 $this->assign("{$prefix}limit", $this->_limit);
239 $this->assign("{$prefix}single", $this->_single);
240
241 $controller = new CRM_Core_Selector_Controller($selector,
242 $this->get(CRM_Utils_Pager::PAGE_ID),
b5c63125 243 $this->getSortID(),
6a488035
TO
244 CRM_Core_Action::VIEW,
245 $this,
246 CRM_Core_Selector_Controller::SESSION,
247 $prefix
248 );
249 $controller->setEmbedded(TRUE);
250
251 $query = &$selector->getQuery();
252 if ($this->_context == 'user') {
253 $query->setSkipPermission(TRUE);
254 }
255 $controller->run();
256 }
257
00be9182 258 public function fixFormValues() {
6a488035
TO
259 if (!$this->_force) {
260 return;
261 }
262
1273d77c 263 $caseStatus = CRM_Utils_Request::retrieve('status', 'Positive');
6a488035
TO
264 if ($caseStatus) {
265 $this->_formValues['case_status_id'] = $caseStatus;
266 $this->_defaults['case_status_id'] = $caseStatus;
267 }
1273d77c 268 $caseType = CRM_Utils_Request::retrieve('type', 'Positive');
6a488035 269 if ($caseType) {
d5e5f843
CW
270 $this->_formValues['case_type_id'] = (array) $caseType;
271 $this->_defaults['case_type_id'] = (array) $caseType;
6a488035
TO
272 }
273
1273d77c 274 $caseFromDate = CRM_Utils_Request::retrieve('pstart', 'Date');
6a488035
TO
275 if ($caseFromDate) {
276 list($date) = CRM_Utils_Date::setDateDefaults($caseFromDate);
277 $this->_formValues['case_start_date_low'] = $date;
278 $this->_defaults['case_start_date_low'] = $date;
279 }
280
1273d77c 281 $caseToDate = CRM_Utils_Request::retrieve('pend', 'Date');
6a488035
TO
282 if ($caseToDate) {
283 list($date) = CRM_Utils_Date::setDateDefaults($caseToDate);
284 $this->_formValues['case_start_date_high'] = $date;
285 $this->_defaults['case_start_date_high'] = $date;
286 }
287
288 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
289 if ($cid) {
290 $cid = CRM_Utils_Type::escape($cid, 'Integer');
291 if ($cid > 0) {
292 $this->_formValues['contact_id'] = $cid;
293 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
294 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
295 'sort_name'
296 );
297 // also assign individual mode to the template
298 $this->_single = TRUE;
299 }
300 }
301 else {
e547f744 302 // First, if "all" is stored in the session, default to all cases, otherwise default to no selection.
6a488035
TO
303 $session = CRM_Core_Session::singleton();
304 if (CRM_Utils_Request::retrieve('all', 'Positive', $session)) {
305 $this->_formValues['case_owner'] = 1;
306 $this->_defaults['case_owner'] = 1;
307 }
308 else {
309 $this->_formValues['case_owner'] = 0;
310 $this->_defaults['case_owner'] = 0;
311 }
312
313 // Now if case_owner is set in the url/post, use that instead.
1273d77c 314 $caseOwner = CRM_Utils_Request::retrieve('case_owner', 'Positive');
6a488035
TO
315 if ($caseOwner) {
316 $this->_formValues['case_owner'] = $caseOwner;
317 $this->_defaults['case_owner'] = $caseOwner;
318 }
319 }
320 }
321
6a488035
TO
322 /**
323 * Return a descriptive name for the page, used in wizard header
324 *
325 * @return string
6a488035
TO
326 */
327 public function getTitle() {
328 return ts('Find Cases');
329 }
96025800 330
91d66ce8
SL
331 /**
332 * Set the metadata for the form.
333 *
334 * @throws \CiviCRM_API3_Exception
335 */
336 protected function setSearchMetadata() {
337 $this->addSearchFieldMetadata(['Case' => CRM_Case_BAO_Query::getSearchFieldMetadata()]);
338 }
339
6a488035 340}