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. | |
b019b130 TO |
19 | * @return \Symfony\Component\DependencyInjection\TaggedContainerInterface |
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 | ||
28 | public static function onCaseChange_static(\Civi\CCase\Event\CaseChangeEvent $event) { | |
29 | self::singleton()->onCaseChange($event); | |
30 | } | |
31 | ||
b019b130 TO |
32 | public function onCaseChange(\Civi\CCase\Event\CaseChangeEvent $event) { |
33 | /** @var \Civi\CCase\Analyzer $analyzer */ | |
34 | $analyzer = $event->analyzer; | |
35 | ||
b019b130 TO |
36 | $activitySetXML = $this->getSequenceXml($analyzer->getXml()); |
37 | if (!$activitySetXML) { | |
38 | return; | |
39 | } | |
40 | ||
41 | $actTypes = array_flip(\CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'name')); | |
42 | $actStatuses = array_flip(\CRM_Core_PseudoConstant::activityStatus('name')); | |
43 | ||
44 | $actIndex = $analyzer->getActivityIndex(array('activity_type_id', 'status_id')); | |
45 | ||
46 | foreach ($activitySetXML->ActivityTypes->ActivityType as $actTypeXML) { | |
47 | $actTypeId = $actTypes[(string) $actTypeXML->name]; | |
48 | if (empty($actIndex[$actTypeId])) { | |
49 | // Haven't tried this step yet! | |
50 | $this->createActivity($analyzer, $actTypeXML); | |
b019b130 TO |
51 | return; |
52 | } | |
53 | elseif (empty($actIndex[$actTypeId][$actStatuses['Completed']])) { | |
54 | // Haven't gotten past this step yet! | |
b019b130 TO |
55 | return; |
56 | } | |
57 | } | |
58 | ||
59 | // OK, the activity has completed every step in the sequence! | |
60 | civicrm_api3('Case', 'create', array( | |
46bcf597 | 61 | 'id' => $analyzer->getCaseId(), |
b019b130 TO |
62 | 'status_id' => 'Closed', |
63 | )); | |
64 | $analyzer->flush(); | |
b019b130 TO |
65 | } |
66 | ||
67 | /** | |
68 | * Find the ActivitySet which defines the pipeline. | |
69 | * | |
70 | * @param \SimpleXMLElement $xml | |
71 | * @return \SimpleXMLElement|NULL | |
72 | */ | |
73 | public function getSequenceXml($xml) { | |
74 | if ($xml->ActivitySets && $xml->ActivitySets->ActivitySet) { | |
75 | foreach ($xml->ActivitySets->ActivitySet as $activitySetXML) { | |
76 | $seq = (string) $activitySetXML->sequence; | |
77 | if ($seq && strtolower($seq) == 'true') { | |
78 | if ($activitySetXML->ActivityTypes && $activitySetXML->ActivityTypes->ActivityType) { | |
79 | return $activitySetXML; | |
80 | } | |
81 | else { | |
82 | return NULL; | |
83 | } | |
84 | } | |
85 | } | |
86 | } | |
87 | return NULL; | |
88 | } | |
89 | ||
90 | /** | |
04855556 TO |
91 | * @param Analyzer $analyzer |
92 | * The case being analyzed -- to which we want to add an activity. | |
b019b130 TO |
93 | * @param \SimpleXMLElement $actXML the <ActivityType> tag which describes the new activity |
94 | */ | |
95 | public function createActivity(Analyzer $analyzer, \SimpleXMLElement $actXML) { | |
96 | $params = array( | |
97 | 'activity_type_id' => (string) $actXML->name, | |
98 | 'status_id' => 'Scheduled', | |
99 | 'activity_date_time' => \CRM_Utils_Time::getTime('YmdHis'), | |
100 | 'case_id' => $analyzer->getCaseId(), | |
101 | ); | |
102 | $r = civicrm_api3('Activity', 'create', $params); | |
103 | $analyzer->flush(); | |
104 | } | |
ef10e0b5 | 105 | } |