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