Merge pull request #17742 from MiyaNoctem/dev-core-1854-fix-resetting-overridden...
[civicrm-core.git] / CRM / Case / BAO / Query.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 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035 16 */
86a0d21e 17class CRM_Case_BAO_Query extends CRM_Core_BAO_Query {
6a488035 18
4c6ce474 19 /**
3819f101 20 * Get fields.
21 *
4c6ce474
EM
22 * @param bool $excludeActivityFields
23 *
24 * @return array
25 */
00be9182 26 public static function &getFields($excludeActivityFields = FALSE) {
6a488035
TO
27 $fields = CRM_Case_BAO_Case::exportableFields();
28
29 // add activity related fields
1cb28d5d 30 if (!$excludeActivityFields) {
31 $fields = array_merge($fields, CRM_Activity_BAO_Activity::exportableFields('Case'));
32 }
6a488035
TO
33
34 return $fields;
35 }
36
37 /**
fe482240 38 * Build select for Case.
6a488035 39 *
d76e5b23 40 * @param CRM_Contact_BAO_Query $query
6a488035 41 */
00be9182 42 public static function select(&$query) {
8cc574cf 43 if (($query->_mode & CRM_Contact_BAO_Query::MODE_CASE) || !empty($query->_returnProperties['case_id'])) {
6a488035
TO
44 $query->_select['case_id'] = "civicrm_case.id as case_id";
45 $query->_element['case_id'] = 1;
46 $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
47 $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1;
48 }
49
a7488080 50 if (!empty($query->_returnProperties['case_type_id'])) {
8ffdec17 51 $query->_select['case_type_id'] = "civicrm_case_type.id as case_type_id";
6a488035
TO
52 $query->_element['case_type_id'] = 1;
53 $query->_tables['case_type'] = $query->_whereTables['case_type'] = 1;
54 $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
55 }
56
e2751e44 57 if (!empty($query->_returnProperties['case_type'])) {
8ffdec17 58 $query->_select['case_type'] = "civicrm_case_type.title as case_type";
6a488035 59 $query->_element['case_type'] = 1;
e2751e44 60 $query->_tables['case_type'] = $query->_whereTables['case_type'] = 1;
6a488035
TO
61 $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
62 }
63
a7488080 64 if (!empty($query->_returnProperties['case_start_date'])) {
6a488035
TO
65 $query->_select['case_start_date'] = "civicrm_case.start_date as case_start_date";
66 $query->_element['case_start_date'] = 1;
67 $query->_tables['civicrm_case'] = 1;
68 }
69
a7488080 70 if (!empty($query->_returnProperties['case_end_date'])) {
6a488035
TO
71 $query->_select['case_end_date'] = "civicrm_case.end_date as case_end_date";
72 $query->_element['case_end_date'] = 1;
73 $query->_tables['civicrm_case'] = 1;
74 }
75
a7488080 76 if (!empty($query->_returnProperties['case_status_id'])) {
6a488035
TO
77 $query->_select['case_status_id'] = "case_status.id as case_status_id";
78 $query->_element['case_status_id'] = 1;
79 $query->_tables['case_status_id'] = $query->_whereTables['case_status_id'] = 1;
80 $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
81 }
82
a7488080 83 if (!empty($query->_returnProperties['case_status'])) {
6a488035
TO
84 $query->_select['case_status'] = "case_status.label as case_status";
85 $query->_element['case_status'] = 1;
86 $query->_tables['case_status_id'] = $query->_whereTables['case_status_id'] = 1;
87 $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
88 }
89
a7488080 90 if (!empty($query->_returnProperties['case_deleted'])) {
6a488035
TO
91 $query->_select['case_deleted'] = "civicrm_case.is_deleted as case_deleted";
92 $query->_element['case_deleted'] = 1;
93 $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
94 }
95
a7488080 96 if (!empty($query->_returnProperties['case_role'])) {
41cf58d3 97 $query->_select['case_role'] = "IF(case_relationship.contact_id_b = contact_a.id, case_relation_type.label_b_a, case_relation_type.label_a_b) as case_role";
6a488035
TO
98 $query->_element['case_role'] = 1;
99 $query->_tables['case_relationship'] = $query->_whereTables['case_relationship'] = 1;
100 $query->_tables['case_relation_type'] = $query->_whereTables['case_relation_type'] = 1;
101 }
102
f046623b 103 if (!empty($query->_returnProperties['case_activity_date_time'])) {
104 $query->_select['case_activity_date_time'] = "case_activity.activity_date_time as case_activity_date_time";
105 $query->_element['case_activity_date_time'] = 1;
6a488035
TO
106 $query->_tables['case_activity'] = $query->_whereTables['case_activity'] = 1;
107 }
108
a7488080 109 if (!empty($query->_returnProperties['case_activity_subject'])) {
6a488035
TO
110 $query->_select['case_activity_subject'] = "case_activity.subject as case_activity_subject";
111 $query->_element['case_activity_subject'] = 1;
112 $query->_tables['case_activity'] = 1;
113 $query->_tables['civicrm_case_contact'] = 1;
114 $query->_tables['civicrm_case'] = 1;
115 }
116
a7488080 117 if (!empty($query->_returnProperties['case_subject'])) {
6a488035
TO
118 $query->_select['case_subject'] = "civicrm_case.subject as case_subject";
119 $query->_element['case_subject'] = 1;
120 $query->_tables['civicrm_case_contact'] = 1;
121 $query->_tables['civicrm_case'] = 1;
122 }
123
cc81d063 124 // @todo switch to a more standard case_source_contact as the key where we want the name not the id.
a7488080 125 if (!empty($query->_returnProperties['case_source_contact_id'])) {
6a488035
TO
126 $query->_select['case_source_contact_id'] = "civicrm_case_reporter.sort_name as case_source_contact_id";
127 $query->_element['case_source_contact_id'] = 1;
128 $query->_tables['civicrm_case_reporter'] = 1;
129 $query->_tables['case_activity'] = 1;
130 $query->_tables['civicrm_case_contact'] = 1;
131 $query->_tables['civicrm_case'] = 1;
132 }
133
a7488080 134 if (!empty($query->_returnProperties['case_activity_status_id'])) {
6a488035
TO
135 $query->_select['case_activity_status_id'] = "rec_activity_status.id as case_activity_status_id";
136 $query->_element['case_activity_status_id'] = 1;
137 $query->_tables['case_activity'] = 1;
f046623b 138 $query->_tables['case_activity_status'] = 1;
6a488035
TO
139 $query->_tables['civicrm_case_contact'] = 1;
140 $query->_tables['civicrm_case'] = 1;
141 }
142
a7488080 143 if (!empty($query->_returnProperties['case_activity_status'])) {
6a488035
TO
144 $query->_select['case_activity_status'] = "rec_activity_status.label as case_activity_status";
145 $query->_element['case_activity_status'] = 1;
146 $query->_tables['case_activity'] = 1;
f046623b 147 $query->_tables['case_activity_status'] = 1;
6a488035
TO
148 $query->_tables['civicrm_case_contact'] = 1;
149 $query->_tables['civicrm_case'] = 1;
150 }
151
a7488080 152 if (!empty($query->_returnProperties['case_activity_duration'])) {
6a488035
TO
153 $query->_select['case_activity_duration'] = "case_activity.duration as case_activity_duration";
154 $query->_element['case_activity_duration'] = 1;
155 $query->_tables['case_activity'] = 1;
156 $query->_tables['civicrm_case_contact'] = 1;
157 $query->_tables['civicrm_case'] = 1;
158 }
159
a7488080 160 if (!empty($query->_returnProperties['case_activity_medium_id'])) {
f046623b 161 $query->_select['case_activity_medium_id'] = "case_activity_medium.label as case_activity_medium_id";
6a488035
TO
162 $query->_element['case_activity_medium_id'] = 1;
163 $query->_tables['case_activity'] = 1;
164 $query->_tables['case_activity_medium'] = 1;
165 $query->_tables['civicrm_case_contact'] = 1;
166 $query->_tables['civicrm_case'] = 1;
167 }
168
a7488080 169 if (!empty($query->_returnProperties['case_activity_details'])) {
6a488035
TO
170 $query->_select['case_activity_details'] = "case_activity.details as case_activity_details";
171 $query->_element['case_activity_details'] = 1;
172 $query->_tables['case_activity'] = 1;
173 $query->_tables['civicrm_case_contact'] = 1;
174 $query->_tables['civicrm_case'] = 1;
175 }
176
a7488080 177 if (!empty($query->_returnProperties['case_activity_is_auto'])) {
6a488035
TO
178 $query->_select['case_activity_is_auto'] = "case_activity.is_auto as case_activity_is_auto";
179 $query->_element['case_activity_is_auto'] = 1;
180 $query->_tables['case_activity'] = 1;
181 $query->_tables['civicrm_case_contact'] = 1;
182 $query->_tables['civicrm_case'] = 1;
183 }
184
f046623b 185 if (!empty($query->_returnProperties['case_activity_date_time'])) {
186 $query->_select['case_activity_date_time'] = "case_activity.activity_date_time as case_activity_date_time";
187 $query->_element['case_activity_date_time'] = 1;
6a488035
TO
188 $query->_tables['case_activity'] = 1;
189 $query->_tables['civicrm_case_contact'] = 1;
190 $query->_tables['civicrm_case'] = 1;
191 }
f046623b 192 if (!empty($query->_returnProperties['case_activity_type'])) {
193 $query->_select['case_activity_type'] = "rec_activity_type.label as case_activity_type";
194 $query->_element['case_activity_type'] = 1;
6a488035
TO
195 $query->_tables['case_activity'] = 1;
196 $query->_tables['case_activity_type'] = 1;
197 $query->_tables['civicrm_case_contact'] = 1;
198 $query->_tables['civicrm_case'] = 1;
199 }
200 }
201
202 /**
3819f101 203 * Given a list of conditions in query generate the required where clause.
77b97be7 204 *
d76e5b23 205 * @param CRM_Contact_BAO_Query $query
6a488035 206 */
00be9182 207 public static function where(&$query) {
6a488035
TO
208 foreach ($query->_params as $id => $values) {
209 if (!is_array($values) || count($values) != 5) {
210 continue;
211 }
212
213 if (substr($query->_params[$id][0], 0, 5) == 'case_') {
214 if ($query->_mode == CRM_Contact_BAO_Query::MODE_CONTACTS) {
215 $query->_useDistinct = TRUE;
216 }
6a488035
TO
217 self::whereClauseSingle($query->_params[$id], $query);
218 }
219 }
6c051493 220 // Add acl clause
96454cad
CW
221 // This is new and so far only for cases - it would be good to find a more abstract
222 // way to auto-apply this for all search components rather than copy-pasting this code to others
223 if (isset($query->_tables['civicrm_case'])) {
224 $aclClauses = array_filter(CRM_Case_BAO_Case::getSelectWhereClause());
225 foreach ($aclClauses as $clause) {
226 $query->_where[0][] = $clause;
227 }
6c051493 228 }
6a488035
TO
229 }
230
231 /**
fe482240 232 * Where clause for a single field.
6a488035 233 *
96dc1ce0 234 * CRM-17120 adds a test that checks the Qill on some of these parameters.
235 * However, I couldn't find a way, other than via test, to access the
236 * case_activity options in the code below and invalid sql was returned.
237 * Perhaps the options are just legacy?
238 *
239 * Also, CRM-17120 locks in the Qill - but it probably is not quite right as I
240 * see 'Activity Type = Scheduled' (rather than activity status).
241 *
242 * See CRM_Case_BAO_QueryTest for more.
243 *
3819f101 244 * @param array $values
d76e5b23 245 * @param CRM_Contact_BAO_Query $query
bf993266 246 *
247 * @throws \CRM_Core_Exception
6a488035 248 */
00be9182 249 public static function whereClauseSingle(&$values, &$query) {
7d832c0c
SL
250 if ($query->buildDateRangeQuery($values)) {
251 // @todo - move this to Contact_Query in or near the call to
252 // $this->buildRelativeDateQuery($values);
253 return;
254 }
6a488035 255 list($name, $op, $value, $grouping, $wildcard) = $values;
11ac6a2b
SL
256 $fields = CRM_Case_BAO_Case::fields();
257 $fieldSpec = $fields[$values[0]] ?? [];
be2fb01f 258 $val = $names = [];
6a488035 259 switch ($name) {
6a488035
TO
260
261 case 'case_type_id':
8ae90f85 262 case 'case_type':
37653fa0 263 case 'case_status':
264 case 'case_status_id':
265 case 'case_id':
6a488035 266
37653fa0 267 if (strpos($name, 'type')) {
268 $name = 'case_type_id';
269 $label = 'Case Type(s)';
6a488035 270 }
37653fa0 271 elseif (strpos($name, 'status')) {
272 $name = 'status_id';
273 $label = 'Case Status(s)';
8ae90f85 274 }
275 else {
37653fa0 276 $name = 'id';
277 $label = 'Case ID';
8ae90f85 278 }
6a488035 279
37653fa0 280 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_case.{$name}", $op, $value, "Integer");
1a7029b8 281 $query->_qill[$grouping][] = CRM_Contact_BAO_Query::getQillValue('CRM_Case_DAO_Case', $name, $value, $op, $label);
6a488035
TO
282 $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
283 return;
284
285 case 'case_owner':
286 case 'case_mycases':
287 if (!empty($value)) {
288 if ($value == 2) {
289 $session = CRM_Core_Session::singleton();
290 $userID = $session->get('userID');
41cf58d3 291 $query->_where[$grouping][] = ' (( ' . CRM_Contact_BAO_Query::buildClause("case_relationship.contact_id_b", $op, $userID, 'Int') . ' AND ' . CRM_Contact_BAO_Query::buildClause("case_relationship.is_active", '<>', 0, 'Int') . ' ) OR ( ' . CRM_Contact_BAO_Query::buildClause("case_relationship.contact_id_a", $op, $userID, 'Int') . ' AND ' . CRM_Contact_BAO_Query::buildClause("case_relationship.is_active", '<>', 0, 'Int') . ' ))';
be2fb01f 292 $query->_qill[$grouping][] = ts('Case %1 My Cases', [1 => $op]);
6a488035
TO
293 $query->_tables['case_relationship'] = $query->_whereTables['case_relationship'] = 1;
294 }
295 elseif ($value == 1) {
be2fb01f 296 $query->_qill[$grouping][] = ts('Case %1 All Cases', [1 => $op]);
6a488035
TO
297 $query->_where[$grouping][] = "civicrm_case_contact.contact_id = contact_a.id";
298 }
299 $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
300 $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1;
301 }
302 return;
303
304 case 'case_deleted':
305 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_case.is_deleted", $op, $value, 'Boolean');
306 if ($value) {
307 $query->_qill[$grouping][] = ts("Find Deleted Cases");
308 }
309 $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
310 return;
311
312 case 'case_activity_subject':
313 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("case_activity.subject", $op, $value, 'String');
be2fb01f 314 $query->_qill[$grouping][] = ts("Activity Subject %1 '%2'", [1 => $op, 2 => $value]);
6a488035
TO
315 $query->_tables['case_activity'] = $query->_whereTables['case_activity'] = 1;
316 $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
317 $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1;
318 return;
319
320 case 'case_subject':
11ac6a2b 321 $query->handleWhereFromMetadata($fieldSpec, $name, $value, $op);
6a488035
TO
322 return;
323
cc81d063 324 // @todo switch to a more standard case_source_contact as the key where we want the name not the id.
6a488035
TO
325 case 'case_source_contact_id':
326 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_case_reporter.sort_name", $op, $value, 'String');
be2fb01f 327 $query->_qill[$grouping][] = ts("Activity Reporter %1 '%2'", [1 => $op, 2 => $value]);
6a488035
TO
328 $query->_tables['case_activity'] = $query->_whereTables['case_activity'] = 1;
329 $query->_tables['civicrm_case_reporter'] = $query->_whereTables['civicrm_case_reporter'] = 1;
330 $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
331 $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1;
332 return;
333
f046623b 334 case 'case_activity_date_time':
6a488035
TO
335 $date = CRM_Utils_Date::format($value);
336 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("case_activity.activity_date_time", $op, $date, 'Date');
337 if ($date) {
338 $date = CRM_Utils_Date::customFormat($date);
f046623b 339 $query->_qill[$grouping][] = ts("Activity Date %1 %2", [1 => $op, 2 => $date]);
6a488035
TO
340 }
341 $query->_tables['case_activity'] = $query->_whereTables['case_activity'] = 1;
342 $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
343 $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1;
344 return;
345
f046623b 346 case 'case_activity_type':
6a488035 347 $names = $value;
96dc1ce0 348 if (($activityType = CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'activity_type_id', $value)) != FALSE) {
6a488035
TO
349 $names = $activityType;
350 }
351
352 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("case_activity.activity_type_id", $op, $value, 'Int');
be2fb01f 353 $query->_qill[$grouping][] = ts("Activity Type %1 %2", [1 => $op, 2 => $names]);
6a488035
TO
354 $query->_tables['case_activity'] = $query->_whereTables['case_activity'] = 1;
355 $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
356 $query->_tables['case_activity_type'] = 1;
357 $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1;
358 return;
359
360 case 'case_activity_status_id':
361 $names = $value;
96dc1ce0 362 if (($activityStatus = CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'status_id', $value)) != FALSE) {
6a488035
TO
363 $names = $activityStatus;
364 }
365
366 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("case_activity.status_id", $op, $value, 'Int');
d13ce5f9 367 $query->_qill[$grouping][] = ts("Activity Status %1 %2", [1 => $op, 2 => $names]);
6a488035
TO
368 $query->_tables['case_activity'] = $query->_whereTables['case_activity'] = 1;
369 $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
370 $query->_tables['case_activity_status'] = 1;
371 $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1;
372 return;
373
374 case 'case_activity_duration':
375 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("case_activity.duration", $op, $value, 'Int');
be2fb01f 376 $query->_qill[$grouping][] = ts("Activity Duration %1 %2", [1 => $op, 2 => $value]);
6a488035
TO
377 $query->_tables['case_activity'] = $query->_whereTables['case_activity'] = 1;
378 $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
379 $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1;
380 return;
381
382 case 'case_activity_medium_id':
383 $names = $value;
96dc1ce0 384 if (($activityMedium = CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'medium_id', $value)) != FALSE) {
6a488035
TO
385 $names = $activityMedium;
386 }
387
388 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("case_activity.medium_id", $op, $value, 'Int');
be2fb01f 389 $query->_qill[$grouping][] = ts("Activity Medium %1 %2", [1 => $op, 2 => $names]);
6a488035
TO
390 $query->_tables['case_activity'] = $query->_whereTables['case_activity'] = 1;
391 $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
392 $query->_tables['case_activity_medium'] = 1;
393 $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1;
394 return;
395
396 case 'case_activity_details':
397 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("case_activity.details", $op, $value, 'String');
be2fb01f 398 $query->_qill[$grouping][] = ts("Activity Details %1 '%2'", [1 => $op, 2 => $value]);
6a488035
TO
399 $query->_tables['case_activity'] = $query->_whereTables['case_activity'] = 1;
400 $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
401 $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1;
402 return;
403
404 case 'case_activity_is_auto':
405 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("case_activity.is_auto", $op, $value, 'Boolean');
1292fe02 406 $query->_qill[$grouping][] = ts("Activity Auto Generated %1 '%2'", [1 => $op, 2 => $value]);
6a488035
TO
407 $query->_tables['case_activity'] = $query->_whereTables['case_activity'] = 1;
408 $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
409 $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1;
410 return;
411
412 // adding where clause for case_role
413
414 case 'case_role':
be2fb01f 415 $query->_qill[$grouping][] = ts("Role in Case %1 '%2'", [1 => $op, 2 => $value]);
6a488035
TO
416 $query->_tables['case_relation_type'] = $query->_whereTables['case_relationship_type'] = 1;
417 $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
418 $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1;
419 return;
420
421 case 'case_from_start_date_low':
422 case 'case_from_start_date_high':
b27fe371 423 CRM_Core_Error::deprecatedFunctionWarning('case_from is deprecated');
6a488035
TO
424 $query->dateQueryBuilder($values,
425 'civicrm_case', 'case_from_start_date', 'start_date', 'Start Date'
426 );
427 return;
428
429 case 'case_to_end_date_low':
430 case 'case_to_end_date_high':
b27fe371 431 CRM_Core_Error::deprecatedFunctionWarning('case_to is deprecated');
6a488035
TO
432 $query->dateQueryBuilder($values,
433 'civicrm_case', 'case_to_end_date', 'end_date', 'End Date'
434 );
435 return;
436
437 case 'case_start_date':
438 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_case.start_date", $op, $value, 'Int');
439 $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
440 return;
441
442 case 'case_end_date':
443 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_case.end_date", $op, $value, 'Int');
444 $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
445 return;
446
447 case 'case_taglist':
448 $taglist = $value;
be2fb01f 449 $value = [];
6a488035
TO
450 foreach ($taglist as $val) {
451 if ($val) {
452 $val = explode(',', $val);
453 foreach ($val as $tId) {
454 if (is_numeric($tId)) {
455 $value[$tId] = 1;
456 }
457 }
458 }
459 }
460 case 'case_tags':
be2fb01f 461 $tags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', ['onlyActive' => FALSE]);
6a488035 462
dd33678f 463 if (!empty($value)) {
0e80e389 464 if (is_array($value)) {
465 // Search tag(s) are part of a tag set
466 $val = array_keys($value);
467 }
468 else {
469 // Search tag(s) are part of the tag tree
470 $val = explode(',', $value);
471 }
dd33678f 472 foreach ($val as $v) {
6a488035 473 if ($v) {
dd33678f 474 $names[] = $tags[$v];
6a488035
TO
475 }
476 }
477 }
478
479 $query->_where[$grouping][] = " civicrm_case_tag.tag_id IN (" . implode(',', $val) . " )";
be2fb01f 480 $query->_qill[$grouping][] = ts('Case Tags %1', [1 => $op]) . ' ' . implode(' ' . ts('or') . ' ', $names);
6a488035
TO
481 $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1;
482 $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1;
483 $query->_tables['civicrm_case_tag'] = $query->_whereTables['civicrm_case_tag'] = 1;
484 return;
485 }
486 }
487
4c6ce474 488 /**
d76e5b23 489 * Build from clause.
490 *
100fef9d 491 * @param string $name
d76e5b23 492 * @param string $mode
493 * @param string $side
4c6ce474
EM
494 *
495 * @return string
496 */
00be9182 497 public static function from($name, $mode, $side) {
6a488035
TO
498 $from = "";
499
500 switch ($name) {
501 case 'civicrm_case_contact':
502 $from .= " $side JOIN civicrm_case_contact ON civicrm_case_contact.contact_id = contact_a.id ";
503 break;
504
505 case 'civicrm_case_reporter':
44f817d4 506 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
962c0eda 507 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
508 $from .= " $side JOIN civicrm_activity_contact as case_activity_contact ON (case_activity.id = case_activity_contact.activity_id AND case_activity_contact.record_type_id = {$sourceID} ) ";
09a5df84 509 $from .= " $side JOIN civicrm_contact as civicrm_case_reporter ON case_activity_contact.contact_id = civicrm_case_reporter.id ";
6a488035
TO
510 break;
511
512 case 'civicrm_case':
513 $from .= " INNER JOIN civicrm_case ON civicrm_case_contact.case_id = civicrm_case.id";
514 break;
515
516 case 'case_status_id':
517 $from .= " $side JOIN civicrm_option_group option_group_case_status ON (option_group_case_status.name = 'case_status')";
518 $from .= " $side JOIN civicrm_option_value case_status ON (civicrm_case.status_id = case_status.value AND option_group_case_status.id = case_status.option_group_id ) ";
519 break;
520
521 case 'case_type':
8ffdec17 522 $from .= " $side JOIN civicrm_case_type ON civicrm_case.case_type_id = civicrm_case_type.id ";
6a488035
TO
523 break;
524
525 case 'case_activity_type':
526 $from .= " $side JOIN civicrm_option_group option_group_activity_type ON (option_group_activity_type.name = 'activity_type')";
527 $from .= " $side JOIN civicrm_option_value rec_activity_type ON (case_activity.activity_type_id = rec_activity_type.value AND option_group_activity_type.id = rec_activity_type.option_group_id ) ";
528 break;
529
f046623b 530 case 'case_activity_status':
6a488035
TO
531 $from .= " $side JOIN civicrm_option_group option_group_activity_status ON (option_group_activity_status.name = 'activity_status')";
532 $from .= " $side JOIN civicrm_option_value rec_activity_status ON (case_activity.status_id = rec_activity_status.value AND option_group_activity_status.id = rec_activity_status.option_group_id ) ";
533 break;
534
535 case 'case_relationship':
536 $session = CRM_Core_Session::singleton();
353ffa53 537 $userID = $session->get('userID');
41cf58d3 538 $from .= " $side JOIN civicrm_relationship case_relationship ON ( case_relationship.contact_id_a = civicrm_case_contact.contact_id AND case_relationship.contact_id_b = {$userID} AND case_relationship.case_id = civicrm_case.id OR case_relationship.contact_id_b = civicrm_case_contact.contact_id AND case_relationship.contact_id_a = {$userID} AND case_relationship.case_id = civicrm_case.id )";
6a488035
TO
539 break;
540
541 case 'case_relation_type':
542 $from .= " $side JOIN civicrm_relationship_type case_relation_type ON ( case_relation_type.id = case_relationship.relationship_type_id AND
543case_relation_type.id = case_relationship.relationship_type_id )";
544 break;
545
546 case 'case_activity_medium':
547 $from .= " $side JOIN civicrm_option_group option_group_activity_medium ON (option_group_activity_medium.name = 'encounter_medium')";
f046623b 548 $from .= " $side JOIN civicrm_option_value case_activity_medium ON (case_activity.medium_id = case_activity_medium.value AND option_group_activity_medium.id = case_activity_medium.option_group_id ) ";
6a488035
TO
549 break;
550
551 case 'case_activity':
552 $from .= " INNER JOIN civicrm_case_activity ON civicrm_case_activity.case_id = civicrm_case.id ";
553 $from .= " INNER JOIN civicrm_activity case_activity ON ( civicrm_case_activity.activity_id = case_activity.id
554 AND case_activity.is_current_revision = 1 )";
555 break;
556
557 case 'civicrm_case_tag':
558 $from .= " $side JOIN civicrm_entity_tag as civicrm_case_tag ON ( civicrm_case_tag.entity_table = 'civicrm_case' AND civicrm_case_tag.entity_id = civicrm_case.id ) ";
559 break;
560 }
561 return $from;
562 }
563
564 /**
fe482240 565 * Getter for the qill object.
6a488035
TO
566 *
567 * @return string
6a488035 568 */
00be9182 569 public function qill() {
6a488035
TO
570 return (isset($this->_qill)) ? $this->_qill : "";
571 }
572
4c6ce474
EM
573 /**
574 * @param $mode
575 * @param bool $includeCustomFields
576 *
577 * @return array|null
578 */
e7483cbe 579 public static function defaultReturnProperties(
28d4d481 580 $mode,
6a488035
TO
581 $includeCustomFields = TRUE
582 ) {
583
584 $properties = NULL;
585
586 if ($mode & CRM_Contact_BAO_Query::MODE_CASE) {
be2fb01f 587 $properties = [
6a488035
TO
588 'contact_type' => 1,
589 'contact_sub_type' => 1,
590 'contact_id' => 1,
591 'sort_name' => 1,
592 'display_name' => 1,
593 'case_id' => 1,
594 'case_activity_subject' => 1,
595 'case_subject' => 1,
596 'case_status' => 1,
597 'case_type' => 1,
598 'case_role' => 1,
599 'case_deleted' => 1,
f046623b 600 'case_activity_date_time' => 1,
601 'case_activity_type' => 1,
6a488035 602 'phone' => 1,
be2fb01f 603 ];
6a488035
TO
604
605 if ($includeCustomFields) {
606 // also get all the custom case properties
607 $fields = CRM_Core_BAO_CustomField::getFieldsForImport('Case');
608 if (!empty($fields)) {
609 foreach ($fields as $name => $dontCare) {
610 $properties[$name] = 1;
611 }
612 }
613 }
614 }
615
616 return $properties;
617 }
618
619 /**
fe482240 620 * This includes any extra fields that might need for export etc.
ad37ac8e 621 *
622 * @param string $mode
623 *
624 * @return array|null
6a488035 625 */
00be9182 626 public static function extraReturnProperties($mode) {
6a488035
TO
627 $properties = NULL;
628
629 if ($mode & CRM_Contact_BAO_Query::MODE_CASE) {
be2fb01f 630 $properties = [
6a488035
TO
631 'case_start_date' => 1,
632 'case_end_date' => 1,
633 'case_subject' => 1,
cc81d063 634 // @todo switch to a more standard case_source_contact as the key where we want the name not the id.
6a488035
TO
635 'case_source_contact_id' => 1,
636 'case_activity_status' => 1,
637 'case_activity_duration' => 1,
638 'case_activity_medium_id' => 1,
639 'case_activity_details' => 1,
640 'case_activity_is_auto' => 1,
be2fb01f 641 ];
6a488035
TO
642 }
643 return $properties;
644 }
645
4c6ce474
EM
646 /**
647 * @param $tables
648 */
00be9182 649 public static function tableNames(&$tables) {
a7488080 650 if (!empty($tables['civicrm_case'])) {
be2fb01f 651 $tables = array_merge(['civicrm_case_contact' => 1], $tables);
6a488035
TO
652 }
653
a7488080 654 if (!empty($tables['case_relation_type'])) {
be2fb01f 655 $tables = array_merge(['case_relationship' => 1], $tables);
6a488035
TO
656 }
657 }
658
b62aa188
MD
659 /**
660 * Get the metadata for fields to be included on the case search form.
661 *
662 * @todo ideally this would be a trait included on the case search & advanced search
663 * rather than a static function.
664 */
665 public static function getSearchFieldMetadata() {
666 $fields = ['case_type_id', 'case_status_id', 'case_start_date', 'case_end_date', 'case_subject', 'case_id', 'case_deleted'];
667 $metadata = civicrm_api3('Case', 'getfields', [])['values'];
020457ad 668 $metadata['case_id'] = $metadata['id'];
b62aa188
MD
669 $metadata = array_intersect_key($metadata, array_flip($fields));
670 $metadata['case_tags'] = [
671 'title' => ts('Case Tag(s)'),
672 'type' => CRM_Utils_Type::T_INT,
673 'is_pseudofield' => TRUE,
774c73d9 674 'html' => ['type' => 'Select2'],
b62aa188
MD
675 ];
676 if (CRM_Core_Permission::check('access all cases and activities')) {
677 $metadata['case_owner'] = [
678 'title' => ts('Cases'),
679 'type' => CRM_Utils_Type::T_INT,
680 'is_pseudofield' => TRUE,
774c73d9 681 'html' => ['type' => 'Radio'],
b62aa188
MD
682 ];
683 }
684 if (!CRM_Core_Permission::check('administer CiviCase')) {
685 unset($metadata['case_deleted']);
686 }
687 return $metadata;
688 }
689
6a488035 690 /**
3819f101 691 * Add all the elements shared between case search and advanced search.
6a488035 692 *
bf993266 693 * @param CRM_Case_Form_Search $form
6a488035 694 */
00be9182 695 public static function buildSearchForm(&$form) {
6a488035
TO
696 //validate case configuration.
697 $configured = CRM_Case_BAO_Case::isCaseConfigured();
698 $form->assign('notConfigured', !$configured['configured']);
699
b62aa188
MD
700 $form->addSearchFieldMetadata(['Case' => self::getSearchFieldMetadata()]);
701 $form->addFormFieldsFromMetadata();
6a488035 702
6a488035
TO
703 $form->assign('validCiviCase', TRUE);
704
705 //give options when all cases are accessible.
706 $accessAllCases = FALSE;
707 if (CRM_Core_Permission::check('access all cases and activities')) {
708 $accessAllCases = TRUE;
be2fb01f 709 $caseOwner = [1 => ts('Search All Cases'), 2 => ts('Only My Cases')];
6a488035 710 $form->addRadio('case_owner', ts('Cases'), $caseOwner);
5f1c8c57 711 if ($form->get('context') != 'dashboard') {
712 $form->add('checkbox', 'upcoming', ts('Search Cases with Upcoming Activities'));
713 }
6a488035
TO
714 }
715 $form->assign('accessAllCases', $accessAllCases);
716
dd33678f 717 $caseTags = CRM_Core_BAO_Tag::getColorTags('civicrm_case');
6a488035
TO
718
719 if ($caseTags) {
be2fb01f 720 $form->add('select2', 'case_tags', ts('Case Tag(s)'), $caseTags, FALSE, ['class' => 'big', 'placeholder' => ts('- select -'), 'multiple' => TRUE]);
6a488035
TO
721 }
722
723 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_case');
4dff5e17 724 CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_case', NULL, TRUE, FALSE);
6a488035 725
be2fb01f 726 self::addCustomFormFields($form, ['Case']);
6a488035 727
be2fb01f 728 $form->setDefaults(['case_owner' => 1]);
e547f744 729 }
96025800 730
6a488035 731}