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 | /** |
06f21064 | 40 | * Cases this activity belongs to. |
6a488035 | 41 | * |
06f21064 | 42 | * @var []int |
6a488035 TO |
43 | */ |
44 | public $_caseId; | |
45 | ||
46 | /** | |
d2e5d2ce | 47 | * The default case type variable defined. |
6a488035 | 48 | * |
06f21064 | 49 | * @var []int |
6a488035 TO |
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 | ||
06f21064 TO |
60 | /** |
61 | * The case type definition column info | |
62 | * for the caseId; | |
63 | * | |
64 | * @var array | |
65 | */ | |
66 | public $_caseTypeDefinition; | |
67 | ||
6a488035 | 68 | /** |
d2e5d2ce | 69 | * Build the form object. |
6a488035 | 70 | */ |
00be9182 | 71 | public function preProcess() { |
06f21064 TO |
72 | $caseIds = CRM_Utils_Request::retrieve('caseid', 'CommaSeparatedIntegers', $this); |
73 | $this->_caseId = $caseIds ? explode(',', $caseIds) : []; | |
edc80cda | 74 | $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this); |
6a488035 TO |
75 | if (!$this->_context) { |
76 | $this->_context = 'caseActivity'; | |
77 | } | |
78 | $this->_crmDir = 'Case'; | |
79 | $this->assign('context', $this->_context); | |
80 | ||
1b67821f | 81 | parent::preProcess(); |
6a488035 | 82 | |
9c248a42 | 83 | $scheduleStatusId = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Scheduled'); |
6a488035 TO |
84 | $this->assign('scheduleStatusId', $scheduleStatusId); |
85 | ||
6a488035 | 86 | if (!$this->_caseId && $this->_activityId) { |
06f21064 | 87 | $this->_caseId = (array) CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseActivity', $this->_activityId, |
6a488035 TO |
88 | 'case_id', 'activity_id' |
89 | ); | |
90 | } | |
91 | if ($this->_caseId) { | |
92 | $this->assign('caseId', $this->_caseId); | |
725fd9d9 | 93 | $this->assign('countId', count($this->_caseId)); |
ad32fddf | 94 | $this->assign('caseID', CRM_Utils_Array::first($this->_caseId)); |
6a488035 TO |
95 | } |
96 | ||
97 | if (!$this->_caseId || | |
98 | (!$this->_activityId && !$this->_activityTypeId) | |
99 | ) { | |
100 | CRM_Core_Error::fatal('required params missing.'); | |
101 | } | |
102 | ||
103 | //check for case activity access. | |
104 | if (!CRM_Case_BAO_Case::accessCiviCase()) { | |
105 | CRM_Core_Error::fatal(ts('You are not authorized to access this page.')); | |
106 | } | |
107 | //validate case id. | |
108 | if ($this->_caseId && | |
109 | !CRM_Core_Permission::check('access all cases and activities') | |
110 | ) { | |
5f1c8c57 | 111 | $params = array('type' => 'any'); |
112 | $allCases = CRM_Case_BAO_Case::getCases(TRUE, $params); | |
7fa5bb16 | 113 | if (count(array_intersect($this->_caseId, array_keys($allCases))) == 0) { |
6a488035 TO |
114 | CRM_Core_Error::fatal(ts('You are not authorized to access this page.')); |
115 | } | |
116 | } | |
117 | ||
118 | //validate case activity id. | |
119 | if ($this->_activityId && | |
120 | ($this->_action & CRM_Core_Action::UPDATE) | |
121 | ) { | |
122 | $valid = CRM_Case_BAO_Case::checkPermission($this->_activityId, 'edit', | |
123 | $this->_activityTypeId | |
124 | ); | |
125 | if (!$valid) { | |
126 | CRM_Core_Error::fatal(ts('You are not authorized to access this page.')); | |
127 | } | |
128 | } | |
129 | ||
cc30482e TO |
130 | foreach ($this->_caseId as $casePos => $caseId) { |
131 | $this->_caseType[$casePos] = CRM_Case_BAO_Case::getCaseType($caseId, 'name'); | |
725fd9d9 | 132 | } |
6a488035 TO |
133 | $this->assign('caseType', $this->_caseType); |
134 | ||
06f21064 TO |
135 | $this->_caseTypeDefinition = $this->getCaseTypeDefinition(); |
136 | ||
6a488035 TO |
137 | $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process(); |
138 | $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients(); | |
139 | $this->assign('multiClient', $isMultiClient); | |
140 | ||
cc30482e TO |
141 | foreach ($this->_caseId as $casePos => $caseId) { |
142 | $clients[] = CRM_Case_BAO_Case::getContactNames($caseId); | |
725fd9d9 | 143 | } |
6a488035 TO |
144 | $this->assign('client_names', $clients); |
145 | ||
725fd9d9 | 146 | $caseIds = implode(',', $this->_caseId); |
6a488035 TO |
147 | // set context for pushUserContext and for statusBounce |
148 | if ($this->_context == 'fulltext') { | |
149 | if ($this->_action == CRM_Core_Action::UPDATE || $this->_action == CRM_Core_Action::DELETE) { | |
150 | $url = CRM_Utils_System::url('civicrm/contact/view/case', | |
725fd9d9 | 151 | "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$caseIds}&show=1&context={$this->_context}" |
6a488035 TO |
152 | ); |
153 | } | |
154 | else { | |
155 | $url = CRM_Utils_System::url('civicrm/contact/search/custom', 'force=1'); | |
156 | } | |
157 | } | |
158 | else { | |
159 | $url = CRM_Utils_System::url('civicrm/contact/view/case', | |
725fd9d9 | 160 | "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$caseIds}&show=1" |
6a488035 TO |
161 | ); |
162 | } | |
163 | if (!$this->_activityId) { | |
164 | $caseTypes = CRM_Case_PseudoConstant::caseType(); | |
165 | ||
166 | if (empty($caseTypes) && ($this->_activityTypeName == 'Change Case Type') && !$this->_caseId) { | |
167 | $url = CRM_Utils_System::url('civicrm/contact/view/case', | |
725fd9d9 | 168 | "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$caseIds}&show=1" |
6a488035 TO |
169 | ); |
170 | $session = CRM_Core_Session::singleton(); | |
171 | $session->pushUserContext($url); | |
172 | CRM_Core_Error::statusBounce(ts("You do not have any active Case Types")); | |
173 | } | |
174 | ||
175 | // check if activity count is within the limit | |
176 | $xmlProcessor = new CRM_Case_XMLProcessor_Process(); | |
cc30482e TO |
177 | foreach ($this->_caseId as $casePos => $caseId) { |
178 | $caseType = $this->_caseType[$casePos]; | |
179 | $activityInst = $xmlProcessor->getMaxInstance($caseType); | |
6a488035 | 180 | |
cc30482e | 181 | // If not bounce back and also provide activity edit link |
725fd9d9 | 182 | if (isset($activityInst[$this->_activityTypeName])) { |
cc30482e | 183 | $activityCount = CRM_Case_BAO_Case::getCaseActivityCount($caseId, $this->_activityTypeId); |
725fd9d9 N |
184 | if ($activityCount >= $activityInst[$this->_activityTypeName]) { |
185 | if ($activityInst[$this->_activityTypeName] == 1) { | |
186 | $atArray = array('activity_type_id' => $this->_activityTypeId); | |
cc30482e | 187 | $activities = CRM_Case_BAO_Case::getCaseActivity($caseId, |
353ffa53 TO |
188 | $atArray, |
189 | $this->_currentUserId | |
725fd9d9 | 190 | ); |
359d4010 | 191 | $activityId = CRM_Utils_Array::first(array_keys($activities['data'])); |
725fd9d9 | 192 | $editUrl = CRM_Utils_System::url('civicrm/case/activity', |
359d4010 | 193 | "reset=1&cid={$this->_currentlyViewedContactId}&caseid={$caseId}&action=update&id={$activityId}" |
725fd9d9 N |
194 | ); |
195 | } | |
196 | CRM_Core_Error::statusBounce(ts("You can not add another '%1' activity to this case. %2", | |
197 | array( | |
198 | 1 => $this->_activityTypeName, | |
e303faaf | 199 | 2 => ts("Do you want to <a %1>edit the existing activity</a>?", array(1 => "href='$editUrl'")), |
725fd9d9 N |
200 | ) |
201 | ), | |
202 | $url | |
6a488035 TO |
203 | ); |
204 | } | |
6a488035 TO |
205 | } |
206 | } | |
207 | } | |
208 | ||
53c79877 SM |
209 | // Turn off the prompt which asks the user if they want to create separate |
210 | // activities when specifying multiple contacts "with" a new activity. | |
211 | // Instead, always create one activity with all contacts together. | |
212 | $this->supportsActivitySeparation = FALSE; | |
213 | ||
6a488035 TO |
214 | $session = CRM_Core_Session::singleton(); |
215 | $session->pushUserContext($url); | |
216 | } | |
217 | ||
218 | /** | |
3819f101 | 219 | * Set default values for the form. |
6a488035 | 220 | */ |
00be9182 | 221 | public function setDefaultValues() { |
6a488035 TO |
222 | $this->_defaults = parent::setDefaultValues(); |
223 | $targetContactValues = array(); | |
725fd9d9 N |
224 | foreach ($this->_caseId as $key => $val) { |
225 | //get all clients. | |
226 | $clients = CRM_Case_BAO_Case::getContactNames($val); | |
227 | if (isset($this->_activityId) && empty($_POST)) { | |
228 | if (!CRM_Utils_Array::crmIsEmptyArray($this->_defaults['target_contact'])) { | |
229 | $targetContactValues = array_combine(array_unique($this->_defaults['target_contact']), | |
230 | explode(';', trim($this->_defaults['target_contact_value'])) | |
231 | ); | |
232 | //exclude all clients. | |
233 | foreach ($clients as $clientId => $vals) { | |
234 | if (array_key_exists($clientId, $targetContactValues)) { | |
235 | unset($targetContactValues[$clientId]); | |
236 | } | |
6a488035 TO |
237 | } |
238 | } | |
239 | } | |
725fd9d9 | 240 | $this->assign('targetContactValues', empty($targetContactValues) ? FALSE : $targetContactValues); |
6a488035 | 241 | |
725fd9d9 N |
242 | if (isset($this->_encounterMedium)) { |
243 | $this->_defaults['medium_id'] = $this->_encounterMedium; | |
244 | } | |
245 | elseif (empty($this->_defaults['medium_id'])) { | |
246 | // set default encounter medium CRM-4816 | |
247 | $medium = CRM_Core_OptionGroup::values('encounter_medium', FALSE, FALSE, FALSE, 'AND is_default = 1'); | |
248 | if (count($medium) == 1) { | |
249 | $this->_defaults['medium_id'] = key($medium); | |
250 | } | |
6a488035 | 251 | } |
6a488035 | 252 | |
725fd9d9 N |
253 | return $this->_defaults; |
254 | } | |
6a488035 TO |
255 | } |
256 | ||
257 | public function buildQuickForm() { | |
258 | $this->_fields['source_contact_id']['label'] = ts('Reported By'); | |
f7305cbc | 259 | unset($this->_fields['status_id']['attributes']['required']); |
6a488035 | 260 | |
06f21064 TO |
261 | if ($this->restrictAssignmentByUserAccount()) { |
262 | $assigneeParameters['uf_user'] = 1; | |
263 | } | |
264 | ||
265 | $activityAssignmentGroups = $this->getActivityAssignmentGroups(); | |
266 | if (!empty($activityAssignmentGroups)) { | |
267 | $assigneeParameters['group'] = ['IN' => $activityAssignmentGroups]; | |
268 | } | |
269 | ||
270 | if (!empty($assigneeParameters)) { | |
271 | $this->_fields['assignee_contact_id']['attributes']['api']['params'] | |
272 | = array_merge($this->_fields['assignee_contact_id']['attributes']['api']['params'], $assigneeParameters); | |
273 | ||
274 | $this->_fields['followup_assignee_contact_id']['attributes']['api']['params'] | |
275 | = array_merge($this->_fields['followup_assignee_contact_id']['attributes']['api']['params'], $assigneeParameters); | |
276 | ||
277 | //Disallow creating a contact from the assignee field UI. | |
278 | $this->_fields['assignee_contact_id']['attributes']['create'] = FALSE; | |
279 | $this->_fields['followup_assignee_contact_id']['attributes']['create'] = FALSE; | |
280 | } | |
281 | ||
6a488035 TO |
282 | if ($this->_caseType) { |
283 | $xmlProcessor = new CRM_Case_XMLProcessor_Process(); | |
725fd9d9 | 284 | $aTypes = array(); |
06f21064 | 285 | foreach (array_unique($this->_caseType) as $val) { |
725fd9d9 N |
286 | $activityTypes = $xmlProcessor->get($val, 'ActivityTypes', TRUE); |
287 | $aTypes = $aTypes + $activityTypes; | |
288 | } | |
6a488035 TO |
289 | |
290 | // remove Open Case activity type since we're inside an existing case | |
9c248a42 | 291 | $openCaseID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Open Case'); |
6a488035 TO |
292 | unset($aTypes[$openCaseID]); |
293 | asort($aTypes); | |
a1a2a83d | 294 | $this->_fields['followup_activity_type_id']['attributes'] = array('' => '- select activity type -') + $aTypes; |
6a488035 TO |
295 | } |
296 | ||
b7617307 | 297 | parent::buildQuickForm(); |
6a488035 TO |
298 | |
299 | if ($this->_action & (CRM_Core_Action::DELETE | CRM_Core_Action::DETACH | CRM_Core_Action::RENEW)) { | |
300 | return; | |
301 | } | |
302 | ||
6a488035 TO |
303 | $this->assign('urlPath', 'civicrm/case/activity'); |
304 | ||
305 | $encounterMediums = CRM_Case_PseudoConstant::encounterMedium(); | |
cedb74cd | 306 | |
06f51047 CW |
307 | if ($this->_activityTypeFile == 'OpenCase' && $this->_action == CRM_Core_Action::UPDATE) { |
308 | $this->getElement('activity_date_time')->freeze(); | |
309 | ||
cedb74cd MWMC |
310 | if ($this->_activityId) { |
311 | // 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. | |
312 | // Is that really a big problem? | |
313 | $this->_encounterMedium = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $this->_activityId, 'medium_id'); | |
314 | if (!array_key_exists($this->_encounterMedium, $encounterMediums)) { | |
315 | $encounterMediums[$this->_encounterMedium] = CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'medium_id', $this->_encounterMedium); | |
316 | } | |
6a488035 TO |
317 | } |
318 | } | |
319 | ||
320 | $this->add('select', 'medium_id', ts('Medium'), $encounterMediums, TRUE); | |
725fd9d9 N |
321 | $i = 0; |
322 | foreach ($this->_caseId as $key => $val) { | |
a82f59a1 | 323 | $this->_relatedContacts[] = $rgc = CRM_Case_BAO_Case::getRelatedAndGlobalContacts($val); |
725fd9d9 N |
324 | $contName = CRM_Case_BAO_Case::getContactNames($val); |
325 | foreach ($contName as $nkey => $nval) { | |
e547f744 | 326 | array_push($this->_relatedContacts[$i][0], $this->_relatedContacts[$i][0]['managerOf'] = $nval['display_name']); |
725fd9d9 N |
327 | } |
328 | $i++; | |
329 | } | |
6a488035 | 330 | |
6a488035 | 331 | //add case client in send a copy selector.CRM-4438. |
725fd9d9 | 332 | foreach ($this->_caseId as $key => $val) { |
e547f744 | 333 | $relatedContacts[] = $relCon = CRM_Case_BAO_Case::getContactNames($val); |
725fd9d9 N |
334 | } |
335 | ||
6a488035 TO |
336 | if (!empty($relatedContacts)) { |
337 | foreach ($relatedContacts as $relatedContact) { | |
338 | $this->_relatedContacts[] = $relatedContact; | |
339 | } | |
340 | } | |
341 | ||
342 | if (!empty($this->_relatedContacts)) { | |
343 | $checkBoxes = array(); | |
344 | foreach ($this->_relatedContacts as $id => $row) { | |
ee1ccac5 EH |
345 | foreach ($row as $key => $value) { |
346 | $checkBoxes[$key] = $this->addElement('checkbox', $key, NULL, NULL, array('class' => 'select-row')); | |
347 | } | |
6a488035 TO |
348 | } |
349 | ||
350 | $this->addGroup($checkBoxes, 'contact_check'); | |
351 | $this->addElement('checkbox', 'toggleSelect', NULL, NULL, | |
d664f648 | 352 | array('class' => 'select-rows') |
6a488035 TO |
353 | ); |
354 | $this->assign('searchRows', $this->_relatedContacts); | |
355 | } | |
a82f59a1 | 356 | $this->_relatedContacts = $rgc + $relCon; |
6a488035 TO |
357 | |
358 | $this->addFormRule(array('CRM_Case_Form_Activity', 'formRule'), $this); | |
359 | } | |
360 | ||
361 | /** | |
d2e5d2ce | 362 | * Global form rule. |
6a488035 | 363 | * |
64bd5a0e TO |
364 | * @param array $fields |
365 | * The input form values. | |
366 | * @param array $files | |
367 | * The uploaded files if any. | |
77b97be7 EM |
368 | * @param $self |
369 | * | |
72b3a70c CW |
370 | * @return bool|array |
371 | * true if no errors, else array of errors | |
6a488035 | 372 | */ |
00be9182 | 373 | public static function formRule($fields, $files, $self) { |
6a488035 TO |
374 | // skip form rule if deleting |
375 | if (CRM_Utils_Array::value('_qf_Activity_next_', $fields) == 'Delete' || CRM_Utils_Array::value('_qf_Activity_next_', $fields) == 'Restore') { | |
376 | return TRUE; | |
377 | } | |
378 | ||
6ea16bbf | 379 | return parent::formRule($fields, $files, $self); |
6a488035 TO |
380 | } |
381 | ||
382 | /** | |
d2e5d2ce | 383 | * Process the form submission. |
6a488035 | 384 | * |
100fef9d | 385 | * @param array $params |
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', | |
21dfd5f5 | 411 | 'entity_id' => $this->_activityId, |
6a488035 TO |
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 | ||
6a488035 TO |
438 | $params['activity_type_id'] = $this->_activityTypeId; |
439 | ||
440 | // format with contact (target contact) values | |
3911d992 DJ |
441 | if (isset($params['target_contact_id'])) { |
442 | $params['target_contact_id'] = explode(',', $params['target_contact_id']); | |
6a488035 TO |
443 | } |
444 | else { | |
445 | $params['target_contact_id'] = array(); | |
446 | } | |
447 | ||
448 | // format activity custom data | |
a7488080 | 449 | if (!empty($params['hidden_custom'])) { |
6a488035 | 450 | if ($this->_activityId) { |
756b5b30 | 451 | // retrieve and include the custom data of old Activity |
452 | $oldActivity = civicrm_api3('Activity', 'getsingle', array('id' => $this->_activityId)); | |
309c49b2 | 453 | $params = array_merge($oldActivity, $params); |
756b5b30 | 454 | |
6a488035 TO |
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, | |
6a488035 TO |
480 | $this->_activityId, |
481 | 'Activity' | |
482 | ); | |
483 | } | |
484 | ||
50237bc9 | 485 | // assigning formatted value |
a7488080 | 486 | if (!empty($params['assignee_contact_id'])) { |
6a488035 TO |
487 | $params['assignee_contact_id'] = explode(',', $params['assignee_contact_id']); |
488 | } | |
489 | else { | |
490 | $params['assignee_contact_id'] = array(); | |
491 | } | |
492 | ||
6a488035 TO |
493 | if (isset($this->_activityId)) { |
494 | // activity which hasn't been modified by a user yet | |
495 | if ($this->_defaults['is_auto'] == 1) { | |
496 | $params['is_auto'] = 0; | |
497 | } | |
498 | ||
499 | // always create a revision of an case activity. CRM-4533 | |
500 | $newActParams = $params; | |
501 | ||
502 | // add target contact values in update mode | |
503 | if (empty($params['target_contact_id']) && !empty($this->_defaults['target_contact'])) { | |
504 | $newActParams['target_contact_id'] = $this->_defaults['target_contact']; | |
505 | } | |
6a488035 TO |
506 | } |
507 | ||
508 | if (!isset($newActParams)) { | |
509 | // add more attachments if needed for old activity | |
510 | CRM_Core_BAO_File::formatAttachment($params, | |
511 | $params, | |
512 | 'civicrm_activity' | |
513 | ); | |
514 | ||
515 | // call begin post process, before the activity is created/updated. | |
516 | $this->beginPostProcess($params); | |
725fd9d9 N |
517 | foreach ($this->_caseId as $key => $val) { |
518 | $params['case_id'] = $val; | |
519 | // activity create/update | |
520 | $activity = CRM_Activity_BAO_Activity::create($params); | |
521 | $vvalue[] = array('case_id' => $val, 'actId' => $activity->id); | |
522 | // call end post process, after the activity has been created/updated. | |
523 | $this->endPostProcess($params, $activity); | |
524 | } | |
6a488035 TO |
525 | } |
526 | else { | |
527 | // since the params we need to set are very few, and we don't want rest of the | |
528 | // work done by bao create method , lets use dao object to make the changes | |
529 | $params = array('id' => $this->_activityId); | |
530 | $params['is_current_revision'] = 0; | |
531 | $activity = new CRM_Activity_DAO_Activity(); | |
532 | $activity->copyValues($params); | |
533 | $activity->save(); | |
534 | } | |
535 | ||
536 | // create a new version of activity if activity was found to | |
537 | // have been modified/created by user | |
538 | if (isset($newActParams)) { | |
539 | // set proper original_id | |
a7488080 | 540 | if (!empty($this->_defaults['original_id'])) { |
6a488035 TO |
541 | $newActParams['original_id'] = $this->_defaults['original_id']; |
542 | } | |
543 | else { | |
544 | $newActParams['original_id'] = $activity->id; | |
545 | } | |
6a488035 | 546 | |
756b5b30 | 547 | //is_current_revision will be set to 1 by default. |
6a488035 TO |
548 | // add attachments if any |
549 | CRM_Core_BAO_File::formatAttachment($newActParams, | |
550 | $newActParams, | |
551 | 'civicrm_activity' | |
552 | ); | |
553 | ||
554 | // call begin post process, before the activity is created/updated. | |
555 | $this->beginPostProcess($newActParams); | |
725fd9d9 N |
556 | foreach ($this->_caseId as $key => $val) { |
557 | $newActParams['case_id'] = $val; | |
558 | $activity = CRM_Activity_BAO_Activity::create($newActParams); | |
e547f744 | 559 | $vvalue[] = array('case_id' => $val, 'actId' => $activity->id); |
725fd9d9 N |
560 | // call end post process, after the activity has been created/updated. |
561 | $this->endPostProcess($newActParams, $activity); | |
562 | } | |
6a488035 TO |
563 | // copy files attached to old activity if any, to new one, |
564 | // as long as users have not selected the 'delete attachment' option. | |
8506cda6 | 565 | if (empty($newActParams['is_delete_attachment']) && ($this->_activityId != $activity->id)) { |
6a488035 TO |
566 | CRM_Core_BAO_File::copyEntityFile('civicrm_activity', $this->_activityId, |
567 | 'civicrm_activity', $activity->id | |
568 | ); | |
569 | } | |
570 | ||
571 | // copy back params to original var | |
572 | $params = $newActParams; | |
573 | } | |
574 | ||
725fd9d9 N |
575 | foreach ($vvalue as $vkey => $vval) { |
576 | if ($vval['actId']) { | |
577 | // add tags if exists | |
578 | $tagParams = array(); | |
579 | if (!empty($params['tag'])) { | |
63b7aefc DB |
580 | if (!is_array($params['tag'])) { |
581 | $params['tag'] = explode(',', $params['tag']); | |
725fd9d9 | 582 | } |
63b7aefc DB |
583 | |
584 | $tagParams = array_fill_keys($params['tag'], 1); | |
6a488035 | 585 | } |
6a488035 | 586 | |
725fd9d9 N |
587 | //save static tags |
588 | CRM_Core_BAO_EntityTag::create($tagParams, 'civicrm_activity', $vval['actId']); | |
6a488035 | 589 | |
725fd9d9 N |
590 | //save free tags |
591 | if (isset($params['taglist']) && !empty($params['taglist'])) { | |
592 | CRM_Core_Form_Tag::postProcess($params['taglist'], $vval['actId'], 'civicrm_activity', $this); | |
593 | } | |
6a488035 | 594 | } |
6a488035 | 595 | |
725fd9d9 N |
596 | // update existing case record if needed |
597 | $caseParams = $params; | |
598 | $caseParams['id'] = $vval['case_id']; | |
725fd9d9 N |
599 | if (!empty($caseParams['case_status_id'])) { |
600 | $caseParams['status_id'] = $caseParams['case_status_id']; | |
601 | } | |
6a488035 | 602 | |
725fd9d9 N |
603 | // unset params intended for activities only |
604 | unset($caseParams['subject'], $caseParams['details'], | |
605 | $caseParams['status_id'], $caseParams['custom'] | |
606 | ); | |
1b67821f | 607 | CRM_Case_BAO_Case::create($caseParams); |
725fd9d9 N |
608 | // create case activity record |
609 | $caseParams = array( | |
610 | 'activity_id' => $vval['actId'], | |
611 | 'case_id' => $vval['case_id'], | |
612 | ); | |
613 | CRM_Case_BAO_Case::processCaseActivity($caseParams); | |
6a488035 TO |
614 | } |
615 | ||
6a488035 TO |
616 | // send copy to selected contacts. |
617 | $mailStatus = ''; | |
618 | $mailToContacts = array(); | |
619 | ||
620 | //CRM-5695 | |
621 | //check for notification settings for assignee contacts | |
622 | $selectedContacts = array('contact_check'); | |
aaffa79f | 623 | if (Civi::settings()->get('activity_assignee_notification')) { |
6a488035 TO |
624 | $selectedContacts[] = 'assignee_contact_id'; |
625 | } | |
626 | ||
725fd9d9 N |
627 | foreach ($vvalue as $vkey => $vval) { |
628 | foreach ($selectedContacts as $dnt => $val) { | |
35522279 | 629 | if (array_key_exists($val, $params) && !CRM_Utils_Array::crmIsEmptyArray($params[$val])) { |
cd122921 | 630 | if ($val == 'contact_check') { |
725fd9d9 | 631 | $mailStatus = ts("A copy of the activity has also been sent to selected contacts(s)."); |
cd122921 | 632 | } |
633 | else { | |
725fd9d9 N |
634 | $this->_relatedContacts = CRM_Activity_BAO_ActivityAssignment::getAssigneeNames(array($vval['actId']), TRUE, FALSE); |
635 | $mailStatus .= ' ' . ts("A copy of the activity has also been sent to assignee contacts(s)."); | |
cd122921 | 636 | } |
725fd9d9 N |
637 | //build an associative array with unique email addresses. |
638 | foreach ($params[$val] as $key => $value) { | |
639 | if ($val == 'contact_check') { | |
640 | $id = $key; | |
6a488035 TO |
641 | } |
642 | else { | |
725fd9d9 N |
643 | $id = $value; |
644 | } | |
645 | ||
646 | if (isset($id) && array_key_exists($id, $this->_relatedContacts) && isset($this->_relatedContacts[$id]['email'])) { | |
647 | //if email already exists in array then append with ', ' another role only otherwise add it to array. | |
648 | if ($contactDetails = CRM_Utils_Array::value($this->_relatedContacts[$id]['email'], $mailToContacts)) { | |
649 | $caseRole = CRM_Utils_Array::value('role', $this->_relatedContacts[$id]); | |
650 | $mailToContacts[$this->_relatedContacts[$id]['email']]['role'] = $contactDetails['role'] . ', ' . $caseRole; | |
651 | } | |
652 | else { | |
653 | $mailToContacts[$this->_relatedContacts[$id]['email']] = $this->_relatedContacts[$id]; | |
654 | } | |
6a488035 TO |
655 | } |
656 | } | |
657 | } | |
658 | } | |
6a488035 | 659 | |
cc7982a1 | 660 | $extraParams = array('case_id' => $vval['case_id'], 'client_id' => $this->_currentlyViewedContactId); |
661 | $result = CRM_Activity_BAO_Activity::sendToAssignee($activity, $mailToContacts, $extraParams); | |
fc110b68 | 662 | if (empty($result)) { |
6a488035 TO |
663 | $mailStatus = ''; |
664 | } | |
6a488035 | 665 | |
725fd9d9 N |
666 | // create follow up activity if needed |
667 | $followupStatus = ''; | |
668 | if (!empty($params['followup_activity_type_id'])) { | |
669 | $followupActivity = CRM_Activity_BAO_Activity::createFollowupActivity($vval['actId'], $params); | |
6a488035 | 670 | |
725fd9d9 N |
671 | if ($followupActivity) { |
672 | $caseParams = array( | |
353ffa53 TO |
673 | 'activity_id' => $followupActivity->id, |
674 | 'case_id' => $vval['case_id'], | |
725fd9d9 N |
675 | ); |
676 | CRM_Case_BAO_Case::processCaseActivity($caseParams); | |
7c77009f | 677 | $followupStatus = ts("A followup activity has been scheduled.") . '<br /><br />'; |
725fd9d9 | 678 | } |
6a488035 | 679 | } |
7c77009f | 680 | $title = ts("%1 Saved", array(1 => $this->_activityTypeName)); |
a62473e7 | 681 | CRM_Core_Session::setStatus($followupStatus . $mailStatus, $title, 'success'); |
725fd9d9 | 682 | } |
6a488035 | 683 | } |
96025800 | 684 | |
06f21064 TO |
685 | /** |
686 | * Returns the groups that contacts must belong to in order to be assigned | |
687 | * an activity for this case. It returns an empty array if no groups are found for | |
688 | * the case type linked to the caseId. | |
689 | * | |
690 | * @return array | |
691 | */ | |
692 | private function getActivityAssignmentGroups() { | |
693 | if (!$this->_caseTypeDefinition) { | |
694 | return []; | |
695 | } | |
696 | ||
697 | $assignmentGroups = []; | |
698 | foreach ($this->_caseTypeDefinition as $caseId => $definition) { | |
699 | if (!empty($definition['activityAsgmtGrps'])) { | |
700 | $assignmentGroups = array_merge($assignmentGroups, $definition['activityAsgmtGrps']); | |
701 | } | |
702 | } | |
703 | ||
704 | return $assignmentGroups; | |
705 | } | |
706 | ||
707 | /** | |
708 | * Returns whether contacts must have a user account in order to be | |
709 | * assigned an activity for this case. | |
710 | * | |
711 | * @return bool | |
712 | */ | |
713 | private function restrictAssignmentByUserAccount() { | |
714 | if (!$this->_caseTypeDefinition) { | |
715 | return FALSE; | |
716 | } | |
717 | ||
718 | foreach ($this->_caseTypeDefinition as $caseId => $definition) { | |
719 | if (!empty($definition['restrictActivityAsgmtToCmsUser'])) { | |
720 | return TRUE; | |
721 | } | |
722 | } | |
723 | ||
724 | return FALSE; | |
725 | } | |
726 | ||
727 | /** | |
728 | * Returns the case type definition column value for the case type linked to the caseId. | |
729 | * | |
730 | * @return array | |
731 | */ | |
732 | private function getCaseTypeDefinition() { | |
733 | if (!$this->_caseId) { | |
734 | return []; | |
735 | } | |
736 | ||
737 | $definitions = civicrm_api3('CaseType', 'get', [ | |
738 | 'return' => ['name', 'definition'], | |
739 | 'name' => ['IN' => array_unique($this->_caseType)], | |
740 | ]); | |
741 | ||
742 | return array_column($definitions['values'], 'definition', 'name'); | |
743 | } | |
744 | ||
6a488035 | 745 | } |