<?php
namespace App\TwigExtensions;
use Empire\Core\Core;
use Empire\Core\User;
use Empire\Core\PageCategory;
use Empire\Legacy\cgt;
use Twig\Extension\AbstractExtension;
use Twig\Markup;
use Twig\TwigFilter;
use Twig\TwigFunction;
class TwigExtension extends AbstractExtension
{
/**
* {@inheritdoc}
*/
public function getFilters(): array
{
return [
new TwigFilter('cgt', function ($time, $format) {
return cgt::toCGTString(cgt::evaluateString($time)->cgtObj, $format);
}),
new TwigFilter('cgt_*', function($type, $time) {
return cgt::evaluateString($time)->$type;
})
];
}
/**
* {@inheritdoc}
*/
public function getFunctions(): array
{
return [
new TwigFunction('core_get', function ($var) { return Core::get($var); }),
new TwigFunction('core_flash', function () { return Core::flash(); }),
new TwigFunction('core_user', function () { return Core::user(); }),
new TwigFunction('getUser', function ($var) {
return \Empire\Core\User::load([$var]);
}),
new TwigFunction('sideMenu', function () {
$return = "";
if(!Core::get('secure')) {
$ing = PageCategory::loadAll(\Empire\Core\Page::PAGE_TYPE_PUBLIC);
$cat = $ing[0];
foreach($cat->subCategories() as $scat) {
$return .= "<div class='menu'>";
$return .= "<h2>" . $scat->getName() . "</h2>\n";
$return .= "<ul>\n";
foreach ($scat->pages() as $page) {
if($page->visible) {
$active = $page->url == ltrim(Core::get('fullPage'), '/') ? 'class="menu-active-link"' : '';
if (strpos($page->url, "http://") !== 0 && strpos($page->url, "https://") !== 0 ) {
$return .= "\t<li><a href='" . Core::get('base_url') . $page->url . "' $active><span><i class='fa fa-caret-right'></i></span> " . $page->title . "</a></li>\n";
} else {
$return .= "\t<li><a href='" . $page->url . "' $active><span><i class='fa fa-caret-right'></i></span> " . $page->title . "</a></li>\n";
}
}
}
$return .= "</ul>\n</div>";
}
} elseif (Core::get('secure')) {
$irms = PageCategory::loadAll(\Empire\Core\Page::PAGE_TYPE_SECURE);
foreach ($irms as $cat) {
$pText = "";
foreach($cat->subCategories() as $scat) {
foreach ($scat->pages() as $page) {
if($page->visible && $page->grantsAccess(Core::user())) {
$active = $page->url === ltrim(Core::get('fullPage'), '/') ? 'class="menu-active-link"' : '';
if(strpos($page->url, "http://") !== 0 && strpos($page->url, "https://") !== 0 ) {
$pText .= "\t<li><a href='" . Core::get('base_url') . $page->url . "' $active><span><i class='fa fa-caret-right'></i></span> " . $page->title . "</a></li>\n";
} else {
$pText .= "\t<li><a href='" . $page->url . "' $active><span><i class='fa fa-caret-right'></i></span> " . $page->title . "</a></li>\n";
}
}
}
if(strlen($pText) > 0) {
$return .= "<div class='menu'>";
$return .= "<h2>" . $scat->name . "</h2>\n";
$return .= "<ul>" . $pText . "</ul>\n</div>";
}
}
}
}
return new Markup($return, 'UTF-8');
}) ];
}
}