Exemplo n.º 1
0
 /**
  * @param int            $id
  * @param null|WidgetMap $parent
  */
 protected function newWidgetMap($id, $parent, $position, View $view, Widget $widget)
 {
     $widgetMap = new WidgetMap();
     $widgetMap->setId($id);
     if ($parent) {
         $widgetMap->setParent($parent);
         $parent->addChild($widgetMap);
     }
     $widgetMap->setPosition($position);
     $widgetMap->setWidget($widget);
     $widgetMap->setSlot('content');
     $view->addWidgetMap($widgetMap);
     return $widgetMap;
 }
Exemplo n.º 2
0
 public function resolve(WidgetMap $widgetMap)
 {
     //TODO: orderize it
     $widgets = $widgetMap->getWidgets();
     // if the widgetmap is linked to no widgets, it seems that it is an overwrite of the position so keep the replaced widgets for display
     if ($widgetMap->getReplaced() && count($widgets) === 0) {
         $widgets = $widgetMap->getReplaced()->getWidgets();
     }
     /* @var Widget $widget */
     foreach ($widgets as $_widget) {
         /** @var Criteria $criteria */
         foreach ($_widget->getCriterias() as $criteria) {
             $value = $this->dataSourceChain->getData($criteria->getName());
             if (!$this->assert($value(), $criteria->getOperator(), $criteria->getValue())) {
                 continue 2;
                 //try with break
             }
         }
         return $_widget;
     }
 }
Exemplo n.º 3
0
 /**
  * Set widgets.
  *
  * @param [WidgetMap] $widgetMaps
  *
  * @return Widget
  */
 public function setWidgetMap(WidgetMap $widgetMap = null)
 {
     if ($widgetMap) {
         $widgetMap->addWidget($this);
     }
     $this->widgetMap = $widgetMap;
     return $this;
 }
 /**
  * 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();
     }
 }
Exemplo n.º 5
0
 /**
  * Add widget.
  *
  * @param Widget $widgetMap
  */
 public function addWidgetMap(WidgetMap $widgetMap)
 {
     if (!$widgetMap->getView()) {
         $widgetMap->setView($this);
     }
     $this->widgetMaps[] = $widgetMap;
 }
Exemplo n.º 6
0
 /**
  * Find return all the given WidgetMap children for each view where it's related.
  *
  * @param WidgetMap $widgetMap
  *
  * @return mixed
  */
 protected function getChildrenByView(WidgetMap $widgetMap)
 {
     $beforeChilds = $widgetMap->getChilds(WidgetMap::POSITION_BEFORE);
     $afterChilds = $widgetMap->getChilds(WidgetMap::POSITION_AFTER);
     $childrenByView['views'] = [];
     $childrenByView['before'] = [];
     $childrenByView['after'] = [];
     foreach ($beforeChilds as $beforeChild) {
         $view = $beforeChild->getView();
         $childrenByView['views'][] = $view;
         $childrenByView['before'][$view->getId()] = $beforeChild;
     }
     foreach ($afterChilds as $afterChild) {
         $view = $afterChild->getView();
         $childrenByView['views'][] = $view;
         $childrenByView['after'][$view->getId()] = $afterChild;
     }
     return $childrenByView;
 }
Exemplo n.º 7
0
 /**
  * @param null|WidgetMap $parent
  */
 public function setParent(WidgetMap $parent = null)
 {
     if ($this->parent) {
         $this->parent->removeChild($this);
     }
     if ($parent) {
         $parent->addChild($this);
     }
     $this->parent = $parent;
 }