/** * Transforms an object (View) to a string (id). * * @param View|null $view * * @return string */ public function transform($view) { if (null === $view) { return ''; } return $view->getId(); }
/** * This method takes the builtWidgetMap for view and creates an array that indicate, for * each widgetmap, if the position "after" and "before" are available. * * @param View $view * * @return array */ public function getAvailablePosition(View $view) { $widgetMaps = $view->getBuiltWidgetMap(); $availablePositions = []; foreach ($widgetMaps as $slot => $widgetMap) { foreach ($widgetMap as $_widgetMap) { $availablePositions[$slot][$_widgetMap->getId()]['id'] = $_widgetMap->getId(); $availablePositions[$slot][$_widgetMap->getId()][WidgetMap::POSITION_BEFORE] = true; $availablePositions[$slot][$_widgetMap->getId()][WidgetMap::POSITION_AFTER] = true; if ($_widgetMap->getReplaced()) { $availablePositions[$slot][$_widgetMap->getId()]['replaced'] = $_widgetMap->getReplaced()->getId(); } } /** @var WidgetMap $_widgetMap */ foreach ($widgetMap as $_widgetMap) { if ($_widgetMap->getParent()) { if ($substitute = $_widgetMap->getParent()->getSubstituteForView($view)) { $availablePositions[$slot][$substitute->getId()][$_widgetMap->getPosition()] = false; } else { $availablePositions[$slot][$_widgetMap->getParent()->getId()][$_widgetMap->getPosition()] = false; } } } } return $availablePositions; }
/** * 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); }
/** * @param View $view, the view to translatate * @param $templatename the new name of the view * @param $loopindex the current loop of iteration in recursion * @param $locale the target locale to translate view * * this methods allow you to add a translation to any view * recursively to its subview */ public function addTranslation(View $view, $viewName = null, $locale) { $template = null; if ($view->getTemplate()) { $template = $view->getTemplate(); if ($template->getI18n()->getTranslation($locale)) { $template = $template->getI18n()->getTranslation($locale); } else { $templateName = $template->getName() . '-' . $locale; $this->em->refresh($view); $template = $this->addTranslation($template, $templateName, $locale); } } $view->setLocale($locale); $view->setTemplate($template); $clonedView = $this->cloneView($view, $viewName, $locale); if ($clonedView instanceof BasePage && $view->getTemplate()) { $template->addPage($clonedView); } $i18n = $view->getI18n(); $i18n->setTranslation($locale, $clonedView); $this->em->persist($clonedView); $this->em->refresh($view); $this->em->flush(); return $clonedView; }
protected function getAvailableLocales(View $view) { $choices = array(); $i18n = $view->getI18n(); foreach ($this->availableLocales as $localeVal) { $choices[$localeVal] = 'victoire.i18n.viewType.locale.' . $localeVal; } return $choices; }
public function buildReference(View $businessPage, EntityManager $em) { $businessPageReference = null; $businessEntityMetadata = new ClassMetadata(get_class($businessPage->getBusinessEntity())); if ($businessEntityMetadata->name !== 'Victoire\\Bundle\\BlogBundle\\Entity\\Article') { $businessPageReference = parent::buildReference($businessPage, $em); } return $businessPageReference; }
protected function getAvailableLocales(View $view) { $choices = []; $i18n = $view->getI18n(); foreach ($this->availableLocales as $localeVal) { if ($i18n->getTranslation($localeVal) === null) { $choices[$localeVal] = 'victoire.i18n.viewType.locale.' . $localeVal; } } return $choices; }
/** * Populate widgets in View's widgetMap. * * @param View $view * @param Widget[] $viewWidgets */ private function populateWidgets(View $view, array $viewWidgets) { foreach ($view->getWidgetMap() as $slotId => $widgetMapArray) { /* @var WidgetMap[] $widgetMapArray */ foreach ($widgetMapArray as $widgetMap) { foreach ($viewWidgets as $viewWidget) { if ($widgetMap->getWidgetId() == $viewWidget->getId()) { $widgetMap->setWidget($viewWidget); break; } } } } }
/** * Find a widgetMap by widgetId and view. * * @param int $widgetId * @param View $view * * @return WidgetMap **/ public function removeWidgetMapByWidgetIdAndView($widgetId, View $view) { foreach ($view->getSlots() as $_slot) { foreach ($_slot->getWidgetMaps() as $_widgetMap) { if ($_widgetMap->getWidgetId() == $widgetId) { $_slot->removeWidgetMap($_widgetMap); //update the widget map $view->updateWidgetMapBySlots(); return ['success' => true]; } } } return ['success' => false, 'message' => 'The widget isn\'t present in widgetMap...']; }
/** * @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; }
/** * @param View $view * @param mixed $entity * * @return string */ public static function generateViewReferenceId(View $view, $entity = null) { $id = $view->getId(); if ($view instanceof BusinessPage) { $id = $view->getTemplate()->getId(); $entity = $view->getBusinessEntity(); } elseif (!$view instanceof WebViewInterface) { return $view->getId(); } $refId = sprintf('ref_%s', $id); if ($entity) { $refId .= '_' . $entity->getId(); } return $refId; }
/** * Construct. **/ public function __construct() { parent::__construct(); $this->widgets = new ArrayCollection(); $this->inheritors = new ArrayCollection(); $this->pages = new ArrayCollection(); }
/** * Construct. **/ public function __construct() { parent::__construct(); $this->publishedAt = new \DateTime(); $this->status = PageStatus::PUBLISHED; $this->homepage = false; }
/** * 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 entty is given and it's not the object, retrive it and set the entity for the widget if ($mode == Widget::MODE_BUSINESS_ENTITY && $view instanceof BusinessEntityPage) { $widget->setEntity($view->getBusinessEntity()); } //the templating service $templating = $this->container->get('victoire_templating'); //the content of the widget $parameters = $this->container->get('victoire_widget.widget_content_resolver')->getWidgetContent($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); }
/** * BasePage settings. * * @param BasePage $page * * @Route("/{id}", name="victoire_seo_pageSeo_settings") * @Template() * * @return JsonResponse */ public function settingsAction(View $page) { //services $em = $this->getDoctrine()->getManager(); $businessProperties = []; //if the page is a business entity template page if ($page instanceof BusinessPage || $page instanceof BusinessTemplate) { //we can use the business entity properties on the seo $businessEntity = $this->get('victoire_core.helper.business_entity_helper')->findById($page->getBusinessEntityId()); $businessProperties = $businessEntity->getBusinessPropertiesByType('seoable'); } $pageSeo = $page->getSeo() ? $page->getSeo() : new PageSeo($page); //url for the form $formUrl = $this->get('router')->generate('victoire_seo_pageSeo_settings', ['id' => $page->getId()]); //create the form $form = $this->get('form.factory')->create(PageSeoType::class, $pageSeo, ['action' => $formUrl, 'method' => 'POST']); $form->handleRequest($this->get('request')); $novalidate = $this->get('request')->query->get('novalidate', false); $template = 'VictoireSeoBundle:PageSeo:form.html.twig'; if ($novalidate === false) { $template = 'VictoireSeoBundle:PageSeo:settings.html.twig'; } if (false === $novalidate && $form->isValid()) { $em->persist($pageSeo); $page->setSeo($pageSeo); $em->persist($page); $em->flush(); //redirect to the page url if (!method_exists($page, 'getUrl')) { $url = $this->generateUrl('victoire_business_template_show', ['id' => $page->getId()]); } else { /** @var ViewReference $viewReference */ $viewReference = $this->container->get('victoire_view_reference.repository')->getOneReferenceByParameters(['viewId' => $page->getId()]); $page->setReference($viewReference); $url = $this->generateUrl('victoire_core_page_show', ['url' => $viewReference->getUrl()]); } $this->get('victoire_core.current_view')->setCurrentView($page); $this->congrat('victoire_seo.save.success'); return new JsonResponse(['success' => true, 'url' => $url]); } return new JsonResponse(['success' => !$form->isSubmitted(), 'html' => $this->container->get('templating')->render($template, ['page' => $page, 'form' => $form->createView(), 'businessProperties' => $businessProperties])]); }
/** * 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); }
public function build(View $view, $updatePage = true) { $viewWidgetMaps = null; $widgetMap = array(); //get the template widget map $template = $view->getTemplate(); if ($template !== null) { $widgetMap = $this->build($template); } // build the view widgetMaps for each its slots foreach ($view->getSlots() as $slot) { if (empty($widgetMap[$slot->getId()])) { $widgetMap[$slot->getId()] = array(); } if ($slot !== null) { $viewWidgetMaps = $slot->getWidgetMaps(); } //if the current view have some widget maps if ($viewWidgetMaps !== null) { //we parse the widget maps foreach ($viewWidgetMaps as $viewWidgetMap) { $viewWidgetMap = clone $viewWidgetMap; //depending on the action $action = $viewWidgetMap->getAction(); switch ($action) { case WidgetMap::ACTION_CREATE: $position = (int) $viewWidgetMap->getPosition(); $reference = (int) $viewWidgetMap->getPositionReference(); if ($reference != 0) { if (isset($widgetMap[$slot->getId()])) { foreach ($widgetMap[$slot->getId()] as $key => $_widgetMap) { if ($_widgetMap->getWidgetId() === $reference) { $position += $_widgetMap->getPosition(); } } } } array_splice($widgetMap[$slot->getId()], $position - 1, 0, array($viewWidgetMap)); array_map(function ($key, $_widgetMap) { $_widgetMap->setPosition($key + 1); }, array_keys($widgetMap[$slot->getId()]), $widgetMap[$slot->getId()]); break; case WidgetMap::ACTION_OVERWRITE: //parse the widget maps $position = null; foreach ($widgetMap[$slot->getId()] as $index => $wm) { if ($wm->getWidgetId() == $viewWidgetMap->getReplacedWidgetId()) { if (null === $viewWidgetMap->isAsynchronous()) { $viewWidgetMap->setAsynchronous($wm->isAsynchronous()); } //replace the widget map from the list unset($widgetMap[$slot->getId()][$index]); break; } } array_splice($widgetMap[$slot->getId()], $viewWidgetMap->getPosition() - 1, 0, array($viewWidgetMap)); array_map(function ($key, $_widgetMap) { $_widgetMap->setPosition($key + 1); }, array_keys($widgetMap[$slot->getId()]), $widgetMap[$slot->getId()]); break; case WidgetMap::ACTION_DELETE: //parse the widget maps foreach ($widgetMap[$slot->getId()] as $index => $wm) { if ($wm->getWidgetId() === $viewWidgetMap->getWidgetId()) { //remove the widget map from the list unset($widgetMap[$slot->getId()][$index]); } } break; default: throw new \Exception('The action [' . $action . '] is not handled yet.'); break; } } ksort($widgetMap[$slot->getId()]); } } if ($updatePage) { $view->setBuiltWidgetMap($widgetMap); } return $widgetMap; }
/** * create a form with given widget * * @param Widget $widget * @param View $view * @param string $entityName * @param string $namespace * @param string $formMode * @param integer $position * * @return $form * * @throws \Exception */ public function buildWidgetForm(Widget $widget, View $view, $entityName = null, $namespace = null, $formMode = Widget::MODE_STATIC, $position = 0) { $router = $this->container->get('router'); //test parameters if ($entityName !== null) { if ($namespace === null) { throw new \Exception('The namespace is mandatory if the entityName is given'); } if ($formMode === null) { throw new \Exception('The formMode is mandatory if the entityName is given'); } } $container = $this->container; $formFactory = $container->get('form.factory'); $formAlias = 'victoire_widget_form_' . strtolower($this->container->get('victoire_widget.widget_helper')->getWidgetName($widget)); $filters = array(); if ($this->container->has('victoire_core.filter_chain')) { $filters = $this->container->get('victoire_core.filter_chain')->getFilters(); } //are we updating or creating the widget? if ($widget->getId() === null) { $viewReference = $view->getReference(); $formUrl = $router->generate('victoire_core_widget_create', array('mode' => $formMode, 'viewReference' => $viewReference['id'], 'slot' => $widget->getSlot(), 'type' => $widget->getType(), 'entityName' => $entityName, 'positionReference' => $position)); } else { $viewReference = $widget->getCurrentView()->getReference(); $formUrl = $router->generate('victoire_core_widget_update', array('id' => $widget->getId(), 'viewReference' => $viewReference['id'], 'entityName' => $entityName)); } $params = array('entityName' => $entityName, 'namespace' => $namespace, 'mode' => $formMode, 'action' => $formUrl, 'method' => 'POST', 'filters' => $filters); /** @var Form $mockForm Get the base form to get the name */ $mockForm = $formFactory->create($formAlias, $widget, $params); //Prefix base name with form mode to avoid to have unique form fields ids $form = $formFactory->createNamed(sprintf("%s_%s_%s", $entityName, $formMode, $mockForm->getName()), $formAlias, $widget, $params); return $form; }
/** * Set a Template inheritors (View) as not up to date. * * @param View $view */ public function setTemplateInheritorsCssToUpdate(View $view) { if (!$view instanceof Template) { return; } foreach ($view->getInheritors() as $inheritor) { $inheritor->setCssUpToDate(false); $this->uow->persist($inheritor); $this->setTemplateInheritorsCssToUpdate($inheritor); } }
/** * Tell if css file exists for a given View. * * @param View $view * * @return bool */ public function cssFileExists(View $view) { $file = $this->getViewCssFileFromHash($view->getCssHash()); return file_exists($file); }
/** * @param View $view * @param $entity * @return string */ public static function getViewReferenceId(View $view, $entity = null) { if ($view instanceof BusinessEntityPage) { $entity = $view->getBusinessEntity(); } $id = sprintf('ref_%s', $view->getId()); if ($entity) { $id .= '_' . $entity->getId(); } return $id; }
/** * Find all widgets for a given View. * * @param View $view * * @return multitype */ public function findAllWidgetsForView(View $view) { return $this->findAllIn($view->getWidgetsIds()); }
/** * Find page's ancestors (templates and parents) and flatted all their roles. * * @param View $view * * @return array */ private function getPageRoles(View $view) { $insertAncestorRole = function (View $view = null) use(&$insertAncestorRole) { if ($view === null) { return; } $roles = $view->getRoles(); if ($templateRoles = $insertAncestorRole($view->getTemplate(), $roles)) { $roles .= ($roles ? ',' : '') . $templateRoles; } if ($parentRoles = $insertAncestorRole($view->getParent(), $roles)) { $roles .= ($roles ? ',' : '') . $parentRoles; } return $roles; }; $roles = $insertAncestorRole($view); if ($roles) { return array_unique(explode(',', $roles)); } }
public function isTemplateOf(View $view) { while ($_view = $view->getTemplate()) { if ($this === $_view) { return true; } $view = $_view; } return false; }
/** * 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; }
/** * 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); }
/** * Is the business entity type allowed for the widget and the view context. * * @param string $formEntityName The business entity name * @param View $view The view * * @return bool Does the form allows this kind of business entity in this view */ public function isBusinessEntityAllowed($formEntityName, View $view) { //the result $isBusinessEntityAllowed = false; if ($view instanceof BusinessTemplate || $view instanceof BusinessPage) { //are we using the same business entity if ($formEntityName === $view->getBusinessEntityId()) { $isBusinessEntityAllowed = true; } } return $isBusinessEntityAllowed; }
/** * Update a Template inheritors (View) if necessary. * * @param View $view */ public function updateTemplateInheritorsCss(View $view) { if (!$view instanceof Template) { return; } foreach ($view->getInheritors() as $inheritor) { $this->updateViewCss($inheritor); $this->updateTemplateInheritorsCss($inheritor); } }