Exemplo n.º 1
0
 /**
  * Get content for the widget.
  *
  * @param Widget $widget
  *
  * @throws \Exception
  *
  * @return array
  */
 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;
 }
Exemplo n.º 2
0
 /**
  * create a form with given widget.
  *
  * @param Widget $widget
  * @param View   $view
  * @param string $businessEntityId
  * @param string $namespace
  * @param string $formMode
  * @param int    $position
  *
  * @throws \Exception
  *
  * @return Form
  */
 public function buildWidgetForm(Widget $widget, View $view, $businessEntityId = null, $namespace = null, $formMode = Widget::MODE_STATIC, $position = null, $parentWidgetMap = null, $slotId = null, $quantum = null)
 {
     $router = $this->container->get('router');
     //test parameters
     if ($businessEntityId !== null) {
         if ($namespace === null) {
             throw new \Exception('The namespace is mandatory if the businessEntityId is given');
         }
         if (in_array($formMode, [Widget::MODE_STATIC, null])) {
             throw new \Exception('The formMode cannot be null or static if the businessEntityId is given');
         }
     }
     $container = $this->container;
     $formFactory = $container->get('form.factory');
     //are we updating or creating the widget?
     if ($widget->getId() === null) {
         $viewReference = $view->getReference();
         $actionParams = ['viewReference' => $viewReference->getId(), 'slot' => $slotId, 'type' => $widget->getType(), 'position' => $position, 'parentWidgetMap' => $parentWidgetMap, 'quantum' => $quantum];
         $action = 'victoire_core_widget_create';
         if ($businessEntityId) {
             $actionParams['businessEntityId'] = $businessEntityId;
             $actionParams['mode'] = $formMode;
         } else {
             $action = 'victoire_core_widget_create_static';
         }
         $formUrl = $router->generate($action, $actionParams);
     } else {
         $viewReference = $this->container->get('victoire_core.current_view')->getCurrentView()->getReference();
         $formUrl = $router->generate('victoire_core_widget_update', ['id' => $widget->getId(), 'viewReference' => $viewReference->getId(), 'businessEntityId' => $businessEntityId, 'mode' => $formMode, 'quantum' => $quantum]);
     }
     $widgetName = $this->container->get('victoire_widget.widget_helper')->getWidgetName($widget);
     $widgetFormTypeClass = ClassUtils::getClass($this->container->get(sprintf('victoire.widget.form.%s', strtolower($widgetName))));
     $optionsContainer = new WidgetOptionsContainer(['businessEntityId' => $businessEntityId, 'namespace' => $namespace, 'mode' => $formMode, 'action' => $formUrl, 'method' => 'POST', 'dataSources' => $this->container->get('victoire_criteria.chain.data_source_chain')]);
     $event = new WidgetFormCreateEvent($optionsContainer, $widgetFormTypeClass);
     $this->container->get('event_dispatcher')->dispatch(WidgetFormEvents::PRE_CREATE, $event);
     $this->container->get('event_dispatcher')->dispatch(WidgetFormEvents::PRE_CREATE . '_' . strtoupper($widgetName), $event);
     /** @var Form $mockForm Get the base form to get the name */
     $mockForm = $formFactory->create($widgetFormTypeClass, $widget, $optionsContainer->getOptions());
     //Prefix base name with form mode to avoid to have unique form fields ids
     $form = $formFactory->createNamed(sprintf('%s_%s_%s_%s', $businessEntityId, $this->convertToString($quantum), $formMode, $mockForm->getName()), $widgetFormTypeClass, $widget, $optionsContainer->getOptions());
     return $form;
 }
Exemplo n.º 3
0
 /**
  * Delete a Widget.
  *
  * @param Widget $widget        The widget to delete
  * @param int    $viewReference The current view
  *
  * @return JsonResponse response
  * @Route("/victoire-dcms/widget/delete/{id}/{viewReference}", name="victoire_core_widget_delete", defaults={"_format": "json"})
  * @Template()
  */
 public function deleteAction(Widget $widget, $viewReference)
 {
     $view = $this->getViewByReferenceId($viewReference);
     try {
         $widgetId = $widget->getId();
         $this->get('widget_manager')->deleteWidget($widget, $view);
         return new JsonResponse(['success' => true, 'message' => $this->get('translator')->trans('victoire_widget.delete.success', [], 'victoire'), 'widgetId' => $widgetId]);
     } catch (Exception $ex) {
         return $this->getJsonReponseFromException($ex);
     }
 }