INFRA-132 - Remove @static annotation
[civicrm-core.git] / CRM / Case / Form / Activity.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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 {
77b97be7 41
6a488035
TO
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 /**
c490a46a 71 * Build the form object
6a488035 72 *
355ba699 73 * @return void
6a488035 74 */
00be9182 75 public function preProcess() {
725fd9d9
N
76 $caseIds = CRM_Utils_Request::retrieve('caseid', 'String', $this);
77 $this->_caseId = explode(',', $caseIds);
6a488035
TO
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);
725fd9d9 101 $this->assign('countId', count($this->_caseId));
ad32fddf 102 $this->assign('caseID', CRM_Utils_Array::first($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
cc30482e
TO
138 foreach ($this->_caseId as $casePos => $caseId) {
139 $this->_caseType[$casePos] = CRM_Case_BAO_Case::getCaseType($caseId, 'name');
725fd9d9 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
cc30482e
TO
147 foreach ($this->_caseId as $casePos => $caseId) {
148 $clients[] = CRM_Case_BAO_Case::getContactNames($caseId);
725fd9d9 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();
cc30482e
TO
183 foreach ($this->_caseId as $casePos => $caseId) {
184 $caseType = $this->_caseType[$casePos];
185 $activityInst = $xmlProcessor->getMaxInstance($caseType);
6a488035 186
cc30482e 187 // If not bounce back and also provide activity edit link
725fd9d9 188 if (isset($activityInst[$this->_activityTypeName])) {
cc30482e 189 $activityCount = CRM_Case_BAO_Case::getCaseActivityCount($caseId, $this->_activityTypeId);
725fd9d9
N
190 if ($activityCount >= $activityInst[$this->_activityTypeName]) {
191 if ($activityInst[$this->_activityTypeName] == 1) {
192 $atArray = array('activity_type_id' => $this->_activityTypeId);
cc30482e 193 $activities = CRM_Case_BAO_Case::getCaseActivity($caseId,
353ffa53
TO
194 $atArray,
195 $this->_currentUserId
725fd9d9
N
196 );
197 $activities = array_keys($activities);
198 $activities = $activities[0];
199 $editUrl = CRM_Utils_System::url('civicrm/case/activity',
cc30482e 200 "reset=1&cid={$this->_currentlyViewedContactId}&caseid={$caseId}&action=update&id={$activities}"
725fd9d9
N
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,
21dfd5f5 206 2 => "Do you want to <a href='$editUrl'>edit the existing activity</a> ?",
725fd9d9
N
207 )
208 ),
209 $url
6a488035
TO
210 );
211 }
6a488035
TO
212 }
213 }
214 }
215
6a488035
TO
216 $session = CRM_Core_Session::singleton();
217 $session->pushUserContext($url);
218 }
219
220 /**
c490a46a 221 * Set default values for the form. For edit/view mode
6a488035
TO
222 * the default values are retrieved from the database
223 *
6a488035 224 *
355ba699 225 * @return void
6a488035 226 */
00be9182 227 public function setDefaultValues() {
6a488035
TO
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(
21dfd5f5 285 '' => '- select activity type -',
50237bc9 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) {
a82f59a1 319 $this->_relatedContacts[] = $rgc = CRM_Case_BAO_Case::getRelatedAndGlobalContacts($val);
725fd9d9
N
320 $contName = CRM_Case_BAO_Case::getContactNames($val);
321 foreach ($contName as $nkey => $nval) {
e547f744 322 array_push($this->_relatedContacts[$i][0], $this->_relatedContacts[$i][0]['managerOf'] = $nval['display_name']);
725fd9d9
N
323 }
324 $i++;
325 }
6a488035 326
6a488035 327 //add case client in send a copy selector.CRM-4438.
725fd9d9 328 foreach ($this->_caseId as $key => $val) {
e547f744 329 $relatedContacts[] = $relCon = CRM_Case_BAO_Case::getContactNames($val);
725fd9d9
N
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 }
a82f59a1 350 $this->_relatedContacts = $rgc + $relCon;
6a488035
TO
351
352 $this->addFormRule(array('CRM_Case_Form_Activity', 'formRule'), $this);
353 }
354
355 /**
100fef9d 356 * Global form rule
6a488035 357 *
64bd5a0e
TO
358 * @param array $fields
359 * The input form values.
360 * @param array $files
361 * The uploaded files if any.
77b97be7
EM
362 * @param $self
363 *
72b3a70c
CW
364 * @return bool|array
365 * true if no errors, else array of errors
6a488035 366 */
00be9182 367 public static function formRule($fields, $files, $self) {
6a488035
TO
368 // skip form rule if deleting
369 if (CRM_Utils_Array::value('_qf_Activity_next_', $fields) == 'Delete' || CRM_Utils_Array::value('_qf_Activity_next_', $fields) == 'Restore') {
370 return TRUE;
371 }
372
373 return parent::formrule($fields, $files, $self);
374 }
375
376 /**
c490a46a 377 * Process the form submission
6a488035 378 *
6a488035 379 *
100fef9d 380 * @param array $params
77b97be7 381 *
355ba699 382 * @return void
6a488035 383 */
2d4a6b57 384 public function postProcess($params = NULL) {
6a488035
TO
385 $transaction = new CRM_Core_Transaction();
386
387 if ($this->_action & CRM_Core_Action::DELETE) {
388 $statusMsg = NULL;
389
390 //block deleting activities which affects
391 //case attributes.CRM-4543
392 $activityCondition = " AND v.name IN ('Open Case', 'Change Case Type', 'Change Case Status', 'Change Case Start Date')";
393 $caseAttributeActivities = CRM_Core_OptionGroup::values('activity_type', FALSE, FALSE, FALSE, $activityCondition);
394
395 if (!array_key_exists($this->_activityTypeId, $caseAttributeActivities)) {
396 $params = array('id' => $this->_activityId);
397 $activityDelete = CRM_Activity_BAO_Activity::deleteActivity($params, TRUE);
398 if ($activityDelete) {
399 $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 />');
400 }
401 }
402 else {
403 $statusMsg = ts("Selected Activity cannot be deleted.");
404 }
405
406 $tagParams = array(
407 'entity_table' => 'civicrm_activity',
21dfd5f5 408 'entity_id' => $this->_activityId,
6a488035
TO
409 );
410 CRM_Core_BAO_EntityTag::del($tagParams);
411
412 CRM_Core_Session::setStatus('', $statusMsg, 'info');
413 return;
414 }
415
416 if ($this->_action & CRM_Core_Action::RENEW) {
50237bc9 417 $statusMsg = NULL;
418 $params = array('id' => $this->_activityId);
6a488035
TO
419 $activityRestore = CRM_Activity_BAO_Activity::restoreActivity($params);
420 if ($activityRestore) {
421 $statusMsg = ts('The selected activity has been restored.<br />');
422 }
423 CRM_Core_Session::setStatus('', $statusMsg, 'info');
424 return;
425 }
426
427 // store the submitted values in an array
428 $params = $this->controller->exportValues($this->_name);
6a488035
TO
429
430 //set parent id if its edit mode
431 if ($parentId = CRM_Utils_Array::value('parent_id', $this->_defaults)) {
432 $params['parent_id'] = $parentId;
433 }
434
435 // required for status msg
436 $recordStatus = 'created';
437
438 // store the dates with proper format
439 $params['activity_date_time'] = CRM_Utils_Date::processDate($params['activity_date_time'], $params['activity_date_time_time']);
440 $params['activity_type_id'] = $this->_activityTypeId;
441
442 // format with contact (target contact) values
3911d992
DJ
443 if (isset($params['target_contact_id'])) {
444 $params['target_contact_id'] = explode(',', $params['target_contact_id']);
6a488035
TO
445 }
446 else {
447 $params['target_contact_id'] = array();
448 }
449
450 // format activity custom data
a7488080 451 if (!empty($params['hidden_custom'])) {
6a488035
TO
452 if ($this->_activityId) {
453 // unset custom fields-id from params since we want custom
454 // fields to be saved for new activity.
455 foreach ($params as $key => $value) {
456 $match = array();
457 if (preg_match('/^(custom_\d+_)(\d+)$/', $key, $match)) {
458 $params[$match[1] . '-1'] = $params[$key];
459
460 // for autocomplete transfer hidden value instead of label
461 if ($params[$key] && isset($params[$key . '_id'])) {
462 $params[$match[1] . '-1_id'] = $params[$key . '_id'];
463 unset($params[$key . '_id']);
464 }
465 unset($params[$key]);
466 }
467 }
468 }
469
470 // build custom data getFields array
471 $customFields = CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE, $this->_activityTypeId);
472 $customFields = CRM_Utils_Array::crmArrayMerge($customFields,
473 CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE,
474 NULL, NULL, TRUE
475 )
476 );
477 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
478 $customFields,
479 $this->_activityId,
480 'Activity'
481 );
482 }
483
50237bc9 484 // assigning formatted value
a7488080 485 if (!empty($params['assignee_contact_id'])) {
6a488035
TO
486 $params['assignee_contact_id'] = explode(',', $params['assignee_contact_id']);
487 }
488 else {
489 $params['assignee_contact_id'] = array();
490 }
491
6a488035
TO
492 if (isset($this->_activityId)) {
493 // activity which hasn't been modified by a user yet
494 if ($this->_defaults['is_auto'] == 1) {
495 $params['is_auto'] = 0;
496 }
497
498 // always create a revision of an case activity. CRM-4533
499 $newActParams = $params;
500
501 // add target contact values in update mode
502 if (empty($params['target_contact_id']) && !empty($this->_defaults['target_contact'])) {
503 $newActParams['target_contact_id'] = $this->_defaults['target_contact'];
504 }
505
506 // record status for status msg
507 $recordStatus = 'updated';
508 }
509
510 if (!isset($newActParams)) {
511 // add more attachments if needed for old activity
512 CRM_Core_BAO_File::formatAttachment($params,
513 $params,
514 'civicrm_activity'
515 );
516
517 // call begin post process, before the activity is created/updated.
518 $this->beginPostProcess($params);
725fd9d9
N
519 foreach ($this->_caseId as $key => $val) {
520 $params['case_id'] = $val;
521 // activity create/update
522 $activity = CRM_Activity_BAO_Activity::create($params);
523 $vvalue[] = array('case_id' => $val, 'actId' => $activity->id);
524 // call end post process, after the activity has been created/updated.
525 $this->endPostProcess($params, $activity);
526 }
6a488035
TO
527 }
528 else {
529 // since the params we need to set are very few, and we don't want rest of the
530 // work done by bao create method , lets use dao object to make the changes
531 $params = array('id' => $this->_activityId);
532 $params['is_current_revision'] = 0;
533 $activity = new CRM_Activity_DAO_Activity();
534 $activity->copyValues($params);
535 $activity->save();
536 }
537
538 // create a new version of activity if activity was found to
539 // have been modified/created by user
540 if (isset($newActParams)) {
541 // set proper original_id
a7488080 542 if (!empty($this->_defaults['original_id'])) {
6a488035
TO
543 $newActParams['original_id'] = $this->_defaults['original_id'];
544 }
545 else {
546 $newActParams['original_id'] = $activity->id;
547 }
548 //is_current_revision will be set to 1 by default.
549
550 // add attachments if any
551 CRM_Core_BAO_File::formatAttachment($newActParams,
552 $newActParams,
553 'civicrm_activity'
554 );
555
556 // call begin post process, before the activity is created/updated.
557 $this->beginPostProcess($newActParams);
725fd9d9
N
558 foreach ($this->_caseId as $key => $val) {
559 $newActParams['case_id'] = $val;
560 $activity = CRM_Activity_BAO_Activity::create($newActParams);
e547f744 561 $vvalue[] = array('case_id' => $val, 'actId' => $activity->id);
725fd9d9
N
562 // call end post process, after the activity has been created/updated.
563 $this->endPostProcess($newActParams, $activity);
564 }
6a488035
TO
565 // copy files attached to old activity if any, to new one,
566 // as long as users have not selected the 'delete attachment' option.
a7488080 567 if (empty($newActParams['is_delete_attachment'])) {
6a488035
TO
568 CRM_Core_BAO_File::copyEntityFile('civicrm_activity', $this->_activityId,
569 'civicrm_activity', $activity->id
570 );
571 }
572
573 // copy back params to original var
574 $params = $newActParams;
575 }
576
725fd9d9
N
577 foreach ($vvalue as $vkey => $vval) {
578 if ($vval['actId']) {
579 // add tags if exists
580 $tagParams = array();
581 if (!empty($params['tag'])) {
582 foreach ($params['tag'] as $tag) {
583 $tagParams[$tag] = 1;
584 }
6a488035 585 }
6a488035 586
725fd9d9
N
587 //save static tags
588 CRM_Core_BAO_EntityTag::create($tagParams, 'civicrm_activity', $vval['actId']);
6a488035 589
725fd9d9
N
590 //save free tags
591 if (isset($params['taglist']) && !empty($params['taglist'])) {
592 CRM_Core_Form_Tag::postProcess($params['taglist'], $vval['actId'], 'civicrm_activity', $this);
593 }
6a488035 594 }
6a488035 595
725fd9d9
N
596 // update existing case record if needed
597 $caseParams = $params;
598 $caseParams['id'] = $vval['case_id'];
725fd9d9
N
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(
353ffa53
TO
696 'activity_id' => $followupActivity->id,
697 'case_id' => $vval['case_id'],
725fd9d9
N
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,
21dfd5f5 709 4 => $mailStatus,
725fd9d9
N
710 )
711 ), 'info');
712 }
6a488035
TO
713 }
714}