| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * |
| 14 | * @package CRM |
| 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 16 | */ |
| 17 | |
| 18 | /** |
| 19 | * This class contains all the function that are called using AJAX (jQuery) |
| 20 | */ |
| 21 | class CRM_Activity_Page_AJAX { |
| 22 | |
| 23 | public static function getCaseActivity() { |
| 24 | // Should those params be passed through the validateParams method? |
| 25 | $caseID = CRM_Utils_Type::validate($_GET['caseID'], 'Integer'); |
| 26 | $contactID = CRM_Utils_Type::validate($_GET['cid'], 'Integer'); |
| 27 | $userID = CRM_Utils_Type::validate($_GET['userID'], 'Integer'); |
| 28 | $context = CRM_Utils_Type::validate(CRM_Utils_Array::value('context', $_GET), 'String'); |
| 29 | |
| 30 | $optionalParameters = [ |
| 31 | 'source_contact_id' => 'Integer', |
| 32 | 'status_id' => 'Integer', |
| 33 | 'activity_deleted' => 'Boolean', |
| 34 | 'activity_type_id' => 'Integer', |
| 35 | // "Date" validation fails because it expects only numbers with no hyphens |
| 36 | 'activity_date_low' => 'Alphanumeric', |
| 37 | 'activity_date_high' => 'Alphanumeric', |
| 38 | ]; |
| 39 | |
| 40 | $params = CRM_Core_Page_AJAX::defaultSortAndPagerParams(); |
| 41 | $params += CRM_Core_Page_AJAX::validateParams([], $optionalParameters); |
| 42 | |
| 43 | // get the activities related to given case |
| 44 | $activities = CRM_Case_BAO_Case::getCaseActivity($caseID, $params, $contactID, $context, $userID); |
| 45 | |
| 46 | CRM_Utils_JSON::output($activities); |
| 47 | } |
| 48 | |
| 49 | public static function getCaseGlobalRelationships() { |
| 50 | $params = CRM_Core_Page_AJAX::defaultSortAndPagerParams(); |
| 51 | |
| 52 | // get the activities related to given case |
| 53 | $globalGroupInfo = []; |
| 54 | |
| 55 | // get the total row count |
| 56 | CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo, NULL, FALSE, TRUE, NULL, NULL); |
| 57 | // limit the rows |
| 58 | $relGlobal = CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo, $params['sortBy'] ?? NULL, $showLinks = TRUE, FALSE, $params['offset'], $params['rp']); |
| 59 | |
| 60 | $relationships = []; |
| 61 | // after sort we can update username fields to be a url |
| 62 | foreach ($relGlobal as $key => $value) { |
| 63 | $relationship = []; |
| 64 | $relationship['sort_name'] = $value['sort_name']; |
| 65 | $relationship['phone'] = $value['phone']; |
| 66 | $relationship['email'] = $value['email']; |
| 67 | |
| 68 | array_push($relationships, $relationship); |
| 69 | } |
| 70 | |
| 71 | $globalRelationshipsDT = []; |
| 72 | $globalRelationshipsDT['data'] = $relationships; |
| 73 | $globalRelationshipsDT['recordsTotal'] = count($relationships); |
| 74 | $globalRelationshipsDT['recordsFiltered'] = count($relationships); |
| 75 | |
| 76 | CRM_Utils_JSON::output($globalRelationshipsDT); |
| 77 | } |
| 78 | |
| 79 | public static function getCaseClientRelationships() { |
| 80 | $caseID = CRM_Utils_Type::escape($_GET['caseID'], 'Integer'); |
| 81 | $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer'); |
| 82 | |
| 83 | $params = CRM_Core_Page_AJAX::defaultSortAndPagerParams(); |
| 84 | |
| 85 | // Retrieve ALL client relationships |
| 86 | $relClient = CRM_Contact_BAO_Relationship::getRelationship($contactID, |
| 87 | CRM_Contact_BAO_Relationship::CURRENT, |
| 88 | 0, 0, 0, NULL, NULL, FALSE |
| 89 | ); |
| 90 | |
| 91 | $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($contactID, $caseID); |
| 92 | |
| 93 | // Now build 'Other Relationships' array by removing relationships that are already listed under Case Roles |
| 94 | // so they don't show up twice. |
| 95 | $clientRelationships = []; |
| 96 | foreach ($relClient as $r) { |
| 97 | if (!array_key_exists($r['id'], $caseRelationships)) { |
| 98 | $clientRelationships[] = $r; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // sort clientRelationships array using jquery call params |
| 103 | $sortArray = []; |
| 104 | if (!empty($params['_raw_values']['sort'])) { |
| 105 | foreach ($clientRelationships as $key => $row) { |
| 106 | $sortArray[$key] = $row[$params['_raw_values']['sort'][0]]; |
| 107 | } |
| 108 | $sort_type = "SORT_" . strtoupper($params['_raw_values']['order'][0]); |
| 109 | array_multisort($sortArray, constant($sort_type), $clientRelationships); |
| 110 | } |
| 111 | |
| 112 | $relationships = []; |
| 113 | // after sort we can update username fields to be a url |
| 114 | foreach ($clientRelationships as $key => $value) { |
| 115 | $relationship = []; |
| 116 | $relationship['relation'] = $value['relation']; |
| 117 | $relationship['name'] = '<a href=' . CRM_Utils_System::url('civicrm/contact/view', |
| 118 | 'action=view&reset=1&cid=' . $clientRelationships[$key]['cid']) . '>' . $clientRelationships[$key]['name'] . '</a>'; |
| 119 | $relationship['phone'] = $value['phone']; |
| 120 | $relationship['email'] = $value['email']; |
| 121 | |
| 122 | array_push($relationships, $relationship); |
| 123 | } |
| 124 | |
| 125 | $clientRelationshipsDT = []; |
| 126 | $clientRelationshipsDT['data'] = $relationships; |
| 127 | $clientRelationshipsDT['recordsTotal'] = count($relationships); |
| 128 | $clientRelationshipsDT['recordsFiltered'] = count($relationships); |
| 129 | |
| 130 | CRM_Utils_JSON::output($clientRelationshipsDT); |
| 131 | } |
| 132 | |
| 133 | public static function getCaseRoles() { |
| 134 | $caseID = CRM_Utils_Type::escape($_GET['caseID'], 'Integer'); |
| 135 | $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer'); |
| 136 | |
| 137 | $params = CRM_Core_Page_AJAX::defaultSortAndPagerParams(); |
| 138 | |
| 139 | $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($contactID, $caseID); |
| 140 | $caseTypeName = CRM_Case_BAO_Case::getCaseType($caseID, 'name'); |
| 141 | $xmlProcessor = new CRM_Case_XMLProcessor_Process(); |
| 142 | $caseRoles = $xmlProcessor->get($caseTypeName, 'CaseRoles'); |
| 143 | |
| 144 | $hasAccessToAllCases = CRM_Core_Permission::check('access all cases and activities'); |
| 145 | |
| 146 | $managerRoleId = $xmlProcessor->getCaseManagerRoleId($caseTypeName); |
| 147 | |
| 148 | foreach ($caseRelationships as $key => $value) { |
| 149 | // This role has been filled |
| 150 | unset($caseRoles[$value['relation_type'] . '_' . $value['relationship_direction']]); |
| 151 | // mark original case relationships record to use on setting edit links below |
| 152 | $caseRelationships[$key]['source'] = 'caseRel'; |
| 153 | } |
| 154 | |
| 155 | $caseRoles['client'] = CRM_Case_BAO_Case::getContactNames($caseID); |
| 156 | |
| 157 | // move/transform caseRoles array data to caseRelationships |
| 158 | // for sorting and display |
| 159 | // CRM-14466 added cid to the non-client array to avoid php notice |
| 160 | foreach ($caseRoles as $id => $value) { |
| 161 | if ($id != "client") { |
| 162 | $rel = []; |
| 163 | $rel['relation'] = $value; |
| 164 | $rel['relation_type'] = $id; |
| 165 | $rel['name'] = '(not assigned)'; |
| 166 | $rel['phone'] = ''; |
| 167 | $rel['email'] = ''; |
| 168 | $rel['source'] = 'caseRoles'; |
| 169 | $caseRelationships[] = $rel; |
| 170 | } |
| 171 | else { |
| 172 | foreach ($value as $clientRole) { |
| 173 | $relClient = []; |
| 174 | $relClient['relation'] = ts('Client'); |
| 175 | $relClient['name'] = $clientRole['sort_name']; |
| 176 | $relClient['phone'] = $clientRole['phone']; |
| 177 | $relClient['email'] = $clientRole['email']; |
| 178 | $relClient['cid'] = $clientRole['contact_id']; |
| 179 | $relClient['source'] = 'contact'; |
| 180 | $caseRelationships[] = $relClient; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // sort clientRelationships array using jquery call params |
| 186 | $sortArray = []; |
| 187 | if (!empty($params['_raw_values']['sort'])) { |
| 188 | foreach ($caseRelationships as $key => $row) { |
| 189 | $sortArray[$key] = $row[$params['_raw_values']['sort'][0]]; |
| 190 | } |
| 191 | $sort_type = "SORT_" . strtoupper($params['_raw_values']['order'][0]); |
| 192 | array_multisort($sortArray, constant($sort_type), $caseRelationships); |
| 193 | } |
| 194 | |
| 195 | $relationships = []; |
| 196 | |
| 197 | // set user name, email and edit columns links |
| 198 | foreach ($caseRelationships as $key => &$row) { |
| 199 | $typeLabel = $row['relation']; |
| 200 | // Add "<br />(Case Manager)" to label |
| 201 | if (!empty($row['relation_type']) && !empty($row['relationship_direction']) && $row['relation_type'] . '_' . $row['relationship_direction'] == $managerRoleId) { |
| 202 | $row['relation'] .= '<br />' . '(' . ts('Case Manager') . ')'; |
| 203 | } |
| 204 | // view user links |
| 205 | if (!empty($row['cid'])) { |
| 206 | $row['name'] = '<a class="view-contact" title="' . ts('View Contact') . '" href=' . CRM_Utils_System::url('civicrm/contact/view', |
| 207 | 'action=view&reset=1&cid=' . $row['cid']) . '>' . $row['name'] . '</a>'; |
| 208 | } |
| 209 | // email column links/icon |
| 210 | if ($row['email']) { |
| 211 | $row['email'] = '<a class="crm-hover-button crm-popup" href="' . CRM_Utils_System::url('civicrm/activity/email/add', 'reset=1&action=add&atype=3&cid=' . $row['cid']) . '&caseid=' . $caseID . '" title="' . ts('Send an Email') . '"><i class="crm-i fa-envelope" aria-hidden="true"></i></a>'; |
| 212 | } |
| 213 | // edit links |
| 214 | $row['actions'] = ''; |
| 215 | if ($hasAccessToAllCases) { |
| 216 | $contactType = empty($row['relation_type']) ? '' : (string) CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $row['relation_type'], 'contact_type_b'); |
| 217 | $contactType = $contactType == 'Contact' ? '' : $contactType; |
| 218 | switch ($row['source']) { |
| 219 | case 'caseRel': |
| 220 | $row['actions'] = '<a href="#editCaseRoleDialog" title="' . ts('Reassign %1', [1 => $typeLabel]) . '" class="crm-hover-button case-miniform" data-contact_type="' . $contactType . '" data-rel_type="' . $row['relation_type'] . '_' . $row['relationship_direction'] . '" data-cid="' . $row['cid'] . '" data-rel_id="' . $row['rel_id'] . '"data-key="' . CRM_Core_Key::get('civicrm/ajax/relation') . '">' . |
| 221 | '<i class="crm-i fa-pencil" aria-hidden="true"></i>' . |
| 222 | '</a>' . |
| 223 | '<a href="#deleteCaseRoleDialog" title="' . ts('Remove %1', [1 => $typeLabel]) . '" class="crm-hover-button case-miniform" data-contact_type="' . $contactType . '" data-rel_type="' . $row['relation_type'] . '_' . $row['relationship_direction'] . '" data-cid="' . $row['cid'] . '" data-key="' . CRM_Core_Key::get('civicrm/ajax/delcaserole') . '">' . |
| 224 | '<span class="icon delete-icon"></span>' . |
| 225 | '</a>'; |
| 226 | break; |
| 227 | |
| 228 | case 'caseRoles': |
| 229 | $row['actions'] = '<a href="#editCaseRoleDialog" title="' . ts('Assign %1', [1 => $typeLabel]) . '" class="crm-hover-button case-miniform" data-contact_type="' . $contactType . '" data-rel_type="' . $row['relation_type'] . '_a_b" data-key="' . CRM_Core_Key::get('civicrm/ajax/relation') . '">' . |
| 230 | '<i class="crm-i fa-pencil" aria-hidden="true"></i>' . |
| 231 | '</a>'; |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | unset($row['cid']); |
| 236 | unset($row['relation_type']); |
| 237 | unset($row['rel_id']); |
| 238 | unset($row['client_id']); |
| 239 | unset($row['source']); |
| 240 | array_push($relationships, $row); |
| 241 | } |
| 242 | $params['total'] = count($relationships); |
| 243 | |
| 244 | $caseRelationshipsDT = []; |
| 245 | $caseRelationshipsDT['data'] = $relationships; |
| 246 | $caseRelationshipsDT['recordsTotal'] = $params['total']; |
| 247 | $caseRelationshipsDT['recordsFiltered'] = $params['total']; |
| 248 | |
| 249 | CRM_Utils_JSON::output($caseRelationshipsDT); |
| 250 | |
| 251 | } |
| 252 | |
| 253 | public static function convertToCaseActivity() { |
| 254 | $params = ['caseID', 'activityID', 'contactID', 'newSubject', 'targetContactIds', 'mode']; |
| 255 | $vals = []; |
| 256 | foreach ($params as $param) { |
| 257 | $vals[$param] = $_POST[$param] ?? NULL; |
| 258 | } |
| 259 | |
| 260 | CRM_Utils_JSON::output(self::_convertToCaseActivity($vals)); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * @param array $params |
| 265 | * |
| 266 | * @return array |
| 267 | */ |
| 268 | public static function _convertToCaseActivity($params) { |
| 269 | if (!$params['activityID'] || !$params['caseID']) { |
| 270 | return (['error_msg' => 'required params missing.']); |
| 271 | } |
| 272 | |
| 273 | $otherActivity = new CRM_Activity_DAO_Activity(); |
| 274 | $otherActivity->id = $params['activityID']; |
| 275 | if (!$otherActivity->find(TRUE)) { |
| 276 | return (['error_msg' => 'activity record is missing.']); |
| 277 | } |
| 278 | $actDateTime = $otherActivity->activity_date_time; |
| 279 | |
| 280 | // Create new activity record. |
| 281 | $mainActivity = new CRM_Activity_DAO_Activity(); |
| 282 | $mainActVals = []; |
| 283 | CRM_Core_DAO::storeValues($otherActivity, $mainActVals); |
| 284 | |
| 285 | // Get new activity subject. |
| 286 | if (!empty($params['newSubject'])) { |
| 287 | $mainActVals['subject'] = $params['newSubject']; |
| 288 | } |
| 289 | |
| 290 | $mainActivity->copyValues($mainActVals); |
| 291 | $mainActivity->id = NULL; |
| 292 | $mainActivity->activity_date_time = $actDateTime; |
| 293 | // Make sure this is current revision. |
| 294 | $mainActivity->is_current_revision = TRUE; |
| 295 | $mainActivity->original_id = $otherActivity->id; |
| 296 | $otherActivity->is_current_revision = FALSE; |
| 297 | |
| 298 | $mainActivity->save(); |
| 299 | $mainActivityId = $mainActivity->id; |
| 300 | CRM_Activity_BAO_Activity::logActivityAction($mainActivity); |
| 301 | |
| 302 | // Mark previous activity as deleted. If it was a non-case activity |
| 303 | // then just change the subject. |
| 304 | if (in_array($params['mode'], [ |
| 305 | 'move', |
| 306 | 'file', |
| 307 | ])) { |
| 308 | $caseActivity = new CRM_Case_DAO_CaseActivity(); |
| 309 | $caseActivity->case_id = $params['caseID']; |
| 310 | $caseActivity->activity_id = $otherActivity->id; |
| 311 | if ($params['mode'] == 'move' || $caseActivity->find(TRUE)) { |
| 312 | $otherActivity->is_deleted = 1; |
| 313 | } |
| 314 | else { |
| 315 | $otherActivity->subject = ts('(Filed on case %1)', [ |
| 316 | 1 => $params['caseID'], |
| 317 | ]) . ' ' . $otherActivity->subject; |
| 318 | } |
| 319 | $otherActivity->save(); |
| 320 | |
| 321 | } |
| 322 | |
| 323 | $targetContacts = []; |
| 324 | if (!empty($params['targetContactIds'])) { |
| 325 | $targetContacts = array_unique(explode(',', $params['targetContactIds'])); |
| 326 | } |
| 327 | |
| 328 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
| 329 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
| 330 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); |
| 331 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
| 332 | |
| 333 | $sourceContactID = CRM_Activity_BAO_Activity::getSourceContactID($params['activityID']); |
| 334 | $src_params = [ |
| 335 | 'activity_id' => $mainActivityId, |
| 336 | 'contact_id' => $sourceContactID, |
| 337 | 'record_type_id' => $sourceID, |
| 338 | ]; |
| 339 | CRM_Activity_BAO_ActivityContact::create($src_params); |
| 340 | |
| 341 | foreach ($targetContacts as $key => $value) { |
| 342 | $targ_params = [ |
| 343 | 'activity_id' => $mainActivityId, |
| 344 | 'contact_id' => $value, |
| 345 | 'record_type_id' => $targetID, |
| 346 | ]; |
| 347 | CRM_Activity_BAO_ActivityContact::create($targ_params); |
| 348 | } |
| 349 | |
| 350 | //CRM-21114 retrieve assignee contacts from original case; allow overriding from params |
| 351 | $assigneeContacts = CRM_Activity_BAO_ActivityContact::retrieveContactIdsByActivityId($params['activityID'], $assigneeID); |
| 352 | if (!empty($params['assigneeContactIds'])) { |
| 353 | $assigneeContacts = array_unique(explode(',', $params['assigneeContactIds'])); |
| 354 | } |
| 355 | foreach ($assigneeContacts as $value) { |
| 356 | $assigneeParams = [ |
| 357 | 'activity_id' => $mainActivityId, |
| 358 | 'contact_id' => $value, |
| 359 | 'record_type_id' => $assigneeID, |
| 360 | ]; |
| 361 | CRM_Activity_BAO_ActivityContact::create($assigneeParams); |
| 362 | } |
| 363 | |
| 364 | // Attach newly created activity to case. |
| 365 | $caseActivity = new CRM_Case_DAO_CaseActivity(); |
| 366 | $caseActivity->case_id = $params['caseID']; |
| 367 | $caseActivity->activity_id = $mainActivityId; |
| 368 | $caseActivity->save(); |
| 369 | $error_msg = $caseActivity->_lastError; |
| 370 | |
| 371 | $params['mainActivityId'] = $mainActivityId; |
| 372 | CRM_Activity_BAO_Activity::copyExtendedActivityData($params); |
| 373 | CRM_Utils_Hook::post('create', 'CaseActivity', $caseActivity->id, $caseActivity); |
| 374 | |
| 375 | return (['error_msg' => $error_msg, 'newId' => $mainActivity->id]); |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Get activities for the contact. |
| 380 | * |
| 381 | * @throws \CRM_Core_Exception |
| 382 | */ |
| 383 | public static function getContactActivity() { |
| 384 | $requiredParameters = [ |
| 385 | 'cid' => 'Integer', |
| 386 | ]; |
| 387 | |
| 388 | $optionalParameters = [ |
| 389 | 'context' => 'String', |
| 390 | 'activity_type_id' => 'Integer', |
| 391 | 'activity_type_exclude_id' => 'Integer', |
| 392 | 'activity_status_id' => 'String', |
| 393 | 'activity_date_time_relative' => 'String', |
| 394 | 'activity_date_time_low' => 'String', |
| 395 | 'activity_date_time_high' => 'String', |
| 396 | ]; |
| 397 | |
| 398 | $params = CRM_Core_Page_AJAX::defaultSortAndPagerParams(); |
| 399 | $params += CRM_Core_Page_AJAX::validateParams($requiredParameters, $optionalParameters); |
| 400 | // $params will be modified later on, need to save original filters |
| 401 | $filterParams = $params; |
| 402 | |
| 403 | // To be consistent, the cid parameter should be renamed to contact_id in |
| 404 | // the template file, see templates/CRM/Activity/Selector/Selector.tpl |
| 405 | $params['contact_id'] = $params['cid']; |
| 406 | unset($params['cid']); |
| 407 | |
| 408 | // get the contact activities |
| 409 | $activities = CRM_Activity_BAO_Activity::getContactActivitySelector($params); |
| 410 | |
| 411 | foreach ($activities['data'] as $key => $value) { |
| 412 | // Check if recurring activity. |
| 413 | if (!empty($value['is_recurring_activity'])) { |
| 414 | $repeat = $value['is_recurring_activity']; |
| 415 | $activities['data'][$key]['activity_type'] .= '<br/><span class="bold">' . ts('Repeating (%1 of %2)', [1 => $repeat[0], 2 => $repeat[1]]) . '</span>'; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // store the activity filter preference CRM-11761 |
| 420 | if (Civi::settings()->get('preserve_activity_tab_filter') && ($userID = CRM_Core_Session::getLoggedInContactID())) { |
| 421 | $activityFilter = []; |
| 422 | unset($optionalParameters['context']); |
| 423 | foreach ($optionalParameters as $searchField => $dataType) { |
| 424 | $formSearchField = $searchField; |
| 425 | if ($searchField === 'activity_type_id') { |
| 426 | $formSearchField = 'activity_type_filter_id'; |
| 427 | } |
| 428 | elseif ($searchField === 'activity_type_exclude_id') { |
| 429 | $formSearchField = 'activity_type_exclude_filter_id'; |
| 430 | } |
| 431 | if (!empty($filterParams[$searchField])) { |
| 432 | $activityFilter[$formSearchField] = $filterParams[$searchField]; |
| 433 | if (in_array($searchField, ['activity_date_time_low', 'activity_date_time_high'])) { |
| 434 | $activityFilter['activity_date_time_relative'] = 0; |
| 435 | } |
| 436 | elseif ($searchField === 'activity_status_id') { |
| 437 | $activityFilter['status_id'] = explode(',', $activityFilter[$searchField]); |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | Civi::contactSettings()->set('activity_tab_filter', $activityFilter); |
| 443 | } |
| 444 | |
| 445 | CRM_Utils_JSON::output($activities); |
| 446 | } |
| 447 | |
| 448 | } |