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