| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
| 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-2019 |
| 32 | */ |
| 33 | class CRM_Case_XMLProcessor_Report extends CRM_Case_XMLProcessor { |
| 34 | |
| 35 | /** |
| 36 | * The default variable defined. |
| 37 | * |
| 38 | * @var bool |
| 39 | */ |
| 40 | protected $_isRedact; |
| 41 | |
| 42 | /** |
| 43 | */ |
| 44 | public function __construct() { |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @param int $clientID |
| 49 | * @param int $caseID |
| 50 | * @param string $activitySetName |
| 51 | * @param array $params |
| 52 | * |
| 53 | * @return mixed |
| 54 | */ |
| 55 | public function run($clientID, $caseID, $activitySetName, $params) { |
| 56 | $contents = self::getCaseReport($clientID, |
| 57 | $caseID, |
| 58 | $activitySetName, |
| 59 | $params, |
| 60 | $this |
| 61 | ); |
| 62 | |
| 63 | return CRM_Case_Audit_Audit::run($contents, $clientID, $caseID); |
| 64 | } |
| 65 | |
| 66 | public function getRedactionRules() { |
| 67 | foreach (array( |
| 68 | 'redactionStringRules', |
| 69 | 'redactionRegexRules', |
| 70 | ) as $key => $rule) { |
| 71 | $$rule = CRM_Case_PseudoConstant::redactionRule($key); |
| 72 | |
| 73 | if (!empty($$rule)) { |
| 74 | foreach ($$rule as & $val) { |
| 75 | //suffixed with a randomly generated 4-digit number |
| 76 | if ($key == 'redactionStringRules') { |
| 77 | $val .= rand(10000, 100000); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if (!empty($this->{'_' . $rule})) { |
| 82 | $this->{'_' . $rule} = CRM_Utils_Array::crmArrayMerge($this->{'_' . $rule}, $$rule); |
| 83 | } |
| 84 | else { |
| 85 | $this->{'_' . $rule} = $$rule; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @param int $clientID |
| 93 | * @param int $caseID |
| 94 | * |
| 95 | * @return array |
| 96 | */ |
| 97 | public function &caseInfo( |
| 98 | $clientID, |
| 99 | $caseID |
| 100 | ) { |
| 101 | $case = $this->_redactionRegexRules = array(); |
| 102 | |
| 103 | if (empty($this->_redactionStringRules)) { |
| 104 | $this->_redactionStringRules = array(); |
| 105 | } |
| 106 | |
| 107 | if ($this->_isRedact == 1) { |
| 108 | $this->getRedactionRules(); |
| 109 | } |
| 110 | |
| 111 | $client = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $clientID, 'display_name'); |
| 112 | |
| 113 | // add Client to the strings to be redacted across the case session |
| 114 | if (!array_key_exists($client, $this->_redactionStringRules)) { |
| 115 | $this->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($this->_redactionStringRules, |
| 116 | array($client => 'name_' . rand(10000, 100000)) |
| 117 | ); |
| 118 | $clientSortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $clientID, 'sort_name'); |
| 119 | if (!array_key_exists($clientSortName, $this->_redactionStringRules)) { |
| 120 | $this->_redactionStringRules[$clientSortName] = $this->_redactionStringRules[$client]; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | $case['clientName'] = $this->redact($client); |
| 125 | |
| 126 | $dao = new CRM_Case_DAO_Case(); |
| 127 | $dao->id = $caseID; |
| 128 | if ($dao->find(TRUE)) { |
| 129 | $case['subject'] = $dao->subject; |
| 130 | $case['start_date'] = $dao->start_date; |
| 131 | $case['end_date'] = $dao->end_date; |
| 132 | $case['caseType'] = CRM_Case_BAO_Case::getCaseType($caseID); |
| 133 | $case['caseTypeName'] = CRM_Case_BAO_Case::getCaseType($caseID, 'name'); |
| 134 | $case['status'] = CRM_Core_PseudoConstant::getLabel('CRM_Case_BAO_Case', 'status_id', $dao->status_id); |
| 135 | } |
| 136 | return $case; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @param $xml |
| 141 | * @param string $activitySetName |
| 142 | * |
| 143 | * @return array|bool |
| 144 | */ |
| 145 | public function getActivityTypes($xml, $activitySetName) { |
| 146 | foreach ($xml->ActivitySets as $activitySetsXML) { |
| 147 | foreach ($activitySetsXML->ActivitySet as $activitySetXML) { |
| 148 | if ((string ) $activitySetXML->name == $activitySetName) { |
| 149 | $activityTypes = array(); |
| 150 | $allActivityTypes = &$this->allActivityTypes(); |
| 151 | foreach ($activitySetXML->ActivityTypes as $activityTypesXML) { |
| 152 | foreach ($activityTypesXML as $activityTypeXML) { |
| 153 | $activityTypeName = (string ) $activityTypeXML->name; |
| 154 | $activityTypeInfo = CRM_Utils_Array::value($activityTypeName, $allActivityTypes); |
| 155 | if ($activityTypeInfo) { |
| 156 | $activityTypes[$activityTypeInfo['id']] = $activityTypeInfo; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | return empty($activityTypes) ? FALSE : $activityTypes; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | return FALSE; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * @param $xml |
| 169 | * @param string $activitySetName |
| 170 | * |
| 171 | * @return null|string |
| 172 | */ |
| 173 | public function getActivitySetLabel($xml, $activitySetName) { |
| 174 | foreach ($xml->ActivitySets as $activitySetsXML) { |
| 175 | foreach ($activitySetsXML->ActivitySet as $activitySetXML) { |
| 176 | if ((string ) $activitySetXML->name == $activitySetName) { |
| 177 | return (string ) $activitySetXML->label; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | return NULL; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * @param int $clientID |
| 186 | * @param int $caseID |
| 187 | * @param $activityTypes |
| 188 | * @param $activities |
| 189 | */ |
| 190 | public function getActivities($clientID, $caseID, $activityTypes, &$activities) { |
| 191 | // get all activities for this case that in this activityTypes set |
| 192 | foreach ($activityTypes as $aType) { |
| 193 | $map[$aType['id']] = $aType; |
| 194 | } |
| 195 | |
| 196 | // get all core activities |
| 197 | $coreActivityTypes = CRM_Case_PseudoConstant::caseActivityType(FALSE, TRUE); |
| 198 | |
| 199 | foreach ($coreActivityTypes as $aType) { |
| 200 | $map[$aType['id']] = $aType; |
| 201 | } |
| 202 | |
| 203 | $activityTypeIDs = implode(',', array_keys($map)); |
| 204 | $query = " |
| 205 | SELECT a.*, c.id as caseID |
| 206 | FROM civicrm_activity a, |
| 207 | civicrm_case c, |
| 208 | civicrm_case_activity ac |
| 209 | WHERE a.is_current_revision = 1 |
| 210 | AND a.is_deleted =0 |
| 211 | AND a.activity_type_id IN ( $activityTypeIDs ) |
| 212 | AND c.id = ac.case_id |
| 213 | AND a.id = ac.activity_id |
| 214 | AND ac.case_id = %1 |
| 215 | "; |
| 216 | |
| 217 | $params = array(1 => array($caseID, 'Integer')); |
| 218 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 219 | while ($dao->fetch()) { |
| 220 | $activityTypeInfo = $map[$dao->activity_type_id]; |
| 221 | $activities[] = $this->getActivity($clientID, |
| 222 | $dao, |
| 223 | $activityTypeInfo |
| 224 | ); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * @param int $clientID |
| 230 | * @param int $activityID |
| 231 | * @param bool $anyActivity |
| 232 | * @param int $redact |
| 233 | * |
| 234 | * @return mixed |
| 235 | */ |
| 236 | public function &getActivityInfo($clientID, $activityID, $anyActivity = FALSE, $redact = 0) { |
| 237 | static $activityInfos = array(); |
| 238 | if ($redact) { |
| 239 | $this->_isRedact = 1; |
| 240 | $this->getRedactionRules(); |
| 241 | } |
| 242 | |
| 243 | $index = $activityID . '_' . (int) $anyActivity; |
| 244 | |
| 245 | if ($clientID) { |
| 246 | $index = $index . '_' . $clientID; |
| 247 | } |
| 248 | |
| 249 | if (!array_key_exists($index, $activityInfos)) { |
| 250 | $activityInfos[$index] = array(); |
| 251 | $selectCaseActivity = ""; |
| 252 | $joinCaseActivity = ""; |
| 253 | |
| 254 | if ($clientID) { |
| 255 | $selectCaseActivity = ", ca.case_id as caseID "; |
| 256 | $joinCaseActivity = " INNER JOIN civicrm_case_activity ca ON a.id = ca.activity_id "; |
| 257 | } |
| 258 | |
| 259 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
| 260 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
| 261 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); |
| 262 | |
| 263 | $query = " |
| 264 | SELECT a.*, aa.contact_id as assigneeID, at.contact_id as targetID |
| 265 | {$selectCaseActivity} |
| 266 | FROM civicrm_activity a |
| 267 | {$joinCaseActivity} |
| 268 | LEFT JOIN civicrm_activity_contact at ON a.id = at.activity_id AND at.record_type_id = $targetID |
| 269 | LEFT JOIN civicrm_activity_contact aa ON a.id = aa.activity_id AND aa.record_type_id = $assigneeID |
| 270 | WHERE a.id = %1 |
| 271 | "; |
| 272 | $params = array(1 => array($activityID, 'Integer')); |
| 273 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 274 | |
| 275 | if ($dao->fetch()) { |
| 276 | //if activity type is email get info of all activities. |
| 277 | if ($dao->activity_type_id == CRM_Core_PseudoConstant::getKey('CRM_Activity_DAO_Activity', 'activity_type_id', 'Email')) { |
| 278 | $anyActivity = TRUE; |
| 279 | } |
| 280 | $activityTypes = CRM_Case_PseudoConstant::caseActivityType(FALSE, $anyActivity); |
| 281 | $activityTypeInfo = NULL; |
| 282 | |
| 283 | if (isset($activityTypes[$dao->activity_type_id])) { |
| 284 | $activityTypeInfo = $activityTypes[$dao->activity_type_id]; |
| 285 | } |
| 286 | if ($activityTypeInfo) { |
| 287 | $activityInfos[$index] = $this->getActivity($clientID, $dao, $activityTypeInfo); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | return $activityInfos[$index]; |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * @param int $clientID |
| 297 | * @param $activityDAO |
| 298 | * @param $activityTypeInfo |
| 299 | * |
| 300 | * @return array |
| 301 | */ |
| 302 | public function &getActivity($clientID, $activityDAO, &$activityTypeInfo) { |
| 303 | if (empty($this->_redactionStringRules)) { |
| 304 | $this->_redactionStringRules = array(); |
| 305 | } |
| 306 | |
| 307 | $activity = array(); |
| 308 | $activity['fields'] = array(); |
| 309 | if ($clientID) { |
| 310 | $clientID = CRM_Utils_Type::escape($clientID, 'Integer'); |
| 311 | if (!in_array($activityTypeInfo['name'], array( |
| 312 | 'Email', |
| 313 | 'Inbound Email', |
| 314 | )) |
| 315 | ) { |
| 316 | $activity['editURL'] = CRM_Utils_System::url('civicrm/case/activity', |
| 317 | "reset=1&cid={$clientID}&caseid={$activityDAO->caseID}&action=update&atype={$activityDAO->activity_type_id}&id={$activityDAO->id}" |
| 318 | ); |
| 319 | } |
| 320 | else { |
| 321 | $activity['editURL'] = ''; |
| 322 | } |
| 323 | |
| 324 | $client = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $clientID, 'display_name'); |
| 325 | // add Client SortName as well as Display to the strings to be redacted across the case session |
| 326 | // suffixed with a randomly generated 4-digit number |
| 327 | if (!array_key_exists($client, $this->_redactionStringRules)) { |
| 328 | $this->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($this->_redactionStringRules, |
| 329 | array($client => 'name_' . rand(10000, 100000)) |
| 330 | ); |
| 331 | |
| 332 | $clientSortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $clientID, 'sort_name'); |
| 333 | if (!array_key_exists($clientSortName, $this->_redactionStringRules)) { |
| 334 | $this->_redactionStringRules[$clientSortName] = $this->_redactionStringRules[$client]; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | $activity['fields'][] = array( |
| 339 | 'label' => 'Client', |
| 340 | 'value' => $this->redact($client), |
| 341 | 'type' => 'String', |
| 342 | ); |
| 343 | } |
| 344 | |
| 345 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
| 346 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); |
| 347 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
| 348 | if (!empty($activityDAO->targetID)) { |
| 349 | // Re-lookup the target ID since the DAO only has the first recipient if there are multiple. |
| 350 | // Maybe not the best solution. |
| 351 | $targetNames = CRM_Activity_BAO_ActivityContact::getNames($activityDAO->id, $targetID); |
| 352 | $processTarget = FALSE; |
| 353 | $label = ts('With Contact(s)'); |
| 354 | if (in_array($activityTypeInfo['name'], array('Email', 'Inbound Email'))) { |
| 355 | $processTarget = TRUE; |
| 356 | $label = ts('Recipient'); |
| 357 | } |
| 358 | if (!$processTarget) { |
| 359 | foreach ($targetNames as $targetID => $targetName) { |
| 360 | if ($targetID != $clientID) { |
| 361 | $processTarget = TRUE; |
| 362 | break; |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | if ($processTarget) { |
| 368 | $targetRedacted = array(); |
| 369 | foreach ($targetNames as $targetID => $target) { |
| 370 | // add Recipient SortName as well as Display to the strings to be redacted across the case session |
| 371 | // suffixed with a randomly generated 4-digit number |
| 372 | if (!array_key_exists($target, $this->_redactionStringRules)) { |
| 373 | $this->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($this->_redactionStringRules, |
| 374 | array($target => 'name_' . rand(10000, 100000)) |
| 375 | ); |
| 376 | $targetSortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $targetID, 'sort_name'); |
| 377 | if (!array_key_exists($targetSortName, $this->_redactionStringRules)) { |
| 378 | $this->_redactionStringRules[$targetSortName] = $this->_redactionStringRules[$target]; |
| 379 | } |
| 380 | } |
| 381 | $targetRedacted[] = $this->redact($target); |
| 382 | } |
| 383 | |
| 384 | $activity['fields'][] = array( |
| 385 | 'label' => $label, |
| 386 | 'value' => implode('; ', $targetRedacted), |
| 387 | 'type' => 'String', |
| 388 | ); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | // Activity Type info is a special field |
| 393 | $activity['fields'][] = array( |
| 394 | 'label' => ts('Activity Type'), |
| 395 | 'value' => $activityTypeInfo['label'], |
| 396 | 'type' => 'String', |
| 397 | ); |
| 398 | |
| 399 | $activity['fields'][] = array( |
| 400 | 'label' => ts('Subject'), |
| 401 | 'value' => htmlspecialchars($this->redact($activityDAO->subject)), |
| 402 | 'type' => 'Memo', |
| 403 | ); |
| 404 | |
| 405 | $creator = $this->getCreatedBy($activityDAO->id); |
| 406 | // add Creator to the strings to be redacted across the case session |
| 407 | if (!array_key_exists($creator, $this->_redactionStringRules)) { |
| 408 | $this->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($this->_redactionStringRules, |
| 409 | array($creator => 'name_' . rand(10000, 100000)) |
| 410 | ); |
| 411 | } |
| 412 | $activity['fields'][] = array( |
| 413 | 'label' => ts('Created By'), |
| 414 | 'value' => $this->redact($creator), |
| 415 | 'type' => 'String', |
| 416 | ); |
| 417 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
| 418 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
| 419 | $source_contact_id = CRM_Activity_BAO_Activity::getActivityContact($activityDAO->id, $sourceID); |
| 420 | $reporter = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', |
| 421 | $source_contact_id, |
| 422 | 'display_name' |
| 423 | ); |
| 424 | |
| 425 | // add Reporter SortName as well as Display to the strings to be redacted across the case session |
| 426 | // suffixed with a randomly generated 4-digit number |
| 427 | if (!array_key_exists($reporter, $this->_redactionStringRules)) { |
| 428 | $this->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($this->_redactionStringRules, |
| 429 | array($reporter => 'name_' . rand(10000, 100000)) |
| 430 | ); |
| 431 | |
| 432 | $reporterSortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', |
| 433 | $source_contact_id, |
| 434 | 'sort_name' |
| 435 | ); |
| 436 | if (!array_key_exists($reporterSortName, $this->_redactionStringRules)) { |
| 437 | $this->_redactionStringRules[$reporterSortName] = $this->_redactionStringRules[$reporter]; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | $activity['fields'][] = array( |
| 442 | 'label' => ts('Reported By'), |
| 443 | 'value' => $this->redact($reporter), |
| 444 | 'type' => 'String', |
| 445 | ); |
| 446 | |
| 447 | if (!empty($activityDAO->assigneeID)) { |
| 448 | //allow multiple assignee contacts.CRM-4503. |
| 449 | $assignee_contact_names = CRM_Activity_BAO_ActivityAssignment::getAssigneeNames(array($activityDAO->id), TRUE); |
| 450 | |
| 451 | foreach ($assignee_contact_names as & $assignee) { |
| 452 | // add Assignee to the strings to be redacted across the case session |
| 453 | $this->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($this->_redactionStringRules, |
| 454 | array($assignee => 'name_' . rand(10000, 100000)) |
| 455 | ); |
| 456 | $assignee = $this->redact($assignee); |
| 457 | } |
| 458 | $assigneeContacts = implode(', ', $assignee_contact_names); |
| 459 | $activity['fields'][] = array( |
| 460 | 'label' => ts('Assigned to'), |
| 461 | 'value' => $assigneeContacts, |
| 462 | 'type' => 'String', |
| 463 | ); |
| 464 | } |
| 465 | |
| 466 | if ($activityDAO->medium_id) { |
| 467 | $activity['fields'][] = array( |
| 468 | 'label' => ts('Medium'), |
| 469 | 'value' => CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'medium_id', $activityDAO->medium_id), |
| 470 | 'type' => 'String', |
| 471 | ); |
| 472 | } |
| 473 | |
| 474 | $activity['fields'][] = array( |
| 475 | 'label' => ts('Location'), |
| 476 | 'value' => $activityDAO->location, |
| 477 | 'type' => 'String', |
| 478 | ); |
| 479 | |
| 480 | $activity['fields'][] = array( |
| 481 | 'label' => ts('Date and Time'), |
| 482 | 'value' => $activityDAO->activity_date_time, |
| 483 | 'type' => 'Date', |
| 484 | ); |
| 485 | |
| 486 | $activity['fields'][] = array( |
| 487 | 'label' => ts('Details'), |
| 488 | 'value' => $this->redact(CRM_Utils_String::stripAlternatives($activityDAO->details)), |
| 489 | 'type' => 'Memo', |
| 490 | ); |
| 491 | |
| 492 | // Skip Duration field if empty (to avoid " minutes" output). Might want to do this for all fields at some point. dgg |
| 493 | if ($activityDAO->duration) { |
| 494 | $activity['fields'][] = array( |
| 495 | 'label' => ts('Duration'), |
| 496 | 'value' => $activityDAO->duration . ' ' . ts('minutes'), |
| 497 | 'type' => 'Int', |
| 498 | ); |
| 499 | } |
| 500 | $activity['fields'][] = array( |
| 501 | 'label' => ts('Status'), |
| 502 | 'value' => CRM_Core_PseudoConstant::getLabel('CRM_Activity_DAO_Activity', 'activity_status_id', |
| 503 | $activityDAO->status_id |
| 504 | ), |
| 505 | 'type' => 'String', |
| 506 | ); |
| 507 | |
| 508 | $activity['fields'][] = array( |
| 509 | 'label' => ts('Priority'), |
| 510 | 'value' => CRM_Core_PseudoConstant::getLabel('CRM_Activity_DAO_Activity', 'priority_id', |
| 511 | $activityDAO->priority_id |
| 512 | ), |
| 513 | 'type' => 'String', |
| 514 | ); |
| 515 | |
| 516 | //for now empty custom groups |
| 517 | $activity['customGroups'] = $this->getCustomData($clientID, |
| 518 | $activityDAO, |
| 519 | $activityTypeInfo |
| 520 | ); |
| 521 | |
| 522 | return $activity; |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * @param int $clientID |
| 527 | * @param $activityDAO |
| 528 | * @param $activityTypeInfo |
| 529 | * |
| 530 | * @return array|null |
| 531 | */ |
| 532 | public function getCustomData($clientID, $activityDAO, &$activityTypeInfo) { |
| 533 | list($typeValues, $options, $sql) = $this->getActivityTypeCustomSQL($activityTypeInfo['id'], '%Y-%m-%d'); |
| 534 | |
| 535 | $params = array(1 => array($activityDAO->id, 'Integer')); |
| 536 | |
| 537 | $customGroups = array(); |
| 538 | foreach ($sql as $tableName => $sqlClause) { |
| 539 | $dao = CRM_Core_DAO::executeQuery($sqlClause, $params); |
| 540 | if ($dao->fetch()) { |
| 541 | $customGroup = array(); |
| 542 | foreach ($typeValues[$tableName] as $columnName => $typeValue) { |
| 543 | |
| 544 | if (CRM_Utils_Array::value('type', $typeValue) == 'Date') { |
| 545 | $value = $dao->$columnName; |
| 546 | } |
| 547 | else { |
| 548 | $value = CRM_Core_BAO_CustomField::displayValue($dao->$columnName, $typeValue['fieldID'], $activityDAO->id); |
| 549 | } |
| 550 | |
| 551 | if ($value) { |
| 552 | // Note: this is already taken care in getDisplayValue above, but sometimes |
| 553 | // strings like '^A^A' creates problem. So to fix this special case - |
| 554 | if (strstr($value, CRM_Core_DAO::VALUE_SEPARATOR)) { |
| 555 | $value = trim($value, CRM_Core_DAO::VALUE_SEPARATOR); |
| 556 | } |
| 557 | if (CRM_Utils_Array::value('type', $typeValue) == 'String' || |
| 558 | CRM_Utils_Array::value('type', $typeValue) == 'Memo' |
| 559 | ) { |
| 560 | $value = $this->redact($value); |
| 561 | } |
| 562 | elseif (CRM_Utils_Array::value('type', $typeValue) == 'Link') { |
| 563 | $value = CRM_Utils_System::formatWikiURL($value); |
| 564 | } |
| 565 | } |
| 566 | //$typeValue |
| 567 | $customGroup[] = array( |
| 568 | 'label' => $typeValue['label'], |
| 569 | 'value' => $value, |
| 570 | 'type' => $typeValue['type'], |
| 571 | 'fieldID' => $typeValue['fieldID'], |
| 572 | ); |
| 573 | } |
| 574 | $customGroups[$dao->groupTitle] = $customGroup; |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | return empty($customGroups) ? NULL : $customGroups; |
| 579 | } |
| 580 | |
| 581 | /** |
| 582 | * @param int $activityTypeID |
| 583 | * @param null $dateFormat |
| 584 | * @param bool $onlyActive |
| 585 | * |
| 586 | * @return mixed |
| 587 | */ |
| 588 | public function getActivityTypeCustomSQL($activityTypeID, $dateFormat = NULL, $onlyActive = TRUE) { |
| 589 | static $cache = array(); |
| 590 | |
| 591 | if (is_null($activityTypeID)) { |
| 592 | $activityTypeID = 0; |
| 593 | } |
| 594 | |
| 595 | if (!isset($cache[$activityTypeID])) { |
| 596 | $query = " |
| 597 | SELECT cg.title as groupTitle, |
| 598 | cg.table_name as tableName , |
| 599 | cf.column_name as columnName, |
| 600 | cf.label as label , |
| 601 | cg.id as groupID , |
| 602 | cf.id as fieldID , |
| 603 | cf.data_type as dataType , |
| 604 | cf.html_type as htmlType , |
| 605 | cf.option_group_id as optionGroupID |
| 606 | FROM civicrm_custom_group cg, |
| 607 | civicrm_custom_field cf |
| 608 | WHERE cf.custom_group_id = cg.id |
| 609 | AND cg.extends = 'Activity' |
| 610 | AND " . CRM_Core_Permission::customGroupClause(CRM_Core_Permission::VIEW, 'cg.'); |
| 611 | |
| 612 | if ($onlyActive) { |
| 613 | $query .= " AND cf.is_active = 1 "; |
| 614 | } |
| 615 | if ($activityTypeID) { |
| 616 | $query .= "AND ( cg.extends_entity_column_value IS NULL OR cg.extends_entity_column_value LIKE '%" . CRM_Core_DAO::VALUE_SEPARATOR . "%1" . CRM_Core_DAO::VALUE_SEPARATOR . "%' )"; |
| 617 | } |
| 618 | else { |
| 619 | $query .= "AND cg.extends_entity_column_value IS NULL"; |
| 620 | } |
| 621 | $query .= "ORDER BY cg.weight, cf.weight"; |
| 622 | $params = array( |
| 623 | 1 => array( |
| 624 | $activityTypeID, |
| 625 | 'Integer', |
| 626 | ), |
| 627 | ); |
| 628 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 629 | |
| 630 | $result = $options = $sql = $groupTitle = array(); |
| 631 | while ($dao->fetch()) { |
| 632 | if (!array_key_exists($dao->tableName, $result)) { |
| 633 | $result[$dao->tableName] = array(); |
| 634 | $sql[$dao->tableName] = array(); |
| 635 | } |
| 636 | $result[$dao->tableName][$dao->columnName] = array( |
| 637 | 'label' => $dao->label, |
| 638 | 'type' => $dao->dataType, |
| 639 | 'fieldID' => $dao->fieldID, |
| 640 | ); |
| 641 | |
| 642 | $options[$dao->fieldID] = array(); |
| 643 | $options[$dao->fieldID]['attributes'] = array( |
| 644 | 'label' => $dao->label, |
| 645 | 'data_type' => $dao->dataType, |
| 646 | 'html_type' => $dao->htmlType, |
| 647 | ); |
| 648 | // since we want to add ISO date format. |
| 649 | if ($dateFormat && $dao->htmlType == 'Select Date') { |
| 650 | $options[$dao->fieldID]['attributes']['format'] = $dateFormat; |
| 651 | } |
| 652 | if ($dao->optionGroupID) { |
| 653 | $query = " |
| 654 | SELECT label, value |
| 655 | FROM civicrm_option_value |
| 656 | WHERE option_group_id = %1 |
| 657 | "; |
| 658 | |
| 659 | $option = CRM_Core_DAO::executeQuery($query, array(1 => array($dao->optionGroupID, 'Positive'))); |
| 660 | while ($option->fetch()) { |
| 661 | $dataType = $dao->dataType; |
| 662 | if ($dataType == 'Int' || $dataType == 'Float') { |
| 663 | $num = round($option->value, 2); |
| 664 | $options[$dao->fieldID]["$num"] = $option->label; |
| 665 | } |
| 666 | else { |
| 667 | $options[$dao->fieldID][$option->value] = $option->label; |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | $sql[$dao->tableName][] = $dao->columnName; |
| 673 | $groupTitle[$dao->tableName] = $dao->groupTitle; |
| 674 | } |
| 675 | |
| 676 | foreach ($sql as $tableName => $values) { |
| 677 | $columnNames = implode(',', $values); |
| 678 | $title = CRM_Core_DAO::escapeString($groupTitle[$tableName]); |
| 679 | $mysqlTableName = CRM_Utils_Type::escape($tableName, 'MysqlColumnNameOrAlias'); |
| 680 | $sql[$tableName] = " |
| 681 | SELECT '" . $title . "' as groupTitle, $columnNames |
| 682 | FROM $mysqlTableName |
| 683 | WHERE entity_id = %1 |
| 684 | "; |
| 685 | } |
| 686 | |
| 687 | $cache[$activityTypeID] = array($result, $options, $sql); |
| 688 | } |
| 689 | return $cache[$activityTypeID]; |
| 690 | } |
| 691 | |
| 692 | /** |
| 693 | * @param int $activityID |
| 694 | * |
| 695 | * @return null|string |
| 696 | */ |
| 697 | public function getCreatedBy($activityID) { |
| 698 | $query = " |
| 699 | SELECT c.display_name |
| 700 | FROM civicrm_contact c, |
| 701 | civicrm_log l |
| 702 | WHERE l.entity_table = 'civicrm_activity' |
| 703 | AND l.entity_id = %1 |
| 704 | AND l.modified_id = c.id |
| 705 | LIMIT 1 |
| 706 | "; |
| 707 | |
| 708 | $params = array(1 => array($activityID, 'Integer')); |
| 709 | return CRM_Core_DAO::singleValueQuery($query, $params); |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * @param $string |
| 714 | * @param bool $printReport |
| 715 | * @param array $replaceString |
| 716 | * |
| 717 | * @return mixed |
| 718 | */ |
| 719 | private function redact($string, $printReport = FALSE, $replaceString = array()) { |
| 720 | if ($printReport) { |
| 721 | return CRM_Utils_String::redaction($string, $replaceString); |
| 722 | } |
| 723 | elseif ($this->_isRedact) { |
| 724 | $regexToReplaceString = CRM_Utils_String::regex($string, $this->_redactionRegexRules); |
| 725 | return CRM_Utils_String::redaction($string, array_merge($this->_redactionStringRules, $regexToReplaceString)); |
| 726 | } |
| 727 | return $string; |
| 728 | } |
| 729 | |
| 730 | /** |
| 731 | * @param int $clientID |
| 732 | * @param int $caseID |
| 733 | * @param string $activitySetName |
| 734 | * @param array $params |
| 735 | * @param CRM_Core_Form $form |
| 736 | * |
| 737 | * @return mixed |
| 738 | */ |
| 739 | public static function getCaseReport($clientID, $caseID, $activitySetName, $params, $form) { |
| 740 | |
| 741 | $template = CRM_Core_Smarty::singleton(); |
| 742 | |
| 743 | $template->assign('caseId', $caseID); |
| 744 | $template->assign('clientID', $clientID); |
| 745 | $template->assign('activitySetName', $activitySetName); |
| 746 | |
| 747 | if (!empty($params['is_redact'])) { |
| 748 | $form->_isRedact = TRUE; |
| 749 | $template->assign('_isRedact', 'true'); |
| 750 | } |
| 751 | else { |
| 752 | $form->_isRedact = FALSE; |
| 753 | $template->assign('_isRedact', 'false'); |
| 754 | } |
| 755 | |
| 756 | // first get all case information |
| 757 | $case = $form->caseInfo($clientID, $caseID); |
| 758 | $template->assign_by_ref('case', $case); |
| 759 | |
| 760 | if ($params['include_activities'] == 1) { |
| 761 | $template->assign('includeActivities', 'All'); |
| 762 | } |
| 763 | else { |
| 764 | $template->assign('includeActivities', 'Missing activities only'); |
| 765 | } |
| 766 | |
| 767 | $xml = $form->retrieve($case['caseTypeName']); |
| 768 | |
| 769 | $activitySetNames = CRM_Case_XMLProcessor_Process::activitySets($xml->ActivitySets); |
| 770 | $pageTitle = CRM_Utils_Array::value($activitySetName, $activitySetNames); |
| 771 | $template->assign('pageTitle', $pageTitle); |
| 772 | |
| 773 | if ($activitySetName) { |
| 774 | $activityTypes = $form->getActivityTypes($xml, $activitySetName); |
| 775 | } |
| 776 | else { |
| 777 | $activityTypes = CRM_Case_XMLProcessor::allActivityTypes(); |
| 778 | } |
| 779 | |
| 780 | if (!$activityTypes) { |
| 781 | return FALSE; |
| 782 | } |
| 783 | |
| 784 | // next get activity set Information |
| 785 | $activitySet = array( |
| 786 | 'label' => $form->getActivitySetLabel($xml, $activitySetName), |
| 787 | 'includeActivities' => 'All', |
| 788 | 'redact' => 'false', |
| 789 | ); |
| 790 | $template->assign_by_ref('activitySet', $activitySet); |
| 791 | |
| 792 | //now collect all the information about activities |
| 793 | $activities = array(); |
| 794 | $form->getActivities($clientID, $caseID, $activityTypes, $activities); |
| 795 | $template->assign_by_ref('activities', $activities); |
| 796 | |
| 797 | // now run the template |
| 798 | $contents = $template->fetch('CRM/Case/XMLProcessor/Report.tpl'); |
| 799 | return $contents; |
| 800 | } |
| 801 | |
| 802 | public static function printCaseReport() { |
| 803 | $caseID = CRM_Utils_Request::retrieve('caseID', 'Positive'); |
| 804 | $clientID = CRM_Utils_Request::retrieve('cid', 'Positive'); |
| 805 | $activitySetName = CRM_Utils_Request::retrieve('asn', 'String'); |
| 806 | $isRedact = CRM_Utils_Request::retrieve('redact', 'Boolean'); |
| 807 | $includeActivities = CRM_Utils_Request::retrieve('all', 'Positive'); |
| 808 | $params = $otherRelationships = $globalGroupInfo = array(); |
| 809 | $report = new CRM_Case_XMLProcessor_Report($isRedact); |
| 810 | if ($includeActivities) { |
| 811 | $params['include_activities'] = 1; |
| 812 | } |
| 813 | |
| 814 | if ($isRedact) { |
| 815 | $params['is_redact'] = 1; |
| 816 | $report->_redactionStringRules = array(); |
| 817 | } |
| 818 | $template = CRM_Core_Smarty::singleton(); |
| 819 | |
| 820 | //get case related relationships (Case Role) |
| 821 | $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($clientID, $caseID); |
| 822 | $caseType = CRM_Case_BAO_Case::getCaseType($caseID, 'name'); |
| 823 | |
| 824 | $xmlProcessor = new CRM_Case_XMLProcessor_Process(); |
| 825 | $caseRoles = $xmlProcessor->get($caseType, 'CaseRoles'); |
| 826 | foreach ($caseRelationships as $key => & $value) { |
| 827 | if (!empty($caseRoles[$value['relation_type'] . '_' . $value['relationship_direction']])) { |
| 828 | unset($caseRoles[$value['relation_type'] . '_' . $value['relationship_direction']]); |
| 829 | } |
| 830 | if ($isRedact) { |
| 831 | if (!array_key_exists($value['name'], $report->_redactionStringRules)) { |
| 832 | $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules, |
| 833 | array($value['name'] => 'name_' . rand(10000, 100000)) |
| 834 | ); |
| 835 | } |
| 836 | $value['name'] = $report->redact($value['name'], TRUE, $report->_redactionStringRules); |
| 837 | if (!empty($value['email']) && |
| 838 | !array_key_exists($value['email'], $report->_redactionStringRules) |
| 839 | ) { |
| 840 | $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules, |
| 841 | array($value['email'] => 'email_' . rand(10000, 100000)) |
| 842 | ); |
| 843 | } |
| 844 | |
| 845 | $value['email'] = $report->redact($value['email'], TRUE, $report->_redactionStringRules); |
| 846 | |
| 847 | if (!empty($value['phone']) && |
| 848 | !array_key_exists($value['phone'], $report->_redactionStringRules) |
| 849 | ) { |
| 850 | $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules, |
| 851 | array($value['phone'] => 'phone_' . rand(10000, 100000)) |
| 852 | ); |
| 853 | } |
| 854 | $value['phone'] = $report->redact($value['phone'], TRUE, $report->_redactionStringRules); |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | $caseRoles['client'] = CRM_Case_BAO_Case::getContactNames($caseID); |
| 859 | if ($isRedact) { |
| 860 | foreach ($caseRoles['client'] as &$client) { |
| 861 | if (!array_key_exists(CRM_Utils_Array::value('sort_name', $client), $report->_redactionStringRules)) { |
| 862 | |
| 863 | $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules, |
| 864 | array(CRM_Utils_Array::value('sort_name', $client) => 'name_' . rand(10000, 100000)) |
| 865 | ); |
| 866 | } |
| 867 | if (!array_key_exists(CRM_Utils_Array::value('display_name', $client), $report->_redactionStringRules)) { |
| 868 | $report->_redactionStringRules[CRM_Utils_Array::value('display_name', $client)] = $report->_redactionStringRules[CRM_Utils_Array::value('sort_name', $client)]; |
| 869 | } |
| 870 | $client['sort_name'] = $report->redact(CRM_Utils_Array::value('sort_name', $client), TRUE, $report->_redactionStringRules); |
| 871 | if (!empty($client['email']) && |
| 872 | !array_key_exists($client['email'], $report->_redactionStringRules) |
| 873 | ) { |
| 874 | $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules, |
| 875 | array($client['email'] => 'email_' . rand(10000, 100000)) |
| 876 | ); |
| 877 | } |
| 878 | $client['email'] = $report->redact(CRM_Utils_Array::value('email', $client), TRUE, $report->_redactionStringRules); |
| 879 | |
| 880 | if (!empty($client['phone']) && |
| 881 | !array_key_exists($client['phone'], $report->_redactionStringRules) |
| 882 | ) { |
| 883 | $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules, |
| 884 | array($client['phone'] => 'phone_' . rand(10000, 100000)) |
| 885 | ); |
| 886 | } |
| 887 | $client['phone'] = $report->redact(CRM_Utils_Array::value('phone', $client), TRUE, $report->_redactionStringRules); |
| 888 | } |
| 889 | } |
| 890 | // Retrieve ALL client relationships |
| 891 | $relClient = CRM_Contact_BAO_Relationship::getRelationship($clientID, |
| 892 | CRM_Contact_BAO_Relationship::CURRENT, |
| 893 | 0, 0, 0, NULL, NULL, FALSE |
| 894 | ); |
| 895 | foreach ($relClient as $r) { |
| 896 | if ($isRedact) { |
| 897 | if (!array_key_exists($r['name'], $report->_redactionStringRules)) { |
| 898 | $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules, |
| 899 | array($r['name'] => 'name_' . rand(10000, 100000)) |
| 900 | ); |
| 901 | } |
| 902 | if (!array_key_exists($r['display_name'], $report->_redactionStringRules)) { |
| 903 | $report->_redactionStringRules[$r['display_name']] = $report->_redactionStringRules[$r['name']]; |
| 904 | } |
| 905 | $r['name'] = $report->redact($r['name'], TRUE, $report->_redactionStringRules); |
| 906 | |
| 907 | if (!empty($r['phone']) && |
| 908 | !array_key_exists($r['phone'], $report->_redactionStringRules) |
| 909 | ) { |
| 910 | $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules, |
| 911 | array($r['phone'] => 'phone_' . rand(10000, 100000)) |
| 912 | ); |
| 913 | } |
| 914 | $r['phone'] = $report->redact($r['phone'], TRUE, $report->_redactionStringRules); |
| 915 | |
| 916 | if (!empty($r['email']) && |
| 917 | !array_key_exists($r['email'], $report->_redactionStringRules) |
| 918 | ) { |
| 919 | $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules, |
| 920 | array($r['email'] => 'email_' . rand(10000, 100000)) |
| 921 | ); |
| 922 | } |
| 923 | $r['email'] = $report->redact($r['email'], TRUE, $report->_redactionStringRules); |
| 924 | } |
| 925 | if (!array_key_exists($r['id'], $caseRelationships)) { |
| 926 | $otherRelationships[] = $r; |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | // Now global contact list that appears on all cases. |
| 931 | $relGlobal = CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo); |
| 932 | foreach ($relGlobal as & $r) { |
| 933 | if ($isRedact) { |
| 934 | if (!array_key_exists($r['sort_name'], $report->_redactionStringRules)) { |
| 935 | $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules, |
| 936 | array($r['sort_name'] => 'name_' . rand(10000, 100000)) |
| 937 | ); |
| 938 | } |
| 939 | if (!array_key_exists($r['display_name'], $report->_redactionStringRules)) { |
| 940 | $report->_redactionStringRules[$r['display_name']] = $report->_redactionStringRules[$r['sort_name']]; |
| 941 | } |
| 942 | |
| 943 | $r['sort_name'] = $report->redact($r['sort_name'], TRUE, $report->_redactionStringRules); |
| 944 | if (!empty($r['phone']) && |
| 945 | !array_key_exists($r['phone'], $report->_redactionStringRules) |
| 946 | ) { |
| 947 | $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules, |
| 948 | array($r['phone'] => 'phone_' . rand(10000, 100000)) |
| 949 | ); |
| 950 | } |
| 951 | $r['phone'] = $report->redact($r['phone'], TRUE, $report->_redactionStringRules); |
| 952 | |
| 953 | if (!empty($r['email']) && |
| 954 | !array_key_exists($r['email'], $report->_redactionStringRules) |
| 955 | ) { |
| 956 | $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules, |
| 957 | array($r['email'] => 'email_' . rand(10000, 100000)) |
| 958 | ); |
| 959 | } |
| 960 | $r['email'] = $report->redact($r['email'], TRUE, $report->_redactionStringRules); |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | // Retrieve custom values for cases. |
| 965 | $customValues = CRM_Core_BAO_CustomValueTable::getEntityValues($caseID, 'Case'); |
| 966 | $extends = array('case'); |
| 967 | $groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, NULL, $extends); |
| 968 | $caseCustomFields = array(); |
| 969 | foreach ($groupTree as $gid => $group_values) { |
| 970 | foreach ($group_values['fields'] as $id => $field_values) { |
| 971 | if (array_key_exists($id, $customValues)) { |
| 972 | $caseCustomFields[$gid]['title'] = $group_values['title']; |
| 973 | $caseCustomFields[$gid]['values'][$id] = array( |
| 974 | 'label' => $field_values['label'], |
| 975 | 'value' => $customValues[$id], |
| 976 | ); |
| 977 | } |
| 978 | } |
| 979 | } |
| 980 | $template->assign('caseRelationships', $caseRelationships); |
| 981 | $template->assign('caseRoles', $caseRoles); |
| 982 | $template->assign('otherRelationships', $otherRelationships); |
| 983 | $template->assign('globalRelationships', $relGlobal); |
| 984 | $template->assign('globalGroupInfo', $globalGroupInfo); |
| 985 | $template->assign('caseCustomFields', $caseCustomFields); |
| 986 | $contents = self::getCaseReport($clientID, |
| 987 | $caseID, |
| 988 | $activitySetName, |
| 989 | $params, |
| 990 | $report |
| 991 | ); |
| 992 | $printReport = CRM_Case_Audit_Audit::run($contents, $clientID, $caseID, TRUE); |
| 993 | echo $printReport; |
| 994 | CRM_Utils_System::civiExit(); |
| 995 | } |
| 996 | |
| 997 | } |