-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Log): Added Twig Log extension to generate link to entities edit…
… pages
- Loading branch information
1 parent
7300cc3
commit 471a571
Showing
11 changed files
with
259 additions
and
85 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
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
119 changes: 119 additions & 0 deletions
119
lib/RoadizCoreBundle/src/TwigExtension/LogExtension.php
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,119 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\CoreBundle\TwigExtension; | ||
|
||
use RZ\Roadiz\CoreBundle\Entity\CustomForm; | ||
use RZ\Roadiz\CoreBundle\Entity\CustomFormField; | ||
use RZ\Roadiz\CoreBundle\Entity\Document; | ||
use RZ\Roadiz\CoreBundle\Entity\Log; | ||
use RZ\Roadiz\CoreBundle\Entity\Node; | ||
use RZ\Roadiz\CoreBundle\Entity\NodesSources; | ||
use RZ\Roadiz\CoreBundle\Entity\Setting; | ||
use RZ\Roadiz\CoreBundle\Entity\Tag; | ||
use RZ\Roadiz\CoreBundle\Entity\Translation; | ||
use RZ\Roadiz\CoreBundle\Entity\User; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Symfony\Component\Security\Core\Security; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFunction; | ||
|
||
final class LogExtension extends AbstractExtension | ||
{ | ||
private Security $security; | ||
private UrlGeneratorInterface $urlGenerator; | ||
|
||
public function __construct(Security $security, UrlGeneratorInterface $urlGenerator) | ||
{ | ||
$this->security = $security; | ||
$this->urlGenerator = $urlGenerator; | ||
} | ||
|
||
public function getFunctions(): array | ||
{ | ||
return [ | ||
new TwigFunction('log_entity_edit_path', [$this, 'getEditPath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]), | ||
]; | ||
} | ||
|
||
public function getEditPath(?object $log): ?string | ||
{ | ||
if (!($log instanceof Log)) { | ||
return null; | ||
} | ||
|
||
switch ($log->getEntityClass()) { | ||
case Node::class: | ||
case NodesSources::class: | ||
if ( | ||
$this->security->isGranted('ROLE_ACCESS_NODES') && | ||
isset($log->getAdditionalData()['node_id']) && | ||
isset($log->getAdditionalData()['translation_id']) | ||
) { | ||
return $this->urlGenerator->generate('nodesEditSourcePage', [ | ||
'nodeId' => $log->getAdditionalData()['node_id'], | ||
'translationId' => $log->getAdditionalData()['translation_id'], | ||
]); | ||
} | ||
break; | ||
case Tag::class: | ||
if ( | ||
$this->security->isGranted('ROLE_ACCESS_TAGS') | ||
) { | ||
return $this->urlGenerator->generate('tagsEditPage', [ | ||
'tagId' => $log->getEntityId(), | ||
]); | ||
} | ||
break; | ||
case Document::class: | ||
if ( | ||
$this->security->isGranted('ROLE_ACCESS_DOCUMENTS') | ||
) { | ||
return $this->urlGenerator->generate('documentsEditPage', [ | ||
'documentId' => $log->getEntityId(), | ||
]); | ||
} | ||
break; | ||
case User::class: | ||
if ( | ||
$this->security->isGranted('ROLE_ACCESS_USERS') | ||
) { | ||
return $this->urlGenerator->generate('usersEditPage', [ | ||
'userId' => $log->getEntityId(), | ||
]); | ||
} | ||
break; | ||
case CustomForm::class: | ||
if ( | ||
$this->security->isGranted('ROLE_ACCESS_CUSTOMFORMS') | ||
) { | ||
return $this->urlGenerator->generate('customFormsEditPage', [ | ||
'id' => $log->getEntityId(), | ||
]); | ||
} | ||
break; | ||
case Translation::class: | ||
if ( | ||
$this->security->isGranted('ROLE_ACCESS_TRANSLATIONS') | ||
) { | ||
return $this->urlGenerator->generate('translationsEditPage', [ | ||
'translationId' => $log->getEntityId(), | ||
]); | ||
} | ||
break; | ||
case Setting::class: | ||
if ( | ||
$this->security->isGranted('ROLE_ACCESS_SETTINGS') | ||
) { | ||
return $this->urlGenerator->generate('settingsEditPage', [ | ||
'settingId' => $log->getEntityId(), | ||
]); | ||
} | ||
break; | ||
|
||
} | ||
|
||
return null; | ||
} | ||
} |
Oops, something went wrong.