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