/**
  * Get content for the widget
  *
  * @param Widget $widget
  *
  * @return Ambigous   <string, unknown, \Victoire\Bundle\CoreBundle\Widget\Managers\mixed, mixed>
  * @throws \Exception
  */
 public function getWidgetContent(Widget $widget)
 {
     //the mode of display of the widget
     $mode = $widget->getMode();
     //the widget must have a mode
     if ($mode === null) {
         throw new \Exception('The widget [' . $widget->getId() . '] has no mode.');
     }
     $resolver = $this->widgetContentResolverChain->getResolverForWidget($widget);
     switch ($mode) {
         case Widget::MODE_STATIC:
             $parameters = $resolver->getWidgetStaticContent($widget);
             break;
         case Widget::MODE_ENTITY:
             //get the content of the widget with its entity
             $parameters = $resolver->getWidgetEntityContent($widget);
             break;
         case Widget::MODE_BUSINESS_ENTITY:
             //get the content of the widget with its entity
             $parameters = $resolver->getWidgetBusinessEntityContent($widget);
             break;
         case Widget::MODE_QUERY:
             $parameters = $resolver->getWidgetQueryContent($widget);
             break;
         default:
             throw new \Exception('The mode [' . $mode . '] is not supported by the widget manager. Widget ID:[' . $widget->getId() . ']');
     }
     return $parameters;
 }
Exemple #2
0
 /**
  * render a widget
  * @param Widget $widget
  * @param View   $view
  *
  * @return string
  */
 public function renderContainer(Widget $widget, View $view)
 {
     $dispatcher = $this->container->get('event_dispatcher');
     $dispatcher->dispatch(VictoireCmsEvents::WIDGET_PRE_RENDER, new WidgetRenderEvent($widget));
     $html = '<div class="vic-widget-container" data-id="' . $widget->getId() . '" id="vic-widget-' . $widget->getId() . '-container">';
     $html .= $this->render($widget, $view);
     $html .= '</div>';
     $dispatcher->dispatch(VictoireCmsEvents::WIDGET_POST_RENDER, new WidgetRenderEvent($widget, $html));
     return $html;
 }
 public function readIntoWidgetRouteParameters(Widget $widget)
 {
     $entity = $widget->getEntity();
     //Creates a new twig environment
     $twig = new \Twig_Environment(new \Twig_Loader_String());
     //add global values for `entity` and `businessEntityId`
     $twig->addGlobal('entity', $entity);
     $twig->addGlobal($widget->getBusinessEntityId(), $entity);
     //Interpret variables in widget route parameters to be able to generate correct
     $params = array();
     foreach ($widget->getLink()->getRouteParameters() as $key => $_routeParameter) {
         $params[$key] = $twig->render($_routeParameter);
     }
     $widget->getLink()->setRouteParameters($params);
 }
 protected function populateParametersWithWidgetFields(Widget $widget, $entity, &$parameters)
 {
     $fields = $widget->getFields();
     //parse the field
     foreach ($fields as $widgetField => $field) {
         //get the value of the field
         if ($entity !== null) {
             $attributeValue = $entity->getEntityAttributeValue($field);
         } else {
             $attributeValue = $widget->getBusinessEntityId() . ' -> ' . $field;
         }
         $parameters[$widgetField] = $attributeValue;
     }
 }
Exemple #5
0
 /**
  * create a widgetMap for the new Widget cloned
  *
  * @return void
  **/
 public function overwriteWidgetMap(Widget $widgetCopy, Widget $widget, View $view)
 {
     //the id of the new widget
     $widgetId = $widgetCopy->getId();
     //the widget slot
     $widgetSlotId = $widget->getSlot();
     //the widget id
     $replacedWidgetId = $widget->getId();
     //get the slot
     $slot = $view->getSlotById($widgetSlotId);
     //there might be no slot yet for the child view
     if ($slot === null) {
         //create a new slot
         $slot = new Slot();
         $slot->setId($widgetSlotId);
         //add the new slot to the view
         $view->addSlot($slot);
     }
     $originalWidgetMap = $slot->getWidgetMapByWidgetId($replacedWidgetId);
     //If widgetmap was not found current view, we dig
     if (!$originalWidgetMap) {
         $watchDog = 100;
         $_view = $view;
         while (!$originalWidgetMap && $watchDog) {
             $_view = $_view->getTemplate();
             $parentSlot = $_view->getSlotById($widgetSlotId);
             if ($parentSlot) {
                 $originalWidgetMap = $parentSlot->getWidgetMapByWidgetId($replacedWidgetId);
             }
             $watchDog--;
         }
         if (0 == $watchDog) {
             throw new \Exception(sprintf("The slot %s doesn't appears to be in any templates WidgetMaps. You should check this manually.", $widgetSlotId));
         }
     }
     //the widget is owned by another view (a parent)
     //so we add a new widget map that indicates we delete this widget
     $widgetMap = new WidgetMap();
     $widgetMap->setAction(WidgetMap::ACTION_OVERWRITE);
     $widgetMap->setReplacedWidgetId($replacedWidgetId);
     $widgetMap->setWidgetId($widgetId);
     $widgetMap->setPosition($originalWidgetMap->getPosition());
     $widgetMap->setAsynchronous($widgetCopy->isAsynchronous());
     $widgetMap->setPositionReference($originalWidgetMap->getPositionReference());
     $slot->addWidgetMap($widgetMap);
 }
Exemple #6
0
 /**
  * Remove a widget.
  *
  * @param Widget $widget
  *
  * @return array The parameter for the view
  */
 public function deleteWidget(Widget $widget, View $view)
 {
     //Used to update view in callback (we do it before delete it else it'll not exists anymore)
     $widgetId = $widget->getId();
     //we update the widget map of the view
     $this->widgetMapBuilder->build($view);
     $widgetMap = WidgetMapHelper::getWidgetMapByWidgetAndView($widget, $view);
     //the widget is removed only if the current view is the view of the widget
     if ($widgetMap->getView() == $view && $widgetMap->getAction() != WidgetMap::ACTION_DELETE) {
         //we remove the widget
         $this->entityManager->remove($widget);
     }
     //we update the view
     $this->entityManager->persist($view);
     //update the view deleting the widget
     $this->widgetMapManager->delete($view, $widget);
     $this->entityManager->flush();
     return ['success' => true, 'widgetId' => $widgetId, 'viewCssHash' => $view->getCssHash()];
 }