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