/**
  * 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);
         }
         $pageRepo = $em->getRepository('VictoirePageBundle:BasePage');
         $pages = $pageRepo->findAll();
         $errorRepo = $em->getRepository('VictoireTwigBundle:ErrorPage');
         $errorPages = $errorRepo->findAll();
         $views = array_merge($templates, array_merge($pages, $errorPages));
     }
     /** @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 = [];
                 usort($oldWidgetMap, function ($a, $b) {
                     if ($b['position'] - $a['position'] == 0) {
                         return 1;
                     }
                     return $b['position'] - $a['position'];
                 });
                 foreach ($oldWidgetMap as $key => $item) {
                     if ($item['action'] !== 'create') {
                         unset($oldWidgetMap[$key]);
                         $oldWidgetMap[] = $item;
                     }
                 }
                 foreach ($oldWidgetMap as $key => $item) {
                     if ($item['positionReference'] != null) {
                         foreach ($oldWidgetMap as $_key => $_item) {
                             if ($_item['widgetId'] == $item['positionReference']) {
                                 array_splice($oldWidgetMap[$_key], 0, 0, [$item]);
                                 unset($oldWidgetMap[$key]);
                             }
                         }
                     }
                 }
                 foreach ($oldWidgetMap as $position => $_oldWidgetMap) {
                     $output->writeln('==========================');
                     $output->writeln($slot);
                     $widget = $widgetRepo->find($_oldWidgetMap['widgetId']);
                     if (!$widget) {
                         $output->writeln('widget does not exists');
                         continue;
                     }
                     $widgetMap = new WidgetMap();
                     $referencedWidgetMap = null;
                     if ($_oldWidgetMap['positionReference'] != 0 && $_oldWidgetMap['positionReference'] != null) {
                         $output->writeln('has positionReference');
                         $referencedWidget = $widgetRepo->find($_oldWidgetMap['positionReference']);
                         $output->writeln($referencedWidget->getId());
                         $referencedWidgetMap = WidgetMapHelper::getWidgetMapByWidgetAndView($referencedWidget, $view);
                         while ($referencedWidgetMap->getChild(WidgetMap::POSITION_AFTER)) {
                             $referencedWidgetMap = $referencedWidgetMap->getChild(WidgetMap::POSITION_AFTER);
                         }
                         $output->writeln('set parent' . $referencedWidgetMap->getWidget()->getId());
                         $widgetMap->setParent($referencedWidgetMap);
                         $widgetMap->setPosition(WidgetMap::POSITION_AFTER);
                     } else {
                         $output->writeln('has no positionReference');
                         if ($position == 0) {
                             if (!isset($view->getBuiltWidgetMap()[$slot])) {
                                 $widgetMap->setPosition(null);
                                 $output->writeln('set parent' . null);
                                 $widgetMap->setParent(null);
                             } else {
                                 $widgetMap->setPosition(WidgetMap::POSITION_AFTER);
                                 $_rootBuilt = null;
                                 foreach ($view->getBuiltWidgetMap()[$slot] as $_built) {
                                     if (!$_built->getParent()) {
                                         $_rootBuilt = $_built;
                                         break;
                                     }
                                 }
                                 while (null !== ($child = $_rootBuilt->getChildren($view)[WidgetMap::POSITION_BEFORE])) {
                                     $_rootBuilt = $_rootBuilt->getChildren($view)[WidgetMap::POSITION_BEFORE];
                                 }
                                 $widgetMap->setParent($_rootBuilt);
                             }
                         } 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']) {
                         $output->writeln('is overwrite');
                         /* @var Widget $replacedWidget */
                         if ($_oldWidgetMap['replacedWidgetId']) {
                             $output->writeln('has replacedWidgetId');
                             $replacedWidget = $widgetRepo->find($_oldWidgetMap['replacedWidgetId']);
                             $supplicantWidget = $widgetRepo->find($_oldWidgetMap['widgetId']);
                             $replacedWidgetView = $replacedWidget->getView();
                             $this->getContainer()->get('victoire_widget_map.builder')->build($replacedWidgetView);
                             try {
                                 $replacedWidgetMap = WidgetMapHelper::getWidgetMapByWidgetAndView($replacedWidget, $replacedWidgetView);
                                 $output->writeln('has replacedWidgetMap');
                                 $widgetMap->setReplaced($replacedWidgetMap);
                                 $output->writeln('replace ' . $replacedWidget->getId() . ' by ' . $supplicantWidget->getId());
                                 $widgetMap->setWidget($supplicantWidget);
                                 $widgetMap->setPosition($replacedWidgetMap->getPosition());
                                 $output->writeln('set parent' . ($replacedWidgetMap->getParent() ? $replacedWidgetMap->getParent()->getWidget()->getId() : null));
                                 $widgetMap->setParent($replacedWidgetMap->getParent());
                             } catch (WidgetMapNotFoundException $e) {
                                 // If replaced widgetMap does not exists, this is not an overwrite but a create
                             }
                         } elseif ($referencedWidgetMap) {
                             $output->writeln('move');
                             $this->getContainer()->get('victoire_widget_map.manager')->move($view, ['position' => WidgetMap::POSITION_AFTER, 'slot' => $slot, 'parentWidgetMap' => $referencedWidgetMap, 'widgetMap' => $_oldWidgetMap['widgetId']]);
                         } else {
                             $_oldWidgetMap['action'] = WidgetMap::ACTION_CREATE;
                         }
                     } elseif (WidgetMap::ACTION_DELETE == $_oldWidgetMap['action']) {
                         $output->writeln('is delete');
                         $replacedWidget = $widgetRepo->find($_oldWidgetMap['widgetId']);
                         $widgetMap->setPosition(null);
                         $output->writeln('set parent' . null);
                         $widgetMap->setParent(null);
                         try {
                             $deletedWidgetMap = WidgetMapHelper::getWidgetMapByWidgetAndView($replacedWidget, $view);
                             $replacedWidgetView = $replacedWidget->getView();
                             $this->getContainer()->get('victoire_widget_map.builder')->build($replacedWidgetView);
                             $replacedWidgetMap = WidgetMapHelper::getWidgetMapByWidgetAndView($replacedWidget, $replacedWidgetView);
                             $widgetMap->setReplaced($replacedWidgetMap);
                             $this->getContainer()->get('victoire_widget_map.manager')->moveChildren($view, $deletedWidgetMap->getChild(WidgetMap::POSITION_BEFORE), $deletedWidgetMap->getChild(WidgetMap::POSITION_AFTER), $deletedWidgetMap->getParent(), $deletedWidgetMap->getPosition());
                         } catch (WidgetMapNotFoundException $e) {
                             continue;
                         }
                     }
                     $widgetMap->setAction($_oldWidgetMap['action']);
                     $widgetMap->setWidget($widget);
                     $widgetMap->setAsynchronous($_oldWidgetMap['asynchronous'] ? true : false);
                     $widgetMap->setSlot($slot);
                     $output->writeln('add widgetMap for widget ' . $widgetMap->getWidget()->getId() . ' to view ' . $view->getId());
                     $view->addWidgetMap($widgetMap);
                     $em->persist($widgetMap);
                     $widgetMaps[] = $widgetMap;
                     $this->getContainer()->get('victoire_widget_map.builder')->build($view);
                 }
             }
         }
         $em->flush();
     }
 }
Beispiel #2
0
 /**
  * Delete the widget from the view.
  *
  * @param View   $view
  * @param Widget $widget
  *
  * @throws \Exception Widget map does not exists
  */
 public function delete(View $view, Widget $widget)
 {
     $this->builder->build($view);
     $widgetMap = WidgetMapHelper::getWidgetMapByWidgetAndView($widget, $view);
     $slot = $widgetMap->getSlot();
     $originalParent = $widgetMap->getParent();
     $originalPosition = $widgetMap->getPosition();
     $children = $widgetMap->getChildren($view);
     $beforeChild = !empty($children[WidgetMap::POSITION_BEFORE]) ? $children[WidgetMap::POSITION_BEFORE] : null;
     $afterChild = !empty($children[WidgetMap::POSITION_AFTER]) ? $children[WidgetMap::POSITION_AFTER] : null;
     //we remove the widget from the current view
     if ($widgetMap->getView() === $view) {
         // If the widgetMap has substitutes, delete them or transform them in create mode
         if (count($widgetMap->getSubstitutes()) > 0) {
             foreach ($widgetMap->getSubstitutes() as $substitute) {
                 if ($substitute->getAction() === WidgetMap::ACTION_OVERWRITE) {
                     $substitute->setAction(WidgetMap::ACTION_CREATE);
                     $substitute->setReplaced(null);
                 } else {
                     $view->removeWidgetMap($widgetMap);
                 }
             }
         }
         //remove the widget map from the slot
         $view->removeWidgetMap($widgetMap);
     } else {
         //the widget is owned by another view (a parent)
         //so we add a new widget map that indicates we delete this widget
         $replaceWidgetMap = new WidgetMap();
         $replaceWidgetMap->setAction(WidgetMap::ACTION_DELETE);
         $replaceWidgetMap->setWidget($widget);
         $replaceWidgetMap->setSlot($slot);
         $replaceWidgetMap->setReplaced($widgetMap);
         $view->addWidgetMap($replaceWidgetMap);
     }
     $this->moveChildren($view, $beforeChild, $afterChild, $originalParent, $originalPosition);
 }