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