3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Case_BAO_Query
extends CRM_Core_BAO_Query
{
22 * @param bool $excludeActivityFields
26 public static function &getFields($excludeActivityFields = FALSE) {
27 $fields = CRM_Case_BAO_Case
::exportableFields();
29 // add activity related fields
30 if (!$excludeActivityFields) {
31 $fields = array_merge($fields, CRM_Activity_BAO_Activity
::exportableFields('Case'));
38 * Build select for Case.
40 * @param CRM_Contact_BAO_Query $query
42 public static function select(&$query) {
43 if (($query->_mode
& CRM_Contact_BAO_Query
::MODE_CASE
) ||
!empty($query->_returnProperties
['case_id'])) {
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;
50 if (!empty($query->_returnProperties
['case_type_id'])) {
51 $query->_select
['case_type_id'] = "civicrm_case_type.id as case_type_id";
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;
57 if (!empty($query->_returnProperties
['case_type'])) {
58 $query->_select
['case_type'] = "civicrm_case_type.title as case_type";
59 $query->_element
['case_type'] = 1;
60 $query->_tables
['case_type'] = $query->_whereTables
['case_type'] = 1;
61 $query->_tables
['civicrm_case'] = $query->_whereTables
['civicrm_case'] = 1;
64 if (!empty($query->_returnProperties
['case_start_date'])) {
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;
70 if (!empty($query->_returnProperties
['case_end_date'])) {
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;
76 if (!empty($query->_returnProperties
['case_status_id'])) {
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;
83 if (!empty($query->_returnProperties
['case_status'])) {
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;
90 if (!empty($query->_returnProperties
['case_deleted'])) {
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;
96 if (!empty($query->_returnProperties
['case_role'])) {
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";
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;
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;
106 $query->_tables
['case_activity'] = $query->_whereTables
['case_activity'] = 1;
109 if (!empty($query->_returnProperties
['case_activity_subject'])) {
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;
117 if (!empty($query->_returnProperties
['case_subject'])) {
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;
124 // @todo switch to a more standard case_source_contact as the key where we want the name not the id.
125 if (!empty($query->_returnProperties
['case_source_contact_id'])) {
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;
134 if (!empty($query->_returnProperties
['case_activity_status_id'])) {
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;
138 $query->_tables
['case_activity_status'] = 1;
139 $query->_tables
['civicrm_case_contact'] = 1;
140 $query->_tables
['civicrm_case'] = 1;
143 if (!empty($query->_returnProperties
['case_activity_status'])) {
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;
147 $query->_tables
['case_activity_status'] = 1;
148 $query->_tables
['civicrm_case_contact'] = 1;
149 $query->_tables
['civicrm_case'] = 1;
152 if (!empty($query->_returnProperties
['case_activity_duration'])) {
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;
160 if (!empty($query->_returnProperties
['case_activity_medium_id'])) {
161 $query->_select
['case_activity_medium_id'] = "case_activity_medium.label as case_activity_medium_id";
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;
169 if (!empty($query->_returnProperties
['case_activity_details'])) {
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;
177 if (!empty($query->_returnProperties
['case_activity_is_auto'])) {
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;
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;
188 $query->_tables
['case_activity'] = 1;
189 $query->_tables
['civicrm_case_contact'] = 1;
190 $query->_tables
['civicrm_case'] = 1;
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;
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;
203 * Given a list of conditions in query generate the required where clause.
205 * @param CRM_Contact_BAO_Query $query
207 public static function where(&$query) {
208 foreach ($query->_params
as $id => $values) {
209 if (!is_array($values) ||
count($values) != 5) {
213 if (substr($query->_params
[$id][0], 0, 5) == 'case_') {
214 if ($query->_mode
== CRM_Contact_BAO_Query
::MODE_CONTACTS
) {
215 $query->_useDistinct
= TRUE;
217 self
::whereClauseSingle($query->_params
[$id], $query);
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;
232 * Where clause for a single field.
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?
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).
242 * See CRM_Case_BAO_QueryTest for more.
244 * @param array $values
245 * @param CRM_Contact_BAO_Query $query
247 * @throws \CRM_Core_Exception
249 public static function whereClauseSingle(&$values, &$query) {
250 if ($query->buildDateRangeQuery($values)) {
251 // @todo - move this to Contact_Query in or near the call to
252 // $this->buildRelativeDateQuery($values);
255 list($name, $op, $value, $grouping, $wildcard) = $values;
256 $fields = CRM_Case_BAO_Case
::fields();
257 $fieldSpec = $fields[$values[0]] ??
[];
264 case 'case_status_id':
267 if (strpos($name, 'type')) {
268 $name = 'case_type_id';
269 $label = 'Case Type(s)';
271 elseif (strpos($name, 'status')) {
273 $label = 'Case Status(s)';
280 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_case.{$name}", $op, $value, "Integer");
281 $query->_qill
[$grouping][] = CRM_Contact_BAO_Query
::getQillValue('CRM_Case_DAO_Case', $name, $value, $op, $label);
282 $query->_tables
['civicrm_case'] = $query->_whereTables
['civicrm_case'] = 1;
287 if (!empty($value)) {
289 $session = CRM_Core_Session
::singleton();
290 $userID = $session->get('userID');
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') . ' ))';
292 $query->_qill
[$grouping][] = ts('Case %1 My Cases', [1 => $op]);
293 $query->_tables
['case_relationship'] = $query->_whereTables
['case_relationship'] = 1;
295 elseif ($value == 1) {
296 $query->_qill
[$grouping][] = ts('Case %1 All Cases', [1 => $op]);
297 $query->_where
[$grouping][] = "civicrm_case_contact.contact_id = contact_a.id";
299 $query->_tables
['civicrm_case'] = $query->_whereTables
['civicrm_case'] = 1;
300 $query->_tables
['civicrm_case_contact'] = $query->_whereTables
['civicrm_case_contact'] = 1;
305 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_case.is_deleted", $op, $value, 'Boolean');
307 $query->_qill
[$grouping][] = ts("Find Deleted Cases");
309 $query->_tables
['civicrm_case'] = $query->_whereTables
['civicrm_case'] = 1;
312 case 'case_activity_subject':
313 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("case_activity.subject", $op, $value, 'String');
314 $query->_qill
[$grouping][] = ts("Activity Subject %1 '%2'", [1 => $op, 2 => $value]);
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;
321 $query->handleWhereFromMetadata($fieldSpec, $name, $value, $op);
324 // @todo switch to a more standard case_source_contact as the key where we want the name not the id.
325 case 'case_source_contact_id':
326 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_case_reporter.sort_name", $op, $value, 'String');
327 $query->_qill
[$grouping][] = ts("Activity Reporter %1 '%2'", [1 => $op, 2 => $value]);
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;
334 case 'case_activity_date_time':
335 $date = CRM_Utils_Date
::format($value);
336 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("case_activity.activity_date_time", $op, $date, 'Date');
338 $date = CRM_Utils_Date
::customFormat($date);
339 $query->_qill
[$grouping][] = ts("Activity Date %1 %2", [1 => $op, 2 => $date]);
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;
346 case 'case_activity_type':
348 if (($activityType = CRM_Core_PseudoConstant
::getLabel('CRM_Activity_BAO_Activity', 'activity_type_id', $value)) != FALSE) {
349 $names = $activityType;
352 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("case_activity.activity_type_id", $op, $value, 'Int');
353 $query->_qill
[$grouping][] = ts("Activity Type %1 %2", [1 => $op, 2 => $names]);
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;
360 case 'case_activity_status_id':
362 if (($activityStatus = CRM_Core_PseudoConstant
::getLabel('CRM_Activity_BAO_Activity', 'status_id', $value)) != FALSE) {
363 $names = $activityStatus;
366 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("case_activity.status_id", $op, $value, 'Int');
367 $query->_qill
[$grouping][] = ts("Activity Status %1 %2", [1 => $op, 2 => $names]);
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;
374 case 'case_activity_duration':
375 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("case_activity.duration", $op, $value, 'Int');
376 $query->_qill
[$grouping][] = ts("Activity Duration %1 %2", [1 => $op, 2 => $value]);
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;
382 case 'case_activity_medium_id':
384 if (($activityMedium = CRM_Core_PseudoConstant
::getLabel('CRM_Activity_BAO_Activity', 'medium_id', $value)) != FALSE) {
385 $names = $activityMedium;
388 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("case_activity.medium_id", $op, $value, 'Int');
389 $query->_qill
[$grouping][] = ts("Activity Medium %1 %2", [1 => $op, 2 => $names]);
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;
396 case 'case_activity_details':
397 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("case_activity.details", $op, $value, 'String');
398 $query->_qill
[$grouping][] = ts("Activity Details %1 '%2'", [1 => $op, 2 => $value]);
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;
404 case 'case_activity_is_auto':
405 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("case_activity.is_auto", $op, $value, 'Boolean');
406 $query->_qill
[$grouping][] = ts("Activity Auto Generated %1 '%2'", [1 => $op, 2 => $value]);
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;
412 // adding where clause for case_role
415 $query->_qill
[$grouping][] = ts("Role in Case %1 '%2'", [1 => $op, 2 => $value]);
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;
421 case 'case_start_date':
422 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_case.start_date", $op, $value, 'Int');
423 $query->_tables
['civicrm_case'] = $query->_whereTables
['civicrm_case'] = 1;
426 case 'case_end_date':
427 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_case.end_date", $op, $value, 'Int');
428 $query->_tables
['civicrm_case'] = $query->_whereTables
['civicrm_case'] = 1;
434 foreach ($taglist as $val) {
436 $val = explode(',', $val);
437 foreach ($val as $tId) {
438 if (is_numeric($tId)) {
445 $tags = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_EntityTag', 'tag_id', ['onlyActive' => FALSE]);
447 if (!empty($value)) {
448 if (is_array($value)) {
449 // Search tag(s) are part of a tag set
450 $val = array_keys($value);
453 // Search tag(s) are part of the tag tree
454 $val = explode(',', $value);
456 foreach ($val as $v) {
458 $names[] = $tags[$v];
463 $query->_where
[$grouping][] = " civicrm_case_tag.tag_id IN (" . implode(',', $val) . " )";
464 $query->_qill
[$grouping][] = ts('Case Tags %1', [1 => $op]) . ' ' . implode(' ' . ts('or') . ' ', $names);
465 $query->_tables
['civicrm_case'] = $query->_whereTables
['civicrm_case'] = 1;
466 $query->_tables
['civicrm_case_contact'] = $query->_whereTables
['civicrm_case_contact'] = 1;
467 $query->_tables
['civicrm_case_tag'] = $query->_whereTables
['civicrm_case_tag'] = 1;
475 * @param string $name
476 * @param string $mode
477 * @param string $side
481 public static function from($name, $mode, $side) {
485 case 'civicrm_case_contact':
486 $from .= " $side JOIN civicrm_case_contact ON civicrm_case_contact.contact_id = contact_a.id ";
489 case 'civicrm_case_reporter':
490 $activityContacts = CRM_Activity_BAO_ActivityContact
::buildOptions('record_type_id', 'validate');
491 $sourceID = CRM_Utils_Array
::key('Activity Source', $activityContacts);
492 $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} ) ";
493 $from .= " $side JOIN civicrm_contact as civicrm_case_reporter ON case_activity_contact.contact_id = civicrm_case_reporter.id ";
497 $from .= " INNER JOIN civicrm_case ON civicrm_case_contact.case_id = civicrm_case.id";
500 case 'case_status_id':
501 $from .= " $side JOIN civicrm_option_group option_group_case_status ON (option_group_case_status.name = 'case_status')";
502 $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 ) ";
506 $from .= " $side JOIN civicrm_case_type ON civicrm_case.case_type_id = civicrm_case_type.id ";
509 case 'case_activity_type':
510 $from .= " $side JOIN civicrm_option_group option_group_activity_type ON (option_group_activity_type.name = 'activity_type')";
511 $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 ) ";
514 case 'case_activity_status':
515 $from .= " $side JOIN civicrm_option_group option_group_activity_status ON (option_group_activity_status.name = 'activity_status')";
516 $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 ) ";
519 case 'case_relationship':
520 $session = CRM_Core_Session
::singleton();
521 $userID = $session->get('userID');
522 $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 )";
525 case 'case_relation_type':
526 $from .= " $side JOIN civicrm_relationship_type case_relation_type ON ( case_relation_type.id = case_relationship.relationship_type_id AND
527 case_relation_type.id = case_relationship.relationship_type_id )";
530 case 'case_activity_medium':
531 $from .= " $side JOIN civicrm_option_group option_group_activity_medium ON (option_group_activity_medium.name = 'encounter_medium')";
532 $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 ) ";
535 case 'case_activity':
536 $from .= " INNER JOIN civicrm_case_activity ON civicrm_case_activity.case_id = civicrm_case.id ";
537 $from .= " INNER JOIN civicrm_activity case_activity ON ( civicrm_case_activity.activity_id = case_activity.id
538 AND case_activity.is_current_revision = 1 )";
541 case 'civicrm_case_tag':
542 $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 ) ";
549 * Getter for the qill object.
553 public function qill() {
554 return (isset($this->_qill
)) ?
$this->_qill
: "";
559 * @param bool $includeCustomFields
563 public static function defaultReturnProperties(
565 $includeCustomFields = TRUE
570 if ($mode & CRM_Contact_BAO_Query
::MODE_CASE
) {
573 'contact_sub_type' => 1,
578 'case_activity_subject' => 1,
584 'case_activity_date_time' => 1,
585 'case_activity_type' => 1,
589 if ($includeCustomFields) {
590 // also get all the custom case properties
591 $fields = CRM_Core_BAO_CustomField
::getFieldsForImport('Case');
592 if (!empty($fields)) {
593 foreach ($fields as $name => $dontCare) {
594 $properties[$name] = 1;
604 * This includes any extra fields that might need for export etc.
606 * @param string $mode
610 public static function extraReturnProperties($mode) {
613 if ($mode & CRM_Contact_BAO_Query
::MODE_CASE
) {
615 'case_start_date' => 1,
616 'case_end_date' => 1,
618 // @todo switch to a more standard case_source_contact as the key where we want the name not the id.
619 'case_source_contact_id' => 1,
620 'case_activity_status' => 1,
621 'case_activity_duration' => 1,
622 'case_activity_medium_id' => 1,
623 'case_activity_details' => 1,
624 'case_activity_is_auto' => 1,
633 public static function tableNames(&$tables) {
634 if (!empty($tables['civicrm_case'])) {
635 $tables = array_merge(['civicrm_case_contact' => 1], $tables);
638 if (!empty($tables['case_relation_type'])) {
639 $tables = array_merge(['case_relationship' => 1], $tables);
644 * Get the metadata for fields to be included on the case search form.
646 * @todo ideally this would be a trait included on the case search & advanced search
647 * rather than a static function.
649 public static function getSearchFieldMetadata() {
650 $fields = ['case_type_id', 'case_status_id', 'case_start_date', 'case_end_date', 'case_subject', 'case_id', 'case_deleted'];
651 $metadata = civicrm_api3('Case', 'getfields', [])['values'];
652 $metadata['case_id'] = $metadata['id'];
653 $metadata = array_intersect_key($metadata, array_flip($fields));
654 $metadata['case_tags'] = [
655 'title' => ts('Case Tag(s)'),
656 'type' => CRM_Utils_Type
::T_INT
,
657 'is_pseudofield' => TRUE,
658 'html' => ['type' => 'Select2'],
660 if (CRM_Core_Permission
::check('access all cases and activities')) {
661 $metadata['case_owner'] = [
662 'title' => ts('Cases'),
663 'type' => CRM_Utils_Type
::T_INT
,
664 'is_pseudofield' => TRUE,
665 'html' => ['type' => 'Radio'],
668 if (!CRM_Core_Permission
::check('administer CiviCase')) {
669 unset($metadata['case_deleted']);
675 * Add all the elements shared between case search and advanced search.
677 * @param CRM_Case_Form_Search $form
679 public static function buildSearchForm(&$form): void
{
680 $form->addOptionalQuickFormElement('upcoming');
681 //validate case configuration.
682 $configured = CRM_Case_BAO_Case
::isCaseConfigured();
683 $form->assign('notConfigured', !$configured['configured']);
685 $form->addSearchFieldMetadata(['Case' => self
::getSearchFieldMetadata()]);
686 $form->addFormFieldsFromMetadata();
688 $form->assign('validCiviCase', TRUE);
690 //give options when all cases are accessible.
691 $accessAllCases = FALSE;
692 if (CRM_Core_Permission
::check('access all cases and activities')) {
693 $accessAllCases = TRUE;
694 $caseOwner = [1 => ts('Search All Cases'), 2 => ts('Only My Cases')];
695 $form->addRadio('case_owner', ts('Cases'), $caseOwner);
696 if ($form->get('context') != 'dashboard') {
697 $form->add('checkbox', 'upcoming', ts('Search Cases with Upcoming Activities'));
700 $form->assign('accessAllCases', $accessAllCases);
702 $caseTags = CRM_Core_BAO_Tag
::getColorTags('civicrm_case');
705 $form->add('select2', 'case_tags', ts('Case Tag(s)'), $caseTags, FALSE, ['class' => 'big', 'placeholder' => ts('- select -'), 'multiple' => TRUE]);
708 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_case');
709 CRM_Core_Form_Tag
::buildQuickForm($form, $parentNames, 'civicrm_case', NULL, TRUE, FALSE);
711 self
::addCustomFormFields($form, ['Case']);
713 $form->setDefaults(['case_owner' => 1]);