Add in searchFieldMetadata for contact_tags, contact_type, group and deleted_contacts...
[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()) {
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');
6a488035
TO
79 $this->_actionButtonName = $this->getButtonName('next', 'action');
80
81 $this->_done = FALSE;
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 }
11ac6a2b 205 // @todo - stop changing formValues - respect submitted form values, change a working array.
c94d39fd 206 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
6a488035 207
11ac6a2b 208 // @todo - stop changing formValues - respect submitted form values, change a working array.
6a488035
TO
209 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
210
6a488035
TO
211 $this->set('queryParams', $this->_queryParams);
212
213 $buttonName = $this->controller->getButtonName();
e341bbee 214 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
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
95ea77ec 218 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
219 $formName = $stateMachine->getTaskFormName();
220 $this->controller->resetPage($formName);
221 return;
222 }
223
6a488035
TO
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),
b5c63125 245 $this->getSortID(),
6a488035
TO
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
00be9182 260 public function fixFormValues() {
6a488035
TO
261 if (!$this->_force) {
262 return;
263 }
264
1273d77c 265 $caseStatus = CRM_Utils_Request::retrieve('status', 'Positive');
6a488035
TO
266 if ($caseStatus) {
267 $this->_formValues['case_status_id'] = $caseStatus;
268 $this->_defaults['case_status_id'] = $caseStatus;
269 }
1273d77c 270 $caseType = CRM_Utils_Request::retrieve('type', 'Positive');
6a488035 271 if ($caseType) {
d5e5f843
CW
272 $this->_formValues['case_type_id'] = (array) $caseType;
273 $this->_defaults['case_type_id'] = (array) $caseType;
6a488035
TO
274 }
275
1273d77c 276 $caseFromDate = CRM_Utils_Request::retrieve('pstart', 'Date');
6a488035
TO
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
1273d77c 283 $caseToDate = CRM_Utils_Request::retrieve('pend', 'Date');
6a488035
TO
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 {
e547f744 304 // First, if "all" is stored in the session, default to all cases, otherwise default to no selection.
6a488035
TO
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.
1273d77c 316 $caseOwner = CRM_Utils_Request::retrieve('case_owner', 'Positive');
6a488035
TO
317 if ($caseOwner) {
318 $this->_formValues['case_owner'] = $caseOwner;
319 $this->_defaults['case_owner'] = $caseOwner;
320 }
321 }
322 }
323
6a488035
TO
324 /**
325 * Return a descriptive name for the page, used in wizard header
326 *
327 * @return string
6a488035
TO
328 */
329 public function getTitle() {
330 return ts('Find Cases');
331 }
96025800 332
91d66ce8
SL
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
6a488035 342}