3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
33 class CRM_Case_BAO_Query
{
38 * @param bool $excludeActivityFields
42 public static function &getFields($excludeActivityFields = FALSE) {
44 $fields = CRM_Case_BAO_Case
::exportableFields();
46 // add activity related fields
47 if (!$excludeActivityFields) {
48 $fields = array_merge($fields, CRM_Activity_BAO_Activity
::exportableFields('Case'));
55 * Build select for Case.
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
219 * Given a list of conditions in query generate the required where clause.
221 * @param CRM_Case_BAO_Query $query
223 public static function where(&$query) {
224 foreach ($query->_params
as $id => $values) {
225 if (!is_array($values) ||
count($values) != 5) {
229 if (substr($query->_params
[$id][0], 0, 5) == 'case_') {
230 if ($query->_mode
== CRM_Contact_BAO_Query
::MODE_CONTACTS
) {
231 $query->_useDistinct
= TRUE;
233 $grouping = $query->_params
[$id][3];
234 self
::whereClauseSingle($query->_params
[$id], $query);
240 * Where clause for a single field.
242 * @param array $values
243 * @param CRM_Case_BAO_Query s$query
245 public static function whereClauseSingle(&$values, &$query) {
246 list($name, $op, $value, $grouping, $wildcard) = $values;
247 $val = $names = array();
253 case 'case_status_id':
256 if (strpos($name, 'type')) {
257 $name = 'case_type_id';
258 $label = 'Case Type(s)';
260 elseif (strpos($name, 'status')) {
262 $label = 'Case Status(s)';
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);
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;
278 if (!empty($value)) {
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;
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";
290 $query->_tables
['civicrm_case'] = $query->_whereTables
['civicrm_case'] = 1;
291 $query->_tables
['civicrm_case_contact'] = $query->_whereTables
['civicrm_case_contact'] = 1;
296 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_case.is_deleted", $op, $value, 'Boolean');
298 $query->_qill
[$grouping][] = ts("Find Deleted Cases");
300 $query->_tables
['civicrm_case'] = $query->_whereTables
['civicrm_case'] = 1;
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;
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;
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;
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');
331 $date = CRM_Utils_Date
::customFormat($date);
332 $query->_qill
[$grouping][] = ts("Activity Actual Date %1 %2", array(1 => $op, 2 => $date));
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;
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');
343 $date = CRM_Utils_Date
::customFormat($date);
344 $query->_qill
[$grouping][] = ts("Activity Schedule Date %1 %2", array(1 => $op, 2 => $date));
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;
351 case 'case_recent_activity_type':
353 if ($activityType = CRM_Core_OptionGroup
::getLabel('activity_type', $value, 'value')) {
354 $names = $activityType;
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;
365 case 'case_activity_status_id':
367 if ($activityStatus = CRM_Core_OptionGroup
::getLabel('activity_status', $value, 'value')) {
368 $names = $activityStatus;
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;
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;
387 case 'case_activity_medium_id':
389 if ($activityMedium = CRM_Core_OptionGroup
::getLabel('encounter_medium', $value, 'value')) {
390 $names = $activityMedium;
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;
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;
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;
417 // adding where clause for 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;
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'
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'
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;
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;
454 foreach ($taglist as $val) {
456 $val = explode(',', $val);
457 foreach ($val as $tId) {
458 if (is_numeric($tId)) {
465 $tags = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
467 if (is_array($value)) {
468 foreach ($value as $k => $v) {
471 $names[] = $tags[$k];
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;
486 * @param string $name
492 public static function from($name, $mode, $side) {
496 case 'civicrm_case_contact':
497 $from .= " $side JOIN civicrm_case_contact ON civicrm_case_contact.contact_id = contact_a.id ";
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 ";
508 $from .= " INNER JOIN civicrm_case ON civicrm_case_contact.case_id = civicrm_case.id";
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 ) ";
517 $from .= " $side JOIN civicrm_case_type ON civicrm_case.case_type_id = civicrm_case_type.id ";
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 ) ";
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 ) ";
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 )";
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 )";
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 ) ";
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 )";
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 ) ";
560 * Getter for the qill object.
564 public function qill() {
565 return (isset($this->_qill
)) ?
$this->_qill
: "";
570 * @param bool $includeCustomFields
574 public static function defaultReturnProperties(
576 $includeCustomFields = TRUE
581 if ($mode & CRM_Contact_BAO_Query
::MODE_CASE
) {
584 'contact_sub_type' => 1,
589 'case_activity_subject' => 1,
595 'case_recent_activity_date' => 1,
596 'case_recent_activity_type' => 1,
597 'case_scheduled_activity_date' => 1,
599 // 'case_scheduled_activity_type'=> 1
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;
617 * This includes any extra fields that might need for export etc.
619 public static function extraReturnProperties($mode) {
622 if ($mode & CRM_Contact_BAO_Query
::MODE_CASE
) {
624 'case_start_date' => 1,
625 'case_end_date' => 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,
641 public static function tableNames(&$tables) {
642 if (!empty($tables['civicrm_case'])) {
643 $tables = array_merge(array('civicrm_case_contact' => 1), $tables);
646 if (!empty($tables['case_relation_type'])) {
647 $tables = array_merge(array('case_relationship' => 1), $tables);
652 * Add all the elements shared between case search and advanced search.
654 * @param CRM_Core_Form $form
656 public static function buildSearchForm(&$form) {
657 $config = CRM_Core_Config
::singleton();
659 //validate case configuration.
660 $configured = CRM_Case_BAO_Case
::isCaseConfigured();
661 $form->assign('notConfigured', !$configured['configured']);
663 $form->add('select', 'case_type_id',
665 CRM_Case_PseudoConstant
::caseType('title', FALSE),
666 FALSE, array('class' => 'crm-select2', 'multiple' => 'multiple')
669 $form->add('select', 'case_status_id',
671 CRM_Case_PseudoConstant
::caseStatus('label', FALSE),
672 FALSE, array('class' => 'crm-select2', 'multiple' => 'multiple')
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);
678 $form->assign('validCiviCase', TRUE);
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);
687 $form->assign('accessAllCases', $accessAllCases);
689 $caseTags = CRM_Core_BAO_Tag
::getTags('civicrm_case');
692 foreach ($caseTags as $tagID => $tagName) {
693 $form->_tagElement
= &$form->addElement('checkbox', "case_tags[$tagID]", NULL, $tagName);
697 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_case');
698 CRM_Core_Form_Tag
::buildQuickForm($form, $parentNames, 'civicrm_case', NULL, TRUE, FALSE);
700 if (CRM_Core_Permission
::check('administer CiviCRM')) {
701 $form->addElement('checkbox', 'case_deleted', ts('Deleted Cases'));
704 // add all the custom searchable fields
705 $extends = array('Case');
706 $groupDetails = CRM_Core_BAO_CustomGroup
::getGroupDetail(NULL, TRUE, $extends);
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,
721 $form->setDefaults(array('case_owner' => 1));
728 public static function searchAction(&$row, $id) {