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