first commit
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
namespace App\Libraries\Twig;
|
||||
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
use Twig\TwigGlobal;
|
||||
use App\Models\OrganizationModel;
|
||||
use Config\Services;
|
||||
|
||||
class TwigGlobalsExtension extends AbstractExtension
|
||||
{
|
||||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
new TwigFunction('get_session', [$this, 'getSession'], ['is_safe' => ['html']]),
|
||||
new TwigFunction('get_current_org', [$this, 'getCurrentOrg'], ['is_safe' => ['html']]),
|
||||
new TwigFunction('get_alerts', [$this, 'getAlerts'], ['is_safe' => ['html']]),
|
||||
new TwigFunction('render_pager', [$this, 'renderPager'], ['is_safe' => ['html']]),
|
||||
new TwigFunction('is_active_route', [$this, 'isActiveRoute'], ['is_safe' => ['html']]),
|
||||
new TwigFunction('get_current_route', [$this, 'getCurrentRoute'], ['is_safe' => ['html']]),
|
||||
];
|
||||
}
|
||||
|
||||
public function getSession()
|
||||
{
|
||||
return session();
|
||||
}
|
||||
|
||||
public function getCurrentOrg()
|
||||
{
|
||||
$session = session();
|
||||
$activeOrgId = $session->get('active_org_id');
|
||||
|
||||
if ($activeOrgId) {
|
||||
$orgModel = new OrganizationModel();
|
||||
return $orgModel->find($activeOrgId);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getAlerts(): array
|
||||
{
|
||||
$session = session();
|
||||
|
||||
$alerts = [];
|
||||
$types = ['success', 'error', 'warning', 'info'];
|
||||
foreach ($types as $type) {
|
||||
if ($msg = $session->getFlashdata($type)) {
|
||||
$alerts[] = ['type' => $type, 'message' => $msg];
|
||||
}
|
||||
}
|
||||
|
||||
if ($validationErrors = $session->getFlashdata('errors')) {
|
||||
foreach ($validationErrors as $error) {
|
||||
$alerts[] = ['type' => 'error', 'message' => $error];
|
||||
}
|
||||
}
|
||||
|
||||
return $alerts;
|
||||
}
|
||||
|
||||
public function renderPager($pager)
|
||||
{
|
||||
if (!$pager) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $pager->links();
|
||||
}
|
||||
|
||||
/**
|
||||
* Проверяет, является ли текущий маршрут активным
|
||||
*/
|
||||
public function isActiveRoute($routes, $exact = false): bool
|
||||
{
|
||||
$currentRoute = $this->getCurrentRoute();
|
||||
|
||||
if (is_string($routes)) {
|
||||
$routes = [$routes];
|
||||
}
|
||||
|
||||
foreach ($routes as $route) {
|
||||
if ($exact) {
|
||||
// Точное совпадение
|
||||
if ($currentRoute === $route) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
// Частичное совпадение (начинается с)
|
||||
// Исключаем пустую строку, так как она совпадает с любым маршрутом
|
||||
if ($route === '') {
|
||||
// Для пустого маршрута проверяем только корень
|
||||
if ($currentRoute === '') {
|
||||
return true;
|
||||
}
|
||||
} elseif (strpos($currentRoute, $route) === 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Получает текущий маршрут без базового URL
|
||||
*/
|
||||
public function getCurrentRoute(): string
|
||||
{
|
||||
$uri = service('uri');
|
||||
$route = $uri->getRoutePath();
|
||||
|
||||
// Убираем начальный слеш если есть
|
||||
return ltrim($route, '/');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Libraries\Twig;
|
||||
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
|
||||
class TwigJsonDecodeExtension extends AbstractExtension
|
||||
{
|
||||
public function getFilters(): array
|
||||
{
|
||||
return [
|
||||
new TwigFilter('json_decode', [$this, 'jsonDecode']),
|
||||
];
|
||||
}
|
||||
|
||||
public function jsonDecode(string $json, bool $assoc = true, int $depth = 512, int $flags = 0)
|
||||
{
|
||||
return json_decode($json, $assoc, $depth, $flags);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user