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