Merge pull request #3072 from mlutfy/master-crm14594
[civicrm-core.git] / CRM / Case / Form / Activity.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class create activities for a case
38 *
39 */
40class 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 *
355ba699 73 * @return void
6a488035
TO
74 * @access public
75 */
76 function preProcess() {
725fd9d9
N
77 $caseIds = CRM_Utils_Request::retrieve('caseid', 'String', $this);
78 $this->_caseId = explode(',', $caseIds);
6a488035
TO
79 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this);
80 if (!$this->_context) {
81 $this->_context = 'caseActivity';
82 }
83 $this->_crmDir = 'Case';
84 $this->assign('context', $this->_context);
85
86 $result = parent::preProcess();
87
88 $scheduleStatusId = CRM_Core_OptionGroup::getValue('activity_status', 'Scheduled', 'name');
89 $this->assign('scheduleStatusId', $scheduleStatusId);
90
91 if ($this->_cdType) {
92 return $result;
93 }
94
95 if (!$this->_caseId && $this->_activityId) {
96 $this->_caseId = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseActivity', $this->_activityId,
97 'case_id', 'activity_id'
98 );
99 }
100 if ($this->_caseId) {
101 $this->assign('caseId', $this->_caseId);
725fd9d9 102 $this->assign('countId', count($this->_caseId));
6a488035
TO
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();
ef0ae32d 120 $allCases = CRM_Case_BAO_Case::getCases(TRUE, $session->get('userID'), 'any');
6a488035
TO
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
725fd9d9
N
138 foreach ($this->_caseId as $key => $val) {
139 $this->_caseType[] = CRM_Case_BAO_Case::getCaseType($val, 'name');
140 }
6a488035
TO
141 $this->assign('caseType', $this->_caseType);
142
143 $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
144 $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients();
145 $this->assign('multiClient', $isMultiClient);
146
725fd9d9
N
147 foreach ($this->_caseId as $key => $val) {
148 $clients[] = CRM_Case_BAO_Case::getContactNames($val);
149 }
6a488035
TO
150 $this->assign('client_names', $clients);
151
725fd9d9 152 $caseIds = implode(',', $this->_caseId);
6a488035
TO
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',
725fd9d9 157 "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$caseIds}&show=1&context={$this->_context}"
6a488035
TO
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',
725fd9d9 166 "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$caseIds}&show=1"
6a488035
TO
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',
725fd9d9 174 "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$caseIds}&show=1"
6a488035
TO
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();
725fd9d9
N
183 foreach ($this->_caseType as $key => $val) {
184 $activityInst = $xmlProcessor->getMaxInstance($val);
6a488035
TO
185
186 // If not bounce back and also provide activity edit link
725fd9d9
N
187 if (isset($activityInst[$this->_activityTypeName])) {
188 $activityCount = CRM_Case_BAO_Case::getCaseActivityCount($val, $this->_activityTypeId);
189 if ($activityCount >= $activityInst[$this->_activityTypeName]) {
190 if ($activityInst[$this->_activityTypeName] == 1) {
191 $atArray = array('activity_type_id' => $this->_activityTypeId);
192 $activities = CRM_Case_BAO_Case::getCaseActivity($val,
6a488035
TO
193 $atArray,
194 $this->_currentUserId
725fd9d9
N
195 );
196 $activities = array_keys($activities);
197 $activities = $activities[0];
198 $editUrl = CRM_Utils_System::url('civicrm/case/activity',
199 "reset=1&cid={$this->_currentlyViewedContactId}&caseid={$val}&action=update&id={$activities}"
200 );
201 }
202 CRM_Core_Error::statusBounce(ts("You can not add another '%1' activity to this case. %2",
203 array(
204 1 => $this->_activityTypeName,
205 2 => "Do you want to <a href='$editUrl'>edit the existing activity</a> ?"
206 )
207 ),
208 $url
6a488035
TO
209 );
210 }
6a488035
TO
211 }
212 }
213 }
214
6a488035
TO
215 $session = CRM_Core_Session::singleton();
216 $session->pushUserContext($url);
217 }
218
219 /**
220 * This function sets the default values for the form. For edit/view mode
221 * the default values are retrieved from the database
222 *
223 * @access public
224 *
355ba699 225 * @return void
6a488035
TO
226 */
227 function setDefaultValues() {
228 $this->_defaults = parent::setDefaultValues();
229 $targetContactValues = array();
725fd9d9
N
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 }
6a488035
TO
243 }
244 }
245 }
725fd9d9 246 $this->assign('targetContactValues', empty($targetContactValues) ? FALSE : $targetContactValues);
6a488035 247
725fd9d9
N
248 //return form for ajax
249 if ($this->_cdType) {
250 return $this->_defaults;
251 }
6a488035 252
725fd9d9
N
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 }
6a488035 262 }
6a488035 263
725fd9d9
N
264 return $this->_defaults;
265 }
6a488035
TO
266 }
267
268 public function buildQuickForm() {
269 $this->_fields['source_contact_id']['label'] = ts('Reported By');
f7305cbc 270 unset($this->_fields['status_id']['attributes']['required']);
6a488035
TO
271
272 if ($this->_caseType) {
273 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
725fd9d9
N
274 $aTypes = array();
275 foreach ($this->_caseType as $key => $val) {
276 $activityTypes = $xmlProcessor->get($val, 'ActivityTypes', TRUE);
277 $aTypes = $aTypes + $activityTypes;
278 }
6a488035
TO
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(
50237bc9 285 '' => '- select activity type -'
286 ) + $aTypes;
6a488035
TO
287 }
288
289 $result = parent::buildQuickForm();
290
291 if ($this->_action & (CRM_Core_Action::DELETE | CRM_Core_Action::DETACH | CRM_Core_Action::RENEW)) {
292 return;
293 }
294
295 if ($this->_cdType) {
296 return $result;
297 }
298
299 $this->assign('urlPath', 'civicrm/case/activity');
300
301 $encounterMediums = CRM_Case_PseudoConstant::encounterMedium();
475e9f44
CW
302 // 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.
303 // Is that really a big problem?
6a488035
TO
304 if ($this->_activityTypeFile == 'OpenCase') {
305 $this->_encounterMedium = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $this->_activityId,
306 'medium_id'
307 );
308 if (!array_key_exists($this->_encounterMedium, $encounterMediums)) {
309 $encounterMediums[$this->_encounterMedium] = CRM_Core_OptionGroup::getLabel('encounter_medium',
310 $this->_encounterMedium,
311 FALSE
312 );
313 }
314 }
315
316 $this->add('select', 'medium_id', ts('Medium'), $encounterMediums, TRUE);
725fd9d9
N
317 $i = 0;
318 foreach ($this->_caseId as $key => $val) {
319 $this->_relatedContacts[] = CRM_Case_BAO_Case::getRelatedAndGlobalContacts($val);
320 $contName = CRM_Case_BAO_Case::getContactNames($val);
321 foreach ($contName as $nkey => $nval) {
322 array_push($this->_relatedContacts[$i][0] , $this->_relatedContacts[$i][0]['managerOf']= $nval['display_name']);
323 }
324 $i++;
325 }
6a488035 326
6a488035 327 //add case client in send a copy selector.CRM-4438.
725fd9d9
N
328 foreach ($this->_caseId as $key => $val) {
329 $relatedContacts[] = CRM_Case_BAO_Case::getContactNames($val);
330 }
331
6a488035
TO
332 if (!empty($relatedContacts)) {
333 foreach ($relatedContacts as $relatedContact) {
334 $this->_relatedContacts[] = $relatedContact;
335 }
336 }
337
338 if (!empty($this->_relatedContacts)) {
339 $checkBoxes = array();
340 foreach ($this->_relatedContacts as $id => $row) {
03a2a396 341 $checkBoxes[$id] = $this->addElement('checkbox', $id, NULL, NULL, array('class' => 'select-row'));
6a488035
TO
342 }
343
344 $this->addGroup($checkBoxes, 'contact_check');
345 $this->addElement('checkbox', 'toggleSelect', NULL, NULL,
d664f648 346 array('class' => 'select-rows')
6a488035
TO
347 );
348 $this->assign('searchRows', $this->_relatedContacts);
349 }
350
351 $this->addFormRule(array('CRM_Case_Form_Activity', 'formRule'), $this);
352 }
353
354 /**
355 * global form rule
356 *
357 * @param array $fields the input form values
358 * @param array $files the uploaded files if any
359 * @param array $options additional user data
360 *
361 * @return true if no errors, else array of errors
362 * @access public
363 * @static
364 */
365 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 * Function to process the form
376 *
377 * @access public
378 *
355ba699 379 * @return void
6a488035 380 */
2d4a6b57 381 public function postProcess($params = NULL) {
6a488035
TO
382 $transaction = new CRM_Core_Transaction();
383
384 if ($this->_action & CRM_Core_Action::DELETE) {
385 $statusMsg = NULL;
386
387 //block deleting activities which affects
388 //case attributes.CRM-4543
389 $activityCondition = " AND v.name IN ('Open Case', 'Change Case Type', 'Change Case Status', 'Change Case Start Date')";
390 $caseAttributeActivities = CRM_Core_OptionGroup::values('activity_type', FALSE, FALSE, FALSE, $activityCondition);
391
392 if (!array_key_exists($this->_activityTypeId, $caseAttributeActivities)) {
393 $params = array('id' => $this->_activityId);
394 $activityDelete = CRM_Activity_BAO_Activity::deleteActivity($params, TRUE);
395 if ($activityDelete) {
396 $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 />');
397 }
398 }
399 else {
400 $statusMsg = ts("Selected Activity cannot be deleted.");
401 }
402
403 $tagParams = array(
404 'entity_table' => 'civicrm_activity',
405 'entity_id' => $this->_activityId
406 );
407 CRM_Core_BAO_EntityTag::del($tagParams);
408
409 CRM_Core_Session::setStatus('', $statusMsg, 'info');
410 return;
411 }
412
413 if ($this->_action & CRM_Core_Action::RENEW) {
50237bc9 414 $statusMsg = NULL;
415 $params = array('id' => $this->_activityId);
6a488035
TO
416 $activityRestore = CRM_Activity_BAO_Activity::restoreActivity($params);
417 if ($activityRestore) {
418 $statusMsg = ts('The selected activity has been restored.<br />');
419 }
420 CRM_Core_Session::setStatus('', $statusMsg, 'info');
421 return;
422 }
423
424 // store the submitted values in an array
425 $params = $this->controller->exportValues($this->_name);
6a488035
TO
426
427 //set parent id if its edit mode
428 if ($parentId = CRM_Utils_Array::value('parent_id', $this->_defaults)) {
429 $params['parent_id'] = $parentId;
430 }
431
432 // required for status msg
433 $recordStatus = 'created';
434
435 // store the dates with proper format
436 $params['activity_date_time'] = CRM_Utils_Date::processDate($params['activity_date_time'], $params['activity_date_time_time']);
437 $params['activity_type_id'] = $this->_activityTypeId;
438
439 // format with contact (target contact) values
440 if (isset($params['contact'][1])) {
441 $params['target_contact_id'] = explode(',', $params['contact'][1]);
442 }
443 else {
444 $params['target_contact_id'] = array();
445 }
446
447 // format activity custom data
a7488080 448 if (!empty($params['hidden_custom'])) {
6a488035
TO
449 if ($this->_activityId) {
450 // unset custom fields-id from params since we want custom
451 // fields to be saved for new activity.
452 foreach ($params as $key => $value) {
453 $match = array();
454 if (preg_match('/^(custom_\d+_)(\d+)$/', $key, $match)) {
455 $params[$match[1] . '-1'] = $params[$key];
456
457 // for autocomplete transfer hidden value instead of label
458 if ($params[$key] && isset($params[$key . '_id'])) {
459 $params[$match[1] . '-1_id'] = $params[$key . '_id'];
460 unset($params[$key . '_id']);
461 }
462 unset($params[$key]);
463 }
464 }
465 }
466
467 // build custom data getFields array
468 $customFields = CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE, $this->_activityTypeId);
469 $customFields = CRM_Utils_Array::crmArrayMerge($customFields,
470 CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE,
471 NULL, NULL, TRUE
472 )
473 );
474 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
475 $customFields,
476 $this->_activityId,
477 'Activity'
478 );
479 }
480
50237bc9 481 // assigning formatted value
a7488080 482 if (!empty($params['assignee_contact_id'])) {
6a488035
TO
483 $params['assignee_contact_id'] = explode(',', $params['assignee_contact_id']);
484 }
485 else {
486 $params['assignee_contact_id'] = array();
487 }
488
6a488035
TO
489 if (isset($this->_activityId)) {
490 // activity which hasn't been modified by a user yet
491 if ($this->_defaults['is_auto'] == 1) {
492 $params['is_auto'] = 0;
493 }
494
495 // always create a revision of an case activity. CRM-4533
496 $newActParams = $params;
497
498 // add target contact values in update mode
499 if (empty($params['target_contact_id']) && !empty($this->_defaults['target_contact'])) {
500 $newActParams['target_contact_id'] = $this->_defaults['target_contact'];
501 }
502
503 // record status for status msg
504 $recordStatus = 'updated';
505 }
506
507 if (!isset($newActParams)) {
508 // add more attachments if needed for old activity
509 CRM_Core_BAO_File::formatAttachment($params,
510 $params,
511 'civicrm_activity'
512 );
513
514 // call begin post process, before the activity is created/updated.
515 $this->beginPostProcess($params);
725fd9d9
N
516 foreach ($this->_caseId as $key => $val) {
517 $params['case_id'] = $val;
518 // activity create/update
519 $activity = CRM_Activity_BAO_Activity::create($params);
520 $vvalue[] = array('case_id' => $val, 'actId' => $activity->id);
521 // call end post process, after the activity has been created/updated.
522 $this->endPostProcess($params, $activity);
523 }
6a488035
TO
524 }
525 else {
526 // since the params we need to set are very few, and we don't want rest of the
527 // work done by bao create method , lets use dao object to make the changes
528 $params = array('id' => $this->_activityId);
529 $params['is_current_revision'] = 0;
530 $activity = new CRM_Activity_DAO_Activity();
531 $activity->copyValues($params);
532 $activity->save();
533 }
534
535 // create a new version of activity if activity was found to
536 // have been modified/created by user
537 if (isset($newActParams)) {
538 // set proper original_id
a7488080 539 if (!empty($this->_defaults['original_id'])) {
6a488035
TO
540 $newActParams['original_id'] = $this->_defaults['original_id'];
541 }
542 else {
543 $newActParams['original_id'] = $activity->id;
544 }
545 //is_current_revision will be set to 1 by default.
546
547 // add attachments if any
548 CRM_Core_BAO_File::formatAttachment($newActParams,
549 $newActParams,
550 'civicrm_activity'
551 );
552
553 // call begin post process, before the activity is created/updated.
554 $this->beginPostProcess($newActParams);
725fd9d9
N
555 foreach ($this->_caseId as $key => $val) {
556 $newActParams['case_id'] = $val;
557 $activity = CRM_Activity_BAO_Activity::create($newActParams);
558 $vvalue[] = array('case_id'=> $val, 'actId'=> $activity->id);
559 // call end post process, after the activity has been created/updated.
560 $this->endPostProcess($newActParams, $activity);
561 }
6a488035
TO
562 // copy files attached to old activity if any, to new one,
563 // as long as users have not selected the 'delete attachment' option.
a7488080 564 if (empty($newActParams['is_delete_attachment'])) {
6a488035
TO
565 CRM_Core_BAO_File::copyEntityFile('civicrm_activity', $this->_activityId,
566 'civicrm_activity', $activity->id
567 );
568 }
569
570 // copy back params to original var
571 $params = $newActParams;
572 }
573
725fd9d9
N
574 foreach ($vvalue as $vkey => $vval) {
575 if ($vval['actId']) {
576 // add tags if exists
577 $tagParams = array();
578 if (!empty($params['tag'])) {
579 foreach ($params['tag'] as $tag) {
580 $tagParams[$tag] = 1;
581 }
6a488035 582 }
6a488035 583
725fd9d9
N
584 //save static tags
585 CRM_Core_BAO_EntityTag::create($tagParams, 'civicrm_activity', $vval['actId']);
6a488035 586
725fd9d9
N
587 //save free tags
588 if (isset($params['taglist']) && !empty($params['taglist'])) {
589 CRM_Core_Form_Tag::postProcess($params['taglist'], $vval['actId'], 'civicrm_activity', $this);
590 }
6a488035 591 }
6a488035 592
725fd9d9
N
593 // update existing case record if needed
594 $caseParams = $params;
595 $caseParams['id'] = $vval['case_id'];
596 if (!empty($caseParams['case_type_id'])) {
597 $caseParams['case_type_id'] = CRM_Core_DAO::VALUE_SEPARATOR . $caseParams['case_type_id'] . CRM_Core_DAO::VALUE_SEPARATOR;
598 }
599 if (!empty($caseParams['case_status_id'])) {
600 $caseParams['status_id'] = $caseParams['case_status_id'];
601 }
6a488035 602
725fd9d9
N
603 // unset params intended for activities only
604 unset($caseParams['subject'], $caseParams['details'],
605 $caseParams['status_id'], $caseParams['custom']
606 );
607 $case = CRM_Case_BAO_Case::create($caseParams);
608 // create case activity record
609 $caseParams = array(
610 'activity_id' => $vval['actId'],
611 'case_id' => $vval['case_id'],
612 );
613 CRM_Case_BAO_Case::processCaseActivity($caseParams);
6a488035
TO
614 }
615
6a488035
TO
616 // Insert civicrm_log record for the activity (e.g. store the
617 // created / edited by contact id and date for the activity)
618 // Note - civicrm_log is already created by CRM_Activity_BAO_Activity::create()
8ef12e64 619
6a488035
TO
620 // send copy to selected contacts.
621 $mailStatus = '';
622 $mailToContacts = array();
623
624 //CRM-5695
625 //check for notification settings for assignee contacts
626 $selectedContacts = array('contact_check');
e7e657f0 627 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
034500d4 628 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
6a488035 629 if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
50237bc9 630 'activity_assignee_notification'
631 )
632 ) {
6a488035
TO
633 $selectedContacts[] = 'assignee_contact_id';
634 }
635
725fd9d9
N
636 foreach ($vvalue as $vkey => $vval) {
637 foreach ($selectedContacts as $dnt => $val) {
638 if (array_key_exists($val, $params) && !CRM_Utils_array::crmIsEmptyArray($params[$val])) {
cd122921 639 if ($val == 'contact_check') {
725fd9d9 640 $mailStatus = ts("A copy of the activity has also been sent to selected contacts(s).");
cd122921 641 }
642 else {
725fd9d9
N
643 $this->_relatedContacts = CRM_Activity_BAO_ActivityAssignment::getAssigneeNames(array($vval['actId']), TRUE, FALSE);
644 $mailStatus .= ' ' . ts("A copy of the activity has also been sent to assignee contacts(s).");
cd122921 645 }
725fd9d9
N
646 //build an associative array with unique email addresses.
647 foreach ($params[$val] as $key => $value) {
648 if ($val == 'contact_check') {
649 $id = $key;
6a488035
TO
650 }
651 else {
725fd9d9
N
652 $id = $value;
653 }
654
655 if (isset($id) && array_key_exists($id, $this->_relatedContacts) && isset($this->_relatedContacts[$id]['email'])) {
656 //if email already exists in array then append with ', ' another role only otherwise add it to array.
657 if ($contactDetails = CRM_Utils_Array::value($this->_relatedContacts[$id]['email'], $mailToContacts)) {
658 $caseRole = CRM_Utils_Array::value('role', $this->_relatedContacts[$id]);
659 $mailToContacts[$this->_relatedContacts[$id]['email']]['role'] = $contactDetails['role'] . ', ' . $caseRole;
660 }
661 else {
662 $mailToContacts[$this->_relatedContacts[$id]['email']] = $this->_relatedContacts[$id];
663 }
6a488035
TO
664 }
665 }
666 }
667 }
6a488035 668
725fd9d9
N
669 if (!CRM_Utils_array::crmIsEmptyArray($mailToContacts)) {
670 //include attachments while sending a copy of activity.
671 $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_activity',
672 $vval['actId']
673 );
6a488035 674
725fd9d9
N
675 $ics = new CRM_Activity_BAO_ICalendar($activity);
676 $ics->addAttachment($attachments, $mailToContacts);
677 $result = CRM_Case_BAO_Case::sendActivityCopy($this->_currentlyViewedContactId,
678 $vval['actId'], $mailToContacts, $attachments, $vval['case_id']
679 );
680 $ics->cleanup();
681 if (empty($result)) {
682 $mailStatus = '';
683 }
684 }
685 else {
6a488035
TO
686 $mailStatus = '';
687 }
6a488035 688
725fd9d9
N
689 // create follow up activity if needed
690 $followupStatus = '';
691 if (!empty($params['followup_activity_type_id'])) {
692 $followupActivity = CRM_Activity_BAO_Activity::createFollowupActivity($vval['actId'], $params);
6a488035 693
725fd9d9
N
694 if ($followupActivity) {
695 $caseParams = array(
696 'activity_id' => $followupActivity->id,
697 'case_id' => $vval['case_id'],
698 );
699 CRM_Case_BAO_Case::processCaseActivity($caseParams);
700 $followupStatus = ts("A followup activity has been scheduled.");
701 }
6a488035 702 }
6a488035 703
725fd9d9
N
704 CRM_Core_Session::setStatus('', ts("'%1' activity has been %2. %3 %4",
705 array(
706 1 => $this->_activityTypeName,
707 2 => $recordStatus,
708 3 => $followupStatus,
709 4 => $mailStatus
710 )
711 ), 'info');
712 }
6a488035
TO
713 }
714}
715