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