summaryrefslogtreecommitdiff
path: root/src/Renderer/Twig/Extensions
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-09-23 19:13:19 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2018-09-23 20:11:37 +0200
commit66038eda14d5d4e624b6636a6156570e3e940e49 (patch)
tree6e4b7557b7d91786ef47f22f7ddef85eed1dfb42 /src/Renderer/Twig/Extensions
parent590adffa9316b98544cb8d67b03b80e44ba9c8b7 (diff)
parent9d34f371cb9c5ab0d60bd3158678b9cc9da6cc80 (diff)
Merge branch 'twig-templates'
Diffstat (limited to 'src/Renderer/Twig/Extensions')
-rw-r--r--src/Renderer/Twig/Extensions/Authentication.php40
-rw-r--r--src/Renderer/Twig/Extensions/Globals.php40
-rw-r--r--src/Renderer/Twig/Extensions/Legacy.php49
3 files changed, 128 insertions, 1 deletions
diff --git a/src/Renderer/Twig/Extensions/Authentication.php b/src/Renderer/Twig/Extensions/Authentication.php
new file mode 100644
index 00000000..6a72d825
--- /dev/null
+++ b/src/Renderer/Twig/Extensions/Authentication.php
@@ -0,0 +1,40 @@
+<?php
+
+namespace Engelsystem\Renderer\Twig\Extensions;
+
+use Twig_Extension as TwigExtension;
+use Twig_Function as TwigFunction;
+
+class Authentication extends TwigExtension
+{
+ /**
+ * @return TwigFunction[]
+ */
+ public function getFunctions()
+ {
+ return [
+ new TwigFunction('is_user', [$this, 'isAuthenticated']),
+ new TwigFunction('is_guest', [$this, 'isGuest']),
+ new TwigFunction('has_permission_to', [$this, 'checkAuth']),
+ ];
+ }
+
+ public function isAuthenticated()
+ {
+ global $user;
+
+ return !empty($user);
+ }
+
+ public function isGuest()
+ {
+ return !$this->isAuthenticated();
+ }
+
+ public function checkAuth($privilege)
+ {
+ global $privileges;
+
+ return in_array($privilege, $privileges);
+ }
+}
diff --git a/src/Renderer/Twig/Extensions/Globals.php b/src/Renderer/Twig/Extensions/Globals.php
index f9bffbc8..1a4df42c 100644
--- a/src/Renderer/Twig/Extensions/Globals.php
+++ b/src/Renderer/Twig/Extensions/Globals.php
@@ -2,6 +2,7 @@
namespace Engelsystem\Renderer\Twig\Extensions;
+use Carbon\Carbon;
use Twig_Extension as TwigExtension;
use Twig_Extension_GlobalsInterface as GlobalsInterface;
@@ -16,8 +17,45 @@ class Globals extends TwigExtension implements GlobalsInterface
{
global $user;
+ $eventConfig = $this->getEventConfig();
+ if (empty($eventConfig)) {
+ $eventConfig = [];
+ }
+
return [
- 'user' => isset($user) ? $user : [],
+ 'user' => isset($user) ? $user : [],
+ 'event_config' => $this->filterEventConfig($eventConfig),
];
}
+
+ /**
+ * @return array
+ * @codeCoverageIgnore
+ */
+ protected function getEventConfig()
+ {
+ return EventConfig();
+ }
+
+ /**
+ * @param $eventConfig
+ * @return mixed
+ */
+ protected function filterEventConfig($eventConfig)
+ {
+ array_walk($eventConfig, function (&$value, $key) {
+ if (is_null($value) || !in_array($key, [
+ 'buildup_start_date',
+ 'event_start_date',
+ 'event_end_date',
+ 'teardown_end_date',
+ ])) {
+ return;
+ }
+
+ $value = Carbon::createFromTimestamp($value);
+ });
+
+ return $eventConfig;
+ }
}
diff --git a/src/Renderer/Twig/Extensions/Legacy.php b/src/Renderer/Twig/Extensions/Legacy.php
new file mode 100644
index 00000000..79de32cb
--- /dev/null
+++ b/src/Renderer/Twig/Extensions/Legacy.php
@@ -0,0 +1,49 @@
+<?php
+
+namespace Engelsystem\Renderer\Twig\Extensions;
+
+use Engelsystem\Http\Request;
+use Twig_Extension as TwigExtension;
+use Twig_Function as TwigFunction;
+
+class Legacy extends TwigExtension
+{
+ /** @var Request */
+ protected $request;
+
+ /**
+ * @param Request $request
+ */
+ public function __construct(Request $request)
+ {
+ $this->request = $request;
+ }
+
+ /**
+ * @return TwigFunction[]
+ */
+ public function getFunctions()
+ {
+ $isSafeHtml = ['is_safe' => ['html']];
+ return [
+ new TwigFunction('menu', 'make_navigation', $isSafeHtml),
+ new TwigFunction('menuUserShiftState', 'User_shift_state_render', $isSafeHtml),
+ new TwigFunction('menuUserMessages', 'user_unread_messages', $isSafeHtml),
+ new TwigFunction('menuUserHints', 'header_render_hints', $isSafeHtml),
+ new TwigFunction('menuUserSubmenu', 'make_user_submenu', $isSafeHtml),
+ new TwigFunction('page', [$this, 'getPage']),
+ ];
+ }
+
+ /**
+ * @return string
+ */
+ public function getPage()
+ {
+ if ($this->request->has('p')) {
+ return $this->request->get('p');
+ }
+
+ return $this->request->path();
+ }
+}