<commit_before><?php

namespace Victoire\Bundle\WidgetMapBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Victoire\Bundle\CoreBundle\Entity\View;
use Victoire\Bundle\TemplateBundle\Entity\Template;
use Victoire\Bundle\WidgetBundle\Entity\Widget;
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap;

class WidgetMapMigrationCommand extends ContainerAwareCommand
{
    /**
     * {@inheritdoc}
     */
    public function configure()
    {
        parent::configure();

        $this
            ->setName('victoire:widget-map:migrate')
            ->addOption('view', null, InputArgument::OPTIONAL, 'view id', null)
            ->setDescription('persists widget map as a tree ofWidgetMap objects');
    }

    /**
     *
     * Takes each view widgetmap array and convert it to persisted WidgetMaps
     *
     * sort widget in inversed order to generate a reversed tree like:
     *       4
     *     3 ┴
     *   2 ┴
     * 1 ┴
     *
     * Then add the overwrited widgetmaps as:
     *       4
     *     3 ┴ 7
     *   2 ┴ 5
     * 1 ┴   ┴ 6
     *         ┴ 8
     *
     * @param InputInterface  $input
     * @param OutputInterface $output
     *
     * @return void
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $em = $this->getContainer()->get('doctrine.orm.entity_manager');

        $widgetRepo = $em->getRepository('VictoireWidgetBundle:Widget');
        $widgetMapRepo = $em->getRepository('VictoireWidgetMapBundle:WidgetMap');

        if ($viewId = $input->getOption('view')) {
            $views = [$em->getRepository('VictoireCoreBundle:View')->find($viewId)];
        } else {

        $templateRepo = $em->getRepository('VictoireTemplateBundle:Template');
        $rootTemplates = $templateRepo->getInstance()
            ->where('template.template IS NULL')
            ->getQuery()
            ->getResult();
        $templates = [];
        $recursiveGetTemplates = function ($template) use (&$recursiveGetTemplates, &$templates) {
                array_push($templates, $template);
                foreach ($template->getInheritors() as $template) {
                    if ($template instanceof Template) {
                        $recursiveGetTemplates($template);
                    }
                }
        };

        foreach ($rootTemplates as $rootTemplate) {
            $recursiveGetTemplates($rootTemplate);
        }

            $viewRepo = $em->getRepository('VictoirePageBundle:BasePage');
        $views = $viewRepo->findAll();

            $views = array_merge($templates, $views);
        }

        /** @var View $view */
        foreach ($views as $view) {
            $this->getContainer()->get('victoire_widget_map.builder')->build($view);
            $widgets = [];
            foreach ($view->getWidgets() as $widget) {
                $widgets[$widget->getId()] = $widget;
            }

            $oldWidgetMaps = $view->getWidgetMap();
            if (!empty($oldWidgetMaps)) {

                foreach ($oldWidgetMaps as $slot => $oldWidgetMap) {
                    $widgetMaps = [];
                    var_dump($oldWidgetMap);
                    usort($oldWidgetMap, function ($a, $b) {
                        if ($b['action'] != $a['action']) {
                            return 0;
                        }

                        return $b['position'] - $a['position'];
                    });
                    var_dump($oldWidgetMap);

                    usort($oldWidgetMap, function ($a, $b) {

                        if ($b['action'] != WidgetMap::ACTION_CREATE) {
                            return -1;
                        }
                        return 1;
                    });
                    usort($oldWidgetMap, function ($a, $b) {

                        if ($b['positionReference'] != null && $b['positionReference'] == $a['widgetId']) {
                            return -1;
                        }
                        return 1;
                    });

                    var_dump($oldWidgetMap);
                    foreach ($oldWidgetMap as $position => $_oldWidgetMap) {
                        var_dump("==========================");
                        var_dump($slot);
                        var_dump($_oldWidgetMap);

                        $widget = $widgetRepo->find($_oldWidgetMap['widgetId']);
                        if (!$widget) {
                            var_dump('widget does not exists');
                            continue;
                        }
                        $widgetMap = new WidgetMap();
                        $referencedWidgetMap = null;
                        var_dump('widget id:');
                        var_dump($_oldWidgetMap['widgetId']);
                        if ($_oldWidgetMap['positionReference']) {
                            var_dump($_oldWidgetMap['positionReference']);
                            $referencedWidget = $widgetRepo->find($_oldWidgetMap['positionReference']);
                            var_dump($referencedWidget->getId());
                            $referencedWidgetMap = $view->getWidgetMapByWidget($referencedWidget);
                            while ($referencedWidgetMap->getChild(WidgetMap::POSITION_AFTER)) {
                                $referencedWidgetMap = $referencedWidgetMap->getChild(WidgetMap::POSITION_AFTER);
                            }
                            var_dump('set parent'.$referencedWidgetMap->getWidget()->getId());
                                $widgetMap->setParent($referencedWidgetMap);
                                $widgetMap->setPosition(WidgetMap::POSITION_AFTER);
                        } else {
                            var_dump('has no positionReference');
                            if ($position == 0) {
                                $widgetMap->setPosition(null);
                                var_dump('set parent'.null);
                                $widgetMap->setParent(null);
                            } else {
                                $widgetMap->setPosition(WidgetMap::POSITION_BEFORE);
                                if (!empty(array_slice($widgetMaps, -1))) {
                                $widgetMap->setParent(array_slice($widgetMaps, -1)[0]);
                            }
                        }
                        }

                        if (WidgetMap::ACTION_OVERWRITE == $_oldWidgetMap['action']) {
                            var_dump('is overwrite');

                            /** @var Widget $replacedWidget */
                            if ($_oldWidgetMap['replacedWidgetId']) {<commit_msg>check for positionReference both != 0 and != null<commit_after><?php

namespace Victoire\Bundle\WidgetMapBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Victoire\Bundle\CoreBundle\Entity\View;
use Victoire\Bundle\TemplateBundle\Entity\Template;
use Victoire\Bundle\WidgetBundle\Entity\Widget;
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap;

class WidgetMapMigrationCommand extends ContainerAwareCommand
{
    /**
     * {@inheritdoc}
     */
    public function configure()
    {
        parent::configure();

        $this
            ->setName('victoire:widget-map:migrate')
            ->addOption('view', null, InputArgument::OPTIONAL, 'view id', null)
            ->setDescription('persists widget map as a tree ofWidgetMap objects');
    }

    /**
     *
     * Takes each view widgetmap array and convert it to persisted WidgetMaps
     *
     * sort widget in inversed order to generate a reversed tree like:
     *       4
     *     3 ┴
     *   2 ┴
     * 1 ┴
     *
     * Then add the overwrited widgetmaps as:
     *       4
     *     3 ┴ 7
     *   2 ┴ 5
     * 1 ┴   ┴ 6
     *         ┴ 8
     *
     * @param InputInterface  $input
     * @param OutputInterface $output
     *
     * @return void
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $em = $this->getContainer()->get('doctrine.orm.entity_manager');

        $widgetRepo = $em->getRepository('VictoireWidgetBundle:Widget');
        $widgetMapRepo = $em->getRepository('VictoireWidgetMapBundle:WidgetMap');

        if ($viewId = $input->getOption('view')) {
            $views = [$em->getRepository('VictoireCoreBundle:View')->find($viewId)];
        } else {

        $templateRepo = $em->getRepository('VictoireTemplateBundle:Template');
        $rootTemplates = $templateRepo->getInstance()
            ->where('template.template IS NULL')
            ->getQuery()
            ->getResult();
        $templates = [];
        $recursiveGetTemplates = function ($template) use (&$recursiveGetTemplates, &$templates) {
                array_push($templates, $template);
                foreach ($template->getInheritors() as $template) {
                    if ($template instanceof Template) {
                        $recursiveGetTemplates($template);
                    }
                }
        };

        foreach ($rootTemplates as $rootTemplate) {
            $recursiveGetTemplates($rootTemplate);
        }

            $viewRepo = $em->getRepository('VictoirePageBundle:BasePage');
        $views = $viewRepo->findAll();

            $views = array_merge($templates, $views);
        }

        /** @var View $view */
        foreach ($views as $view) {
            $this->getContainer()->get('victoire_widget_map.builder')->build($view);
            $widgets = [];
            foreach ($view->getWidgets() as $widget) {
                $widgets[$widget->getId()] = $widget;
            }

            $oldWidgetMaps = $view->getWidgetMap();
            if (!empty($oldWidgetMaps)) {

                foreach ($oldWidgetMaps as $slot => $oldWidgetMap) {
                    $widgetMaps = [];
                    var_dump($oldWidgetMap);
                    usort($oldWidgetMap, function ($a, $b) {
                        if ($b['action'] != $a['action']) {
                            return 0;
                        }

                        return $b['position'] - $a['position'];
                    });
                    var_dump($oldWidgetMap);

                    usort($oldWidgetMap, function ($a, $b) {

                        if ($b['action'] != WidgetMap::ACTION_CREATE) {
                            return -1;
                        }
                        return 1;
                    });
                    usort($oldWidgetMap, function ($a, $b) {

                        if ($b['positionReference'] != null && $b['positionReference'] == $a['widgetId']) {
                            return -1;
                        }
                        return 1;
                    });

                    var_dump($oldWidgetMap);
                    foreach ($oldWidgetMap as $position => $_oldWidgetMap) {
                        var_dump("==========================");
                        var_dump($slot);
                        var_dump($_oldWidgetMap);

                        $widget = $widgetRepo->find($_oldWidgetMap['widgetId']);
                        if (!$widget) {
                            var_dump('widget does not exists');
                            continue;
                        }
                        $widgetMap = new WidgetMap();
                        $referencedWidgetMap = null;
                        if ($_oldWidgetMap['positionReference'] != 0 && $_oldWidgetMap['positionReference'] != null) {
                            var_dump('has positionReference');
                            $referencedWidget = $widgetRepo->find($_oldWidgetMap['positionReference']);
                            var_dump($referencedWidget->getId());
                            $referencedWidgetMap = $view->getWidgetMapByWidget($referencedWidget);
                            while ($referencedWidgetMap->getChild(WidgetMap::POSITION_AFTER)) {
                                $referencedWidgetMap = $referencedWidgetMap->getChild(WidgetMap::POSITION_AFTER);
                            }
                            var_dump('set parent'.$referencedWidgetMap->getWidget()->getId());
                                $widgetMap->setParent($referencedWidgetMap);
                                $widgetMap->setPosition(WidgetMap::POSITION_AFTER);
                        } else {
                            var_dump('has no positionReference');
                            if ($position == 0) {
                                $widgetMap->setPosition(null);
                                var_dump('set parent'.null);
                                $widgetMap->setParent(null);
                            } else {
                                $widgetMap->setPosition(WidgetMap::POSITION_BEFORE);
                                if (!empty(array_slice($widgetMaps, -1))) {
                                $widgetMap->setParent(array_slice($widgetMaps, -1)[0]);
                            }
                        }
                        }

                        if (WidgetMap::ACTION_OVERWRITE == $_oldWidgetMap['action']) {
                            var_dump('is overwrite');

                            /** @var Widget $replacedWidget */
                            if ($_oldWidgetMap['replacedWidgetId']) {
{'fraction_non_alphanumeric': 0.1213407494145199, 'fraction_numerical': 0.0032201405152224825, 'mean_word_length': 1.4626058749324202, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5034596', 'n_tokens_mistral': 3408, 'n_tokens_neox': 3255, 'n_words': 587}