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.
9 class SequenceListener
implements CaseChangeListener
{
12 * @var SequenceListener
14 private static $singleton;
18 * Whether to forcibly rebuild the entire container.
19 * @return \Symfony\Component\DependencyInjection\TaggedContainerInterface
21 public static function singleton($reset = FALSE) {
22 if ($reset || self
::$singleton === NULL) {
23 self
::$singleton = new SequenceListener();
25 return self
::$singleton;
28 public static function onCaseChange_static(\Civi\CCase\Event\CaseChangeEvent
$event) {
29 self
::singleton()->onCaseChange($event);
32 public function onCaseChange(\Civi\CCase\Event\CaseChangeEvent
$event) {
33 /** @var \Civi\CCase\Analyzer $analyzer */
34 $analyzer = $event->analyzer
;
36 $activitySetXML = $this->getSequenceXml($analyzer->getXml());
37 if (!$activitySetXML) {
41 $actTypes = array_flip(\CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'name'));
42 $actStatuses = array_flip(\CRM_Core_PseudoConstant
::activityStatus('name'));
44 $actIndex = $analyzer->getActivityIndex(array('activity_type_id', 'status_id'));
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);
53 elseif (empty($actIndex[$actTypeId][$actStatuses['Completed']])) {
54 // Haven't gotten past this step yet!
59 // OK, the activity has completed every step in the sequence!
60 civicrm_api3('Case', 'create', array(
61 'id' => $analyzer->getCaseId(),
62 'status_id' => 'Closed',
68 * Find the ActivitySet which defines the pipeline.
70 * @param \SimpleXMLElement $xml
71 * @return \SimpleXMLElement|NULL
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;
91 * @param Analyzer $analyzer
92 * The case being analyzed -- to which we want to add an activity.
93 * @param \SimpleXMLElement $actXML the <ActivityType> tag which describes the new activity
95 public function createActivity(Analyzer
$analyzer, \SimpleXMLElement
$actXML) {
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(),
102 $r = civicrm_api3('Activity', 'create', $params);