Commit | Line | Data |
---|---|---|
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 | */ | |
40 | class 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 | /** | |
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 | ||
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, |
6a488035 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, | |
206 | 2 => "Do you want to <a href='$editUrl'>edit the existing activity</a> ?" | |
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 | /** | |
221 | * This function sets the default values for the form. For edit/view mode | |
222 | * the default values are retrieved from the database | |
223 | * | |
224 | * @access public | |
225 | * | |
355ba699 | 226 | * @return void |
6a488035 TO |
227 | */ |
228 | function setDefaultValues() { | |
229 | $this->_defaults = parent::setDefaultValues(); | |
230 | $targetContactValues = array(); | |
725fd9d9 N |
231 | foreach ($this->_caseId as $key => $val) { |
232 | //get all clients. | |
233 | $clients = CRM_Case_BAO_Case::getContactNames($val); | |
234 | if (isset($this->_activityId) && empty($_POST)) { | |
235 | if (!CRM_Utils_Array::crmIsEmptyArray($this->_defaults['target_contact'])) { | |
236 | $targetContactValues = array_combine(array_unique($this->_defaults['target_contact']), | |
237 | explode(';', trim($this->_defaults['target_contact_value'])) | |
238 | ); | |
239 | //exclude all clients. | |
240 | foreach ($clients as $clientId => $vals) { | |
241 | if (array_key_exists($clientId, $targetContactValues)) { | |
242 | unset($targetContactValues[$clientId]); | |
243 | } | |
6a488035 TO |
244 | } |
245 | } | |
246 | } | |
725fd9d9 | 247 | $this->assign('targetContactValues', empty($targetContactValues) ? FALSE : $targetContactValues); |
6a488035 | 248 | |
725fd9d9 N |
249 | //return form for ajax |
250 | if ($this->_cdType) { | |
251 | return $this->_defaults; | |
252 | } | |
6a488035 | 253 | |
725fd9d9 N |
254 | if (isset($this->_encounterMedium)) { |
255 | $this->_defaults['medium_id'] = $this->_encounterMedium; | |
256 | } | |
257 | elseif (empty($this->_defaults['medium_id'])) { | |
258 | // set default encounter medium CRM-4816 | |
259 | $medium = CRM_Core_OptionGroup::values('encounter_medium', FALSE, FALSE, FALSE, 'AND is_default = 1'); | |
260 | if (count($medium) == 1) { | |
261 | $this->_defaults['medium_id'] = key($medium); | |
262 | } | |
6a488035 | 263 | } |
6a488035 | 264 | |
725fd9d9 N |
265 | return $this->_defaults; |
266 | } | |
6a488035 TO |
267 | } |
268 | ||
269 | public function buildQuickForm() { | |
270 | $this->_fields['source_contact_id']['label'] = ts('Reported By'); | |
f7305cbc | 271 | unset($this->_fields['status_id']['attributes']['required']); |
6a488035 TO |
272 | |
273 | if ($this->_caseType) { | |
274 | $xmlProcessor = new CRM_Case_XMLProcessor_Process(); | |
725fd9d9 N |
275 | $aTypes = array(); |
276 | foreach ($this->_caseType as $key => $val) { | |
277 | $activityTypes = $xmlProcessor->get($val, 'ActivityTypes', TRUE); | |
278 | $aTypes = $aTypes + $activityTypes; | |
279 | } | |
6a488035 TO |
280 | |
281 | // remove Open Case activity type since we're inside an existing case | |
282 | $openCaseID = CRM_Core_OptionGroup::getValue('activity_type', 'Open Case', 'name'); | |
283 | unset($aTypes[$openCaseID]); | |
284 | asort($aTypes); | |
285 | $this->_fields['followup_activity_type_id']['attributes'] = array( | |
50237bc9 | 286 | '' => '- select activity type -' |
287 | ) + $aTypes; | |
6a488035 TO |
288 | } |
289 | ||
290 | $result = parent::buildQuickForm(); | |
291 | ||
292 | if ($this->_action & (CRM_Core_Action::DELETE | CRM_Core_Action::DETACH | CRM_Core_Action::RENEW)) { | |
293 | return; | |
294 | } | |
295 | ||
296 | if ($this->_cdType) { | |
297 | return $result; | |
298 | } | |
299 | ||
300 | $this->assign('urlPath', 'civicrm/case/activity'); | |
301 | ||
302 | $encounterMediums = CRM_Case_PseudoConstant::encounterMedium(); | |
475e9f44 CW |
303 | // 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. |
304 | // Is that really a big problem? | |
6a488035 TO |
305 | if ($this->_activityTypeFile == 'OpenCase') { |
306 | $this->_encounterMedium = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $this->_activityId, | |
307 | 'medium_id' | |
308 | ); | |
309 | if (!array_key_exists($this->_encounterMedium, $encounterMediums)) { | |
310 | $encounterMediums[$this->_encounterMedium] = CRM_Core_OptionGroup::getLabel('encounter_medium', | |
311 | $this->_encounterMedium, | |
312 | FALSE | |
313 | ); | |
314 | } | |
315 | } | |
316 | ||
317 | $this->add('select', 'medium_id', ts('Medium'), $encounterMediums, TRUE); | |
725fd9d9 N |
318 | $i = 0; |
319 | foreach ($this->_caseId as $key => $val) { | |
320 | $this->_relatedContacts[] = CRM_Case_BAO_Case::getRelatedAndGlobalContacts($val); | |
321 | $contName = CRM_Case_BAO_Case::getContactNames($val); | |
322 | foreach ($contName as $nkey => $nval) { | |
323 | array_push($this->_relatedContacts[$i][0] , $this->_relatedContacts[$i][0]['managerOf']= $nval['display_name']); | |
324 | } | |
325 | $i++; | |
326 | } | |
6a488035 | 327 | |
6a488035 | 328 | //add case client in send a copy selector.CRM-4438. |
725fd9d9 N |
329 | foreach ($this->_caseId as $key => $val) { |
330 | $relatedContacts[] = CRM_Case_BAO_Case::getContactNames($val); | |
331 | } | |
332 | ||
6a488035 TO |
333 | if (!empty($relatedContacts)) { |
334 | foreach ($relatedContacts as $relatedContact) { | |
335 | $this->_relatedContacts[] = $relatedContact; | |
336 | } | |
337 | } | |
338 | ||
339 | if (!empty($this->_relatedContacts)) { | |
340 | $checkBoxes = array(); | |
341 | foreach ($this->_relatedContacts as $id => $row) { | |
03a2a396 | 342 | $checkBoxes[$id] = $this->addElement('checkbox', $id, NULL, NULL, array('class' => 'select-row')); |
6a488035 TO |
343 | } |
344 | ||
345 | $this->addGroup($checkBoxes, 'contact_check'); | |
346 | $this->addElement('checkbox', 'toggleSelect', NULL, NULL, | |
d664f648 | 347 | array('class' => 'select-rows') |
6a488035 TO |
348 | ); |
349 | $this->assign('searchRows', $this->_relatedContacts); | |
350 | } | |
351 | ||
352 | $this->addFormRule(array('CRM_Case_Form_Activity', 'formRule'), $this); | |
353 | } | |
354 | ||
355 | /** | |
356 | * global form rule | |
357 | * | |
77b97be7 EM |
358 | * @param array $fields the input form values |
359 | * @param array $files the uploaded files if any | |
360 | * @param $self | |
361 | * | |
362 | * @internal param array $options additional user data | |
6a488035 TO |
363 | * |
364 | * @return true if no errors, else array of errors | |
365 | * @access public | |
366 | * @static | |
367 | */ | |
368 | static function formRule($fields, $files, $self) { | |
369 | // skip form rule if deleting | |
370 | if (CRM_Utils_Array::value('_qf_Activity_next_', $fields) == 'Delete' || CRM_Utils_Array::value('_qf_Activity_next_', $fields) == 'Restore') { | |
371 | return TRUE; | |
372 | } | |
373 | ||
374 | return parent::formrule($fields, $files, $self); | |
375 | } | |
376 | ||
377 | /** | |
378 | * Function to process the form | |
379 | * | |
380 | * @access public | |
381 | * | |
77b97be7 EM |
382 | * @param null $params |
383 | * | |
355ba699 | 384 | * @return void |
6a488035 | 385 | */ |
2d4a6b57 | 386 | public function postProcess($params = NULL) { |
6a488035 TO |
387 | $transaction = new CRM_Core_Transaction(); |
388 | ||
389 | if ($this->_action & CRM_Core_Action::DELETE) { | |
390 | $statusMsg = NULL; | |
391 | ||
392 | //block deleting activities which affects | |
393 | //case attributes.CRM-4543 | |
394 | $activityCondition = " AND v.name IN ('Open Case', 'Change Case Type', 'Change Case Status', 'Change Case Start Date')"; | |
395 | $caseAttributeActivities = CRM_Core_OptionGroup::values('activity_type', FALSE, FALSE, FALSE, $activityCondition); | |
396 | ||
397 | if (!array_key_exists($this->_activityTypeId, $caseAttributeActivities)) { | |
398 | $params = array('id' => $this->_activityId); | |
399 | $activityDelete = CRM_Activity_BAO_Activity::deleteActivity($params, TRUE); | |
400 | if ($activityDelete) { | |
401 | $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 />'); | |
402 | } | |
403 | } | |
404 | else { | |
405 | $statusMsg = ts("Selected Activity cannot be deleted."); | |
406 | } | |
407 | ||
408 | $tagParams = array( | |
409 | 'entity_table' => 'civicrm_activity', | |
410 | 'entity_id' => $this->_activityId | |
411 | ); | |
412 | CRM_Core_BAO_EntityTag::del($tagParams); | |
413 | ||
414 | CRM_Core_Session::setStatus('', $statusMsg, 'info'); | |
415 | return; | |
416 | } | |
417 | ||
418 | if ($this->_action & CRM_Core_Action::RENEW) { | |
50237bc9 | 419 | $statusMsg = NULL; |
420 | $params = array('id' => $this->_activityId); | |
6a488035 TO |
421 | $activityRestore = CRM_Activity_BAO_Activity::restoreActivity($params); |
422 | if ($activityRestore) { | |
423 | $statusMsg = ts('The selected activity has been restored.<br />'); | |
424 | } | |
425 | CRM_Core_Session::setStatus('', $statusMsg, 'info'); | |
426 | return; | |
427 | } | |
428 | ||
429 | // store the submitted values in an array | |
430 | $params = $this->controller->exportValues($this->_name); | |
6a488035 TO |
431 | |
432 | //set parent id if its edit mode | |
433 | if ($parentId = CRM_Utils_Array::value('parent_id', $this->_defaults)) { | |
434 | $params['parent_id'] = $parentId; | |
435 | } | |
436 | ||
437 | // required for status msg | |
438 | $recordStatus = 'created'; | |
439 | ||
440 | // store the dates with proper format | |
441 | $params['activity_date_time'] = CRM_Utils_Date::processDate($params['activity_date_time'], $params['activity_date_time_time']); | |
442 | $params['activity_type_id'] = $this->_activityTypeId; | |
443 | ||
444 | // format with contact (target contact) values | |
3911d992 DJ |
445 | if (isset($params['target_contact_id'])) { |
446 | $params['target_contact_id'] = explode(',', $params['target_contact_id']); | |
6a488035 TO |
447 | } |
448 | else { | |
449 | $params['target_contact_id'] = array(); | |
450 | } | |
451 | ||
452 | // format activity custom data | |
a7488080 | 453 | if (!empty($params['hidden_custom'])) { |
6a488035 TO |
454 | if ($this->_activityId) { |
455 | // unset custom fields-id from params since we want custom | |
456 | // fields to be saved for new activity. | |
457 | foreach ($params as $key => $value) { | |
458 | $match = array(); | |
459 | if (preg_match('/^(custom_\d+_)(\d+)$/', $key, $match)) { | |
460 | $params[$match[1] . '-1'] = $params[$key]; | |
461 | ||
462 | // for autocomplete transfer hidden value instead of label | |
463 | if ($params[$key] && isset($params[$key . '_id'])) { | |
464 | $params[$match[1] . '-1_id'] = $params[$key . '_id']; | |
465 | unset($params[$key . '_id']); | |
466 | } | |
467 | unset($params[$key]); | |
468 | } | |
469 | } | |
470 | } | |
471 | ||
472 | // build custom data getFields array | |
473 | $customFields = CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE, $this->_activityTypeId); | |
474 | $customFields = CRM_Utils_Array::crmArrayMerge($customFields, | |
475 | CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE, | |
476 | NULL, NULL, TRUE | |
477 | ) | |
478 | ); | |
479 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, | |
480 | $customFields, | |
481 | $this->_activityId, | |
482 | 'Activity' | |
483 | ); | |
484 | } | |
485 | ||
50237bc9 | 486 | // assigning formatted value |
a7488080 | 487 | if (!empty($params['assignee_contact_id'])) { |
6a488035 TO |
488 | $params['assignee_contact_id'] = explode(',', $params['assignee_contact_id']); |
489 | } | |
490 | else { | |
491 | $params['assignee_contact_id'] = array(); | |
492 | } | |
493 | ||
6a488035 TO |
494 | if (isset($this->_activityId)) { |
495 | // activity which hasn't been modified by a user yet | |
496 | if ($this->_defaults['is_auto'] == 1) { | |
497 | $params['is_auto'] = 0; | |
498 | } | |
499 | ||
500 | // always create a revision of an case activity. CRM-4533 | |
501 | $newActParams = $params; | |
502 | ||
503 | // add target contact values in update mode | |
504 | if (empty($params['target_contact_id']) && !empty($this->_defaults['target_contact'])) { | |
505 | $newActParams['target_contact_id'] = $this->_defaults['target_contact']; | |
506 | } | |
507 | ||
508 | // record status for status msg | |
509 | $recordStatus = 'updated'; | |
510 | } | |
511 | ||
512 | if (!isset($newActParams)) { | |
513 | // add more attachments if needed for old activity | |
514 | CRM_Core_BAO_File::formatAttachment($params, | |
515 | $params, | |
516 | 'civicrm_activity' | |
517 | ); | |
518 | ||
519 | // call begin post process, before the activity is created/updated. | |
520 | $this->beginPostProcess($params); | |
725fd9d9 N |
521 | foreach ($this->_caseId as $key => $val) { |
522 | $params['case_id'] = $val; | |
523 | // activity create/update | |
524 | $activity = CRM_Activity_BAO_Activity::create($params); | |
525 | $vvalue[] = array('case_id' => $val, 'actId' => $activity->id); | |
526 | // call end post process, after the activity has been created/updated. | |
527 | $this->endPostProcess($params, $activity); | |
528 | } | |
6a488035 TO |
529 | } |
530 | else { | |
531 | // since the params we need to set are very few, and we don't want rest of the | |
532 | // work done by bao create method , lets use dao object to make the changes | |
533 | $params = array('id' => $this->_activityId); | |
534 | $params['is_current_revision'] = 0; | |
535 | $activity = new CRM_Activity_DAO_Activity(); | |
536 | $activity->copyValues($params); | |
537 | $activity->save(); | |
538 | } | |
539 | ||
540 | // create a new version of activity if activity was found to | |
541 | // have been modified/created by user | |
542 | if (isset($newActParams)) { | |
543 | // set proper original_id | |
a7488080 | 544 | if (!empty($this->_defaults['original_id'])) { |
6a488035 TO |
545 | $newActParams['original_id'] = $this->_defaults['original_id']; |
546 | } | |
547 | else { | |
548 | $newActParams['original_id'] = $activity->id; | |
549 | } | |
550 | //is_current_revision will be set to 1 by default. | |
551 | ||
552 | // add attachments if any | |
553 | CRM_Core_BAO_File::formatAttachment($newActParams, | |
554 | $newActParams, | |
555 | 'civicrm_activity' | |
556 | ); | |
557 | ||
558 | // call begin post process, before the activity is created/updated. | |
559 | $this->beginPostProcess($newActParams); | |
725fd9d9 N |
560 | foreach ($this->_caseId as $key => $val) { |
561 | $newActParams['case_id'] = $val; | |
562 | $activity = CRM_Activity_BAO_Activity::create($newActParams); | |
563 | $vvalue[] = array('case_id'=> $val, 'actId'=> $activity->id); | |
564 | // call end post process, after the activity has been created/updated. | |
565 | $this->endPostProcess($newActParams, $activity); | |
566 | } | |
6a488035 TO |
567 | // copy files attached to old activity if any, to new one, |
568 | // as long as users have not selected the 'delete attachment' option. | |
a7488080 | 569 | if (empty($newActParams['is_delete_attachment'])) { |
6a488035 TO |
570 | CRM_Core_BAO_File::copyEntityFile('civicrm_activity', $this->_activityId, |
571 | 'civicrm_activity', $activity->id | |
572 | ); | |
573 | } | |
574 | ||
575 | // copy back params to original var | |
576 | $params = $newActParams; | |
577 | } | |
578 | ||
725fd9d9 N |
579 | foreach ($vvalue as $vkey => $vval) { |
580 | if ($vval['actId']) { | |
581 | // add tags if exists | |
582 | $tagParams = array(); | |
583 | if (!empty($params['tag'])) { | |
584 | foreach ($params['tag'] as $tag) { | |
585 | $tagParams[$tag] = 1; | |
586 | } | |
6a488035 | 587 | } |
6a488035 | 588 | |
725fd9d9 N |
589 | //save static tags |
590 | CRM_Core_BAO_EntityTag::create($tagParams, 'civicrm_activity', $vval['actId']); | |
6a488035 | 591 | |
725fd9d9 N |
592 | //save free tags |
593 | if (isset($params['taglist']) && !empty($params['taglist'])) { | |
594 | CRM_Core_Form_Tag::postProcess($params['taglist'], $vval['actId'], 'civicrm_activity', $this); | |
595 | } | |
6a488035 | 596 | } |
6a488035 | 597 | |
725fd9d9 N |
598 | // update existing case record if needed |
599 | $caseParams = $params; | |
600 | $caseParams['id'] = $vval['case_id']; | |
601 | if (!empty($caseParams['case_type_id'])) { | |
602 | $caseParams['case_type_id'] = CRM_Core_DAO::VALUE_SEPARATOR . $caseParams['case_type_id'] . CRM_Core_DAO::VALUE_SEPARATOR; | |
603 | } | |
604 | if (!empty($caseParams['case_status_id'])) { | |
605 | $caseParams['status_id'] = $caseParams['case_status_id']; | |
606 | } | |
6a488035 | 607 | |
725fd9d9 N |
608 | // unset params intended for activities only |
609 | unset($caseParams['subject'], $caseParams['details'], | |
610 | $caseParams['status_id'], $caseParams['custom'] | |
611 | ); | |
612 | $case = CRM_Case_BAO_Case::create($caseParams); | |
613 | // create case activity record | |
614 | $caseParams = array( | |
615 | 'activity_id' => $vval['actId'], | |
616 | 'case_id' => $vval['case_id'], | |
617 | ); | |
618 | CRM_Case_BAO_Case::processCaseActivity($caseParams); | |
6a488035 TO |
619 | } |
620 | ||
6a488035 TO |
621 | // Insert civicrm_log record for the activity (e.g. store the |
622 | // created / edited by contact id and date for the activity) | |
623 | // Note - civicrm_log is already created by CRM_Activity_BAO_Activity::create() | |
8ef12e64 | 624 | |
6a488035 TO |
625 | // send copy to selected contacts. |
626 | $mailStatus = ''; | |
627 | $mailToContacts = array(); | |
628 | ||
629 | //CRM-5695 | |
630 | //check for notification settings for assignee contacts | |
631 | $selectedContacts = array('contact_check'); | |
e7e657f0 | 632 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
034500d4 | 633 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); |
6a488035 | 634 | if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, |
50237bc9 | 635 | 'activity_assignee_notification' |
636 | ) | |
637 | ) { | |
6a488035 TO |
638 | $selectedContacts[] = 'assignee_contact_id'; |
639 | } | |
640 | ||
725fd9d9 N |
641 | foreach ($vvalue as $vkey => $vval) { |
642 | foreach ($selectedContacts as $dnt => $val) { | |
643 | if (array_key_exists($val, $params) && !CRM_Utils_array::crmIsEmptyArray($params[$val])) { | |
cd122921 | 644 | if ($val == 'contact_check') { |
725fd9d9 | 645 | $mailStatus = ts("A copy of the activity has also been sent to selected contacts(s)."); |
cd122921 | 646 | } |
647 | else { | |
725fd9d9 N |
648 | $this->_relatedContacts = CRM_Activity_BAO_ActivityAssignment::getAssigneeNames(array($vval['actId']), TRUE, FALSE); |
649 | $mailStatus .= ' ' . ts("A copy of the activity has also been sent to assignee contacts(s)."); | |
cd122921 | 650 | } |
725fd9d9 N |
651 | //build an associative array with unique email addresses. |
652 | foreach ($params[$val] as $key => $value) { | |
653 | if ($val == 'contact_check') { | |
654 | $id = $key; | |
6a488035 TO |
655 | } |
656 | else { | |
725fd9d9 N |
657 | $id = $value; |
658 | } | |
659 | ||
660 | if (isset($id) && array_key_exists($id, $this->_relatedContacts) && isset($this->_relatedContacts[$id]['email'])) { | |
661 | //if email already exists in array then append with ', ' another role only otherwise add it to array. | |
662 | if ($contactDetails = CRM_Utils_Array::value($this->_relatedContacts[$id]['email'], $mailToContacts)) { | |
663 | $caseRole = CRM_Utils_Array::value('role', $this->_relatedContacts[$id]); | |
664 | $mailToContacts[$this->_relatedContacts[$id]['email']]['role'] = $contactDetails['role'] . ', ' . $caseRole; | |
665 | } | |
666 | else { | |
667 | $mailToContacts[$this->_relatedContacts[$id]['email']] = $this->_relatedContacts[$id]; | |
668 | } | |
6a488035 TO |
669 | } |
670 | } | |
671 | } | |
672 | } | |
6a488035 | 673 | |
725fd9d9 N |
674 | if (!CRM_Utils_array::crmIsEmptyArray($mailToContacts)) { |
675 | //include attachments while sending a copy of activity. | |
676 | $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_activity', | |
677 | $vval['actId'] | |
678 | ); | |
6a488035 | 679 | |
725fd9d9 N |
680 | $ics = new CRM_Activity_BAO_ICalendar($activity); |
681 | $ics->addAttachment($attachments, $mailToContacts); | |
682 | $result = CRM_Case_BAO_Case::sendActivityCopy($this->_currentlyViewedContactId, | |
683 | $vval['actId'], $mailToContacts, $attachments, $vval['case_id'] | |
684 | ); | |
685 | $ics->cleanup(); | |
686 | if (empty($result)) { | |
687 | $mailStatus = ''; | |
688 | } | |
689 | } | |
690 | else { | |
6a488035 TO |
691 | $mailStatus = ''; |
692 | } | |
6a488035 | 693 | |
725fd9d9 N |
694 | // create follow up activity if needed |
695 | $followupStatus = ''; | |
696 | if (!empty($params['followup_activity_type_id'])) { | |
697 | $followupActivity = CRM_Activity_BAO_Activity::createFollowupActivity($vval['actId'], $params); | |
6a488035 | 698 | |
725fd9d9 N |
699 | if ($followupActivity) { |
700 | $caseParams = array( | |
701 | 'activity_id' => $followupActivity->id, | |
702 | 'case_id' => $vval['case_id'], | |
703 | ); | |
704 | CRM_Case_BAO_Case::processCaseActivity($caseParams); | |
705 | $followupStatus = ts("A followup activity has been scheduled."); | |
706 | } | |
6a488035 | 707 | } |
6a488035 | 708 | |
725fd9d9 N |
709 | CRM_Core_Session::setStatus('', ts("'%1' activity has been %2. %3 %4", |
710 | array( | |
711 | 1 => $this->_activityTypeName, | |
712 | 2 => $recordStatus, | |
713 | 3 => $followupStatus, | |
714 | 4 => $mailStatus | |
715 | ) | |
716 | ), 'info'); | |
717 | } | |
6a488035 TO |
718 | } |
719 | } | |
720 |