Merge pull request #16123 from civicrm/5.21
[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 */
78 $this->_searchButtonName = $this->getButtonName('refresh');
6a488035
TO
79 $this->_actionButtonName = $this->getButtonName('next', 'action');
80
81 $this->_done = FALSE;
c82067be 82 $this->sortNameOnly = TRUE;
6a488035 83
11ac6a2b 84 parent::preProcess();
6a488035 85
6a488035
TO
86 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
87 $selector = new CRM_Case_Selector_Search($this->_queryParams,
88 $this->_action,
89 NULL,
90 $this->_single,
91 $this->_limit,
92 $this->_context
93 );
94
95 $prefix = NULL;
96 if ($this->_context == 'user') {
97 $prefix = $this->_prefix;
98 }
99
100 $this->assign("{$prefix}limit", $this->_limit);
101 $this->assign("{$prefix}single", $this->_single);
102
103 $controller = new CRM_Core_Selector_Controller($selector,
104 $this->get(CRM_Utils_Pager::PAGE_ID),
b5c63125 105 $this->getSortID(),
6a488035
TO
106 CRM_Core_Action::VIEW,
107 $this,
108 CRM_Core_Selector_Controller::TRANSFER,
109 $prefix
110 );
111 $controller->setEmbedded(TRUE);
112 $controller->moveFromSessionToTemplate();
113
114 $this->assign('summary', $this->get('summary'));
115 }
116
117 /**
fe482240 118 * Build the form object.
6a488035 119 */
00be9182 120 public function buildQuickForm() {
3efb5b86 121 parent::buildQuickForm();
e597fc33 122 $this->addSortNameField();
6a488035
TO
123
124 CRM_Case_BAO_Query::buildSearchForm($this);
125
6a488035
TO
126 $rows = $this->get('rows');
127 if (is_array($rows)) {
6a488035 128 if (!$this->_single) {
8d36b801 129 $this->addRowSelectors($rows);
6a488035
TO
130 }
131
047838bc 132 $tasks = CRM_Case_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission());
6a488035 133
a7488080 134 if (!empty($this->_formValues['case_deleted'])) {
7388b48a 135 unset($tasks[CRM_Case_Task::TASK_DELETE]);
6a488035
TO
136 }
137 else {
047838bc 138 unset($tasks[CRM_Case_Task::RESTORE_CASES]);
6a488035
TO
139 }
140
34197a55 141 $this->addTaskMenu($tasks);
6a488035
TO
142 }
143
6a488035
TO
144 }
145
e597fc33
DG
146 /**
147 * Get the label for the sortName field if email searching is on.
148 *
149 * (email searching is a setting under search preferences).
150 *
151 * @return string
152 */
153 protected function getSortNameLabelWithEmail() {
154 return ts('Client Name or Email');
155 }
156
157 /**
158 * Get the label for the sortName field if email searching is off.
159 *
160 * (email searching is a setting under search preferences).
161 *
162 * @return string
163 */
164 protected function getSortNameLabelWithOutEmail() {
165 return ts('Client Name');
166 }
167
6a488035
TO
168 /**
169 * The post processing of the form gets done here.
170 *
171 * Key things done during post processing are
172 * - check for reset or next request. if present, skip post procesing.
173 * - now check if user requested running a saved search, if so, then
174 * the form values associated with the saved search are used for searching.
175 * - if user has done a submit with new values the regular post submissing is
176 * done.
177 * The processing consists of using a Selector / Controller framework for getting the
178 * search results.
6a488035 179 */
00be9182 180 public function postProcess() {
6a488035
TO
181 if ($this->_done) {
182 return;
183 }
184
185 $this->_done = TRUE;
b62aa188 186 $this->setFormValues();
11ac6a2b 187 // @todo - stop changing formValues - respect submitted form values, change a working array.
6a488035 188 $this->fixFormValues();
6a488035
TO
189 if (isset($this->_ssID) && empty($_POST)) {
190 // if we are editing / running a saved search and the form has not been posted
191 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
192 }
193
194 //search for civicase
195 if (!$this->_force) {
11ac6a2b 196 // @todo - stop changing formValues - respect submitted form values, change a working array.
6a488035
TO
197 if (array_key_exists('case_owner', $this->_formValues) && !$this->_formValues['case_owner']) {
198 $this->_formValues['case_owner'] = 0;
199 }
200 }
201
11ac6a2b 202 // @todo - stop changing formValues - respect submitted form values, change a working array.
a7488080 203 if (empty($this->_formValues['case_deleted'])) {
6a488035
TO
204 $this->_formValues['case_deleted'] = 0;
205 }
11ac6a2b 206 // @todo - stop changing formValues - respect submitted form values, change a working array.
c94d39fd 207 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
6a488035 208
11ac6a2b 209 // @todo - stop changing formValues - respect submitted form values, change a working array.
6a488035
TO
210 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
211
6a488035
TO
212 $this->set('queryParams', $this->_queryParams);
213
214 $buttonName = $this->controller->getButtonName();
e341bbee 215 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
216 // check actionName and if next, then do not repeat a search, since we are going to the next page
217
218 // hack, make sure we reset the task values
95ea77ec 219 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
220 $formName = $stateMachine->getTaskFormName();
221 $this->controller->resetPage($formName);
222 return;
223 }
224
6a488035
TO
225 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
226
227 $selector = new CRM_Case_Selector_Search($this->_queryParams,
228 $this->_action,
229 NULL,
230 $this->_single,
231 $this->_limit,
232 $this->_context
233 );
234 $selector->setKey($this->controller->_key);
235
236 $prefix = NULL;
237 if ($this->_context == 'user') {
238 $prefix = $this->_prefix;
239 }
240
241 $this->assign("{$prefix}limit", $this->_limit);
242 $this->assign("{$prefix}single", $this->_single);
243
244 $controller = new CRM_Core_Selector_Controller($selector,
245 $this->get(CRM_Utils_Pager::PAGE_ID),
b5c63125 246 $this->getSortID(),
6a488035
TO
247 CRM_Core_Action::VIEW,
248 $this,
249 CRM_Core_Selector_Controller::SESSION,
250 $prefix
251 );
252 $controller->setEmbedded(TRUE);
253
254 $query = &$selector->getQuery();
255 if ($this->_context == 'user') {
256 $query->setSkipPermission(TRUE);
257 }
258 $controller->run();
259 }
260
00be9182 261 public function fixFormValues() {
6a488035
TO
262 if (!$this->_force) {
263 return;
264 }
265
1273d77c 266 $caseStatus = CRM_Utils_Request::retrieve('status', 'Positive');
6a488035
TO
267 if ($caseStatus) {
268 $this->_formValues['case_status_id'] = $caseStatus;
269 $this->_defaults['case_status_id'] = $caseStatus;
270 }
1273d77c 271 $caseType = CRM_Utils_Request::retrieve('type', 'Positive');
6a488035 272 if ($caseType) {
d5e5f843
CW
273 $this->_formValues['case_type_id'] = (array) $caseType;
274 $this->_defaults['case_type_id'] = (array) $caseType;
6a488035
TO
275 }
276
1273d77c 277 $caseFromDate = CRM_Utils_Request::retrieve('pstart', 'Date');
6a488035
TO
278 if ($caseFromDate) {
279 list($date) = CRM_Utils_Date::setDateDefaults($caseFromDate);
280 $this->_formValues['case_start_date_low'] = $date;
281 $this->_defaults['case_start_date_low'] = $date;
282 }
283
1273d77c 284 $caseToDate = CRM_Utils_Request::retrieve('pend', 'Date');
6a488035
TO
285 if ($caseToDate) {
286 list($date) = CRM_Utils_Date::setDateDefaults($caseToDate);
287 $this->_formValues['case_start_date_high'] = $date;
288 $this->_defaults['case_start_date_high'] = $date;
289 }
290
291 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
292 if ($cid) {
293 $cid = CRM_Utils_Type::escape($cid, 'Integer');
294 if ($cid > 0) {
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,
298 'sort_name'
299 );
300 // also assign individual mode to the template
301 $this->_single = TRUE;
302 }
303 }
304 else {
e547f744 305 // First, if "all" is stored in the session, default to all cases, otherwise default to no selection.
6a488035
TO
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;
310 }
311 else {
312 $this->_formValues['case_owner'] = 0;
313 $this->_defaults['case_owner'] = 0;
314 }
315
316 // Now if case_owner is set in the url/post, use that instead.
1273d77c 317 $caseOwner = CRM_Utils_Request::retrieve('case_owner', 'Positive');
6a488035
TO
318 if ($caseOwner) {
319 $this->_formValues['case_owner'] = $caseOwner;
320 $this->_defaults['case_owner'] = $caseOwner;
321 }
322 }
323 }
324
6a488035
TO
325 /**
326 * Return a descriptive name for the page, used in wizard header
327 *
328 * @return string
6a488035
TO
329 */
330 public function getTitle() {
331 return ts('Find Cases');
332 }
96025800 333
91d66ce8
SL
334 /**
335 * Set the metadata for the form.
336 *
337 * @throws \CiviCRM_API3_Exception
338 */
339 protected function setSearchMetadata() {
340 $this->addSearchFieldMetadata(['Case' => CRM_Case_BAO_Query::getSearchFieldMetadata()]);
341 }
342
6a488035 343}