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 9 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,38 @@
<?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
->where('b.lang = :lang')
->andWhere('n.deleted = 1')
;
}
}
10 changes: 6 additions & 4 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,10 +243,10 @@ 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')
->addOrderBy('b.updated', Criteria::DESC)
->setParameter('lang', $this->locale);

if (!$this->domainConfiguration) {
Expand Down
181 changes: 172 additions & 9 deletions src/Kunstmaan/NodeBundle/Controller/NodeAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManager;
use Esites\CustomerAdminBundle\Entity\User;
JZuidema marked this conversation as resolved.
Show resolved Hide resolved
use InvalidArgumentException;
use Kunstmaan\AdminBundle\Entity\BaseUser;
use Kunstmaan\AdminBundle\Entity\EntityInterface;
Expand All @@ -16,6 +17,9 @@
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMap;
use Kunstmaan\AdminBundle\Service\AclManager;
use Kunstmaan\AdminListBundle\AdminList\AdminList;
use Kunstmaan\AdminListBundle\AdminList\ItemAction\SimpleItemAction;
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 @@ -140,18 +144,28 @@ public function indexAction(Request $request)
'params' => ['_locale' => $locale, 'url' => $item->getUrl()],
);
}

return null;
};

$nodeAdminListConfigurator->addSimpleItemAction('action.preview', $itemRoute, 'eye');
$nodeAdminListConfigurator->addListAction(
new SimpleListAction(
[
'path' => 'KunstmaanNodeBundle_deleted_nodes',
'params' => [],
],
'deleted_pages.view_action',
null,
'@KunstmaanAdmin/Settings/button_resolve_all.html.twig'
)
);
$nodeAdminListConfigurator->setDomainConfiguration($this->get('kunstmaan_admin.domain_configuration'));
$nodeAdminListConfigurator->setShowAddHomepage($this->getParameter('kunstmaan_node.show_add_homepage') && $this->isGranted('ROLE_SUPER_ADMIN'));

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

return array(
'adminlist' => $adminlist,
$nodeAdminListConfigurator->setShowAddHomepage(
$this->getParameter('kunstmaan_node.show_add_homepage') && $this->isGranted('ROLE_SUPER_ADMIN')
);

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

/**
Expand Down Expand Up @@ -347,7 +361,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 @@ -451,6 +464,102 @@ public function deleteAction(Request $request, $id)
return $response;
}

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

$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'
)
);

JZuidema marked this conversation as resolved.
Show resolved Hide resolved
$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;
},
'undo',
'@KunstmaanAdmin/Settings/button_resolve_all.html.twig'
);

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

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

/* @var Node $node */
$node = $this->em->getRepository('KunstmaanNodeBundle:Node')->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 @@ -881,6 +990,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 @@ -1219,6 +1335,34 @@ private function deleteNodeChildren(
}
}

/**
* @param Node $node
*/
private function undoDeleteNode(
Node $node
) {
$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 @@ -1302,4 +1446,23 @@ private function renderNodeNotTranslatedPage(Node $node)
)
);
}

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

return [
'adminlist' => $adminList,
];
}
}
44 changes: 30 additions & 14 deletions src/Kunstmaan/NodeBundle/Helper/Menu/ActionsMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,20 +417,36 @@ public function createActionsMenu()
$node
)
) {
$menu->addChild(
'action.delete',
[
'linkAttributes' => [
'type' => 'button',
'class' => 'btn btn-default btn--raise-on-hover',
'onClick' => 'oldEdited = isEdited; isEdited=false',
'data-toggle' => 'modal',
'data-keyboard' => 'true',
'data-target' => '#delete-page-modal',
],
'extras' => ['renderType' => 'button'],
]
);
if (!$node->isDeleted()) {
$menu->addChild(
'action.delete',
[
'linkAttributes' => [
'type' => 'button',
'class' => 'btn btn-default btn--raise-on-hover',
'onClick' => 'oldEdited = isEdited; isEdited=false',
'data-toggle' => 'modal',
'data-keyboard' => 'true',
'data-target' => '#delete-page-modal',
],
'extras' => ['renderType' => 'button'],
]
);
} else {
$menu->addChild(
'action.undo_delete',
[
'linkAttributes' => [
'type' => 'button',
'class' => 'btn btn-default btn--raise-on-hover',
'data-toggle' => 'modal',
'data-keyboard' => 'true',
'data-target' => '#undo-delete-page-modal',
],
'extras' => ['renderType' => 'button'],
]
);
}
}

$this->dispatcher->dispatch(
Expand Down