Merge pull request #15843 from totten/master-simplehead
[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 $this->loadStandardSearchOptionsFromUrl();
84 $this->loadFormValues();
85
86 if ($this->_force) {
87 $this->handleForcedSearch();
88 }
89
90 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
91 $selector = new CRM_Case_Selector_Search($this->_queryParams,
92 $this->_action,
93 NULL,
94 $this->_single,
95 $this->_limit,
96 $this->_context
97 );
98
99 $prefix = NULL;
100 if ($this->_context == 'user') {
101 $prefix = $this->_prefix;
102 }
103
104 $this->assign("{$prefix}limit", $this->_limit);
105 $this->assign("{$prefix}single", $this->_single);
106
107 $controller = new CRM_Core_Selector_Controller($selector,
108 $this->get(CRM_Utils_Pager::PAGE_ID),
109 $this->getSortID(),
110 CRM_Core_Action::VIEW,
111 $this,
112 CRM_Core_Selector_Controller::TRANSFER,
113 $prefix
114 );
115 $controller->setEmbedded(TRUE);
116 $controller->moveFromSessionToTemplate();
117
118 $this->assign('summary', $this->get('summary'));
119 }
120
121 /**
122 * Build the form object.
123 */
124 public function buildQuickForm() {
125 parent::buildQuickForm();
126 $this->addSortNameField();
127
128 CRM_Case_BAO_Query::buildSearchForm($this);
129
130 $rows = $this->get('rows');
131 if (is_array($rows)) {
132 if (!$this->_single) {
133 $this->addRowSelectors($rows);
134 }
135
136 $tasks = CRM_Case_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission());
137
138 if (!empty($this->_formValues['case_deleted'])) {
139 unset($tasks[CRM_Case_Task::TASK_DELETE]);
140 }
141 else {
142 unset($tasks[CRM_Case_Task::RESTORE_CASES]);
143 }
144
145 $this->addTaskMenu($tasks);
146 }
147
148 }
149
150 /**
151 * Get the label for the sortName field if email searching is on.
152 *
153 * (email searching is a setting under search preferences).
154 *
155 * @return string
156 */
157 protected function getSortNameLabelWithEmail() {
158 return ts('Client Name or Email');
159 }
160
161 /**
162 * Get the label for the sortName field if email searching is off.
163 *
164 * (email searching is a setting under search preferences).
165 *
166 * @return string
167 */
168 protected function getSortNameLabelWithOutEmail() {
169 return ts('Client Name');
170 }
171
172 /**
173 * The post processing of the form gets done here.
174 *
175 * Key things done during post processing are
176 * - check for reset or next request. if present, skip post procesing.
177 * - now check if user requested running a saved search, if so, then
178 * the form values associated with the saved search are used for searching.
179 * - if user has done a submit with new values the regular post submissing is
180 * done.
181 * The processing consists of using a Selector / Controller framework for getting the
182 * search results.
183 */
184 public function postProcess() {
185 if ($this->_done) {
186 return;
187 }
188
189 $this->_done = TRUE;
190 $this->setFormValues();
191 $this->fixFormValues();
192 if (isset($this->_ssID) && empty($_POST)) {
193 // if we are editing / running a saved search and the form has not been posted
194 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
195 }
196
197 //search for civicase
198 if (!$this->_force) {
199 if (array_key_exists('case_owner', $this->_formValues) && !$this->_formValues['case_owner']) {
200 $this->_formValues['case_owner'] = 0;
201 }
202 }
203
204 if (empty($this->_formValues['case_deleted'])) {
205 $this->_formValues['case_deleted'] = 0;
206 }
207 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
208
209 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
210
211 $this->set('formValues', $this->_formValues);
212 $this->set('queryParams', $this->_queryParams);
213
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
217
218 // hack, make sure we reset the task values
219 $stateMachine = $this->controller->getStateMachine();
220 $formName = $stateMachine->getTaskFormName();
221 $this->controller->resetPage($formName);
222 return;
223 }
224
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),
246 $this->getSortID(),
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
261 /**
262 * Add the rules (mainly global rules) for form.
263 *
264 * All local rules are added near the element
265 *
266 * @see valid_date
267 */
268 public function addRules() {
269 $this->addFormRule(['CRM_Case_Form_Search', 'formRule']);
270 }
271
272 /**
273 * Global validation rules for the form.
274 *
275 * @param array $fields
276 * Posted values of the form.
277 * @param array $files
278 * @param object $form
279 *
280 * @return array|bool
281 */
282 public static function formRule($fields, $files, $form) {
283 $errors = [];
284
285 if (!empty($errors)) {
286 return $errors;
287 }
288
289 return TRUE;
290 }
291
292 public function fixFormValues() {
293 if (!$this->_force) {
294 return;
295 }
296
297 $caseStatus = CRM_Utils_Request::retrieve('status', 'Positive');
298 if ($caseStatus) {
299 $this->_formValues['case_status_id'] = $caseStatus;
300 $this->_defaults['case_status_id'] = $caseStatus;
301 }
302 $caseType = CRM_Utils_Request::retrieve('type', 'Positive');
303 if ($caseType) {
304 $this->_formValues['case_type_id'] = (array) $caseType;
305 $this->_defaults['case_type_id'] = (array) $caseType;
306 }
307
308 $caseFromDate = CRM_Utils_Request::retrieve('pstart', 'Date');
309 if ($caseFromDate) {
310 list($date) = CRM_Utils_Date::setDateDefaults($caseFromDate);
311 $this->_formValues['case_start_date_low'] = $date;
312 $this->_defaults['case_start_date_low'] = $date;
313 }
314
315 $caseToDate = CRM_Utils_Request::retrieve('pend', 'Date');
316 if ($caseToDate) {
317 list($date) = CRM_Utils_Date::setDateDefaults($caseToDate);
318 $this->_formValues['case_start_date_high'] = $date;
319 $this->_defaults['case_start_date_high'] = $date;
320 }
321
322 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
323 if ($cid) {
324 $cid = CRM_Utils_Type::escape($cid, 'Integer');
325 if ($cid > 0) {
326 $this->_formValues['contact_id'] = $cid;
327 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
328 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
329 'sort_name'
330 );
331 // also assign individual mode to the template
332 $this->_single = TRUE;
333 }
334 }
335 else {
336 // First, if "all" is stored in the session, default to all cases, otherwise default to no selection.
337 $session = CRM_Core_Session::singleton();
338 if (CRM_Utils_Request::retrieve('all', 'Positive', $session)) {
339 $this->_formValues['case_owner'] = 1;
340 $this->_defaults['case_owner'] = 1;
341 }
342 else {
343 $this->_formValues['case_owner'] = 0;
344 $this->_defaults['case_owner'] = 0;
345 }
346
347 // Now if case_owner is set in the url/post, use that instead.
348 $caseOwner = CRM_Utils_Request::retrieve('case_owner', 'Positive');
349 if ($caseOwner) {
350 $this->_formValues['case_owner'] = $caseOwner;
351 $this->_defaults['case_owner'] = $caseOwner;
352 }
353 }
354 }
355
356 /**
357 * Return a descriptive name for the page, used in wizard header
358 *
359 * @return string
360 */
361 public function getTitle() {
362 return ts('Find Cases');
363 }
364
365 /**
366 * Set the metadata for the form.
367 *
368 * @throws \CiviCRM_API3_Exception
369 */
370 protected function setSearchMetadata() {
371 $this->addSearchFieldMetadata(['Case' => CRM_Case_BAO_Query::getSearchFieldMetadata()]);
372 }
373
374 }