/** * Guess the position of the widget relatively to the positionReference. * * @param Widget $widget The widget to position * @param int $positionReference Id of the parent widget * * @return WidgetMap The position of the widget */ public function generateWidgetPosition(EntityManager $entityManager, WidgetMap $widgetMapEntry, $widget, $widgetMap, $positionReference) { $position = 1; $slotId = $widget->getSlot(); if (empty($widgetMap[$slotId])) { $widgetMapEntry->setPosition($position); return $widgetMapEntry; } $slot = $widgetMap[$slotId]; $referenceWidget = $entityManager->getRepository('Victoire\\Bundle\\WidgetBundle\\Entity\\Widget')->findOneById($positionReference); //If we added a widget just after a parent widget //The position of the new widget is the one just after the parent widget if ($referenceWidget && $widget->getView() !== $referenceWidget->getView()) { $position = 1; $widgetMapEntry->setPosition($position); $widgetMapEntry->setPositionReference($positionReference); } else { foreach ($slot as $key => $_widgetMap) { if ($_widgetMap->getWidgetId() === (int) $positionReference) { $widgetMapEntry->setPosition($_widgetMap->getPosition() + 1); break; } elseif (0 === (int) $positionReference) { $widgetMapEntry->setPosition(1); } } } return $widgetMapEntry; }
/** * 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; }