<NME> WidgetMapBuilder.php
<BEF> <?php

namespace Victoire\Bundle\WidgetMapBundle\Builder;

use Victoire\Bundle\CoreBundle\Entity\View;
use Victoire\Bundle\PageBundle\Entity\WidgetMap;
use Victoire\Bundle\WidgetMapBundle\DataTransformer\WidgetMapToArrayTransformer;
use Victoire\Bundle\WidgetMapBundle\Helper\WidgetMapHelper;

/**
 * View WidgetMap builder.
class WidgetMapBuilder
{
    private $contextualViewWarmer;
    private $resolver;
{
    protected $helper;
    protected $widgetMapTransformer;

    /**
     * Constructor.
     *
     * @param WidgetMapHelper $helper Widget map helper
     */
    public function __construct(WidgetMapHelper $helper, WidgetMapToArrayTransformer $widgetMapTransformer)
    {
        $this->helper = $helper;
        $this->widgetMapTransformer = $widgetMapTransformer;
    }

    public function rebuild(View $view)
    {
        $builtWidgetMap = [];

        $widgetMaps = $this->contextualViewWarmer->warm($view);

        $slots = $this->removeOverwritedWidgetMaps($widgetMaps);

        $this->removeDeletedWidgetMaps($slots);

        foreach ($slots as $slot => $slotWidgetMaps) {
            $mainWidgetMap = null;
            $builtWidgetMap[$slot] = [];

            $rootWidgetMap = $this->findRootWidgetMap($slotWidgetMaps);

            if ($rootWidgetMap) {
                $builtWidgetMap[$slot][] = $rootWidgetMap;
                $builtWidgetMap = $this->orderizeWidgetMap($rootWidgetMap, $builtWidgetMap, $slot, $slotWidgetMaps, $view);
            }
        }

        if ($updatePage) {
            $view->setBuiltWidgetMap($builtWidgetMap);
        }

        $view->setWidgetMap($widgetMapAsArray);
    }

    public function build(View $view, $updatePage = true)
    {
        $viewWidgetMaps = null;
        $widgetMap = [];
     * @param View $view
     *
     * @return array
     */
    public function getAvailablePosition(View $view)
    {
        $widgetMaps = $view->getBuiltWidgetMap();

        $availablePositions = [];
        foreach ($widgetMaps as $slot => $widgetMap) {
            foreach ($widgetMap as $_widgetMap) {
                $availablePositions[$slot][$_widgetMap->getId()]['id'] = $_widgetMap->getId();
                $availablePositions[$slot][$_widgetMap->getId()][WidgetMap::POSITION_BEFORE] = true;
                $availablePositions[$slot][$_widgetMap->getId()][WidgetMap::POSITION_AFTER] = true;
                if ($_widgetMap->getReplaced()) {
                    $availablePositions[$slot][$_widgetMap->getId()]['replaced'] = $_widgetMap->getReplaced()->getId();
                }
            }
            /** @var WidgetMap $_widgetMap */
            foreach ($widgetMap as $_widgetMap) {
                if ($_widgetMap->getParent()) {
                    if ($substitute = $_widgetMap->getParent()->getSubstituteForView($view)) {
                        $availablePositions[$slot][$substitute->getId()][$_widgetMap->getPosition()] = false;
                    } else {
                        $availablePositions[$slot][$_widgetMap->getParent()->getId()][$_widgetMap->getPosition()] = false;
                    }
                }
            }
        }

        return $availablePositions;
    }

    /**
     * Get the children of given WidgetMap and place them recursively in the "builtWidgetMap" array at the right place
     * depending of the children parents and positions.
     *
     * @param WidgetMap $currentWidgetMap
     * @param $builtWidgetMap
     * @param $slot
     * @param $slotWidgetMaps
     * @param View $view
     *
     * @return mixed
     */
    protected function orderizeWidgetMap(WidgetMap $currentWidgetMap, $builtWidgetMap, $slot, $slotWidgetMaps, View $view)
    {
        $children = $this->resolver->getChildren($currentWidgetMap, $view);
        foreach ($children as $child) {
            // check if the founded child belongs to the view
            if (in_array($child, $slotWidgetMaps, true)) {
                // Find the position of the "currentWidgetMap" inside the builtWidgetMap,
                // add "1" to this position if wanted position is "after", 0 is it's before.
                $offset = array_search($currentWidgetMap, $builtWidgetMap[$slot]) + ($child->getPosition() == WidgetMap::POSITION_AFTER ? 1 : 0);
                // insert the child in builtWidgetMap at offset position
                array_splice($builtWidgetMap[$slot], $offset, 0, [$child]);
                // call myself with child
                $builtWidgetMap = $this->orderizeWidgetMap($child, $builtWidgetMap, $slot, $slotWidgetMaps, $view);
            }
        }

        return $builtWidgetMap;
    }

    /**
     * Create a $slot array that'll contain, for each slot, a widgetmap id as key and a widgetmap as value
     * Do not keeps widgetMaps that are overwrited.
     *
     * @param $widgetMaps
     *
     * @return array
     */
    protected function removeOverwritedWidgetMaps($widgetMaps)
    {
        $slots = [];
        /** @var WidgetMap $widgetMapItem */
        foreach ($widgetMaps as $widgetMapItem) {
            $id = $widgetMapItem->getId();
            // If the widgetmap replace one (has a "replaced"), use the replaced id as key
            if ($widgetMapItem->getReplaced()) {
                $id = $widgetMapItem->getReplaced()->getId();
            }
            // If "id" is not present in slot array or
            if (empty($slots[$widgetMapItem->getSlot()][$id])
                // or if the id exists AND the inserted widgetmap is overwrite|delete, then erase the initial widget by it
                || !empty($slots[$widgetMapItem->getSlot()][$id])
                && $widgetMapItem->getAction() !== WidgetMap::ACTION_CREATE) {
                $slots[$widgetMapItem->getSlot()][$id] = $widgetMapItem;
            }
        }
            $view->setBuiltWidgetMap($widgetMap);
        }

        return $widgetMap;
    }
}
                if ($widgetMap->getAction() == WidgetMap::ACTION_DELETE) {
                    unset($slots[$slot][$key]);
                }
            }
        }
    }

    /**
     * Find the "root" widgetmap (the one that has no parent).
     *
     * @param $widgetMaps
     *
     * @return WidgetMap|null
     */
    private function findRootWidgetMap($widgetMaps)
    {
        $rootWidgetMap = null;
        foreach ($widgetMaps as $_widgetMap) {
            if (!$_widgetMap->getParent()) {
                $rootWidgetMap = $_widgetMap;
                break;
            }
        }

        return $rootWidgetMap;
    }
}

<MSG> Call WidgetDataWarmer at the end of WidgetMapBuilder build method if loading parameter is true

<DFF> @@ -2,10 +2,13 @@
 
 namespace Victoire\Bundle\WidgetMapBundle\Builder;
 
+use Doctrine\ORM\EntityManager;
 use Victoire\Bundle\CoreBundle\Entity\View;
+use Victoire\Bundle\WidgetBundle\Entity\Widget;
 use Victoire\Bundle\PageBundle\Entity\WidgetMap;
 use Victoire\Bundle\WidgetMapBundle\DataTransformer\WidgetMapToArrayTransformer;
 use Victoire\Bundle\WidgetMapBundle\Helper\WidgetMapHelper;
+use Victoire\Bundle\WidgetMapBundle\Warmer\WidgetDataWarmer;
 
 /**
  * View WidgetMap builder.
@@ -16,16 +19,24 @@ class WidgetMapBuilder
 {
     protected $helper;
     protected $widgetMapTransformer;
+    protected $widgetDataWarmer;
+    protected $em;
 
     /**
      * Constructor.
      *
      * @param WidgetMapHelper $helper Widget map helper
      */
-    public function __construct(WidgetMapHelper $helper, WidgetMapToArrayTransformer $widgetMapTransformer)
-    {
+    public function __construct(
+        WidgetMapHelper $helper,
+        WidgetMapToArrayTransformer $widgetMapTransformer,
+        WidgetDataWarmer $widgetDataWarmer,
+        EntityManager $entityManager
+    ) {
         $this->helper = $helper;
         $this->widgetMapTransformer = $widgetMapTransformer;
+        $this->widgetDataWarmer = $widgetDataWarmer;
+        $this->em = $entityManager;
     }
 
     public function rebuild(View $view)
@@ -57,7 +68,7 @@ class WidgetMapBuilder
         $view->setWidgetMap($widgetMapAsArray);
     }
 
-    public function build(View $view, $updatePage = true)
+    public function build(View $view, $updatePage = true, $loadWidgets = false)
     {
         $viewWidgetMaps = null;
         $widgetMap = [];
@@ -154,6 +165,13 @@ class WidgetMapBuilder
             $view->setBuiltWidgetMap($widgetMap);
         }
 
+        //Populate widgets with their data if needed
+        if($loadWidgets) {
+            $widgetRepo = $this->em->getRepository('Victoire\Bundle\WidgetBundle\Entity\Widget');
+            $viewWidgets = $widgetRepo->findAllWidgetsForView($view);
+            $this->widgetDataWarmer->warm($view, $viewWidgets);
+        }
+
         return $widgetMap;
     }
 }

{'addition_count': 21, 'commit_subject': 'Call WidgetDataWarmer at the end of WidgetMapBuilder build method if loading parameter is true', 'deletion_count': 3, 'file_extension': '.php', 'lang': 'php', 'license': 'mit', 'repo_name': 'Victoire/victoire'}