public function rebuild(View $view) { $widgetMap = array(); if ($view->getTemplate()) { $widgetMap = $view->getTemplate()->getWidgetMap(); } foreach ($view->getWidgets() as $widget) { if (!isset($widgetMap[$widget->getSlot()])) { $widgetMap[$widget->getSlot()] = array(); } //create the new widget map $widgetMapEntry = new WidgetMap(); $widgetMapEntry->setAction(WidgetMap::ACTION_CREATE); $widgetMapEntry->setWidgetId($widget->getId()); $widgetMapEntry->setAsynchronous($widget->isAsynchronous()); $widgetMapEntry = $this->helper->generateWidgetPosition($widgetMapEntry, $widget, $widgetMap, null); $widgetMap[$widget->getSlot()][] = $widgetMapEntry; } $widgetMapAsArray = array(); foreach ($widgetMap as $slotId => $widgetMapItems) { foreach ($widgetMapItems as $widgetMapItem) { $widgetMapAsArray[$slotId][] = $this->widgetMapTransformer->transform($widgetMapItem); } } $view->setWidgetMap($widgetMapAsArray); }
/** * Transforms an array to an object (WidgetMap). * @param array $widgetMapAsArray * * @return WidgetMap|null * @throws TransformationFailedException if object (issue) is not found. */ public function reverseTransform($widgetMapAsArray) { if (!$widgetMapAsArray) { return null; } $widgetMap = new WidgetMap(); $widgetMap->setAction(@$widgetMapAsArray['action']); $widgetMap->setPosition(@$widgetMapAsArray['position']); $widgetMap->setPositionReference(@$widgetMapAsArray['positionReference']); $widgetMap->setAsynchronous(@$widgetMapAsArray['asynchronous']); $widgetMap->setReplacedWidgetId(@$widgetMapAsArray['replacedWidgetId']); $widgetMap->setWidgetId(intval($widgetMapAsArray['widgetId'])); return $widgetMap; }
/** * create a widgetMap for the new Widget cloned * * @return void **/ public function overwriteWidgetMap(Widget $widgetCopy, Widget $widget, View $view) { //the id of the new widget $widgetId = $widgetCopy->getId(); //the widget slot $widgetSlotId = $widget->getSlot(); //the widget id $replacedWidgetId = $widget->getId(); //get the slot $slot = $view->getSlotById($widgetSlotId); //there might be no slot yet for the child view if ($slot === null) { //create a new slot $slot = new Slot(); $slot->setId($widgetSlotId); //add the new slot to the view $view->addSlot($slot); } $originalWidgetMap = $slot->getWidgetMapByWidgetId($replacedWidgetId); //If widgetmap was not found current view, we dig if (!$originalWidgetMap) { $watchDog = 100; $_view = $view; while (!$originalWidgetMap && $watchDog) { $_view = $_view->getTemplate(); $parentSlot = $_view->getSlotById($widgetSlotId); if ($parentSlot) { $originalWidgetMap = $parentSlot->getWidgetMapByWidgetId($replacedWidgetId); } $watchDog--; } if (0 == $watchDog) { throw new \Exception(sprintf("The slot %s doesn't appears to be in any templates WidgetMaps. You should check this manually.", $widgetSlotId)); } } //the widget is owned by another view (a parent) //so we add a new widget map that indicates we delete this widget $widgetMap = new WidgetMap(); $widgetMap->setAction(WidgetMap::ACTION_OVERWRITE); $widgetMap->setReplacedWidgetId($replacedWidgetId); $widgetMap->setWidgetId($widgetId); $widgetMap->setPosition($originalWidgetMap->getPosition()); $widgetMap->setAsynchronous($widgetCopy->isAsynchronous()); $widgetMap->setPositionReference($originalWidgetMap->getPositionReference()); $slot->addWidgetMap($widgetMap); }
/** * Method called once the entity is loaded. * * @ORM\PostLoad */ public function postLoad() { $widgetMap = $this->widgetMap; //the slots of the page $slots = []; //convert the widget map array as objects foreach ($widgetMap as $slotId => $_widgetMapEntries) { $slot = new Slot(); $slot->setId($slotId); foreach ($_widgetMapEntries as $_widgetMapEntry) { $_widgetMap = new WidgetMap(); $_widgetMap->setAction(@$_widgetMapEntry['action']); $_widgetMap->setPosition(@$_widgetMapEntry['position']); $_widgetMap->setPositionReference(@$_widgetMapEntry['positionReference']); $_widgetMap->setAsynchronous(isset($_widgetMapEntry['asynchronous']) ? $_widgetMapEntry['asynchronous'] : null); $_widgetMap->setReplacedWidgetId(@$_widgetMapEntry['replacedWidgetId']); $_widgetMap->setWidgetId(intval($_widgetMapEntry['widgetId'])); $slot->addWidgetMap($_widgetMap); } $slots[] = $slot; } //set the slots to the page $this->slots = $slots; }