Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
3819f101 | 35 | * This class create activities for a case. |
6a488035 TO |
36 | */ |
37 | class 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); | |
edc80cda | 66 | $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this); |
6a488035 TO |
67 | if (!$this->_context) { |
68 | $this->_context = 'caseActivity'; | |
69 | } | |
70 | $this->_crmDir = 'Case'; | |
71 | $this->assign('context', $this->_context); | |
72 | ||
1b67821f | 73 | parent::preProcess(); |
6a488035 | 74 | |
9c248a42 | 75 | $scheduleStatusId = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Scheduled'); |
6a488035 TO |
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 | ) { | |
5f1c8c57 | 103 | $params = array('type' => 'any'); |
104 | $allCases = CRM_Case_BAO_Case::getCases(TRUE, $params); | |
7fa5bb16 | 105 | if (count(array_intersect($this->_caseId, array_keys($allCases))) == 0) { |
6a488035 TO |
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 | 180 | ); |
359d4010 | 181 | $activityId = CRM_Utils_Array::first(array_keys($activities['data'])); |
725fd9d9 | 182 | $editUrl = CRM_Utils_System::url('civicrm/case/activity', |
359d4010 | 183 | "reset=1&cid={$this->_currentlyViewedContactId}&caseid={$caseId}&action=update&id={$activityId}" |
725fd9d9 N |
184 | ); |
185 | } | |
186 | CRM_Core_Error::statusBounce(ts("You can not add another '%1' activity to this case. %2", | |
187 | array( | |
188 | 1 => $this->_activityTypeName, | |
e303faaf | 189 | 2 => ts("Do you want to <a %1>edit the existing activity</a>?", array(1 => "href='$editUrl'")), |
725fd9d9 N |
190 | ) |
191 | ), | |
192 | $url | |
6a488035 TO |
193 | ); |
194 | } | |
6a488035 TO |
195 | } |
196 | } | |
197 | } | |
198 | ||
53c79877 SM |
199 | // Turn off the prompt which asks the user if they want to create separate |
200 | // activities when specifying multiple contacts "with" a new activity. | |
201 | // Instead, always create one activity with all contacts together. | |
202 | $this->supportsActivitySeparation = FALSE; | |
203 | ||
6a488035 TO |
204 | $session = CRM_Core_Session::singleton(); |
205 | $session->pushUserContext($url); | |
206 | } | |
207 | ||
208 | /** | |
3819f101 | 209 | * Set default values for the form. |
6a488035 | 210 | */ |
00be9182 | 211 | public function setDefaultValues() { |
6a488035 TO |
212 | $this->_defaults = parent::setDefaultValues(); |
213 | $targetContactValues = array(); | |
725fd9d9 N |
214 | foreach ($this->_caseId as $key => $val) { |
215 | //get all clients. | |
216 | $clients = CRM_Case_BAO_Case::getContactNames($val); | |
217 | if (isset($this->_activityId) && empty($_POST)) { | |
218 | if (!CRM_Utils_Array::crmIsEmptyArray($this->_defaults['target_contact'])) { | |
219 | $targetContactValues = array_combine(array_unique($this->_defaults['target_contact']), | |
220 | explode(';', trim($this->_defaults['target_contact_value'])) | |
221 | ); | |
222 | //exclude all clients. | |
223 | foreach ($clients as $clientId => $vals) { | |
224 | if (array_key_exists($clientId, $targetContactValues)) { | |
225 | unset($targetContactValues[$clientId]); | |
226 | } | |
6a488035 TO |
227 | } |
228 | } | |
229 | } | |
725fd9d9 | 230 | $this->assign('targetContactValues', empty($targetContactValues) ? FALSE : $targetContactValues); |
6a488035 | 231 | |
725fd9d9 N |
232 | if (isset($this->_encounterMedium)) { |
233 | $this->_defaults['medium_id'] = $this->_encounterMedium; | |
234 | } | |
235 | elseif (empty($this->_defaults['medium_id'])) { | |
236 | // set default encounter medium CRM-4816 | |
237 | $medium = CRM_Core_OptionGroup::values('encounter_medium', FALSE, FALSE, FALSE, 'AND is_default = 1'); | |
238 | if (count($medium) == 1) { | |
239 | $this->_defaults['medium_id'] = key($medium); | |
240 | } | |
6a488035 | 241 | } |
6a488035 | 242 | |
725fd9d9 N |
243 | return $this->_defaults; |
244 | } | |
6a488035 TO |
245 | } |
246 | ||
247 | public function buildQuickForm() { | |
248 | $this->_fields['source_contact_id']['label'] = ts('Reported By'); | |
f7305cbc | 249 | unset($this->_fields['status_id']['attributes']['required']); |
6a488035 TO |
250 | |
251 | if ($this->_caseType) { | |
252 | $xmlProcessor = new CRM_Case_XMLProcessor_Process(); | |
725fd9d9 N |
253 | $aTypes = array(); |
254 | foreach ($this->_caseType as $key => $val) { | |
255 | $activityTypes = $xmlProcessor->get($val, 'ActivityTypes', TRUE); | |
256 | $aTypes = $aTypes + $activityTypes; | |
257 | } | |
6a488035 TO |
258 | |
259 | // remove Open Case activity type since we're inside an existing case | |
9c248a42 | 260 | $openCaseID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Open Case'); |
6a488035 TO |
261 | unset($aTypes[$openCaseID]); |
262 | asort($aTypes); | |
a1a2a83d | 263 | $this->_fields['followup_activity_type_id']['attributes'] = array('' => '- select activity type -') + $aTypes; |
6a488035 TO |
264 | } |
265 | ||
b7617307 | 266 | parent::buildQuickForm(); |
6a488035 TO |
267 | |
268 | if ($this->_action & (CRM_Core_Action::DELETE | CRM_Core_Action::DETACH | CRM_Core_Action::RENEW)) { | |
269 | return; | |
270 | } | |
271 | ||
6a488035 TO |
272 | $this->assign('urlPath', 'civicrm/case/activity'); |
273 | ||
274 | $encounterMediums = CRM_Case_PseudoConstant::encounterMedium(); | |
cedb74cd | 275 | |
06f51047 CW |
276 | if ($this->_activityTypeFile == 'OpenCase' && $this->_action == CRM_Core_Action::UPDATE) { |
277 | $this->getElement('activity_date_time')->freeze(); | |
278 | ||
cedb74cd MWMC |
279 | if ($this->_activityId) { |
280 | // 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. | |
281 | // Is that really a big problem? | |
282 | $this->_encounterMedium = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $this->_activityId, 'medium_id'); | |
283 | if (!array_key_exists($this->_encounterMedium, $encounterMediums)) { | |
284 | $encounterMediums[$this->_encounterMedium] = CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'medium_id', $this->_encounterMedium); | |
285 | } | |
6a488035 TO |
286 | } |
287 | } | |
288 | ||
289 | $this->add('select', 'medium_id', ts('Medium'), $encounterMediums, TRUE); | |
725fd9d9 N |
290 | $i = 0; |
291 | foreach ($this->_caseId as $key => $val) { | |
a82f59a1 | 292 | $this->_relatedContacts[] = $rgc = CRM_Case_BAO_Case::getRelatedAndGlobalContacts($val); |
725fd9d9 N |
293 | $contName = CRM_Case_BAO_Case::getContactNames($val); |
294 | foreach ($contName as $nkey => $nval) { | |
e547f744 | 295 | array_push($this->_relatedContacts[$i][0], $this->_relatedContacts[$i][0]['managerOf'] = $nval['display_name']); |
725fd9d9 N |
296 | } |
297 | $i++; | |
298 | } | |
6a488035 | 299 | |
6a488035 | 300 | //add case client in send a copy selector.CRM-4438. |
725fd9d9 | 301 | foreach ($this->_caseId as $key => $val) { |
e547f744 | 302 | $relatedContacts[] = $relCon = CRM_Case_BAO_Case::getContactNames($val); |
725fd9d9 N |
303 | } |
304 | ||
6a488035 TO |
305 | if (!empty($relatedContacts)) { |
306 | foreach ($relatedContacts as $relatedContact) { | |
307 | $this->_relatedContacts[] = $relatedContact; | |
308 | } | |
309 | } | |
310 | ||
311 | if (!empty($this->_relatedContacts)) { | |
312 | $checkBoxes = array(); | |
313 | foreach ($this->_relatedContacts as $id => $row) { | |
ee1ccac5 EH |
314 | foreach ($row as $key => $value) { |
315 | $checkBoxes[$key] = $this->addElement('checkbox', $key, NULL, NULL, array('class' => 'select-row')); | |
316 | } | |
6a488035 TO |
317 | } |
318 | ||
319 | $this->addGroup($checkBoxes, 'contact_check'); | |
320 | $this->addElement('checkbox', 'toggleSelect', NULL, NULL, | |
d664f648 | 321 | array('class' => 'select-rows') |
6a488035 TO |
322 | ); |
323 | $this->assign('searchRows', $this->_relatedContacts); | |
324 | } | |
a82f59a1 | 325 | $this->_relatedContacts = $rgc + $relCon; |
6a488035 TO |
326 | |
327 | $this->addFormRule(array('CRM_Case_Form_Activity', 'formRule'), $this); | |
328 | } | |
329 | ||
330 | /** | |
d2e5d2ce | 331 | * Global form rule. |
6a488035 | 332 | * |
64bd5a0e TO |
333 | * @param array $fields |
334 | * The input form values. | |
335 | * @param array $files | |
336 | * The uploaded files if any. | |
77b97be7 EM |
337 | * @param $self |
338 | * | |
72b3a70c CW |
339 | * @return bool|array |
340 | * true if no errors, else array of errors | |
6a488035 | 341 | */ |
00be9182 | 342 | public static function formRule($fields, $files, $self) { |
6a488035 TO |
343 | // skip form rule if deleting |
344 | if (CRM_Utils_Array::value('_qf_Activity_next_', $fields) == 'Delete' || CRM_Utils_Array::value('_qf_Activity_next_', $fields) == 'Restore') { | |
345 | return TRUE; | |
346 | } | |
347 | ||
6ea16bbf | 348 | return parent::formRule($fields, $files, $self); |
6a488035 TO |
349 | } |
350 | ||
351 | /** | |
d2e5d2ce | 352 | * Process the form submission. |
6a488035 | 353 | * |
100fef9d | 354 | * @param array $params |
6a488035 | 355 | */ |
2d4a6b57 | 356 | public function postProcess($params = NULL) { |
6a488035 TO |
357 | $transaction = new CRM_Core_Transaction(); |
358 | ||
359 | if ($this->_action & CRM_Core_Action::DELETE) { | |
360 | $statusMsg = NULL; | |
361 | ||
362 | //block deleting activities which affects | |
363 | //case attributes.CRM-4543 | |
364 | $activityCondition = " AND v.name IN ('Open Case', 'Change Case Type', 'Change Case Status', 'Change Case Start Date')"; | |
365 | $caseAttributeActivities = CRM_Core_OptionGroup::values('activity_type', FALSE, FALSE, FALSE, $activityCondition); | |
366 | ||
367 | if (!array_key_exists($this->_activityTypeId, $caseAttributeActivities)) { | |
368 | $params = array('id' => $this->_activityId); | |
369 | $activityDelete = CRM_Activity_BAO_Activity::deleteActivity($params, TRUE); | |
370 | if ($activityDelete) { | |
371 | $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 />'); | |
372 | } | |
373 | } | |
374 | else { | |
375 | $statusMsg = ts("Selected Activity cannot be deleted."); | |
376 | } | |
377 | ||
378 | $tagParams = array( | |
379 | 'entity_table' => 'civicrm_activity', | |
21dfd5f5 | 380 | 'entity_id' => $this->_activityId, |
6a488035 TO |
381 | ); |
382 | CRM_Core_BAO_EntityTag::del($tagParams); | |
383 | ||
384 | CRM_Core_Session::setStatus('', $statusMsg, 'info'); | |
385 | return; | |
386 | } | |
387 | ||
388 | if ($this->_action & CRM_Core_Action::RENEW) { | |
50237bc9 | 389 | $statusMsg = NULL; |
390 | $params = array('id' => $this->_activityId); | |
6a488035 TO |
391 | $activityRestore = CRM_Activity_BAO_Activity::restoreActivity($params); |
392 | if ($activityRestore) { | |
393 | $statusMsg = ts('The selected activity has been restored.<br />'); | |
394 | } | |
395 | CRM_Core_Session::setStatus('', $statusMsg, 'info'); | |
396 | return; | |
397 | } | |
398 | ||
399 | // store the submitted values in an array | |
400 | $params = $this->controller->exportValues($this->_name); | |
6a488035 TO |
401 | |
402 | //set parent id if its edit mode | |
403 | if ($parentId = CRM_Utils_Array::value('parent_id', $this->_defaults)) { | |
404 | $params['parent_id'] = $parentId; | |
405 | } | |
406 | ||
6a488035 TO |
407 | $params['activity_type_id'] = $this->_activityTypeId; |
408 | ||
409 | // format with contact (target contact) values | |
3911d992 DJ |
410 | if (isset($params['target_contact_id'])) { |
411 | $params['target_contact_id'] = explode(',', $params['target_contact_id']); | |
6a488035 TO |
412 | } |
413 | else { | |
414 | $params['target_contact_id'] = array(); | |
415 | } | |
416 | ||
417 | // format activity custom data | |
a7488080 | 418 | if (!empty($params['hidden_custom'])) { |
6a488035 | 419 | if ($this->_activityId) { |
756b5b30 | 420 | // retrieve and include the custom data of old Activity |
421 | $oldActivity = civicrm_api3('Activity', 'getsingle', array('id' => $this->_activityId)); | |
309c49b2 | 422 | $params = array_merge($oldActivity, $params); |
756b5b30 | 423 | |
6a488035 TO |
424 | // unset custom fields-id from params since we want custom |
425 | // fields to be saved for new activity. | |
426 | foreach ($params as $key => $value) { | |
427 | $match = array(); | |
428 | if (preg_match('/^(custom_\d+_)(\d+)$/', $key, $match)) { | |
429 | $params[$match[1] . '-1'] = $params[$key]; | |
430 | ||
431 | // for autocomplete transfer hidden value instead of label | |
432 | if ($params[$key] && isset($params[$key . '_id'])) { | |
433 | $params[$match[1] . '-1_id'] = $params[$key . '_id']; | |
434 | unset($params[$key . '_id']); | |
435 | } | |
436 | unset($params[$key]); | |
437 | } | |
438 | } | |
439 | } | |
440 | ||
441 | // build custom data getFields array | |
442 | $customFields = CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE, $this->_activityTypeId); | |
443 | $customFields = CRM_Utils_Array::crmArrayMerge($customFields, | |
444 | CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE, | |
445 | NULL, NULL, TRUE | |
446 | ) | |
447 | ); | |
448 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, | |
6a488035 TO |
449 | $this->_activityId, |
450 | 'Activity' | |
451 | ); | |
452 | } | |
453 | ||
50237bc9 | 454 | // assigning formatted value |
a7488080 | 455 | if (!empty($params['assignee_contact_id'])) { |
6a488035 TO |
456 | $params['assignee_contact_id'] = explode(',', $params['assignee_contact_id']); |
457 | } | |
458 | else { | |
459 | $params['assignee_contact_id'] = array(); | |
460 | } | |
461 | ||
6a488035 TO |
462 | if (isset($this->_activityId)) { |
463 | // activity which hasn't been modified by a user yet | |
464 | if ($this->_defaults['is_auto'] == 1) { | |
465 | $params['is_auto'] = 0; | |
466 | } | |
467 | ||
468 | // always create a revision of an case activity. CRM-4533 | |
469 | $newActParams = $params; | |
470 | ||
471 | // add target contact values in update mode | |
472 | if (empty($params['target_contact_id']) && !empty($this->_defaults['target_contact'])) { | |
473 | $newActParams['target_contact_id'] = $this->_defaults['target_contact']; | |
474 | } | |
6a488035 TO |
475 | } |
476 | ||
477 | if (!isset($newActParams)) { | |
478 | // add more attachments if needed for old activity | |
479 | CRM_Core_BAO_File::formatAttachment($params, | |
480 | $params, | |
481 | 'civicrm_activity' | |
482 | ); | |
483 | ||
484 | // call begin post process, before the activity is created/updated. | |
485 | $this->beginPostProcess($params); | |
725fd9d9 N |
486 | foreach ($this->_caseId as $key => $val) { |
487 | $params['case_id'] = $val; | |
488 | // activity create/update | |
489 | $activity = CRM_Activity_BAO_Activity::create($params); | |
490 | $vvalue[] = array('case_id' => $val, 'actId' => $activity->id); | |
491 | // call end post process, after the activity has been created/updated. | |
492 | $this->endPostProcess($params, $activity); | |
493 | } | |
6a488035 TO |
494 | } |
495 | else { | |
496 | // since the params we need to set are very few, and we don't want rest of the | |
497 | // work done by bao create method , lets use dao object to make the changes | |
498 | $params = array('id' => $this->_activityId); | |
499 | $params['is_current_revision'] = 0; | |
500 | $activity = new CRM_Activity_DAO_Activity(); | |
501 | $activity->copyValues($params); | |
502 | $activity->save(); | |
503 | } | |
504 | ||
505 | // create a new version of activity if activity was found to | |
506 | // have been modified/created by user | |
507 | if (isset($newActParams)) { | |
508 | // set proper original_id | |
a7488080 | 509 | if (!empty($this->_defaults['original_id'])) { |
6a488035 TO |
510 | $newActParams['original_id'] = $this->_defaults['original_id']; |
511 | } | |
512 | else { | |
513 | $newActParams['original_id'] = $activity->id; | |
514 | } | |
6a488035 | 515 | |
756b5b30 | 516 | //is_current_revision will be set to 1 by default. |
6a488035 TO |
517 | // add attachments if any |
518 | CRM_Core_BAO_File::formatAttachment($newActParams, | |
519 | $newActParams, | |
520 | 'civicrm_activity' | |
521 | ); | |
522 | ||
523 | // call begin post process, before the activity is created/updated. | |
524 | $this->beginPostProcess($newActParams); | |
725fd9d9 N |
525 | foreach ($this->_caseId as $key => $val) { |
526 | $newActParams['case_id'] = $val; | |
527 | $activity = CRM_Activity_BAO_Activity::create($newActParams); | |
e547f744 | 528 | $vvalue[] = array('case_id' => $val, 'actId' => $activity->id); |
725fd9d9 N |
529 | // call end post process, after the activity has been created/updated. |
530 | $this->endPostProcess($newActParams, $activity); | |
531 | } | |
6a488035 TO |
532 | // copy files attached to old activity if any, to new one, |
533 | // as long as users have not selected the 'delete attachment' option. | |
8506cda6 | 534 | if (empty($newActParams['is_delete_attachment']) && ($this->_activityId != $activity->id)) { |
6a488035 TO |
535 | CRM_Core_BAO_File::copyEntityFile('civicrm_activity', $this->_activityId, |
536 | 'civicrm_activity', $activity->id | |
537 | ); | |
538 | } | |
539 | ||
540 | // copy back params to original var | |
541 | $params = $newActParams; | |
542 | } | |
543 | ||
725fd9d9 N |
544 | foreach ($vvalue as $vkey => $vval) { |
545 | if ($vval['actId']) { | |
546 | // add tags if exists | |
547 | $tagParams = array(); | |
548 | if (!empty($params['tag'])) { | |
63b7aefc DB |
549 | if (!is_array($params['tag'])) { |
550 | $params['tag'] = explode(',', $params['tag']); | |
725fd9d9 | 551 | } |
63b7aefc DB |
552 | |
553 | $tagParams = array_fill_keys($params['tag'], 1); | |
6a488035 | 554 | } |
6a488035 | 555 | |
725fd9d9 N |
556 | //save static tags |
557 | CRM_Core_BAO_EntityTag::create($tagParams, 'civicrm_activity', $vval['actId']); | |
6a488035 | 558 | |
725fd9d9 N |
559 | //save free tags |
560 | if (isset($params['taglist']) && !empty($params['taglist'])) { | |
561 | CRM_Core_Form_Tag::postProcess($params['taglist'], $vval['actId'], 'civicrm_activity', $this); | |
562 | } | |
6a488035 | 563 | } |
6a488035 | 564 | |
725fd9d9 N |
565 | // update existing case record if needed |
566 | $caseParams = $params; | |
567 | $caseParams['id'] = $vval['case_id']; | |
725fd9d9 N |
568 | if (!empty($caseParams['case_status_id'])) { |
569 | $caseParams['status_id'] = $caseParams['case_status_id']; | |
570 | } | |
6a488035 | 571 | |
725fd9d9 N |
572 | // unset params intended for activities only |
573 | unset($caseParams['subject'], $caseParams['details'], | |
574 | $caseParams['status_id'], $caseParams['custom'] | |
575 | ); | |
1b67821f | 576 | CRM_Case_BAO_Case::create($caseParams); |
725fd9d9 N |
577 | // create case activity record |
578 | $caseParams = array( | |
579 | 'activity_id' => $vval['actId'], | |
580 | 'case_id' => $vval['case_id'], | |
581 | ); | |
582 | CRM_Case_BAO_Case::processCaseActivity($caseParams); | |
6a488035 TO |
583 | } |
584 | ||
6a488035 TO |
585 | // send copy to selected contacts. |
586 | $mailStatus = ''; | |
587 | $mailToContacts = array(); | |
588 | ||
589 | //CRM-5695 | |
590 | //check for notification settings for assignee contacts | |
591 | $selectedContacts = array('contact_check'); | |
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) { | |
35522279 | 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 | } |