| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * This class contains the functions for Case Management |
| 38 | * |
| 39 | */ |
| 40 | class CRM_Case_BAO_Case extends CRM_Case_DAO_Case { |
| 41 | |
| 42 | /** |
| 43 | * static field for all the case information that we can potentially export |
| 44 | * |
| 45 | * @var array |
| 46 | * @static |
| 47 | */ |
| 48 | static $_exportableFields = NULL; |
| 49 | |
| 50 | /** |
| 51 | * |
| 52 | */ |
| 53 | function __construct() { |
| 54 | parent::__construct(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * takes an associative array and creates a case object |
| 59 | * |
| 60 | * the function extract all the params it needs to initialize the create a |
| 61 | * case object. the params array could contain additional unused name/value |
| 62 | * pairs |
| 63 | * |
| 64 | * @param array $params (reference ) an assoc array of name/value pairs |
| 65 | * |
| 66 | * @internal param array $ids the array that holds all the db ids |
| 67 | * |
| 68 | * @return object CRM_Case_BAO_Case object |
| 69 | * @access public |
| 70 | * @static |
| 71 | */ |
| 72 | static function add(&$params) { |
| 73 | $caseDAO = new CRM_Case_DAO_Case(); |
| 74 | $caseDAO->copyValues($params); |
| 75 | return $caseDAO->save(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Given the list of params in the params array, fetch the object |
| 80 | * and store the values in the values array |
| 81 | * |
| 82 | * @param array $params input parameters to find object |
| 83 | * @param array $values output values of the object |
| 84 | * @param array $ids the array that holds all the db ids |
| 85 | * |
| 86 | * @return CRM_Case_BAO_Case|null the found object or null |
| 87 | * @access public |
| 88 | * @static |
| 89 | */ |
| 90 | static function &getValues(&$params, &$values, &$ids) { |
| 91 | $case = new CRM_Case_BAO_Case(); |
| 92 | |
| 93 | $case->copyValues($params); |
| 94 | |
| 95 | if ($case->find(TRUE)) { |
| 96 | $ids['case'] = $case->id; |
| 97 | CRM_Core_DAO::storeValues($case, $values); |
| 98 | return $case; |
| 99 | } |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * takes an associative array and creates a case object |
| 105 | * |
| 106 | * @param array $params (reference ) an assoc array of name/value pairs |
| 107 | * |
| 108 | * @internal param array $ids the array that holds all the db ids |
| 109 | * |
| 110 | * @return object CRM_Case_BAO_Case object |
| 111 | * @access public |
| 112 | * @static |
| 113 | */ |
| 114 | static function &create(&$params) { |
| 115 | $transaction = new CRM_Core_Transaction(); |
| 116 | |
| 117 | if (!empty($params['id'])) { |
| 118 | CRM_Utils_Hook::pre('edit', 'Case', $params['id'], $params); |
| 119 | } |
| 120 | else { |
| 121 | CRM_Utils_Hook::pre('create', 'Case', NULL, $params); |
| 122 | } |
| 123 | |
| 124 | $case = self::add($params); |
| 125 | |
| 126 | if (!empty($params['custom']) && |
| 127 | is_array($params['custom']) |
| 128 | ) { |
| 129 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_case', $case->id); |
| 130 | } |
| 131 | |
| 132 | if (is_a($case, 'CRM_Core_Error')) { |
| 133 | $transaction->rollback(); |
| 134 | return $case; |
| 135 | } |
| 136 | |
| 137 | if (!empty($params['id'])) { |
| 138 | CRM_Utils_Hook::post('edit', 'Case', $case->id, $case); |
| 139 | } |
| 140 | else { |
| 141 | CRM_Utils_Hook::post('create', 'Case', $case->id, $case); |
| 142 | } |
| 143 | $transaction->commit(); |
| 144 | |
| 145 | //we are not creating log for case |
| 146 | //since case log can be tracked using log for activity. |
| 147 | return $case; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Create case contact record |
| 152 | * |
| 153 | * @param array case_id, contact_id |
| 154 | * |
| 155 | * @return object |
| 156 | * @access public |
| 157 | */ |
| 158 | static function addCaseToContact($params) { |
| 159 | $caseContact = new CRM_Case_DAO_CaseContact(); |
| 160 | $caseContact->case_id = $params['case_id']; |
| 161 | $caseContact->contact_id = $params['contact_id']; |
| 162 | $caseContact->find(TRUE); |
| 163 | $caseContact->save(); |
| 164 | |
| 165 | // add to recently viewed |
| 166 | $caseType = CRM_Case_BAO_Case::getCaseType($caseContact->case_id); |
| 167 | $url = CRM_Utils_System::url('civicrm/contact/view/case', |
| 168 | "action=view&reset=1&id={$caseContact->case_id}&cid={$caseContact->contact_id}&context=home" |
| 169 | ); |
| 170 | |
| 171 | $title = CRM_Contact_BAO_Contact::displayName($caseContact->contact_id) . ' - ' . $caseType; |
| 172 | |
| 173 | $recentOther = array(); |
| 174 | if (CRM_Core_Permission::checkActionPermission('CiviCase', CRM_Core_Action::DELETE)) { |
| 175 | $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/case', |
| 176 | "action=delete&reset=1&id={$caseContact->case_id}&cid={$caseContact->contact_id}&context=home" |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | // add the recently created case |
| 181 | CRM_Utils_Recent::add($title, |
| 182 | $url, |
| 183 | $caseContact->case_id, |
| 184 | 'Case', |
| 185 | $params['contact_id'], |
| 186 | NULL, |
| 187 | $recentOther |
| 188 | ); |
| 189 | |
| 190 | return $caseContact; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Delet case contact record |
| 195 | * |
| 196 | * @param int case_id |
| 197 | * |
| 198 | * @return Void |
| 199 | * @access public |
| 200 | */ |
| 201 | static function deleteCaseContact($caseID) { |
| 202 | $caseContact = new CRM_Case_DAO_CaseContact(); |
| 203 | $caseContact->case_id = $caseID; |
| 204 | $caseContact->delete(); |
| 205 | |
| 206 | // delete the recently created Case |
| 207 | $caseRecent = array( |
| 208 | 'id' => $caseID, |
| 209 | 'type' => 'Case', |
| 210 | ); |
| 211 | CRM_Utils_Recent::del($caseRecent); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * This function is used to convert associative array names to values |
| 216 | * and vice-versa. |
| 217 | * |
| 218 | * This function is used by both the web form layer and the api. Note that |
| 219 | * the api needs the name => value conversion, also the view layer typically |
| 220 | * requires value => name conversion |
| 221 | */ |
| 222 | static function lookupValue(&$defaults, $property, &$lookup, $reverse) { |
| 223 | $id = $property . '_id'; |
| 224 | |
| 225 | $src = $reverse ? $property : $id; |
| 226 | $dst = $reverse ? $id : $property; |
| 227 | |
| 228 | if (!array_key_exists($src, $defaults)) { |
| 229 | return FALSE; |
| 230 | } |
| 231 | |
| 232 | $look = $reverse ? array_flip($lookup) : $lookup; |
| 233 | |
| 234 | if (is_array($look)) { |
| 235 | if (!array_key_exists($defaults[$src], $look)) { |
| 236 | return FALSE; |
| 237 | } |
| 238 | } |
| 239 | $defaults[$dst] = $look[$defaults[$src]]; |
| 240 | return TRUE; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Takes a bunch of params that are needed to match certain criteria and |
| 245 | * retrieves the relevant objects. We'll tweak this function to be more |
| 246 | * full featured over a period of time. This is the inverse function of |
| 247 | * create. It also stores all the retrieved values in the default array |
| 248 | * |
| 249 | * @param array $params (reference ) an assoc array of name/value pairs |
| 250 | * @param array $defaults (reference ) an assoc array to hold the name / value pairs |
| 251 | * in a hierarchical manner |
| 252 | * @param array $ids (reference) the array that holds all the db ids |
| 253 | * |
| 254 | * @return object CRM_Case_BAO_Case object |
| 255 | * @access public |
| 256 | * @static |
| 257 | */ |
| 258 | static function retrieve(&$params, &$defaults, &$ids) { |
| 259 | $case = CRM_Case_BAO_Case::getValues($params, $defaults, $ids); |
| 260 | return $case; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Function to process case activity add/delete |
| 265 | * takes an associative array and |
| 266 | * |
| 267 | * @param array $params (reference ) an assoc array of name/value pairs |
| 268 | * |
| 269 | * @access public |
| 270 | * @static |
| 271 | */ |
| 272 | static function processCaseActivity(&$params) { |
| 273 | $caseActivityDAO = new CRM_Case_DAO_CaseActivity(); |
| 274 | $caseActivityDAO->activity_id = $params['activity_id']; |
| 275 | $caseActivityDAO->case_id = $params['case_id']; |
| 276 | |
| 277 | $caseActivityDAO->find(TRUE); |
| 278 | $caseActivityDAO->save(); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Function to get the case subject for Activity |
| 283 | * |
| 284 | * @param int $activityId activity id |
| 285 | * |
| 286 | * @return case subject or null |
| 287 | * @access public |
| 288 | * @static |
| 289 | */ |
| 290 | static function getCaseSubject($activityId) { |
| 291 | $caseActivity = new CRM_Case_DAO_CaseActivity(); |
| 292 | $caseActivity->activity_id = $activityId; |
| 293 | if ($caseActivity->find(TRUE)) { |
| 294 | return CRM_Core_DAO::getFieldValue('CRM_Case_BAO_Case', $caseActivity->case_id, 'subject'); |
| 295 | } |
| 296 | return NULL; |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Function to get the case type. |
| 301 | * |
| 302 | * @param int $caseId |
| 303 | * |
| 304 | * @param string $colName |
| 305 | * |
| 306 | * @return case type |
| 307 | * @access public |
| 308 | * @static |
| 309 | */ |
| 310 | static function getCaseType($caseId, $colName = 'title') { |
| 311 | $query = " |
| 312 | SELECT civicrm_case_type.{$colName} FROM civicrm_case |
| 313 | LEFT JOIN civicrm_case_type ON |
| 314 | civicrm_case.case_type_id = civicrm_case_type.id |
| 315 | WHERE civicrm_case.id = %1"; |
| 316 | |
| 317 | $queryParams = array(1 => array($caseId, 'Integer')); |
| 318 | |
| 319 | return CRM_Core_DAO::singleValueQuery($query, $queryParams); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Delete the record that are associated with this case |
| 324 | * record are deleted from case |
| 325 | * |
| 326 | * @param int $caseId id of the case to delete |
| 327 | * |
| 328 | * @param bool $moveToTrash |
| 329 | * |
| 330 | * @return bool is successful |
| 331 | * @access public |
| 332 | * @static |
| 333 | */ |
| 334 | static function deleteCase($caseId, $moveToTrash = FALSE) { |
| 335 | CRM_Utils_Hook::pre('delete', 'Case', $caseId, CRM_Core_DAO::$_nullArray); |
| 336 | |
| 337 | //delete activities |
| 338 | $activities = self::getCaseActivityDates($caseId); |
| 339 | if ($activities) { |
| 340 | foreach ($activities as $value) { |
| 341 | CRM_Activity_BAO_Activity::deleteActivity($value, $moveToTrash); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | if (!$moveToTrash) { |
| 346 | $transaction = new CRM_Core_Transaction(); |
| 347 | } |
| 348 | $case = new CRM_Case_DAO_Case(); |
| 349 | $case->id = $caseId; |
| 350 | if (!$moveToTrash) { |
| 351 | $result = $case->delete(); |
| 352 | $transaction->commit(); |
| 353 | } |
| 354 | else { |
| 355 | $result = $case->is_deleted = 1; |
| 356 | $case->save(); |
| 357 | } |
| 358 | |
| 359 | if ($result) { |
| 360 | // CRM-7364, disable relationships |
| 361 | self::enableDisableCaseRelationships($caseId, FALSE); |
| 362 | |
| 363 | CRM_Utils_Hook::post('delete', 'Case', $caseId, $case); |
| 364 | |
| 365 | // remove case from recent items. |
| 366 | $caseRecent = array( |
| 367 | 'id' => $caseId, |
| 368 | 'type' => 'Case', |
| 369 | ); |
| 370 | CRM_Utils_Recent::del($caseRecent); |
| 371 | return TRUE; |
| 372 | } |
| 373 | |
| 374 | return FALSE; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Function to enable disable case related relationships |
| 379 | * |
| 380 | * @param int $caseId case id |
| 381 | * @param boolean $enable action |
| 382 | * |
| 383 | * @return void |
| 384 | * @access public |
| 385 | * @static |
| 386 | */ |
| 387 | static function enableDisableCaseRelationships($caseId, $enable) { |
| 388 | $contactIds = self::retrieveContactIdsByCaseId($caseId); |
| 389 | if (!empty($contactIds)) { |
| 390 | foreach ($contactIds as $cid) { |
| 391 | $roles = self::getCaseRoles($cid, $caseId); |
| 392 | if (!empty($roles)) { |
| 393 | $relationshipIds = implode(',', array_keys($roles)); |
| 394 | $enable = (int) $enable; |
| 395 | $query = "UPDATE civicrm_relationship SET is_active = {$enable} |
| 396 | WHERE id IN ( {$relationshipIds} )"; |
| 397 | CRM_Core_DAO::executeQuery($query); |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * Delete the activities related to case |
| 405 | * |
| 406 | * @param int $activityId id of the activity |
| 407 | * |
| 408 | * @return void |
| 409 | * @access public |
| 410 | * @static |
| 411 | */ |
| 412 | static function deleteCaseActivity($activityId) { |
| 413 | $case = new CRM_Case_DAO_CaseActivity(); |
| 414 | $case->activity_id = $activityId; |
| 415 | $case->delete(); |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * Retrieve contact_id by case_id |
| 420 | * |
| 421 | * @param int $caseId ID of the case |
| 422 | * |
| 423 | * @param null $contactID |
| 424 | * |
| 425 | * @return array |
| 426 | * @access public |
| 427 | */ |
| 428 | static function retrieveContactIdsByCaseId($caseId, $contactID = NULL) { |
| 429 | $caseContact = new CRM_Case_DAO_CaseContact(); |
| 430 | $caseContact->case_id = $caseId; |
| 431 | $caseContact->find(); |
| 432 | $contactArray = array(); |
| 433 | $count = 1; |
| 434 | while ($caseContact->fetch()) { |
| 435 | if ($contactID != $caseContact->contact_id) { |
| 436 | $contactArray[$count] = $caseContact->contact_id; |
| 437 | $count++; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | return $contactArray; |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Look up a case using an activity ID |
| 446 | * |
| 447 | * @param $activityId |
| 448 | * |
| 449 | * @internal param $activity_id |
| 450 | * |
| 451 | * @return int, case ID |
| 452 | */ |
| 453 | static function getCaseIdByActivityId($activityId) { |
| 454 | $originalId = CRM_Core_DAO::singleValueQuery( |
| 455 | 'SELECT original_id FROM civicrm_activity WHERE id = %1', |
| 456 | array('1' => array($activityId, 'Integer')) |
| 457 | ); |
| 458 | $caseId = CRM_Core_DAO::singleValueQuery( |
| 459 | 'SELECT case_id FROM civicrm_case_activity WHERE activity_id in (%1,%2)', |
| 460 | array( |
| 461 | '1' => array($activityId, 'Integer'), |
| 462 | '2' => array($originalId ? $originalId : $activityId, 'Integer'), |
| 463 | ) |
| 464 | ); |
| 465 | return $caseId; |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Retrieve contact names by caseId |
| 470 | * |
| 471 | * @param int $caseId ID of the case |
| 472 | * |
| 473 | * @return array |
| 474 | * |
| 475 | * @access public |
| 476 | * |
| 477 | */ |
| 478 | static function getContactNames($caseId) { |
| 479 | $contactNames = array(); |
| 480 | if (!$caseId) { |
| 481 | return $contactNames; |
| 482 | } |
| 483 | |
| 484 | $query = " |
| 485 | SELECT contact_a.sort_name name, |
| 486 | contact_a.display_name as display_name, |
| 487 | contact_a.id cid, |
| 488 | contact_a.birth_date as birth_date, |
| 489 | ce.email as email, |
| 490 | cp.phone as phone |
| 491 | FROM civicrm_contact contact_a |
| 492 | LEFT JOIN civicrm_case_contact ON civicrm_case_contact.contact_id = contact_a.id |
| 493 | LEFT JOIN civicrm_email ce ON ( ce.contact_id = contact_a.id AND ce.is_primary = 1) |
| 494 | LEFT JOIN civicrm_phone cp ON ( cp.contact_id = contact_a.id AND cp.is_primary = 1) |
| 495 | WHERE civicrm_case_contact.case_id = %1"; |
| 496 | |
| 497 | $dao = CRM_Core_DAO::executeQuery($query, |
| 498 | array(1 => array($caseId, 'Integer')) |
| 499 | ); |
| 500 | while ($dao->fetch()) { |
| 501 | $contactNames[$dao->cid]['contact_id'] = $dao->cid; |
| 502 | $contactNames[$dao->cid]['sort_name'] = $dao->name; |
| 503 | $contactNames[$dao->cid]['display_name'] = $dao->display_name; |
| 504 | $contactNames[$dao->cid]['email'] = $dao->email; |
| 505 | $contactNames[$dao->cid]['phone'] = $dao->phone; |
| 506 | $contactNames[$dao->cid]['birth_date'] = $dao->birth_date; |
| 507 | $contactNames[$dao->cid]['role'] = ts('Client'); |
| 508 | } |
| 509 | |
| 510 | return $contactNames; |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Retrieve case_id by contact_id |
| 515 | * |
| 516 | * @param $contactID |
| 517 | * @param boolean $includeDeleted include the deleted cases in result |
| 518 | * |
| 519 | * @param null $caseType |
| 520 | * |
| 521 | * @internal param int $contactId ID of the contact |
| 522 | * @return array |
| 523 | * |
| 524 | * @access public |
| 525 | */ |
| 526 | static function retrieveCaseIdsByContactId($contactID, $includeDeleted = FALSE, $caseType = NULL) { |
| 527 | $query = " |
| 528 | SELECT ca.id as id |
| 529 | FROM civicrm_case_contact cc |
| 530 | INNER JOIN civicrm_case ca ON cc.case_id = ca.id |
| 531 | "; |
| 532 | if (isset($caseType)) { |
| 533 | $query .= |
| 534 | "INNER JOIN civicrm_case_type ON civicrm_case_type.id = ca.case_type_id |
| 535 | WHERE cc.contact_id = %1 AND civicrm_case_type.name = '{$caseType}'"; |
| 536 | } |
| 537 | if (!isset($caseType)) { |
| 538 | $query .= "WHERE cc.contact_id = %1"; |
| 539 | } |
| 540 | if (!$includeDeleted) { |
| 541 | $query .= " AND ca.is_deleted = 0"; |
| 542 | } |
| 543 | |
| 544 | $params = array(1 => array($contactID, 'Integer')); |
| 545 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 546 | |
| 547 | $caseArray = array(); |
| 548 | while ($dao->fetch()) { |
| 549 | $caseArray[] = $dao->id; |
| 550 | } |
| 551 | |
| 552 | $dao->free(); |
| 553 | return $caseArray; |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * @param string $type |
| 558 | * @param null $userID |
| 559 | * @param null $condition |
| 560 | * @param int $isDeleted |
| 561 | * |
| 562 | * @return string |
| 563 | */ |
| 564 | static function getCaseActivityQuery($type = 'upcoming', $userID = NULL, $condition = NULL, $isDeleted = 0) { |
| 565 | if (!$userID) { |
| 566 | $session = CRM_Core_Session::singleton(); |
| 567 | $userID = $session->get('userID'); |
| 568 | } |
| 569 | |
| 570 | $actStatus = array_flip(CRM_Core_PseudoConstant::activityStatus('name')); |
| 571 | $scheduledStatusId = $actStatus['Scheduled']; |
| 572 | |
| 573 | $query = "SELECT |
| 574 | civicrm_case.id as case_id, |
| 575 | civicrm_case.subject as case_subject, |
| 576 | civicrm_contact.id as contact_id, |
| 577 | civicrm_contact.sort_name as sort_name, |
| 578 | civicrm_phone.phone as phone, |
| 579 | civicrm_contact.contact_type as contact_type, |
| 580 | civicrm_contact.contact_sub_type as contact_sub_type, |
| 581 | t_act.activity_type_id, |
| 582 | c_type.title as case_type, |
| 583 | civicrm_case.case_type_id as case_type_id, |
| 584 | cov_status.label as case_status, |
| 585 | cov_status.label as case_status_name, |
| 586 | t_act.status_id, |
| 587 | civicrm_case.start_date as case_start_date, |
| 588 | case_relation_type.label_b_a as case_role, "; |
| 589 | |
| 590 | if ($type == 'upcoming') { |
| 591 | $query .= " |
| 592 | t_act.desired_date as case_scheduled_activity_date, |
| 593 | t_act.id as case_scheduled_activity_id, |
| 594 | t_act.act_type_name as case_scheduled_activity_type_name, |
| 595 | t_act.act_type AS case_scheduled_activity_type "; |
| 596 | } |
| 597 | elseif ($type == 'recent') { |
| 598 | $query .= " |
| 599 | t_act.desired_date as case_recent_activity_date, |
| 600 | t_act.id as case_recent_activity_id, |
| 601 | t_act.act_type_name as case_recent_activity_type_name, |
| 602 | t_act.act_type AS case_recent_activity_type "; |
| 603 | } |
| 604 | elseif ( $type == 'any' ) { |
| 605 | $query .= " |
| 606 | t_act.desired_date as case_activity_date, |
| 607 | t_act.id as case_activity_id, |
| 608 | t_act.act_type_name as case_activity_type_name, |
| 609 | t_act.act_type AS case_activity_type "; |
| 610 | } |
| 611 | |
| 612 | $query .= " FROM civicrm_case |
| 613 | INNER JOIN civicrm_case_contact ON civicrm_case.id = civicrm_case_contact.case_id |
| 614 | INNER JOIN civicrm_contact ON civicrm_case_contact.contact_id = civicrm_contact.id "; |
| 615 | |
| 616 | if ($type == 'upcoming') { |
| 617 | // This gets the earliest activity per case that's scheduled within 14 days from now. |
| 618 | // Note we have an inner select to get the min activity id in order to remove duplicates in case there are two with the same datetime. |
| 619 | // In this case we don't really care which one, so min(id) works. |
| 620 | // optimized in CRM-11837 |
| 621 | $query .= " INNER JOIN |
| 622 | ( |
| 623 | SELECT case_id, act.id, activity_date_time AS desired_date, activity_type_id, status_id, aov.name AS act_type_name, aov.label AS act_type |
| 624 | FROM ( |
| 625 | SELECT * |
| 626 | FROM ( |
| 627 | SELECT * |
| 628 | FROM civicrm_view_case_activity_upcoming |
| 629 | ORDER BY activity_date_time ASC, id ASC |
| 630 | ) AS upcomingOrdered |
| 631 | GROUP BY case_id |
| 632 | ) AS act |
| 633 | LEFT JOIN civicrm_option_group aog ON aog.name='activity_type' |
| 634 | LEFT JOIN civicrm_option_value aov ON ( aov.option_group_id = aog.id AND aov.value = act.activity_type_id ) |
| 635 | ) AS t_act |
| 636 | "; |
| 637 | } |
| 638 | elseif ($type == 'recent') { |
| 639 | // Similarly, the most recent activity in the past 14 days, and exclude scheduled. |
| 640 | //improve query performance - CRM-10598 |
| 641 | $query .= " INNER JOIN |
| 642 | ( |
| 643 | SELECT case_id, act.id, activity_date_time AS desired_date, activity_type_id, status_id, aov.name AS act_type_name, aov.label AS act_type |
| 644 | FROM ( |
| 645 | SELECT * |
| 646 | FROM ( |
| 647 | SELECT * |
| 648 | FROM civicrm_view_case_activity_recent |
| 649 | ORDER BY activity_date_time DESC, id ASC |
| 650 | ) AS recentOrdered |
| 651 | GROUP BY case_id |
| 652 | ) AS act |
| 653 | LEFT JOIN civicrm_option_group aog ON aog.name='activity_type' |
| 654 | LEFT JOIN civicrm_option_value aov ON ( aov.option_group_id = aog.id AND aov.value = act.activity_type_id ) |
| 655 | ) AS t_act "; |
| 656 | } |
| 657 | elseif ( $type == 'any' ) { |
| 658 | $query .= " LEFT JOIN |
| 659 | ( |
| 660 | SELECT ca4.case_id, act4.id AS id, act4.activity_date_time AS desired_date, act4.activity_type_id, act4.status_id, aov.name AS act_type_name, aov.label AS act_type |
| 661 | FROM civicrm_activity act4 |
| 662 | LEFT JOIN civicrm_case_activity ca4 |
| 663 | ON ca4.activity_id = act4.id |
| 664 | AND act4.is_current_revision = 1 |
| 665 | LEFT JOIN civicrm_option_group aog |
| 666 | ON aog.name='activity_type' |
| 667 | LEFT JOIN civicrm_option_value aov |
| 668 | ON aov.option_group_id = aog.id |
| 669 | AND aov.value = act4.activity_type_id |
| 670 | ) AS t_act"; |
| 671 | } |
| 672 | |
| 673 | $query .= " |
| 674 | ON t_act.case_id = civicrm_case.id |
| 675 | LEFT JOIN civicrm_phone ON (civicrm_phone.contact_id = civicrm_contact.id AND civicrm_phone.is_primary=1) |
| 676 | LEFT JOIN civicrm_relationship case_relationship |
| 677 | ON ( case_relationship.contact_id_a = civicrm_case_contact.contact_id AND case_relationship.contact_id_b = {$userID} |
| 678 | AND case_relationship.case_id = civicrm_case.id ) |
| 679 | |
| 680 | LEFT JOIN civicrm_relationship_type case_relation_type |
| 681 | ON ( case_relation_type.id = case_relationship.relationship_type_id |
| 682 | AND case_relation_type.id = case_relationship.relationship_type_id ) |
| 683 | |
| 684 | LEFT JOIN civicrm_case_type c_type |
| 685 | ON civicrm_case.case_type_id = c_type.id |
| 686 | |
| 687 | LEFT JOIN civicrm_option_group cog_status |
| 688 | ON cog_status.name = 'case_status' |
| 689 | |
| 690 | LEFT JOIN civicrm_option_value cov_status |
| 691 | ON ( civicrm_case.status_id = cov_status.value |
| 692 | AND cog_status.id = cov_status.option_group_id ) |
| 693 | "; |
| 694 | |
| 695 | if ($condition) { |
| 696 | // CRM-8749 backwards compatibility - callers of this function expect to start $condition with "AND" |
| 697 | $query .= " WHERE (1) $condition "; |
| 698 | } |
| 699 | |
| 700 | if ($type == 'upcoming') { |
| 701 | $query .= " ORDER BY case_scheduled_activity_date ASC "; |
| 702 | } |
| 703 | elseif ($type == 'recent') { |
| 704 | $query .= " ORDER BY case_recent_activity_date ASC "; |
| 705 | } |
| 706 | elseif ( $type == 'any' ) { |
| 707 | $query .= " ORDER BY case_activity_date ASC "; |
| 708 | } |
| 709 | |
| 710 | return $query; |
| 711 | } |
| 712 | |
| 713 | /** |
| 714 | * Retrieve cases related to particular contact or whole contact |
| 715 | * used in Dashboad and Tab |
| 716 | * |
| 717 | * @param boolean $allCases |
| 718 | * |
| 719 | * @param int $userID |
| 720 | * |
| 721 | * @param String $type /upcoming,recent,all/ |
| 722 | * |
| 723 | * @param string $context |
| 724 | * |
| 725 | * @return array Array of Cases |
| 726 | * |
| 727 | * @access public |
| 728 | */ |
| 729 | static function getCases($allCases = TRUE, $userID = NULL, $type = 'upcoming', $context = 'dashboard') { |
| 730 | $condition = NULL; |
| 731 | $casesList = array(); |
| 732 | |
| 733 | //validate access for own cases. |
| 734 | if (!self::accessCiviCase()) { |
| 735 | return $casesList; |
| 736 | } |
| 737 | |
| 738 | if (!$userID) { |
| 739 | $session = CRM_Core_Session::singleton(); |
| 740 | $userID = $session->get('userID'); |
| 741 | } |
| 742 | |
| 743 | //validate access for all cases. |
| 744 | if ($allCases && !CRM_Core_Permission::check('access all cases and activities')) { |
| 745 | $allCases = FALSE; |
| 746 | } |
| 747 | |
| 748 | |
| 749 | $condition = " AND civicrm_case.is_deleted = 0 "; |
| 750 | |
| 751 | if (!$allCases) { |
| 752 | $condition .= " AND case_relationship.contact_id_b = {$userID} "; |
| 753 | } |
| 754 | if ( $type == 'upcoming' || $type == 'any' ) { |
| 755 | $closedId = CRM_Core_OptionGroup::getValue('case_status', 'Closed', 'name'); |
| 756 | $condition .= " |
| 757 | AND civicrm_case.status_id != $closedId"; |
| 758 | } |
| 759 | |
| 760 | $query = self::getCaseActivityQuery($type, $userID, $condition); |
| 761 | |
| 762 | $queryParams = array(); |
| 763 | $result = CRM_Core_DAO::executeQuery($query, |
| 764 | $queryParams |
| 765 | ); |
| 766 | |
| 767 | $caseStatus = CRM_Core_OptionGroup::values('case_status', FALSE, FALSE, FALSE, " AND v.name = 'Urgent' "); |
| 768 | |
| 769 | $resultFields = array( |
| 770 | 'contact_id', |
| 771 | 'contact_type', |
| 772 | 'sort_name', |
| 773 | 'phone', |
| 774 | 'case_id', |
| 775 | 'case_subject', |
| 776 | 'case_type', |
| 777 | 'case_type_id', |
| 778 | 'status_id', |
| 779 | 'case_status', |
| 780 | 'case_status_name', |
| 781 | 'activity_type_id', |
| 782 | 'case_start_date', |
| 783 | 'case_role', |
| 784 | ); |
| 785 | |
| 786 | if ($type == 'upcoming') { |
| 787 | $resultFields[] = 'case_scheduled_activity_date'; |
| 788 | $resultFields[] = 'case_scheduled_activity_type_name'; |
| 789 | $resultFields[] = 'case_scheduled_activity_type'; |
| 790 | $resultFields[] = 'case_scheduled_activity_id'; |
| 791 | } |
| 792 | elseif ($type == 'recent') { |
| 793 | $resultFields[] = 'case_recent_activity_date'; |
| 794 | $resultFields[] = 'case_recent_activity_type_name'; |
| 795 | $resultFields[] = 'case_recent_activity_type'; |
| 796 | $resultFields[] = 'case_recent_activity_id'; |
| 797 | } |
| 798 | elseif ( $type == 'any' ) { |
| 799 | $resultFields[] = 'case_activity_date'; |
| 800 | $resultFields[] = 'case_activity_type_name'; |
| 801 | $resultFields[] = 'case_activity_type'; |
| 802 | $resultFields[] = 'case_activity_id'; |
| 803 | } |
| 804 | |
| 805 | // we're going to use the usual actions, so doesn't make sense to duplicate definitions |
| 806 | $actions = CRM_Case_Selector_Search::links(); |
| 807 | |
| 808 | |
| 809 | // check is the user has view/edit signer permission |
| 810 | $permissions = array(CRM_Core_Permission::VIEW); |
| 811 | if (CRM_Core_Permission::check('access all cases and activities') || |
| 812 | (!$allCases && CRM_Core_Permission::check('access my cases and activities')) |
| 813 | ) { |
| 814 | $permissions[] = CRM_Core_Permission::EDIT; |
| 815 | } |
| 816 | if (CRM_Core_Permission::check('delete in CiviCase')) { |
| 817 | $permissions[] = CRM_Core_Permission::DELETE; |
| 818 | } |
| 819 | $mask = CRM_Core_Action::mask($permissions); |
| 820 | |
| 821 | while ($result->fetch()) { |
| 822 | foreach ($resultFields as $donCare => $field) { |
| 823 | $casesList[$result->case_id][$field] = $result->$field; |
| 824 | if ($field == 'contact_type') { |
| 825 | $casesList[$result->case_id]['contact_type_icon'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? |
| 826 | $result->contact_sub_type : $result->contact_type |
| 827 | ); |
| 828 | $casesList[$result->case_id]['action'] = CRM_Core_Action::formLink($actions['primaryActions'], $mask, |
| 829 | array( |
| 830 | 'id' => $result->case_id, |
| 831 | 'cid' => $result->contact_id, |
| 832 | 'cxt' => $context, |
| 833 | ), |
| 834 | ts('more'), |
| 835 | FALSE, |
| 836 | 'case.actions.primary', |
| 837 | 'Case', |
| 838 | $result->case_id |
| 839 | ); |
| 840 | $casesList[$result->case_id]['moreActions'] = CRM_Core_Action::formLink($actions['moreActions'], |
| 841 | $mask, |
| 842 | array( |
| 843 | 'id' => $result->case_id, |
| 844 | 'cid' => $result->contact_id, |
| 845 | 'cxt' => $context, |
| 846 | ), |
| 847 | ts('more'), |
| 848 | TRUE, |
| 849 | 'case.actions.more', |
| 850 | 'Case', |
| 851 | $result->case_id |
| 852 | ); |
| 853 | } |
| 854 | elseif ($field == 'case_status') { |
| 855 | if (in_array($result->$field, $caseStatus)) { |
| 856 | $casesList[$result->case_id]['class'] = "status-urgent"; |
| 857 | } |
| 858 | else { |
| 859 | $casesList[$result->case_id]['class'] = "status-normal"; |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | //CRM-4510. |
| 864 | $caseTypes = CRM_Case_PseudoConstant::caseType('name'); |
| 865 | $caseManagerContact = self::getCaseManagerContact($caseTypes[$result->case_type_id], $result->case_id); |
| 866 | if (!empty($caseManagerContact)) { |
| 867 | $casesList[$result->case_id]['casemanager_id'] = CRM_Utils_Array::value('casemanager_id', $caseManagerContact); |
| 868 | $casesList[$result->case_id]['casemanager'] = CRM_Utils_Array::value('casemanager', $caseManagerContact); |
| 869 | } |
| 870 | |
| 871 | //do check user permissions for edit/view activity. |
| 872 | if (($actId = CRM_Utils_Array::value('case_scheduled_activity_id', $casesList[$result->case_id])) || |
| 873 | ($actId = CRM_Utils_Array::value('case_recent_activity_id', $casesList[$result->case_id])) |
| 874 | ) { |
| 875 | $casesList[$result->case_id]["case_{$type}_activity_editable"] = self::checkPermission($actId, |
| 876 | 'edit', |
| 877 | $casesList[$result->case_id]['activity_type_id'], $userID |
| 878 | ); |
| 879 | $casesList[$result->case_id]["case_{$type}_activity_viewable"] = self::checkPermission($actId, |
| 880 | 'view', |
| 881 | $casesList[$result->case_id]['activity_type_id'], $userID |
| 882 | ); |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | return $casesList; |
| 887 | } |
| 888 | |
| 889 | /** |
| 890 | * Function to get the summary of cases counts by type and status. |
| 891 | */ |
| 892 | static function getCasesSummary($allCases = TRUE, $userID) { |
| 893 | $caseSummary = array(); |
| 894 | |
| 895 | //validate access for civicase. |
| 896 | if (!self::accessCiviCase()) { |
| 897 | return $caseSummary; |
| 898 | } |
| 899 | |
| 900 | //validate access for all cases. |
| 901 | if ($allCases && !CRM_Core_Permission::check('access all cases and activities')) { |
| 902 | $allCases = FALSE; |
| 903 | } |
| 904 | |
| 905 | $caseTypes = CRM_Case_PseudoConstant::caseType(); |
| 906 | $caseStatuses = CRM_Case_PseudoConstant::caseStatus(); |
| 907 | $caseTypes = array_flip($caseTypes); |
| 908 | |
| 909 | // get statuses as headers for the table |
| 910 | $url = CRM_Utils_System::url('civicrm/case/search', "reset=1&force=1&all=1&status="); |
| 911 | foreach ($caseStatuses as $key => $name) { |
| 912 | $caseSummary['headers'][$key]['status'] = $name; |
| 913 | $caseSummary['headers'][$key]['url'] = $url . $key; |
| 914 | } |
| 915 | |
| 916 | // build rows with actual data |
| 917 | $rows = array(); |
| 918 | $myGroupByClause = $mySelectClause = $myCaseFromClause = $myCaseWhereClause = ''; |
| 919 | |
| 920 | if ($allCases) { |
| 921 | $userID = 'null'; |
| 922 | $all = 1; |
| 923 | $case_owner = 1; |
| 924 | } |
| 925 | else { |
| 926 | $all = 0; |
| 927 | $case_owner = 2; |
| 928 | $myCaseWhereClause = " AND case_relationship.contact_id_b = {$userID}"; |
| 929 | $myGroupByClause = " GROUP BY CONCAT(case_relationship.case_id,'-',case_relationship.contact_id_b)"; |
| 930 | } |
| 931 | |
| 932 | $query = " |
| 933 | SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS case_type, |
| 934 | case_type_id, case_relationship.contact_id_b |
| 935 | FROM civicrm_case |
| 936 | LEFT JOIN civicrm_case_type ON civicrm_case.case_type_id = civicrm_case_type.id |
| 937 | LEFT JOIN civicrm_option_group option_group_case_status ON ( option_group_case_status.name = 'case_status' ) |
| 938 | LEFT JOIN civicrm_option_value case_status ON ( civicrm_case.status_id = case_status.value |
| 939 | AND option_group_case_status.id = case_status.option_group_id ) |
| 940 | LEFT JOIN civicrm_relationship case_relationship ON ( case_relationship.case_id = civicrm_case.id |
| 941 | AND case_relationship.contact_id_b = {$userID}) |
| 942 | WHERE is_deleted =0 |
| 943 | {$myCaseWhereClause} {$myGroupByClause}"; |
| 944 | |
| 945 | $res = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); |
| 946 | while ($res->fetch()) { |
| 947 | if (!empty($rows[$res->case_type]) && !empty($rows[$res->case_type][$res->case_status])) { |
| 948 | $rows[$res->case_type][$res->case_status]['count'] = $rows[$res->case_type][$res->case_status]['count'] + 1; |
| 949 | } |
| 950 | else { |
| 951 | $rows[$res->case_type][$res->case_status] = array( |
| 952 | 'count' => 1, |
| 953 | 'url' => CRM_Utils_System::url('civicrm/case/search', |
| 954 | "reset=1&force=1&status={$res->status_id}&type={$res->case_type_id}&case_owner={$case_owner}" |
| 955 | ), |
| 956 | ); |
| 957 | } |
| 958 | } |
| 959 | $caseSummary['rows'] = array_merge($caseTypes, $rows); |
| 960 | |
| 961 | return $caseSummary; |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * Function to get Case roles |
| 966 | * |
| 967 | * @param int $contactID contact id |
| 968 | * @param int $caseID case id |
| 969 | * @param null $relationshipID |
| 970 | * |
| 971 | * @return returns case role / relationships |
| 972 | * |
| 973 | * @static |
| 974 | */ |
| 975 | static function getCaseRoles($contactID, $caseID, $relationshipID = NULL) { |
| 976 | $query = ' |
| 977 | SELECT civicrm_relationship.id as civicrm_relationship_id, |
| 978 | civicrm_contact.sort_name as sort_name, |
| 979 | civicrm_email.email as email, |
| 980 | civicrm_phone.phone as phone, |
| 981 | civicrm_relationship.contact_id_b as civicrm_contact_id, |
| 982 | civicrm_relationship.contact_id_a as client_id, |
| 983 | civicrm_relationship_type.label_a_b as relation, |
| 984 | civicrm_relationship_type.id as relation_type |
| 985 | FROM civicrm_relationship |
| 986 | INNER JOIN civicrm_relationship_type ON civicrm_relationship.relationship_type_id = civicrm_relationship_type.id |
| 987 | INNER JOIN civicrm_contact ON civicrm_relationship.contact_id_b = civicrm_contact.id |
| 988 | LEFT JOIN civicrm_phone ON (civicrm_phone.contact_id = civicrm_contact.id AND civicrm_phone.is_primary = 1) |
| 989 | LEFT JOIN civicrm_email ON (civicrm_email.contact_id = civicrm_contact.id ) |
| 990 | WHERE civicrm_relationship.contact_id_a = %1 AND civicrm_relationship.case_id = %2'; |
| 991 | |
| 992 | |
| 993 | $params = array( |
| 994 | 1 => array($contactID, 'Positive'), |
| 995 | 2 => array($caseID, 'Positive'), |
| 996 | ); |
| 997 | |
| 998 | if ($relationshipID) { |
| 999 | $query .= ' AND civicrm_relationship.id = %3 '; |
| 1000 | $params[3] = array($relationshipID, 'Integer'); |
| 1001 | } |
| 1002 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 1003 | |
| 1004 | $values = array(); |
| 1005 | while ($dao->fetch()) { |
| 1006 | $rid = $dao->civicrm_relationship_id; |
| 1007 | $values[$rid]['cid'] = $dao->civicrm_contact_id; |
| 1008 | $values[$rid]['relation'] = $dao->relation; |
| 1009 | $values[$rid]['name'] = $dao->sort_name; |
| 1010 | $values[$rid]['email'] = $dao->email; |
| 1011 | $values[$rid]['phone'] = $dao->phone; |
| 1012 | $values[$rid]['relation_type'] = $dao->relation_type; |
| 1013 | $values[$rid]['rel_id'] = $dao->civicrm_relationship_id; |
| 1014 | $values[$rid]['client_id'] = $dao->client_id; |
| 1015 | } |
| 1016 | |
| 1017 | $dao->free(); |
| 1018 | return $values; |
| 1019 | } |
| 1020 | |
| 1021 | /** |
| 1022 | * Function to get Case Activities |
| 1023 | * |
| 1024 | * @param int $caseID case id |
| 1025 | * @param array $params posted params |
| 1026 | * @param int $contactID contact id |
| 1027 | * |
| 1028 | * @param null $context |
| 1029 | * @param null $userID |
| 1030 | * @param null $type |
| 1031 | * |
| 1032 | * @return returns case activities |
| 1033 | * |
| 1034 | * @static |
| 1035 | */ |
| 1036 | static function getCaseActivity($caseID, &$params, $contactID, $context = NULL, $userID = NULL, $type = NULL) { |
| 1037 | $values = array(); |
| 1038 | |
| 1039 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
| 1040 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); |
| 1041 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
| 1042 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
| 1043 | |
| 1044 | // CRM-5081 - formatting the dates to omit seconds. |
| 1045 | // Note the 00 in the date format string is needed otherwise later on it thinks scheduled ones are overdue. |
| 1046 | $select = "SELECT count(ca.id) as ismultiple, ca.id as id, |
| 1047 | ca.activity_type_id as type, |
| 1048 | ca.activity_type_id as activity_type_id, |
| 1049 | cc.sort_name as reporter, |
| 1050 | cc.id as reporter_id, |
| 1051 | acc.sort_name AS assignee, |
| 1052 | acc.id AS assignee_id, |
| 1053 | DATE_FORMAT(IF(ca.activity_date_time < NOW() AND ca.status_id=ov.value, |
| 1054 | ca.activity_date_time, |
| 1055 | DATE_ADD(NOW(), INTERVAL 1 YEAR) |
| 1056 | ), '%Y%m%d%H%i00') as overdue_date, |
| 1057 | DATE_FORMAT(ca.activity_date_time, '%Y%m%d%H%i00') as display_date, |
| 1058 | ca.status_id as status, |
| 1059 | ca.subject as subject, |
| 1060 | ca.is_deleted as deleted, |
| 1061 | ca.priority_id as priority, |
| 1062 | ca.weight as weight, |
| 1063 | GROUP_CONCAT(ef.file_id) as attachment_ids "; |
| 1064 | |
| 1065 | $from = " |
| 1066 | FROM civicrm_case_activity cca |
| 1067 | INNER JOIN civicrm_activity ca ON ca.id = cca.activity_id |
| 1068 | INNER JOIN civicrm_activity_contact cac ON cac.activity_id = ca.id AND cac.record_type_id = {$sourceID} |
| 1069 | INNER JOIN civicrm_contact cc ON cc.id = cac.contact_id |
| 1070 | INNER JOIN civicrm_option_group cog ON cog.name = 'activity_type' |
| 1071 | INNER JOIN civicrm_option_value cov ON cov.option_group_id = cog.id |
| 1072 | AND cov.value = ca.activity_type_id AND cov.is_active = 1 |
| 1073 | LEFT JOIN civicrm_entity_file ef on ef.entity_table = 'civicrm_activity' AND ef.entity_id = ca.id |
| 1074 | LEFT OUTER JOIN civicrm_option_group og ON og.name = 'activity_status' |
| 1075 | LEFT OUTER JOIN civicrm_option_value ov ON ov.option_group_id=og.id AND ov.name = 'Scheduled' |
| 1076 | LEFT JOIN civicrm_activity_contact caa |
| 1077 | ON caa.activity_id = ca.id AND caa.record_type_id = {$assigneeID} |
| 1078 | LEFT JOIN civicrm_contact acc ON acc.id = caa.contact_id "; |
| 1079 | |
| 1080 | $where = 'WHERE cca.case_id= %1 |
| 1081 | AND ca.is_current_revision = 1'; |
| 1082 | |
| 1083 | if (!empty($params['reporter_id'])) { |
| 1084 | $where .= " AND cac.contact_id = " . CRM_Utils_Type::escape($params['reporter_id'], 'Integer'); |
| 1085 | } |
| 1086 | |
| 1087 | if (!empty($params['status_id'])) { |
| 1088 | $where .= " AND ca.status_id = " . CRM_Utils_Type::escape($params['status_id'], 'Integer'); |
| 1089 | } |
| 1090 | |
| 1091 | if (!empty($params['activity_deleted'])) { |
| 1092 | $where .= " AND ca.is_deleted = 1"; |
| 1093 | } |
| 1094 | else { |
| 1095 | $where .= " AND ca.is_deleted = 0"; |
| 1096 | } |
| 1097 | |
| 1098 | if (!empty($params['activity_type_id'])) { |
| 1099 | $where .= " AND ca.activity_type_id = " . CRM_Utils_Type::escape($params['activity_type_id'], 'Integer'); |
| 1100 | } |
| 1101 | |
| 1102 | if (!empty($params['activity_date_low'])) { |
| 1103 | $fromActivityDate = CRM_Utils_Type::escape(CRM_Utils_Date::processDate($params['activity_date_low']), 'Date'); |
| 1104 | } |
| 1105 | if (!empty($params['activity_date_high'])) { |
| 1106 | $toActivityDate = CRM_Utils_Type::escape(CRM_Utils_Date::processDate($params['activity_date_high']), 'Date'); |
| 1107 | $toActivityDate = $toActivityDate ? $toActivityDate + 235959 : NULL; |
| 1108 | } |
| 1109 | |
| 1110 | if (!empty($fromActivityDate)) { |
| 1111 | $where .= " AND ca.activity_date_time >= '{$fromActivityDate}'"; |
| 1112 | } |
| 1113 | |
| 1114 | if (!empty($toActivityDate)) { |
| 1115 | $where .= " AND ca.activity_date_time <= '{$toActivityDate}'"; |
| 1116 | } |
| 1117 | |
| 1118 | // hack to handle to allow initial sorting to be done by query |
| 1119 | if (CRM_Utils_Array::value('sortname', $params) == 'undefined') { |
| 1120 | $params['sortname'] = NULL; |
| 1121 | } |
| 1122 | |
| 1123 | if (CRM_Utils_Array::value('sortorder', $params) == 'undefined') { |
| 1124 | $params['sortorder'] = NULL; |
| 1125 | } |
| 1126 | |
| 1127 | $sortname = CRM_Utils_Array::value('sortname', $params); |
| 1128 | $sortorder = CRM_Utils_Array::value('sortorder', $params); |
| 1129 | |
| 1130 | $groupBy = " GROUP BY ca.id "; |
| 1131 | |
| 1132 | if (!$sortname AND !$sortorder) { |
| 1133 | // CRM-5081 - added id to act like creation date |
| 1134 | $orderBy = " ORDER BY overdue_date ASC, display_date DESC, weight DESC"; |
| 1135 | } |
| 1136 | else { |
| 1137 | $sort = "{$sortname} {$sortorder}"; |
| 1138 | $sort = CRM_Utils_Type::escape($sort, 'String'); |
| 1139 | $orderBy = " ORDER BY $sort "; |
| 1140 | if ($sortname != 'display_date') { |
| 1141 | $orderBy .= ', display_date DESC'; |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | $page = CRM_Utils_Array::value('page', $params); |
| 1146 | $rp = CRM_Utils_Array::value('rp', $params); |
| 1147 | |
| 1148 | if (!$page) { |
| 1149 | $page = 1; |
| 1150 | } |
| 1151 | if (!$rp) { |
| 1152 | $rp = 10; |
| 1153 | } |
| 1154 | |
| 1155 | $start = (($page - 1) * $rp); |
| 1156 | $query = $select . $from . $where . $groupBy . $orderBy; |
| 1157 | |
| 1158 | $params = array(1 => array($caseID, 'Integer')); |
| 1159 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 1160 | $params['total'] = $dao->N; |
| 1161 | |
| 1162 | //FIXME: need to optimize/cache these queries |
| 1163 | $limit = " LIMIT $start, $rp"; |
| 1164 | $query .= $limit; |
| 1165 | |
| 1166 | //EXIT; |
| 1167 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 1168 | |
| 1169 | |
| 1170 | $activityTypes = CRM_Case_PseudoConstant::caseActivityType(FALSE, TRUE); |
| 1171 | $activityStatus = CRM_Core_PseudoConstant::activityStatus(); |
| 1172 | $activityPriority = CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'priority_id'); |
| 1173 | |
| 1174 | $url = CRM_Utils_System::url("civicrm/case/activity", |
| 1175 | "reset=1&cid={$contactID}&caseid={$caseID}", FALSE, NULL, FALSE |
| 1176 | ); |
| 1177 | |
| 1178 | $contextUrl = ''; |
| 1179 | if ($context == 'fulltext') { |
| 1180 | $contextUrl = "&context={$context}"; |
| 1181 | } |
| 1182 | $editUrl = "{$url}&action=update{$contextUrl}"; |
| 1183 | $deleteUrl = "{$url}&action=delete{$contextUrl}"; |
| 1184 | $restoreUrl = "{$url}&action=renew{$contextUrl}"; |
| 1185 | $viewTitle = ts('View this activity.'); |
| 1186 | $statusTitle = ts('Edit Status'); |
| 1187 | |
| 1188 | $emailActivityTypeIDs = array( |
| 1189 | 'Email' => CRM_Core_OptionGroup::getValue('activity_type', |
| 1190 | 'Email', |
| 1191 | 'name' |
| 1192 | ), |
| 1193 | 'Inbound Email' => CRM_Core_OptionGroup::getValue('activity_type', |
| 1194 | 'Inbound Email', |
| 1195 | 'name' |
| 1196 | ), |
| 1197 | ); |
| 1198 | |
| 1199 | $emailActivityTypeIDs = array( |
| 1200 | 'Email' => CRM_Core_OptionGroup::getValue('activity_type', |
| 1201 | 'Email', |
| 1202 | 'name' |
| 1203 | ), |
| 1204 | 'Inbound Email' => CRM_Core_OptionGroup::getValue('activity_type', |
| 1205 | 'Inbound Email', |
| 1206 | 'name' |
| 1207 | ), |
| 1208 | ); |
| 1209 | |
| 1210 | $caseDeleted = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $caseID, 'is_deleted'); |
| 1211 | |
| 1212 | // define statuses which are handled like Completed status (others are assumed to be handled like Scheduled status) |
| 1213 | $compStatusValues = array(); |
| 1214 | $compStatusNames = array('Completed', 'Left Message', 'Cancelled', 'Unreachable', 'Not Required'); |
| 1215 | foreach ($compStatusNames as $name) { |
| 1216 | $compStatusValues[] = CRM_Core_OptionGroup::getValue('activity_status', $name, 'name'); |
| 1217 | } |
| 1218 | $contactViewUrl = CRM_Utils_System::url("civicrm/contact/view", |
| 1219 | "reset=1&cid=", FALSE, NULL, FALSE |
| 1220 | ); |
| 1221 | $hasViewContact = CRM_Core_Permission::giveMeAllACLs(); |
| 1222 | $clientIds = self::retrieveContactIdsByCaseId($caseID); |
| 1223 | |
| 1224 | if (!$userID) { |
| 1225 | $session = CRM_Core_Session::singleton(); |
| 1226 | $userID = $session->get('userID'); |
| 1227 | } |
| 1228 | |
| 1229 | while ($dao->fetch()) { |
| 1230 | |
| 1231 | $allowView = self::checkPermission($dao->id, 'view', $dao->activity_type_id, $userID); |
| 1232 | $allowEdit = self::checkPermission($dao->id, 'edit', $dao->activity_type_id, $userID); |
| 1233 | $allowDelete = self::checkPermission($dao->id, 'delete', $dao->activity_type_id, $userID); |
| 1234 | |
| 1235 | //do not have sufficient permission |
| 1236 | //to access given case activity record. |
| 1237 | if (!$allowView && !$allowEdit && !$allowDelete) { |
| 1238 | continue; |
| 1239 | } |
| 1240 | |
| 1241 | $values[$dao->id]['id'] = $dao->id; |
| 1242 | $values[$dao->id]['type'] = $activityTypes[$dao->type]['label']; |
| 1243 | |
| 1244 | $reporterName = $dao->reporter; |
| 1245 | if ($hasViewContact) { |
| 1246 | $reporterName = '<a href="' . $contactViewUrl . $dao->reporter_id . '">' . $dao->reporter . '</a>'; |
| 1247 | } |
| 1248 | $values[$dao->id]['reporter'] = $reporterName; |
| 1249 | $targetNames = CRM_Activity_BAO_ActivityContact::getNames($dao->id, $targetID); |
| 1250 | $targetContactUrls = $withContacts = array(); |
| 1251 | foreach ($targetNames as $targetId => $targetName) { |
| 1252 | if (!in_array($targetId, $clientIds)) { |
| 1253 | $withContacts[$targetId] = $targetName; |
| 1254 | } |
| 1255 | } |
| 1256 | foreach ($withContacts as $cid => $name) { |
| 1257 | if ($hasViewContact) { |
| 1258 | $name = '<a href="' . $contactViewUrl . $cid . '">' . $name . '</a>'; |
| 1259 | } |
| 1260 | $targetContactUrls[] = $name; |
| 1261 | } |
| 1262 | $values[$dao->id]['with_contacts'] = implode('; ', $targetContactUrls); |
| 1263 | |
| 1264 | $values[$dao->id]['display_date'] = CRM_Utils_Date::customFormat($dao->display_date); |
| 1265 | $values[$dao->id]['status'] = $activityStatus[$dao->status]; |
| 1266 | |
| 1267 | //check for view activity. |
| 1268 | $subject = (empty($dao->subject)) ? '(' . ts('no subject') . ')' : $dao->subject; |
| 1269 | if ($allowView) { |
| 1270 | $url = CRM_Utils_System::url('civicrm/case/activity/view', array('cid' => $contactID, 'aid' => $dao->id)); |
| 1271 | $subject = '<a class="crm-popup medium-popup" href="' . $url . '" title="' . $viewTitle . '">' . $subject . '</a>'; |
| 1272 | } |
| 1273 | $values[$dao->id]['subject'] = $subject; |
| 1274 | |
| 1275 | // add activity assignee to activity selector. CRM-4485. |
| 1276 | if (isset($dao->assignee)) { |
| 1277 | if ($dao->ismultiple == 1) { |
| 1278 | if ($dao->reporter_id != $dao->assignee_id) { |
| 1279 | $values[$dao->id]['reporter'] .= ($hasViewContact) ? ' / ' . "<a href='{$contactViewUrl}{$dao->assignee_id}'>$dao->assignee</a>" : ' / ' . $dao->assignee; |
| 1280 | } |
| 1281 | $values[$dao->id]['assignee'] = $dao->assignee; |
| 1282 | } |
| 1283 | else { |
| 1284 | $values[$dao->id]['reporter'] .= ' / ' . ts('(multiple)'); |
| 1285 | } |
| 1286 | } |
| 1287 | // FIXME: Why are we not using CRM_Core_Action for these links? This is too much manual work and likely to get out-of-sync with core markup. |
| 1288 | $url = ""; |
| 1289 | $css = 'class="action-item crm-hover-button"'; |
| 1290 | $additionalUrl = "&id={$dao->id}"; |
| 1291 | if (!$dao->deleted) { |
| 1292 | //hide edit link of activity type email.CRM-4530. |
| 1293 | if (!in_array($dao->type, $emailActivityTypeIDs)) { |
| 1294 | //hide Edit link if activity type is NOT editable (special case activities).CRM-5871 |
| 1295 | if ($allowEdit) { |
| 1296 | $url = '<a ' . $css . ' href="' . $editUrl . $additionalUrl . '">' . ts('Edit') . '</a> '; |
| 1297 | } |
| 1298 | } |
| 1299 | if ($allowDelete) { |
| 1300 | $url .= ' <a ' . str_replace('action-item', 'action-item small-popup', $css) . ' href="' . $deleteUrl . $additionalUrl . '">' . ts('Delete') . '</a>'; |
| 1301 | } |
| 1302 | } |
| 1303 | elseif (!$caseDeleted) { |
| 1304 | $url = ' <a ' . $css . ' href="' . $restoreUrl . $additionalUrl . '">' . ts('Restore') . '</a>'; |
| 1305 | $values[$dao->id]['status'] = $values[$dao->id]['status'] . '<br /> (deleted)'; |
| 1306 | } |
| 1307 | |
| 1308 | //check for operations. |
| 1309 | if (self::checkPermission($dao->id, 'Move To Case', $dao->activity_type_id)) { |
| 1310 | $url .= ' <a ' . $css . ' href="#" onClick="Javascript:fileOnCase( \'move\',' . $dao->id . ', ' . $caseID . ', this ); return false;">' . ts('Move To Case') . '</a> '; |
| 1311 | } |
| 1312 | if (self::checkPermission($dao->id, 'Copy To Case', $dao->activity_type_id)) { |
| 1313 | $url .= ' <a ' . $css . ' href="#" onClick="Javascript:fileOnCase( \'copy\',' . $dao->id . ',' . $caseID . ', this ); return false;">' . ts('Copy To Case') . '</a> '; |
| 1314 | } |
| 1315 | // if there are file attachments we will return how many and, if only one, add a link to it |
| 1316 | if (!empty($dao->attachment_ids)) { |
| 1317 | $attachmentIDs = explode(',', $dao->attachment_ids); |
| 1318 | $values[$dao->id]['no_attachments'] = count($attachmentIDs); |
| 1319 | if ($values[$dao->id]['no_attachments'] == 1) { |
| 1320 | // if there is only one it's easy to do a link - otherwise just flag it |
| 1321 | $attachmentViewUrl = CRM_Utils_System::url( |
| 1322 | "civicrm/file", |
| 1323 | "reset=1&eid=" . $dao->id . "&id=" . $dao->attachment_ids, |
| 1324 | FALSE, |
| 1325 | NULL, |
| 1326 | FALSE |
| 1327 | ); |
| 1328 | $url .= " <a href='$attachmentViewUrl' ><span class='icon paper-icon'></span></a>"; |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | |
| 1333 | $values[$dao->id]['links'] = $url; |
| 1334 | $values[$dao->id]['class'] = ""; |
| 1335 | |
| 1336 | if (!empty($dao->priority)) { |
| 1337 | if ($dao->priority == CRM_Core_OptionGroup::getValue('priority', 'Urgent', 'name')) { |
| 1338 | $values[$dao->id]['class'] = $values[$dao->id]['class'] . "priority-urgent "; |
| 1339 | } |
| 1340 | elseif ($dao->priority == CRM_Core_OptionGroup::getValue('priority', 'Low', 'name')) { |
| 1341 | $values[$dao->id]['class'] = $values[$dao->id]['class'] . "priority-low "; |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | if (CRM_Utils_Array::crmInArray($dao->status, $compStatusValues)) { |
| 1346 | $values[$dao->id]['class'] = $values[$dao->id]['class'] . " status-completed"; |
| 1347 | } |
| 1348 | else { |
| 1349 | if (CRM_Utils_Date::overdue($dao->display_date)) { |
| 1350 | $values[$dao->id]['class'] = $values[$dao->id]['class'] . " status-overdue"; |
| 1351 | } |
| 1352 | else { |
| 1353 | $values[$dao->id]['class'] = $values[$dao->id]['class'] . " status-scheduled"; |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | if ($allowEdit) { |
| 1358 | $values[$dao->id]['status'] = '<a class="crm-activity-status crm-activity-status-' . $dao->id . ' ' . $values[$dao->id]['class'] . ' crm-activity-change-status crm-editable-enabled" activity_id=' . $dao->id . ' current_status=' . $dao->status . ' case_id=' . $caseID . ' href="#" title=\'' . $statusTitle . '\'>' . $values[$dao->id]['status'] . '</a>'; |
| 1359 | } |
| 1360 | } |
| 1361 | $dao->free(); |
| 1362 | |
| 1363 | return $values; |
| 1364 | } |
| 1365 | |
| 1366 | /** |
| 1367 | * Function to get Case Related Contacts |
| 1368 | * |
| 1369 | * @param int $caseID case id |
| 1370 | * @param boolean $skipDetails if true include details of contacts |
| 1371 | * |
| 1372 | * @return array $searchRows array of return properties |
| 1373 | * |
| 1374 | * @static |
| 1375 | */ |
| 1376 | static function getRelatedContacts($caseID, $skipDetails = FALSE) { |
| 1377 | $values = array(); |
| 1378 | $query = ' |
| 1379 | SELECT cc.display_name as name, cc.sort_name as sort_name, cc.id, crt.label_b_a as role, ce.email |
| 1380 | FROM civicrm_relationship cr |
| 1381 | LEFT JOIN civicrm_relationship_type crt |
| 1382 | ON crt.id = cr.relationship_type_id |
| 1383 | LEFT JOIN civicrm_contact cc |
| 1384 | ON cc.id = cr.contact_id_b |
| 1385 | LEFT JOIN civicrm_email ce |
| 1386 | ON ce.contact_id = cc.id |
| 1387 | AND ce.is_primary= 1 |
| 1388 | WHERE cr.case_id = %1 |
| 1389 | GROUP BY cc.id'; |
| 1390 | |
| 1391 | $params = array(1 => array($caseID, 'Integer')); |
| 1392 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 1393 | |
| 1394 | while ($dao->fetch()) { |
| 1395 | if ($skipDetails) { |
| 1396 | $values[$dao->id] = 1; |
| 1397 | } |
| 1398 | else { |
| 1399 | $values[] = array( |
| 1400 | 'contact_id' => $dao->id, |
| 1401 | 'display_name' => $dao->name, |
| 1402 | 'sort_name' => $dao->sort_name, |
| 1403 | 'role' => $dao->role, |
| 1404 | 'email' => $dao->email, |
| 1405 | ); |
| 1406 | } |
| 1407 | } |
| 1408 | $dao->free(); |
| 1409 | |
| 1410 | return $values; |
| 1411 | } |
| 1412 | |
| 1413 | /** |
| 1414 | * Function that sends e-mail copy of activity |
| 1415 | * |
| 1416 | * @param $clientId |
| 1417 | * @param int $activityId activity Id |
| 1418 | * @param array $contacts array of related contact |
| 1419 | * |
| 1420 | * @param null $attachments |
| 1421 | * @param $caseId |
| 1422 | * |
| 1423 | * @return void |
| 1424 | * @access public |
| 1425 | */ |
| 1426 | static function sendActivityCopy($clientId, $activityId, $contacts, $attachments = NULL, $caseId) { |
| 1427 | if (!$activityId) { |
| 1428 | return; |
| 1429 | } |
| 1430 | |
| 1431 | $tplParams = $activityInfo = array(); |
| 1432 | //if its a case activity |
| 1433 | if ($caseId) { |
| 1434 | $activityTypeId = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $activityId, 'activity_type_id'); |
| 1435 | $nonCaseActivityTypes = CRM_Core_PseudoConstant::activityType(); |
| 1436 | if (!empty($nonCaseActivityTypes[$activityTypeId])) { |
| 1437 | $anyActivity = TRUE; |
| 1438 | } |
| 1439 | else { |
| 1440 | $anyActivity = FALSE; |
| 1441 | } |
| 1442 | $tplParams['isCaseActivity'] = 1; |
| 1443 | $tplParams['client_id'] = $clientId; |
| 1444 | } |
| 1445 | else { |
| 1446 | $anyActivity = TRUE; |
| 1447 | } |
| 1448 | |
| 1449 | $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process(); |
| 1450 | $isRedact = $xmlProcessorProcess->getRedactActivityEmail(); |
| 1451 | |
| 1452 | $xmlProcessorReport = new CRM_Case_XMLProcessor_Report(); |
| 1453 | |
| 1454 | $activityInfo = $xmlProcessorReport->getActivityInfo($clientId, $activityId, $anyActivity, $isRedact); |
| 1455 | if ($caseId) { |
| 1456 | $activityInfo['fields'][] = array('label' => 'Case ID', 'type' => 'String', 'value' => $caseId); |
| 1457 | } |
| 1458 | $tplParams['activity'] = $activityInfo; |
| 1459 | foreach ($tplParams['activity']['fields'] as $k => $val) { |
| 1460 | if (CRM_Utils_Array::value('label', $val) == ts('Subject')) { |
| 1461 | $activitySubject = $val['value']; |
| 1462 | break; |
| 1463 | } |
| 1464 | } |
| 1465 | $session = CRM_Core_Session::singleton(); |
| 1466 | // CRM-8926 If user is not logged in, use the activity creator as userID |
| 1467 | if (!($userID = $session->get('userID'))) { |
| 1468 | $userID = CRM_Activity_BAO_Activity::getSourceContactID($activityId); |
| 1469 | } |
| 1470 | |
| 1471 | //also create activities simultaneously of this copy. |
| 1472 | $activityParams = array(); |
| 1473 | |
| 1474 | $activityParams['source_record_id'] = $activityId; |
| 1475 | $activityParams['source_contact_id'] = $userID; |
| 1476 | $activityParams['activity_type_id'] = CRM_Core_OptionGroup::getValue('activity_type', 'Email', 'name'); |
| 1477 | $activityParams['activity_date_time'] = date('YmdHis'); |
| 1478 | $activityParams['status_id'] = CRM_Core_OptionGroup::getValue('activity_status', 'Completed', 'name'); |
| 1479 | $activityParams['medium_id'] = CRM_Core_OptionGroup::getValue('encounter_medium', 'email', 'name'); |
| 1480 | $activityParams['case_id'] = $caseId; |
| 1481 | $activityParams['is_auto'] = 0; |
| 1482 | $activityParams['target_id'] = $clientId; |
| 1483 | |
| 1484 | $tplParams['activitySubject'] = $activitySubject; |
| 1485 | |
| 1486 | // if it’s a case activity, add hashed id to the template (CRM-5916) |
| 1487 | if ($caseId) { |
| 1488 | $tplParams['idHash'] = substr(sha1(CIVICRM_SITE_KEY . $caseId), 0, 7); |
| 1489 | } |
| 1490 | |
| 1491 | $result = array(); |
| 1492 | list($name, $address) = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID); |
| 1493 | |
| 1494 | $receiptFrom = "$name <$address>"; |
| 1495 | |
| 1496 | $recordedActivityParams = array(); |
| 1497 | |
| 1498 | foreach ($contacts as $mail => $info) { |
| 1499 | $tplParams['contact'] = $info; |
| 1500 | self::buildPermissionLinks($tplParams, $activityParams); |
| 1501 | |
| 1502 | $displayName = CRM_Utils_Array::value('display_name', $info); |
| 1503 | |
| 1504 | list($result[CRM_Utils_Array::value('contact_id', $info)], $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate( |
| 1505 | array( |
| 1506 | 'groupName' => 'msg_tpl_workflow_case', |
| 1507 | 'valueName' => 'case_activity', |
| 1508 | 'contactId' => CRM_Utils_Array::value('contact_id', $info), |
| 1509 | 'tplParams' => $tplParams, |
| 1510 | 'from' => $receiptFrom, |
| 1511 | 'toName' => $displayName, |
| 1512 | 'toEmail' => $mail, |
| 1513 | 'attachments' => $attachments, |
| 1514 | ) |
| 1515 | ); |
| 1516 | |
| 1517 | $activityParams['subject'] = $activitySubject . ' - copy sent to ' . $displayName; |
| 1518 | $activityParams['details'] = $message; |
| 1519 | |
| 1520 | if (!empty($result[$info['contact_id']])) { |
| 1521 | /* |
| 1522 | * Really only need to record one activity with all the targets combined. |
| 1523 | * Originally the template was going to possibly have different content, e.g. depending on permissions, |
| 1524 | * but it's always the same content at the moment. |
| 1525 | */ |
| 1526 | if (empty($recordedActivityParams)) { |
| 1527 | $recordedActivityParams = $activityParams; |
| 1528 | } |
| 1529 | else { |
| 1530 | $recordedActivityParams['subject'] .= "; $displayName"; |
| 1531 | } |
| 1532 | $recordedActivityParams['target_contact_id'][] = $info['contact_id']; |
| 1533 | } |
| 1534 | else { |
| 1535 | unset($result[CRM_Utils_Array::value('contact_id', $info)]); |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | if (!empty($recordedActivityParams)) { |
| 1540 | $activity = CRM_Activity_BAO_Activity::create($recordedActivityParams); |
| 1541 | |
| 1542 | //create case_activity record if its case activity. |
| 1543 | if ($caseId) { |
| 1544 | $caseParams = array( |
| 1545 | 'activity_id' => $activity->id, |
| 1546 | 'case_id' => $caseId, |
| 1547 | ); |
| 1548 | self::processCaseActivity($caseParams); |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | return $result; |
| 1553 | } |
| 1554 | |
| 1555 | /** |
| 1556 | * Retrieve count of activities having a particular type, and |
| 1557 | * associated with a particular case. |
| 1558 | * |
| 1559 | * @param int $caseId ID of the case |
| 1560 | * @param int $activityTypeId ID of the activity type |
| 1561 | * |
| 1562 | * @return array |
| 1563 | * |
| 1564 | * @access public |
| 1565 | * |
| 1566 | */ |
| 1567 | static function getCaseActivityCount($caseId, $activityTypeId) { |
| 1568 | $queryParam = array( |
| 1569 | 1 => array($caseId, 'Integer'), |
| 1570 | 2 => array($activityTypeId, 'Integer'), |
| 1571 | ); |
| 1572 | $query = "SELECT count(ca.id) as countact |
| 1573 | FROM civicrm_activity ca |
| 1574 | INNER JOIN civicrm_case_activity cca ON ca.id = cca.activity_id |
| 1575 | WHERE ca.activity_type_id = %2 |
| 1576 | AND cca.case_id = %1 |
| 1577 | AND ca.is_deleted = 0"; |
| 1578 | |
| 1579 | $dao = CRM_Core_DAO::executeQuery($query, $queryParam); |
| 1580 | if ($dao->fetch()) { |
| 1581 | return $dao->countact; |
| 1582 | } |
| 1583 | |
| 1584 | return FALSE; |
| 1585 | } |
| 1586 | |
| 1587 | /** |
| 1588 | * Create an activity for a case via email |
| 1589 | * |
| 1590 | * @param int $file email sent |
| 1591 | * |
| 1592 | * @return array|void $activity object of newly creted activity via email@access public |
| 1593 | */ |
| 1594 | static function recordActivityViaEmail($file) { |
| 1595 | if (!file_exists($file) || |
| 1596 | !is_readable($file) |
| 1597 | ) { |
| 1598 | return CRM_Core_Error::fatal(ts('File %1 does not exist or is not readable', |
| 1599 | array(1 => $file) |
| 1600 | )); |
| 1601 | } |
| 1602 | |
| 1603 | $result = CRM_Utils_Mail_Incoming::parse($file); |
| 1604 | if ($result['is_error']) { |
| 1605 | return $result; |
| 1606 | } |
| 1607 | |
| 1608 | foreach ($result['to'] as $to) { |
| 1609 | $caseId = NULL; |
| 1610 | |
| 1611 | $emailPattern = '/^([A-Z0-9._%+-]+)\+([\d]+)@[A-Z0-9.-]+\.[A-Z]{2,4}$/i'; |
| 1612 | $replacement = preg_replace($emailPattern, '$2', $to['email']); |
| 1613 | |
| 1614 | if ($replacement !== $to['email']) { |
| 1615 | $caseId = $replacement; |
| 1616 | //if caseId is invalid, return as error file |
| 1617 | if (!CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $caseId, 'id')) { |
| 1618 | return CRM_Core_Error::createAPIError(ts('Invalid case ID ( %1 ) in TO: field.', |
| 1619 | array(1 => $caseId) |
| 1620 | )); |
| 1621 | } |
| 1622 | } |
| 1623 | else { |
| 1624 | continue; |
| 1625 | } |
| 1626 | |
| 1627 | // TODO: May want to replace this with a call to getRelatedAndGlobalContacts() when this feature is revisited. |
| 1628 | // (Or for efficiency call the global one outside the loop and then union with this each time.) |
| 1629 | $contactDetails = self::getRelatedContacts($caseId, TRUE); |
| 1630 | |
| 1631 | if (!empty($contactDetails[$result['from']['id']])) { |
| 1632 | $params = array(); |
| 1633 | $params['subject'] = $result['subject']; |
| 1634 | $params['activity_date_time'] = $result['date']; |
| 1635 | $params['details'] = $result['body']; |
| 1636 | $params['source_contact_id'] = $result['from']['id']; |
| 1637 | $params['status_id'] = CRM_Core_OptionGroup::getValue('activity_status', |
| 1638 | 'Completed', |
| 1639 | 'name' |
| 1640 | ); |
| 1641 | |
| 1642 | $details = CRM_Case_PseudoConstant::caseActivityType(); |
| 1643 | $matches = array(); |
| 1644 | preg_match('/^\W+([a-zA-Z0-9_ ]+)(\W+)?\n/i', |
| 1645 | $result['body'], $matches |
| 1646 | ); |
| 1647 | |
| 1648 | if (!empty($matches) && isset($matches[1])) { |
| 1649 | $activityType = trim($matches[1]); |
| 1650 | if (isset($details[$activityType])) { |
| 1651 | $params['activity_type_id'] = $details[$activityType]['id']; |
| 1652 | } |
| 1653 | } |
| 1654 | if (!isset($params['activity_type_id'])) { |
| 1655 | $params['activity_type_id'] = CRM_Core_OptionGroup::getValue('activity_type', 'Inbound Email', 'name'); |
| 1656 | } |
| 1657 | |
| 1658 | // create activity |
| 1659 | $activity = CRM_Activity_BAO_Activity::create($params); |
| 1660 | |
| 1661 | $caseParams = array( |
| 1662 | 'activity_id' => $activity->id, |
| 1663 | 'case_id' => $caseId, |
| 1664 | ); |
| 1665 | self::processCaseActivity($caseParams); |
| 1666 | } |
| 1667 | else { |
| 1668 | return CRM_Core_Error::createAPIError(ts('FROM email contact %1 doesn\'t have a relationship to the referenced case.', |
| 1669 | array(1 => $result['from']['email']) |
| 1670 | )); |
| 1671 | } |
| 1672 | } |
| 1673 | } |
| 1674 | |
| 1675 | /** |
| 1676 | * Function to retrieve the scheduled activity type and date |
| 1677 | * |
| 1678 | * @param array $cases Array of contact and case id |
| 1679 | * |
| 1680 | * @param string $type |
| 1681 | * |
| 1682 | * @return array $activityInfo Array of scheduled activity type and date |
| 1683 | * |
| 1684 | * @access public |
| 1685 | * |
| 1686 | * @static |
| 1687 | */ |
| 1688 | static function getNextScheduledActivity($cases, $type = 'upcoming') { |
| 1689 | $session = CRM_Core_Session::singleton(); |
| 1690 | $userID = $session->get('userID'); |
| 1691 | |
| 1692 | $caseID = implode(',', $cases['case_id']); |
| 1693 | $contactID = implode(',', $cases['contact_id']); |
| 1694 | |
| 1695 | $condition = " |
| 1696 | AND civicrm_case_contact.contact_id IN( {$contactID} ) |
| 1697 | AND civicrm_case.id IN( {$caseID}) |
| 1698 | AND civicrm_case.is_deleted = {$cases['case_deleted']}"; |
| 1699 | |
| 1700 | $query = self::getCaseActivityQuery($type, $userID, $condition, $cases['case_deleted']); |
| 1701 | |
| 1702 | $res = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); |
| 1703 | |
| 1704 | $activityInfo = array(); |
| 1705 | while ($res->fetch()) { |
| 1706 | if ($type == 'upcoming') { |
| 1707 | $activityInfo[$res->case_id]['date'] = $res->case_scheduled_activity_date; |
| 1708 | $activityInfo[$res->case_id]['type'] = $res->case_scheduled_activity_type; |
| 1709 | } |
| 1710 | else { |
| 1711 | $activityInfo[$res->case_id]['date'] = $res->case_recent_activity_date; |
| 1712 | $activityInfo[$res->case_id]['type'] = $res->case_recent_activity_type; |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | return $activityInfo; |
| 1717 | } |
| 1718 | |
| 1719 | /** |
| 1720 | * combine all the exportable fields from the lower levels object |
| 1721 | * |
| 1722 | * @return array array of exportable Fields |
| 1723 | * @access public |
| 1724 | * @static |
| 1725 | */ |
| 1726 | static function &exportableFields() { |
| 1727 | if (!self::$_exportableFields) { |
| 1728 | if (!self::$_exportableFields) { |
| 1729 | self::$_exportableFields = array(); |
| 1730 | } |
| 1731 | |
| 1732 | $fields = CRM_Case_DAO_Case::export(); |
| 1733 | $fields['case_role'] = array('title' => ts('Role in Case')); |
| 1734 | $fields['case_type'] = array( |
| 1735 | 'title' => ts('Case Type'), |
| 1736 | 'name' => 'case_type', |
| 1737 | ); |
| 1738 | $fields['case_status'] = array( |
| 1739 | 'title' => ts('Case Status'), |
| 1740 | 'name' => 'case_status', |
| 1741 | ); |
| 1742 | |
| 1743 | self::$_exportableFields = $fields; |
| 1744 | } |
| 1745 | return self::$_exportableFields; |
| 1746 | } |
| 1747 | |
| 1748 | /** |
| 1749 | * Restore the record that are associated with this case |
| 1750 | * |
| 1751 | * @param int $caseId id of the case to restore |
| 1752 | * |
| 1753 | * @return true if success. |
| 1754 | * @access public |
| 1755 | * @static |
| 1756 | */ |
| 1757 | static function restoreCase($caseId) { |
| 1758 | //restore activities |
| 1759 | $activities = self::getCaseActivityDates($caseId); |
| 1760 | if ($activities) { |
| 1761 | foreach ($activities as $value) { |
| 1762 | CRM_Activity_BAO_Activity::restoreActivity($value); |
| 1763 | } |
| 1764 | } |
| 1765 | //restore case |
| 1766 | $case = new CRM_Case_DAO_Case(); |
| 1767 | $case->id = $caseId; |
| 1768 | $case->is_deleted = 0; |
| 1769 | $case->save(); |
| 1770 | |
| 1771 | //CRM-7364, enable relationships |
| 1772 | self::enableDisableCaseRelationships($caseId, TRUE); |
| 1773 | return TRUE; |
| 1774 | } |
| 1775 | |
| 1776 | /** |
| 1777 | * @param $groupInfo |
| 1778 | * @param null $sort |
| 1779 | * @param null $showLinks |
| 1780 | * @param bool $returnOnlyCount |
| 1781 | * @param int $offset |
| 1782 | * @param int $rowCount |
| 1783 | * |
| 1784 | * @return array |
| 1785 | */ |
| 1786 | static function getGlobalContacts(&$groupInfo, $sort = NULL, $showLinks = NULL, $returnOnlyCount = FALSE, $offset = 0, $rowCount = 25) { |
| 1787 | $globalContacts = array(); |
| 1788 | |
| 1789 | $settingsProcessor = new CRM_Case_XMLProcessor_Settings(); |
| 1790 | $settings = $settingsProcessor->run(); |
| 1791 | if (!empty($settings)) { |
| 1792 | $groupInfo['name'] = $settings['groupname']; |
| 1793 | if ($groupInfo['name']) { |
| 1794 | $searchParams = array('name' => $groupInfo['name']); |
| 1795 | $results = array(); |
| 1796 | CRM_Contact_BAO_Group::retrieve($searchParams, $results); |
| 1797 | if ($results) { |
| 1798 | $groupInfo['id'] = $results['id']; |
| 1799 | $groupInfo['title'] = $results['title']; |
| 1800 | $params = array(array('group', 'IN', array($groupInfo['id'] => 1), 0, 0)); |
| 1801 | $return = array('contact_id' => 1, 'sort_name' => 1, 'display_name' => 1, 'email' => 1, 'phone' => 1); |
| 1802 | list($globalContacts) = CRM_Contact_BAO_Query::apiQuery($params, $return, NULL, $sort, $offset, $rowCount, TRUE, $returnOnlyCount); |
| 1803 | |
| 1804 | if ($returnOnlyCount) { |
| 1805 | return $globalContacts; |
| 1806 | } |
| 1807 | |
| 1808 | if ($showLinks) { |
| 1809 | foreach ($globalContacts as $idx => $contact) { |
| 1810 | $globalContacts[$idx]['sort_name'] = '<a href="' . CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$contact['contact_id']}") . '">' . $contact['sort_name'] . '</a>'; |
| 1811 | } |
| 1812 | } |
| 1813 | } |
| 1814 | } |
| 1815 | } |
| 1816 | return $globalContacts; |
| 1817 | } |
| 1818 | |
| 1819 | /* |
| 1820 | * Convenience function to get both case contacts and global in one array |
| 1821 | */ |
| 1822 | /** |
| 1823 | * @param $caseId |
| 1824 | * |
| 1825 | * @return array |
| 1826 | */ |
| 1827 | static function getRelatedAndGlobalContacts($caseId) { |
| 1828 | $relatedContacts = self::getRelatedContacts($caseId); |
| 1829 | |
| 1830 | $groupInfo = array(); |
| 1831 | $globalContacts = self::getGlobalContacts($groupInfo); |
| 1832 | |
| 1833 | //unset values which are not required. |
| 1834 | foreach ($globalContacts as $k => & $v) { |
| 1835 | unset($v['email_id']); |
| 1836 | unset($v['group_contact_id']); |
| 1837 | unset($v['status']); |
| 1838 | unset($v['phone']); |
| 1839 | $v['role'] = $groupInfo['title']; |
| 1840 | } |
| 1841 | //include multiple listings for the same contact/different roles. |
| 1842 | $relatedGlobalContacts = array_merge($relatedContacts, $globalContacts); |
| 1843 | return $relatedGlobalContacts; |
| 1844 | } |
| 1845 | |
| 1846 | /** |
| 1847 | * Function to get Case ActivitiesDueDates with given criteria. |
| 1848 | * |
| 1849 | * @param int $caseID case id |
| 1850 | * @param array $criteriaParams given criteria |
| 1851 | * @param boolean $latestDate if set newest or oldest date is selceted. |
| 1852 | * |
| 1853 | * @return returns case activities due dates |
| 1854 | * |
| 1855 | * @static |
| 1856 | */ |
| 1857 | static function getCaseActivityDates($caseID, $criteriaParams = array(), $latestDate = FALSE) { |
| 1858 | $values = array(); |
| 1859 | $selectDate = " ca.activity_date_time"; |
| 1860 | $where = $groupBy = ' '; |
| 1861 | |
| 1862 | if (!$caseID) { |
| 1863 | return; |
| 1864 | } |
| 1865 | |
| 1866 | if ($latestDate) { |
| 1867 | if (!empty($criteriaParams['activity_type_id'])) { |
| 1868 | $where .= " AND ca.activity_type_id = " . CRM_Utils_Type::escape($criteriaParams['activity_type_id'], 'Integer'); |
| 1869 | $where .= " AND ca.is_current_revision = 1"; |
| 1870 | $groupBy .= " GROUP BY ca.activity_type_id"; |
| 1871 | } |
| 1872 | |
| 1873 | if (!empty($criteriaParams['newest'])) { |
| 1874 | $selectDate = " max(ca.activity_date_time) "; |
| 1875 | } |
| 1876 | else { |
| 1877 | $selectDate = " min(ca.activity_date_time) "; |
| 1878 | } |
| 1879 | } |
| 1880 | |
| 1881 | $query = "SELECT ca.id, {$selectDate} as activity_date |
| 1882 | FROM civicrm_activity ca |
| 1883 | LEFT JOIN civicrm_case_activity cca ON cca.activity_id = ca.id LEFT JOIN civicrm_case cc ON cc.id = cca.case_id |
| 1884 | WHERE cc.id = %1 {$where} {$groupBy}"; |
| 1885 | |
| 1886 | $params = array(1 => array($caseID, 'Integer')); |
| 1887 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 1888 | |
| 1889 | while ($dao->fetch()) { |
| 1890 | $values[$dao->id]['id'] = $dao->id; |
| 1891 | $values[$dao->id]['activity_date'] = $dao->activity_date; |
| 1892 | } |
| 1893 | $dao->free(); |
| 1894 | return $values; |
| 1895 | } |
| 1896 | |
| 1897 | /** |
| 1898 | * Function to create activities when Case or Other roles assigned/modified/deleted. |
| 1899 | * |
| 1900 | * @param $caseId |
| 1901 | * @param int $relationshipId relationship id |
| 1902 | * @param int $relContactId case role assignee contactId. |
| 1903 | * |
| 1904 | * @param null $contactId |
| 1905 | * |
| 1906 | * @internal param int $caseID case id |
| 1907 | * @return void on success creates activity and case activity |
| 1908 | * |
| 1909 | * @static |
| 1910 | */ |
| 1911 | static function createCaseRoleActivity($caseId, $relationshipId, $relContactId = NULL, $contactId = NULL) { |
| 1912 | if (!$caseId || !$relationshipId || empty($relationshipId)) { |
| 1913 | return; |
| 1914 | } |
| 1915 | |
| 1916 | $queryParam = array(); |
| 1917 | if (is_array($relationshipId)) { |
| 1918 | $relationshipId = implode(',', $relationshipId); |
| 1919 | $relationshipClause = " civicrm_relationship.id IN ($relationshipId)"; |
| 1920 | } |
| 1921 | else { |
| 1922 | $relationshipClause = " civicrm_relationship.id = %1"; |
| 1923 | $queryParam[1] = array($relationshipId, 'Positive'); |
| 1924 | } |
| 1925 | |
| 1926 | $query = " |
| 1927 | SELECT cc.display_name as clientName, |
| 1928 | cca.display_name as assigneeContactName, |
| 1929 | civicrm_relationship.case_id as caseId, |
| 1930 | civicrm_relationship_type.label_a_b as relation_a_b, |
| 1931 | civicrm_relationship_type.label_b_a as relation_b_a, |
| 1932 | civicrm_relationship.contact_id_b as rel_contact_id, |
| 1933 | civicrm_relationship.contact_id_a as assign_contact_id |
| 1934 | FROM civicrm_relationship_type, civicrm_relationship |
| 1935 | LEFT JOIN civicrm_contact cc ON cc.id = civicrm_relationship.contact_id_b |
| 1936 | LEFT JOIN civicrm_contact cca ON cca.id = civicrm_relationship.contact_id_a |
| 1937 | WHERE civicrm_relationship.relationship_type_id = civicrm_relationship_type.id AND {$relationshipClause}"; |
| 1938 | |
| 1939 | $dao = CRM_Core_DAO::executeQuery($query, $queryParam); |
| 1940 | |
| 1941 | while ($dao->fetch()) { |
| 1942 | //to get valid assignee contact(s). |
| 1943 | if (isset($dao->caseId) || $dao->rel_contact_id != $contactId) { |
| 1944 | $caseRelationship = $dao->relation_a_b; |
| 1945 | $assigneContactName = $dao->clientName; |
| 1946 | $assigneContactIds[$dao->rel_contact_id] = $dao->rel_contact_id; |
| 1947 | } |
| 1948 | else { |
| 1949 | $caseRelationship = $dao->relation_b_a; |
| 1950 | $assigneContactName = $dao->assigneeContactName; |
| 1951 | $assigneContactIds[$dao->assign_contact_id] = $dao->assign_contact_id; |
| 1952 | } |
| 1953 | } |
| 1954 | |
| 1955 | $session = CRM_Core_Session::singleton(); |
| 1956 | $activityParams = array( |
| 1957 | 'source_contact_id' => $session->get('userID'), |
| 1958 | 'subject' => $caseRelationship . ' : ' . $assigneContactName, |
| 1959 | 'activity_date_time' => date('YmdHis'), |
| 1960 | 'status_id' => CRM_Core_OptionGroup::getValue('activity_status', 'Completed', 'name'), |
| 1961 | ); |
| 1962 | |
| 1963 | //if $relContactId is passed, role is added or modified. |
| 1964 | if (!empty($relContactId)) { |
| 1965 | $activityParams['assignee_contact_id'] = $assigneContactIds; |
| 1966 | |
| 1967 | $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', |
| 1968 | 'Assign Case Role', |
| 1969 | 'name' |
| 1970 | ); |
| 1971 | } |
| 1972 | else { |
| 1973 | $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', |
| 1974 | 'Remove Case Role', |
| 1975 | 'name' |
| 1976 | ); |
| 1977 | } |
| 1978 | |
| 1979 | $activityParams['activity_type_id'] = $activityTypeID; |
| 1980 | |
| 1981 | $activity = CRM_Activity_BAO_Activity::create($activityParams); |
| 1982 | |
| 1983 | //create case_activity record. |
| 1984 | $caseParams = array( |
| 1985 | 'activity_id' => $activity->id, |
| 1986 | 'case_id' => $caseId, |
| 1987 | ); |
| 1988 | |
| 1989 | CRM_Case_BAO_Case::processCaseActivity($caseParams); |
| 1990 | } |
| 1991 | |
| 1992 | /** |
| 1993 | * Function to get case manger |
| 1994 | * contact which is assigned a case role of case manager. |
| 1995 | * |
| 1996 | * @param int $caseType case type |
| 1997 | * @param int $caseId case id |
| 1998 | * |
| 1999 | * @return array $caseManagerContact array of contact on success otherwise empty |
| 2000 | * |
| 2001 | * @static |
| 2002 | */ |
| 2003 | static function getCaseManagerContact($caseType, $caseId) { |
| 2004 | if (!$caseType || !$caseId) { |
| 2005 | return; |
| 2006 | } |
| 2007 | |
| 2008 | $caseManagerContact = array(); |
| 2009 | $xmlProcessor = new CRM_Case_XMLProcessor_Process(); |
| 2010 | |
| 2011 | $managerRoleId = $xmlProcessor->getCaseManagerRoleId($caseType); |
| 2012 | |
| 2013 | if (!empty($managerRoleId)) { |
| 2014 | $managerRoleQuery = " |
| 2015 | SELECT civicrm_contact.id as casemanager_id, |
| 2016 | civicrm_contact.sort_name as casemanager |
| 2017 | FROM civicrm_contact |
| 2018 | LEFT JOIN civicrm_relationship ON (civicrm_relationship.contact_id_b = civicrm_contact.id AND civicrm_relationship.relationship_type_id = %1) |
| 2019 | LEFT JOIN civicrm_case ON civicrm_case.id = civicrm_relationship.case_id |
| 2020 | WHERE civicrm_case.id = %2"; |
| 2021 | |
| 2022 | $managerRoleParams = array( |
| 2023 | 1 => array($managerRoleId, 'Integer'), |
| 2024 | 2 => array($caseId, 'Integer'), |
| 2025 | ); |
| 2026 | |
| 2027 | $dao = CRM_Core_DAO::executeQuery($managerRoleQuery, $managerRoleParams); |
| 2028 | if ($dao->fetch()) { |
| 2029 | $caseManagerContact['casemanager_id'] = $dao->casemanager_id; |
| 2030 | $caseManagerContact['casemanager'] = $dao->casemanager; |
| 2031 | } |
| 2032 | } |
| 2033 | |
| 2034 | return $caseManagerContact; |
| 2035 | } |
| 2036 | |
| 2037 | /** |
| 2038 | * Get all cases with no end dates |
| 2039 | * |
| 2040 | * @param array $params |
| 2041 | * @param array $excludeCaseIds |
| 2042 | * @param bool $excludeDeleted |
| 2043 | * |
| 2044 | * @return array of case and related data keyed on case id |
| 2045 | */ |
| 2046 | static function getUnclosedCases($params = array(), $excludeCaseIds = array(), $excludeDeleted = TRUE, $includeClosed = FALSE) { |
| 2047 | //params from ajax call. |
| 2048 | $where = array($includeClosed ? '(1)' : '(ca.end_date is null)'); |
| 2049 | if ($caseType = CRM_Utils_Array::value('case_type', $params)) { |
| 2050 | $where[] = "( civicrm_case_type.title LIKE '%$caseType%' )"; |
| 2051 | } |
| 2052 | if ($sortName = CRM_Utils_Array::value('sort_name', $params)) { |
| 2053 | $config = CRM_Core_Config::singleton(); |
| 2054 | $search = ($config->includeWildCardInName) ? "%$sortName%" : "$sortName%"; |
| 2055 | $where[] = "( sort_name LIKE '$search' )"; |
| 2056 | } |
| 2057 | if ($cid = CRM_Utils_Array::value('contact_id', $params)) { |
| 2058 | $where[] = " c.id = $cid "; |
| 2059 | } |
| 2060 | if (is_array($excludeCaseIds) && |
| 2061 | !CRM_Utils_System::isNull($excludeCaseIds) |
| 2062 | ) { |
| 2063 | $where[] = ' ( ca.id NOT IN ( ' . implode(',', $excludeCaseIds) . ' ) ) '; |
| 2064 | } |
| 2065 | if ($excludeDeleted) { |
| 2066 | $where[] = ' ( ca.is_deleted = 0 OR ca.is_deleted IS NULL ) '; |
| 2067 | } |
| 2068 | |
| 2069 | //filter for permissioned cases. |
| 2070 | $filterCases = array(); |
| 2071 | $doFilterCases = FALSE; |
| 2072 | if (!CRM_Core_Permission::check('access all cases and activities')) { |
| 2073 | $doFilterCases = TRUE; |
| 2074 | $session = CRM_Core_Session::singleton(); |
| 2075 | $filterCases = CRM_Case_BAO_Case::getCases(FALSE, $session->get('userID')); |
| 2076 | } |
| 2077 | $whereClause = implode(' AND ', $where); |
| 2078 | |
| 2079 | $limitClause = ''; |
| 2080 | if ($limit = CRM_Utils_Array::value('limit', $params)) { |
| 2081 | $limitClause = "LIMIT 0, $limit"; |
| 2082 | } |
| 2083 | |
| 2084 | $query = " |
| 2085 | SELECT c.id as contact_id, |
| 2086 | c.sort_name, |
| 2087 | ca.id, |
| 2088 | ca.subject as case_subject, |
| 2089 | civicrm_case_type.title as case_type, |
| 2090 | ca.start_date as start_date, |
| 2091 | ca.end_date as end_date, |
| 2092 | ca.status_id |
| 2093 | FROM civicrm_case ca INNER JOIN civicrm_case_contact cc ON ca.id=cc.case_id |
| 2094 | INNER JOIN civicrm_contact c ON cc.contact_id=c.id |
| 2095 | INNER JOIN civicrm_case_type ON ca.case_type_id = civicrm_case_type.id |
| 2096 | WHERE {$whereClause} |
| 2097 | ORDER BY c.sort_name, ca.end_date |
| 2098 | {$limitClause} |
| 2099 | "; |
| 2100 | $dao = CRM_Core_DAO::executeQuery($query); |
| 2101 | $statuses = CRM_Case_BAO_Case::buildOptions('status_id', 'create'); |
| 2102 | |
| 2103 | $unclosedCases = array(); |
| 2104 | while ($dao->fetch()) { |
| 2105 | if ($doFilterCases && !array_key_exists($dao->id, $filterCases)) { |
| 2106 | continue; |
| 2107 | } |
| 2108 | $unclosedCases[$dao->id] = array( |
| 2109 | 'sort_name' => $dao->sort_name, |
| 2110 | 'case_type' => $dao->case_type, |
| 2111 | 'contact_id' => $dao->contact_id, |
| 2112 | 'start_date' => $dao->start_date, |
| 2113 | 'end_date' => $dao->end_date, |
| 2114 | 'case_subject' => $dao->case_subject, |
| 2115 | 'case_status' => $statuses[$dao->status_id], |
| 2116 | ); |
| 2117 | } |
| 2118 | $dao->free(); |
| 2119 | |
| 2120 | return $unclosedCases; |
| 2121 | } |
| 2122 | |
| 2123 | /** |
| 2124 | * @param null $contactId |
| 2125 | * @param bool $excludeDeleted |
| 2126 | * |
| 2127 | * @return null|string |
| 2128 | */ |
| 2129 | static function caseCount($contactId = NULL, $excludeDeleted = TRUE) { |
| 2130 | $whereConditions = array(); |
| 2131 | if ($excludeDeleted) { |
| 2132 | $whereConditions[] = "( civicrm_case.is_deleted = 0 OR civicrm_case.is_deleted IS NULL )"; |
| 2133 | } |
| 2134 | if ($contactId) { |
| 2135 | $whereConditions[] = "civicrm_case_contact.contact_id = {$contactId}"; |
| 2136 | } |
| 2137 | if (!CRM_Core_Permission::check('access all cases and activities')) { |
| 2138 | static $accessibleCaseIds; |
| 2139 | if (!is_array($accessibleCaseIds)) { |
| 2140 | $session = CRM_Core_Session::singleton(); |
| 2141 | $accessibleCaseIds = array_keys(self::getCases(FALSE, $session->get('userID'), 'any')); |
| 2142 | } |
| 2143 | //no need of further processing. |
| 2144 | if (empty($accessibleCaseIds)) { |
| 2145 | return 0; |
| 2146 | } |
| 2147 | $whereConditions[] = "( civicrm_case.id in (" . implode(',', $accessibleCaseIds) . ") )"; |
| 2148 | } |
| 2149 | |
| 2150 | $whereClause = ''; |
| 2151 | if (!empty($whereConditions)) { |
| 2152 | $whereClause = "WHERE " . implode(' AND ', $whereConditions); |
| 2153 | } |
| 2154 | |
| 2155 | $query = " |
| 2156 | SELECT count( civicrm_case.id ) |
| 2157 | FROM civicrm_case |
| 2158 | LEFT JOIN civicrm_case_contact ON ( civicrm_case.id = civicrm_case_contact.case_id ) |
| 2159 | {$whereClause}"; |
| 2160 | |
| 2161 | return CRM_Core_DAO::singleValueQuery($query); |
| 2162 | } |
| 2163 | |
| 2164 | /** |
| 2165 | * Retrieve cases related to particular contact. |
| 2166 | * |
| 2167 | * @param int $contactId contact id |
| 2168 | * @param boolean $excludeDeleted do not include deleted cases. |
| 2169 | * |
| 2170 | * @return an array of cases. |
| 2171 | * |
| 2172 | * @access public |
| 2173 | */ |
| 2174 | static function getContactCases($contactId, $excludeDeleted = TRUE) { |
| 2175 | $cases = array(); |
| 2176 | if (!$contactId) { |
| 2177 | return $cases; |
| 2178 | } |
| 2179 | |
| 2180 | $whereClause = "civicrm_case_contact.contact_id = %1"; |
| 2181 | if ($excludeDeleted) { |
| 2182 | $whereClause .= " AND ( civicrm_case.is_deleted = 0 OR civicrm_case.is_deleted IS NULL )"; |
| 2183 | } |
| 2184 | |
| 2185 | $query = " |
| 2186 | SELECT civicrm_case.id, civicrm_case_type.title as case_type, civicrm_case.start_date |
| 2187 | FROM civicrm_case |
| 2188 | INNER JOIN civicrm_case_contact ON ( civicrm_case.id = civicrm_case_contact.case_id ) |
| 2189 | LEFT JOIN civicrm_case_type ON civicrm_case.case_type_id = civicrm_case_type.id |
| 2190 | WHERE {$whereClause}"; |
| 2191 | |
| 2192 | $dao = CRM_Core_DAO::executeQuery($query, array(1 => array($contactId, 'Integer'))); |
| 2193 | while ($dao->fetch()) { |
| 2194 | $cases[$dao->id] = array( |
| 2195 | 'case_id' => $dao->id, |
| 2196 | 'case_type' => $dao->case_type, |
| 2197 | 'case_start_date' => $dao->start_date, |
| 2198 | ); |
| 2199 | } |
| 2200 | $dao->free(); |
| 2201 | |
| 2202 | return $cases; |
| 2203 | } |
| 2204 | |
| 2205 | /** |
| 2206 | * Retrieve related cases for give case. |
| 2207 | * |
| 2208 | * @param int $mainCaseId id of main case |
| 2209 | * @param int $contactId id of contact |
| 2210 | * @param boolean $excludeDeleted do not include deleted cases. |
| 2211 | * |
| 2212 | * @return an array of related cases. |
| 2213 | * |
| 2214 | * @access public |
| 2215 | */ |
| 2216 | static function getRelatedCases($mainCaseId, $contactId, $excludeDeleted = TRUE) { |
| 2217 | //FIXME : do check for permissions. |
| 2218 | |
| 2219 | $relatedCases = array(); |
| 2220 | if (!$mainCaseId || !$contactId) { |
| 2221 | return $relatedCases; |
| 2222 | } |
| 2223 | |
| 2224 | $linkActType = array_search('Link Cases', |
| 2225 | CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'name') |
| 2226 | ); |
| 2227 | if (!$linkActType) { |
| 2228 | return $relatedCases; |
| 2229 | } |
| 2230 | |
| 2231 | $whereClause = "mainCase.id = %2"; |
| 2232 | if ($excludeDeleted) { |
| 2233 | $whereClause .= " AND ( relAct.is_deleted = 0 OR relAct.is_deleted IS NULL )"; |
| 2234 | } |
| 2235 | |
| 2236 | //1. first fetch related case ids. |
| 2237 | $query = " |
| 2238 | SELECT relCaseAct.case_id |
| 2239 | FROM civicrm_case mainCase |
| 2240 | INNER JOIN civicrm_case_activity mainCaseAct ON (mainCaseAct.case_id = mainCase.id) |
| 2241 | INNER JOIN civicrm_activity mainAct ON (mainCaseAct.activity_id = mainAct.id AND mainAct.activity_type_id = %1) |
| 2242 | INNER JOIN civicrm_case_activity relCaseAct ON (relCaseAct.activity_id = mainAct.id AND mainCaseAct.id != relCaseAct.id) |
| 2243 | INNER JOIN civicrm_activity relAct ON (relCaseAct.activity_id = relAct.id AND relAct.activity_type_id = %1) |
| 2244 | WHERE $whereClause"; |
| 2245 | |
| 2246 | $dao = CRM_Core_DAO::executeQuery($query, array( |
| 2247 | 1 => array($linkActType, 'Integer'), |
| 2248 | 2 => array($mainCaseId, 'Integer'), |
| 2249 | )); |
| 2250 | $relatedCaseIds = array(); |
| 2251 | while ($dao->fetch()) { |
| 2252 | $relatedCaseIds[$dao->case_id] = $dao->case_id; |
| 2253 | } |
| 2254 | $dao->free(); |
| 2255 | |
| 2256 | // there are no related cases. |
| 2257 | if (empty($relatedCaseIds)) { |
| 2258 | return $relatedCases; |
| 2259 | } |
| 2260 | |
| 2261 | $whereClause = 'relCase.id IN ( ' . implode(',', $relatedCaseIds) . ' )'; |
| 2262 | if ($excludeDeleted) { |
| 2263 | $whereClause .= " AND ( relCase.is_deleted = 0 OR relCase.is_deleted IS NULL )"; |
| 2264 | } |
| 2265 | |
| 2266 | //filter for permissioned cases. |
| 2267 | $filterCases = array(); |
| 2268 | $doFilterCases = FALSE; |
| 2269 | if (!CRM_Core_Permission::check('access all cases and activities')) { |
| 2270 | $doFilterCases = TRUE; |
| 2271 | $session = CRM_Core_Session::singleton(); |
| 2272 | $filterCases = CRM_Case_BAO_Case::getCases(FALSE, $session->get('userID')); |
| 2273 | } |
| 2274 | |
| 2275 | //2. fetch the details of related cases. |
| 2276 | $query = " |
| 2277 | SELECT relCase.id as id, |
| 2278 | civicrm_case_type.title as case_type, |
| 2279 | client.display_name as client_name, |
| 2280 | client.id as client_id |
| 2281 | FROM civicrm_case relCase |
| 2282 | INNER JOIN civicrm_case_contact relCaseContact ON ( relCase.id = relCaseContact.case_id ) |
| 2283 | INNER JOIN civicrm_contact client ON ( client.id = relCaseContact.contact_id ) |
| 2284 | LEFT JOIN civicrm_case_type ON relCase.case_type_id = civicrm_case_type.id |
| 2285 | WHERE {$whereClause}"; |
| 2286 | |
| 2287 | $dao = CRM_Core_DAO::executeQuery($query); |
| 2288 | $contactViewUrl = CRM_Utils_System::url("civicrm/contact/view", "reset=1&cid="); |
| 2289 | $hasViewContact = CRM_Core_Permission::giveMeAllACLs(); |
| 2290 | |
| 2291 | while ($dao->fetch()) { |
| 2292 | $caseView = NULL; |
| 2293 | if (!$doFilterCases || array_key_exists($dao->id, $filterCases)) { |
| 2294 | $caseViewStr = "reset=1&id={$dao->id}&cid={$dao->client_id}&action=view&context=case&selectedChild=case"; |
| 2295 | $caseViewUrl = CRM_Utils_System::url("civicrm/contact/view/case", $caseViewStr); |
| 2296 | $caseView = "<a class='action-item no-popup crm-hover-button' href='{$caseViewUrl}'>" . ts('View Case') . "</a>"; |
| 2297 | } |
| 2298 | $clientView = $dao->client_name; |
| 2299 | if ($hasViewContact) { |
| 2300 | $clientView = "<a href='{$contactViewUrl}{$dao->client_id}'>$dao->client_name</a>"; |
| 2301 | } |
| 2302 | |
| 2303 | $relatedCases[$dao->id] = array( |
| 2304 | 'case_id' => $dao->id, |
| 2305 | 'case_type' => $dao->case_type, |
| 2306 | 'client_name' => $clientView, |
| 2307 | 'links' => $caseView, |
| 2308 | ); |
| 2309 | } |
| 2310 | $dao->free(); |
| 2311 | |
| 2312 | return $relatedCases; |
| 2313 | } |
| 2314 | |
| 2315 | /** |
| 2316 | * Merge two duplicate contacts' cases - follow CRM-5758 rules. |
| 2317 | * |
| 2318 | * @see CRM_Dedupe_Merger::cpTables() |
| 2319 | * |
| 2320 | * TODO: use the 3rd $sqls param to append sql statements rather than executing them here |
| 2321 | */ |
| 2322 | static function mergeContacts($mainContactId, $otherContactId) { |
| 2323 | self::mergeCases($mainContactId, NULL, $otherContactId); |
| 2324 | } |
| 2325 | |
| 2326 | /** |
| 2327 | * Function perform two task. |
| 2328 | * 1. Merge two duplicate contacts cases - follow CRM-5758 rules. |
| 2329 | * 2. Merge two cases of same contact - follow CRM-5598 rules. |
| 2330 | * |
| 2331 | * @param int $mainContactId contact id of main contact record. |
| 2332 | * @param int $mainCaseId case id of main case record. |
| 2333 | * @param int $otherContactId contact id of record which is going to merge. |
| 2334 | * @param int $otherCaseId case id of record which is going to merge. |
| 2335 | * |
| 2336 | * @param bool $changeClient |
| 2337 | * |
| 2338 | * @return integer|NULL |
| 2339 | * @static |
| 2340 | */ |
| 2341 | static function mergeCases($mainContactId, $mainCaseId = NULL, $otherContactId = NULL, |
| 2342 | $otherCaseId = NULL, $changeClient = FALSE) { |
| 2343 | $moveToTrash = TRUE; |
| 2344 | |
| 2345 | $duplicateContacts = FALSE; |
| 2346 | if ($mainContactId && $otherContactId && |
| 2347 | $mainContactId != $otherContactId |
| 2348 | ) { |
| 2349 | $duplicateContacts = TRUE; |
| 2350 | } |
| 2351 | |
| 2352 | $duplicateCases = FALSE; |
| 2353 | if ($mainCaseId && $otherCaseId && |
| 2354 | $mainCaseId != $otherCaseId |
| 2355 | ) { |
| 2356 | $duplicateCases = TRUE; |
| 2357 | } |
| 2358 | |
| 2359 | $mainCaseIds = array(); |
| 2360 | if (!$duplicateContacts && !$duplicateCases) { |
| 2361 | return $mainCaseIds; |
| 2362 | } |
| 2363 | |
| 2364 | $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'name'); |
| 2365 | $activityStatuses = CRM_Core_PseudoConstant::activityStatus('name'); |
| 2366 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
| 2367 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
| 2368 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); |
| 2369 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
| 2370 | |
| 2371 | $processCaseIds = array($otherCaseId); |
| 2372 | if ($duplicateContacts && !$duplicateCases) { |
| 2373 | if ($changeClient) { |
| 2374 | $processCaseIds = array($mainCaseId); |
| 2375 | } |
| 2376 | else { |
| 2377 | //get all case ids for other contact. |
| 2378 | $processCaseIds = self::retrieveCaseIdsByContactId($otherContactId, TRUE); |
| 2379 | } |
| 2380 | if (!is_array($processCaseIds)) { |
| 2381 | return; |
| 2382 | } |
| 2383 | } |
| 2384 | |
| 2385 | $session = CRM_Core_Session::singleton(); |
| 2386 | $currentUserId = $session->get('userID'); |
| 2387 | |
| 2388 | CRM_Utils_Hook::pre_case_merge($mainContactId, $mainCaseId, $otherContactId, $otherCaseId, $changeClient); |
| 2389 | |
| 2390 | // copy all cases and connect to main contact id. |
| 2391 | foreach ($processCaseIds as $otherCaseId) { |
| 2392 | if ($duplicateContacts) { |
| 2393 | $mainCase = CRM_Core_DAO::copyGeneric('CRM_Case_DAO_Case', array('id' => $otherCaseId)); |
| 2394 | $mainCaseId = $mainCase->id; |
| 2395 | if (!$mainCaseId) { |
| 2396 | continue; |
| 2397 | } |
| 2398 | |
| 2399 | // CRM-11662 Copy Case custom data |
| 2400 | $extends = array('case'); |
| 2401 | $groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, NULL, $extends); |
| 2402 | if ($groupTree) { |
| 2403 | foreach ($groupTree as $groupID => $group) { |
| 2404 | $table[$groupTree[$groupID]['table_name']] = array('entity_id'); |
| 2405 | foreach ($group['fields'] as $fieldID => $field) { |
| 2406 | $table[$groupTree[$groupID]['table_name']][] = $groupTree[$groupID]['fields'][$fieldID]['column_name']; |
| 2407 | } |
| 2408 | } |
| 2409 | |
| 2410 | foreach ($table as $tableName => $tableColumns) { |
| 2411 | $insert = 'INSERT INTO ' . $tableName . ' (' . implode(', ', $tableColumns) . ') '; |
| 2412 | $tableColumns[0] = $mainCaseId; |
| 2413 | $select = 'SELECT ' . implode(', ', $tableColumns); |
| 2414 | $from = ' FROM ' . $tableName; |
| 2415 | $where = " WHERE {$tableName}.entity_id = {$otherCaseId}"; |
| 2416 | $query = $insert . $select . $from . $where; |
| 2417 | $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); |
| 2418 | } |
| 2419 | } |
| 2420 | |
| 2421 | $mainCase->free(); |
| 2422 | |
| 2423 | $mainCaseIds[] = $mainCaseId; |
| 2424 | //insert record for case contact. |
| 2425 | $otherCaseContact = new CRM_Case_DAO_CaseContact(); |
| 2426 | $otherCaseContact->case_id = $otherCaseId; |
| 2427 | $otherCaseContact->find(); |
| 2428 | while ($otherCaseContact->fetch()) { |
| 2429 | $mainCaseContact = new CRM_Case_DAO_CaseContact(); |
| 2430 | $mainCaseContact->case_id = $mainCaseId; |
| 2431 | $mainCaseContact->contact_id = $otherCaseContact->contact_id; |
| 2432 | if ($mainCaseContact->contact_id == $otherContactId) { |
| 2433 | $mainCaseContact->contact_id = $mainContactId; |
| 2434 | } |
| 2435 | //avoid duplicate object. |
| 2436 | if (!$mainCaseContact->find(TRUE)) { |
| 2437 | $mainCaseContact->save(); |
| 2438 | } |
| 2439 | $mainCaseContact->free(); |
| 2440 | } |
| 2441 | $otherCaseContact->free(); |
| 2442 | } |
| 2443 | elseif (!$otherContactId) { |
| 2444 | $otherContactId = $mainContactId; |
| 2445 | } |
| 2446 | |
| 2447 | if (!$mainCaseId || !$otherCaseId || |
| 2448 | !$mainContactId || !$otherContactId |
| 2449 | ) { |
| 2450 | continue; |
| 2451 | } |
| 2452 | |
| 2453 | // get all activities for other case. |
| 2454 | $otherCaseActivities = array(); |
| 2455 | CRM_Core_DAO::commonRetrieveAll('CRM_Case_DAO_CaseActivity', 'case_id', $otherCaseId, $otherCaseActivities); |
| 2456 | |
| 2457 | //for duplicate cases do not process singleton activities. |
| 2458 | $otherActivityIds = $singletonActivityIds = array(); |
| 2459 | foreach ($otherCaseActivities as $caseActivityId => $otherIds) { |
| 2460 | $otherActId = CRM_Utils_Array::value('activity_id', $otherIds); |
| 2461 | if (!$otherActId || in_array($otherActId, $otherActivityIds)) { |
| 2462 | continue; |
| 2463 | } |
| 2464 | $otherActivityIds[] = $otherActId; |
| 2465 | } |
| 2466 | if ($duplicateCases) { |
| 2467 | if ($openCaseType = array_search('Open Case', $activityTypes)) { |
| 2468 | $sql = " |
| 2469 | SELECT id |
| 2470 | FROM civicrm_activity |
| 2471 | WHERE activity_type_id = $openCaseType |
| 2472 | AND id IN ( " . implode(',', array_values($otherActivityIds)) . ');'; |
| 2473 | $dao = CRM_Core_DAO::executeQuery($sql); |
| 2474 | while ($dao->fetch()) { |
| 2475 | $singletonActivityIds[] = $dao->id; |
| 2476 | } |
| 2477 | $dao->free(); |
| 2478 | } |
| 2479 | } |
| 2480 | |
| 2481 | // migrate all activities and connect to main contact. |
| 2482 | $copiedActivityIds = $activityMappingIds = array(); |
| 2483 | sort($otherActivityIds); |
| 2484 | foreach ($otherActivityIds as $otherActivityId) { |
| 2485 | |
| 2486 | //for duplicate cases - |
| 2487 | //do not migrate singleton activities. |
| 2488 | if (!$otherActivityId || in_array($otherActivityId, $singletonActivityIds)) { |
| 2489 | continue; |
| 2490 | } |
| 2491 | |
| 2492 | //migrate activity record. |
| 2493 | $otherActivity = new CRM_Activity_DAO_Activity(); |
| 2494 | $otherActivity->id = $otherActivityId; |
| 2495 | if (!$otherActivity->find(TRUE)) { |
| 2496 | continue; |
| 2497 | } |
| 2498 | |
| 2499 | $mainActVals = array(); |
| 2500 | $mainActivity = new CRM_Activity_DAO_Activity(); |
| 2501 | CRM_Core_DAO::storeValues($otherActivity, $mainActVals); |
| 2502 | $mainActivity->copyValues($mainActVals); |
| 2503 | $mainActivity->id = NULL; |
| 2504 | $mainActivity->activity_date_time = CRM_Utils_Date::isoToMysql($otherActivity->activity_date_time); |
| 2505 | $mainActivity->source_record_id = CRM_Utils_Array::value($mainActivity->source_record_id, |
| 2506 | $activityMappingIds |
| 2507 | ); |
| 2508 | |
| 2509 | $mainActivity->original_id = CRM_Utils_Array::value($mainActivity->original_id, |
| 2510 | $activityMappingIds |
| 2511 | ); |
| 2512 | |
| 2513 | $mainActivity->parent_id = CRM_Utils_Array::value($mainActivity->parent_id, |
| 2514 | $activityMappingIds |
| 2515 | ); |
| 2516 | $mainActivity->save(); |
| 2517 | $mainActivityId = $mainActivity->id; |
| 2518 | if (!$mainActivityId) { |
| 2519 | continue; |
| 2520 | } |
| 2521 | |
| 2522 | $activityMappingIds[$otherActivityId] = $mainActivityId; |
| 2523 | // insert log of all activities |
| 2524 | CRM_Activity_BAO_Activity::logActivityAction($mainActivity); |
| 2525 | |
| 2526 | $otherActivity->free(); |
| 2527 | $mainActivity->free(); |
| 2528 | $copiedActivityIds[] = $otherActivityId; |
| 2529 | |
| 2530 | //create case activity record. |
| 2531 | $mainCaseActivity = new CRM_Case_DAO_CaseActivity(); |
| 2532 | $mainCaseActivity->case_id = $mainCaseId; |
| 2533 | $mainCaseActivity->activity_id = $mainActivityId; |
| 2534 | $mainCaseActivity->save(); |
| 2535 | $mainCaseActivity->free(); |
| 2536 | |
| 2537 | //migrate source activity. |
| 2538 | $otherSourceActivity = new CRM_Activity_DAO_ActivityContact(); |
| 2539 | $otherSourceActivity->activity_id = $otherActivityId; |
| 2540 | $otherSourceActivity->record_type_id = $sourceID; |
| 2541 | $otherSourceActivity->find(); |
| 2542 | while ($otherSourceActivity->fetch()) { |
| 2543 | $mainActivitySource = new CRM_Activity_DAO_ActivityContact(); |
| 2544 | $mainActivitySource->record_type_id = $sourceID; |
| 2545 | $mainActivitySource->activity_id = $mainActivityId; |
| 2546 | $mainActivitySource->contact_id = $otherSourceActivity->contact_id; |
| 2547 | if ($mainActivitySource->contact_id == $otherContactId) { |
| 2548 | $mainActivitySource->contact_id = $mainContactId; |
| 2549 | } |
| 2550 | //avoid duplicate object. |
| 2551 | if (!$mainActivitySource->find(TRUE)) { |
| 2552 | $mainActivitySource->save(); |
| 2553 | } |
| 2554 | $mainActivitySource->free(); |
| 2555 | } |
| 2556 | $otherSourceActivity->free(); |
| 2557 | |
| 2558 | //migrate target activities. |
| 2559 | $otherTargetActivity = new CRM_Activity_DAO_ActivityContact(); |
| 2560 | $otherTargetActivity->activity_id = $otherActivityId; |
| 2561 | $otherTargetActivity->record_type_id = $targetID; |
| 2562 | $otherTargetActivity->find(); |
| 2563 | while ($otherTargetActivity->fetch()) { |
| 2564 | $mainActivityTarget = new CRM_Activity_DAO_ActivityContact(); |
| 2565 | $mainActivityTarget->record_type_id = $targetID; |
| 2566 | $mainActivityTarget->activity_id = $mainActivityId; |
| 2567 | $mainActivityTarget->contact_id = $otherTargetActivity->contact_id; |
| 2568 | if ($mainActivityTarget->contact_id == $otherContactId) { |
| 2569 | $mainActivityTarget->contact_id = $mainContactId; |
| 2570 | } |
| 2571 | //avoid duplicate object. |
| 2572 | if (!$mainActivityTarget->find(TRUE)) { |
| 2573 | $mainActivityTarget->save(); |
| 2574 | } |
| 2575 | $mainActivityTarget->free(); |
| 2576 | } |
| 2577 | $otherTargetActivity->free(); |
| 2578 | |
| 2579 | //migrate assignee activities. |
| 2580 | $otherAssigneeActivity = new CRM_Activity_DAO_ActivityContact(); |
| 2581 | $otherAssigneeActivity->activity_id = $otherActivityId; |
| 2582 | $otherAssigneeActivity->record_type_id = $assigneeID; |
| 2583 | $otherAssigneeActivity->find(); |
| 2584 | while ($otherAssigneeActivity->fetch()) { |
| 2585 | $mainAssigneeActivity = new CRM_Activity_DAO_ActivityContact(); |
| 2586 | $mainAssigneeActivity->activity_id = $mainActivityId; |
| 2587 | $mainAssigneeActivity->record_type_id = $assigneeID; |
| 2588 | $mainAssigneeActivity->contact_id = $otherAssigneeActivity->contact_id; |
| 2589 | if ($mainAssigneeActivity->contact_id == $otherContactId) { |
| 2590 | $mainAssigneeActivity->contact_id = $mainContactId; |
| 2591 | } |
| 2592 | //avoid duplicate object. |
| 2593 | if (!$mainAssigneeActivity->find(TRUE)) { |
| 2594 | $mainAssigneeActivity->save(); |
| 2595 | } |
| 2596 | $mainAssigneeActivity->free(); |
| 2597 | } |
| 2598 | $otherAssigneeActivity->free(); |
| 2599 | |
| 2600 | // copy custom fields and attachments |
| 2601 | $aparams = array( |
| 2602 | 'activityID' => $otherActivityId, |
| 2603 | 'mainActivityId' => $mainActivityId, |
| 2604 | ); |
| 2605 | CRM_Activity_BAO_Activity::copyExtendedActivityData($aparams); |
| 2606 | } |
| 2607 | |
| 2608 | //copy case relationship. |
| 2609 | if ($duplicateContacts) { |
| 2610 | //migrate relationship records. |
| 2611 | $otherRelationship = new CRM_Contact_DAO_Relationship(); |
| 2612 | $otherRelationship->case_id = $otherCaseId; |
| 2613 | $otherRelationship->find(); |
| 2614 | $otherRelationshipIds = array(); |
| 2615 | while ($otherRelationship->fetch()) { |
| 2616 | $otherRelVals = array(); |
| 2617 | $updateOtherRel = FALSE; |
| 2618 | CRM_Core_DAO::storeValues($otherRelationship, $otherRelVals); |
| 2619 | |
| 2620 | $mainRelationship = new CRM_Contact_DAO_Relationship(); |
| 2621 | $mainRelationship->copyValues($otherRelVals); |
| 2622 | $mainRelationship->id = NULL; |
| 2623 | $mainRelationship->case_id = $mainCaseId; |
| 2624 | if ($mainRelationship->contact_id_a == $otherContactId) { |
| 2625 | $updateOtherRel = TRUE; |
| 2626 | $mainRelationship->contact_id_a = $mainContactId; |
| 2627 | } |
| 2628 | |
| 2629 | //case creator change only when we merge user contact. |
| 2630 | if ($mainRelationship->contact_id_b == $otherContactId) { |
| 2631 | //do not change creator for change client. |
| 2632 | if (!$changeClient) { |
| 2633 | $updateOtherRel = TRUE; |
| 2634 | $mainRelationship->contact_id_b = ($currentUserId) ? $currentUserId : $mainContactId; |
| 2635 | } |
| 2636 | } |
| 2637 | $mainRelationship->end_date = CRM_Utils_Date::isoToMysql($otherRelationship->end_date); |
| 2638 | $mainRelationship->start_date = CRM_Utils_Date::isoToMysql($otherRelationship->start_date); |
| 2639 | |
| 2640 | //avoid duplicate object. |
| 2641 | if (!$mainRelationship->find(TRUE)) { |
| 2642 | $mainRelationship->save(); |
| 2643 | } |
| 2644 | $mainRelationship->free(); |
| 2645 | |
| 2646 | //get the other relationship ids to update end date. |
| 2647 | if ($updateOtherRel) { |
| 2648 | $otherRelationshipIds[$otherRelationship->id] = $otherRelationship->id; |
| 2649 | } |
| 2650 | } |
| 2651 | $otherRelationship->free(); |
| 2652 | |
| 2653 | //update other relationships end dates |
| 2654 | if (!empty($otherRelationshipIds)) { |
| 2655 | $sql = 'UPDATE civicrm_relationship |
| 2656 | SET end_date = CURDATE() |
| 2657 | WHERE id IN ( ' . implode(',', $otherRelationshipIds) . ')'; |
| 2658 | CRM_Core_DAO::executeQuery($sql); |
| 2659 | } |
| 2660 | } |
| 2661 | |
| 2662 | //move other case to trash. |
| 2663 | $mergeCase = self::deleteCase($otherCaseId, $moveToTrash); |
| 2664 | if (!$mergeCase) { |
| 2665 | continue; |
| 2666 | } |
| 2667 | |
| 2668 | $mergeActSubject = $mergeActSubjectDetails = $mergeActType = ''; |
| 2669 | if ($changeClient) { |
| 2670 | $mainContactDisplayName = CRM_Contact_BAO_Contact::displayName($mainContactId); |
| 2671 | $otherContactDisplayName = CRM_Contact_BAO_Contact::displayName($otherContactId); |
| 2672 | |
| 2673 | $mergeActType = array_search('Reassigned Case', $activityTypes); |
| 2674 | $mergeActSubject = ts("Case %1 reassigned client from %2 to %3. New Case ID is %4.", |
| 2675 | array( |
| 2676 | 1 => $otherCaseId, |
| 2677 | 2 => $otherContactDisplayName, |
| 2678 | 3 => $mainContactDisplayName, |
| 2679 | 4 => $mainCaseId |
| 2680 | ) |
| 2681 | ); |
| 2682 | } |
| 2683 | elseif ($duplicateContacts) { |
| 2684 | $mergeActType = array_search('Merge Case', $activityTypes); |
| 2685 | $mergeActSubject = ts("Case %1 copied from contact id %2 to contact id %3 via merge. New Case ID is %4.", |
| 2686 | array( |
| 2687 | 1 => $otherCaseId, |
| 2688 | 2 => $otherContactId, |
| 2689 | 3 => $mainContactId, |
| 2690 | 4 => $mainCaseId |
| 2691 | ) |
| 2692 | ); |
| 2693 | } |
| 2694 | else { |
| 2695 | $mergeActType = array_search('Merge Case', $activityTypes); |
| 2696 | $mergeActSubject = ts("Case %1 merged into case %2", array(1 => $otherCaseId, 2 => $mainCaseId)); |
| 2697 | if (!empty($copiedActivityIds)) { |
| 2698 | $sql = ' |
| 2699 | SELECT id, subject, activity_date_time, activity_type_id |
| 2700 | FROM civicrm_activity |
| 2701 | WHERE id IN (' . implode(',', $copiedActivityIds) . ')'; |
| 2702 | $dao = CRM_Core_DAO::executeQuery($sql); |
| 2703 | while ($dao->fetch()) { |
| 2704 | $mergeActSubjectDetails .= "{$dao->activity_date_time} :: {$activityTypes[$dao->activity_type_id]}"; |
| 2705 | if ($dao->subject) { |
| 2706 | $mergeActSubjectDetails .= " :: {$dao->subject}"; |
| 2707 | } |
| 2708 | $mergeActSubjectDetails .= "<br />"; |
| 2709 | } |
| 2710 | } |
| 2711 | } |
| 2712 | |
| 2713 | //create merge activity record. |
| 2714 | $activityParams = array( |
| 2715 | 'subject' => $mergeActSubject, |
| 2716 | 'details' => $mergeActSubjectDetails, |
| 2717 | 'status_id' => array_search('Completed', $activityStatuses), |
| 2718 | 'activity_type_id' => $mergeActType, |
| 2719 | 'source_contact_id' => $mainContactId, |
| 2720 | 'activity_date_time' => date('YmdHis'), |
| 2721 | ); |
| 2722 | |
| 2723 | $mergeActivity = CRM_Activity_BAO_Activity::create($activityParams); |
| 2724 | $mergeActivityId = $mergeActivity->id; |
| 2725 | if (!$mergeActivityId) { |
| 2726 | continue; |
| 2727 | } |
| 2728 | $mergeActivity->free(); |
| 2729 | |
| 2730 | //connect merge activity to case. |
| 2731 | $mergeCaseAct = array( |
| 2732 | 'case_id' => $mainCaseId, |
| 2733 | 'activity_id' => $mergeActivityId, |
| 2734 | ); |
| 2735 | |
| 2736 | self::processCaseActivity($mergeCaseAct); |
| 2737 | } |
| 2738 | |
| 2739 | CRM_Utils_Hook::post_case_merge($mainContactId, $mainCaseId, $otherContactId, $otherCaseId, $changeClient); |
| 2740 | |
| 2741 | return $mainCaseIds; |
| 2742 | } |
| 2743 | |
| 2744 | /** |
| 2745 | * Validate contact permission for |
| 2746 | * edit/view on activity record and build links. |
| 2747 | * |
| 2748 | * @param array $tplParams params to be sent to template for sending email. |
| 2749 | * @param array $activityParams info of the activity. |
| 2750 | * |
| 2751 | * @return void |
| 2752 | * @static |
| 2753 | */ |
| 2754 | static function buildPermissionLinks(&$tplParams, $activityParams) { |
| 2755 | $activityTypeId = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $activityParams['source_record_id'], |
| 2756 | 'activity_type_id', 'id' |
| 2757 | ); |
| 2758 | |
| 2759 | if (!empty($tplParams['isCaseActivity'])) { |
| 2760 | $tplParams['editActURL'] = CRM_Utils_System::url('civicrm/case/activity', |
| 2761 | "reset=1&cid={$activityParams['target_id']}&caseid={$activityParams['case_id']}&action=update&id={$activityParams['source_record_id']}", TRUE |
| 2762 | ); |
| 2763 | |
| 2764 | $tplParams['viewActURL'] = CRM_Utils_System::url('civicrm/case/activity/view', |
| 2765 | "reset=1&aid={$activityParams['source_record_id']}&cid={$activityParams['target_id']}&caseID={$activityParams['case_id']}", TRUE |
| 2766 | ); |
| 2767 | |
| 2768 | $tplParams['manageCaseURL'] = CRM_Utils_System::url('civicrm/contact/view/case', |
| 2769 | "reset=1&id={$activityParams['case_id']}&cid={$activityParams['target_id']}&action=view&context=home", TRUE |
| 2770 | ); |
| 2771 | } |
| 2772 | else { |
| 2773 | $tplParams['editActURL'] = CRM_Utils_System::url('civicrm/contact/view/activity', |
| 2774 | "atype=$activityTypeId&action=update&reset=1&id={$activityParams['source_record_id']}&cid={$tplParams['contact']['contact_id']}&context=activity", TRUE |
| 2775 | ); |
| 2776 | |
| 2777 | $tplParams['viewActURL'] = CRM_Utils_System::url('civicrm/contact/view/activity', |
| 2778 | "atype=$activityTypeId&action=view&reset=1&id={$activityParams['source_record_id']}&cid={$tplParams['contact']['contact_id']}&context=activity", TRUE |
| 2779 | ); |
| 2780 | } |
| 2781 | } |
| 2782 | |
| 2783 | /** |
| 2784 | * Validate contact permission for |
| 2785 | * given operation on activity record. |
| 2786 | * |
| 2787 | * @param int $activityId activity record id. |
| 2788 | * @param string $operation user operation. |
| 2789 | * @param int $actTypeId activity type id. |
| 2790 | * @param int $contactId contact id/if not pass consider logged in |
| 2791 | * @param boolean $checkComponent do we need to check component enabled. |
| 2792 | * |
| 2793 | * @return boolean $allow true/false |
| 2794 | * @static |
| 2795 | */ |
| 2796 | static function checkPermission($activityId, $operation, $actTypeId = NULL, $contactId = NULL, $checkComponent = TRUE) { |
| 2797 | $allow = FALSE; |
| 2798 | if (!$actTypeId && $activityId) { |
| 2799 | $actTypeId = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $activityId, 'activity_type_id'); |
| 2800 | } |
| 2801 | |
| 2802 | if (!$activityId || !$operation || !$actTypeId) { |
| 2803 | return $allow; |
| 2804 | } |
| 2805 | |
| 2806 | //do check for civicase component enabled. |
| 2807 | if ($checkComponent) { |
| 2808 | static $componentEnabled; |
| 2809 | if (!isset($componentEnabled)) { |
| 2810 | $config = CRM_Core_Config::singleton(); |
| 2811 | $componentEnabled = FALSE; |
| 2812 | if (in_array('CiviCase', $config->enableComponents)) { |
| 2813 | $componentEnabled = TRUE; |
| 2814 | } |
| 2815 | } |
| 2816 | if (!$componentEnabled) { |
| 2817 | return $allow; |
| 2818 | } |
| 2819 | } |
| 2820 | |
| 2821 | //do check for cases. |
| 2822 | $caseActOperations = array( |
| 2823 | 'File On Case', |
| 2824 | 'Link Cases', |
| 2825 | 'Move To Case', |
| 2826 | 'Copy To Case', |
| 2827 | ); |
| 2828 | |
| 2829 | if (in_array($operation, $caseActOperations)) { |
| 2830 | static $unclosedCases; |
| 2831 | if (!is_array($unclosedCases)) { |
| 2832 | $unclosedCases = self::getUnclosedCases(); |
| 2833 | } |
| 2834 | if ($operation == 'File On Case') { |
| 2835 | $allow = (empty($unclosedCases)) ? FALSE : TRUE; |
| 2836 | } |
| 2837 | else { |
| 2838 | $allow = (count($unclosedCases) > 1) ? TRUE : FALSE; |
| 2839 | } |
| 2840 | } |
| 2841 | |
| 2842 | $actionOperations = array('view', 'edit', 'delete'); |
| 2843 | if (in_array($operation, $actionOperations)) { |
| 2844 | |
| 2845 | //do cache when user has non/supper permission. |
| 2846 | static $allowOperations; |
| 2847 | |
| 2848 | if (!is_array($allowOperations) || |
| 2849 | !array_key_exists($operation, $allowOperations) |
| 2850 | ) { |
| 2851 | |
| 2852 | if (!$contactId) { |
| 2853 | $session = CRM_Core_Session::singleton(); |
| 2854 | $contactId = $session->get('userID'); |
| 2855 | } |
| 2856 | |
| 2857 | //check for permissions. |
| 2858 | $permissions = array( |
| 2859 | 'view' => array( |
| 2860 | 'access my cases and activities', |
| 2861 | 'access all cases and activities', |
| 2862 | ), |
| 2863 | 'edit' => array( |
| 2864 | 'access my cases and activities', |
| 2865 | 'access all cases and activities', |
| 2866 | ), |
| 2867 | 'delete' => array('delete activities'), |
| 2868 | ); |
| 2869 | |
| 2870 | //check for core permission. |
| 2871 | $hasPermissions = array(); |
| 2872 | $checkPermissions = CRM_Utils_Array::value($operation, $permissions); |
| 2873 | if (is_array($checkPermissions)) { |
| 2874 | foreach ($checkPermissions as $per) { |
| 2875 | if (CRM_Core_Permission::check($per)) { |
| 2876 | $hasPermissions[$operation][] = $per; |
| 2877 | } |
| 2878 | } |
| 2879 | } |
| 2880 | |
| 2881 | //has permissions. |
| 2882 | if (!empty($hasPermissions)) { |
| 2883 | //need to check activity object specific. |
| 2884 | if (in_array($operation, array( |
| 2885 | 'view', |
| 2886 | 'edit' |
| 2887 | )) |
| 2888 | ) { |
| 2889 | //do we have supper permission. |
| 2890 | if (in_array('access all cases and activities', $hasPermissions[$operation])) { |
| 2891 | $allowOperations[$operation] = $allow = TRUE; |
| 2892 | } |
| 2893 | else { |
| 2894 | //user has only access to my cases and activity. |
| 2895 | //here object specific permmions come in picture. |
| 2896 | |
| 2897 | //edit - contact must be source or assignee |
| 2898 | //view - contact must be source/assignee/target |
| 2899 | $isTarget = $isAssignee = $isSource = FALSE; |
| 2900 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
| 2901 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
| 2902 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); |
| 2903 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
| 2904 | |
| 2905 | $target = new CRM_Activity_DAO_ActivityContact(); |
| 2906 | $target->record_type_id = $targetID; |
| 2907 | $target->activity_id = $activityId; |
| 2908 | $target->contact_id = $contactId; |
| 2909 | if ($target->find(TRUE)) { |
| 2910 | $isTarget = TRUE; |
| 2911 | } |
| 2912 | |
| 2913 | $assignee = new CRM_Activity_DAO_ActivityContact(); |
| 2914 | $assignee->activity_id = $activityId; |
| 2915 | $assignee->record_type_id = $assigneeID; |
| 2916 | $assignee->contact_id = $contactId; |
| 2917 | if ($assignee->find(TRUE)) { |
| 2918 | $isAssignee = TRUE; |
| 2919 | } |
| 2920 | |
| 2921 | $source = new CRM_Activity_DAO_ActivityContact(); |
| 2922 | $source->activity_id = $activityId; |
| 2923 | $source->record_type_id = $sourceID; |
| 2924 | $source->contact_id = $contactId; |
| 2925 | if ($source->find(TRUE)) { |
| 2926 | $isSource = TRUE; |
| 2927 | } |
| 2928 | |
| 2929 | if ($operation == 'edit') { |
| 2930 | if ($isAssignee || $isSource) { |
| 2931 | $allow = TRUE; |
| 2932 | } |
| 2933 | } |
| 2934 | if ($operation == 'view') { |
| 2935 | if ($isTarget || $isAssignee || $isSource) { |
| 2936 | $allow = TRUE; |
| 2937 | } |
| 2938 | } |
| 2939 | } |
| 2940 | } |
| 2941 | elseif (is_array($hasPermissions[$operation])) { |
| 2942 | $allowOperations[$operation] = $allow = TRUE; |
| 2943 | } |
| 2944 | } |
| 2945 | else { |
| 2946 | //contact do not have permission. |
| 2947 | $allowOperations[$operation] = FALSE; |
| 2948 | } |
| 2949 | } |
| 2950 | else { |
| 2951 | //use cache. |
| 2952 | //here contact might have supper/non permission. |
| 2953 | $allow = $allowOperations[$operation]; |
| 2954 | } |
| 2955 | } |
| 2956 | |
| 2957 | //do further only when operation is granted. |
| 2958 | if ($allow) { |
| 2959 | $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'name'); |
| 2960 | |
| 2961 | //get the activity type name. |
| 2962 | $actTypeName = CRM_Utils_Array::value($actTypeId, $activityTypes); |
| 2963 | |
| 2964 | //do not allow multiple copy / edit action. |
| 2965 | $singletonNames = array( |
| 2966 | 'Open Case', |
| 2967 | 'Reassigned Case', |
| 2968 | 'Merge Case', |
| 2969 | 'Link Cases', |
| 2970 | 'Assign Case Role', |
| 2971 | 'Email', |
| 2972 | 'Inbound Email' |
| 2973 | ); |
| 2974 | |
| 2975 | //do not allow to delete these activities, CRM-4543 |
| 2976 | $doNotDeleteNames = array('Open Case', 'Change Case Type', 'Change Case Status', 'Change Case Start Date'); |
| 2977 | |
| 2978 | //allow edit operation. |
| 2979 | $allowEditNames = array('Open Case'); |
| 2980 | |
| 2981 | // do not allow File on Case |
| 2982 | $doNotFileNames = array( |
| 2983 | 'Open Case', |
| 2984 | 'Change Case Type', |
| 2985 | 'Change Case Status', |
| 2986 | 'Change Case Start Date', |
| 2987 | 'Reassigned Case', |
| 2988 | 'Merge Case', |
| 2989 | 'Link Cases', |
| 2990 | 'Assign Case Role' |
| 2991 | ); |
| 2992 | |
| 2993 | if (in_array($actTypeName, $singletonNames)) { |
| 2994 | $allow = FALSE; |
| 2995 | if ($operation == 'File On Case') { |
| 2996 | $allow = (in_array($actTypeName, $doNotFileNames)) ? FALSE : TRUE; |
| 2997 | } |
| 2998 | if (in_array($operation, $actionOperations)) { |
| 2999 | $allow = TRUE; |
| 3000 | if ($operation == 'edit') { |
| 3001 | $allow = (in_array($actTypeName, $allowEditNames)) ? TRUE : FALSE; |
| 3002 | } |
| 3003 | elseif ($operation == 'delete') { |
| 3004 | $allow = (in_array($actTypeName, $doNotDeleteNames)) ? FALSE : TRUE; |
| 3005 | } |
| 3006 | } |
| 3007 | } |
| 3008 | if ($allow && ($operation == 'delete') && |
| 3009 | in_array($actTypeName, $doNotDeleteNames) |
| 3010 | ) { |
| 3011 | $allow = FALSE; |
| 3012 | } |
| 3013 | |
| 3014 | if ($allow && ($operation == 'File On Case') && |
| 3015 | in_array($actTypeName, $doNotFileNames) |
| 3016 | ) { |
| 3017 | $allow = FALSE; |
| 3018 | } |
| 3019 | |
| 3020 | //check settings file for masking actions |
| 3021 | //on the basis the activity types |
| 3022 | //hide Edit link if activity type is NOT editable |
| 3023 | //(special case activities).CRM-5871 |
| 3024 | if ($allow && in_array($operation, $actionOperations)) { |
| 3025 | static $actionFilter = array(); |
| 3026 | if (!array_key_exists($operation, $actionFilter)) { |
| 3027 | $xmlProcessor = new CRM_Case_XMLProcessor_Process(); |
| 3028 | $actionFilter[$operation] = $xmlProcessor->get('Settings', 'ActivityTypes', FALSE, $operation); |
| 3029 | } |
| 3030 | if (array_key_exists($operation, $actionFilter[$operation]) && |
| 3031 | in_array($actTypeId, $actionFilter[$operation][$operation]) |
| 3032 | ) { |
| 3033 | $allow = FALSE; |
| 3034 | } |
| 3035 | } |
| 3036 | } |
| 3037 | |
| 3038 | return $allow; |
| 3039 | } |
| 3040 | |
| 3041 | /** |
| 3042 | * since we drop 'access CiviCase', allow access |
| 3043 | * if user has 'access my cases and activities' |
| 3044 | * or 'access all cases and activities' |
| 3045 | */ |
| 3046 | static function accessCiviCase() { |
| 3047 | static $componentEnabled; |
| 3048 | if (!isset($componentEnabled)) { |
| 3049 | $componentEnabled = FALSE; |
| 3050 | $config = CRM_Core_Config::singleton(); |
| 3051 | if (in_array('CiviCase', $config->enableComponents)) { |
| 3052 | $componentEnabled = TRUE; |
| 3053 | } |
| 3054 | } |
| 3055 | if (!$componentEnabled) { |
| 3056 | return FALSE; |
| 3057 | } |
| 3058 | |
| 3059 | if (CRM_Core_Permission::check('access my cases and activities') || |
| 3060 | CRM_Core_Permission::check('access all cases and activities') |
| 3061 | ) { |
| 3062 | return TRUE; |
| 3063 | } |
| 3064 | |
| 3065 | return FALSE; |
| 3066 | } |
| 3067 | |
| 3068 | /** |
| 3069 | * Function to check whether activity is a case Activity |
| 3070 | * |
| 3071 | * @param int $activityID activity id |
| 3072 | * |
| 3073 | * @return boolean $isCaseActivity true/false |
| 3074 | */ |
| 3075 | static function isCaseActivity($activityID) { |
| 3076 | $isCaseActivity = FALSE; |
| 3077 | if ($activityID) { |
| 3078 | $params = array(1 => array($activityID, 'Integer')); |
| 3079 | $query = "SELECT id FROM civicrm_case_activity WHERE activity_id = %1"; |
| 3080 | if (CRM_Core_DAO::singleValueQuery($query, $params)) { |
| 3081 | $isCaseActivity = TRUE; |
| 3082 | } |
| 3083 | } |
| 3084 | |
| 3085 | return $isCaseActivity; |
| 3086 | } |
| 3087 | |
| 3088 | /** |
| 3089 | * Function to get all the case type ids currently in use |
| 3090 | * |
| 3091 | * |
| 3092 | * @return array $caseTypeIds |
| 3093 | */ |
| 3094 | static function getUsedCaseType() { |
| 3095 | static $caseTypeIds; |
| 3096 | |
| 3097 | if (!is_array($caseTypeIds)) { |
| 3098 | $query = "SELECT DISTINCT( civicrm_case.case_type_id ) FROM civicrm_case"; |
| 3099 | |
| 3100 | $dao = CRM_Core_DAO::executeQuery($query); |
| 3101 | $caseTypeIds = array(); |
| 3102 | while ($dao->fetch()) { |
| 3103 | $typeId = explode(CRM_Core_DAO::VALUE_SEPARATOR, |
| 3104 | $dao->case_type_id |
| 3105 | ); |
| 3106 | $caseTypeIds[] = $typeId[1]; |
| 3107 | } |
| 3108 | } |
| 3109 | |
| 3110 | return $caseTypeIds; |
| 3111 | } |
| 3112 | |
| 3113 | /** |
| 3114 | * Function to get all the case status ids currently in use |
| 3115 | * |
| 3116 | * |
| 3117 | * @return array $caseStatusIds |
| 3118 | */ |
| 3119 | static function getUsedCaseStatuses() { |
| 3120 | static $caseStatusIds; |
| 3121 | |
| 3122 | if (!is_array($caseStatusIds)) { |
| 3123 | $query = "SELECT DISTINCT( civicrm_case.status_id ) FROM civicrm_case"; |
| 3124 | |
| 3125 | $dao = CRM_Core_DAO::executeQuery($query); |
| 3126 | $caseStatusIds = array(); |
| 3127 | while ($dao->fetch()) { |
| 3128 | $caseStatusIds[] = $dao->status_id; |
| 3129 | } |
| 3130 | } |
| 3131 | |
| 3132 | return $caseStatusIds; |
| 3133 | } |
| 3134 | |
| 3135 | /** |
| 3136 | * Function to get all the encounter medium ids currently in use |
| 3137 | * @return array |
| 3138 | */ |
| 3139 | static function getUsedEncounterMediums() { |
| 3140 | static $mediumIds; |
| 3141 | |
| 3142 | if (!is_array($mediumIds)) { |
| 3143 | $query = "SELECT DISTINCT( civicrm_activity.medium_id ) FROM civicrm_activity"; |
| 3144 | |
| 3145 | $dao = CRM_Core_DAO::executeQuery($query); |
| 3146 | $mediumIds = array(); |
| 3147 | while ($dao->fetch()) { |
| 3148 | $mediumIds[] = $dao->medium_id; |
| 3149 | } |
| 3150 | } |
| 3151 | |
| 3152 | return $mediumIds; |
| 3153 | } |
| 3154 | |
| 3155 | /** |
| 3156 | * Function to check case configuration. |
| 3157 | * |
| 3158 | * @param null $contactId |
| 3159 | * |
| 3160 | * @return array $configured |
| 3161 | */ |
| 3162 | static function isCaseConfigured($contactId = NULL) { |
| 3163 | $configured = array_fill_keys(array('configured', 'allowToAddNewCase', 'redirectToCaseAdmin'), FALSE); |
| 3164 | |
| 3165 | //lets check for case configured. |
| 3166 | $allCasesCount = CRM_Case_BAO_Case::caseCount(NULL, FALSE); |
| 3167 | $configured['configured'] = ($allCasesCount) ? TRUE : FALSE; |
| 3168 | if (!$configured['configured']) { |
| 3169 | //do check for case type and case status. |
| 3170 | $caseTypes = CRM_Case_PseudoConstant::caseType('title', FALSE); |
| 3171 | if (!empty($caseTypes)) { |
| 3172 | $configured['configured'] = TRUE; |
| 3173 | if (!$configured['configured']) { |
| 3174 | $caseStatuses = CRM_Case_PseudoConstant::caseStatus('label', FALSE); |
| 3175 | if (!empty($caseStatuses)) { |
| 3176 | $configured['configured'] = TRUE; |
| 3177 | } |
| 3178 | } |
| 3179 | } |
| 3180 | } |
| 3181 | if ($configured['configured']) { |
| 3182 | //do check for active case type and case status. |
| 3183 | $caseTypes = CRM_Case_PseudoConstant::caseType(); |
| 3184 | if (!empty($caseTypes)) { |
| 3185 | $caseStatuses = CRM_Case_PseudoConstant::caseStatus(); |
| 3186 | if (!empty($caseStatuses)) { |
| 3187 | $configured['allowToAddNewCase'] = TRUE; |
| 3188 | } |
| 3189 | } |
| 3190 | |
| 3191 | //do we need to redirect user to case admin. |
| 3192 | if (!$configured['allowToAddNewCase'] && $contactId) { |
| 3193 | //check for current contact case count. |
| 3194 | $currentContatCasesCount = CRM_Case_BAO_Case::caseCount($contactId); |
| 3195 | //redirect user to case admin page. |
| 3196 | if (!$currentContatCasesCount) { |
| 3197 | $configured['redirectToCaseAdmin'] = TRUE; |
| 3198 | } |
| 3199 | } |
| 3200 | } |
| 3201 | |
| 3202 | return $configured; |
| 3203 | } |
| 3204 | |
| 3205 | /** |
| 3206 | * Used during case component enablement and during ugprade |
| 3207 | */ |
| 3208 | static function createCaseViews() { |
| 3209 | $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); |
| 3210 | $dao = new CRM_Core_DAO(); |
| 3211 | |
| 3212 | $sql = self::createCaseViewsQuery('upcoming'); |
| 3213 | $dao->query($sql); |
| 3214 | if (PEAR::getStaticProperty('DB_DataObject', 'lastError')) { |
| 3215 | return FALSE; |
| 3216 | } |
| 3217 | |
| 3218 | // Above error doesn't get caught? |
| 3219 | $doublecheck = $dao->singleValueQuery("SELECT count(id) FROM civicrm_view_case_activity_upcoming"); |
| 3220 | if (is_null($doublecheck)) { |
| 3221 | return FALSE; |
| 3222 | } |
| 3223 | |
| 3224 | $sql = self::createCaseViewsQuery('recent'); |
| 3225 | $dao->query($sql); |
| 3226 | if (PEAR::getStaticProperty('DB_DataObject', 'lastError')) { |
| 3227 | return FALSE; |
| 3228 | } |
| 3229 | |
| 3230 | // Above error doesn't get caught? |
| 3231 | $doublecheck = $dao->singleValueQuery("SELECT count(id) FROM civicrm_view_case_activity_recent"); |
| 3232 | if (is_null($doublecheck)) { |
| 3233 | return FALSE; |
| 3234 | } |
| 3235 | |
| 3236 | return TRUE; |
| 3237 | } |
| 3238 | |
| 3239 | /** |
| 3240 | * helper function, also used by the upgrade in case of error |
| 3241 | */ |
| 3242 | static function createCaseViewsQuery($section = 'upcoming') { |
| 3243 | $sql = ""; |
| 3244 | $scheduled_id = CRM_Core_OptionGroup::getValue('activity_status', 'Scheduled', 'name'); |
| 3245 | switch ($section) { |
| 3246 | case 'upcoming': |
| 3247 | $sql = "CREATE OR REPLACE VIEW `civicrm_view_case_activity_upcoming` |
| 3248 | AS SELECT ca.case_id, a.id, a.activity_date_time, a.status_id, a.activity_type_id |
| 3249 | FROM civicrm_case_activity ca |
| 3250 | INNER JOIN civicrm_activity a ON ca.activity_id=a.id |
| 3251 | WHERE a.activity_date_time <= DATE_ADD( NOW(), INTERVAL 14 DAY ) |
| 3252 | AND a.is_current_revision = 1 AND a.is_deleted=0 AND a.status_id = $scheduled_id"; |
| 3253 | break; |
| 3254 | |
| 3255 | case 'recent': |
| 3256 | $sql = "CREATE OR REPLACE VIEW `civicrm_view_case_activity_recent` |
| 3257 | AS SELECT ca.case_id, a.id, a.activity_date_time, a.status_id, a.activity_type_id |
| 3258 | FROM civicrm_case_activity ca |
| 3259 | INNER JOIN civicrm_activity a ON ca.activity_id=a.id |
| 3260 | WHERE a.activity_date_time <= NOW() |
| 3261 | AND a.activity_date_time >= DATE_SUB( NOW(), INTERVAL 14 DAY ) |
| 3262 | AND a.is_current_revision = 1 AND a.is_deleted=0 AND a.status_id <> $scheduled_id"; |
| 3263 | break; |
| 3264 | } |
| 3265 | return $sql; |
| 3266 | } |
| 3267 | |
| 3268 | /** |
| 3269 | * Function to add/copy relationships, when new client is added for a case |
| 3270 | * |
| 3271 | * @param int $caseId case id |
| 3272 | * @param int $contactId contact id / new client id |
| 3273 | * |
| 3274 | * @return void |
| 3275 | */ |
| 3276 | static function addCaseRelationships($caseId, $contactId) { |
| 3277 | // get the case role / relationships for the case |
| 3278 | $caseRelationships = new CRM_Contact_DAO_Relationship(); |
| 3279 | $caseRelationships->case_id = $caseId; |
| 3280 | $caseRelationships->find(); |
| 3281 | $relationshipTypes = array(); |
| 3282 | |
| 3283 | // make sure we don't add duplicate relationships of same relationship type. |
| 3284 | while ($caseRelationships->fetch() && !in_array($caseRelationships->relationship_type_id, $relationshipTypes)) { |
| 3285 | $values = array(); |
| 3286 | CRM_Core_DAO::storeValues($caseRelationships, $values); |
| 3287 | |
| 3288 | // add relationship for new client. |
| 3289 | $newRelationship = new CRM_Contact_DAO_Relationship(); |
| 3290 | $newRelationship->copyValues($values); |
| 3291 | $newRelationship->id = NULL; |
| 3292 | $newRelationship->case_id = $caseId; |
| 3293 | $newRelationship->contact_id_a = $contactId; |
| 3294 | $newRelationship->end_date = CRM_Utils_Date::isoToMysql($caseRelationships->end_date); |
| 3295 | $newRelationship->start_date = CRM_Utils_Date::isoToMysql($caseRelationships->start_date); |
| 3296 | |
| 3297 | // another check to avoid duplicate relationship, in cases where client is removed and re-added again. |
| 3298 | if (!$newRelationship->find(TRUE)) { |
| 3299 | $newRelationship->save(); |
| 3300 | } |
| 3301 | $newRelationship->free(); |
| 3302 | |
| 3303 | // store relationship type of newly created relationship |
| 3304 | $relationshipTypes[] = $caseRelationships->relationship_type_id; |
| 3305 | } |
| 3306 | } |
| 3307 | |
| 3308 | /** |
| 3309 | * Function to get the list of clients for a case |
| 3310 | * |
| 3311 | * @param int $caseId |
| 3312 | * |
| 3313 | * @return array $clients associated array with client ids |
| 3314 | * @static |
| 3315 | */ |
| 3316 | static function getCaseClients($caseId) { |
| 3317 | $clients = array(); |
| 3318 | $caseContact = new CRM_Case_DAO_CaseContact(); |
| 3319 | $caseContact->case_id = $caseId; |
| 3320 | $caseContact->find(); |
| 3321 | |
| 3322 | while ($caseContact->fetch()) { |
| 3323 | $clients[] = $caseContact->contact_id; |
| 3324 | } |
| 3325 | |
| 3326 | return $clients; |
| 3327 | } |
| 3328 | |
| 3329 | /** |
| 3330 | * Get options for a given case field. |
| 3331 | * @see CRM_Core_DAO::buildOptions |
| 3332 | * |
| 3333 | * @param String $fieldName |
| 3334 | * @param String $context : @see CRM_Core_DAO::buildOptionsContext |
| 3335 | * @param Array $props : whatever is known about this dao object |
| 3336 | * |
| 3337 | * @return Array|bool |
| 3338 | */ |
| 3339 | public static function buildOptions($fieldName, $context = NULL, $props = array()) { |
| 3340 | $className = __CLASS__; |
| 3341 | $params = array(); |
| 3342 | switch ($fieldName) { |
| 3343 | // This field is not part of this object but the api supports it |
| 3344 | case 'medium_id': |
| 3345 | $className = 'CRM_Activity_BAO_Activity'; |
| 3346 | break; |
| 3347 | } |
| 3348 | return CRM_Core_PseudoConstant::get($className, $fieldName, $params, $context); |
| 3349 | } |
| 3350 | } |
| 3351 | |