Skip to content

Commit

Permalink
View parent nodes from list
Browse files Browse the repository at this point in the history
  • Loading branch information
Danny van Wijk committed Mar 6, 2024
1 parent 4f28062 commit 5b3f3fe
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 23 deletions.
44 changes: 43 additions & 1 deletion src/Kunstmaan/NodeBundle/AdminList/NodeAdminListConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\QueryBuilder;
use Kunstmaan\AdminBundle\Entity\EntityInterface;
use Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface;
use Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper;
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionDefinition;
Expand Down Expand Up @@ -46,6 +47,8 @@ class NodeAdminListConfigurator extends AbstractDoctrineORMAdminListConfigurator
*/
protected $authorizationChecker;

private ?Node $node = null;

/**
* @param EntityManager $em The entity
* manager
Expand All @@ -54,7 +57,7 @@ class NodeAdminListConfigurator extends AbstractDoctrineORMAdminListConfigurator
* locale
* @param string $permission The permission
*/
public function __construct(EntityManager $em, AclHelper $aclHelper, $locale, $permission, AuthorizationCheckerInterface $authorizationChecker)
public function __construct(EntityManager $em, AclHelper $aclHelper, $locale, $permission, AuthorizationCheckerInterface $authorizationChecker, ?Node $node = null)
{
parent::__construct($em, $aclHelper);
$this->locale = $locale;
Expand All @@ -66,6 +69,7 @@ public function __construct(EntityManager $em, AclHelper $aclHelper, $locale, $p
'n'
)
);
$this->node = $node;
}

public function setDomainConfiguration(DomainConfigurationInterface $domainConfiguration)
Expand Down Expand Up @@ -111,6 +115,39 @@ public function buildListActions()
);
}

public function buildItemActions(): void
{
$locale = $this->locale;
$acl = $this->authorizationChecker;

$itemRoute = function (EntityInterface $item) use ($locale, $acl) {
if ($acl->isGranted(PermissionMap::PERMISSION_VIEW, $item->getNode())) {
return [
'path' => '_slug_preview',
'params' => ['_locale' => $locale, 'url' => $item->getUrl()],
];
}
};
$this->addSimpleItemAction('action.preview', $itemRoute, 'eye');

$nodeItemsRoute = function (EntityInterface $item) use ($acl) {
$node = $item->getNode();
if (!$acl->isGranted(PermissionMap::PERMISSION_VIEW, $node)) {
return null;
}

if ($node->getChildren()->count() === 0) {
return null;
}

return [
'path' => 'KunstmaanNodeBundle_nodes_items',
'params' => ['nodeId' => $node->getId()],
];
};
$this->addSimpleItemAction('action.list_nodes', $nodeItemsRoute, 'th-list');
}

/**
* Configure filters
*/
Expand Down Expand Up @@ -243,6 +280,11 @@ public function adaptQueryBuilder(QueryBuilder $queryBuilder)
->addOrderBy('b.updated', 'DESC')
->setParameter('lang', $this->locale);

if ($this->node instanceof Node) {
$queryBuilder->andWhere('n.parent = :parent')
->setParameter('parent', $this->node->getId());
}

if (!$this->domainConfiguration) {
return;
}
Expand Down
60 changes: 38 additions & 22 deletions src/Kunstmaan/NodeBundle/Controller/NodeAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use InvalidArgumentException;
use Kunstmaan\AdminBundle\Entity\BaseUser;
use Kunstmaan\AdminBundle\Entity\EntityInterface;
use Kunstmaan\AdminBundle\FlashMessages\FlashTypes;
use Kunstmaan\AdminBundle\Helper\CloneHelper;
use Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface;
Expand Down Expand Up @@ -126,26 +124,7 @@ public function indexAction(Request $request): Response
{
$this->init($request);

$nodeAdminListConfigurator = new NodeAdminListConfigurator(
$this->em,
$this->aclHelper,
$this->locale,
PermissionMap::PERMISSION_VIEW,
$this->authorizationChecker
);

$locale = $this->locale;
$acl = $this->authorizationChecker;
$itemRoute = function (EntityInterface $item) use ($locale, $acl) {
if ($acl->isGranted(PermissionMap::PERMISSION_VIEW, $item->getNode())) {
return [
'path' => '_slug_preview',
'params' => ['_locale' => $locale, 'url' => $item->getUrl()],
];
}
};
$nodeAdminListConfigurator->addSimpleItemAction('action.preview', $itemRoute, 'eye');
$nodeAdminListConfigurator->setDomainConfiguration($this->domainConfiguration);
$nodeAdminListConfigurator = $this->getAdminListConfigurator();
$nodeAdminListConfigurator->setShowAddHomepage($this->getParameter('kunstmaan_node.show_add_homepage') && $this->isGranted('ROLE_SUPER_ADMIN'));

/** @var AdminList $adminlist */
Expand All @@ -157,6 +136,43 @@ public function indexAction(Request $request): Response
]);
}

#[Route(path: '/{nodeId}/items', name: 'KunstmaanNodeBundle_nodes_items')]
public function itemsAction(Request $request, int $nodeId): Response
{
$this->init($request);

$node = $this->em->getRepository(Node::class)->find($nodeId);
if (!$node) {
throw $this->createNotFoundException(sprintf('No node found for id "%s"', $nodeId));
}

$nodeAdminListConfigurator = $this->getAdminListConfigurator($node);

/** @var AdminList $adminlist */
$adminlist = $this->container->get('kunstmaan_adminlist.factory')->createList($nodeAdminListConfigurator);
$adminlist->bindRequest($request);

return $this->render('@KunstmaanNode/Admin/list.html.twig', [
'adminlist' => $adminlist,
]);
}

public function getAdminListConfigurator(?Node $node = null): NodeAdminListConfigurator
{
$nodeAdminListConfigurator = new NodeAdminListConfigurator(
$this->em,
$this->aclHelper,
$this->locale,
PermissionMap::PERMISSION_VIEW,
$this->authorizationChecker,
$node
);

$nodeAdminListConfigurator->setDomainConfiguration($this->domainConfiguration);

return $nodeAdminListConfigurator;
}

/**
* @param int $id The node id
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ action:
addsubpage: "Add subpage"
duplicate: "Duplicate"
duplicate_with_children: "Duplicate with sub pages"
list_nodes: "List nodes"

modal:
sourcelanguage: "Source language"
Expand Down

0 comments on commit 5b3f3fe

Please sign in to comment.