Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NodeBundle] Undo deleting pages #2706

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Kunstmaan\NodeBundle\AdminList;

use Doctrine\ORM\QueryBuilder;
use Kunstmaan\AdminListBundle\AdminList\FilterType\ORM\DateFilterType;
use Kunstmaan\AdminListBundle\AdminList\FilterType\ORM\StringFilterType;

class DeletedNodeAdminListConfigurator extends NodeAdminListConfigurator
{
public function buildFilters()
{
$this
->addFilter('title', new StringFilterType('title'), 'kuma_node.admin.list.filter.title')
->addFilter('created', new DateFilterType('created'), 'kuma_node.admin.list.filter.created_at')
->addFilter('updated', new DateFilterType('updated'), 'kuma_node.admin.list.filter.updated_at')
;
}

public function buildFields()
{
$this
->addField('title', 'kuma_node.admin.list.header.title', true, '@KunstmaanNode/Admin/title.html.twig')
->addField('created', 'kuma_node.admin.list.header.created_at', true)
->addField('updated', 'kuma_node.admin.list.header.updated_at', true)
;
}

public function adaptQueryBuilder(QueryBuilder $queryBuilder)
{
parent::adaptQueryBuilder($queryBuilder);

$queryBuilder->setParameter('deleted', 1);
}
}
16 changes: 10 additions & 6 deletions src/Kunstmaan/NodeBundle/AdminList/NodeAdminListConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Kunstmaan\NodeBundle\AdminList;

use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
use Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface;
use Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper;
Expand Down Expand Up @@ -62,14 +64,14 @@ public function __construct(EntityManager $em, AclHelper $aclHelper, $locale, $p
$this->setPermissionDefinition(
new PermissionDefinition(
array($permission),
'Kunstmaan\NodeBundle\Entity\Node',
Node::class,
'n'
)
);
}

/**
* @param \Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface $domainConfiguration
* @param DomainConfigurationInterface $domainConfiguration
*/
public function setDomainConfiguration(DomainConfigurationInterface $domainConfiguration)
{
Expand Down Expand Up @@ -241,11 +243,13 @@ public function adaptQueryBuilder(QueryBuilder $queryBuilder)

$queryBuilder
->select('b,n')
->innerJoin('b.node', 'n', 'WITH', 'b.node = n.id')
->innerJoin('b.node', 'n', Join::WITH, 'b.node = n.id')
->andWhere('b.lang = :lang')
->andWhere('n.deleted = 0')
->addOrderBy('b.updated', 'DESC')
->setParameter('lang', $this->locale);
->andWhere('n.deleted = :deleted')
->addOrderBy('b.updated', Criteria::DESC)
->setParameter('lang', $this->locale)
->setParameter('deleted', 0)
;

if (!$this->domainConfiguration) {
return;
Expand Down
190 changes: 182 additions & 8 deletions src/Kunstmaan/NodeBundle/Controller/NodeAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMap;
use Kunstmaan\AdminBundle\Service\AclManager;
use Kunstmaan\AdminListBundle\AdminList\AdminList;
use Kunstmaan\AdminListBundle\AdminList\ListAction\SimpleListAction;
use Kunstmaan\NodeBundle\AdminList\DeletedNodeAdminListConfigurator;
use Kunstmaan\NodeBundle\AdminList\NodeAdminListConfigurator;
use Kunstmaan\NodeBundle\Entity\HasNodeInterface;
use Kunstmaan\NodeBundle\Entity\Node;
Expand Down Expand Up @@ -91,9 +93,16 @@ class NodeAdminController extends Controller
*/
protected $translator;

/** @var PageCloningHelper */
/**
* @var PageCloningHelper
*/
private $pageCloningHelper;

/**
* @var bool
*/
private $isUndoDeletingNodesEnabled;

/**
* init
*
Expand All @@ -110,6 +119,9 @@ protected function init(Request $request)
$this->nodePublisher = $this->container->get('kunstmaan_node.admin_node.publisher');
$this->translator = $this->container->get('translator');
$this->pageCloningHelper = $this->container->get(PageCloningHelper::class);
$this->isUndoDeletingNodesEnabled = $this->container->getParameter(
'kunstmaan_node.kunstmaan_node.enable_undo_deleting_nodes'
);
}

/**
Expand Down Expand Up @@ -141,17 +153,37 @@ public function indexAction(Request $request)
'params' => ['_locale' => $locale, 'url' => $item->getUrl()],
);
}

return null;
};

$nodeAdminListConfigurator->addSimpleItemAction('action.preview', $itemRoute, 'eye');
$nodeAdminListConfigurator->setDomainConfiguration($this->get('kunstmaan_admin.domain_configuration'));
$nodeAdminListConfigurator->setShowAddHomepage($this->getParameter('kunstmaan_node.show_add_homepage') && $this->isGranted('ROLE_SUPER_ADMIN'));
$nodeAdminListConfigurator->setShowAddHomepage(
$this->getParameter('kunstmaan_node.show_add_homepage') && $this->isGranted('ROLE_SUPER_ADMIN')
);

$this->addViewDeletedNodesAction($nodeAdminListConfigurator);

return $this->renderAdminList($request, $nodeAdminListConfigurator);
}

/** @var AdminList $adminlist */
$adminlist = $this->get('kunstmaan_adminlist.factory')->createList($nodeAdminListConfigurator);
$adminlist->bindRequest($request);
private function addViewDeletedNodesAction(NodeAdminListConfigurator $nodeAdminListConfigurator): void
{
if (!$this->isUndoDeletingNodesEnabled) {
return;
}

return array(
'adminlist' => $adminlist,
$nodeAdminListConfigurator->addListAction(
new SimpleListAction(
[
'path' => 'KunstmaanNodeBundle_deleted_nodes',
'params' => [],
],
'deleted_pages.view_action',
null,
'@KunstmaanAdmin/Settings/button_resolve_all.html.twig'
)
);
}

Expand Down Expand Up @@ -348,7 +380,6 @@ public function unPublishAction(Request $request, $id)
$node = $this->em->getRepository(Node::class)->find($id);

$nodeTranslation = $node->getNodeTranslation($this->locale, true);
$request = $this->get('request_stack')->getCurrentRequest();
$this->nodePublisher->chooseHowToUnpublish($request, $nodeTranslation, $this->translator);

return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $node->getId())));
Expand Down Expand Up @@ -452,6 +483,101 @@ public function deleteAction(Request $request, $id)
return $response;
}

/**
* @Route("/deleted", name="KunstmaanNodeBundle_deleted_nodes")
* @Template("@KunstmaanNode/Admin/deleted_list.html.twig")
*/
public function deletedNodesAction(Request $request): array
{
$this->init($request);

if (!$this->isUndoDeletingNodesEnabled) {
throw $this->createAccessDeniedException();
}

$nodeAdminListConfigurator = new DeletedNodeAdminListConfigurator(
$this->em,
$this->aclHelper,
$this->locale,
PermissionMap::PERMISSION_DELETE,
$this->authorizationChecker
);
$nodeAdminListConfigurator->addListAction(
new SimpleListAction(
[
'path' => 'KunstmaanNodeBundle_nodes',
'params' => [],
],
'pages.view_action',
null,
'@KunstmaanAdmin/Settings/button_resolve_all.html.twig'
)
);
$nodeAdminListConfigurator->setDomainConfiguration($this->get('kunstmaan_admin.domain_configuration'));
$locale = $this->locale;
$acl = $this->authorizationChecker;

$nodeAdminListConfigurator->addSimpleItemAction(
'action.undo_delete',
function (EntityInterface $item) use ($locale, $acl) {
if ($acl->isGranted(PermissionMap::PERMISSION_DELETE, $item->getNode())) {
return [
'path' => 'KunstmaanNodeBundle_nodes_delete_undo',
'params' => [
'_locale' => $locale,
'id' => $item->getNode()->getId(),
],
];
}

return null;
},
'fa fa-undo',
'@KunstmaanNode\Admin\undo_delete_button.html.twig'
);

return $this->renderAdminList($request, $nodeAdminListConfigurator);
}

/**
* @Route(
* "/{id}/delete/undo",
* requirements={"id" = "\d+"},
* name="KunstmaanNodeBundle_nodes_delete_undo",
* )
*/
public function undoDeleteAction(Request $request, int $id): RedirectResponse
{
$this->init($request);

if (!$this->isUndoDeletingNodesEnabled) {
throw $this->createAccessDeniedException();
}

/* @var Node $node */
$node = $this->em->getRepository(Node::class)->find($id);

try {
$this->undoDeleteNode($node);

$this->em->flush();

$this->addFlash(
FlashTypes::SUCCESS,
$this->get('translator')->trans('kuma_node.admin.undo_delete.flash.success')
);
} catch (AccessDeniedException $exception) {
$this->addFlash(
FlashTypes::SUCCESS,
$this->get('translator')->trans('kuma_node.admin.undo_delete.flash.error')
);
}

return $this->redirectToRoute(
'KunstmaanNodeBundle_deleted_nodes'
);
}

/**
* @Route(
* "/{id}/duplicate",
Expand Down Expand Up @@ -882,6 +1008,13 @@ public function editAction(Request $request, $id, $subaction)
$nodeVersion = $draftNodeVersion;
$page = $nodeVersion->getRef($this->em);
} else {
if (!empty($request->request->get('undo_delete'))) {
$node->setDeleted(false);

$this->em->persist($node);
$this->em->flush();
}

if ($request->getMethod() == 'POST') {
$nodeVersionIsLocked = $this->isNodeVersionLocked($nodeTranslation, true);

Expand Down Expand Up @@ -1220,6 +1353,34 @@ private function deleteNodeChildren(
}
}

private function undoDeleteNode(Node $node): void
{
if (!$node->isDeleted()) {
return;
}

$this->denyAccessUnlessGranted(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a early return here if the node is not deleted. A possible case could be that you delete a parent with nested children and already restore a child before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've applied this

PermissionMap::PERMISSION_DELETE,
$node
);

$node->setDeleted(false);

foreach ($node->getNodeTranslations() as $nodeTranslation) {
if (!$nodeTranslation instanceof NodeTranslation) {
continue;
}

$this->nodePublisher->unPublish($nodeTranslation);
}

$this->em->persist($node);

foreach ($node->getChildren() as $child) {
$this->undoDeleteNode($child);
}
}

/**
* @param Request $request
* @param string $type
Expand Down Expand Up @@ -1321,4 +1482,17 @@ private function dispatch($event, string $eventName)

return $eventDispatcher->dispatch($eventName, $event);
}

private function renderAdminList(
Request $request,
NodeAdminListConfigurator $nodeAdminListConfigurator
): array {
/** @var AdminList $adminList */
$adminList = $this->get('kunstmaan_adminlist.factory')->createList($nodeAdminListConfigurator);
$adminList->bindRequest($request);

return [
'adminlist' => $adminList,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function getConfigTreeBuilder()
->booleanNode('show_add_homepage')->defaultTrue()->end()
->booleanNode('show_duplicate_with_children')->defaultFalse()->end()
->booleanNode('enable_export_page_template')->defaultFalse()->end()
->booleanNode('enable_undo_deleting_nodes')->defaultTrue()->end()
->arrayNode('lock')
->addDefaultsIfNotSet()
->canBeEnabled()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('kunstmaan_node.show_add_homepage', $config['show_add_homepage']);
$container->setParameter('kunstmaan_node.show_duplicate_with_children', $config['show_duplicate_with_children']);
$container->setParameter('kunstmaan_node.enable_export_page_template', $config['enable_export_page_template']);
$container->setParameter('kunstmaan_node.enable_undo_deleting_nodes', $config['enable_undo_deleting_nodes']);
$container->setParameter('kunstmaan_node.lock_check_interval', $config['lock']['check_interval']);
$container->setParameter('kunstmaan_node.lock_threshold', $config['lock']['threshold']);
$container->setParameter('kunstmaan_node.lock_enabled', $config['lock']['enabled']);
Expand Down