/**
* Guess the position of the widget relatively to the positionReference
*
* @param Widget $widget The widget to position
* @param integer $positionReference Id of the parent widget
*
* @return WidgetMap The position of the widget
*/
public function generateWidgetPosition(WidgetMap $widgetMapEntry, $widget, $widgetMap, $positionReference)
{
$position = 1;
$slotId = $widget->getSlot();
if (empty($widgetMap[$slotId])) {
$widgetMapEntry->setPosition($position);
return $widgetMapEntry;
}
$slot = $widgetMap[$slotId];
$referenceWidget = $this->em->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;
}