-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement ApplicationSettings entity
- Loading branch information
1 parent
f761e49
commit 4e553f9
Showing
11 changed files
with
269 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
], | ||
], | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters