Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Case_BAO_Query { | |
36 | ||
4c6ce474 EM |
37 | /** |
38 | * @param bool $excludeActivityFields | |
39 | * | |
40 | * @return array | |
41 | */ | |
00be9182 | 42 | public static function &getFields($excludeActivityFields = FALSE) { |
6a488035 TO |
43 | $fields = array(); |
44 | $fields = CRM_Case_BAO_Case::exportableFields(); | |
45 | ||
46 | // add activity related fields | |
1cb28d5d | 47 | if (!$excludeActivityFields) { |
48 | $fields = array_merge($fields, CRM_Activity_BAO_Activity::exportableFields('Case')); | |
49 | } | |
6a488035 TO |
50 | |
51 | return $fields; | |
52 | } | |
53 | ||
54 | /** | |
fe482240 | 55 | * Build select for Case. |
6a488035 | 56 | * |
77b97be7 EM |
57 | * @param $query |
58 | * | |
6a488035 | 59 | * @return void |
6a488035 | 60 | */ |
00be9182 | 61 | public static function select(&$query) { |
8cc574cf | 62 | if (($query->_mode & CRM_Contact_BAO_Query::MODE_CASE) || !empty($query->_returnProperties['case_id'])) { |
6a488035 TO |
63 | $query->_select['case_id'] = "civicrm_case.id as case_id"; |
64 | $query->_element['case_id'] = 1; | |
65 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
66 | $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1; | |
67 | } | |
68 | ||
a7488080 | 69 | if (!empty($query->_returnProperties['case_type_id'])) { |
8ffdec17 | 70 | $query->_select['case_type_id'] = "civicrm_case_type.id as case_type_id"; |
6a488035 TO |
71 | $query->_element['case_type_id'] = 1; |
72 | $query->_tables['case_type'] = $query->_whereTables['case_type'] = 1; | |
73 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
74 | } | |
75 | ||
e2751e44 | 76 | if (!empty($query->_returnProperties['case_type'])) { |
8ffdec17 | 77 | $query->_select['case_type'] = "civicrm_case_type.title as case_type"; |
6a488035 | 78 | $query->_element['case_type'] = 1; |
e2751e44 | 79 | $query->_tables['case_type'] = $query->_whereTables['case_type'] = 1; |
6a488035 TO |
80 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; |
81 | } | |
82 | ||
a7488080 | 83 | if (!empty($query->_returnProperties['case_start_date'])) { |
6a488035 TO |
84 | $query->_select['case_start_date'] = "civicrm_case.start_date as case_start_date"; |
85 | $query->_element['case_start_date'] = 1; | |
86 | $query->_tables['civicrm_case'] = 1; | |
87 | } | |
88 | ||
a7488080 | 89 | if (!empty($query->_returnProperties['case_end_date'])) { |
6a488035 TO |
90 | $query->_select['case_end_date'] = "civicrm_case.end_date as case_end_date"; |
91 | $query->_element['case_end_date'] = 1; | |
92 | $query->_tables['civicrm_case'] = 1; | |
93 | } | |
94 | ||
a7488080 | 95 | if (!empty($query->_returnProperties['case_status_id'])) { |
6a488035 TO |
96 | $query->_select['case_status_id'] = "case_status.id as case_status_id"; |
97 | $query->_element['case_status_id'] = 1; | |
98 | $query->_tables['case_status_id'] = $query->_whereTables['case_status_id'] = 1; | |
99 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
100 | } | |
101 | ||
a7488080 | 102 | if (!empty($query->_returnProperties['case_status'])) { |
6a488035 TO |
103 | $query->_select['case_status'] = "case_status.label as case_status"; |
104 | $query->_element['case_status'] = 1; | |
105 | $query->_tables['case_status_id'] = $query->_whereTables['case_status_id'] = 1; | |
106 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
107 | } | |
108 | ||
a7488080 | 109 | if (!empty($query->_returnProperties['case_deleted'])) { |
6a488035 TO |
110 | $query->_select['case_deleted'] = "civicrm_case.is_deleted as case_deleted"; |
111 | $query->_element['case_deleted'] = 1; | |
112 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
113 | } | |
114 | ||
a7488080 | 115 | if (!empty($query->_returnProperties['case_role'])) { |
6a488035 TO |
116 | $query->_select['case_role'] = "case_relation_type.label_b_a as case_role"; |
117 | $query->_element['case_role'] = 1; | |
118 | $query->_tables['case_relationship'] = $query->_whereTables['case_relationship'] = 1; | |
119 | $query->_tables['case_relation_type'] = $query->_whereTables['case_relation_type'] = 1; | |
120 | } | |
121 | ||
a7488080 | 122 | if (!empty($query->_returnProperties['case_recent_activity_date'])) { |
6a488035 TO |
123 | $query->_select['case_recent_activity_date'] = "case_activity.activity_date_time as case_recent_activity_date"; |
124 | $query->_element['case_recent_activity_date'] = 1; | |
125 | $query->_tables['case_activity'] = $query->_whereTables['case_activity'] = 1; | |
126 | } | |
127 | ||
a7488080 | 128 | if (!empty($query->_returnProperties['case_activity_subject'])) { |
6a488035 TO |
129 | $query->_select['case_activity_subject'] = "case_activity.subject as case_activity_subject"; |
130 | $query->_element['case_activity_subject'] = 1; | |
131 | $query->_tables['case_activity'] = 1; | |
132 | $query->_tables['civicrm_case_contact'] = 1; | |
133 | $query->_tables['civicrm_case'] = 1; | |
134 | } | |
135 | ||
a7488080 | 136 | if (!empty($query->_returnProperties['case_subject'])) { |
6a488035 TO |
137 | $query->_select['case_subject'] = "civicrm_case.subject as case_subject"; |
138 | $query->_element['case_subject'] = 1; | |
139 | $query->_tables['civicrm_case_contact'] = 1; | |
140 | $query->_tables['civicrm_case'] = 1; | |
141 | } | |
142 | ||
a7488080 | 143 | if (!empty($query->_returnProperties['case_source_contact_id'])) { |
6a488035 TO |
144 | $query->_select['case_source_contact_id'] = "civicrm_case_reporter.sort_name as case_source_contact_id"; |
145 | $query->_element['case_source_contact_id'] = 1; | |
146 | $query->_tables['civicrm_case_reporter'] = 1; | |
147 | $query->_tables['case_activity'] = 1; | |
148 | $query->_tables['civicrm_case_contact'] = 1; | |
149 | $query->_tables['civicrm_case'] = 1; | |
150 | } | |
151 | ||
a7488080 | 152 | if (!empty($query->_returnProperties['case_activity_status_id'])) { |
6a488035 TO |
153 | $query->_select['case_activity_status_id'] = "rec_activity_status.id as case_activity_status_id"; |
154 | $query->_element['case_activity_status_id'] = 1; | |
155 | $query->_tables['case_activity'] = 1; | |
156 | $query->_tables['recent_activity_status'] = 1; | |
157 | $query->_tables['civicrm_case_contact'] = 1; | |
158 | $query->_tables['civicrm_case'] = 1; | |
159 | } | |
160 | ||
a7488080 | 161 | if (!empty($query->_returnProperties['case_activity_status'])) { |
6a488035 TO |
162 | $query->_select['case_activity_status'] = "rec_activity_status.label as case_activity_status"; |
163 | $query->_element['case_activity_status'] = 1; | |
164 | $query->_tables['case_activity'] = 1; | |
165 | $query->_tables['recent_activity_status'] = 1; | |
166 | $query->_tables['civicrm_case_contact'] = 1; | |
167 | $query->_tables['civicrm_case'] = 1; | |
168 | } | |
169 | ||
a7488080 | 170 | if (!empty($query->_returnProperties['case_activity_duration'])) { |
6a488035 TO |
171 | $query->_select['case_activity_duration'] = "case_activity.duration as case_activity_duration"; |
172 | $query->_element['case_activity_duration'] = 1; | |
173 | $query->_tables['case_activity'] = 1; | |
174 | $query->_tables['civicrm_case_contact'] = 1; | |
175 | $query->_tables['civicrm_case'] = 1; | |
176 | } | |
177 | ||
a7488080 | 178 | if (!empty($query->_returnProperties['case_activity_medium_id'])) { |
6a488035 TO |
179 | $query->_select['case_activity_medium_id'] = "recent_activity_medium.label as case_activity_medium_id"; |
180 | $query->_element['case_activity_medium_id'] = 1; | |
181 | $query->_tables['case_activity'] = 1; | |
182 | $query->_tables['case_activity_medium'] = 1; | |
183 | $query->_tables['civicrm_case_contact'] = 1; | |
184 | $query->_tables['civicrm_case'] = 1; | |
185 | } | |
186 | ||
a7488080 | 187 | if (!empty($query->_returnProperties['case_activity_details'])) { |
6a488035 TO |
188 | $query->_select['case_activity_details'] = "case_activity.details as case_activity_details"; |
189 | $query->_element['case_activity_details'] = 1; | |
190 | $query->_tables['case_activity'] = 1; | |
191 | $query->_tables['civicrm_case_contact'] = 1; | |
192 | $query->_tables['civicrm_case'] = 1; | |
193 | } | |
194 | ||
a7488080 | 195 | if (!empty($query->_returnProperties['case_activity_is_auto'])) { |
6a488035 TO |
196 | $query->_select['case_activity_is_auto'] = "case_activity.is_auto as case_activity_is_auto"; |
197 | $query->_element['case_activity_is_auto'] = 1; | |
198 | $query->_tables['case_activity'] = 1; | |
199 | $query->_tables['civicrm_case_contact'] = 1; | |
200 | $query->_tables['civicrm_case'] = 1; | |
201 | } | |
202 | ||
a7488080 | 203 | if (!empty($query->_returnProperties['case_scheduled_activity_date'])) { |
6a488035 TO |
204 | $query->_select['case_scheduled_activity_date'] = "case_activity.activity_date_time as case_scheduled_activity_date"; |
205 | $query->_element['case_scheduled_activity_date'] = 1; | |
206 | $query->_tables['case_activity'] = 1; | |
207 | $query->_tables['civicrm_case_contact'] = 1; | |
208 | $query->_tables['civicrm_case'] = 1; | |
209 | } | |
a7488080 | 210 | if (!empty($query->_returnProperties['case_recent_activity_type'])) { |
6a488035 TO |
211 | $query->_select['case_recent_activity_type'] = "rec_activity_type.label as case_recent_activity_type"; |
212 | $query->_element['case_recent_activity_type'] = 1; | |
213 | $query->_tables['case_activity'] = 1; | |
214 | $query->_tables['case_activity_type'] = 1; | |
215 | $query->_tables['civicrm_case_contact'] = 1; | |
216 | $query->_tables['civicrm_case'] = 1; | |
217 | } | |
218 | } | |
219 | ||
220 | /** | |
221 | * Given a list of conditions in query generate the required | |
222 | * where clause | |
223 | * | |
77b97be7 EM |
224 | * @param $query |
225 | * | |
6a488035 | 226 | * @return void |
6a488035 | 227 | */ |
00be9182 | 228 | public static function where(&$query) { |
6a488035 TO |
229 | foreach ($query->_params as $id => $values) { |
230 | if (!is_array($values) || count($values) != 5) { | |
231 | continue; | |
232 | } | |
233 | ||
234 | if (substr($query->_params[$id][0], 0, 5) == 'case_') { | |
235 | if ($query->_mode == CRM_Contact_BAO_Query::MODE_CONTACTS) { | |
236 | $query->_useDistinct = TRUE; | |
237 | } | |
238 | $grouping = $query->_params[$id][3]; | |
239 | self::whereClauseSingle($query->_params[$id], $query); | |
240 | } | |
241 | } | |
242 | } | |
243 | ||
244 | /** | |
fe482240 | 245 | * Where clause for a single field. |
6a488035 | 246 | * |
77b97be7 EM |
247 | * @param $values |
248 | * @param $query | |
249 | * | |
6a488035 | 250 | * @return void |
6a488035 | 251 | */ |
00be9182 | 252 | public static function whereClauseSingle(&$values, &$query) { |
6a488035 TO |
253 | list($name, $op, $value, $grouping, $wildcard) = $values; |
254 | $val = $names = array(); | |
255 | switch ($name) { | |
256 | case 'case_status': | |
257 | case 'case_status_id': | |
258 | $statuses = CRM_Case_PseudoConstant::caseStatus(); | |
259 | // Standardize input from checkboxes or single value | |
8ae90f85 | 260 | if (is_array($value) && $query->_mode == CRM_Contact_BAO_Query::MODE_CASE) { |
6a488035 TO |
261 | $value = array_keys($value, 1); |
262 | } | |
263 | foreach ((array) $value as $k) { | |
264 | if ($k && isset($statuses[$k])) { | |
265 | $val[$k] = $k; | |
266 | $names[] = $statuses[$k]; | |
267 | } | |
268 | elseif ($k && ($v = CRM_Utils_Array::key($k, $statuses))) { | |
269 | $val[$v] = $v; | |
270 | $names[] = $k; | |
271 | } | |
272 | } | |
273 | if ($val) { | |
274 | $query->_where[$grouping][] = "civicrm_case.status_id IN (" . implode(',', $val) . ")"; | |
6a488035 | 275 | } |
8ae90f85 | 276 | else { |
277 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_case.status_id', $op, $value, "Integer"); | |
278 | } | |
279 | $query->_qill[$grouping][] = ts('Case Status is %1', array(1 => implode(' ' . ts('or') . ' ', $names))); | |
280 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
6a488035 TO |
281 | return; |
282 | ||
283 | case 'case_type_id': | |
8ae90f85 | 284 | case 'case_type': |
8ffdec17 | 285 | $caseTypes = CRM_Case_PseudoConstant::caseType('title', FALSE); |
6a488035 TO |
286 | |
287 | if (is_array($value)) { | |
288 | foreach ($value as $k => $v) { | |
289 | if ($v) { | |
290 | $val[$k] = $k; | |
291 | $names[] = $caseTypes[$k]; | |
292 | } | |
293 | } | |
294 | } | |
295 | elseif (is_numeric($value)) { | |
296 | $val[$value] = $value; | |
297 | $names[] = $value; | |
298 | } | |
299 | elseif ($caseTypeId = CRM_Utils_Array::key($value, $caseTypes)) { | |
300 | $val[$caseTypeId] = $caseTypeId; | |
301 | $names[] = $caseTypes[$caseTypeId]; | |
302 | } | |
303 | ||
8ae90f85 | 304 | if ($val) { |
305 | $query->_where[$grouping][] = "(civicrm_case.case_type_id IN (" . implode(',', $val) . "))"; | |
306 | } | |
307 | else { | |
308 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause('civicrm_case.case_type_id', $op, $value, "Integer"); | |
309 | } | |
6a488035 TO |
310 | |
311 | $query->_qill[$grouping][] = ts('Case Type is %1', array(1 => implode(' ' . ts('or') . ' ', $names))); | |
312 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
313 | return; | |
314 | ||
315 | case 'case_id': | |
316 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_case.id", $op, $value, 'Int'); | |
317 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
318 | return; | |
319 | ||
320 | case 'case_owner': | |
321 | case 'case_mycases': | |
322 | if (!empty($value)) { | |
323 | if ($value == 2) { | |
324 | $session = CRM_Core_Session::singleton(); | |
325 | $userID = $session->get('userID'); | |
326 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("case_relationship.contact_id_b", $op, $userID, 'Int'); | |
327 | $query->_qill[$grouping][] = ts('Case %1 My Cases', array(1 => $op)); | |
328 | $query->_tables['case_relationship'] = $query->_whereTables['case_relationship'] = 1; | |
329 | } | |
330 | elseif ($value == 1) { | |
331 | $query->_qill[$grouping][] = ts('Case %1 All Cases', array(1 => $op)); | |
332 | $query->_where[$grouping][] = "civicrm_case_contact.contact_id = contact_a.id"; | |
333 | } | |
334 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
335 | $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1; | |
336 | } | |
337 | return; | |
338 | ||
339 | case 'case_deleted': | |
340 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_case.is_deleted", $op, $value, 'Boolean'); | |
341 | if ($value) { | |
342 | $query->_qill[$grouping][] = ts("Find Deleted Cases"); | |
343 | } | |
344 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
345 | return; | |
346 | ||
347 | case 'case_activity_subject': | |
348 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("case_activity.subject", $op, $value, 'String'); | |
349 | $query->_qill[$grouping][] = ts("Activity Subject %1 '%2'", array(1 => $op, 2 => $value)); | |
350 | $query->_tables['case_activity'] = $query->_whereTables['case_activity'] = 1; | |
351 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
352 | $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1; | |
353 | return; | |
354 | ||
355 | case 'case_subject': | |
356 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_case.subject", $op, $value, 'String'); | |
357 | $query->_qill[$grouping][] = ts("Case Subject %1 '%2'", array(1 => $op, 2 => $value)); | |
358 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
359 | $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1; | |
360 | return; | |
361 | ||
362 | case 'case_source_contact_id': | |
363 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_case_reporter.sort_name", $op, $value, 'String'); | |
364 | $query->_qill[$grouping][] = ts("Activity Reporter %1 '%2'", array(1 => $op, 2 => $value)); | |
365 | $query->_tables['case_activity'] = $query->_whereTables['case_activity'] = 1; | |
366 | $query->_tables['civicrm_case_reporter'] = $query->_whereTables['civicrm_case_reporter'] = 1; | |
367 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
368 | $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1; | |
369 | return; | |
370 | ||
371 | case 'case_recent_activity_date': | |
372 | $date = CRM_Utils_Date::format($value); | |
373 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("case_activity.activity_date_time", $op, $date, 'Date'); | |
374 | if ($date) { | |
375 | $date = CRM_Utils_Date::customFormat($date); | |
376 | $query->_qill[$grouping][] = ts("Activity Actual Date %1 %2", array(1 => $op, 2 => $date)); | |
377 | } | |
378 | $query->_tables['case_activity'] = $query->_whereTables['case_activity'] = 1; | |
379 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
380 | $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1; | |
381 | return; | |
382 | ||
383 | case 'case_scheduled_activity_date': | |
384 | $date = CRM_Utils_Date::format($value); | |
385 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("case_activity.activity_date_time", $op, $date, 'Date'); | |
386 | if ($date) { | |
387 | $date = CRM_Utils_Date::customFormat($date); | |
388 | $query->_qill[$grouping][] = ts("Activity Schedule Date %1 %2", array(1 => $op, 2 => $date)); | |
389 | } | |
390 | $query->_tables['case_activity'] = $query->_whereTables['case_activity'] = 1; | |
391 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
392 | $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1; | |
393 | return; | |
394 | ||
395 | case 'case_recent_activity_type': | |
396 | $names = $value; | |
397 | if ($activityType = CRM_Core_OptionGroup::getLabel('activity_type', $value, 'value')) { | |
398 | $names = $activityType; | |
399 | } | |
400 | ||
401 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("case_activity.activity_type_id", $op, $value, 'Int'); | |
402 | $query->_qill[$grouping][] = ts("Activity Type %1 %2", array(1 => $op, 2 => $names)); | |
403 | $query->_tables['case_activity'] = $query->_whereTables['case_activity'] = 1; | |
404 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
405 | $query->_tables['case_activity_type'] = 1; | |
406 | $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1; | |
407 | return; | |
408 | ||
409 | case 'case_activity_status_id': | |
410 | $names = $value; | |
411 | if ($activityStatus = CRM_Core_OptionGroup::getLabel('activity_status', $value, 'value')) { | |
412 | $names = $activityStatus; | |
413 | } | |
414 | ||
415 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("case_activity.status_id", $op, $value, 'Int'); | |
416 | $query->_qill[$grouping][] = ts("Activity Type %1 %2", array(1 => $op, 2 => $names)); | |
417 | $query->_tables['case_activity'] = $query->_whereTables['case_activity'] = 1; | |
418 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
419 | $query->_tables['case_activity_status'] = 1; | |
420 | $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1; | |
421 | return; | |
422 | ||
423 | case 'case_activity_duration': | |
424 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("case_activity.duration", $op, $value, 'Int'); | |
425 | $query->_qill[$grouping][] = ts("Activity Duration %1 %2", array(1 => $op, 2 => $value)); | |
426 | $query->_tables['case_activity'] = $query->_whereTables['case_activity'] = 1; | |
427 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
428 | $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1; | |
429 | return; | |
430 | ||
431 | case 'case_activity_medium_id': | |
432 | $names = $value; | |
433 | if ($activityMedium = CRM_Core_OptionGroup::getLabel('encounter_medium', $value, 'value')) { | |
434 | $names = $activityMedium; | |
435 | } | |
436 | ||
437 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("case_activity.medium_id", $op, $value, 'Int'); | |
438 | $query->_qill[$grouping][] = ts("Activity Medium %1 %2", array(1 => $op, 2 => $names)); | |
439 | $query->_tables['case_activity'] = $query->_whereTables['case_activity'] = 1; | |
440 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
441 | $query->_tables['case_activity_medium'] = 1; | |
442 | $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1; | |
443 | return; | |
444 | ||
445 | case 'case_activity_details': | |
446 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("case_activity.details", $op, $value, 'String'); | |
447 | $query->_qill[$grouping][] = ts("Activity Details %1 '%2'", array(1 => $op, 2 => $value)); | |
448 | $query->_tables['case_activity'] = $query->_whereTables['case_activity'] = 1; | |
449 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
450 | $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1; | |
451 | return; | |
452 | ||
453 | case 'case_activity_is_auto': | |
454 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("case_activity.is_auto", $op, $value, 'Boolean'); | |
455 | $query->_qill[$grouping][] = ts("Activity Auto Genrated %1 '%2'", array(1 => $op, 2 => $value)); | |
456 | $query->_tables['case_activity'] = $query->_whereTables['case_activity'] = 1; | |
457 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
458 | $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1; | |
459 | return; | |
460 | ||
461 | // adding where clause for case_role | |
462 | ||
463 | case 'case_role': | |
464 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("case_relation_type.name_b_a", $op, $value, 'String'); | |
465 | $query->_qill[$grouping][] = ts("Role in Case %1 '%2'", array(1 => $op, 2 => $value)); | |
466 | $query->_tables['case_relation_type'] = $query->_whereTables['case_relationship_type'] = 1; | |
467 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
468 | $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1; | |
469 | return; | |
470 | ||
471 | case 'case_from_start_date_low': | |
472 | case 'case_from_start_date_high': | |
473 | $query->dateQueryBuilder($values, | |
474 | 'civicrm_case', 'case_from_start_date', 'start_date', 'Start Date' | |
475 | ); | |
476 | return; | |
477 | ||
478 | case 'case_to_end_date_low': | |
479 | case 'case_to_end_date_high': | |
480 | $query->dateQueryBuilder($values, | |
481 | 'civicrm_case', 'case_to_end_date', 'end_date', 'End Date' | |
482 | ); | |
483 | return; | |
484 | ||
485 | case 'case_start_date': | |
486 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_case.start_date", $op, $value, 'Int'); | |
487 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
488 | return; | |
489 | ||
490 | case 'case_end_date': | |
491 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_case.end_date", $op, $value, 'Int'); | |
492 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
493 | return; | |
494 | ||
495 | case 'case_taglist': | |
496 | $taglist = $value; | |
497 | $value = array(); | |
498 | foreach ($taglist as $val) { | |
499 | if ($val) { | |
500 | $val = explode(',', $val); | |
501 | foreach ($val as $tId) { | |
502 | if (is_numeric($tId)) { | |
503 | $value[$tId] = 1; | |
504 | } | |
505 | } | |
506 | } | |
507 | } | |
508 | case 'case_tags': | |
cd43c5e3 | 509 | $tags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE)); |
6a488035 TO |
510 | |
511 | if (is_array($value)) { | |
512 | foreach ($value as $k => $v) { | |
513 | if ($v) { | |
514 | $val[$k] = $k; | |
515 | $names[] = $tags[$k]; | |
516 | } | |
517 | } | |
518 | } | |
519 | ||
520 | $query->_where[$grouping][] = " civicrm_case_tag.tag_id IN (" . implode(',', $val) . " )"; | |
521 | $query->_qill[$grouping][] = ts('Case Tags %1', array(1 => $op)) . ' ' . implode(' ' . ts('or') . ' ', $names); | |
522 | $query->_tables['civicrm_case'] = $query->_whereTables['civicrm_case'] = 1; | |
523 | $query->_tables['civicrm_case_contact'] = $query->_whereTables['civicrm_case_contact'] = 1; | |
524 | $query->_tables['civicrm_case_tag'] = $query->_whereTables['civicrm_case_tag'] = 1; | |
525 | return; | |
526 | } | |
527 | } | |
528 | ||
4c6ce474 | 529 | /** |
100fef9d | 530 | * @param string $name |
4c6ce474 EM |
531 | * @param $mode |
532 | * @param $side | |
533 | * | |
534 | * @return string | |
535 | */ | |
00be9182 | 536 | public static function from($name, $mode, $side) { |
6a488035 TO |
537 | $from = ""; |
538 | ||
539 | switch ($name) { | |
540 | case 'civicrm_case_contact': | |
541 | $from .= " $side JOIN civicrm_case_contact ON civicrm_case_contact.contact_id = contact_a.id "; | |
542 | break; | |
543 | ||
544 | case 'civicrm_case_reporter': | |
962c0eda | 545 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
546 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); | |
547 | $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 | 548 | $from .= " $side JOIN civicrm_contact as civicrm_case_reporter ON case_activity_contact.contact_id = civicrm_case_reporter.id "; |
6a488035 TO |
549 | break; |
550 | ||
551 | case 'civicrm_case': | |
552 | $from .= " INNER JOIN civicrm_case ON civicrm_case_contact.case_id = civicrm_case.id"; | |
553 | break; | |
554 | ||
555 | case 'case_status_id': | |
556 | $from .= " $side JOIN civicrm_option_group option_group_case_status ON (option_group_case_status.name = 'case_status')"; | |
557 | $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 ) "; | |
558 | break; | |
559 | ||
560 | case 'case_type': | |
8ffdec17 | 561 | $from .= " $side JOIN civicrm_case_type ON civicrm_case.case_type_id = civicrm_case_type.id "; |
6a488035 TO |
562 | break; |
563 | ||
564 | case 'case_activity_type': | |
565 | $from .= " $side JOIN civicrm_option_group option_group_activity_type ON (option_group_activity_type.name = 'activity_type')"; | |
566 | $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 ) "; | |
567 | break; | |
568 | ||
569 | case 'recent_activity_status': | |
570 | $from .= " $side JOIN civicrm_option_group option_group_activity_status ON (option_group_activity_status.name = 'activity_status')"; | |
571 | $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 ) "; | |
572 | break; | |
573 | ||
574 | case 'case_relationship': | |
575 | $session = CRM_Core_Session::singleton(); | |
353ffa53 TO |
576 | $userID = $session->get('userID'); |
577 | $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 )"; | |
6a488035 TO |
578 | break; |
579 | ||
580 | case 'case_relation_type': | |
581 | $from .= " $side JOIN civicrm_relationship_type case_relation_type ON ( case_relation_type.id = case_relationship.relationship_type_id AND | |
582 | case_relation_type.id = case_relationship.relationship_type_id )"; | |
583 | break; | |
584 | ||
585 | case 'case_activity_medium': | |
586 | $from .= " $side JOIN civicrm_option_group option_group_activity_medium ON (option_group_activity_medium.name = 'encounter_medium')"; | |
587 | $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 ) "; | |
588 | break; | |
589 | ||
590 | case 'case_activity': | |
591 | $from .= " INNER JOIN civicrm_case_activity ON civicrm_case_activity.case_id = civicrm_case.id "; | |
592 | $from .= " INNER JOIN civicrm_activity case_activity ON ( civicrm_case_activity.activity_id = case_activity.id | |
593 | AND case_activity.is_current_revision = 1 )"; | |
594 | break; | |
595 | ||
596 | case 'civicrm_case_tag': | |
597 | $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 ) "; | |
598 | break; | |
599 | } | |
600 | return $from; | |
601 | } | |
602 | ||
603 | /** | |
fe482240 | 604 | * Getter for the qill object. |
6a488035 TO |
605 | * |
606 | * @return string | |
6a488035 | 607 | */ |
00be9182 | 608 | public function qill() { |
6a488035 TO |
609 | return (isset($this->_qill)) ? $this->_qill : ""; |
610 | } | |
611 | ||
4c6ce474 EM |
612 | /** |
613 | * @param $mode | |
614 | * @param bool $includeCustomFields | |
615 | * | |
616 | * @return array|null | |
617 | */ | |
e7483cbe | 618 | public static function defaultReturnProperties( |
28d4d481 | 619 | $mode, |
6a488035 TO |
620 | $includeCustomFields = TRUE |
621 | ) { | |
622 | ||
623 | $properties = NULL; | |
624 | ||
625 | if ($mode & CRM_Contact_BAO_Query::MODE_CASE) { | |
626 | $properties = array( | |
627 | 'contact_type' => 1, | |
628 | 'contact_sub_type' => 1, | |
629 | 'contact_id' => 1, | |
630 | 'sort_name' => 1, | |
631 | 'display_name' => 1, | |
632 | 'case_id' => 1, | |
633 | 'case_activity_subject' => 1, | |
634 | 'case_subject' => 1, | |
635 | 'case_status' => 1, | |
636 | 'case_type' => 1, | |
637 | 'case_role' => 1, | |
638 | 'case_deleted' => 1, | |
639 | 'case_recent_activity_date' => 1, | |
640 | 'case_recent_activity_type' => 1, | |
641 | 'case_scheduled_activity_date' => 1, | |
642 | 'phone' => 1, | |
643 | // 'case_scheduled_activity_type'=> 1 | |
644 | ); | |
645 | ||
646 | if ($includeCustomFields) { | |
647 | // also get all the custom case properties | |
648 | $fields = CRM_Core_BAO_CustomField::getFieldsForImport('Case'); | |
649 | if (!empty($fields)) { | |
650 | foreach ($fields as $name => $dontCare) { | |
651 | $properties[$name] = 1; | |
652 | } | |
653 | } | |
654 | } | |
655 | } | |
656 | ||
657 | return $properties; | |
658 | } | |
659 | ||
660 | /** | |
fe482240 | 661 | * This includes any extra fields that might need for export etc. |
6a488035 | 662 | */ |
00be9182 | 663 | public static function extraReturnProperties($mode) { |
6a488035 TO |
664 | $properties = NULL; |
665 | ||
666 | if ($mode & CRM_Contact_BAO_Query::MODE_CASE) { | |
667 | $properties = array( | |
668 | 'case_start_date' => 1, | |
669 | 'case_end_date' => 1, | |
670 | 'case_subject' => 1, | |
671 | 'case_source_contact_id' => 1, | |
672 | 'case_activity_status' => 1, | |
673 | 'case_activity_duration' => 1, | |
674 | 'case_activity_medium_id' => 1, | |
675 | 'case_activity_details' => 1, | |
676 | 'case_activity_is_auto' => 1, | |
677 | ); | |
678 | } | |
679 | return $properties; | |
680 | } | |
681 | ||
4c6ce474 EM |
682 | /** |
683 | * @param $tables | |
684 | */ | |
00be9182 | 685 | public static function tableNames(&$tables) { |
a7488080 | 686 | if (!empty($tables['civicrm_case'])) { |
6a488035 TO |
687 | $tables = array_merge(array('civicrm_case_contact' => 1), $tables); |
688 | } | |
689 | ||
a7488080 | 690 | if (!empty($tables['case_relation_type'])) { |
6a488035 TO |
691 | $tables = array_merge(array('case_relationship' => 1), $tables); |
692 | } | |
693 | } | |
694 | ||
695 | /** | |
fe482240 | 696 | * Add all the elements shared between case search and advanaced search. |
6a488035 | 697 | * |
6a488035 | 698 | * |
c490a46a | 699 | * @param CRM_Core_Form $form |
2a6da8d7 | 700 | * |
6a488035 | 701 | * @return void |
6a488035 | 702 | */ |
00be9182 | 703 | public static function buildSearchForm(&$form) { |
6a488035 TO |
704 | $config = CRM_Core_Config::singleton(); |
705 | ||
706 | //validate case configuration. | |
707 | $configured = CRM_Case_BAO_Case::isCaseConfigured(); | |
708 | $form->assign('notConfigured', !$configured['configured']); | |
709 | ||
0372ffa2 | 710 | $caseTypes = CRM_Case_PseudoConstant::caseType('title', FALSE); |
6a488035 TO |
711 | foreach ($caseTypes as $id => $name) { |
712 | $form->addElement('checkbox', "case_type_id[$id]", NULL, $name); | |
713 | } | |
714 | ||
715 | $statuses = CRM_Case_PseudoConstant::caseStatus('label', FALSE); | |
716 | foreach ($statuses as $id => $name) { | |
717 | $form->addElement('checkbox', "case_status_id[$id]", NULL, $name); | |
718 | } | |
719 | ||
bc3f7f04 | 720 | CRM_Core_Form_Date::buildDateRange($form, 'case_from', 1, '_start_date_low', '_start_date_high', ts('From'), FALSE); |
e547f744 | 721 | CRM_Core_Form_Date::buildDateRange($form, 'case_to', 1, '_end_date_low', '_end_date_high', ts('From'), FALSE); |
6a488035 TO |
722 | |
723 | $form->assign('validCiviCase', TRUE); | |
724 | ||
725 | //give options when all cases are accessible. | |
726 | $accessAllCases = FALSE; | |
727 | if (CRM_Core_Permission::check('access all cases and activities')) { | |
728 | $accessAllCases = TRUE; | |
729 | $caseOwner = array(1 => ts('Search All Cases'), 2 => ts('Only My Cases')); | |
730 | $form->addRadio('case_owner', ts('Cases'), $caseOwner); | |
731 | } | |
732 | $form->assign('accessAllCases', $accessAllCases); | |
733 | ||
734 | $caseTags = CRM_Core_BAO_Tag::getTags('civicrm_case'); | |
735 | ||
736 | if ($caseTags) { | |
737 | foreach ($caseTags as $tagID => $tagName) { | |
738 | $form->_tagElement = &$form->addElement('checkbox', "case_tags[$tagID]", NULL, $tagName); | |
739 | } | |
740 | } | |
741 | ||
742 | $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_case'); | |
4dff5e17 | 743 | CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_case', NULL, TRUE, FALSE); |
6a488035 TO |
744 | |
745 | if (CRM_Core_Permission::check('administer CiviCRM')) { | |
746 | $form->addElement('checkbox', 'case_deleted', ts('Deleted Cases')); | |
747 | } | |
748 | ||
749 | // add all the custom searchable fields | |
750 | $extends = array('Case'); | |
751 | $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE, $extends); | |
752 | if ($groupDetails) { | |
753 | $form->assign('caseGroupTree', $groupDetails); | |
754 | foreach ($groupDetails as $group) { | |
755 | foreach ($group['fields'] as $field) { | |
756 | $fieldId = $field['id']; | |
757 | $elementName = 'custom_' . $fieldId; | |
758 | CRM_Core_BAO_CustomField::addQuickFormElement($form, | |
759 | $elementName, | |
760 | $fieldId, | |
761 | FALSE, FALSE, TRUE | |
762 | ); | |
763 | } | |
764 | } | |
765 | } | |
766 | $form->setDefaults(array('case_owner' => 1)); | |
767 | } | |
768 | ||
4c6ce474 EM |
769 | /** |
770 | * @param $row | |
100fef9d | 771 | * @param int $id |
4c6ce474 | 772 | */ |
e547f744 TO |
773 | public static function searchAction(&$row, $id) { |
774 | } | |
96025800 | 775 | |
6a488035 | 776 | } |