Merge pull request #18276 from civicrm/5.29
[civicrm-core.git] / CRM / Case / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This file is for Case search.
20 */
21 class CRM_Case_Form_Search extends CRM_Core_Form_Search {
22
23 /**
24 * The params that are sent to the query
25 *
26 * @var array
27 */
28 protected $_queryParams;
29
30 /**
31 * Are we restricting ourselves to a single contact
32 *
33 * @var bool
34 */
35 protected $_single = FALSE;
36
37 /**
38 * Are we restricting ourselves to a single contact
39 *
40 * @var bool
41 */
42 protected $_limit = NULL;
43
44 /**
45 * Prefix for the controller
46 * @var string
47 */
48 protected $_prefix = 'case_';
49
50 /**
51 * @return string
52 */
53 public function getDefaultEntity() {
54 return 'Case';
55 }
56
57 /**
58 * Processing needed for buildForm and later.
59 */
60 public function preProcess() {
61 $this->set('searchFormName', 'Search');
62
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.'));
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 */
78 $this->_actionButtonName = $this->getButtonName('next', 'action');
79
80 $this->_done = FALSE;
81 $this->sortNameOnly = TRUE;
82
83 parent::preProcess();
84
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),
104 $this->getSortID(),
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 /**
117 * Build the form object.
118 */
119 public function buildQuickForm() {
120 parent::buildQuickForm();
121 $this->addSortNameField();
122
123 CRM_Case_BAO_Query::buildSearchForm($this);
124
125 $rows = $this->get('rows');
126 if (is_array($rows)) {
127 if (!$this->_single) {
128 $this->addRowSelectors($rows);
129 }
130
131 $tasks = CRM_Case_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission());
132
133 if (!empty($this->_formValues['case_deleted'])) {
134 unset($tasks[CRM_Case_Task::TASK_DELETE]);
135 }
136 else {
137 unset($tasks[CRM_Case_Task::RESTORE_CASES]);
138 }
139
140 $this->addTaskMenu($tasks);
141 }
142
143 }
144
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
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.
178 */
179 public function postProcess() {
180 if ($this->_done) {
181 return;
182 }
183
184 $this->_done = TRUE;
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);
191 }
192
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;
198 }
199 }
200
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;
204 }
205
206 // @todo - stop changing formValues - respect submitted form values, change a working array.
207 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
208
209 $this->set('queryParams', $this->_queryParams);
210
211 $buttonName = $this->controller->getButtonName();
212 if ($buttonName == $this->_actionButtonName) {
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
216 $stateMachine = $this->controller->getStateMachine();
217 $formName = $stateMachine->getTaskFormName();
218 $this->controller->resetPage($formName);
219 return;
220 }
221
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),
243 $this->getSortID(),
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
258 public function fixFormValues() {
259 if (!$this->_force) {
260 return;
261 }
262
263 $caseStatus = CRM_Utils_Request::retrieve('status', 'Positive');
264 if ($caseStatus) {
265 $this->_formValues['case_status_id'] = $caseStatus;
266 $this->_defaults['case_status_id'] = $caseStatus;
267 }
268 $caseType = CRM_Utils_Request::retrieve('type', 'Positive');
269 if ($caseType) {
270 $this->_formValues['case_type_id'] = (array) $caseType;
271 $this->_defaults['case_type_id'] = (array) $caseType;
272 }
273
274 $caseFromDate = CRM_Utils_Request::retrieve('pstart', 'Date');
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
281 $caseToDate = CRM_Utils_Request::retrieve('pend', 'Date');
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 {
302 // First, if "all" is stored in the session, default to all cases, otherwise default to no selection.
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.
314 $caseOwner = CRM_Utils_Request::retrieve('case_owner', 'Positive');
315 if ($caseOwner) {
316 $this->_formValues['case_owner'] = $caseOwner;
317 $this->_defaults['case_owner'] = $caseOwner;
318 }
319 }
320 }
321
322 /**
323 * Return a descriptive name for the page, used in wizard header
324 *
325 * @return string
326 */
327 public function getTitle() {
328 return ts('Find Cases');
329 }
330
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
340 }