Skip to content

Commit

Permalink
Merge pull request #791 from gregumo/2.2
Browse files Browse the repository at this point in the history
Improve WidgetMap Builder coverage
  • Loading branch information
lenybernard authored Feb 13, 2017
2 parents 5fb56be + 9a7680c commit 91bbbb4
Show file tree
Hide file tree
Showing 18 changed files with 495 additions and 244 deletions.
20 changes: 10 additions & 10 deletions Bundle/BusinessPageBundle/Controller/BusinessTemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public function indexAction()
}

return new JsonResponse([
'html' => $this->container->get('templating')->render(
'html' => $this->container->get('templating')->render(
'VictoireBusinessPageBundle:BusinessEntity:index.html.twig',
[
'businessEntities' => $businessEntities,
'BusinessTemplates' => $BusinessTemplates,
'businessEntities' => $businessEntities,
'BusinessTemplates' => $BusinessTemplates,
]
),
'success' => true,
Expand Down Expand Up @@ -188,8 +188,8 @@ public function newAction($id)
$form = $this->createCreateForm($view);

$parameters = [
'entity' => $view,
'form' => $form->createView(),
'entity' => $view,
'form' => $form->createView(),
];

return new JsonResponse([
Expand Down Expand Up @@ -221,9 +221,9 @@ public function editAction(View $view)
$deleteForm = $this->createDeleteForm($view->getId());

$parameters = [
'entity' => $view,
'form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
'entity' => $view,
'form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
];

return new JsonResponse([
Expand Down Expand Up @@ -373,8 +373,8 @@ public function listEntitiesAction(BusinessTemplate $view)

//parameters for the view
return [
'BusinessTemplate' => $view,
'items' => $bepHelper->getEntitiesAllowed($view, $this->get('doctrine.orm.entity_manager')),
'BusinessTemplate' => $view,
'items' => $bepHelper->getEntitiesAllowed($view, $this->get('doctrine.orm.entity_manager')),
];
}

Expand Down
51 changes: 31 additions & 20 deletions Bundle/CoreBundle/Entity/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Victoire\Bundle\CoreBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Knp\DoctrineBehaviors\Model\Translatable\Translatable;
Expand Down Expand Up @@ -167,6 +166,11 @@ abstract class View
*/
protected $roles;

/**
* @var array
*/
protected $builtWidgetMap;

/**
* Construct.
**/
Expand Down Expand Up @@ -224,7 +228,7 @@ public function setTemplate($template)
/**
* Get template.
*
* @return string
* @return View
*/
public function getTemplate()
{
Expand Down Expand Up @@ -521,19 +525,19 @@ public function setWidgetMaps($widgetMaps)
}

/**
* Get widgets.
* Get WidgetMaps.
*
* @return Collection[WidgetMap]
* @return WidgetMap[]
*/
public function getWidgetMaps()
{
return $this->widgetMaps;
}

/**
* Add widget.
* Add WidgetMap.
*
* @param Widget $widgetMap
* @param WidgetMap $widgetMap
*/
public function addWidgetMap(WidgetMap $widgetMap)
{
Expand All @@ -554,22 +558,24 @@ public function removeWidgetMap(WidgetMap $widgetMap)
}

/**
* Get widgets ids as array.
* Get WidgetMaps for View and its Templates.
*
* @return array
* @return WidgetMap[]
*/
public function getWidgetsIds()
public function getWidgetMapsForViewAndTemplates()
{
$widgetIds = [];
foreach ($this->getBuiltWidgetMap() as $slot => $_widgetMaps) {
foreach ($_widgetMaps as $widgetMap) {
foreach ($widgetMap->getWidgets() as $widget) {
$widgetIds[] = $widget->getId();
}
}
$widgetMaps = [];

foreach ($this->getWidgetMaps() as $_widgetMap) {
$widgetMaps[] = $_widgetMap;
}

if ($template = $this->getTemplate()) {
$templateWidgetMaps = $template->getWidgetMapsForViewAndTemplates();
$widgetMaps = array_merge($widgetMaps, $templateWidgetMaps);
}

return $widgetIds;
return $widgetMaps;
}

/**
Expand All @@ -585,7 +591,7 @@ public function getBuiltWidgetMap()
/**
* Set builtWidgetMap.
*
* @param string $builtWidgetMap
* @param array $builtWidgetMap
*
* @return $this
*/
Expand Down Expand Up @@ -717,7 +723,7 @@ public function changeCssHash()

/**
* @deprecated
* Get widgetMap.
* Get widgetMap
*
* @return widgetMap
*/
Expand All @@ -728,7 +734,7 @@ public function getWidgetMap()

/**
* @deprecated
* Get widgets.
* Get widgets
*
* @return string
*/
Expand All @@ -737,6 +743,11 @@ public function getWidgets()
return $this->widgets;
}

/**
* @param View $view
*
* @return bool
*/
public function isTemplateOf(View $view)
{
while ($_view = $view->getTemplate()) {
Expand Down
2 changes: 1 addition & 1 deletion Bundle/CoreBundle/EventSubscriber/WidgetSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function updateViewCss(View $view)
$view->changeCssHash();

//Update css file
$this->widgetMapBuilder->build($view, $this->em, true);
$this->widgetMapBuilder->build($view, true);
$widgets = $this->widgetRepo->findAllWidgetsForView($view);

//Generate CSS file and set View's CSS as up to date
Expand Down
10 changes: 5 additions & 5 deletions Bundle/PageBundle/Helper/PageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function renderPage($view, $layout = null)
$this->eventDispatcher->dispatch('victoire.on_render_page', $pageRenderEvent);

//Build WidgetMap
$this->widgetMapBuilder->build($view, $this->entityManager, true);
$this->widgetMapBuilder->build($view, true);

//Populate widgets with their data
$this->widgetDataWarmer->warm($this->entityManager, $view);
Expand Down Expand Up @@ -253,7 +253,7 @@ public function updatePageWithEntity(BusinessTemplate $page, $entity)
* @param BusinessPageReference $viewReference
*
* @return BusinessPage
* read the cache to find entity according tu given url.
* read the cache to find entity according tu given url
* @return object|null
*/
protected function findEntityByReference(ViewReference $viewReference)
Expand All @@ -276,13 +276,13 @@ public function findPageByReference($viewReference)
if ($viewReference->getViewId()) { //BusinessPage
$page = $this->entityManager->getRepository('VictoireCoreBundle:View')
->findOneBy([
'id' => $viewReference->getViewId(),
'id' => $viewReference->getViewId(),
]);
$page->setCurrentLocale($viewReference->getLocale());
} else { //VirtualBusinessPage
$page = $this->entityManager->getRepository('VictoireCoreBundle:View')
->findOneBy([
'id' => $viewReference->getTemplateId(),
'id' => $viewReference->getTemplateId(),
]);
if ($entity = $this->findEntityByReference($viewReference)) {
if ($page instanceof BusinessTemplate) {
Expand All @@ -299,7 +299,7 @@ public function findPageByReference($viewReference)
} elseif ($viewReference instanceof ViewReference) {
$page = $this->entityManager->getRepository('VictoireCoreBundle:View')
->findOneBy([
'id' => $viewReference->getViewId(),
'id' => $viewReference->getViewId(),
]);
$page->setCurrentLocale($viewReference->getLocale());
} else {
Expand Down
3 changes: 2 additions & 1 deletion Bundle/WidgetBundle/Entity/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public function __construct()
* @ORM\JoinColumn(name="view_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $view;

/**
* @var WidgetMap
*
Expand Down Expand Up @@ -442,7 +443,7 @@ public function __clone()

/**
* @deprecated
* Get view.
* Get view
*
* @return string
*/
Expand Down
5 changes: 4 additions & 1 deletion Bundle/WidgetBundle/Renderer/WidgetRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,16 @@ public function render(Widget $widget, View $view)
$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);
if ($widget->getMode() == Widget::MODE_BUSINESS_ENTITY) {
$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());
Expand Down
47 changes: 23 additions & 24 deletions Bundle/WidgetBundle/Repository/WidgetRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,49 @@
namespace Victoire\Bundle\WidgetBundle\Repository;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Victoire\Bundle\CoreBundle\Entity\View;
use Victoire\Bundle\WidgetBundle\Entity\Widget;

/**
* The widget Repository.
* The Widget Repository.
*/
class WidgetRepository extends EntityRepository
{
/**
* Get all the widget within a list of ids.
* Get all Widgets for a given View.
*
* @param array $widgetIds
*
* @return \Doctrine\ORM\QueryBuilder
*/
public function getAllIn(array $widgetIds)
{
return $this->createQueryBuilder('widget')
->where('widget.id IN (:map)')
->setParameter('map', $widgetIds);
}

/**
* Find all the widgets in a list of ids.
*
* @param array $widgetIds
* @param View $view
*
* @return multitype:
* @return QueryBuilder
*/
public function findAllIn(array $widgetIds)
public function getAllForView(View $view)
{
$qb = $this->getAllIn($widgetIds);
//Get all WidgetMaps ids for this View
$widgetMapsIdsToSearch = [];
foreach ($view->getBuiltWidgetMap() as $widgetMaps) {
foreach ($widgetMaps as $widgetMap) {
$widgetMapsIdsToSearch[] = $widgetMap->getId();
}
}

return $qb->getQuery()->getResult();
return $this->createQueryBuilder('widget')
->join('widget.widgetMap', 'widgetMap')
->andWhere('widgetMap.id IN (:widgetMapsIds)')
->setParameter('widgetMapsIds', $widgetMapsIdsToSearch);
}

/**
* Find all widgets for a given View.
* Find all Widgets for a given View.
*
* @param View $view
*
* @return multitype
* @return Widget[]
*/
public function findAllWidgetsForView(View $view)
{
return $this->findAllIn($view->getWidgetsIds());
$qb = $this->getAllForView($view);

return $qb->getQuery()->getResult();
}
}
3 changes: 2 additions & 1 deletion Bundle/WidgetBundle/Resolver/WidgetResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ public function __construct(DataSourceChain $dataSourceChain, AuthorizationCheck
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());
Expand Down
Loading

0 comments on commit 91bbbb4

Please sign in to comment.