/** * Guess the position of the widget relatively to the positionReference. * * @param Widget $widget The widget to position * @param int $positionReference Id of the parent widget * * @return WidgetMap The position of the widget */ public function generateWidgetPosition(EntityManager $entityManager, WidgetMap $widgetMapEntry, $widget, $widgetMap, $positionReference) { $position = 1; $slotId = $widget->getSlot(); if (empty($widgetMap[$slotId])) { $widgetMapEntry->setPosition($position); return $widgetMapEntry; } $slot = $widgetMap[$slotId]; $referenceWidget = $entityManager->getRepository('Victoire\\Bundle\\WidgetBundle\\Entity\\Widget')->findOneById($positionReference); //If we added a widget just after a parent widget //The position of the new widget is the one just after the parent widget if ($referenceWidget && $widget->getView() !== $referenceWidget->getView()) { $position = 1; $widgetMapEntry->setPosition($position); $widgetMapEntry->setPositionReference($positionReference); } else { foreach ($slot as $key => $_widgetMap) { if ($_widgetMap->getWidgetId() === (int) $positionReference) { $widgetMapEntry->setPosition($_widgetMap->getPosition() + 1); break; } elseif (0 === (int) $positionReference) { $widgetMapEntry->setPosition(1); } } } return $widgetMapEntry; }
/** * 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; }
/** * render the Widget. * * @param Widget $widget * @param View $view * * @return widget show */ public function render(Widget $widget, View $view) { //the mode of display of the widget $mode = $widget->getMode(); //if entity is given and it's not the object, retrieve it and set the entity for the widget if ($mode == Widget::MODE_BUSINESS_ENTITY && $view instanceof BusinessPage) { $widget->setEntity($view->getBusinessEntity()); } elseif ($view instanceof BusinessTemplate) { //We'll try to find a sample entity to mock the widget behavior /** @var EntityManager $entityManager */ $entityManager = $this->container->get('doctrine.orm.entity_manager'); if ($mock = $this->bepHelper->getEntitiesAllowedQueryBuilder($view, $entityManager)->setMaxResults(1)->getQuery()->getOneOrNullResult()) { $widget->setEntity($mock); } } //the templating service $templating = $this->container->get('templating'); //the content of the widget $parameters = $this->container->get('victoire_widget.widget_content_resolver')->getWidgetContent($widget); /* * In some cases, for example, WidgetRender in BusinessEntity mode with magic variables {{entity.id}} transformed * into the real business entity id, then if in the rendered action, we need to flush, it would persist the * modified widget which really uncomfortable ;) */ $this->container->get('doctrine.orm.entity_manager')->refresh($widget); //the template displayed is in the widget bundle (with the potential theme) $showView = 'show' . ucfirst($widget->getTheme()); $templateName = $this->container->get('victoire_widget.widget_helper')->getTemplateName($showView, $widget); return $templating->render($templateName, $parameters); }
/** * Render a widget. * * @param Widget $widget * * @return string */ public function cmsWidget($widget) { $widget->setCurrentView($this->currentViewHelper->getCurrentView()); try { $response = $this->widgetRenderer->renderContainer($widget, $widget->getCurrentView()); } catch (\Exception $ex) { $response = $this->widgetExceptionHandler->handle($ex, $widget); } return $response; }
/** * 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; }
/** * Delete a Widget quantum. * * @param Widget $widget The widget to delete * @param int $viewReference The current view * * @return JsonResponse response * @Route("/victoire-dcms/widget/delete/quantum/{id}/{viewReference}", name="victoire_core_widget_delete_bulk", defaults={"_format": "json"}) * @Template() */ public function deleteBulkAction(Widget $widget, $viewReference) { $view = $this->getViewByReferenceId($viewReference); try { $widgets = $widget->getWidgetMap()->getWidgets(); foreach ($widgets as $widget) { $this->get('widget_manager')->deleteWidget($widget, $view); } return new JsonResponse(['success' => true, 'message' => $this->get('translator')->trans('victoire_widget.delete.success', [], 'victoire')]); } catch (Exception $ex) { return $this->getJsonReponseFromException($ex); } }
private function generateHash(Widget $widget) { return sprintf('%s-%s', $widget->generateCacheId(), (string) $this->authorizationChecker->isGranted('ROLE_VICTOIRE')); }
/** * Stylize a widget * @param Widget $widget The widget to stylize * @param integer $viewReference The current view * @param string $entityName The entity name (could be null is the submitted form is in static mode) * * @return JsonResponse * * @Route("/victoire-dcms/widget/stylize/{id}/{viewReference}/{entityName}", name="victoire_core_widget_stylize", options={"expose"=true}) * @Template() */ public function stylizeAction(Widget $widget, $viewReference, $entityName = null) { $view = $this->getViewByReferenceId($viewReference); $widgetView = $widget->getView(); $widgetViewReferenceId = $this->get('victoire_core.view_cache_helper')->getViewReferenceId($widgetView); $widgetViewReference = $this->get('victoire_core.view_cache_helper')->getReferenceByParameters(array('id' => $widgetViewReferenceId)); $widgetView->setReference($widgetViewReference); $this->get('victoire_core.current_view')->setCurrentView($view); try { $form = $this->container->get('form.factory')->create('victoire_widget_style_type', $widget, array('method' => 'POST', 'action' => $this->generateUrl('victoire_core_widget_stylize', array('id' => $widget->getId(), 'viewReference' => $viewReference)))); $form->handleRequest($this->get('request')); if ($form->isValid()) { $this->get('doctrine.orm.entity_manager')->flush(); $params = array('view' => $view, 'success' => true, 'html' => $this->get('victoire_widget.widget_renderer')->render($widget, $view), 'widgetId' => $widget->getId()); } else { $params = array("success" => false, "html" => $this->get('victoire_core.template_mapper')->render("VictoireCoreBundle:Widget:Form/stylize.html.twig", array('view' => $view, 'form' => $form->createView(), 'widget' => $widget))); } $response = new JsonResponse($params); } catch (Exception $ex) { $response = $this->getJsonReponseFromException($ex); } return $response; }
/** * the widget is owned by another view (a parent) * so we add a new widget map that indicates we delete this widget. * * @param View $view * @param WidgetMap $originalWidgetMap * @param Widget $widgetCopy * * @throws \Exception */ public function overwrite(View $view, WidgetMap $originalWidgetMap, Widget $widgetCopy) { $widgetMap = new WidgetMap(); $widgetMap->setAction(WidgetMap::ACTION_OVERWRITE); $widgetMap->setReplaced($originalWidgetMap); $widgetCopy->setWidgetMap($widgetMap); $widgetMap->setSlot($originalWidgetMap->getSlot()); $widgetMap->setPosition($originalWidgetMap->getPosition()); $widgetMap->setParent($originalWidgetMap->getParent()); $view->addWidgetMap($widgetMap); }
/** * @param Widget $widget * @param View $view * * @return WidgetMap|WidgetMapNotFoundException */ public static function getWidgetMapByWidgetAndView(Widget $widget, View $view) { return $widget->getWidgetMap(); }