Merge pull request #16001 from agileware/CIVICRM-1383
[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::fatal(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->_searchButtonName = $this->getButtonName('refresh');
79 $this->_actionButtonName = $this->getButtonName('next', 'action');
80
81 $this->_done = FALSE;
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 // @todo - stop changing formValues - respect submitted form values, change a working array.
206 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
207
208 // @todo - stop changing formValues - respect submitted form values, change a working array.
209 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
210
211 $this->set('queryParams', $this->_queryParams);
212
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
216
217 // hack, make sure we reset the task values
218 $stateMachine = $this->controller->getStateMachine();
219 $formName = $stateMachine->getTaskFormName();
220 $this->controller->resetPage($formName);
221 return;
222 }
223
224 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
225
226 $selector = new CRM_Case_Selector_Search($this->_queryParams,
227 $this->_action,
228 NULL,
229 $this->_single,
230 $this->_limit,
231 $this->_context
232 );
233 $selector->setKey($this->controller->_key);
234
235 $prefix = NULL;
236 if ($this->_context == 'user') {
237 $prefix = $this->_prefix;
238 }
239
240 $this->assign("{$prefix}limit", $this->_limit);
241 $this->assign("{$prefix}single", $this->_single);
242
243 $controller = new CRM_Core_Selector_Controller($selector,
244 $this->get(CRM_Utils_Pager::PAGE_ID),
245 $this->getSortID(),
246 CRM_Core_Action::VIEW,
247 $this,
248 CRM_Core_Selector_Controller::SESSION,
249 $prefix
250 );
251 $controller->setEmbedded(TRUE);
252
253 $query = &$selector->getQuery();
254 if ($this->_context == 'user') {
255 $query->setSkipPermission(TRUE);
256 }
257 $controller->run();
258 }
259
260 public function fixFormValues() {
261 if (!$this->_force) {
262 return;
263 }
264
265 $caseStatus = CRM_Utils_Request::retrieve('status', 'Positive');
266 if ($caseStatus) {
267 $this->_formValues['case_status_id'] = $caseStatus;
268 $this->_defaults['case_status_id'] = $caseStatus;
269 }
270 $caseType = CRM_Utils_Request::retrieve('type', 'Positive');
271 if ($caseType) {
272 $this->_formValues['case_type_id'] = (array) $caseType;
273 $this->_defaults['case_type_id'] = (array) $caseType;
274 }
275
276 $caseFromDate = CRM_Utils_Request::retrieve('pstart', 'Date');
277 if ($caseFromDate) {
278 list($date) = CRM_Utils_Date::setDateDefaults($caseFromDate);
279 $this->_formValues['case_start_date_low'] = $date;
280 $this->_defaults['case_start_date_low'] = $date;
281 }
282
283 $caseToDate = CRM_Utils_Request::retrieve('pend', 'Date');
284 if ($caseToDate) {
285 list($date) = CRM_Utils_Date::setDateDefaults($caseToDate);
286 $this->_formValues['case_start_date_high'] = $date;
287 $this->_defaults['case_start_date_high'] = $date;
288 }
289
290 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
291 if ($cid) {
292 $cid = CRM_Utils_Type::escape($cid, 'Integer');
293 if ($cid > 0) {
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,
297 'sort_name'
298 );
299 // also assign individual mode to the template
300 $this->_single = TRUE;
301 }
302 }
303 else {
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;
309 }
310 else {
311 $this->_formValues['case_owner'] = 0;
312 $this->_defaults['case_owner'] = 0;
313 }
314
315 // Now if case_owner is set in the url/post, use that instead.
316 $caseOwner = CRM_Utils_Request::retrieve('case_owner', 'Positive');
317 if ($caseOwner) {
318 $this->_formValues['case_owner'] = $caseOwner;
319 $this->_defaults['case_owner'] = $caseOwner;
320 }
321 }
322 }
323
324 /**
325 * Return a descriptive name for the page, used in wizard header
326 *
327 * @return string
328 */
329 public function getTitle() {
330 return ts('Find Cases');
331 }
332
333 /**
334 * Set the metadata for the form.
335 *
336 * @throws \CiviCRM_API3_Exception
337 */
338 protected function setSearchMetadata() {
339 $this->addSearchFieldMetadata(['Case' => CRM_Case_BAO_Query::getSearchFieldMetadata()]);
340 }
341
342 }