| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.3 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 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-2013 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * This class create activities for a case |
| 38 | * |
| 39 | */ |
| 40 | class CRM_Case_Form_Activity extends CRM_Activity_Form_Activity { |
| 41 | |
| 42 | /** |
| 43 | * The default variable defined |
| 44 | * |
| 45 | * @var int |
| 46 | */ |
| 47 | public $_caseId; |
| 48 | |
| 49 | /** |
| 50 | * The default case type variable defined |
| 51 | * |
| 52 | * @var int |
| 53 | */ |
| 54 | public $_caseType; |
| 55 | |
| 56 | /** |
| 57 | * The default values of an activity |
| 58 | * |
| 59 | * @var array |
| 60 | */ |
| 61 | public $_defaults = array(); |
| 62 | |
| 63 | /** |
| 64 | * The array of releted contact info |
| 65 | * |
| 66 | * @var array |
| 67 | */ |
| 68 | public $_relatedContacts; |
| 69 | |
| 70 | /** |
| 71 | * Function to build the form |
| 72 | * |
| 73 | * @return None |
| 74 | * @access public |
| 75 | */ |
| 76 | function preProcess() { |
| 77 | $this->_caseId = CRM_Utils_Request::retrieve('caseid', 'Positive', $this); |
| 78 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); |
| 79 | if (!$this->_context) { |
| 80 | $this->_context = 'caseActivity'; |
| 81 | } |
| 82 | $this->_crmDir = 'Case'; |
| 83 | $this->assign('context', $this->_context); |
| 84 | |
| 85 | $result = parent::preProcess(); |
| 86 | |
| 87 | $scheduleStatusId = CRM_Core_OptionGroup::getValue('activity_status', 'Scheduled', 'name'); |
| 88 | $this->assign('scheduleStatusId', $scheduleStatusId); |
| 89 | |
| 90 | if ($this->_cdType) { |
| 91 | return $result; |
| 92 | } |
| 93 | |
| 94 | if (!$this->_caseId && $this->_activityId) { |
| 95 | $this->_caseId = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseActivity', $this->_activityId, |
| 96 | 'case_id', 'activity_id' |
| 97 | ); |
| 98 | } |
| 99 | if ($this->_caseId) { |
| 100 | $this->assign('caseId', $this->_caseId); |
| 101 | } |
| 102 | |
| 103 | if (!$this->_caseId || |
| 104 | (!$this->_activityId && !$this->_activityTypeId) |
| 105 | ) { |
| 106 | CRM_Core_Error::fatal('required params missing.'); |
| 107 | } |
| 108 | |
| 109 | //check for case activity access. |
| 110 | if (!CRM_Case_BAO_Case::accessCiviCase()) { |
| 111 | CRM_Core_Error::fatal(ts('You are not authorized to access this page.')); |
| 112 | } |
| 113 | //validate case id. |
| 114 | if ($this->_caseId && |
| 115 | !CRM_Core_Permission::check('access all cases and activities') |
| 116 | ) { |
| 117 | $session = CRM_Core_Session::singleton(); |
| 118 | $allCases = CRM_Case_BAO_Case::getCases(TRUE, $session->get('userID')); |
| 119 | if (!array_key_exists($this->_caseId, $allCases)) { |
| 120 | CRM_Core_Error::fatal(ts('You are not authorized to access this page.')); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | //validate case activity id. |
| 125 | if ($this->_activityId && |
| 126 | ($this->_action & CRM_Core_Action::UPDATE) |
| 127 | ) { |
| 128 | $valid = CRM_Case_BAO_Case::checkPermission($this->_activityId, 'edit', |
| 129 | $this->_activityTypeId |
| 130 | ); |
| 131 | if (!$valid) { |
| 132 | CRM_Core_Error::fatal(ts('You are not authorized to access this page.')); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | $this->_caseType = CRM_Case_BAO_Case::getCaseType($this->_caseId, 'name'); |
| 137 | $this->assign('caseType', $this->_caseType); |
| 138 | |
| 139 | $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process(); |
| 140 | $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients(); |
| 141 | $this->assign('multiClient', $isMultiClient); |
| 142 | |
| 143 | $clients = CRM_Case_BAO_Case::getContactNames($this->_caseId); |
| 144 | $this->assign('client_names', $clients); |
| 145 | |
| 146 | // set context for pushUserContext and for statusBounce |
| 147 | if ($this->_context == 'fulltext') { |
| 148 | if ($this->_action == CRM_Core_Action::UPDATE || $this->_action == CRM_Core_Action::DELETE) { |
| 149 | $url = CRM_Utils_System::url('civicrm/contact/view/case', |
| 150 | "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$this->_caseId}&show=1&context={$this->_context}" |
| 151 | ); |
| 152 | } |
| 153 | else { |
| 154 | $url = CRM_Utils_System::url('civicrm/contact/search/custom', 'force=1'); |
| 155 | } |
| 156 | } |
| 157 | else { |
| 158 | $url = CRM_Utils_System::url('civicrm/contact/view/case', |
| 159 | "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$this->_caseId}&show=1" |
| 160 | ); |
| 161 | } |
| 162 | if (!$this->_activityId) { |
| 163 | $caseTypes = CRM_Case_PseudoConstant::caseType(); |
| 164 | |
| 165 | if (empty($caseTypes) && ($this->_activityTypeName == 'Change Case Type') && !$this->_caseId) { |
| 166 | $url = CRM_Utils_System::url('civicrm/contact/view/case', |
| 167 | "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$this->_caseId}&show=1" |
| 168 | ); |
| 169 | $session = CRM_Core_Session::singleton(); |
| 170 | $session->pushUserContext($url); |
| 171 | CRM_Core_Error::statusBounce(ts("You do not have any active Case Types")); |
| 172 | } |
| 173 | |
| 174 | // check if activity count is within the limit |
| 175 | $xmlProcessor = new CRM_Case_XMLProcessor_Process(); |
| 176 | $activityInst = $xmlProcessor->getMaxInstance($this->_caseType); |
| 177 | |
| 178 | // If not bounce back and also provide activity edit link |
| 179 | if (isset($activityInst[$this->_activityTypeName])) { |
| 180 | $activityCount = CRM_Case_BAO_Case::getCaseActivityCount($this->_caseId, $this->_activityTypeId); |
| 181 | if ($activityCount >= $activityInst[$this->_activityTypeName]) { |
| 182 | if ($activityInst[$this->_activityTypeName] == 1) { |
| 183 | $atArray = array('activity_type_id' => $this->_activityTypeId); |
| 184 | $activities = CRM_Case_BAO_Case::getCaseActivity($this->_caseId, |
| 185 | $atArray, |
| 186 | $this->_currentUserId |
| 187 | ); |
| 188 | $activities = array_keys($activities); |
| 189 | $activities = $activities[0]; |
| 190 | $editUrl = CRM_Utils_System::url('civicrm/case/activity', |
| 191 | "reset=1&cid={$this->_currentlyViewedContactId}&caseid={$this->_caseId}&action=update&id={$activities}" |
| 192 | ); |
| 193 | } |
| 194 | CRM_Core_Error::statusBounce(ts("You can not add another '%1' activity to this case. %2", |
| 195 | array( |
| 196 | 1 => $this->_activityTypeName, |
| 197 | 2 => "Do you want to <a href='$editUrl'>edit the existing activity</a> ?" |
| 198 | ) |
| 199 | ), |
| 200 | $url |
| 201 | ); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | if ($this->_currentlyViewedContactId) { |
| 207 | CRM_Contact_Page_View::setTitle($this->_currentlyViewedContactId); |
| 208 | } |
| 209 | |
| 210 | $session = CRM_Core_Session::singleton(); |
| 211 | $session->pushUserContext($url); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * This function sets the default values for the form. For edit/view mode |
| 216 | * the default values are retrieved from the database |
| 217 | * |
| 218 | * @access public |
| 219 | * |
| 220 | * @return None |
| 221 | */ |
| 222 | function setDefaultValues() { |
| 223 | $this->_defaults = parent::setDefaultValues(); |
| 224 | $targetContactValues = array(); |
| 225 | |
| 226 | //get all clients. |
| 227 | $clients = CRM_Case_BAO_Case::getContactNames($this->_caseId); |
| 228 | if (isset($this->_activityId) && empty($_POST)) { |
| 229 | if (!CRM_Utils_Array::crmIsEmptyArray($this->_defaults['target_contact'])) { |
| 230 | $targetContactValues = array_combine(array_unique($this->_defaults['target_contact']), |
| 231 | explode(';', trim($this->_defaults['target_contact_value'])) |
| 232 | ); |
| 233 | |
| 234 | //exclude all clients. |
| 235 | foreach ($clients as $clientId => $vals) { |
| 236 | if (array_key_exists($clientId, $targetContactValues)) { |
| 237 | unset($targetContactValues[$clientId]); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | $this->assign('targetContactValues', empty($targetContactValues) ? FALSE : $targetContactValues); |
| 243 | |
| 244 | //return form for ajax |
| 245 | if ($this->_cdType) { |
| 246 | return $this->_defaults; |
| 247 | } |
| 248 | |
| 249 | if (isset($this->_encounterMedium)) { |
| 250 | $this->_defaults['medium_id'] = $this->_encounterMedium; |
| 251 | } |
| 252 | elseif (empty($this->_defaults['medium_id'])) { |
| 253 | // set default encounter medium CRM-4816 |
| 254 | $medium = CRM_Core_OptionGroup::values('encounter_medium', FALSE, FALSE, FALSE, 'AND is_default = 1'); |
| 255 | if (count($medium) == 1) { |
| 256 | $this->_defaults['medium_id'] = key($medium); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | return $this->_defaults; |
| 261 | } |
| 262 | |
| 263 | public function buildQuickForm() { |
| 264 | $this->_fields['source_contact_id']['label'] = ts('Reported By'); |
| 265 | $this->_fields['status_id']['attributes'] = array('' => ts('- select -')) + CRM_Core_PseudoConstant::activityStatus(); |
| 266 | |
| 267 | if ($this->_caseType) { |
| 268 | $xmlProcessor = new CRM_Case_XMLProcessor_Process(); |
| 269 | $aTypes = $xmlProcessor->get($this->_caseType, 'ActivityTypes', TRUE); |
| 270 | |
| 271 | // remove Open Case activity type since we're inside an existing case |
| 272 | $openCaseID = CRM_Core_OptionGroup::getValue('activity_type', 'Open Case', 'name'); |
| 273 | unset($aTypes[$openCaseID]); |
| 274 | asort($aTypes); |
| 275 | $this->_fields['followup_activity_type_id']['attributes'] = array( |
| 276 | '' => '- select activity type -') + $aTypes; |
| 277 | } |
| 278 | |
| 279 | $result = parent::buildQuickForm(); |
| 280 | |
| 281 | if ($this->_action & (CRM_Core_Action::DELETE | CRM_Core_Action::DETACH | CRM_Core_Action::RENEW)) { |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | if ($this->_cdType) { |
| 286 | return $result; |
| 287 | } |
| 288 | |
| 289 | $this->assign('urlPath', 'civicrm/case/activity'); |
| 290 | |
| 291 | $encounterMediums = CRM_Case_PseudoConstant::encounterMedium(); |
| 292 | if ($this->_activityTypeFile == 'OpenCase') { |
| 293 | $this->_encounterMedium = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $this->_activityId, |
| 294 | 'medium_id' |
| 295 | ); |
| 296 | if (!array_key_exists($this->_encounterMedium, $encounterMediums)) { |
| 297 | $encounterMediums[$this->_encounterMedium] = CRM_Core_OptionGroup::getLabel('encounter_medium', |
| 298 | $this->_encounterMedium, |
| 299 | FALSE |
| 300 | ); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | $this->add('select', 'medium_id', ts('Medium'), $encounterMediums, TRUE); |
| 305 | |
| 306 | $this->_relatedContacts = CRM_Case_BAO_Case::getRelatedAndGlobalContacts($this->_caseId); |
| 307 | //add case client in send a copy selector.CRM-4438. |
| 308 | $relatedContacts = CRM_Case_BAO_Case::getContactNames($this->_caseId); |
| 309 | if (!empty($relatedContacts)) { |
| 310 | foreach ($relatedContacts as $relatedContact) { |
| 311 | $this->_relatedContacts[] = $relatedContact; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | if (!empty($this->_relatedContacts)) { |
| 316 | $checkBoxes = array(); |
| 317 | foreach ($this->_relatedContacts as $id => $row) { |
| 318 | $checkBoxes[$id] = $this->addElement('checkbox', $id, NULL, ''); |
| 319 | } |
| 320 | |
| 321 | $this->addGroup($checkBoxes, 'contact_check'); |
| 322 | $this->addElement('checkbox', 'toggleSelect', NULL, NULL, |
| 323 | array('onclick' => "return toggleCheckboxVals('contact_check',this);") |
| 324 | ); |
| 325 | $this->assign('searchRows', $this->_relatedContacts); |
| 326 | } |
| 327 | |
| 328 | $this->addFormRule(array('CRM_Case_Form_Activity', 'formRule'), $this); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * global form rule |
| 333 | * |
| 334 | * @param array $fields the input form values |
| 335 | * @param array $files the uploaded files if any |
| 336 | * @param array $options additional user data |
| 337 | * |
| 338 | * @return true if no errors, else array of errors |
| 339 | * @access public |
| 340 | * @static |
| 341 | */ |
| 342 | static function formRule($fields, $files, $self) { |
| 343 | // skip form rule if deleting |
| 344 | if (CRM_Utils_Array::value('_qf_Activity_next_', $fields) == 'Delete' || CRM_Utils_Array::value('_qf_Activity_next_', $fields) == 'Restore') { |
| 345 | return TRUE; |
| 346 | } |
| 347 | |
| 348 | return parent::formrule($fields, $files, $self); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Function to process the form |
| 353 | * |
| 354 | * @access public |
| 355 | * |
| 356 | * @return None |
| 357 | */ |
| 358 | public function postProcess() { |
| 359 | $transaction = new CRM_Core_Transaction(); |
| 360 | |
| 361 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 362 | $statusMsg = NULL; |
| 363 | |
| 364 | //block deleting activities which affects |
| 365 | //case attributes.CRM-4543 |
| 366 | $activityCondition = " AND v.name IN ('Open Case', 'Change Case Type', 'Change Case Status', 'Change Case Start Date')"; |
| 367 | $caseAttributeActivities = CRM_Core_OptionGroup::values('activity_type', FALSE, FALSE, FALSE, $activityCondition); |
| 368 | |
| 369 | if (!array_key_exists($this->_activityTypeId, $caseAttributeActivities)) { |
| 370 | $params = array('id' => $this->_activityId); |
| 371 | $activityDelete = CRM_Activity_BAO_Activity::deleteActivity($params, TRUE); |
| 372 | if ($activityDelete) { |
| 373 | $statusMsg = ts('The selected activity has been moved to the Trash. You can view and / or restore deleted activities by checking "Deleted Activities" from the Case Activities search filter (under Manage Case).<br />'); |
| 374 | } |
| 375 | } |
| 376 | else { |
| 377 | $statusMsg = ts("Selected Activity cannot be deleted."); |
| 378 | } |
| 379 | |
| 380 | $tagParams = array( |
| 381 | 'entity_table' => 'civicrm_activity', |
| 382 | 'entity_id' => $this->_activityId |
| 383 | ); |
| 384 | CRM_Core_BAO_EntityTag::del($tagParams); |
| 385 | |
| 386 | CRM_Core_Session::setStatus('', $statusMsg, 'info'); |
| 387 | return; |
| 388 | } |
| 389 | |
| 390 | if ($this->_action & CRM_Core_Action::RENEW) { |
| 391 | $statusMsg = NULL; |
| 392 | $params = array('id' => $this->_activityId); |
| 393 | $activityRestore = CRM_Activity_BAO_Activity::restoreActivity($params); |
| 394 | if ($activityRestore) { |
| 395 | $statusMsg = ts('The selected activity has been restored.<br />'); |
| 396 | } |
| 397 | CRM_Core_Session::setStatus('', $statusMsg, 'info'); |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | // store the submitted values in an array |
| 402 | $params = $this->controller->exportValues($this->_name); |
| 403 | if ($params['source_contact_id']) { |
| 404 | $params['source_contact_id'] = $params['source_contact_qid']; |
| 405 | } |
| 406 | |
| 407 | //set parent id if its edit mode |
| 408 | if ($parentId = CRM_Utils_Array::value('parent_id', $this->_defaults)) { |
| 409 | $params['parent_id'] = $parentId; |
| 410 | } |
| 411 | |
| 412 | // required for status msg |
| 413 | $recordStatus = 'created'; |
| 414 | |
| 415 | // store the dates with proper format |
| 416 | $params['activity_date_time'] = CRM_Utils_Date::processDate($params['activity_date_time'], $params['activity_date_time_time']); |
| 417 | $params['activity_type_id'] = $this->_activityTypeId; |
| 418 | |
| 419 | // format with contact (target contact) values |
| 420 | if (isset($params['contact'][1])) { |
| 421 | $params['target_contact_id'] = explode(',', $params['contact'][1]); |
| 422 | } |
| 423 | else { |
| 424 | $params['target_contact_id'] = array(); |
| 425 | } |
| 426 | |
| 427 | // format activity custom data |
| 428 | if (CRM_Utils_Array::value('hidden_custom', $params)) { |
| 429 | if ($this->_activityId) { |
| 430 | // unset custom fields-id from params since we want custom |
| 431 | // fields to be saved for new activity. |
| 432 | foreach ($params as $key => $value) { |
| 433 | $match = array(); |
| 434 | if (preg_match('/^(custom_\d+_)(\d+)$/', $key, $match)) { |
| 435 | $params[$match[1] . '-1'] = $params[$key]; |
| 436 | |
| 437 | // for autocomplete transfer hidden value instead of label |
| 438 | if ($params[$key] && isset($params[$key . '_id'])) { |
| 439 | $params[$match[1] . '-1_id'] = $params[$key . '_id']; |
| 440 | unset($params[$key . '_id']); |
| 441 | } |
| 442 | unset($params[$key]); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | // build custom data getFields array |
| 448 | $customFields = CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE, $this->_activityTypeId); |
| 449 | $customFields = CRM_Utils_Array::crmArrayMerge($customFields, |
| 450 | CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE, |
| 451 | NULL, NULL, TRUE |
| 452 | ) |
| 453 | ); |
| 454 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, |
| 455 | $customFields, |
| 456 | $this->_activityId, |
| 457 | 'Activity' |
| 458 | ); |
| 459 | } |
| 460 | |
| 461 | // assigning formated value |
| 462 | if (CRM_Utils_Array::value('assignee_contact_id', $params)) { |
| 463 | $params['assignee_contact_id'] = explode(',', $params['assignee_contact_id']); |
| 464 | } |
| 465 | else { |
| 466 | $params['assignee_contact_id'] = array(); |
| 467 | } |
| 468 | |
| 469 | |
| 470 | if (isset($this->_activityId)) { |
| 471 | // activity which hasn't been modified by a user yet |
| 472 | if ($this->_defaults['is_auto'] == 1) { |
| 473 | $params['is_auto'] = 0; |
| 474 | } |
| 475 | |
| 476 | // always create a revision of an case activity. CRM-4533 |
| 477 | $newActParams = $params; |
| 478 | |
| 479 | // add target contact values in update mode |
| 480 | if (empty($params['target_contact_id']) && !empty($this->_defaults['target_contact'])) { |
| 481 | $newActParams['target_contact_id'] = $this->_defaults['target_contact']; |
| 482 | } |
| 483 | |
| 484 | // record status for status msg |
| 485 | $recordStatus = 'updated'; |
| 486 | } |
| 487 | |
| 488 | if (!isset($newActParams)) { |
| 489 | // add more attachments if needed for old activity |
| 490 | CRM_Core_BAO_File::formatAttachment($params, |
| 491 | $params, |
| 492 | 'civicrm_activity' |
| 493 | ); |
| 494 | |
| 495 | // call begin post process, before the activity is created/updated. |
| 496 | $this->beginPostProcess($params); |
| 497 | $params['case_id'] = $this->_caseId; |
| 498 | // activity create/update |
| 499 | $activity = CRM_Activity_BAO_Activity::create($params); |
| 500 | |
| 501 | // call end post process, after the activity has been created/updated. |
| 502 | $this->endPostProcess($params, $activity); |
| 503 | } |
| 504 | else { |
| 505 | // since the params we need to set are very few, and we don't want rest of the |
| 506 | // work done by bao create method , lets use dao object to make the changes |
| 507 | $params = array('id' => $this->_activityId); |
| 508 | $params['is_current_revision'] = 0; |
| 509 | $activity = new CRM_Activity_DAO_Activity(); |
| 510 | $activity->copyValues($params); |
| 511 | $activity->save(); |
| 512 | } |
| 513 | |
| 514 | // create a new version of activity if activity was found to |
| 515 | // have been modified/created by user |
| 516 | if (isset($newActParams)) { |
| 517 | // set proper original_id |
| 518 | if (CRM_Utils_Array::value('original_id', $this->_defaults)) { |
| 519 | $newActParams['original_id'] = $this->_defaults['original_id']; |
| 520 | } |
| 521 | else { |
| 522 | $newActParams['original_id'] = $activity->id; |
| 523 | } |
| 524 | //is_current_revision will be set to 1 by default. |
| 525 | |
| 526 | // add attachments if any |
| 527 | CRM_Core_BAO_File::formatAttachment($newActParams, |
| 528 | $newActParams, |
| 529 | 'civicrm_activity' |
| 530 | ); |
| 531 | |
| 532 | // call begin post process, before the activity is created/updated. |
| 533 | $this->beginPostProcess($newActParams); |
| 534 | $newActParams['case_id'] = $this->_caseId; |
| 535 | |
| 536 | $activity = CRM_Activity_BAO_Activity::create($newActParams); |
| 537 | |
| 538 | // call end post process, after the activity has been created/updated. |
| 539 | $this->endPostProcess($newActParams, $activity); |
| 540 | |
| 541 | // copy files attached to old activity if any, to new one, |
| 542 | // as long as users have not selected the 'delete attachment' option. |
| 543 | if (!CRM_Utils_Array::value('is_delete_attachment', $newActParams)) { |
| 544 | CRM_Core_BAO_File::copyEntityFile('civicrm_activity', $this->_activityId, |
| 545 | 'civicrm_activity', $activity->id |
| 546 | ); |
| 547 | } |
| 548 | |
| 549 | // copy back params to original var |
| 550 | $params = $newActParams; |
| 551 | } |
| 552 | |
| 553 | if ($activity->id) { |
| 554 | // add tags if exists |
| 555 | $tagParams = array(); |
| 556 | if (!empty($params['tag'])) { |
| 557 | foreach ($params['tag'] as $tag) { |
| 558 | $tagParams[$tag] = 1; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | //save static tags |
| 563 | CRM_Core_BAO_EntityTag::create($tagParams, 'civicrm_activity', $activity->id); |
| 564 | |
| 565 | //save free tags |
| 566 | if (isset($params['taglist']) && !empty($params['taglist'])) { |
| 567 | CRM_Core_Form_Tag::postProcess($params['taglist'], $activity->id, 'civicrm_activity', $this); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | // update existing case record if needed |
| 572 | $caseParams = $params; |
| 573 | $caseParams['id'] = $this->_caseId; |
| 574 | |
| 575 | if (CRM_Utils_Array::value('case_type_id', $caseParams)) { |
| 576 | $caseParams['case_type_id'] = CRM_Core_DAO::VALUE_SEPARATOR . $caseParams['case_type_id'] . CRM_Core_DAO::VALUE_SEPARATOR; |
| 577 | } |
| 578 | if (CRM_Utils_Array::value('case_status_id', $caseParams)) { |
| 579 | $caseParams['status_id'] = $caseParams['case_status_id']; |
| 580 | } |
| 581 | |
| 582 | // unset params intended for activities only |
| 583 | unset($caseParams['subject'], $caseParams['details'], |
| 584 | $caseParams['status_id'], $caseParams['custom'] |
| 585 | ); |
| 586 | $case = CRM_Case_BAO_Case::create($caseParams); |
| 587 | |
| 588 | // create case activity record |
| 589 | $caseParams = array( |
| 590 | 'activity_id' => $activity->id, |
| 591 | 'case_id' => $this->_caseId, |
| 592 | ); |
| 593 | CRM_Case_BAO_Case::processCaseActivity($caseParams); |
| 594 | |
| 595 | $activityContacts = CRM_Core_PseudoConstant::activityContacts('name'); |
| 596 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); |
| 597 | // create activity assignee records |
| 598 | $assigneeParams = array( |
| 599 | 'activity_id' => $activity->id, |
| 600 | 'record_type_id' => $assigneeID |
| 601 | ); |
| 602 | |
| 603 | if (!CRM_Utils_Array::crmIsEmptyArray($params['assignee_contact_id'])) { |
| 604 | //skip those assignee contacts which are already assigned |
| 605 | //while sending a copy.CRM-4509. |
| 606 | $activityAssigned = array_flip($params['assignee_contact_id']); |
| 607 | $activityId = isset($this->_activityId) ? $this->_activityId : $activity->id; |
| 608 | $assigneeContacts = CRM_Activity_BAO_ActivityContact::getNames($activityId, $assigneeID); |
| 609 | $activityAssigned = array_diff_key($activityAssigned, $assigneeContacts); |
| 610 | |
| 611 | foreach ($params['assignee_contact_id'] as $key => $id) { |
| 612 | $assigneeParams['contact_id'] = $id; |
| 613 | CRM_Activity_BAO_ActivityContact::create($assigneeParams); |
| 614 | } |
| 615 | //modify assigne_contact as per newly assigned contact before sending copy. CRM-4509. |
| 616 | $params['assignee_contact_id'] = $activityAssigned; |
| 617 | } |
| 618 | |
| 619 | // Insert civicrm_log record for the activity (e.g. store the |
| 620 | // created / edited by contact id and date for the activity) |
| 621 | // Note - civicrm_log is already created by CRM_Activity_BAO_Activity::create() |
| 622 | |
| 623 | |
| 624 | // send copy to selected contacts. |
| 625 | $mailStatus = ''; |
| 626 | $mailToContacts = array(); |
| 627 | |
| 628 | //CRM-5695 |
| 629 | //check for notification settings for assignee contacts |
| 630 | $selectedContacts = array('contact_check'); |
| 631 | $activityContacts = CRM_Core_PseudoConstant::activityContacts('name'); |
| 632 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); |
| 633 | if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, |
| 634 | 'activity_assignee_notification' |
| 635 | )) { |
| 636 | $selectedContacts[] = 'assignee_contact_id'; |
| 637 | } |
| 638 | |
| 639 | foreach ($selectedContacts as $dnt => $val) { |
| 640 | if (array_key_exists($val, $params) && !CRM_Utils_array::crmIsEmptyArray($params[$val])) { |
| 641 | if ($val == 'contact_check') { |
| 642 | $mailStatus = ts("A copy of the activity has also been sent to selected contacts(s)."); |
| 643 | } |
| 644 | else { |
| 645 | $this->_relatedContacts = CRM_Activity_BAO_ActivityContact::getNames($activity->id, $assigneeID, TRUE, FALSE); |
| 646 | $mailStatus .= ' ' . ts("A copy of the activity has also been sent to assignee contacts(s)."); |
| 647 | } |
| 648 | //build an associative array with unique email addresses. |
| 649 | foreach ($params[$val] as $id => $dnc) { |
| 650 | if (isset($id) && array_key_exists($id, $this->_relatedContacts)) { |
| 651 | //if email already exists in array then append with ', ' another role only otherwise add it to array. |
| 652 | if ($contactDetails = CRM_Utils_Array::value($this->_relatedContacts[$id]['email'], $mailToContacts)) { |
| 653 | $caseRole = CRM_Utils_Array::value('role', $this->_relatedContacts[$id]); |
| 654 | $mailToContacts[$this->_relatedContacts[$id]['email']]['role'] = $contactDetails['role'] . ', ' . $caseRole; |
| 655 | } |
| 656 | else { |
| 657 | $mailToContacts[$this->_relatedContacts[$id]['email']] = $this->_relatedContacts[$id]; |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | if (!CRM_Utils_array::crmIsEmptyArray($mailToContacts)) { |
| 665 | //include attachments while sendig a copy of activity. |
| 666 | $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_activity', |
| 667 | $activity->id |
| 668 | ); |
| 669 | |
| 670 | $ics = new CRM_Activity_BAO_ICalendar( $activity ); |
| 671 | $ics->addAttachment( $attachments, $mailToContacts ); |
| 672 | |
| 673 | $result = CRM_Case_BAO_Case::sendActivityCopy($this->_currentlyViewedContactId, |
| 674 | $activity->id, $mailToContacts, $attachments, $this->_caseId |
| 675 | ); |
| 676 | |
| 677 | $ics->cleanup(); |
| 678 | |
| 679 | if (empty($result)) { |
| 680 | $mailStatus = ''; |
| 681 | } |
| 682 | } |
| 683 | else { |
| 684 | $mailStatus = ''; |
| 685 | } |
| 686 | |
| 687 | // create follow up activity if needed |
| 688 | $followupStatus = ''; |
| 689 | if (CRM_Utils_Array::value('followup_activity_type_id', $params)) { |
| 690 | $followupActivity = CRM_Activity_BAO_Activity::createFollowupActivity($activity->id, $params); |
| 691 | |
| 692 | if ($followupActivity) { |
| 693 | $caseParams = array( |
| 694 | 'activity_id' => $followupActivity->id, |
| 695 | 'case_id' => $this->_caseId, |
| 696 | ); |
| 697 | CRM_Case_BAO_Case::processCaseActivity($caseParams); |
| 698 | $followupStatus = ts("A followup activity has been scheduled."); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | CRM_Core_Session::setStatus('', ts("'%1' activity has been %2. %3 %4", |
| 703 | array( |
| 704 | 1 => $this->_activityTypeName, |
| 705 | 2 => $recordStatus, |
| 706 | 3 => $followupStatus, |
| 707 | 4 => $mailStatus |
| 708 | ) |
| 709 | ), 'info'); |
| 710 | } |
| 711 | } |
| 712 | |