Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
32 | */ |
33 | class CRM_Case_XMLProcessor_Process extends CRM_Case_XMLProcessor { | |
4c6ce474 | 34 | /** |
67d19299 | 35 | * Run. |
36 | * | |
37 | * @param string $caseType | |
c490a46a | 38 | * @param array $params |
4c6ce474 | 39 | * |
cde2037d | 40 | * @return bool |
4c6ce474 EM |
41 | * @throws Exception |
42 | */ | |
00be9182 | 43 | public function run($caseType, &$params) { |
6a488035 TO |
44 | $xml = $this->retrieve($caseType); |
45 | ||
46 | if ($xml === FALSE) { | |
6b02fdc3 | 47 | $docLink = CRM_Utils_System::docURL2("user/case-management/set-up"); |
6a488035 | 48 | CRM_Core_Error::fatal(ts("Configuration file could not be retrieved for case type = '%1' %2.", |
353ffa53 TO |
49 | array(1 => $caseType, 2 => $docLink) |
50 | )); | |
6a488035 TO |
51 | return FALSE; |
52 | } | |
53 | ||
54 | $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process(); | |
55 | $this->_isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients(); | |
56 | ||
57 | $this->process($xml, $params); | |
58 | } | |
59 | ||
4c6ce474 EM |
60 | /** |
61 | * @param $caseType | |
62 | * @param $fieldSet | |
63 | * @param bool $isLabel | |
64 | * @param bool $maskAction | |
65 | * | |
66 | * @return array|bool|mixed | |
67 | * @throws Exception | |
68 | */ | |
00be9182 | 69 | public function get($caseType, $fieldSet, $isLabel = FALSE, $maskAction = FALSE) { |
6a488035 TO |
70 | $xml = $this->retrieve($caseType); |
71 | if ($xml === FALSE) { | |
6b02fdc3 | 72 | $docLink = CRM_Utils_System::docURL2("user/case-management/set-up"); |
6a488035 | 73 | CRM_Core_Error::fatal(ts("Unable to load configuration file for the referenced case type: '%1' %2.", |
353ffa53 TO |
74 | array(1 => $caseType, 2 => $docLink) |
75 | )); | |
6a488035 TO |
76 | return FALSE; |
77 | } | |
78 | ||
79 | switch ($fieldSet) { | |
80 | case 'CaseRoles': | |
81 | return $this->caseRoles($xml->CaseRoles); | |
82 | ||
83 | case 'ActivitySets': | |
84 | return $this->activitySets($xml->ActivitySets); | |
85 | ||
86 | case 'ActivityTypes': | |
87 | return $this->activityTypes($xml->ActivityTypes, FALSE, $isLabel, $maskAction); | |
88 | } | |
89 | } | |
90 | ||
4c6ce474 EM |
91 | /** |
92 | * @param $xml | |
c490a46a | 93 | * @param array $params |
4c6ce474 EM |
94 | * |
95 | * @throws Exception | |
96 | */ | |
00be9182 | 97 | public function process($xml, &$params) { |
6a488035 | 98 | $standardTimeline = CRM_Utils_Array::value('standardTimeline', $params); |
353ffa53 | 99 | $activitySetName = CRM_Utils_Array::value('activitySetName', $params); |
6a488035 TO |
100 | $activityTypeName = CRM_Utils_Array::value('activityTypeName', $params); |
101 | ||
102 | if ('Open Case' == CRM_Utils_Array::value('activityTypeName', $params)) { | |
103 | // create relationships for the ones that are required | |
104 | foreach ($xml->CaseRoles as $caseRoleXML) { | |
105 | foreach ($caseRoleXML->RelationshipType as $relationshipTypeXML) { | |
106 | if ((int ) $relationshipTypeXML->creator == 1) { | |
107 | if (!$this->createRelationships((string ) $relationshipTypeXML->name, | |
353ffa53 TO |
108 | $params |
109 | ) | |
110 | ) { | |
6a488035 TO |
111 | CRM_Core_Error::fatal(); |
112 | return FALSE; | |
113 | } | |
114 | } | |
115 | } | |
116 | } | |
117 | } | |
118 | ||
119 | if ('Change Case Start Date' == CRM_Utils_Array::value('activityTypeName', $params)) { | |
120 | // delete all existing activities which are non-empty | |
121 | $this->deleteEmptyActivity($params); | |
122 | } | |
123 | ||
124 | foreach ($xml->ActivitySets as $activitySetsXML) { | |
125 | foreach ($activitySetsXML->ActivitySet as $activitySetXML) { | |
126 | if ($standardTimeline) { | |
127 | if ((boolean ) $activitySetXML->timeline) { | |
128 | return $this->processStandardTimeline($activitySetXML, | |
129 | $params | |
130 | ); | |
131 | } | |
132 | } | |
133 | elseif ($activitySetName) { | |
134 | $name = (string ) $activitySetXML->name; | |
135 | if ($name == $activitySetName) { | |
136 | return $this->processActivitySet($activitySetXML, | |
137 | $params | |
138 | ); | |
139 | } | |
140 | } | |
141 | } | |
142 | } | |
143 | } | |
144 | ||
4c6ce474 EM |
145 | /** |
146 | * @param $activitySetXML | |
c490a46a | 147 | * @param array $params |
4c6ce474 | 148 | */ |
00be9182 | 149 | public function processStandardTimeline($activitySetXML, &$params) { |
448e7600 | 150 | if ('Change Case Type' == CRM_Utils_Array::value('activityTypeName', $params) |
353ffa53 TO |
151 | && CRM_Utils_Array::value('resetTimeline', $params, TRUE) |
152 | ) { | |
6a488035 TO |
153 | // delete all existing activities which are non-empty |
154 | $this->deleteEmptyActivity($params); | |
155 | } | |
156 | ||
157 | foreach ($activitySetXML->ActivityTypes as $activityTypesXML) { | |
158 | foreach ($activityTypesXML as $activityTypeXML) { | |
159 | $this->createActivity($activityTypeXML, $params); | |
160 | } | |
161 | } | |
162 | } | |
163 | ||
4c6ce474 EM |
164 | /** |
165 | * @param $activitySetXML | |
c490a46a | 166 | * @param array $params |
4c6ce474 | 167 | */ |
00be9182 | 168 | public function processActivitySet($activitySetXML, &$params) { |
6a488035 TO |
169 | foreach ($activitySetXML->ActivityTypes as $activityTypesXML) { |
170 | foreach ($activityTypesXML as $activityTypeXML) { | |
171 | $this->createActivity($activityTypeXML, $params); | |
172 | } | |
173 | } | |
174 | } | |
175 | ||
4c6ce474 EM |
176 | /** |
177 | * @param $caseRolesXML | |
178 | * @param bool $isCaseManager | |
179 | * | |
180 | * @return array|mixed | |
181 | */ | |
00be9182 | 182 | public function &caseRoles($caseRolesXML, $isCaseManager = FALSE) { |
6a488035 TO |
183 | $relationshipTypes = &$this->allRelationshipTypes(); |
184 | ||
185 | $result = array(); | |
186 | foreach ($caseRolesXML as $caseRoleXML) { | |
187 | foreach ($caseRoleXML->RelationshipType as $relationshipTypeXML) { | |
188 | $relationshipTypeName = (string ) $relationshipTypeXML->name; | |
189 | $relationshipTypeID = array_search($relationshipTypeName, | |
190 | $relationshipTypes | |
191 | ); | |
192 | if ($relationshipTypeID === FALSE) { | |
193 | continue; | |
194 | } | |
195 | ||
196 | if (!$isCaseManager) { | |
197 | $result[$relationshipTypeID] = $relationshipTypeName; | |
198 | } | |
199 | elseif ($relationshipTypeXML->manager) { | |
200 | return $relationshipTypeID; | |
201 | } | |
202 | } | |
203 | } | |
204 | return $result; | |
205 | } | |
206 | ||
4c6ce474 | 207 | /** |
100fef9d | 208 | * @param string $relationshipTypeName |
c490a46a | 209 | * @param array $params |
4c6ce474 EM |
210 | * |
211 | * @return bool | |
212 | * @throws Exception | |
213 | */ | |
00be9182 | 214 | public function createRelationships($relationshipTypeName, &$params) { |
6a488035 TO |
215 | $relationshipTypes = &$this->allRelationshipTypes(); |
216 | // get the relationship id | |
217 | $relationshipTypeID = array_search($relationshipTypeName, $relationshipTypes); | |
218 | ||
219 | if ($relationshipTypeID === FALSE) { | |
6b02fdc3 | 220 | $docLink = CRM_Utils_System::docURL2("user/case-management/set-up"); |
6a488035 | 221 | CRM_Core_Error::fatal(ts('Relationship type %1, found in case configuration file, is not present in the database %2', |
353ffa53 TO |
222 | array(1 => $relationshipTypeName, 2 => $docLink) |
223 | )); | |
6a488035 TO |
224 | return FALSE; |
225 | } | |
226 | ||
227 | $client = $params['clientID']; | |
228 | if (!is_array($client)) { | |
229 | $client = array($client); | |
230 | } | |
231 | ||
232 | foreach ($client as $key => $clientId) { | |
233 | $relationshipParams = array( | |
234 | 'relationship_type_id' => $relationshipTypeID, | |
235 | 'contact_id_a' => $clientId, | |
236 | 'contact_id_b' => $params['creatorID'], | |
237 | 'is_active' => 1, | |
238 | 'case_id' => $params['caseID'], | |
239 | 'start_date' => date("Ymd"), | |
240 | ); | |
241 | ||
242 | if (!$this->createRelationship($relationshipParams)) { | |
243 | CRM_Core_Error::fatal(); | |
244 | return FALSE; | |
245 | } | |
246 | } | |
247 | return TRUE; | |
248 | } | |
249 | ||
4c6ce474 | 250 | /** |
c490a46a | 251 | * @param array $params |
4c6ce474 EM |
252 | * |
253 | * @return bool | |
254 | */ | |
00be9182 | 255 | public function createRelationship(&$params) { |
6a488035 TO |
256 | $dao = new CRM_Contact_DAO_Relationship(); |
257 | $dao->copyValues($params); | |
258 | // only create a relationship if it does not exist | |
259 | if (!$dao->find(TRUE)) { | |
260 | $dao->save(); | |
261 | } | |
262 | return TRUE; | |
263 | } | |
264 | ||
4c6ce474 EM |
265 | /** |
266 | * @param $activityTypesXML | |
267 | * @param bool $maxInst | |
268 | * @param bool $isLabel | |
269 | * @param bool $maskAction | |
270 | * | |
271 | * @return array | |
272 | */ | |
00be9182 | 273 | public function activityTypes($activityTypesXML, $maxInst = FALSE, $isLabel = FALSE, $maskAction = FALSE) { |
6a488035 TO |
274 | $activityTypes = &$this->allActivityTypes(TRUE, TRUE); |
275 | $result = array(); | |
276 | foreach ($activityTypesXML as $activityTypeXML) { | |
277 | foreach ($activityTypeXML as $recordXML) { | |
278 | $activityTypeName = (string ) $recordXML->name; | |
353ffa53 | 279 | $maxInstances = (string ) $recordXML->max_instances; |
6a488035 TO |
280 | $activityTypeInfo = CRM_Utils_Array::value($activityTypeName, $activityTypes); |
281 | ||
282 | if ($activityTypeInfo['id']) { | |
283 | if ($maskAction) { | |
284 | if ($maskAction == 'edit' && '0' === (string ) $recordXML->editable) { | |
285 | $result[$maskAction][] = $activityTypeInfo['id']; | |
286 | } | |
287 | } | |
288 | else { | |
289 | if (!$maxInst) { | |
290 | //if we want,labels of activities should be returned. | |
291 | if ($isLabel) { | |
292 | $result[$activityTypeInfo['id']] = $activityTypeInfo['label']; | |
293 | } | |
294 | else { | |
295 | $result[$activityTypeInfo['id']] = $activityTypeName; | |
296 | } | |
297 | } | |
298 | else { | |
299 | if ($maxInstances) { | |
300 | $result[$activityTypeName] = $maxInstances; | |
301 | } | |
302 | } | |
303 | } | |
304 | } | |
305 | } | |
306 | } | |
307 | ||
308 | // call option value hook | |
309 | CRM_Utils_Hook::optionValues($result, 'case_activity_type'); | |
310 | ||
311 | return $result; | |
312 | } | |
313 | ||
e19323c9 TO |
314 | /** |
315 | * @param SimpleXMLElement $caseTypeXML | |
316 | * @return array<string> symbolic activity-type names | |
317 | */ | |
00be9182 | 318 | public function getDeclaredActivityTypes($caseTypeXML) { |
e19323c9 | 319 | $result = array(); |
83151a3f DJ |
320 | |
321 | if (!empty($caseTypeXML->ActivityTypes) && $caseTypeXML->ActivityTypes->ActivityType) { | |
322 | foreach ($caseTypeXML->ActivityTypes->ActivityType as $activityTypeXML) { | |
323 | $result[] = (string) $activityTypeXML->name; | |
e19323c9 | 324 | } |
83151a3f | 325 | } |
e19323c9 | 326 | |
83151a3f DJ |
327 | if (!empty($caseTypeXML->ActivitySets) && $caseTypeXML->ActivitySets->ActivitySet) { |
328 | foreach ($caseTypeXML->ActivitySets->ActivitySet as $activitySetXML) { | |
329 | if ($activitySetXML->ActivityTypes && $activitySetXML->ActivityTypes->ActivityType) { | |
330 | foreach ($activitySetXML->ActivityTypes->ActivityType as $activityTypeXML) { | |
331 | $result[] = (string) $activityTypeXML->name; | |
e19323c9 TO |
332 | } |
333 | } | |
334 | } | |
335 | } | |
83151a3f | 336 | |
e19323c9 TO |
337 | $result = array_unique($result); |
338 | sort($result); | |
339 | return $result; | |
340 | } | |
341 | ||
342 | /** | |
343 | * @param SimpleXMLElement $caseTypeXML | |
344 | * @return array<string> symbolic relationship-type names | |
345 | */ | |
00be9182 | 346 | public function getDeclaredRelationshipTypes($caseTypeXML) { |
e19323c9 | 347 | $result = array(); |
83151a3f DJ |
348 | |
349 | if (!empty($caseTypeXML->CaseRoles) && $caseTypeXML->CaseRoles->RelationshipType) { | |
350 | foreach ($caseTypeXML->CaseRoles->RelationshipType as $relTypeXML) { | |
351 | $result[] = (string) $relTypeXML->name; | |
e19323c9 TO |
352 | } |
353 | } | |
83151a3f | 354 | |
e19323c9 TO |
355 | $result = array_unique($result); |
356 | sort($result); | |
357 | return $result; | |
358 | } | |
359 | ||
4c6ce474 | 360 | /** |
c490a46a | 361 | * @param array $params |
4c6ce474 | 362 | */ |
00be9182 | 363 | public function deleteEmptyActivity(&$params) { |
44f817d4 | 364 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
9e74e3ce | 365 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
8ef12e64 | 366 | |
6a488035 TO |
367 | $query = " |
368 | DELETE a | |
369 | FROM civicrm_activity a | |
91da6cd5 | 370 | INNER JOIN civicrm_activity_contact t ON t.activity_id = a.id |
97875df1 | 371 | INNER JOIN civicrm_case_activity ca on ca.activity_id = a.id |
91da6cd5 | 372 | WHERE t.contact_id = %1 |
9e74e3ce | 373 | AND t.record_type_id = $targetID |
6a488035 TO |
374 | AND a.is_auto = 1 |
375 | AND a.is_current_revision = 1 | |
97875df1 | 376 | AND ca.case_id = %2 |
6a488035 | 377 | "; |
97875df1 | 378 | $sqlParams = array(1 => array($params['clientID'], 'Integer'), 2 => array($params['caseID'], 'Integer')); |
6a488035 TO |
379 | CRM_Core_DAO::executeQuery($query, $sqlParams); |
380 | } | |
381 | ||
4c6ce474 | 382 | /** |
c490a46a | 383 | * @param array $params |
4c6ce474 EM |
384 | * |
385 | * @return bool | |
386 | */ | |
00be9182 | 387 | public function isActivityPresent(&$params) { |
6a488035 TO |
388 | $query = " |
389 | SELECT count(a.id) | |
390 | FROM civicrm_activity a | |
391 | INNER JOIN civicrm_case_activity ca on ca.activity_id = a.id | |
392 | WHERE a.activity_type_id = %1 | |
393 | AND ca.case_id = %2 | |
394 | AND a.is_deleted = 0 | |
395 | "; | |
396 | ||
e547f744 | 397 | $sqlParams = array( |
353ffa53 | 398 | 1 => array($params['activityTypeID'], 'Integer'), |
6a488035 TO |
399 | 2 => array($params['caseID'], 'Integer'), |
400 | ); | |
401 | $count = CRM_Core_DAO::singleValueQuery($query, $sqlParams); | |
402 | ||
403 | // check for max instance | |
6f55186b | 404 | $caseType = CRM_Case_BAO_Case::getCaseType($params['caseID'], 'name'); |
6a488035 TO |
405 | $maxInstance = self::getMaxInstance($caseType, $params['activityTypeName']); |
406 | ||
407 | return $maxInstance ? ($count < $maxInstance ? FALSE : TRUE) : FALSE; | |
408 | } | |
409 | ||
4c6ce474 EM |
410 | /** |
411 | * @param $activityTypeXML | |
c490a46a | 412 | * @param array $params |
4c6ce474 EM |
413 | * |
414 | * @return bool | |
415 | * @throws CRM_Core_Exception | |
416 | * @throws Exception | |
417 | */ | |
00be9182 | 418 | public function createActivity($activityTypeXML, &$params) { |
6a488035 | 419 | $activityTypeName = (string) $activityTypeXML->name; |
353ffa53 | 420 | $activityTypes = &$this->allActivityTypes(TRUE, TRUE); |
6a488035 TO |
421 | $activityTypeInfo = CRM_Utils_Array::value($activityTypeName, $activityTypes); |
422 | ||
423 | if (!$activityTypeInfo) { | |
6b02fdc3 | 424 | $docLink = CRM_Utils_System::docURL2("user/case-management/set-up"); |
6a488035 | 425 | CRM_Core_Error::fatal(ts('Activity type %1, found in case configuration file, is not present in the database %2', |
353ffa53 TO |
426 | array(1 => $activityTypeName, 2 => $docLink) |
427 | )); | |
6a488035 TO |
428 | return FALSE; |
429 | } | |
430 | ||
431 | $activityTypeID = $activityTypeInfo['id']; | |
432 | ||
433 | if (isset($activityTypeXML->status)) { | |
434 | $statusName = (string) $activityTypeXML->status; | |
435 | } | |
436 | else { | |
437 | $statusName = 'Scheduled'; | |
438 | } | |
439 | ||
10befc1f | 440 | $client = (array) $params['clientID']; |
6a488035 TO |
441 | |
442 | //set order | |
443 | $orderVal = ''; | |
444 | if (isset($activityTypeXML->order)) { | |
445 | $orderVal = (string) $activityTypeXML->order; | |
446 | } | |
447 | ||
448 | if ($activityTypeName == 'Open Case') { | |
449 | $activityParams = array( | |
450 | 'activity_type_id' => $activityTypeID, | |
451 | 'source_contact_id' => $params['creatorID'], | |
452 | 'is_auto' => FALSE, | |
453 | 'is_current_revision' => 1, | |
454 | 'subject' => CRM_Utils_Array::value('subject', $params) ? $params['subject'] : $activityTypeName, | |
d66c61b6 | 455 | 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', $statusName), |
6a488035 TO |
456 | 'target_contact_id' => $client, |
457 | 'medium_id' => CRM_Utils_Array::value('medium_id', $params), | |
458 | 'location' => CRM_Utils_Array::value('location', $params), | |
459 | 'details' => CRM_Utils_Array::value('details', $params), | |
460 | 'duration' => CRM_Utils_Array::value('duration', $params), | |
461 | 'weight' => $orderVal, | |
462 | ); | |
463 | } | |
464 | else { | |
465 | $activityParams = array( | |
466 | 'activity_type_id' => $activityTypeID, | |
467 | 'source_contact_id' => $params['creatorID'], | |
468 | 'is_auto' => TRUE, | |
469 | 'is_current_revision' => 1, | |
d66c61b6 | 470 | 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', $statusName), |
6a488035 TO |
471 | 'target_contact_id' => $client, |
472 | 'weight' => $orderVal, | |
473 | ); | |
474 | } | |
475 | ||
476 | //parsing date to default preference format | |
477 | $params['activity_date_time'] = CRM_Utils_Date::processDate($params['activity_date_time']); | |
478 | ||
479 | if ($activityTypeName == 'Open Case') { | |
480 | // we don't set activity_date_time for auto generated | |
481 | // activities, but we want it to be set for open case. | |
482 | $activityParams['activity_date_time'] = $params['activity_date_time']; | |
483 | if (array_key_exists('custom', $params) && is_array($params['custom'])) { | |
484 | $activityParams['custom'] = $params['custom']; | |
485 | } | |
486 | ||
487 | // Add parameters for attachments | |
488 | ||
aaffa79f | 489 | $numAttachments = Civi::settings()->get('max_attachments'); |
481a74f4 | 490 | for ($i = 1; $i <= $numAttachments; $i++) { |
6a488035 | 491 | $attachName = "attachFile_$i"; |
481a74f4 | 492 | if (isset($params[$attachName]) && !empty($params[$attachName])) { |
6a488035 TO |
493 | $activityParams[$attachName] = $params[$attachName]; |
494 | } | |
495 | } | |
496 | } | |
497 | else { | |
498 | $activityDate = NULL; | |
499 | //get date of reference activity if set. | |
500 | if ($referenceActivityName = (string) $activityTypeXML->reference_activity) { | |
501 | ||
502 | //we skip open case as reference activity.CRM-4374. | |
a7488080 | 503 | if (!empty($params['resetTimeline']) && $referenceActivityName == 'Open Case') { |
6a488035 TO |
504 | $activityDate = $params['activity_date_time']; |
505 | } | |
506 | else { | |
507 | $referenceActivityInfo = CRM_Utils_Array::value($referenceActivityName, $activityTypes); | |
508 | if ($referenceActivityInfo['id']) { | |
509 | $caseActivityParams = array('activity_type_id' => $referenceActivityInfo['id']); | |
510 | ||
511 | //if reference_select is set take according activity. | |
512 | if ($referenceSelect = (string) $activityTypeXML->reference_select) { | |
513 | $caseActivityParams[$referenceSelect] = 1; | |
514 | } | |
515 | ||
516 | $referenceActivity = CRM_Case_BAO_Case::getCaseActivityDates($params['caseID'], $caseActivityParams, TRUE); | |
517 | ||
518 | if (is_array($referenceActivity)) { | |
519 | foreach ($referenceActivity as $aId => $details) { | |
520 | $activityDate = CRM_Utils_Array::value('activity_date', $details); | |
521 | break; | |
522 | } | |
523 | } | |
524 | } | |
525 | } | |
526 | } | |
527 | if (!$activityDate) { | |
528 | $activityDate = $params['activity_date_time']; | |
529 | } | |
530 | list($activity_date, $activity_time) = CRM_Utils_Date::setDateDefaults($activityDate); | |
531 | $activityDateTime = CRM_Utils_Date::processDate($activity_date, $activity_time); | |
532 | //add reference offset to date. | |
533 | if ((int) $activityTypeXML->reference_offset) { | |
534 | $activityDateTime = CRM_Utils_Date::intervalAdd('day', (int) $activityTypeXML->reference_offset, | |
535 | $activityDateTime | |
536 | ); | |
537 | } | |
538 | ||
539 | $activityParams['activity_date_time'] = CRM_Utils_Date::format($activityDateTime); | |
540 | } | |
541 | ||
542 | // if same activity is already there, skip and dont touch | |
543 | $params['activityTypeID'] = $activityTypeID; | |
544 | $params['activityTypeName'] = $activityTypeName; | |
545 | if ($this->isActivityPresent($params)) { | |
546 | return TRUE; | |
547 | } | |
548 | $activityParams['case_id'] = $params['caseID']; | |
a7488080 | 549 | if (!empty($activityParams['is_auto'])) { |
6a488035 TO |
550 | $activityParams['skipRecentView'] = TRUE; |
551 | } | |
552 | ||
d66c61b6 | 553 | // @todo - switch to using api & remove the parameter pre-wrangling above. |
6a488035 TO |
554 | $activity = CRM_Activity_BAO_Activity::create($activityParams); |
555 | ||
556 | if (!$activity) { | |
557 | CRM_Core_Error::fatal(); | |
558 | return FALSE; | |
559 | } | |
560 | ||
561 | // create case activity record | |
562 | $caseParams = array( | |
563 | 'activity_id' => $activity->id, | |
564 | 'case_id' => $params['caseID'], | |
565 | ); | |
566 | CRM_Case_BAO_Case::processCaseActivity($caseParams); | |
567 | return TRUE; | |
568 | } | |
569 | ||
4c6ce474 EM |
570 | /** |
571 | * @param $activitySetsXML | |
572 | * | |
573 | * @return array | |
574 | */ | |
00be9182 | 575 | public static function activitySets($activitySetsXML) { |
6a488035 TO |
576 | $result = array(); |
577 | foreach ($activitySetsXML as $activitySetXML) { | |
578 | foreach ($activitySetXML as $recordXML) { | |
579 | $activitySetName = (string ) $recordXML->name; | |
580 | $activitySetLabel = (string ) $recordXML->label; | |
581 | $result[$activitySetName] = $activitySetLabel; | |
582 | } | |
583 | } | |
584 | ||
585 | return $result; | |
586 | } | |
587 | ||
4c6ce474 EM |
588 | /** |
589 | * @param $caseType | |
590 | * @param null $activityTypeName | |
591 | * | |
592 | * @return array|bool|mixed | |
593 | * @throws Exception | |
594 | */ | |
00be9182 | 595 | public function getMaxInstance($caseType, $activityTypeName = NULL) { |
6a488035 TO |
596 | $xml = $this->retrieve($caseType); |
597 | ||
598 | if ($xml === FALSE) { | |
599 | CRM_Core_Error::fatal(); | |
600 | return FALSE; | |
601 | } | |
602 | ||
603 | $activityInstances = $this->activityTypes($xml->ActivityTypes, TRUE); | |
604 | return $activityTypeName ? CRM_Utils_Array::value($activityTypeName, $activityInstances) : $activityInstances; | |
605 | } | |
606 | ||
4c6ce474 EM |
607 | /** |
608 | * @param $caseType | |
609 | * | |
610 | * @return array|mixed | |
611 | */ | |
00be9182 | 612 | public function getCaseManagerRoleId($caseType) { |
6a488035 TO |
613 | $xml = $this->retrieve($caseType); |
614 | return $this->caseRoles($xml->CaseRoles, TRUE); | |
615 | } | |
616 | ||
708d8fa2 TO |
617 | /** |
618 | * @param string $caseType | |
619 | * @return array<\Civi\CCase\CaseChangeListener> | |
620 | */ | |
00be9182 | 621 | public function getListeners($caseType) { |
708d8fa2 TO |
622 | $xml = $this->retrieve($caseType); |
623 | $listeners = array(); | |
624 | if ($xml->Listeners && $xml->Listeners->Listener) { | |
625 | foreach ($xml->Listeners->Listener as $listenerXML) { | |
626 | $class = (string) $listenerXML; | |
627 | $listeners[] = new $class(); | |
628 | } | |
629 | } | |
630 | return $listeners; | |
631 | } | |
632 | ||
4c6ce474 EM |
633 | /** |
634 | * @return int | |
635 | */ | |
00be9182 | 636 | public function getRedactActivityEmail() { |
23d6731b | 637 | return $this->getBoolSetting('civicaseRedactActivityEmail', 'RedactActivityEmail'); |
6a488035 TO |
638 | } |
639 | ||
640 | /** | |
fe482240 | 641 | * Retrieves AllowMultipleCaseClients setting. |
6a488035 | 642 | * |
a6c01b45 CW |
643 | * @return string |
644 | * 1 if allowed, 0 if not | |
6a488035 | 645 | */ |
00be9182 | 646 | public function getAllowMultipleCaseClients() { |
c8fd28dc | 647 | return $this->getBoolSetting('civicaseAllowMultipleClients', 'AllowMultipleCaseClients'); |
6a488035 TO |
648 | } |
649 | ||
650 | /** | |
fe482240 | 651 | * Retrieves NaturalActivityTypeSort setting. |
6a488035 | 652 | * |
a6c01b45 CW |
653 | * @return string |
654 | * 1 if natural, 0 if alphabetic | |
6a488035 | 655 | */ |
00be9182 | 656 | public function getNaturalActivityTypeSort() { |
23d6731b TO |
657 | return $this->getBoolSetting('civicaseNaturalActivityTypeSort', 'NaturalActivityTypeSort'); |
658 | } | |
659 | ||
660 | /** | |
661 | * @param string $settingKey | |
662 | * @param string $xmlTag | |
663 | * @param mixed $default | |
664 | * @return int | |
665 | */ | |
666 | private function getBoolSetting($settingKey, $xmlTag, $default = 0) { | |
667 | $setting = Civi::settings()->get($settingKey); | |
ec61a2b2 | 668 | if ($setting !== 'default') { |
d6742e0f TO |
669 | return (int) $setting; |
670 | } | |
23d6731b TO |
671 | if ($xml = $this->retrieve("Settings")) { |
672 | return (string) $xml->{$xmlTag} ? 1 : 0; | |
673 | } | |
674 | return $default; | |
6a488035 | 675 | } |
96025800 | 676 | |
6a488035 | 677 | } |