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