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