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 5 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
171 changes: 163 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 @@ -134,18 +136,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 @@ -445,6 +457,101 @@ 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'
);

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 @@ -849,6 +956,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 @@ -1189,6 +1303,28 @@ private function deleteNodeChildren(
}
}

/**
* @param Node $node
*
* @return void
*/
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);

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

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

/**
* @param Request $request
* @param string $type
Expand Down Expand Up @@ -1272,4 +1408,23 @@ private function renderNodeNotTranslatedPage(Node $node)
)
);
}

/**
* @param Request $request
* @var 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,
];
}
}
42 changes: 28 additions & 14 deletions src/Kunstmaan/NodeBundle/Helper/Menu/ActionsMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,20 +396,34 @@ 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' => 'submit',
'value' => 'undo_delete',
'name' => 'undo_delete',
],
'extras' => ['renderType' => 'button'],
]
);
}
}

$this->dispatcher->dispatch(
Expand Down
11 changes: 11 additions & 0 deletions src/Kunstmaan/NodeBundle/Resources/translations/messages.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ action:
delete: "Delete"
addsubpage: "Add subpage"
duplicate: "Duplicate"
undo_delete: "Undo deleting page"

modal:
sourcelanguage: "Source language"

pages:
view_action: 'View pages'
copynotavailable: "Please translate the parent page first"

kuma_node:
Expand Down Expand Up @@ -81,6 +83,10 @@ kuma_node:
success: The page has been edited
locked: The page is currently being edited by %users%
locked_success: An automatic backup has been created because the page is being edited by another user
undo_delete:
flash:
success: The deleting the page has been undone
error: There was an error while undoing deleting the page
new_page:
title:
default: New page
Expand Down Expand Up @@ -187,3 +193,8 @@ toolbar:
node:
title: 'Page title'
edit: 'Edit page'

deleted_pages:
title: 'Deleted pages'
additional_title: 'This list contains all the deleted pages, chronologically ordered by updated date.'
view_action: 'View deleted pages'