-
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
8d0fea4
commit 5b7d35b
Showing
10 changed files
with
275 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
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 | ||
{ | ||
public const TAB_VIEW = 'app.application_settings'; | ||
public 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,86 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Controller\Admin; | ||
|
||
use App\Entity\ApplicationSettings; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Sulu\Component\Security\SecuredControllerInterface; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
|
||
/** | ||
* @phpstan-type ApplicationSettingsData array{ | ||
* demobarText: string|null, | ||
* } | ||
*/ | ||
class ApplicationSettingsController extends AbstractController implements SecuredControllerInterface | ||
{ | ||
private EntityManagerInterface $entityManager; | ||
|
||
public function __construct( | ||
EntityManagerInterface $entityManager | ||
) { | ||
$this->entityManager = $entityManager; | ||
} | ||
|
||
/** | ||
* @Route("/admin/api/application-settings/{id}", methods={"GET"}, name="app.get_application_settings") | ||
*/ | ||
public function getAction(): Response | ||
{ | ||
$applicationSettings = $this->entityManager->getRepository(ApplicationSettings::class)->findOneBy([]); | ||
|
||
return $this->json($this->getDataForEntity($applicationSettings ?: new ApplicationSettings())); | ||
} | ||
|
||
/** | ||
* @Route("/admin/api/application-settings/{id}", methods={"PUT"}, name="app.put_application_settings") | ||
*/ | ||
public function putAction(Request $request): Response | ||
{ | ||
$applicationSettings = $this->entityManager->getRepository(ApplicationSettings::class)->findOneBy([]); | ||
if (!$applicationSettings) { | ||
$applicationSettings = new ApplicationSettings(); | ||
$this->entityManager->persist($applicationSettings); | ||
} | ||
|
||
/** @var ApplicationSettingsData $data */ | ||
$data = $request->toArray(); | ||
$this->mapDataToEntity($data, $applicationSettings); | ||
$this->entityManager->flush(); | ||
|
||
return $this->json($this->getDataForEntity($applicationSettings)); | ||
} | ||
|
||
/** | ||
* @return ApplicationSettingsData $data | ||
*/ | ||
protected function getDataForEntity(ApplicationSettings $entity): array | ||
{ | ||
return [ | ||
'demobarText' => $entity->getDemobarText(), | ||
]; | ||
} | ||
|
||
/** | ||
* @param ApplicationSettingsData $data | ||
*/ | ||
protected function mapDataToEntity(array $data, ApplicationSettings $entity): void | ||
{ | ||
$entity->setDemobarText($data['demobarText']); | ||
} | ||
|
||
public function getSecurityContext(): string | ||
{ | ||
return ApplicationSettings::SECURITY_CONTEXT; | ||
} | ||
|
||
public function getLocale(Request $request): ?string | ||
{ | ||
return $request->query->get('locale'); | ||
} | ||
} |
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,47 @@ | ||
<?php | ||
|
||
namespace App\Entity; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
use Sulu\Component\Persistence\Model\AuditableInterface; | ||
use Sulu\Component\Persistence\Model\AuditableTrait; | ||
|
||
/** | ||
* @ORM\Entity() | ||
* @ORM\Table(name="app_application_settings") | ||
*/ | ||
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") | ||
*/ | ||
private ?int $id = null; | ||
|
||
/** | ||
* @ORM\Column(type="string", length=255, nullable=true) | ||
*/ | ||
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