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