Commit | Line | Data |
---|---|---|
b019b130 TO |
1 | <?php |
2 | namespace Civi\CCase; | |
3 | ||
4 | /** | |
5 | * The sequence-listener looks for CiviCase XML tags with "<sequence>". If | |
6 | * a change is made to any record in case-type which uses "<sequence>", then | |
7 | * it attempts to add the next step in the sequence. | |
8 | */ | |
9 | class SequenceListener implements CaseChangeListener { | |
10 | ||
11 | /** | |
12 | * @var SequenceListener | |
13 | */ | |
14 | private static $singleton; | |
15 | ||
16 | /** | |
04855556 TO |
17 | * @param bool $reset |
18 | * Whether to forcibly rebuild the entire container. | |
45322593 | 19 | * @return SequenceListener |
b019b130 TO |
20 | */ |
21 | public static function singleton($reset = FALSE) { | |
22 | if ($reset || self::$singleton === NULL) { | |
23 | self::$singleton = new SequenceListener(); | |
24 | } | |
25 | return self::$singleton; | |
26 | } | |
27 | ||
7fe37828 EM |
28 | /** |
29 | * @param \Civi\CCase\Event\CaseChangeEvent $event | |
30 | */ | |
b019b130 TO |
31 | public static function onCaseChange_static(\Civi\CCase\Event\CaseChangeEvent $event) { |
32 | self::singleton()->onCaseChange($event); | |
33 | } | |
34 | ||
7fe37828 EM |
35 | /** |
36 | * @param \Civi\CCase\Event\CaseChangeEvent $event | |
37 | * | |
38 | * @throws \CiviCRM_API3_Exception | |
45322593 | 39 | * @return void |
7fe37828 | 40 | */ |
b019b130 TO |
41 | public function onCaseChange(\Civi\CCase\Event\CaseChangeEvent $event) { |
42 | /** @var \Civi\CCase\Analyzer $analyzer */ | |
43 | $analyzer = $event->analyzer; | |
44 | ||
b019b130 TO |
45 | $activitySetXML = $this->getSequenceXml($analyzer->getXml()); |
46 | if (!$activitySetXML) { | |
47 | return; | |
48 | } | |
49 | ||
50 | $actTypes = array_flip(\CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'name')); | |
51 | $actStatuses = array_flip(\CRM_Core_PseudoConstant::activityStatus('name')); | |
52 | ||
53 | $actIndex = $analyzer->getActivityIndex(array('activity_type_id', 'status_id')); | |
54 | ||
55 | foreach ($activitySetXML->ActivityTypes->ActivityType as $actTypeXML) { | |
56 | $actTypeId = $actTypes[(string) $actTypeXML->name]; | |
57 | if (empty($actIndex[$actTypeId])) { | |
58 | // Haven't tried this step yet! | |
59 | $this->createActivity($analyzer, $actTypeXML); | |
b019b130 TO |
60 | return; |
61 | } | |
62 | elseif (empty($actIndex[$actTypeId][$actStatuses['Completed']])) { | |
63 | // Haven't gotten past this step yet! | |
b019b130 TO |
64 | return; |
65 | } | |
66 | } | |
67 | ||
674829bf DA |
68 | //CRM-17452 - Close the case only if all the activities are complete |
69 | $activities = $analyzer->getActivities(); | |
70 | foreach ($activities as $activity) { | |
71 | if ($activity['status_id'] != $actStatuses['Completed']) { | |
72 | return; | |
73 | } | |
74 | } | |
75 | ||
76 | // OK, the all activities have completed | |
b019b130 | 77 | civicrm_api3('Case', 'create', array( |
46bcf597 | 78 | 'id' => $analyzer->getCaseId(), |
b019b130 TO |
79 | 'status_id' => 'Closed', |
80 | )); | |
81 | $analyzer->flush(); | |
b019b130 TO |
82 | } |
83 | ||
84 | /** | |
85 | * Find the ActivitySet which defines the pipeline. | |
86 | * | |
87 | * @param \SimpleXMLElement $xml | |
88 | * @return \SimpleXMLElement|NULL | |
89 | */ | |
90 | public function getSequenceXml($xml) { | |
91 | if ($xml->ActivitySets && $xml->ActivitySets->ActivitySet) { | |
92 | foreach ($xml->ActivitySets->ActivitySet as $activitySetXML) { | |
93 | $seq = (string) $activitySetXML->sequence; | |
94 | if ($seq && strtolower($seq) == 'true') { | |
95 | if ($activitySetXML->ActivityTypes && $activitySetXML->ActivityTypes->ActivityType) { | |
96 | return $activitySetXML; | |
97 | } | |
98 | else { | |
99 | return NULL; | |
100 | } | |
101 | } | |
102 | } | |
103 | } | |
104 | return NULL; | |
105 | } | |
106 | ||
107 | /** | |
04855556 TO |
108 | * @param Analyzer $analyzer |
109 | * The case being analyzed -- to which we want to add an activity. | |
b019b130 TO |
110 | * @param \SimpleXMLElement $actXML the <ActivityType> tag which describes the new activity |
111 | */ | |
112 | public function createActivity(Analyzer $analyzer, \SimpleXMLElement $actXML) { | |
113 | $params = array( | |
114 | 'activity_type_id' => (string) $actXML->name, | |
115 | 'status_id' => 'Scheduled', | |
116 | 'activity_date_time' => \CRM_Utils_Time::getTime('YmdHis'), | |
117 | 'case_id' => $analyzer->getCaseId(), | |
118 | ); | |
119 | $r = civicrm_api3('Activity', 'create', $params); | |
120 | $analyzer->flush(); | |
121 | } | |
96025800 | 122 | |
ef10e0b5 | 123 | } |