| 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 | |
| 34 | /** |
| 35 | * This class is for activity functions. |
| 36 | */ |
| 37 | class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity { |
| 38 | |
| 39 | /** |
| 40 | * Activity status types |
| 41 | */ |
| 42 | const |
| 43 | INCOMPLETE = 0, |
| 44 | COMPLETED = 1, |
| 45 | CANCELLED = 2; |
| 46 | |
| 47 | /** |
| 48 | * Static field for all the activity information that we can potentially export. |
| 49 | * |
| 50 | * @var array |
| 51 | */ |
| 52 | public static $_exportableFields = NULL; |
| 53 | |
| 54 | /** |
| 55 | * Static field for all the activity information that we can potentially import. |
| 56 | * |
| 57 | * @var array |
| 58 | */ |
| 59 | public static $_importableFields = NULL; |
| 60 | |
| 61 | /** |
| 62 | * Check if there is absolute minimum of data to add the object. |
| 63 | * |
| 64 | * @param array $params |
| 65 | * (reference ) an assoc array of name/value pairs. |
| 66 | * |
| 67 | * @return bool |
| 68 | */ |
| 69 | public static function dataExists(&$params) { |
| 70 | if (!empty($params['source_contact_id']) || !empty($params['id'])) { |
| 71 | return TRUE; |
| 72 | } |
| 73 | return FALSE; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @deprecated |
| 78 | * |
| 79 | * Fetch object based on array of properties. |
| 80 | * |
| 81 | * @param array $params |
| 82 | * (reference ) an assoc array of name/value pairs. |
| 83 | * @param array $defaults |
| 84 | * (reference ) an assoc array to hold the flattened values. |
| 85 | * |
| 86 | * @return CRM_Activity_DAO_Activity |
| 87 | */ |
| 88 | public static function retrieve(&$params, &$defaults) { |
| 89 | // this will bypass acls - use the api instead. |
| 90 | // @todo add deprecation logging to this function. |
| 91 | $activity = new CRM_Activity_DAO_Activity(); |
| 92 | $activity->copyValues($params); |
| 93 | |
| 94 | if ($activity->find(TRUE)) { |
| 95 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
| 96 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
| 97 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
| 98 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); |
| 99 | |
| 100 | // TODO: at some stage we'll have to deal |
| 101 | // with multiple values for assignees and targets, but |
| 102 | // for now, let's just fetch first row. |
| 103 | $defaults['assignee_contact'] = CRM_Activity_BAO_ActivityContact::retrieveContactIdsByActivityId($activity->id, $assigneeID); |
| 104 | $assignee_contact_names = CRM_Activity_BAO_ActivityContact::getNames($activity->id, $assigneeID); |
| 105 | $defaults['assignee_contact_value'] = implode('; ', $assignee_contact_names); |
| 106 | $sourceContactId = self::getActivityContact($activity->id, $sourceID); |
| 107 | if ($activity->activity_type_id != CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Bulk Email')) { |
| 108 | $defaults['target_contact'] = CRM_Activity_BAO_ActivityContact::retrieveContactIdsByActivityId($activity->id, $targetID); |
| 109 | $target_contact_names = CRM_Activity_BAO_ActivityContact::getNames($activity->id, $targetID); |
| 110 | $defaults['target_contact_value'] = implode('; ', $target_contact_names); |
| 111 | } |
| 112 | elseif (CRM_Core_Permission::check('access CiviMail') || |
| 113 | (CRM_Mailing_Info::workflowEnabled() && |
| 114 | CRM_Core_Permission::check('create mailings') |
| 115 | ) |
| 116 | ) { |
| 117 | $defaults['mailingId'] = CRM_Utils_System::url('civicrm/mailing/report', |
| 118 | "mid={$activity->source_record_id}&reset=1&atype={$activity->activity_type_id}&aid={$activity->id}&cid={$sourceContactId}&context=activity" |
| 119 | ); |
| 120 | } |
| 121 | else { |
| 122 | $defaults['target_contact_value'] = ts('(recipients)'); |
| 123 | } |
| 124 | |
| 125 | $sourceContactId = self::getActivityContact($activity->id, $sourceID); |
| 126 | $defaults['source_contact_id'] = $sourceContactId; |
| 127 | |
| 128 | if ($sourceContactId && |
| 129 | !CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', |
| 130 | $sourceContactId, |
| 131 | 'is_deleted' |
| 132 | ) |
| 133 | ) { |
| 134 | $defaults['source_contact'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', |
| 135 | $sourceContactId, |
| 136 | 'sort_name' |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | // Get case subject. |
| 141 | $defaults['case_subject'] = CRM_Case_BAO_Case::getCaseSubject($activity->id); |
| 142 | |
| 143 | CRM_Core_DAO::storeValues($activity, $defaults); |
| 144 | |
| 145 | return $activity; |
| 146 | } |
| 147 | return NULL; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Delete the activity. |
| 152 | * |
| 153 | * @param array $params |
| 154 | * @param bool $moveToTrash |
| 155 | * |
| 156 | * @return mixed |
| 157 | */ |
| 158 | public static function deleteActivity(&$params, $moveToTrash = FALSE) { |
| 159 | // CRM-9137 |
| 160 | if (!empty($params['id']) && !is_array($params['id'])) { |
| 161 | CRM_Utils_Hook::pre('delete', 'Activity', $params['id'], $params); |
| 162 | } |
| 163 | else { |
| 164 | CRM_Utils_Hook::pre('delete', 'Activity', NULL, $params); |
| 165 | } |
| 166 | |
| 167 | $transaction = new CRM_Core_Transaction(); |
| 168 | if (is_array(CRM_Utils_Array::value('source_record_id', $params))) { |
| 169 | $sourceRecordIds = implode(',', $params['source_record_id']); |
| 170 | } |
| 171 | else { |
| 172 | $sourceRecordIds = CRM_Utils_Array::value('source_record_id', $params); |
| 173 | } |
| 174 | |
| 175 | $result = NULL; |
| 176 | if (!$moveToTrash) { |
| 177 | if (!isset($params['id'])) { |
| 178 | if (is_array($params['activity_type_id'])) { |
| 179 | $activityTypes = implode(',', $params['activity_type_id']); |
| 180 | } |
| 181 | else { |
| 182 | $activityTypes = $params['activity_type_id']; |
| 183 | } |
| 184 | |
| 185 | $query = "DELETE FROM civicrm_activity WHERE source_record_id IN ({$sourceRecordIds}) AND activity_type_id IN ( {$activityTypes} )"; |
| 186 | $dao = CRM_Core_DAO::executeQuery($query); |
| 187 | } |
| 188 | else { |
| 189 | $activity = new CRM_Activity_DAO_Activity(); |
| 190 | $activity->copyValues($params); |
| 191 | $result = $activity->delete(); |
| 192 | |
| 193 | // CRM-8708 |
| 194 | $activity->case_id = CRM_Case_BAO_Case::getCaseIdByActivityId($activity->id); |
| 195 | |
| 196 | // CRM-13994 delete activity entity_tag |
| 197 | $query = "DELETE FROM civicrm_entity_tag WHERE entity_table = 'civicrm_activity' AND entity_id = {$activity->id}"; |
| 198 | $dao = CRM_Core_DAO::executeQuery($query); |
| 199 | } |
| 200 | } |
| 201 | else { |
| 202 | $activity = new CRM_Activity_DAO_Activity(); |
| 203 | $activity->copyValues($params); |
| 204 | |
| 205 | $activity->is_deleted = 1; |
| 206 | $result = $activity->save(); |
| 207 | |
| 208 | // CRM-4525 log activity delete |
| 209 | $logMsg = 'Case Activity deleted for'; |
| 210 | $msgs = []; |
| 211 | |
| 212 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
| 213 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
| 214 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); |
| 215 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
| 216 | $sourceContactId = self::getActivityContact($activity->id, $sourceID); |
| 217 | if ($sourceContactId) { |
| 218 | $msgs[] = " source={$sourceContactId}"; |
| 219 | } |
| 220 | |
| 221 | // get target contacts. |
| 222 | $targetContactIds = CRM_Activity_BAO_ActivityContact::getNames($activity->id, $targetID); |
| 223 | if (!empty($targetContactIds)) { |
| 224 | $msgs[] = " target =" . implode(',', array_keys($targetContactIds)); |
| 225 | } |
| 226 | // get assignee contacts. |
| 227 | $assigneeContactIds = CRM_Activity_BAO_ActivityContact::getNames($activity->id, $assigneeID); |
| 228 | if (!empty($assigneeContactIds)) { |
| 229 | $msgs[] = " assignee =" . implode(',', array_keys($assigneeContactIds)); |
| 230 | } |
| 231 | |
| 232 | $logMsg .= implode(', ', $msgs); |
| 233 | |
| 234 | self::logActivityAction($activity, $logMsg); |
| 235 | } |
| 236 | |
| 237 | // delete the recently created Activity |
| 238 | if ($result) { |
| 239 | $activityRecent = [ |
| 240 | 'id' => $activity->id, |
| 241 | 'type' => 'Activity', |
| 242 | ]; |
| 243 | CRM_Utils_Recent::del($activityRecent); |
| 244 | } |
| 245 | |
| 246 | $transaction->commit(); |
| 247 | if (isset($activity)) { |
| 248 | // CRM-8708 |
| 249 | $activity->case_id = CRM_Case_BAO_Case::getCaseIdByActivityId($activity->id); |
| 250 | CRM_Utils_Hook::post('delete', 'Activity', $activity->id, $activity); |
| 251 | } |
| 252 | |
| 253 | return $result; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Delete activity assignment record. |
| 258 | * |
| 259 | * @param int $activityId |
| 260 | * @param int $recordTypeID |
| 261 | */ |
| 262 | public static function deleteActivityContact($activityId, $recordTypeID = NULL) { |
| 263 | $activityContact = new CRM_Activity_BAO_ActivityContact(); |
| 264 | $activityContact->activity_id = $activityId; |
| 265 | if ($recordTypeID) { |
| 266 | $activityContact->record_type_id = $recordTypeID; |
| 267 | } |
| 268 | |
| 269 | // Let's check if activity contact record exits and then delete. |
| 270 | // Looks like delete leads to deadlock when multiple simultaneous |
| 271 | // requests are done. CRM-15470 |
| 272 | if ($activityContact->find()) { |
| 273 | $activityContact->delete(); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Process the activities. |
| 279 | * |
| 280 | * @param array $params |
| 281 | * Associated array of the submitted values. |
| 282 | * |
| 283 | * @throws CRM_Core_Exception |
| 284 | * |
| 285 | * @return CRM_Activity_BAO_Activity|null|object |
| 286 | */ |
| 287 | public static function create(&$params) { |
| 288 | // CRM-20958 - These fields are managed by MySQL triggers. Watch out for clients resaving stale timestamps. |
| 289 | unset($params['created_date']); |
| 290 | unset($params['modified_date']); |
| 291 | |
| 292 | // check required params |
| 293 | if (!self::dataExists($params)) { |
| 294 | throw new CRM_Core_Exception('Not enough data to create activity object'); |
| 295 | } |
| 296 | |
| 297 | $activity = new CRM_Activity_DAO_Activity(); |
| 298 | |
| 299 | if (isset($params['id']) && empty($params['id'])) { |
| 300 | unset($params['id']); |
| 301 | } |
| 302 | |
| 303 | if (empty($params['status_id']) && empty($params['activity_status_id']) && empty($params['id'])) { |
| 304 | if (isset($params['activity_date_time']) && |
| 305 | strcmp($params['activity_date_time'], CRM_Utils_Date::processDate(date('Ymd')) == -1) |
| 306 | ) { |
| 307 | $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'status_id', 'Completed'); |
| 308 | } |
| 309 | else { |
| 310 | $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'status_id', 'Scheduled'); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // Set priority to Normal for Auto-populated activities (for Cases) |
| 315 | if (CRM_Utils_Array::value('priority_id', $params) === NULL && |
| 316 | // if not set and not 0 |
| 317 | !CRM_Utils_Array::value('id', $params) |
| 318 | ) { |
| 319 | $priority = CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'priority_id'); |
| 320 | $params['priority_id'] = array_search('Normal', $priority); |
| 321 | } |
| 322 | |
| 323 | if (!empty($params['target_contact_id']) && is_array($params['target_contact_id'])) { |
| 324 | $params['target_contact_id'] = array_unique($params['target_contact_id']); |
| 325 | } |
| 326 | if (!empty($params['assignee_contact_id']) && is_array($params['assignee_contact_id'])) { |
| 327 | $params['assignee_contact_id'] = array_unique($params['assignee_contact_id']); |
| 328 | } |
| 329 | |
| 330 | // CRM-9137 |
| 331 | if (!empty($params['id'])) { |
| 332 | CRM_Utils_Hook::pre('edit', 'Activity', $activity->id, $params); |
| 333 | } |
| 334 | else { |
| 335 | CRM_Utils_Hook::pre('create', 'Activity', NULL, $params); |
| 336 | } |
| 337 | |
| 338 | $activity->copyValues($params); |
| 339 | if (isset($params['case_id'])) { |
| 340 | // CRM-8708, preserve case ID even though it's not part of the SQL model |
| 341 | $activity->case_id = $params['case_id']; |
| 342 | } |
| 343 | elseif (is_numeric($activity->id)) { |
| 344 | // CRM-8708, preserve case ID even though it's not part of the SQL model |
| 345 | $activity->case_id = CRM_Case_BAO_Case::getCaseIdByActivityId($activity->id); |
| 346 | } |
| 347 | |
| 348 | // start transaction |
| 349 | $transaction = new CRM_Core_Transaction(); |
| 350 | |
| 351 | $result = $activity->save(); |
| 352 | |
| 353 | if (is_a($result, 'CRM_Core_Error')) { |
| 354 | $transaction->rollback(); |
| 355 | return $result; |
| 356 | } |
| 357 | |
| 358 | $activityId = $activity->id; |
| 359 | $sourceID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Source'); |
| 360 | $assigneeID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Assignees'); |
| 361 | $targetID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Targets'); |
| 362 | |
| 363 | if (isset($params['source_contact_id'])) { |
| 364 | $acParams = [ |
| 365 | 'activity_id' => $activityId, |
| 366 | 'contact_id' => $params['source_contact_id'], |
| 367 | 'record_type_id' => $sourceID, |
| 368 | ]; |
| 369 | self::deleteActivityContact($activityId, $sourceID); |
| 370 | CRM_Activity_BAO_ActivityContact::create($acParams); |
| 371 | } |
| 372 | |
| 373 | // check and attach and files as needed |
| 374 | CRM_Core_BAO_File::processAttachment($params, 'civicrm_activity', $activityId); |
| 375 | |
| 376 | // attempt to save activity assignment |
| 377 | $resultAssignment = NULL; |
| 378 | if (!empty($params['assignee_contact_id'])) { |
| 379 | |
| 380 | $assignmentParams = ['activity_id' => $activityId]; |
| 381 | |
| 382 | if (is_array($params['assignee_contact_id'])) { |
| 383 | if (CRM_Utils_Array::value('deleteActivityAssignment', $params, TRUE)) { |
| 384 | // first delete existing assignments if any |
| 385 | self::deleteActivityContact($activityId, $assigneeID); |
| 386 | } |
| 387 | |
| 388 | foreach ($params['assignee_contact_id'] as $acID) { |
| 389 | if ($acID) { |
| 390 | $assigneeParams = [ |
| 391 | 'activity_id' => $activityId, |
| 392 | 'contact_id' => $acID, |
| 393 | 'record_type_id' => $assigneeID, |
| 394 | ]; |
| 395 | CRM_Activity_BAO_ActivityContact::create($assigneeParams); |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | else { |
| 400 | $assignmentParams['contact_id'] = $params['assignee_contact_id']; |
| 401 | $assignmentParams['record_type_id'] = $assigneeID; |
| 402 | if (!empty($params['id'])) { |
| 403 | $assignment = new CRM_Activity_BAO_ActivityContact(); |
| 404 | $assignment->activity_id = $activityId; |
| 405 | $assignment->record_type_id = $assigneeID; |
| 406 | $assignment->find(TRUE); |
| 407 | |
| 408 | if ($assignment->contact_id != $params['assignee_contact_id']) { |
| 409 | $assignmentParams['id'] = $assignment->id; |
| 410 | $resultAssignment = CRM_Activity_BAO_ActivityContact::create($assignmentParams); |
| 411 | } |
| 412 | } |
| 413 | else { |
| 414 | $resultAssignment = CRM_Activity_BAO_ActivityContact::create($assignmentParams); |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | else { |
| 419 | if (CRM_Utils_Array::value('deleteActivityAssignment', $params, TRUE)) { |
| 420 | self::deleteActivityContact($activityId, $assigneeID); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | if (is_a($resultAssignment, 'CRM_Core_Error')) { |
| 425 | $transaction->rollback(); |
| 426 | return $resultAssignment; |
| 427 | } |
| 428 | |
| 429 | // attempt to save activity targets |
| 430 | $resultTarget = NULL; |
| 431 | if (!empty($params['target_contact_id'])) { |
| 432 | |
| 433 | $targetParams = ['activity_id' => $activityId]; |
| 434 | $resultTarget = []; |
| 435 | if (is_array($params['target_contact_id'])) { |
| 436 | if (CRM_Utils_Array::value('deleteActivityTarget', $params, TRUE)) { |
| 437 | // first delete existing targets if any |
| 438 | self::deleteActivityContact($activityId, $targetID); |
| 439 | } |
| 440 | |
| 441 | foreach ($params['target_contact_id'] as $tid) { |
| 442 | if ($tid) { |
| 443 | $targetContactParams = [ |
| 444 | 'activity_id' => $activityId, |
| 445 | 'contact_id' => $tid, |
| 446 | 'record_type_id' => $targetID, |
| 447 | ]; |
| 448 | CRM_Activity_BAO_ActivityContact::create($targetContactParams); |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | else { |
| 453 | $targetParams['contact_id'] = $params['target_contact_id']; |
| 454 | $targetParams['record_type_id'] = $targetID; |
| 455 | if (!empty($params['id'])) { |
| 456 | $target = new CRM_Activity_BAO_ActivityContact(); |
| 457 | $target->activity_id = $activityId; |
| 458 | $target->record_type_id = $targetID; |
| 459 | $target->find(TRUE); |
| 460 | |
| 461 | if ($target->contact_id != $params['target_contact_id']) { |
| 462 | $targetParams['id'] = $target->id; |
| 463 | $resultTarget = CRM_Activity_BAO_ActivityContact::create($targetParams); |
| 464 | } |
| 465 | } |
| 466 | else { |
| 467 | $resultTarget = CRM_Activity_BAO_ActivityContact::create($targetParams); |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | else { |
| 472 | if (CRM_Utils_Array::value('deleteActivityTarget', $params, TRUE)) { |
| 473 | self::deleteActivityContact($activityId, $targetID); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | // write to changelog before transaction is committed/rolled |
| 478 | // back (and prepare status to display) |
| 479 | if (!empty($params['id'])) { |
| 480 | $logMsg = "Activity (id: {$result->id} ) updated with "; |
| 481 | } |
| 482 | else { |
| 483 | $logMsg = "Activity created for "; |
| 484 | } |
| 485 | |
| 486 | $msgs = []; |
| 487 | if (isset($params['source_contact_id'])) { |
| 488 | $msgs[] = "source={$params['source_contact_id']}"; |
| 489 | } |
| 490 | |
| 491 | if (!empty($params['target_contact_id'])) { |
| 492 | if (is_array($params['target_contact_id']) && !CRM_Utils_Array::crmIsEmptyArray($params['target_contact_id'])) { |
| 493 | $msgs[] = "target=" . implode(',', $params['target_contact_id']); |
| 494 | // take only first target |
| 495 | // will be used for recently viewed display |
| 496 | $t = array_slice($params['target_contact_id'], 0, 1); |
| 497 | $recentContactId = $t[0]; |
| 498 | } |
| 499 | // Is array check fixes warning without degrading functionality but it seems this bit of code may no longer work |
| 500 | // as it may always be an array |
| 501 | elseif (isset($params['target_contact_id']) && !is_array($params['target_contact_id'])) { |
| 502 | $msgs[] = "target={$params['target_contact_id']}"; |
| 503 | // will be used for recently viewed display |
| 504 | $recentContactId = $params['target_contact_id']; |
| 505 | } |
| 506 | } |
| 507 | else { |
| 508 | // at worst, take source for recently viewed display |
| 509 | $recentContactId = CRM_Utils_Array::value('source_contact_id', $params); |
| 510 | } |
| 511 | |
| 512 | if (isset($params['assignee_contact_id'])) { |
| 513 | if (is_array($params['assignee_contact_id'])) { |
| 514 | $msgs[] = "assignee=" . implode(',', $params['assignee_contact_id']); |
| 515 | } |
| 516 | else { |
| 517 | $msgs[] = "assignee={$params['assignee_contact_id']}"; |
| 518 | } |
| 519 | } |
| 520 | $logMsg .= implode(', ', $msgs); |
| 521 | |
| 522 | self::logActivityAction($result, $logMsg); |
| 523 | |
| 524 | if (!empty($params['custom']) && |
| 525 | is_array($params['custom']) |
| 526 | ) { |
| 527 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_activity', $result->id); |
| 528 | } |
| 529 | |
| 530 | $transaction->commit(); |
| 531 | if (empty($params['skipRecentView'])) { |
| 532 | $recentOther = []; |
| 533 | if (!empty($params['case_id'])) { |
| 534 | $caseContactID = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseContact', $params['case_id'], 'contact_id', 'case_id'); |
| 535 | $url = CRM_Utils_System::url('civicrm/case/activity/view', |
| 536 | "reset=1&aid={$activity->id}&cid={$caseContactID}&caseID={$params['case_id']}&context=home" |
| 537 | ); |
| 538 | } |
| 539 | else { |
| 540 | $q = "action=view&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid=" . CRM_Utils_Array::value('source_contact_id', $params) . "&context=home"; |
| 541 | if ($activity->activity_type_id != CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Email')) { |
| 542 | $url = CRM_Utils_System::url('civicrm/activity', $q); |
| 543 | if ($activity->activity_type_id == CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Print PDF Letter')) { |
| 544 | $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/activity/pdf/add', |
| 545 | "action=update&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid={$params['source_contact_id']}&context=home" |
| 546 | ); |
| 547 | } |
| 548 | else { |
| 549 | $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/activity/add', |
| 550 | "action=update&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid=" . CRM_Utils_Array::value('source_contact_id', $params) . "&context=home" |
| 551 | ); |
| 552 | } |
| 553 | |
| 554 | if (CRM_Core_Permission::check("delete activities")) { |
| 555 | $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/activity', |
| 556 | "action=delete&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid=" . CRM_Utils_Array::value('source_contact_id', $params) . "&context=home" |
| 557 | ); |
| 558 | } |
| 559 | } |
| 560 | else { |
| 561 | $url = CRM_Utils_System::url('civicrm/activity/view', $q); |
| 562 | if (CRM_Core_Permission::check('delete activities')) { |
| 563 | $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/activity', |
| 564 | "action=delete&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid=" . CRM_Utils_Array::value('source_contact_id', $params) . "&context=home" |
| 565 | ); |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | if (!isset($activity->parent_id)) { |
| 571 | $recentContactDisplay = CRM_Contact_BAO_Contact::displayName($recentContactId); |
| 572 | // add the recently created Activity |
| 573 | $activityTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id'); |
| 574 | $activitySubject = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $activity->id, 'subject'); |
| 575 | |
| 576 | $title = ""; |
| 577 | if (isset($activitySubject)) { |
| 578 | $title = $activitySubject . ' - '; |
| 579 | } |
| 580 | |
| 581 | $title = $title . $recentContactDisplay; |
| 582 | if (!empty($activityTypes[$activity->activity_type_id])) { |
| 583 | $title .= ' (' . $activityTypes[$activity->activity_type_id] . ')'; |
| 584 | } |
| 585 | |
| 586 | CRM_Utils_Recent::add($title, |
| 587 | $url, |
| 588 | $activity->id, |
| 589 | 'Activity', |
| 590 | $recentContactId, |
| 591 | $recentContactDisplay, |
| 592 | $recentOther |
| 593 | ); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | // reset the group contact cache since smart groups might be affected due to this |
| 598 | // CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush(); |
| 599 | |
| 600 | // if the subject contains a ‘[case #…]’ string, file that activity on the related case (CRM-5916) |
| 601 | $matches = []; |
| 602 | $subjectToMatch = CRM_Utils_Array::value('subject', $params); |
| 603 | if (preg_match('/\[case #([0-9a-h]{7})\]/', $subjectToMatch, $matches)) { |
| 604 | $key = CRM_Core_DAO::escapeString(CIVICRM_SITE_KEY); |
| 605 | $hash = $matches[1]; |
| 606 | $query = "SELECT id FROM civicrm_case WHERE SUBSTR(SHA1(CONCAT('$key', id)), 1, 7) = '" . CRM_Core_DAO::escapeString($hash) . "'"; |
| 607 | } |
| 608 | elseif (preg_match('/\[case #(\d+)\]/', $subjectToMatch, $matches)) { |
| 609 | $query = "SELECT id FROM civicrm_case WHERE id = '" . CRM_Core_DAO::escapeString($matches[1]) . "'"; |
| 610 | } |
| 611 | if (!empty($matches)) { |
| 612 | $caseParams = [ |
| 613 | 'activity_id' => $activity->id, |
| 614 | 'case_id' => CRM_Core_DAO::singleValueQuery($query), |
| 615 | ]; |
| 616 | if ($caseParams['case_id']) { |
| 617 | CRM_Case_BAO_Case::processCaseActivity($caseParams); |
| 618 | } |
| 619 | else { |
| 620 | self::logActivityAction($activity, "Case details for {$matches[1]} not found while recording an activity on case."); |
| 621 | } |
| 622 | } |
| 623 | if (!empty($params['id'])) { |
| 624 | CRM_Utils_Hook::post('edit', 'Activity', $activity->id, $activity); |
| 625 | } |
| 626 | else { |
| 627 | CRM_Utils_Hook::post('create', 'Activity', $activity->id, $activity); |
| 628 | } |
| 629 | |
| 630 | return $result; |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * Create an activity. |
| 635 | * |
| 636 | * @todo elaborate on what this does. |
| 637 | * |
| 638 | * @param CRM_Core_DAO_Activity $activity |
| 639 | * @param string $logMessage |
| 640 | * |
| 641 | * @return bool |
| 642 | */ |
| 643 | public static function logActivityAction($activity, $logMessage = NULL) { |
| 644 | $id = CRM_Core_Session::getLoggedInContactID(); |
| 645 | if (!$id) { |
| 646 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
| 647 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
| 648 | $id = self::getActivityContact($activity->id, $sourceID); |
| 649 | } |
| 650 | $logParams = [ |
| 651 | 'entity_table' => 'civicrm_activity', |
| 652 | 'entity_id' => $activity->id, |
| 653 | 'modified_id' => $id, |
| 654 | 'modified_date' => date('YmdHis'), |
| 655 | 'data' => $logMessage, |
| 656 | ]; |
| 657 | CRM_Core_BAO_Log::add($logParams); |
| 658 | return TRUE; |
| 659 | } |
| 660 | |
| 661 | /** |
| 662 | * Get the list Activities. |
| 663 | * |
| 664 | * @param array $params |
| 665 | * Array of parameters. |
| 666 | * Keys include |
| 667 | * - contact_id int contact_id whose activities we want to retrieve |
| 668 | * - offset int which row to start from ? |
| 669 | * - rowCount int how many rows to fetch |
| 670 | * - sort object|array object or array describing sort order for sql query. |
| 671 | * - admin boolean if contact is admin |
| 672 | * - caseId int case ID |
| 673 | * - context string page on which selector is build |
| 674 | * - activity_type_id int|string the activitiy types we want to restrict by |
| 675 | * |
| 676 | * @return array |
| 677 | * Relevant data object values of open activities |
| 678 | * @throws \CiviCRM_API3_Exception |
| 679 | */ |
| 680 | public static function getActivities($params) { |
| 681 | $activities = []; |
| 682 | |
| 683 | // Activity.Get API params |
| 684 | $activityParams = self::getActivityParamsForDashboardFunctions($params); |
| 685 | |
| 686 | if (!empty($params['rowCount']) && |
| 687 | $params['rowCount'] > 0 |
| 688 | ) { |
| 689 | $activityParams['options']['limit'] = $params['rowCount']; |
| 690 | } |
| 691 | |
| 692 | if (!empty($params['sort'])) { |
| 693 | if (is_a($params['sort'], 'CRM_Utils_Sort')) { |
| 694 | $order = $params['sort']->orderBy(); |
| 695 | } |
| 696 | elseif (trim($params['sort'])) { |
| 697 | $order = CRM_Utils_Type::escape($params['sort'], 'String'); |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | $activityParams['options']['sort'] = empty($order) ? "activity_date_time DESC" : str_replace('activity_type ', 'activity_type_id.label ', $order); |
| 702 | |
| 703 | $activityParams['return'] = [ |
| 704 | 'activity_date_time', |
| 705 | 'source_record_id', |
| 706 | 'source_contact_id', |
| 707 | 'source_contact_name', |
| 708 | 'assignee_contact_id', |
| 709 | 'assignee_contact_name', |
| 710 | 'status_id', |
| 711 | 'subject', |
| 712 | 'activity_type_id', |
| 713 | 'activity_type', |
| 714 | 'case_id', |
| 715 | 'campaign_id', |
| 716 | ]; |
| 717 | foreach (['case_id' => 'CiviCase', 'campaign_id' => 'CiviCampaign'] as $attr => $component) { |
| 718 | if (in_array($component, self::activityComponents())) { |
| 719 | $activityParams['return'][] = $attr; |
| 720 | } |
| 721 | } |
| 722 | $result = civicrm_api3('Activity', 'Get', $activityParams)['values']; |
| 723 | |
| 724 | $bulkActivityTypeID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Bulk Email'); |
| 725 | $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE); |
| 726 | |
| 727 | // CRM-3553, need to check user has access to target groups. |
| 728 | $mailingIDs = CRM_Mailing_BAO_Mailing::mailingACLIDs(); |
| 729 | $accessCiviMail = ((CRM_Core_Permission::check('access CiviMail')) || |
| 730 | (CRM_Mailing_Info::workflowEnabled() && CRM_Core_Permission::check('create mailings')) |
| 731 | ); |
| 732 | |
| 733 | // @todo - get rid of this & just handle in the array declaration like we do with 'subject' etc. |
| 734 | $mappingParams = [ |
| 735 | 'source_record_id' => 'source_record_id', |
| 736 | 'activity_type_id' => 'activity_type_id', |
| 737 | 'status_id' => 'status_id', |
| 738 | 'campaign_id' => 'campaign_id', |
| 739 | 'case_id' => 'case_id', |
| 740 | ]; |
| 741 | |
| 742 | if (empty($result)) { |
| 743 | $targetCount = []; |
| 744 | } |
| 745 | else { |
| 746 | $targetCount = CRM_Core_DAO::executeQuery(' |
| 747 | SELECT activity_id, count(*) as target_contact_count |
| 748 | FROM civicrm_activity_contact |
| 749 | INNER JOIN civicrm_contact c ON contact_id = c.id AND c.is_deleted = 0 |
| 750 | WHERE activity_id IN (' . implode(',', array_keys($result)) . ') |
| 751 | AND record_type_id = %1 |
| 752 | GROUP BY activity_id', [ |
| 753 | 1 => [ |
| 754 | CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Targets'), |
| 755 | 'Integer' |
| 756 | ] |
| 757 | ])->fetchAll(); |
| 758 | } |
| 759 | foreach ($targetCount as $activityTarget) { |
| 760 | $result[$activityTarget['activity_id']]['target_contact_count'] = $activityTarget['target_contact_count']; |
| 761 | } |
| 762 | // Iterate through & do basic mappings & determine which ones we want to retrieve target count for. |
| 763 | foreach ($result as $id => $activity) { |
| 764 | $activities[$id] = [ |
| 765 | 'activity_id' => $activity['id'], |
| 766 | 'activity_date_time' => CRM_Utils_Array::value('activity_date_time', $activity), |
| 767 | 'subject' => CRM_Utils_Array::value('subject', $activity), |
| 768 | 'assignee_contact_name' => CRM_Utils_Array::value('assignee_contact_sort_name', $activity, []), |
| 769 | 'source_contact_id' => CRM_Utils_Array::value('source_contact_id', $activity), |
| 770 | 'source_contact_name' => CRM_Utils_Array::value('source_contact_sort_name', $activity), |
| 771 | ]; |
| 772 | $activities[$id]['activity_type_name'] = CRM_Core_PseudoConstant::getName('CRM_Activity_BAO_Activity', 'activity_type_id', $activity['activity_type_id']); |
| 773 | $activities[$id]['activity_type'] = CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'activity_type_id', $activity['activity_type_id']); |
| 774 | $activities[$id]['target_contact_count'] = CRM_Utils_Array::value('target_contact_count', $activity, 0); |
| 775 | if (!empty($activity['target_contact_count'])) { |
| 776 | $displayedTarget = civicrm_api3('ActivityContact', 'get', [ |
| 777 | 'activity_id' => $id, |
| 778 | 'check_permissions' => TRUE, |
| 779 | 'options' => ['limit' => 1], |
| 780 | 'record_type_id' => 'Activity Targets', |
| 781 | 'return' => ['contact_id.sort_name', 'contact_id'], |
| 782 | 'sequential' => 1, |
| 783 | ])['values']; |
| 784 | if (empty($displayedTarget[0])) { |
| 785 | $activities[$id]['target_contact_name'] = []; |
| 786 | } |
| 787 | else { |
| 788 | $activities[$id]['target_contact_name'] = [$displayedTarget[0]['contact_id'] => $displayedTarget[0]['contact_id.sort_name']]; |
| 789 | } |
| 790 | } |
| 791 | if ($activities[$id]['activity_type_name'] === 'Bulk Email') { |
| 792 | $bulkActivities[] = $id; |
| 793 | // Get the total without permissions being passed but only display names after permissioning. |
| 794 | $activities[$id]['recipients'] = ts('(%1 recipients)', [1 => $activities[$id]['target_contact_count']]); |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | // Eventually this second iteration should just handle the target contacts. It's a bit muddled at |
| 799 | // the moment as the bulk activity stuff needs unravelling & test coverage. |
| 800 | foreach ($result as $id => $activity) { |
| 801 | $isBulkActivity = (!$bulkActivityTypeID || ($bulkActivityTypeID === $activity['activity_type_id'])); |
| 802 | foreach ($mappingParams as $apiKey => $expectedName) { |
| 803 | if (in_array($apiKey, [ |
| 804 | 'target_contact_name', |
| 805 | ])) { |
| 806 | |
| 807 | if ($isBulkActivity) { |
| 808 | // @todo - how is this used? Couldn't we use 'is_bulk' or something clearer? |
| 809 | // or the calling function could handle |
| 810 | $activities[$id]['mailingId'] = FALSE; |
| 811 | if ($accessCiviMail && |
| 812 | ($mailingIDs === TRUE || in_array($activity['source_record_id'], $mailingIDs)) |
| 813 | ) { |
| 814 | $activities[$id]['mailingId'] = TRUE; |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | // case related fields |
| 819 | elseif ($apiKey == 'case_id' && !$isBulkActivity) { |
| 820 | $activities[$id][$expectedName] = CRM_Utils_Array::value($apiKey, $activity); |
| 821 | |
| 822 | // fetch case subject for case ID found |
| 823 | if (!empty($activity['case_id'])) { |
| 824 | $activities[$id]['case_subject'] = CRM_Core_DAO::executeQuery('CRM_Case_DAO_Case', $activity['case_id'], 'subject'); |
| 825 | } |
| 826 | } |
| 827 | else { |
| 828 | // @todo this generic assign could just be handled in array declaration earlier. |
| 829 | $activities[$id][$expectedName] = CRM_Utils_Array::value($apiKey, $activity); |
| 830 | if ($apiKey == 'campaign_id') { |
| 831 | $activities[$id]['campaign'] = CRM_Utils_Array::value($activities[$id][$expectedName], $allCampaigns); |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | // if deleted, wrap in <del> |
| 836 | if (!empty($activity['source_contact_id']) && |
| 837 | CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $activity['source_contact_id'], 'is_deleted') |
| 838 | ) { |
| 839 | $activities[$id]['source_contact_name'] = sprintf("<del>%s<del>", $activity['source_contact_name']); |
| 840 | } |
| 841 | $activities[$id]['is_recurring_activity'] = CRM_Core_BAO_RecurringEntity::getParentFor($id, 'civicrm_activity'); |
| 842 | } |
| 843 | |
| 844 | return $activities; |
| 845 | } |
| 846 | |
| 847 | /** |
| 848 | * Filter the activity types to only return the ones we actually asked for |
| 849 | * Uses params['activity_type_id'] and params['activity_type_exclude_id'] |
| 850 | * |
| 851 | * @param $params |
| 852 | * @return array|null (Use in Activity.get API activity_type_id) |
| 853 | */ |
| 854 | public static function filterActivityTypes($params) { |
| 855 | $activityTypes = []; |
| 856 | |
| 857 | // If no activity types are specified, get all the active ones |
| 858 | if (empty($params['activity_type_id'])) { |
| 859 | $activityTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id', 'get'); |
| 860 | } |
| 861 | |
| 862 | // If no activity types are specified or excluded, return the list of all active ones |
| 863 | if (empty($params['activity_type_id']) && empty($params['activity_type_exclude_id'])) { |
| 864 | if (!empty($activityTypes)) { |
| 865 | return ['IN' => array_keys($activityTypes)]; |
| 866 | } |
| 867 | return NULL; |
| 868 | } |
| 869 | |
| 870 | // If we have specified activity types, build a list to return, excluding the ones we don't want. |
| 871 | if (!empty($params['activity_type_id'])) { |
| 872 | if (!is_array($params['activity_type_id'])) { |
| 873 | // Turn it into array if only one specified, so we don't duplicate processing below |
| 874 | $params['activity_type_id'] = [$params['activity_type_id'] => $params['activity_type_id']]; |
| 875 | } |
| 876 | foreach ($params['activity_type_id'] as $value) { |
| 877 | // Add each activity type that was specified to list |
| 878 | $value = CRM_Utils_Type::escape($value, 'Positive'); |
| 879 | $activityTypes[$value] = $value; |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | // Build the list of activity types to exclude (from $params['activity_type_exclude_id']) |
| 884 | if (!empty($params['activity_type_exclude_id'])) { |
| 885 | if (!is_array($params['activity_type_exclude_id'])) { |
| 886 | // Turn it into array if only one specified, so we don't duplicate processing below |
| 887 | $params['activity_type_exclude_id'] = [$params['activity_type_exclude_id'] => $params['activity_type_exclude_id']]; |
| 888 | } |
| 889 | foreach ($params['activity_type_exclude_id'] as $value) { |
| 890 | // Remove each activity type from list if it should be excluded |
| 891 | $value = CRM_Utils_Type::escape($value, 'Positive'); |
| 892 | if (array_key_exists($value, $activityTypes)) { |
| 893 | unset($activityTypes[$value]); |
| 894 | } |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | return ['IN' => array_keys($activityTypes)]; |
| 899 | } |
| 900 | |
| 901 | /** |
| 902 | * @inheritDoc |
| 903 | */ |
| 904 | public function addSelectWhereClause() { |
| 905 | $clauses = []; |
| 906 | $permittedActivityTypeIDs = self::getPermittedActivityTypes(); |
| 907 | if (empty($permittedActivityTypeIDs)) { |
| 908 | // This just prevents a mysql fail if they have no access - should be extremely edge case. |
| 909 | $permittedActivityTypeIDs = [0]; |
| 910 | } |
| 911 | $clauses['activity_type_id'] = ('IN (' . implode(', ', $permittedActivityTypeIDs) . ')'); |
| 912 | |
| 913 | $contactClause = CRM_Utils_SQL::mergeSubquery('Contact'); |
| 914 | if ($contactClause) { |
| 915 | $contactClause = implode(' AND contact_id ', $contactClause); |
| 916 | $clauses['id'][] = "IN (SELECT activity_id FROM civicrm_activity_contact WHERE contact_id $contactClause)"; |
| 917 | } |
| 918 | CRM_Utils_Hook::selectWhereClause($this, $clauses); |
| 919 | return $clauses; |
| 920 | } |
| 921 | |
| 922 | /** |
| 923 | * Get an array of components that are accessible by the currenct user. |
| 924 | * |
| 925 | * This means checking if they are enabled and if the user has appropriate permission. |
| 926 | * |
| 927 | * For most components the permission is access component (e.g 'access CiviContribute'). |
| 928 | * Exceptions as CiviCampaign (administer CiviCampaign) and CiviCase |
| 929 | * (accesses a case function which enforces edit all cases or edit my cases. Case |
| 930 | * permissions are also handled on a per activity basis). |
| 931 | * |
| 932 | * Checks whether logged in user has permission to the component. |
| 933 | * |
| 934 | * @param bool $excludeComponentHandledActivities |
| 935 | * Should we exclude components whose display is handled in the components. |
| 936 | * In practice this means should we include CiviCase in the results. Presumbaly |
| 937 | * at the time it was decided case activities should be shown in the case framework and |
| 938 | * that this concept might be extended later. In practice most places that |
| 939 | * call this then re-add CiviCase in some way so it's all a bit... odd. |
| 940 | * |
| 941 | * @return array |
| 942 | * Array of component id and name. |
| 943 | */ |
| 944 | public static function activityComponents($excludeComponentHandledActivities = TRUE) { |
| 945 | $components = []; |
| 946 | $compInfo = CRM_Core_Component::getEnabledComponents(); |
| 947 | foreach ($compInfo as $compObj) { |
| 948 | $includeComponent = !$excludeComponentHandledActivities || !empty($compObj->info['showActivitiesInCore']); |
| 949 | if ($includeComponent) { |
| 950 | if ($compObj->info['name'] == 'CiviCampaign') { |
| 951 | $componentPermission = "administer {$compObj->name}"; |
| 952 | } |
| 953 | else { |
| 954 | $componentPermission = "access {$compObj->name}"; |
| 955 | } |
| 956 | if ($compObj->info['name'] == 'CiviCase') { |
| 957 | if (CRM_Case_BAO_Case::accessCiviCase()) { |
| 958 | $components[$compObj->componentID] = $compObj->info['name']; |
| 959 | } |
| 960 | } |
| 961 | elseif (CRM_Core_Permission::check($componentPermission)) { |
| 962 | $components[$compObj->componentID] = $compObj->info['name']; |
| 963 | } |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | return $components; |
| 968 | } |
| 969 | |
| 970 | /** |
| 971 | * Get the activity Count. |
| 972 | * |
| 973 | * @param array $input |
| 974 | * Array of parameters. |
| 975 | * Keys include |
| 976 | * - contact_id int contact_id whose activities we want to retrieve |
| 977 | * - admin boolean if contact is admin |
| 978 | * - caseId int case ID |
| 979 | * - context string page on which selector is build |
| 980 | * - activity_type_id int|string the activity types we want to restrict by |
| 981 | * |
| 982 | * @return int |
| 983 | * count of activities |
| 984 | */ |
| 985 | public static function getActivitiesCount($input) { |
| 986 | $activityParams = self::getActivityParamsForDashboardFunctions($input); |
| 987 | return civicrm_api3('Activity', 'getcount', $activityParams); |
| 988 | } |
| 989 | |
| 990 | /** |
| 991 | * Send the message to all the contacts. |
| 992 | * |
| 993 | * Also insert a contact activity in each contacts record. |
| 994 | * |
| 995 | * @param array $contactDetails |
| 996 | * The array of contact details to send the email. |
| 997 | * @param string $subject |
| 998 | * The subject of the message. |
| 999 | * @param $text |
| 1000 | * @param $html |
| 1001 | * @param string $emailAddress |
| 1002 | * Use this 'to' email address instead of the default Primary address. |
| 1003 | * @param int $userID |
| 1004 | * Use this userID if set. |
| 1005 | * @param string $from |
| 1006 | * @param array $attachments |
| 1007 | * The array of attachments if any. |
| 1008 | * @param string $cc |
| 1009 | * Cc recipient. |
| 1010 | * @param string $bcc |
| 1011 | * Bcc recipient. |
| 1012 | * @param array $contactIds |
| 1013 | * Contact ids. |
| 1014 | * @param string $additionalDetails |
| 1015 | * The additional information of CC and BCC appended to the activity Details. |
| 1016 | * @param array $contributionIds |
| 1017 | * @param int $campaignId |
| 1018 | * |
| 1019 | * @return array |
| 1020 | * ( sent, activityId) if any email is sent and activityId |
| 1021 | */ |
| 1022 | public static function sendEmail( |
| 1023 | &$contactDetails, |
| 1024 | &$subject, |
| 1025 | &$text, |
| 1026 | &$html, |
| 1027 | $emailAddress, |
| 1028 | $userID = NULL, |
| 1029 | $from = NULL, |
| 1030 | $attachments = NULL, |
| 1031 | $cc = NULL, |
| 1032 | $bcc = NULL, |
| 1033 | $contactIds = NULL, |
| 1034 | $additionalDetails = NULL, |
| 1035 | $contributionIds = NULL, |
| 1036 | $campaignId = NULL |
| 1037 | ) { |
| 1038 | // get the contact details of logged in contact, which we set as from email |
| 1039 | if ($userID == NULL) { |
| 1040 | $userID = CRM_Core_Session::getLoggedInContactID(); |
| 1041 | } |
| 1042 | |
| 1043 | list($fromDisplayName, $fromEmail, $fromDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($userID); |
| 1044 | if (!$fromEmail) { |
| 1045 | return [count($contactDetails), 0, count($contactDetails)]; |
| 1046 | } |
| 1047 | if (!trim($fromDisplayName)) { |
| 1048 | $fromDisplayName = $fromEmail; |
| 1049 | } |
| 1050 | |
| 1051 | // CRM-4575 |
| 1052 | // token replacement of addressee/email/postal greetings |
| 1053 | // get the tokens added in subject and message |
| 1054 | $subjectToken = CRM_Utils_Token::getTokens($subject); |
| 1055 | $messageToken = CRM_Utils_Token::getTokens($text); |
| 1056 | $messageToken = array_merge($messageToken, CRM_Utils_Token::getTokens($html)); |
| 1057 | $allTokens = array_merge($messageToken, $subjectToken); |
| 1058 | |
| 1059 | if (!$from) { |
| 1060 | $from = "$fromDisplayName <$fromEmail>"; |
| 1061 | } |
| 1062 | |
| 1063 | //create the meta level record first ( email activity ) |
| 1064 | $activityTypeID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Email'); |
| 1065 | |
| 1066 | // CRM-6265: save both text and HTML parts in details (if present) |
| 1067 | if ($html and $text) { |
| 1068 | $details = "-ALTERNATIVE ITEM 0-\n$html$additionalDetails\n-ALTERNATIVE ITEM 1-\n$text$additionalDetails\n-ALTERNATIVE END-\n"; |
| 1069 | } |
| 1070 | else { |
| 1071 | $details = $html ? $html : $text; |
| 1072 | $details .= $additionalDetails; |
| 1073 | } |
| 1074 | |
| 1075 | $activityParams = [ |
| 1076 | 'source_contact_id' => $userID, |
| 1077 | 'activity_type_id' => $activityTypeID, |
| 1078 | 'activity_date_time' => date('YmdHis'), |
| 1079 | 'subject' => $subject, |
| 1080 | 'details' => $details, |
| 1081 | // FIXME: check for name Completed and get ID from that lookup |
| 1082 | 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'status_id', 'Completed'), |
| 1083 | 'campaign_id' => $campaignId, |
| 1084 | ]; |
| 1085 | |
| 1086 | // CRM-5916: strip [case #…] before saving the activity (if present in subject) |
| 1087 | $activityParams['subject'] = preg_replace('/\[case #([0-9a-h]{7})\] /', '', $activityParams['subject']); |
| 1088 | |
| 1089 | // add the attachments to activity params here |
| 1090 | if ($attachments) { |
| 1091 | // first process them |
| 1092 | $activityParams = array_merge($activityParams, |
| 1093 | $attachments |
| 1094 | ); |
| 1095 | } |
| 1096 | |
| 1097 | $activity = self::create($activityParams); |
| 1098 | |
| 1099 | // get the set of attachments from where they are stored |
| 1100 | $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_activity', |
| 1101 | $activity->id |
| 1102 | ); |
| 1103 | $returnProperties = []; |
| 1104 | if (isset($messageToken['contact'])) { |
| 1105 | foreach ($messageToken['contact'] as $key => $value) { |
| 1106 | $returnProperties[$value] = 1; |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | if (isset($subjectToken['contact'])) { |
| 1111 | foreach ($subjectToken['contact'] as $key => $value) { |
| 1112 | if (!isset($returnProperties[$value])) { |
| 1113 | $returnProperties[$value] = 1; |
| 1114 | } |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | // get token details for contacts, call only if tokens are used |
| 1119 | $details = []; |
| 1120 | if (!empty($returnProperties) || !empty($tokens) || !empty($allTokens)) { |
| 1121 | list($details) = CRM_Utils_Token::getTokenDetails( |
| 1122 | $contactIds, |
| 1123 | $returnProperties, |
| 1124 | NULL, NULL, FALSE, |
| 1125 | $allTokens, |
| 1126 | 'CRM_Activity_BAO_Activity' |
| 1127 | ); |
| 1128 | } |
| 1129 | |
| 1130 | // call token hook |
| 1131 | $tokens = []; |
| 1132 | CRM_Utils_Hook::tokens($tokens); |
| 1133 | $categories = array_keys($tokens); |
| 1134 | |
| 1135 | $escapeSmarty = FALSE; |
| 1136 | if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) { |
| 1137 | $smarty = CRM_Core_Smarty::singleton(); |
| 1138 | $escapeSmarty = TRUE; |
| 1139 | } |
| 1140 | |
| 1141 | $contributionDetails = []; |
| 1142 | if (!empty($contributionIds)) { |
| 1143 | $contributionDetails = CRM_Contribute_BAO_Contribution::replaceContributionTokens( |
| 1144 | $contributionIds, |
| 1145 | $subject, |
| 1146 | $subjectToken, |
| 1147 | $text, |
| 1148 | $html, |
| 1149 | $messageToken, |
| 1150 | $escapeSmarty |
| 1151 | ); |
| 1152 | } |
| 1153 | |
| 1154 | $sent = $notSent = []; |
| 1155 | foreach ($contactDetails as $values) { |
| 1156 | $contactId = $values['contact_id']; |
| 1157 | $emailAddress = $values['email']; |
| 1158 | |
| 1159 | if (!empty($contributionDetails)) { |
| 1160 | $subject = $contributionDetails[$contactId]['subject']; |
| 1161 | $text = $contributionDetails[$contactId]['text']; |
| 1162 | $html = $contributionDetails[$contactId]['html']; |
| 1163 | } |
| 1164 | |
| 1165 | if (!empty($details) && is_array($details["{$contactId}"])) { |
| 1166 | // unset email from details since it always returns primary email address |
| 1167 | unset($details["{$contactId}"]['email']); |
| 1168 | unset($details["{$contactId}"]['email_id']); |
| 1169 | $values = array_merge($values, $details["{$contactId}"]); |
| 1170 | } |
| 1171 | |
| 1172 | $tokenSubject = CRM_Utils_Token::replaceContactTokens($subject, $values, FALSE, $subjectToken, FALSE, $escapeSmarty); |
| 1173 | $tokenSubject = CRM_Utils_Token::replaceHookTokens($tokenSubject, $values, $categories, FALSE, $escapeSmarty); |
| 1174 | |
| 1175 | // CRM-4539 |
| 1176 | if ($values['preferred_mail_format'] == 'Text' || $values['preferred_mail_format'] == 'Both') { |
| 1177 | $tokenText = CRM_Utils_Token::replaceContactTokens($text, $values, FALSE, $messageToken, FALSE, $escapeSmarty); |
| 1178 | $tokenText = CRM_Utils_Token::replaceHookTokens($tokenText, $values, $categories, FALSE, $escapeSmarty); |
| 1179 | } |
| 1180 | else { |
| 1181 | $tokenText = NULL; |
| 1182 | } |
| 1183 | |
| 1184 | if ($values['preferred_mail_format'] == 'HTML' || $values['preferred_mail_format'] == 'Both') { |
| 1185 | $tokenHtml = CRM_Utils_Token::replaceContactTokens($html, $values, TRUE, $messageToken, FALSE, $escapeSmarty); |
| 1186 | $tokenHtml = CRM_Utils_Token::replaceHookTokens($tokenHtml, $values, $categories, TRUE, $escapeSmarty); |
| 1187 | } |
| 1188 | else { |
| 1189 | $tokenHtml = NULL; |
| 1190 | } |
| 1191 | |
| 1192 | if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) { |
| 1193 | // also add the contact tokens to the template |
| 1194 | $smarty->assign_by_ref('contact', $values); |
| 1195 | |
| 1196 | $tokenSubject = $smarty->fetch("string:$tokenSubject"); |
| 1197 | $tokenText = $smarty->fetch("string:$tokenText"); |
| 1198 | $tokenHtml = $smarty->fetch("string:$tokenHtml"); |
| 1199 | } |
| 1200 | |
| 1201 | $sent = FALSE; |
| 1202 | if (self::sendMessage( |
| 1203 | $from, |
| 1204 | $userID, |
| 1205 | $contactId, |
| 1206 | $tokenSubject, |
| 1207 | $tokenText, |
| 1208 | $tokenHtml, |
| 1209 | $emailAddress, |
| 1210 | $activity->id, |
| 1211 | $attachments, |
| 1212 | $cc, |
| 1213 | $bcc |
| 1214 | ) |
| 1215 | ) { |
| 1216 | $sent = TRUE; |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | return [$sent, $activity->id]; |
| 1221 | } |
| 1222 | |
| 1223 | /** |
| 1224 | * Send SMS. Returns: bool $sent, int $activityId, int $success (number of sent SMS) |
| 1225 | * |
| 1226 | * @param array $contactDetails |
| 1227 | * @param array $activityParams |
| 1228 | * @param array $smsProviderParams |
| 1229 | * @param array $contactIds |
| 1230 | * @param int $sourceContactId This is the source contact Id |
| 1231 | * |
| 1232 | * @return array(bool $sent, int $activityId, int $success) |
| 1233 | * @throws CRM_Core_Exception |
| 1234 | */ |
| 1235 | public static function sendSMS( |
| 1236 | &$contactDetails = NULL, |
| 1237 | &$activityParams, |
| 1238 | &$smsProviderParams = [], |
| 1239 | &$contactIds = NULL, |
| 1240 | $sourceContactId = NULL |
| 1241 | ) { |
| 1242 | if (!CRM_Core_Permission::check('send SMS')) { |
| 1243 | throw new CRM_Core_Exception("You do not have the 'send SMS' permission"); |
| 1244 | } |
| 1245 | |
| 1246 | if (!isset($contactDetails) && !isset($contactIds)) { |
| 1247 | throw new CRM_Core_Exception('You must specify either $contactDetails or $contactIds'); |
| 1248 | } |
| 1249 | // Populate $contactDetails and $contactIds if only one is set |
| 1250 | if (is_array($contactIds) && !empty($contactIds) && empty($contactDetails)) { |
| 1251 | foreach ($contactIds as $id) { |
| 1252 | try { |
| 1253 | $contactDetails[] = civicrm_api3('Contact', 'getsingle', ['contact_id' => $id]); |
| 1254 | } |
| 1255 | catch (Exception $e) { |
| 1256 | // Contact Id doesn't exist |
| 1257 | } |
| 1258 | } |
| 1259 | } |
| 1260 | elseif (is_array($contactDetails) && !empty($contactDetails) && empty($contactIds)) { |
| 1261 | foreach ($contactDetails as $contact) { |
| 1262 | $contactIds[] = $contact['contact_id']; |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | // Get logged in User Id |
| 1267 | if (empty($sourceContactId)) { |
| 1268 | $sourceContactId = CRM_Core_Session::getLoggedInContactID(); |
| 1269 | } |
| 1270 | |
| 1271 | $text = &$activityParams['sms_text_message']; |
| 1272 | |
| 1273 | // Create the meta level record first ( sms activity ) |
| 1274 | $activityParams = [ |
| 1275 | 'source_contact_id' => $sourceContactId, |
| 1276 | 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'SMS'), |
| 1277 | 'activity_date_time' => date('YmdHis'), |
| 1278 | 'subject' => $activityParams['activity_subject'], |
| 1279 | 'details' => $text, |
| 1280 | 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'status_id', 'Completed'), |
| 1281 | ]; |
| 1282 | $activity = self::create($activityParams); |
| 1283 | $activityID = $activity->id; |
| 1284 | |
| 1285 | // Process Tokens |
| 1286 | // token replacement of addressee/email/postal greetings |
| 1287 | // get the tokens added in subject and message |
| 1288 | $messageToken = CRM_Utils_Token::getTokens($text); |
| 1289 | $returnProperties = []; |
| 1290 | if (isset($messageToken['contact'])) { |
| 1291 | foreach ($messageToken['contact'] as $key => $value) { |
| 1292 | $returnProperties[$value] = 1; |
| 1293 | } |
| 1294 | } |
| 1295 | // Call tokens hook |
| 1296 | $tokens = []; |
| 1297 | CRM_Utils_Hook::tokens($tokens); |
| 1298 | $categories = array_keys($tokens); |
| 1299 | // get token details for contacts, call only if tokens are used |
| 1300 | $tokenDetails = []; |
| 1301 | if (!empty($returnProperties) || !empty($tokens)) { |
| 1302 | list($tokenDetails) = CRM_Utils_Token::getTokenDetails($contactIds, |
| 1303 | $returnProperties, |
| 1304 | NULL, NULL, FALSE, |
| 1305 | $messageToken, |
| 1306 | 'CRM_Activity_BAO_Activity' |
| 1307 | ); |
| 1308 | } |
| 1309 | |
| 1310 | $success = 0; |
| 1311 | $errMsgs = []; |
| 1312 | foreach ($contactDetails as $contact) { |
| 1313 | $contactId = $contact['contact_id']; |
| 1314 | |
| 1315 | // Replace tokens |
| 1316 | if (!empty($tokenDetails) && is_array($tokenDetails["{$contactId}"])) { |
| 1317 | // unset phone from details since it always returns primary number |
| 1318 | unset($tokenDetails["{$contactId}"]['phone']); |
| 1319 | unset($tokenDetails["{$contactId}"]['phone_type_id']); |
| 1320 | $contact = array_merge($contact, $tokenDetails["{$contactId}"]); |
| 1321 | } |
| 1322 | $tokenText = CRM_Utils_Token::replaceContactTokens($text, $contact, FALSE, $messageToken, FALSE, FALSE); |
| 1323 | $tokenText = CRM_Utils_Token::replaceHookTokens($tokenText, $contact, $categories, FALSE, FALSE); |
| 1324 | |
| 1325 | // Only send if the phone is of type mobile |
| 1326 | if ($contact['phone_type_id'] == CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'phone_type_id', 'Mobile')) { |
| 1327 | $smsProviderParams['To'] = $contact['phone']; |
| 1328 | } |
| 1329 | else { |
| 1330 | $smsProviderParams['To'] = ''; |
| 1331 | } |
| 1332 | |
| 1333 | $doNotSms = CRM_Utils_Array::value('do_not_sms', $contact, 0); |
| 1334 | |
| 1335 | if ($doNotSms) { |
| 1336 | $errMsgs[] = PEAR::raiseError('Contact Does not accept SMS', NULL, PEAR_ERROR_RETURN); |
| 1337 | } |
| 1338 | else { |
| 1339 | $sendResult = self::sendSMSMessage( |
| 1340 | $contactId, |
| 1341 | $tokenText, |
| 1342 | $smsProviderParams, |
| 1343 | $activityID, |
| 1344 | $sourceContactId |
| 1345 | ); |
| 1346 | |
| 1347 | if (PEAR::isError($sendResult)) { |
| 1348 | // Collect all of the PEAR_Error objects |
| 1349 | $errMsgs[] = $sendResult; |
| 1350 | } |
| 1351 | else { |
| 1352 | $success++; |
| 1353 | } |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | // If at least one message was sent and no errors |
| 1358 | // were generated then return a boolean value of TRUE. |
| 1359 | // Otherwise, return FALSE (no messages sent) or |
| 1360 | // and array of 1 or more PEAR_Error objects. |
| 1361 | $sent = FALSE; |
| 1362 | if ($success > 0 && count($errMsgs) == 0) { |
| 1363 | $sent = TRUE; |
| 1364 | } |
| 1365 | elseif (count($errMsgs) > 0) { |
| 1366 | $sent = $errMsgs; |
| 1367 | } |
| 1368 | |
| 1369 | return [$sent, $activity->id, $success]; |
| 1370 | } |
| 1371 | |
| 1372 | /** |
| 1373 | * Send the sms message to a specific contact. |
| 1374 | * |
| 1375 | * @param int $toID |
| 1376 | * The contact id of the recipient. |
| 1377 | * @param $tokenText |
| 1378 | * @param array $smsProviderParams |
| 1379 | * The params used for sending sms. |
| 1380 | * @param int $activityID |
| 1381 | * The activity ID that tracks the message. |
| 1382 | * @param int $sourceContactID |
| 1383 | * |
| 1384 | * @return bool|PEAR_Error |
| 1385 | * true on success or PEAR_Error object |
| 1386 | */ |
| 1387 | public static function sendSMSMessage( |
| 1388 | $toID, |
| 1389 | &$tokenText, |
| 1390 | $smsProviderParams = [], |
| 1391 | $activityID, |
| 1392 | $sourceContactID = NULL |
| 1393 | ) { |
| 1394 | $toPhoneNumber = NULL; |
| 1395 | if ($smsProviderParams['To']) { |
| 1396 | // If phone number is specified use it |
| 1397 | $toPhoneNumber = trim($smsProviderParams['To']); |
| 1398 | } |
| 1399 | elseif ($toID) { |
| 1400 | // No phone number specified, so find a suitable one for the contact |
| 1401 | $filters = ['is_deceased' => 0, 'is_deleted' => 0, 'do_not_sms' => 0]; |
| 1402 | $toPhoneNumbers = CRM_Core_BAO_Phone::allPhones($toID, FALSE, 'Mobile', $filters); |
| 1403 | // To get primary mobile phonenumber, if not get the first mobile phonenumber |
| 1404 | if (!empty($toPhoneNumbers)) { |
| 1405 | $toPhoneNumberDetails = reset($toPhoneNumbers); |
| 1406 | $toPhoneNumber = CRM_Utils_Array::value('phone', $toPhoneNumberDetails); |
| 1407 | // Contact allows to send sms |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | // make sure both phone are valid |
| 1412 | // and that the recipient wants to receive sms |
| 1413 | if (empty($toPhoneNumber)) { |
| 1414 | return PEAR::raiseError( |
| 1415 | 'Recipient phone number is invalid or recipient does not want to receive SMS', |
| 1416 | NULL, |
| 1417 | PEAR_ERROR_RETURN |
| 1418 | ); |
| 1419 | } |
| 1420 | |
| 1421 | $recipient = $toPhoneNumber; |
| 1422 | $smsProviderParams['contact_id'] = $toID; |
| 1423 | $smsProviderParams['parent_activity_id'] = $activityID; |
| 1424 | |
| 1425 | $providerObj = CRM_SMS_Provider::singleton(['provider_id' => $smsProviderParams['provider_id']]); |
| 1426 | $sendResult = $providerObj->send($recipient, $smsProviderParams, $tokenText, NULL, $sourceContactID); |
| 1427 | if (PEAR::isError($sendResult)) { |
| 1428 | return $sendResult; |
| 1429 | } |
| 1430 | |
| 1431 | // add activity target record for every sms that is sent |
| 1432 | $targetID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Targets'); |
| 1433 | $activityTargetParams = [ |
| 1434 | 'activity_id' => $activityID, |
| 1435 | 'contact_id' => $toID, |
| 1436 | 'record_type_id' => $targetID, |
| 1437 | ]; |
| 1438 | CRM_Activity_BAO_ActivityContact::create($activityTargetParams); |
| 1439 | |
| 1440 | return TRUE; |
| 1441 | } |
| 1442 | |
| 1443 | /** |
| 1444 | * Send the message to a specific contact. |
| 1445 | * |
| 1446 | * @param string $from |
| 1447 | * The name and email of the sender. |
| 1448 | * @param int $fromID |
| 1449 | * @param int $toID |
| 1450 | * The contact id of the recipient. |
| 1451 | * @param string $subject |
| 1452 | * The subject of the message. |
| 1453 | * @param $text_message |
| 1454 | * @param $html_message |
| 1455 | * @param string $emailAddress |
| 1456 | * Use this 'to' email address instead of the default Primary address. |
| 1457 | * @param int $activityID |
| 1458 | * The activity ID that tracks the message. |
| 1459 | * @param null $attachments |
| 1460 | * @param null $cc |
| 1461 | * @param null $bcc |
| 1462 | * |
| 1463 | * @return bool |
| 1464 | * TRUE if successful else FALSE. |
| 1465 | */ |
| 1466 | public static function sendMessage( |
| 1467 | $from, |
| 1468 | $fromID, |
| 1469 | $toID, |
| 1470 | &$subject, |
| 1471 | &$text_message, |
| 1472 | &$html_message, |
| 1473 | $emailAddress, |
| 1474 | $activityID, |
| 1475 | $attachments = NULL, |
| 1476 | $cc = NULL, |
| 1477 | $bcc = NULL |
| 1478 | ) { |
| 1479 | list($toDisplayName, $toEmail, $toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($toID); |
| 1480 | if ($emailAddress) { |
| 1481 | $toEmail = trim($emailAddress); |
| 1482 | } |
| 1483 | |
| 1484 | // make sure both email addresses are valid |
| 1485 | // and that the recipient wants to receive email |
| 1486 | if (empty($toEmail) or $toDoNotEmail) { |
| 1487 | return FALSE; |
| 1488 | } |
| 1489 | if (!trim($toDisplayName)) { |
| 1490 | $toDisplayName = $toEmail; |
| 1491 | } |
| 1492 | |
| 1493 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
| 1494 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
| 1495 | |
| 1496 | // create the params array |
| 1497 | $mailParams = [ |
| 1498 | 'groupName' => 'Activity Email Sender', |
| 1499 | 'from' => $from, |
| 1500 | 'toName' => $toDisplayName, |
| 1501 | 'toEmail' => $toEmail, |
| 1502 | 'subject' => $subject, |
| 1503 | 'cc' => $cc, |
| 1504 | 'bcc' => $bcc, |
| 1505 | 'text' => $text_message, |
| 1506 | 'html' => $html_message, |
| 1507 | 'attachments' => $attachments, |
| 1508 | ]; |
| 1509 | |
| 1510 | if (!CRM_Utils_Mail::send($mailParams)) { |
| 1511 | return FALSE; |
| 1512 | } |
| 1513 | |
| 1514 | // add activity target record for every mail that is send |
| 1515 | $activityTargetParams = [ |
| 1516 | 'activity_id' => $activityID, |
| 1517 | 'contact_id' => $toID, |
| 1518 | 'record_type_id' => $targetID, |
| 1519 | ]; |
| 1520 | CRM_Activity_BAO_ActivityContact::create($activityTargetParams); |
| 1521 | return TRUE; |
| 1522 | } |
| 1523 | |
| 1524 | /** |
| 1525 | * Combine all the importable fields from the lower levels object. |
| 1526 | * |
| 1527 | * The ordering is important, since currently we do not have a weight |
| 1528 | * scheme. Adding weight is super important and should be done in the |
| 1529 | * next week or so, before this can be called complete. |
| 1530 | * |
| 1531 | * @param bool $status |
| 1532 | * |
| 1533 | * @return array |
| 1534 | * array of importable Fields |
| 1535 | */ |
| 1536 | public static function &importableFields($status = FALSE) { |
| 1537 | if (!self::$_importableFields) { |
| 1538 | if (!self::$_importableFields) { |
| 1539 | self::$_importableFields = []; |
| 1540 | } |
| 1541 | if (!$status) { |
| 1542 | $fields = ['' => ['title' => ts('- do not import -')]]; |
| 1543 | } |
| 1544 | else { |
| 1545 | $fields = ['' => ['title' => ts('- Activity Fields -')]]; |
| 1546 | } |
| 1547 | |
| 1548 | $tmpFields = CRM_Activity_DAO_Activity::import(); |
| 1549 | $contactFields = CRM_Contact_BAO_Contact::importableFields('Individual', NULL); |
| 1550 | |
| 1551 | // Using new Dedupe rule. |
| 1552 | $ruleParams = [ |
| 1553 | 'contact_type' => 'Individual', |
| 1554 | 'used' => 'Unsupervised', |
| 1555 | ]; |
| 1556 | $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams); |
| 1557 | |
| 1558 | $tmpConatctField = []; |
| 1559 | if (is_array($fieldsArray)) { |
| 1560 | foreach ($fieldsArray as $value) { |
| 1561 | $customFieldId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', |
| 1562 | $value, |
| 1563 | 'id', |
| 1564 | 'column_name' |
| 1565 | ); |
| 1566 | $value = $customFieldId ? 'custom_' . $customFieldId : $value; |
| 1567 | $tmpConatctField[trim($value)] = $contactFields[trim($value)]; |
| 1568 | $tmpConatctField[trim($value)]['title'] = $tmpConatctField[trim($value)]['title'] . " (match to contact)"; |
| 1569 | } |
| 1570 | } |
| 1571 | $tmpConatctField['external_identifier'] = $contactFields['external_identifier']; |
| 1572 | $tmpConatctField['external_identifier']['title'] = $contactFields['external_identifier']['title'] . " (match to contact)"; |
| 1573 | $fields = array_merge($fields, $tmpConatctField); |
| 1574 | $fields = array_merge($fields, $tmpFields); |
| 1575 | $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Activity')); |
| 1576 | self::$_importableFields = $fields; |
| 1577 | } |
| 1578 | return self::$_importableFields; |
| 1579 | } |
| 1580 | |
| 1581 | /** |
| 1582 | * @deprecated - use the api instead. |
| 1583 | * |
| 1584 | * Get the Activities of a target contact. |
| 1585 | * |
| 1586 | * @param int $contactId |
| 1587 | * Id of the contact whose activities need to find. |
| 1588 | * |
| 1589 | * @return array |
| 1590 | * array of activity fields |
| 1591 | */ |
| 1592 | public static function getContactActivity($contactId) { |
| 1593 | // @todo remove this function entirely. |
| 1594 | $activities = []; |
| 1595 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
| 1596 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
| 1597 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); |
| 1598 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
| 1599 | |
| 1600 | // First look for activities where contactId is one of the targets |
| 1601 | $query = " |
| 1602 | SELECT activity_id, record_type_id |
| 1603 | FROM civicrm_activity_contact |
| 1604 | WHERE contact_id = $contactId |
| 1605 | "; |
| 1606 | $dao = CRM_Core_DAO::executeQuery($query); |
| 1607 | while ($dao->fetch()) { |
| 1608 | if ($dao->record_type_id == $targetID) { |
| 1609 | $activities[$dao->activity_id]['targets'][$contactId] = $contactId; |
| 1610 | } |
| 1611 | elseif ($dao->record_type_id == $assigneeID) { |
| 1612 | $activities[$dao->activity_id]['asignees'][$contactId] = $contactId; |
| 1613 | } |
| 1614 | else { |
| 1615 | // do source stuff here |
| 1616 | $activities[$dao->activity_id]['source_contact_id'] = $contactId; |
| 1617 | } |
| 1618 | } |
| 1619 | |
| 1620 | $activityIds = array_keys($activities); |
| 1621 | if (count($activityIds) < 1) { |
| 1622 | return []; |
| 1623 | } |
| 1624 | |
| 1625 | $activityIds = implode(',', $activityIds); |
| 1626 | $query = " |
| 1627 | SELECT activity.id as activity_id, |
| 1628 | activity_type_id, |
| 1629 | subject, location, activity_date_time, details, status_id |
| 1630 | FROM civicrm_activity activity |
| 1631 | WHERE activity.id IN ($activityIds)"; |
| 1632 | |
| 1633 | $dao = CRM_Core_DAO::executeQuery($query); |
| 1634 | |
| 1635 | while ($dao->fetch()) { |
| 1636 | $activities[$dao->activity_id]['id'] = $dao->activity_id; |
| 1637 | $activities[$dao->activity_id]['activity_type_id'] = $dao->activity_type_id; |
| 1638 | $activities[$dao->activity_id]['subject'] = $dao->subject; |
| 1639 | $activities[$dao->activity_id]['location'] = $dao->location; |
| 1640 | $activities[$dao->activity_id]['activity_date_time'] = $dao->activity_date_time; |
| 1641 | $activities[$dao->activity_id]['details'] = $dao->details; |
| 1642 | $activities[$dao->activity_id]['status_id'] = $dao->status_id; |
| 1643 | $activities[$dao->activity_id]['activity_name'] = CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'activity_type_id', $dao->activity_type_id); |
| 1644 | $activities[$dao->activity_id]['status'] = CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'activity_status_id', $dao->status_id); |
| 1645 | |
| 1646 | // set to null if not set |
| 1647 | if (!isset($activities[$dao->activity_id]['source_contact_id'])) { |
| 1648 | $activities[$dao->activity_id]['source_contact_id'] = NULL; |
| 1649 | } |
| 1650 | } |
| 1651 | return $activities; |
| 1652 | } |
| 1653 | |
| 1654 | /** |
| 1655 | * Add activity for Membership/Event/Contribution. |
| 1656 | * |
| 1657 | * @param object $activity |
| 1658 | * (reference) particular component object. |
| 1659 | * @param string $activityType |
| 1660 | * For Membership Signup or Renewal. |
| 1661 | * @param int $targetContactID |
| 1662 | * @param array $params |
| 1663 | * Activity params to override. |
| 1664 | * |
| 1665 | * @return bool|NULL |
| 1666 | */ |
| 1667 | public static function addActivity( |
| 1668 | &$activity, |
| 1669 | $activityType = 'Membership Signup', |
| 1670 | $targetContactID = NULL, |
| 1671 | $params = [] |
| 1672 | ) { |
| 1673 | $date = date('YmdHis'); |
| 1674 | if ($activity->__table == 'civicrm_membership') { |
| 1675 | $component = 'Membership'; |
| 1676 | } |
| 1677 | elseif ($activity->__table == 'civicrm_participant') { |
| 1678 | if ($activityType != 'Email') { |
| 1679 | $activityType = 'Event Registration'; |
| 1680 | } |
| 1681 | $component = 'Event'; |
| 1682 | } |
| 1683 | elseif ($activity->__table == 'civicrm_contribution') { |
| 1684 | // create activity record only for Completed Contributions |
| 1685 | $contributionCompletedStatusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'); |
| 1686 | if ($activity->contribution_status_id != $contributionCompletedStatusId) { |
| 1687 | return NULL; |
| 1688 | } |
| 1689 | $activityType = $component = 'Contribution'; |
| 1690 | |
| 1691 | // retrieve existing activity based on source_record_id and activity_type |
| 1692 | if (empty($params['id'])) { |
| 1693 | $params['id'] = CRM_Utils_Array::value('id', civicrm_api3('Activity', 'Get', [ |
| 1694 | 'source_record_id' => $activity->id, |
| 1695 | 'activity_type_id' => $activityType, |
| 1696 | ])); |
| 1697 | } |
| 1698 | if (!empty($params['id'])) { |
| 1699 | // CRM-13237 : if activity record found, update it with campaign id of contribution |
| 1700 | $params['campaign_id'] = $activity->campaign_id; |
| 1701 | } |
| 1702 | |
| 1703 | $date = CRM_Utils_Date::isoToMysql($activity->receive_date); |
| 1704 | } |
| 1705 | |
| 1706 | $activityParams = [ |
| 1707 | 'source_contact_id' => $activity->contact_id, |
| 1708 | 'source_record_id' => $activity->id, |
| 1709 | 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', $activityType), |
| 1710 | 'activity_date_time' => $date, |
| 1711 | 'is_test' => $activity->is_test, |
| 1712 | 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'), |
| 1713 | 'skipRecentView' => TRUE, |
| 1714 | 'campaign_id' => $activity->campaign_id, |
| 1715 | ]; |
| 1716 | $activityParams = array_merge($activityParams, $params); |
| 1717 | |
| 1718 | if (empty($activityParams['subject'])) { |
| 1719 | $activityParams['subject'] = self::getActivitySubject($activity); |
| 1720 | } |
| 1721 | |
| 1722 | if (!empty($activity->activity_id)) { |
| 1723 | $activityParams['id'] = $activity->activity_id; |
| 1724 | } |
| 1725 | // create activity with target contacts |
| 1726 | $id = CRM_Core_Session::getLoggedInContactID(); |
| 1727 | if ($id) { |
| 1728 | $activityParams['source_contact_id'] = $id; |
| 1729 | $activityParams['target_contact_id'][] = $activity->contact_id; |
| 1730 | } |
| 1731 | |
| 1732 | // CRM-14945 |
| 1733 | if (property_exists($activity, 'details')) { |
| 1734 | $activityParams['details'] = $activity->details; |
| 1735 | } |
| 1736 | //CRM-4027 |
| 1737 | if ($targetContactID) { |
| 1738 | $activityParams['target_contact_id'][] = $targetContactID; |
| 1739 | } |
| 1740 | // @todo - use api - remove lots of wrangling above. Remove deprecated fatal & let form layer |
| 1741 | // deal with any exceptions. |
| 1742 | if (is_a(self::create($activityParams), 'CRM_Core_Error')) { |
| 1743 | CRM_Core_Error::fatal("Failed creating Activity for $component of id {$activity->id}"); |
| 1744 | return FALSE; |
| 1745 | } |
| 1746 | } |
| 1747 | |
| 1748 | /** |
| 1749 | * Get activity subject on basis of component object. |
| 1750 | * |
| 1751 | * @param object $entityObj |
| 1752 | * particular component object. |
| 1753 | * |
| 1754 | * @return string |
| 1755 | */ |
| 1756 | public static function getActivitySubject($entityObj) { |
| 1757 | switch ($entityObj->__table) { |
| 1758 | case 'civicrm_membership': |
| 1759 | $membershipType = CRM_Member_PseudoConstant::membershipType($entityObj->membership_type_id); |
| 1760 | $subject = $membershipType ? $membershipType : ts('Membership'); |
| 1761 | |
| 1762 | if (is_array($subject)) { |
| 1763 | $subject = implode(", ", $subject); |
| 1764 | } |
| 1765 | |
| 1766 | if (!CRM_Utils_System::isNull($entityObj->source)) { |
| 1767 | $subject .= " - {$entityObj->source}"; |
| 1768 | } |
| 1769 | |
| 1770 | if ($entityObj->owner_membership_id) { |
| 1771 | list($displayName) = CRM_Contact_BAO_Contact::getDisplayAndImage(CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $entityObj->owner_membership_id, 'contact_id')); |
| 1772 | $subject .= sprintf(' (by %s)', $displayName); |
| 1773 | } |
| 1774 | |
| 1775 | $subject .= " - Status: " . CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', $entityObj->status_id, 'label'); |
| 1776 | return $subject; |
| 1777 | |
| 1778 | case 'civicrm_participant': |
| 1779 | $event = CRM_Event_BAO_Event::getEvents(1, $entityObj->event_id, TRUE, FALSE); |
| 1780 | $roles = CRM_Event_PseudoConstant::participantRole(); |
| 1781 | $status = CRM_Event_PseudoConstant::participantStatus(); |
| 1782 | $subject = $event[$entityObj->event_id]; |
| 1783 | |
| 1784 | if (!empty($roles[$entityObj->role_id])) { |
| 1785 | $subject .= ' - ' . $roles[$entityObj->role_id]; |
| 1786 | } |
| 1787 | if (!empty($status[$entityObj->status_id])) { |
| 1788 | $subject .= ' - ' . $status[$entityObj->status_id]; |
| 1789 | } |
| 1790 | |
| 1791 | return $subject; |
| 1792 | |
| 1793 | case 'civicrm_contribution': |
| 1794 | $subject = CRM_Utils_Money::format($entityObj->total_amount, $entityObj->currency); |
| 1795 | if (!CRM_Utils_System::isNull($entityObj->source)) { |
| 1796 | $subject .= " - {$entityObj->source}"; |
| 1797 | } |
| 1798 | |
| 1799 | // Amount and source could exceed max length of subject column. |
| 1800 | return CRM_Utils_String::ellipsify($subject, 255); |
| 1801 | } |
| 1802 | } |
| 1803 | |
| 1804 | /** |
| 1805 | * Get Parent activity for currently viewed activity. |
| 1806 | * |
| 1807 | * @param int $activityId |
| 1808 | * Current activity id. |
| 1809 | * |
| 1810 | * @return int |
| 1811 | * Id of parent activity otherwise false. |
| 1812 | */ |
| 1813 | public static function getParentActivity($activityId) { |
| 1814 | static $parentActivities = []; |
| 1815 | |
| 1816 | $activityId = CRM_Utils_Type::escape($activityId, 'Integer'); |
| 1817 | |
| 1818 | if (!array_key_exists($activityId, $parentActivities)) { |
| 1819 | $parentActivities[$activityId] = []; |
| 1820 | |
| 1821 | $parentId = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', |
| 1822 | $activityId, |
| 1823 | 'parent_id' |
| 1824 | ); |
| 1825 | |
| 1826 | $parentActivities[$activityId] = $parentId ? $parentId : FALSE; |
| 1827 | } |
| 1828 | |
| 1829 | return $parentActivities[$activityId]; |
| 1830 | } |
| 1831 | |
| 1832 | /** |
| 1833 | * Get total count of prior revision of currently viewed activity. |
| 1834 | * |
| 1835 | * @param $activityID |
| 1836 | * Current activity id. |
| 1837 | * |
| 1838 | * @return int |
| 1839 | * $params count of prior activities otherwise false. |
| 1840 | */ |
| 1841 | public static function getPriorCount($activityID) { |
| 1842 | static $priorCounts = []; |
| 1843 | |
| 1844 | $activityID = CRM_Utils_Type::escape($activityID, 'Integer'); |
| 1845 | |
| 1846 | if (!array_key_exists($activityID, $priorCounts)) { |
| 1847 | $priorCounts[$activityID] = []; |
| 1848 | $originalID = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', |
| 1849 | $activityID, |
| 1850 | 'original_id' |
| 1851 | ); |
| 1852 | $count = 0; |
| 1853 | if ($originalID) { |
| 1854 | $query = " |
| 1855 | SELECT count( id ) AS cnt |
| 1856 | FROM civicrm_activity |
| 1857 | WHERE ( id = {$originalID} OR original_id = {$originalID} ) |
| 1858 | AND is_current_revision = 0 |
| 1859 | AND id < {$activityID} |
| 1860 | "; |
| 1861 | $params = [1 => [$originalID, 'Integer']]; |
| 1862 | $count = CRM_Core_DAO::singleValueQuery($query, $params); |
| 1863 | } |
| 1864 | $priorCounts[$activityID] = $count ? $count : 0; |
| 1865 | } |
| 1866 | |
| 1867 | return $priorCounts[$activityID]; |
| 1868 | } |
| 1869 | |
| 1870 | /** |
| 1871 | * Get all prior activities of currently viewed activity. |
| 1872 | * |
| 1873 | * @param $activityID |
| 1874 | * Current activity id. |
| 1875 | * @param bool $onlyPriorRevisions |
| 1876 | * |
| 1877 | * @return array |
| 1878 | * prior activities info. |
| 1879 | */ |
| 1880 | public static function getPriorAcitivities($activityID, $onlyPriorRevisions = FALSE) { |
| 1881 | static $priorActivities = []; |
| 1882 | |
| 1883 | $activityID = CRM_Utils_Type::escape($activityID, 'Integer'); |
| 1884 | $index = $activityID . '_' . (int) $onlyPriorRevisions; |
| 1885 | |
| 1886 | if (!array_key_exists($index, $priorActivities)) { |
| 1887 | $priorActivities[$index] = []; |
| 1888 | |
| 1889 | $originalID = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', |
| 1890 | $activityID, |
| 1891 | 'original_id' |
| 1892 | ); |
| 1893 | if (!$originalID) { |
| 1894 | $originalID = $activityID; |
| 1895 | } |
| 1896 | if ($originalID) { |
| 1897 | $query = " |
| 1898 | SELECT c.display_name as name, cl.modified_date as date, ca.id as activityID |
| 1899 | FROM civicrm_log cl, civicrm_contact c, civicrm_activity ca |
| 1900 | WHERE (ca.id = %1 OR ca.original_id = %1) |
| 1901 | AND cl.entity_table = 'civicrm_activity' |
| 1902 | AND cl.entity_id = ca.id |
| 1903 | AND cl.modified_id = c.id |
| 1904 | "; |
| 1905 | if ($onlyPriorRevisions) { |
| 1906 | $query .= " AND ca.id < {$activityID}"; |
| 1907 | } |
| 1908 | $query .= " ORDER BY ca.id DESC"; |
| 1909 | |
| 1910 | $params = [1 => [$originalID, 'Integer']]; |
| 1911 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 1912 | |
| 1913 | while ($dao->fetch()) { |
| 1914 | $priorActivities[$index][$dao->activityID]['id'] = $dao->activityID; |
| 1915 | $priorActivities[$index][$dao->activityID]['name'] = $dao->name; |
| 1916 | $priorActivities[$index][$dao->activityID]['date'] = $dao->date; |
| 1917 | } |
| 1918 | } |
| 1919 | } |
| 1920 | return $priorActivities[$index]; |
| 1921 | } |
| 1922 | |
| 1923 | /** |
| 1924 | * Find the latest revision of a given activity. |
| 1925 | * |
| 1926 | * @param int $activityID |
| 1927 | * Prior activity id. |
| 1928 | * |
| 1929 | * @return int |
| 1930 | * current activity id. |
| 1931 | */ |
| 1932 | public static function getLatestActivityId($activityID) { |
| 1933 | static $latestActivityIds = []; |
| 1934 | |
| 1935 | $activityID = CRM_Utils_Type::escape($activityID, 'Integer'); |
| 1936 | |
| 1937 | if (!array_key_exists($activityID, $latestActivityIds)) { |
| 1938 | $latestActivityIds[$activityID] = []; |
| 1939 | |
| 1940 | $originalID = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', |
| 1941 | $activityID, |
| 1942 | 'original_id' |
| 1943 | ); |
| 1944 | if ($originalID) { |
| 1945 | $activityID = $originalID; |
| 1946 | } |
| 1947 | $params = [1 => [$activityID, 'Integer']]; |
| 1948 | $query = "SELECT id from civicrm_activity where original_id = %1 and is_current_revision = 1"; |
| 1949 | |
| 1950 | $latestActivityIds[$activityID] = CRM_Core_DAO::singleValueQuery($query, $params); |
| 1951 | } |
| 1952 | |
| 1953 | return $latestActivityIds[$activityID]; |
| 1954 | } |
| 1955 | |
| 1956 | /** |
| 1957 | * Create a follow up a given activity. |
| 1958 | * |
| 1959 | * @param int $activityId |
| 1960 | * activity id of parent activity. |
| 1961 | * @param array $params |
| 1962 | * |
| 1963 | * @return CRM_Activity_BAO_Activity|null|object |
| 1964 | */ |
| 1965 | public static function createFollowupActivity($activityId, $params) { |
| 1966 | if (!$activityId) { |
| 1967 | return NULL; |
| 1968 | } |
| 1969 | |
| 1970 | $followupParams = []; |
| 1971 | $followupParams['parent_id'] = $activityId; |
| 1972 | $followupParams['source_contact_id'] = CRM_Core_Session::getLoggedInContactID(); |
| 1973 | $followupParams['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Scheduled'); |
| 1974 | |
| 1975 | $followupParams['activity_type_id'] = $params['followup_activity_type_id']; |
| 1976 | // Get Subject of Follow-up Activiity, CRM-4491 |
| 1977 | $followupParams['subject'] = CRM_Utils_Array::value('followup_activity_subject', $params); |
| 1978 | $followupParams['assignee_contact_id'] = CRM_Utils_Array::value('followup_assignee_contact_id', $params); |
| 1979 | |
| 1980 | // Create target contact for followup. |
| 1981 | if (!empty($params['target_contact_id'])) { |
| 1982 | $followupParams['target_contact_id'] = $params['target_contact_id']; |
| 1983 | } |
| 1984 | |
| 1985 | $followupParams['activity_date_time'] = $params['followup_date']; |
| 1986 | $followupActivity = self::create($followupParams); |
| 1987 | |
| 1988 | return $followupActivity; |
| 1989 | } |
| 1990 | |
| 1991 | /** |
| 1992 | * Get Activity specific File according activity type Id. |
| 1993 | * |
| 1994 | * @param int $activityTypeId |
| 1995 | * Activity id. |
| 1996 | * @param string $crmDir |
| 1997 | * |
| 1998 | * @return string|bool |
| 1999 | * if file exists returns $activityTypeFile activity filename otherwise false. |
| 2000 | */ |
| 2001 | public static function getFileForActivityTypeId($activityTypeId, $crmDir = 'Activity') { |
| 2002 | $activityTypes = CRM_Case_PseudoConstant::caseActivityType(FALSE, TRUE); |
| 2003 | |
| 2004 | if ($activityTypes[$activityTypeId]['name']) { |
| 2005 | $activityTypeFile = CRM_Utils_String::munge(ucwords($activityTypes[$activityTypeId]['name']), '', 0); |
| 2006 | } |
| 2007 | else { |
| 2008 | return FALSE; |
| 2009 | } |
| 2010 | |
| 2011 | global $civicrm_root; |
| 2012 | $config = CRM_Core_Config::singleton(); |
| 2013 | if (!file_exists(rtrim($civicrm_root, '/') . "/CRM/{$crmDir}/Form/Activity/{$activityTypeFile}.php")) { |
| 2014 | if (empty($config->customPHPPathDir)) { |
| 2015 | return FALSE; |
| 2016 | } |
| 2017 | elseif (!file_exists(rtrim($config->customPHPPathDir, '/') . "/CRM/{$crmDir}/Form/Activity/{$activityTypeFile}.php")) { |
| 2018 | return FALSE; |
| 2019 | } |
| 2020 | } |
| 2021 | |
| 2022 | return $activityTypeFile; |
| 2023 | } |
| 2024 | |
| 2025 | /** |
| 2026 | * Restore the activity. |
| 2027 | * |
| 2028 | * @param array $params |
| 2029 | * |
| 2030 | * @return CRM_Activity_DAO_Activity |
| 2031 | */ |
| 2032 | public static function restoreActivity(&$params) { |
| 2033 | $activity = new CRM_Activity_DAO_Activity(); |
| 2034 | $activity->copyValues($params); |
| 2035 | |
| 2036 | $activity->is_deleted = 0; |
| 2037 | $result = $activity->save(); |
| 2038 | |
| 2039 | return $result; |
| 2040 | } |
| 2041 | |
| 2042 | /** |
| 2043 | * Return list of activity statuses of a given type. |
| 2044 | * |
| 2045 | * Note: activity status options use the "grouping" field to distinguish status types. |
| 2046 | * Types are defined in class constants INCOMPLETE, COMPLETED, CANCELLED |
| 2047 | * |
| 2048 | * @param int $type |
| 2049 | * |
| 2050 | * @return array |
| 2051 | */ |
| 2052 | public static function getStatusesByType($type) { |
| 2053 | if (!isset(Civi::$statics[__CLASS__][__FUNCTION__])) { |
| 2054 | $statuses = civicrm_api3('OptionValue', 'get', [ |
| 2055 | 'option_group_id' => 'activity_status', |
| 2056 | 'return' => ['value', 'name', 'filter'], |
| 2057 | 'options' => ['limit' => 0], |
| 2058 | ]); |
| 2059 | Civi::$statics[__CLASS__][__FUNCTION__] = $statuses['values']; |
| 2060 | } |
| 2061 | $ret = []; |
| 2062 | foreach (Civi::$statics[__CLASS__][__FUNCTION__] as $status) { |
| 2063 | if ($status['filter'] == $type) { |
| 2064 | $ret[$status['value']] = $status['name']; |
| 2065 | } |
| 2066 | } |
| 2067 | return $ret; |
| 2068 | } |
| 2069 | |
| 2070 | /** |
| 2071 | * Check if activity is overdue. |
| 2072 | * |
| 2073 | * @param array $activity |
| 2074 | * |
| 2075 | * @return bool |
| 2076 | */ |
| 2077 | public static function isOverdue($activity) { |
| 2078 | return array_key_exists($activity['status_id'], self::getStatusesByType(self::INCOMPLETE)) && CRM_Utils_Date::overdue($activity['activity_date_time']); |
| 2079 | } |
| 2080 | |
| 2081 | /** |
| 2082 | * Get the exportable fields for Activities. |
| 2083 | * |
| 2084 | * @param string $name |
| 2085 | * If it is called by case $name = Case else $name = Activity. |
| 2086 | * |
| 2087 | * @return array |
| 2088 | * array of exportable Fields |
| 2089 | */ |
| 2090 | public static function exportableFields($name = 'Activity') { |
| 2091 | self::$_exportableFields[$name] = []; |
| 2092 | |
| 2093 | // TODO: ideally we should retrieve all fields from xml, in this case since activity processing is done |
| 2094 | // my case hence we have defined fields as case_* |
| 2095 | if ($name == 'Activity') { |
| 2096 | $exportableFields = CRM_Activity_DAO_Activity::export(); |
| 2097 | $exportableFields['source_contact_id'] = [ |
| 2098 | 'title' => ts('Source Contact ID'), |
| 2099 | 'type' => CRM_Utils_Type::T_INT, |
| 2100 | ]; |
| 2101 | $exportableFields['source_contact'] = [ |
| 2102 | 'title' => ts('Source Contact'), |
| 2103 | 'type' => CRM_Utils_Type::T_STRING, |
| 2104 | ]; |
| 2105 | |
| 2106 | $Activityfields = [ |
| 2107 | 'activity_type' => [ |
| 2108 | 'title' => ts('Activity Type'), |
| 2109 | 'name' => 'activity_type', |
| 2110 | 'type' => CRM_Utils_Type::T_STRING, |
| 2111 | 'searchByLabel' => TRUE, |
| 2112 | ], |
| 2113 | 'activity_status' => [ |
| 2114 | 'title' => ts('Activity Status'), |
| 2115 | 'name' => 'activity_status', |
| 2116 | 'type' => CRM_Utils_Type::T_STRING, |
| 2117 | 'searchByLabel' => TRUE, |
| 2118 | ], |
| 2119 | 'activity_priority' => [ |
| 2120 | 'title' => ts('Activity Priority'), |
| 2121 | 'name' => 'activity_priority', |
| 2122 | 'type' => CRM_Utils_Type::T_STRING, |
| 2123 | 'searchByLabel' => TRUE, |
| 2124 | ], |
| 2125 | ]; |
| 2126 | $fields = array_merge($Activityfields, $exportableFields); |
| 2127 | } |
| 2128 | else { |
| 2129 | // Set title to activity fields. |
| 2130 | $fields = [ |
| 2131 | 'case_activity_subject' => [ |
| 2132 | 'title' => ts('Activity Subject'), |
| 2133 | 'type' => CRM_Utils_Type::T_STRING, |
| 2134 | ], |
| 2135 | 'case_source_contact_id' => [ |
| 2136 | 'title' => ts('Activity Reporter'), |
| 2137 | 'type' => CRM_Utils_Type::T_STRING, |
| 2138 | ], |
| 2139 | 'case_recent_activity_date' => [ |
| 2140 | 'title' => ts('Activity Actual Date'), |
| 2141 | 'type' => CRM_Utils_Type::T_DATE, |
| 2142 | ], |
| 2143 | 'case_scheduled_activity_date' => [ |
| 2144 | 'title' => ts('Activity Scheduled Date'), |
| 2145 | 'type' => CRM_Utils_Type::T_DATE, |
| 2146 | ], |
| 2147 | 'case_recent_activity_type' => [ |
| 2148 | 'title' => ts('Activity Type'), |
| 2149 | 'type' => CRM_Utils_Type::T_STRING, |
| 2150 | ], |
| 2151 | 'case_activity_status' => [ |
| 2152 | 'title' => ts('Activity Status'), |
| 2153 | 'type' => CRM_Utils_Type::T_STRING, |
| 2154 | ], |
| 2155 | 'case_activity_duration' => [ |
| 2156 | 'title' => ts('Activity Duration'), |
| 2157 | 'type' => CRM_Utils_Type::T_INT, |
| 2158 | ], |
| 2159 | 'case_activity_medium_id' => [ |
| 2160 | 'title' => ts('Activity Medium'), |
| 2161 | 'type' => CRM_Utils_Type::T_INT, |
| 2162 | ], |
| 2163 | 'case_activity_details' => [ |
| 2164 | 'title' => ts('Activity Details'), |
| 2165 | 'type' => CRM_Utils_Type::T_TEXT, |
| 2166 | ], |
| 2167 | 'case_activity_is_auto' => [ |
| 2168 | 'title' => ts('Activity Auto-generated?'), |
| 2169 | 'type' => CRM_Utils_Type::T_BOOLEAN, |
| 2170 | ], |
| 2171 | ]; |
| 2172 | } |
| 2173 | |
| 2174 | // add custom data for case activities |
| 2175 | $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Activity')); |
| 2176 | |
| 2177 | self::$_exportableFields[$name] = $fields; |
| 2178 | return self::$_exportableFields[$name]; |
| 2179 | } |
| 2180 | |
| 2181 | /** |
| 2182 | * Get the allowed profile fields for Activities. |
| 2183 | * |
| 2184 | * @return array |
| 2185 | * array of activity profile Fields |
| 2186 | */ |
| 2187 | public static function getProfileFields() { |
| 2188 | $exportableFields = self::exportableFields('Activity'); |
| 2189 | $skipFields = [ |
| 2190 | 'activity_id', |
| 2191 | 'activity_type', |
| 2192 | 'source_contact_id', |
| 2193 | 'source_contact', |
| 2194 | 'activity_campaign', |
| 2195 | 'activity_is_test', |
| 2196 | 'is_current_revision', |
| 2197 | 'activity_is_deleted', |
| 2198 | ]; |
| 2199 | $config = CRM_Core_Config::singleton(); |
| 2200 | if (!in_array('CiviCampaign', $config->enableComponents)) { |
| 2201 | $skipFields[] = 'activity_engagement_level'; |
| 2202 | } |
| 2203 | |
| 2204 | foreach ($skipFields as $field) { |
| 2205 | if (isset($exportableFields[$field])) { |
| 2206 | unset($exportableFields[$field]); |
| 2207 | } |
| 2208 | } |
| 2209 | |
| 2210 | // hack to use 'activity_type_id' instead of 'activity_type' |
| 2211 | $exportableFields['activity_status_id'] = $exportableFields['activity_status']; |
| 2212 | unset($exportableFields['activity_status']); |
| 2213 | |
| 2214 | return $exportableFields; |
| 2215 | } |
| 2216 | |
| 2217 | /** |
| 2218 | * This function deletes the activity record related to contact record. |
| 2219 | * |
| 2220 | * This is conditional on there being no target and assignee record |
| 2221 | * with other contacts. |
| 2222 | * |
| 2223 | * @param int $contactId |
| 2224 | * ContactId. |
| 2225 | * |
| 2226 | * @return true/null |
| 2227 | */ |
| 2228 | public static function cleanupActivity($contactId) { |
| 2229 | $result = NULL; |
| 2230 | if (!$contactId) { |
| 2231 | return $result; |
| 2232 | } |
| 2233 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
| 2234 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
| 2235 | |
| 2236 | $transaction = new CRM_Core_Transaction(); |
| 2237 | |
| 2238 | // delete activity if there is no record in civicrm_activity_contact |
| 2239 | // pointing to any other contact record |
| 2240 | $activityContact = new CRM_Activity_DAO_ActivityContact(); |
| 2241 | $activityContact->contact_id = $contactId; |
| 2242 | $activityContact->record_type_id = $sourceID; |
| 2243 | $activityContact->find(); |
| 2244 | |
| 2245 | while ($activityContact->fetch()) { |
| 2246 | // delete activity_contact record for the deleted contact |
| 2247 | $activityContact->delete(); |
| 2248 | |
| 2249 | $activityContactOther = new CRM_Activity_DAO_ActivityContact(); |
| 2250 | $activityContactOther->activity_id = $activityContact->activity_id; |
| 2251 | |
| 2252 | // delete activity only if no other contacts connected |
| 2253 | if (!$activityContactOther->find(TRUE)) { |
| 2254 | $activityParams = ['id' => $activityContact->activity_id]; |
| 2255 | $result = self::deleteActivity($activityParams); |
| 2256 | } |
| 2257 | |
| 2258 | } |
| 2259 | |
| 2260 | $transaction->commit(); |
| 2261 | |
| 2262 | return $result; |
| 2263 | } |
| 2264 | |
| 2265 | /** |
| 2266 | * Does user has sufficient permission for view/edit activity record. |
| 2267 | * |
| 2268 | * @param int $activityId |
| 2269 | * Activity record id. |
| 2270 | * @param int $action |
| 2271 | * Edit/view. |
| 2272 | * |
| 2273 | * @return bool |
| 2274 | */ |
| 2275 | public static function checkPermission($activityId, $action) { |
| 2276 | |
| 2277 | if (!$activityId || |
| 2278 | !in_array($action, [CRM_Core_Action::UPDATE, CRM_Core_Action::VIEW]) |
| 2279 | ) { |
| 2280 | return FALSE; |
| 2281 | } |
| 2282 | |
| 2283 | $activity = new CRM_Activity_DAO_Activity(); |
| 2284 | $activity->id = $activityId; |
| 2285 | if (!$activity->find(TRUE)) { |
| 2286 | return FALSE; |
| 2287 | } |
| 2288 | |
| 2289 | if (!self::hasPermissionForActivityType($activity->activity_type_id)) { |
| 2290 | // this check is redundant for api access / anything that calls the selectWhereClause |
| 2291 | // to determine ACLs. |
| 2292 | return FALSE; |
| 2293 | } |
| 2294 | // Return early when it is case activity. |
| 2295 | // Check for CiviCase related permission. |
| 2296 | if (CRM_Case_BAO_Case::isCaseActivity($activityId)) { |
| 2297 | return self::isContactPermittedAccessToCaseActivity($activityId, $action, $activity->activity_type_id); |
| 2298 | } |
| 2299 | |
| 2300 | // Check for this permission related to contact. |
| 2301 | $permission = CRM_Core_Permission::VIEW; |
| 2302 | if ($action == CRM_Core_Action::UPDATE) { |
| 2303 | $permission = CRM_Core_Permission::EDIT; |
| 2304 | } |
| 2305 | |
| 2306 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
| 2307 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
| 2308 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); |
| 2309 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
| 2310 | |
| 2311 | // Check for source contact. |
| 2312 | $sourceContactId = self::getActivityContact($activity->id, $sourceID); |
| 2313 | // Account for possibility of activity not having a source contact (as it may have been deleted). |
| 2314 | $allow = $sourceContactId ? CRM_Contact_BAO_Contact_Permission::allow($sourceContactId, $permission) : TRUE; |
| 2315 | if (!$allow) { |
| 2316 | return FALSE; |
| 2317 | } |
| 2318 | |
| 2319 | // Check for target and assignee contacts. |
| 2320 | // First check for supper permission. |
| 2321 | $supPermission = 'view all contacts'; |
| 2322 | if ($action == CRM_Core_Action::UPDATE) { |
| 2323 | $supPermission = 'edit all contacts'; |
| 2324 | } |
| 2325 | $allow = CRM_Core_Permission::check($supPermission); |
| 2326 | |
| 2327 | // User might have sufficient permission, through acls. |
| 2328 | if (!$allow) { |
| 2329 | $allow = TRUE; |
| 2330 | // Get the target contacts. |
| 2331 | $targetContacts = CRM_Activity_BAO_ActivityContact::retrieveContactIdsByActivityId($activity->id, $targetID); |
| 2332 | foreach ($targetContacts as $cnt => $contactId) { |
| 2333 | if (!CRM_Contact_BAO_Contact_Permission::allow($contactId, $permission)) { |
| 2334 | $allow = FALSE; |
| 2335 | break; |
| 2336 | } |
| 2337 | } |
| 2338 | |
| 2339 | // Get the assignee contacts. |
| 2340 | if ($allow) { |
| 2341 | $assigneeContacts = CRM_Activity_BAO_ActivityContact::retrieveContactIdsByActivityId($activity->id, $assigneeID); |
| 2342 | foreach ($assigneeContacts as $cnt => $contactId) { |
| 2343 | if (!CRM_Contact_BAO_Contact_Permission::allow($contactId, $permission)) { |
| 2344 | $allow = FALSE; |
| 2345 | break; |
| 2346 | } |
| 2347 | } |
| 2348 | } |
| 2349 | } |
| 2350 | |
| 2351 | return $allow; |
| 2352 | } |
| 2353 | |
| 2354 | /** |
| 2355 | * Check if the logged in user has permission for the given case activity. |
| 2356 | * |
| 2357 | * @param int $activityId |
| 2358 | * @param int $action |
| 2359 | * @param int $activityTypeID |
| 2360 | * |
| 2361 | * @return bool |
| 2362 | */ |
| 2363 | protected static function isContactPermittedAccessToCaseActivity($activityId, $action, $activityTypeID) { |
| 2364 | $oper = 'view'; |
| 2365 | if ($action == CRM_Core_Action::UPDATE) { |
| 2366 | $oper = 'edit'; |
| 2367 | } |
| 2368 | $allow = CRM_Case_BAO_Case::checkPermission($activityId, |
| 2369 | $oper, |
| 2370 | $activityTypeID |
| 2371 | ); |
| 2372 | |
| 2373 | return $allow; |
| 2374 | } |
| 2375 | |
| 2376 | /** |
| 2377 | * Check if the logged in user has permission to access the given activity type. |
| 2378 | * |
| 2379 | * @param int $activityTypeID |
| 2380 | * |
| 2381 | * @return bool |
| 2382 | */ |
| 2383 | protected static function hasPermissionForActivityType($activityTypeID) { |
| 2384 | $permittedActivityTypes = self::getPermittedActivityTypes(); |
| 2385 | return isset($permittedActivityTypes[$activityTypeID]); |
| 2386 | } |
| 2387 | |
| 2388 | /** |
| 2389 | * Get the activity types the user is permitted to access. |
| 2390 | * |
| 2391 | * The types are filtered by the components they have access to. ie. a user |
| 2392 | * with access CiviContribute but not CiviMember will see contribution related |
| 2393 | * activities and activities with no component (e.g meetings) but not member related ones. |
| 2394 | * |
| 2395 | * @return array |
| 2396 | */ |
| 2397 | protected static function getPermittedActivityTypes() { |
| 2398 | $userID = (int) CRM_Core_Session::getLoggedInContactID(); |
| 2399 | if (!isset(Civi::$statics[__CLASS__]['permitted_activity_types'][$userID])) { |
| 2400 | $permittedActivityTypes = []; |
| 2401 | $components = self::activityComponents(FALSE); |
| 2402 | $componentClause = empty($components) ? '' : (' OR component_id IN (' . implode(', ', array_keys($components)) . ')'); |
| 2403 | |
| 2404 | $types = CRM_Core_DAO::executeQuery( |
| 2405 | " |
| 2406 | SELECT option_value.value activity_type_id |
| 2407 | FROM civicrm_option_value option_value |
| 2408 | INNER JOIN civicrm_option_group grp ON (grp.id = option_group_id AND grp.name = 'activity_type') |
| 2409 | WHERE component_id IS NULL $componentClause")->fetchAll(); |
| 2410 | foreach ($types as $type) { |
| 2411 | $permittedActivityTypes[$type['activity_type_id']] = (int) $type['activity_type_id']; |
| 2412 | } |
| 2413 | Civi::$statics[__CLASS__]['permitted_activity_types'][$userID] = $permittedActivityTypes; |
| 2414 | } |
| 2415 | return Civi::$statics[__CLASS__]['permitted_activity_types'][$userID]; |
| 2416 | } |
| 2417 | |
| 2418 | /** |
| 2419 | * @param $params |
| 2420 | * @return array |
| 2421 | */ |
| 2422 | protected static function getActivityParamsForDashboardFunctions($params) { |
| 2423 | $activityParams = [ |
| 2424 | 'is_deleted' => 0, |
| 2425 | 'is_current_revision' => 1, |
| 2426 | 'is_test' => 0, |
| 2427 | 'contact_id' => CRM_Utils_Array::value('contact_id', $params), |
| 2428 | 'activity_date_time' => CRM_Utils_Array::value('activity_date_time', $params), |
| 2429 | 'check_permissions' => 1, |
| 2430 | 'options' => [ |
| 2431 | 'offset' => CRM_Utils_Array::value('offset', $params, 0), |
| 2432 | ], |
| 2433 | ]; |
| 2434 | |
| 2435 | if (!empty($params['activity_status_id'])) { |
| 2436 | $activityParams['activity_status_id'] = ['IN' => explode(',', $params['activity_status_id'])]; |
| 2437 | } |
| 2438 | |
| 2439 | $activityParams['activity_type_id'] = self::filterActivityTypes($params); |
| 2440 | $enabledComponents = self::activityComponents(); |
| 2441 | // @todo - should we move this to activity get api. |
| 2442 | foreach ([ |
| 2443 | 'case_id' => 'CiviCase', |
| 2444 | 'campaign_id' => 'CiviCampaign', |
| 2445 | ] as $attr => $component) { |
| 2446 | if (!in_array($component, $enabledComponents)) { |
| 2447 | $activityParams[$attr] = ['IS NULL' => 1]; |
| 2448 | } |
| 2449 | } |
| 2450 | return $activityParams; |
| 2451 | } |
| 2452 | |
| 2453 | /** |
| 2454 | * Checks if user has permissions to edit inbound e-mails, either bsic info |
| 2455 | * or both basic information and content. |
| 2456 | * |
| 2457 | * @return bool |
| 2458 | */ |
| 2459 | public static function checkEditInboundEmailsPermissions() { |
| 2460 | if (CRM_Core_Permission::check('edit inbound email basic information') |
| 2461 | || CRM_Core_Permission::check('edit inbound email basic information and content') |
| 2462 | ) { |
| 2463 | return TRUE; |
| 2464 | } |
| 2465 | |
| 2466 | return FALSE; |
| 2467 | } |
| 2468 | |
| 2469 | /** |
| 2470 | * Wrapper for ajax activity selector. |
| 2471 | * |
| 2472 | * @param array $params |
| 2473 | * Associated array for params record id. |
| 2474 | * |
| 2475 | * @return array |
| 2476 | * Associated array of contact activities |
| 2477 | */ |
| 2478 | public static function getContactActivitySelector(&$params) { |
| 2479 | // Format the params. |
| 2480 | $params['offset'] = ($params['page'] - 1) * $params['rp']; |
| 2481 | $params['rowCount'] = $params['rp']; |
| 2482 | $params['sort'] = CRM_Utils_Array::value('sortBy', $params); |
| 2483 | $params['caseId'] = NULL; |
| 2484 | $context = CRM_Utils_Array::value('context', $params); |
| 2485 | $showContactOverlay = !CRM_Utils_String::startsWith($context, "dashlet"); |
| 2486 | $activityTypeInfo = civicrm_api3('OptionValue', 'get', [ |
| 2487 | 'option_group_id' => "activity_type", |
| 2488 | 'options' => ['limit' => 0], |
| 2489 | ]); |
| 2490 | $activityIcons = []; |
| 2491 | foreach ($activityTypeInfo['values'] as $type) { |
| 2492 | if (!empty($type['icon'])) { |
| 2493 | $activityIcons[$type['value']] = $type['icon']; |
| 2494 | } |
| 2495 | } |
| 2496 | CRM_Utils_Date::convertFormDateToApiFormat($params, 'activity_date_time'); |
| 2497 | |
| 2498 | // Get contact activities. |
| 2499 | $activities = CRM_Activity_BAO_Activity::getActivities($params); |
| 2500 | |
| 2501 | // Add total. |
| 2502 | $params['total'] = CRM_Activity_BAO_Activity::getActivitiesCount($params); |
| 2503 | |
| 2504 | // Format params and add links. |
| 2505 | $contactActivities = []; |
| 2506 | |
| 2507 | if (!empty($activities)) { |
| 2508 | $activityStatus = CRM_Core_PseudoConstant::activityStatus(); |
| 2509 | |
| 2510 | // Check logged in user for permission. |
| 2511 | $page = new CRM_Core_Page(); |
| 2512 | CRM_Contact_Page_View::checkUserPermission($page, $params['contact_id']); |
| 2513 | $permissions = [$page->_permission]; |
| 2514 | if (CRM_Core_Permission::check('delete activities')) { |
| 2515 | $permissions[] = CRM_Core_Permission::DELETE; |
| 2516 | } |
| 2517 | |
| 2518 | $mask = CRM_Core_Action::mask($permissions); |
| 2519 | |
| 2520 | foreach ($activities as $activityId => $values) { |
| 2521 | $activity = ['source_contact_name' => '', 'target_contact_name' => '']; |
| 2522 | $activity['DT_RowId'] = $activityId; |
| 2523 | // Add class to this row if overdue. |
| 2524 | $activity['DT_RowClass'] = "crm-entity status-id-{$values['status_id']}"; |
| 2525 | if (self::isOverdue($values)) { |
| 2526 | $activity['DT_RowClass'] .= ' status-overdue'; |
| 2527 | } |
| 2528 | else { |
| 2529 | $activity['DT_RowClass'] .= ' status-ontime'; |
| 2530 | } |
| 2531 | |
| 2532 | $activity['DT_RowAttr'] = []; |
| 2533 | $activity['DT_RowAttr']['data-entity'] = 'activity'; |
| 2534 | $activity['DT_RowAttr']['data-id'] = $activityId; |
| 2535 | |
| 2536 | $activity['activity_type'] = (!empty($activityIcons[$values['activity_type_id']]) ? '<span class="crm-i ' . $activityIcons[$values['activity_type_id']] . '"></span> ' : '') . $values['activity_type']; |
| 2537 | $activity['subject'] = $values['subject']; |
| 2538 | |
| 2539 | if ($params['contact_id'] == $values['source_contact_id']) { |
| 2540 | $activity['source_contact_name'] = $values['source_contact_name']; |
| 2541 | } |
| 2542 | elseif ($values['source_contact_id']) { |
| 2543 | $srcTypeImage = ""; |
| 2544 | if ($showContactOverlay) { |
| 2545 | $srcTypeImage = CRM_Contact_BAO_Contact_Utils::getImage( |
| 2546 | CRM_Contact_BAO_Contact::getContactType($values['source_contact_id']), |
| 2547 | FALSE, |
| 2548 | $values['source_contact_id']); |
| 2549 | } |
| 2550 | $activity['source_contact_name'] = $srcTypeImage . CRM_Utils_System::href($values['source_contact_name'], |
| 2551 | 'civicrm/contact/view', "reset=1&cid={$values['source_contact_id']}"); |
| 2552 | } |
| 2553 | else { |
| 2554 | $activity['source_contact_name'] = '<em>n/a</em>'; |
| 2555 | } |
| 2556 | |
| 2557 | if (isset($values['mailingId']) && !empty($values['mailingId'])) { |
| 2558 | $activity['target_contact'] = CRM_Utils_System::href($values['recipients'], |
| 2559 | 'civicrm/mailing/report/event', |
| 2560 | "mid={$values['source_record_id']}&reset=1&event=queue&cid={$params['contact_id']}&context=activitySelector"); |
| 2561 | } |
| 2562 | elseif (!empty($values['recipients'])) { |
| 2563 | $activity['target_contact_name'] = $values['recipients']; |
| 2564 | } |
| 2565 | elseif (isset($values['target_contact_count']) && $values['target_contact_count']) { |
| 2566 | $activity['target_contact_name'] = ''; |
| 2567 | $firstTargetName = reset($values['target_contact_name']); |
| 2568 | $firstTargetContactID = key($values['target_contact_name']); |
| 2569 | |
| 2570 | $targetLink = CRM_Utils_System::href($firstTargetName, 'civicrm/contact/view', "reset=1&cid={$firstTargetContactID}"); |
| 2571 | if ($showContactOverlay) { |
| 2572 | $targetTypeImage = CRM_Contact_BAO_Contact_Utils::getImage( |
| 2573 | CRM_Contact_BAO_Contact::getContactType($firstTargetContactID), |
| 2574 | FALSE, |
| 2575 | $firstTargetContactID); |
| 2576 | $activity['target_contact_name'] .= "<div>$targetTypeImage $targetLink"; |
| 2577 | } |
| 2578 | else { |
| 2579 | $activity['target_contact_name'] .= $targetLink; |
| 2580 | } |
| 2581 | |
| 2582 | if ($extraCount = $values['target_contact_count'] - 1) { |
| 2583 | $activity['target_contact_name'] .= ";<br />" . "(" . ts('%1 more', [1 => $extraCount]) . ")"; |
| 2584 | } |
| 2585 | if ($showContactOverlay) { |
| 2586 | $activity['target_contact_name'] .= "</div> "; |
| 2587 | } |
| 2588 | } |
| 2589 | elseif (!$values['target_contact_name']) { |
| 2590 | $activity['target_contact_name'] = '<em>n/a</em>'; |
| 2591 | } |
| 2592 | |
| 2593 | $activity['assignee_contact_name'] = ''; |
| 2594 | if (empty($values['assignee_contact_name'])) { |
| 2595 | $activity['assignee_contact_name'] = '<em>n/a</em>'; |
| 2596 | } |
| 2597 | elseif (!empty($values['assignee_contact_name'])) { |
| 2598 | $count = 0; |
| 2599 | $activity['assignee_contact_name'] = ''; |
| 2600 | foreach ($values['assignee_contact_name'] as $acID => $acName) { |
| 2601 | if ($acID && $count < 5) { |
| 2602 | $assigneeTypeImage = ""; |
| 2603 | $assigneeLink = CRM_Utils_System::href($acName, 'civicrm/contact/view', "reset=1&cid={$acID}"); |
| 2604 | if ($showContactOverlay) { |
| 2605 | $assigneeTypeImage = CRM_Contact_BAO_Contact_Utils::getImage( |
| 2606 | CRM_Contact_BAO_Contact::getContactType($acID), |
| 2607 | FALSE, |
| 2608 | $acID); |
| 2609 | $activity['assignee_contact_name'] .= "<div>$assigneeTypeImage $assigneeLink"; |
| 2610 | } |
| 2611 | else { |
| 2612 | $activity['assignee_contact_name'] .= $assigneeLink; |
| 2613 | } |
| 2614 | |
| 2615 | $count++; |
| 2616 | if ($count) { |
| 2617 | $activity['assignee_contact_name'] .= "; "; |
| 2618 | } |
| 2619 | if ($showContactOverlay) { |
| 2620 | $activity['assignee_contact_name'] .= "</div> "; |
| 2621 | } |
| 2622 | |
| 2623 | if ($count == 4) { |
| 2624 | $activity['assignee_contact_name'] .= "(" . ts('more') . ")"; |
| 2625 | break; |
| 2626 | } |
| 2627 | } |
| 2628 | } |
| 2629 | } |
| 2630 | |
| 2631 | $activity['activity_date_time'] = CRM_Utils_Date::customFormat($values['activity_date_time']); |
| 2632 | $activity['status_id'] = $activityStatus[$values['status_id']]; |
| 2633 | |
| 2634 | // build links |
| 2635 | $activity['links'] = ''; |
| 2636 | $accessMailingReport = FALSE; |
| 2637 | if (!empty($values['mailingId'])) { |
| 2638 | $accessMailingReport = TRUE; |
| 2639 | } |
| 2640 | |
| 2641 | $actionLinks = CRM_Activity_Selector_Activity::actionLinks( |
| 2642 | CRM_Utils_Array::value('activity_type_id', $values), |
| 2643 | CRM_Utils_Array::value('source_record_id', $values), |
| 2644 | $accessMailingReport, |
| 2645 | CRM_Utils_Array::value('activity_id', $values) |
| 2646 | ); |
| 2647 | |
| 2648 | $actionMask = array_sum(array_keys($actionLinks)) & $mask; |
| 2649 | |
| 2650 | $activity['links'] = CRM_Core_Action::formLink($actionLinks, |
| 2651 | $actionMask, |
| 2652 | [ |
| 2653 | 'id' => $values['activity_id'], |
| 2654 | 'cid' => $params['contact_id'], |
| 2655 | 'cxt' => $context, |
| 2656 | 'caseid' => CRM_Utils_Array::value('case_id', $values), |
| 2657 | ], |
| 2658 | ts('more'), |
| 2659 | FALSE, |
| 2660 | 'activity.tab.row', |
| 2661 | 'Activity', |
| 2662 | $values['activity_id'] |
| 2663 | ); |
| 2664 | |
| 2665 | if ($values['is_recurring_activity']) { |
| 2666 | $activity['is_recurring_activity'] = CRM_Core_BAO_RecurringEntity::getPositionAndCount($values['activity_id'], 'civicrm_activity'); |
| 2667 | } |
| 2668 | |
| 2669 | array_push($contactActivities, $activity); |
| 2670 | } |
| 2671 | } |
| 2672 | |
| 2673 | $activitiesDT = []; |
| 2674 | $activitiesDT['data'] = $contactActivities; |
| 2675 | $activitiesDT['recordsTotal'] = $params['total']; |
| 2676 | $activitiesDT['recordsFiltered'] = $params['total']; |
| 2677 | |
| 2678 | return $activitiesDT; |
| 2679 | } |
| 2680 | |
| 2681 | /** |
| 2682 | * Copy custom fields and attachments from an existing activity to another. |
| 2683 | * |
| 2684 | * @see CRM_Case_Page_AJAX::_convertToCaseActivity() |
| 2685 | * |
| 2686 | * @param array $params |
| 2687 | */ |
| 2688 | public static function copyExtendedActivityData($params) { |
| 2689 | // attach custom data to the new activity |
| 2690 | $customParams = $htmlType = []; |
| 2691 | $customValues = CRM_Core_BAO_CustomValueTable::getEntityValues($params['activityID'], 'Activity'); |
| 2692 | |
| 2693 | if (!empty($customValues)) { |
| 2694 | $fieldIds = implode(', ', array_keys($customValues)); |
| 2695 | $sql = "SELECT id FROM civicrm_custom_field WHERE html_type = 'File' AND id IN ( {$fieldIds} )"; |
| 2696 | $result = CRM_Core_DAO::executeQuery($sql); |
| 2697 | |
| 2698 | while ($result->fetch()) { |
| 2699 | $htmlType[] = $result->id; |
| 2700 | } |
| 2701 | |
| 2702 | foreach ($customValues as $key => $value) { |
| 2703 | if ($value !== NULL) { |
| 2704 | // CRM-10542 |
| 2705 | if (in_array($key, $htmlType)) { |
| 2706 | $fileValues = CRM_Core_BAO_File::path($value, $params['activityID']); |
| 2707 | $customParams["custom_{$key}_-1"] = [ |
| 2708 | 'name' => $fileValues[0], |
| 2709 | 'type' => $fileValues[1], |
| 2710 | ]; |
| 2711 | } |
| 2712 | else { |
| 2713 | $customParams["custom_{$key}_-1"] = $value; |
| 2714 | } |
| 2715 | } |
| 2716 | } |
| 2717 | CRM_Core_BAO_CustomValueTable::postProcess($customParams, 'civicrm_activity', |
| 2718 | $params['mainActivityId'], 'Activity' |
| 2719 | ); |
| 2720 | } |
| 2721 | |
| 2722 | // copy activity attachments ( if any ) |
| 2723 | CRM_Core_BAO_File::copyEntityFile('civicrm_activity', $params['activityID'], 'civicrm_activity', $params['mainActivityId']); |
| 2724 | } |
| 2725 | |
| 2726 | /** |
| 2727 | * Get activity contact. |
| 2728 | * |
| 2729 | * @param int $activityId |
| 2730 | * @param int $recordTypeID |
| 2731 | * @param string $column |
| 2732 | * |
| 2733 | * @return null |
| 2734 | */ |
| 2735 | public static function getActivityContact($activityId, $recordTypeID = NULL, $column = 'contact_id') { |
| 2736 | $activityContact = new CRM_Activity_BAO_ActivityContact(); |
| 2737 | $activityContact->activity_id = $activityId; |
| 2738 | if ($recordTypeID) { |
| 2739 | $activityContact->record_type_id = $recordTypeID; |
| 2740 | } |
| 2741 | if ($activityContact->find(TRUE)) { |
| 2742 | return $activityContact->$column; |
| 2743 | } |
| 2744 | return NULL; |
| 2745 | } |
| 2746 | |
| 2747 | /** |
| 2748 | * Get source contact id. |
| 2749 | * |
| 2750 | * @param int $activityId |
| 2751 | * |
| 2752 | * @return null |
| 2753 | */ |
| 2754 | public static function getSourceContactID($activityId) { |
| 2755 | static $sourceID = NULL; |
| 2756 | if (!$sourceID) { |
| 2757 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
| 2758 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
| 2759 | } |
| 2760 | |
| 2761 | return self::getActivityContact($activityId, $sourceID); |
| 2762 | } |
| 2763 | |
| 2764 | /** |
| 2765 | * Set api filter. |
| 2766 | * |
| 2767 | * @todo Document what this is for. |
| 2768 | * |
| 2769 | * @param array $params |
| 2770 | */ |
| 2771 | public function setApiFilter(&$params) { |
| 2772 | if (!empty($params['target_contact_id'])) { |
| 2773 | $this->selectAdd(); |
| 2774 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
| 2775 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
| 2776 | $obj = new CRM_Activity_BAO_ActivityContact(); |
| 2777 | $params['return.target_contact_id'] = 1; |
| 2778 | $this->joinAdd($obj, 'LEFT'); |
| 2779 | $this->selectAdd('civicrm_activity.*'); |
| 2780 | $this->whereAdd(" civicrm_activity_contact.contact_id = {$params['target_contact_id']} AND civicrm_activity_contact.record_type_id = {$targetID}"); |
| 2781 | } |
| 2782 | } |
| 2783 | |
| 2784 | /** |
| 2785 | * Send activity as attachment. |
| 2786 | * |
| 2787 | * @param object $activity |
| 2788 | * @param array $mailToContacts |
| 2789 | * @param array $params |
| 2790 | * |
| 2791 | * @return bool |
| 2792 | */ |
| 2793 | public static function sendToAssignee($activity, $mailToContacts, $params = []) { |
| 2794 | if (!CRM_Utils_Array::crmIsEmptyArray($mailToContacts)) { |
| 2795 | $clientID = CRM_Utils_Array::value('client_id', $params); |
| 2796 | $caseID = CRM_Utils_Array::value('case_id', $params); |
| 2797 | |
| 2798 | $ics = new CRM_Activity_BAO_ICalendar($activity); |
| 2799 | $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_activity', $activity->id); |
| 2800 | $ics->addAttachment($attachments, $mailToContacts); |
| 2801 | |
| 2802 | $result = CRM_Case_BAO_Case::sendActivityCopy($clientID, $activity->id, $mailToContacts, $attachments, $caseID); |
| 2803 | $ics->cleanup(); |
| 2804 | return $result; |
| 2805 | } |
| 2806 | return FALSE; |
| 2807 | } |
| 2808 | |
| 2809 | /** |
| 2810 | * @return array |
| 2811 | */ |
| 2812 | public static function getEntityRefFilters() { |
| 2813 | return [ |
| 2814 | ['key' => 'activity_type_id', 'value' => ts('Activity Type')], |
| 2815 | ['key' => 'status_id', 'value' => ts('Activity Status')], |
| 2816 | ]; |
| 2817 | } |
| 2818 | |
| 2819 | } |