['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, '/'); } }