Skip to content

Commit

Permalink
Add toggler toolbar action (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-rath authored Mar 8, 2021
1 parent d12a4ef commit 3cbc8b4
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 19 deletions.
7 changes: 7 additions & 0 deletions Admin/RedirectAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Sulu\Bundle\AdminBundle\Admin\Admin;
use Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItem;
use Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItemCollection;
use Sulu\Bundle\AdminBundle\Admin\View\TogglerToolbarAction;
use Sulu\Bundle\AdminBundle\Admin\View\ToolbarAction;
use Sulu\Bundle\AdminBundle\Admin\View\ViewBuilderFactoryInterface;
use Sulu\Bundle\AdminBundle\Admin\View\ViewCollection;
Expand Down Expand Up @@ -68,6 +69,12 @@ public function configureViews(ViewCollection $viewCollection): void
$formToolbarActions = [
new ToolbarAction('sulu_admin.save'),
new ToolbarAction('sulu_admin.delete'),
new TogglerToolbarAction(
'sulu_redirect.enabled',
'enabled',
'enable',
'disable'
),
];

$listToolbarActions = [
Expand Down
51 changes: 44 additions & 7 deletions Controller/RedirectRouteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@

use Doctrine\ORM\EntityManagerInterface;
use FOS\RestBundle\Controller\Annotations\RouteResource;
use FOS\RestBundle\Routing\ClassResourceInterface;
use FOS\RestBundle\View\ViewHandlerInterface;
use HandcraftedInTheAlps\RestRoutingBundle\Routing\ClassResourceInterface;
use Sulu\Bundle\RedirectBundle\Admin\RedirectAdmin;
use Sulu\Bundle\RedirectBundle\Manager\RedirectRouteManagerInterface;
use Sulu\Bundle\RedirectBundle\Model\RedirectRouteInterface;
use Sulu\Bundle\RedirectBundle\Model\RedirectRouteRepositoryInterface;
use Sulu\Component\Rest\AbstractRestController;
use Sulu\Component\Rest\DoctrineRestHelper;
use Sulu\Component\Rest\Exception\EntityNotFoundException;
use Sulu\Component\Rest\Exception\RestException;
use Sulu\Component\Rest\ListBuilder\Doctrine\DoctrineListBuilderFactoryInterface;
use Sulu\Component\Rest\ListBuilder\FieldDescriptorInterface;
use Sulu\Component\Rest\ListBuilder\ListRepresentation;
Expand Down Expand Up @@ -77,7 +78,7 @@ public function getSecurityContext(): string
/**
* @var string
*/
protected $tagEntityName;
protected $redirectRouteEntityName;

public function __construct(
ViewHandlerInterface $viewHandler,
Expand All @@ -87,7 +88,7 @@ public function __construct(
EntityManagerInterface $entityManager,
RedirectRouteManagerInterface $redirectRouteManager,
RedirectRouteRepositoryInterface $redirectRouteRepository,
string $tagEntityName
string $redirectRouteEntityName
) {
parent::__construct($viewHandler);
$this->restHelper = $restHelper;
Expand All @@ -96,7 +97,7 @@ public function __construct(
$this->entityManager = $entityManager;
$this->redirectRouteManager = $redirectRouteManager;
$this->redirectRouteRepository = $redirectRouteRepository;
$this->tagEntityName = $tagEntityName;
$this->redirectRouteEntityName = $redirectRouteEntityName;
}

/**
Expand All @@ -108,7 +109,7 @@ public function cgetAction(Request $request)
{
/** @var FieldDescriptorInterface[] $fieldDescriptors */
$fieldDescriptors = $this->fieldDescriptor->getFieldDescriptors('redirect_routes');
$listBuilder = $this->factory->create($this->tagEntityName);
$listBuilder = $this->factory->create($this->redirectRouteEntityName);

$this->restHelper->initializeListBuilder($listBuilder, $fieldDescriptors);
$results = $listBuilder->execute();
Expand Down Expand Up @@ -141,6 +142,42 @@ public function postAction(Request $request)
return $this->handleView($this->view($redirectRoute));
}

public function postTriggerAction(Request $request, string $id): Response
{
$action = $request->get('action');

/** @var RedirectRouteInterface|null $redirectRoute */
$redirectRoute = $this->redirectRouteRepository->find($id);

if (null === $redirectRoute) {
throw new EntityNotFoundException($this->redirectRouteEntityName, $id);
}

try {
switch ($action) {
case 'enable':
$redirectRoute->setEnabled(true);
$this->entityManager->flush();

break;
case 'disable':
$redirectRoute->setEnabled(false);
$this->entityManager->flush();

break;
default:
throw new RestException('Unrecognized action: ' . $action);
}

// prepare view
$view = $this->view($redirectRoute, 200);
} catch (RestException $exception) {
$view = $this->view($exception->toArray(), 400);
}

return $this->handleView($view);
}

/**
* Returns single redirect-route.
*
Expand All @@ -154,7 +191,7 @@ public function getAction($id)
{
$entity = $this->redirectRouteRepository->find($id);
if (!$entity) {
throw new EntityNotFoundException($this->tagEntityName, $id);
throw new EntityNotFoundException($this->redirectRouteEntityName, $id);
}

return $this->handleView($this->view($entity));
Expand Down Expand Up @@ -192,7 +229,7 @@ public function deleteAction($id)
/** @var RedirectRouteInterface|null $redirectRoute */
$redirectRoute = $this->redirectRouteRepository->find($id);
if (!$redirectRoute) {
throw new EntityNotFoundException($this->tagEntityName, $id);
throw new EntityNotFoundException($this->redirectRouteEntityName, $id);
}

$this->redirectRouteManager->delete($redirectRoute);
Expand Down
12 changes: 1 addition & 11 deletions Resources/config/forms/redirect_route_details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,7 @@
<key>redirect_route_details</key>

<properties>
<property name="enabled" type="checkbox" colspan="2">
<meta>
<title>sulu_redirect.enabled</title>
</meta>
<params>
<param name="type" value="toggler"/>
<param name="default_value" value="true"/>
</params>
</property>

<property name="statusCode" type="single_select" colspan="10" mandatory="true">
<property name="statusCode" type="single_select" mandatory="true">
<meta>
<title>sulu_redirect.status_code</title>
</meta>
Expand Down
7 changes: 7 additions & 0 deletions Resources/config/routing_api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ sulu_redirect.redirect_route:
resource: sulu_redirect.redirect_route_controller
options:
expose: true

sulu_redirect.post_redirect-route_trigger:
path: /redirect-routes/{id}.{_format}
methods: POST
defaults:
_controller: sulu_redirect.redirect_route_controller::postTriggerAction
_format: json
30 changes: 30 additions & 0 deletions Tests/Functional/Controller/RedirectRouteControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,36 @@ public function testPostWithSourceHostAlreadyExists()
$this->assertHttpStatusCode(409, $response);
}

public function testPostTriggerActionEnable(): void
{
$response = $this->post(array_merge($this->defaultData, ['enabled' => false]));
$data = json_decode($response->getContent(), true);

$this->client->request('POST', self::BASE_URL . '/' . $data['id'], [
'action' => 'enable',
]);
$response = $this->client->getResponse();
$this->assertHttpStatusCode(200, $response);

$content = json_decode($response->getContent(), true);
$this->assertTrue($content['enabled']);
}

public function testPostTriggerActionDisable(): void
{
$response = $this->post(array_merge($this->defaultData, ['enabled' => true]));
$data = json_decode($response->getContent(), true);

$this->client->request('POST', self::BASE_URL . '/' . $data['id'], [
'action' => 'disable',
]);
$response = $this->client->getResponse();
$this->assertHttpStatusCode(200, $response);

$content = json_decode($response->getContent(), true);
$this->assertFalse($content['enabled']);
}

public function testGet()
{
$response = $this->post($this->defaultData);
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"symfony/config": "^4.3 || ^5.0",
"symfony/console": "^4.3 || ^5.0",
"symfony/http-foundation": "^4.3 || ^5.0",
"friendsofsymfony/rest-bundle": "^2.6 || ^3.0",
"handcraftedinthealps/rest-routing-bundle": "^1.0",
"friendsofsymfony/rest-bundle": "^2.8 || ^3.0",
"symfony/http-kernel": "^4.3 || ^5.0",
"doctrine/orm": "^2.5.3",
"symfony/event-dispatcher": "^4.3 || ^5.0",
Expand Down

0 comments on commit 3cbc8b4

Please sign in to comment.