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