Skip to content

Commit

Permalink
Implement ApplicationSettings entity
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasnatter committed Dec 15, 2020
1 parent f761e49 commit 4e553f9
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 3 deletions.
15 changes: 15 additions & 0 deletions config/forms/application_settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" ?>
<form xmlns="http://schemas.sulu.io/template/template"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://schemas.sulu.io/template/template http://schemas.sulu.io/template/form-1.0.xsd"
>
<key>application_settings</key>

<properties>
<property name="demobarText" type="text_line">
<meta>
<title>app.demobar_text</title>
</meta>
</property>
</properties>
</form>
5 changes: 5 additions & 0 deletions config/packages/app_application_settings_admin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sulu_admin:
resources:
application_settings:
routes:
detail: 'app.get_application-settings'
6 changes: 6 additions & 0 deletions config/routes_admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ app_albums_api:
prefix: /admin/api
resource: App\Controller\Admin\AlbumController
name_prefix: app.

app_application_settings_api:
type: rest
prefix: /admin/api
resource: App\Controller\Admin\ApplicationSettingsController
name_prefix: app.
81 changes: 81 additions & 0 deletions src/Admin/ApplicationSettingsAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace App\Admin;

use App\Entity\ApplicationSettings;
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\ToolbarAction;
use Sulu\Bundle\AdminBundle\Admin\View\ViewBuilderFactoryInterface;
use Sulu\Bundle\AdminBundle\Admin\View\ViewCollection;
use Sulu\Component\Security\Authorization\PermissionTypes;
use Sulu\Component\Security\Authorization\SecurityCheckerInterface;

class ApplicationSettingsAdmin extends Admin
{
const TAB_VIEW = 'app.application_settings';
const FORM_VIEW = 'app.application_settings.form';

private ViewBuilderFactoryInterface $viewBuilderFactory;
private SecurityCheckerInterface $securityChecker;

public function __construct(
ViewBuilderFactoryInterface $viewBuilderFactory,
SecurityCheckerInterface $securityChecker
) {
$this->viewBuilderFactory = $viewBuilderFactory;
$this->securityChecker = $securityChecker;
}

public function configureNavigationItems(NavigationItemCollection $navigationItemCollection): void
{
if ($this->securityChecker->hasPermission(ApplicationSettings::SECURITY_CONTEXT, PermissionTypes::EDIT)) {
$categoryItem = new NavigationItem('app.application');
$categoryItem->setPosition(0);
$categoryItem->setView(static::TAB_VIEW);

$navigationItemCollection->get(Admin::SETTINGS_NAVIGATION_ITEM)->addChild($categoryItem);
}
}

public function configureViews(ViewCollection $viewCollection): void
{
if ($this->securityChecker->hasPermission(ApplicationSettings::SECURITY_CONTEXT, PermissionTypes::EDIT)) {
$viewCollection->add(
// sulu will only load the existing entity if the path of the form includes an id attribute
$this->viewBuilderFactory->createResourceTabViewBuilder(static::TAB_VIEW, '/application-settings/:id')
->setResourceKey(ApplicationSettings::RESOURCE_KEY)
->setAttributeDefault('id', '-')
);

$viewCollection->add(
$this->viewBuilderFactory->createFormViewBuilder(static::FORM_VIEW, '/details')
->setResourceKey(ApplicationSettings::RESOURCE_KEY)
->setFormKey(ApplicationSettings::FORM_KEY)
->setTabTitle('sulu_admin.details')
->addToolbarActions([new ToolbarAction('sulu_admin.save')])
->setParent(static::TAB_VIEW)
);
}
}

/**
* @return mixed[]
*/
public function getSecurityContexts(): array
{
return [
self::SULU_ADMIN_SECURITY_SYSTEM => [
'Settings' => [
ApplicationSettings::SECURITY_CONTEXT => [
PermissionTypes::VIEW,
PermissionTypes::EDIT,
],
],
],
];
}
}
68 changes: 68 additions & 0 deletions src/Controller/Admin/ApplicationSettingsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use App\Entity\ApplicationSettings;
use Doctrine\ORM\EntityManagerInterface;
use FOS\RestBundle\View\ViewHandlerInterface;
use HandcraftedInTheAlps\RestRoutingBundle\Controller\Annotations\RouteResource;
use HandcraftedInTheAlps\RestRoutingBundle\Routing\ClassResourceInterface;
use Sulu\Component\Rest\AbstractRestController;
use Sulu\Component\Security\SecuredControllerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

/**
* @RouteResource("application-settings")
*/
class ApplicationSettingsController extends AbstractRestController implements ClassResourceInterface, SecuredControllerInterface
{
private EntityManagerInterface $entityManager;

public function __construct(
EntityManagerInterface $entityManager,
ViewHandlerInterface $viewHandler,
?TokenStorageInterface $tokenStorage = null
) {
$this->entityManager = $entityManager;

parent::__construct($viewHandler, $tokenStorage);
}

public function getAction(): Response
{
$applicationSettings = $this->entityManager->getRepository(ApplicationSettings::class)->findOneBy([]);

return $this->handleView($this->view($applicationSettings ?: new ApplicationSettings()));
}

public function putAction(Request $request): Response
{
$applicationSettings = $this->entityManager->getRepository(ApplicationSettings::class)->findOneBy([]);
if (!$applicationSettings) {
$applicationSettings = new ApplicationSettings();
$this->entityManager->persist($applicationSettings);
}

$this->mapDataToEntity($request->request->all(), $applicationSettings);
$this->entityManager->flush();

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

/**
* @param array<string, mixed> $data
*/
protected function mapDataToEntity(array $data, ApplicationSettings $entity): void
{
$entity->setDemobarText($data['demobarText']);
}

public function getSecurityContext(): string
{
return ApplicationSettings::SECURITY_CONTEXT;
}
}
53 changes: 53 additions & 0 deletions src/Entity/ApplicationSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use Sulu\Component\Persistence\Model\AuditableInterface;
use Sulu\Component\Persistence\Model\AuditableTrait;

/**
* @ORM\Entity()
* @ORM\Table(name="app_application_settings")
* @Serializer\ExclusionPolicy("all")
*/
class ApplicationSettings implements AuditableInterface
{
use AuditableTrait;

public const RESOURCE_KEY = 'application_settings';
public const FORM_KEY = 'application_settings';
public const SECURITY_CONTEXT = 'sulu.settings.application_settings';

/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*
* @Serializer\Expose()
*/
private ?int $id = null;

/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @Serializer\Expose()
*/
private ?string $demobarText = null;

public function getId(): ?int
{
return $this->id;
}

public function getDemobarText(): ?string
{
return $this->demobarText;
}

public function setDemobarText(?string $demobarText): void
{
$this->demobarText = $demobarText;
}
}
33 changes: 33 additions & 0 deletions src/Twig/ApplicationSettingsTwigExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Twig;

use App\Entity\ApplicationSettings;
use Doctrine\ORM\EntityManagerInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class ApplicationSettingsTwigExtension extends AbstractExtension
{
private EntityManagerInterface $entityManager;

public function __construct(
EntityManagerInterface $entityManager
) {
$this->entityManager = $entityManager;
}

public function getFunctions()
{
return [
new TwigFunction('load_application_settings', [$this, 'loadApplicationSettings']),
];
}

public function loadApplicationSettings(): ApplicationSettings
{
$applicationSettings = $this->entityManager->getRepository(ApplicationSettings::class)->findOneBy([]) ?? null;

return $applicationSettings ?: new ApplicationSettings();
}
}
1 change: 1 addition & 0 deletions templates/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
{# @see https://docs.sulu.io/en/2.1/reference/twig-extensions/functions/sulu_snippet_load_by_area.html #}
{% set webspaceSettings = sulu_snippet_load_by_area('webspace_settings') %}
{% set applicationSettings = load_application_settings() %}

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
Expand Down
2 changes: 1 addition & 1 deletion templates/includes/demobar.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</a>

<p class="demobar__title">
SULU DEMO
{{ applicationSettings.demobarText|default('SULU DEMO') }}
</p>

<a href="https://github.com/sulu/sulu-demo" target="_blank" class="demobar__github">
Expand Down
4 changes: 3 additions & 1 deletion translations/admin.de.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
"app.album_selection_label": "{count} {count, plural, =1 {Album} other {Alben}} ausgewählt",
"app.select_albums": "Alben auswählen",
"app.no_album_selected": "Kein Album ausgewählt",
"app.select_album": "Album auswählen"
"app.select_album": "Album auswählen",
"app.application": "Applikation",
"app.demobar_text": "Demobar Text"
}
4 changes: 3 additions & 1 deletion translations/admin.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
"app.album_selection_label": "{count} {count, plural, =1 {album} other {albums}} selected",
"app.select_albums": "Select albums",
"app.no_album_selected": "No album selected",
"app.select_album": "Select album"
"app.select_album": "Select album",
"app.application": "Application",
"app.demobar_text": "Demobar Text"
}

0 comments on commit 4e553f9

Please sign in to comment.