Пример #1
0
 /**
  * @param Campaign $entity
  * @param          $sessionEvents
  * @param          $sessionConnections
  * @param          $deletedEvents
  *
  * @return array
  */
 public function setEvents(Campaign &$entity, $sessionEvents, $sessionConnections, $deletedEvents)
 {
     $existingEvents = $entity->getEvents();
     $events = $hierarchy = $parentUpdated = array();
     //set the events from session
     foreach ($sessionEvents as $id => $properties) {
         $isNew = !empty($properties['id']) && isset($existingEvents[$properties['id']]) ? false : true;
         $event = !$isNew ? $existingEvents[$properties['id']] : new Event();
         foreach ($properties as $f => $v) {
             if ($f == 'id' && strpos($v, 'new') === 0) {
                 //set the temp ID used to be able to match up connections
                 $event->setTempId($v);
             }
             if (in_array($f, array('id', 'order', 'parent'))) {
                 continue;
             }
             $func = "set" . ucfirst($f);
             if (method_exists($event, $func)) {
                 $event->{$func}($v);
             }
         }
         $event->setCampaign($entity);
         $events[$id] = $event;
     }
     foreach ($deletedEvents as $deleteMe) {
         if (isset($existingEvents[$deleteMe])) {
             // Remove child from parent
             $parent = $existingEvents[$deleteMe]->getParent();
             if ($parent) {
                 $parent->removeChild($existingEvents[$deleteMe]);
                 $existingEvents[$deleteMe]->removeParent();
             }
             $entity->removeEvent($existingEvents[$deleteMe]);
             unset($events[$deleteMe]);
         }
     }
     $relationships = array();
     if (isset($sessionConnections['connections'])) {
         foreach ($sessionConnections['connections'] as $connection) {
             $source = $connection['sourceId'];
             $target = $connection['targetId'];
             $sourceDecision = !empty($connection['anchors']) ? $connection['anchors'][0]['endpoint'] : null;
             if ($sourceDecision == 'leadsource') {
                 // Lead source connection that does not matter
                 continue;
             }
             $relationships[$target] = array('parent' => $source, 'decision' => $sourceDecision);
         }
     }
     // Assign parent/child relationships
     foreach ($events as $id => $e) {
         if (isset($relationships[$id])) {
             // Has a parent
             $anchor = in_array($relationships[$id]['decision'], array('yes', 'no')) ? $relationships[$id]['decision'] : null;
             $events[$id]->setDecisionPath($anchor);
             $parentId = $relationships[$id]['parent'];
             $events[$id]->setParent($events[$parentId]);
             $hierarchy[$id] = $parentId;
         } elseif ($events[$id]->getParent()) {
             // No longer has a parent so null it out
             // Remove decision so that it doesn't affect execution
             $events[$id]->setDecisionPath(null);
             // Remove child from parent
             $parent = $events[$id]->getParent();
             $parent->removeChild($events[$id]);
             // Remove parent from child
             $events[$id]->removeParent();
             $hierarchy[$id] = 'null';
         } else {
             // Is a parent
             $hierarchy[$id] = 'null';
             // Remove decision so that it doesn't affect execution
             $events[$id]->setDecisionPath(null);
         }
     }
     //set event order used when querying the events
     $this->buildOrder($hierarchy, $events, $entity);
     uasort($events, function ($a, $b) {
         $aOrder = $a->getOrder();
         $bOrder = $b->getOrder();
         if ($aOrder == $bOrder) {
             return 0;
         }
         return $aOrder < $bOrder ? -1 : 1;
     });
     return $events;
 }
 /**
  * {@inheritDoc}
  */
 public function getEvents()
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'getEvents', array());
     return parent::getEvents();
 }