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