['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']]),
new TwigFunction('render_actions', [$this, 'renderActions'], ['is_safe' => ['html']]),
new TwigFunction('render_cell', [$this, 'renderCell'], ['is_safe' => ['html']]),
new TwigFunction('get_avatar_url', [$this, 'getAvatarUrl'], ['is_safe' => ['html']]),
new TwigFunction('get_avatar', [$this, 'getAvatar'], ['is_safe' => ['html']]),
// Access functions
new TwigFunction('can', [$this, 'can'], ['is_safe' => ['html']]),
new TwigFunction('is_role', [$this, 'isRole'], ['is_safe' => ['html']]),
new TwigFunction('is_owner', [$this, 'isOwner'], ['is_safe' => ['html']]),
new TwigFunction('is_admin', [$this, 'isAdmin'], ['is_safe' => ['html']]),
new TwigFunction('is_manager', [$this, 'isManager'], ['is_safe' => ['html']]),
new TwigFunction('current_role', [$this, 'currentRole'], ['is_safe' => ['html']]),
new TwigFunction('role_label', [$this, 'roleLabel'], ['is_safe' => ['html']]),
new TwigFunction('can_view', [$this, 'canView'], ['is_safe' => ['html']]),
new TwigFunction('can_create', [$this, 'canCreate'], ['is_safe' => ['html']]),
new TwigFunction('can_edit', [$this, 'canEdit'], ['is_safe' => ['html']]),
new TwigFunction('can_delete', [$this, 'canDelete'], ['is_safe' => ['html']]),
new TwigFunction('can_manage_users', [$this, 'canManageUsers'], ['is_safe' => ['html']]),
// Role & Status badge functions
new TwigFunction('role_badge', [$this, 'roleBadge'], ['is_safe' => ['html']]),
new TwigFunction('status_badge', [$this, 'statusBadge'], ['is_safe' => ['html']]),
new TwigFunction('get_all_roles', [$this, 'getAllRoles'], ['is_safe' => ['html']]),
// System role functions (superadmin)
new TwigFunction('is_superadmin', [$this, 'isSuperadmin'], ['is_safe' => ['html']]),
new TwigFunction('is_system_admin', [$this, 'isSystemAdmin'], ['is_safe' => ['html']]),
new TwigFunction('get_system_role', [$this, 'getSystemRole'], ['is_safe' => ['html']]),
// Module subscription functions
new TwigFunction('is_module_active', [$this, 'isModuleActive'], ['is_safe' => ['html']]),
new TwigFunction('is_module_available', [$this, 'isModuleAvailable'], ['is_safe' => ['html']]),
];
}
// ========================================
// Access Functions для Twig
// ========================================
public function can(string $action, string $resource): bool
{
return service('access')->can($action, $resource);
}
public function isRole($roles): bool
{
return service('access')->isRole($roles);
}
public function isOwner(): bool
{
return service('access')->isRole(\App\Services\AccessService::ROLE_OWNER);
}
public function isAdmin(): bool
{
$role = service('access')->getCurrentRole();
return $role === \App\Services\AccessService::ROLE_ADMIN
|| $role === \App\Services\AccessService::ROLE_OWNER;
}
public function isManager(): bool
{
return service('access')->isManagerOrHigher();
}
public function currentRole(): ?string
{
return service('access')->getCurrentRole();
}
public function roleLabel(string $role): string
{
return service('access')->getRoleLabel($role);
}
public function canView(string $resource): bool
{
return service('access')->can(\App\Services\AccessService::PERMISSION_VIEW, $resource);
}
public function canCreate(string $resource): bool
{
return service('access')->can(\App\Services\AccessService::PERMISSION_CREATE, $resource);
}
public function canEdit(string $resource): bool
{
return service('access')->can(\App\Services\AccessService::PERMISSION_EDIT, $resource);
}
public function canDelete(string $resource): bool
{
return service('access')->can(\App\Services\AccessService::PERMISSION_DELETE, $resource);
}
public function canManageUsers(): bool
{
return service('access')->canManageUsers();
}
// ========================================
// Role & Status Badge Functions
// ========================================
public function roleBadge(string $role): string
{
$colors = [
'owner' => 'bg-primary',
'admin' => 'bg-info',
'manager' => 'bg-success',
'guest' => 'bg-secondary',
];
$labels = [
'owner' => 'Владелец',
'admin' => 'Администратор',
'manager' => 'Менеджер',
'guest' => 'Гость',
];
$color = $colors[$role] ?? 'bg-secondary';
$label = $labels[$role] ?? $role;
return '' . esc($label) . '';
}
// ========================================
// System Role Functions (superadmin)
// ========================================
public function isSuperadmin(): bool
{
return service('access')->isSuperadmin();
}
public function isSystemAdmin(): bool
{
return service('access')->isSystemAdmin();
}
public function getSystemRole(): ?string
{
return service('access')->getSystemRole();
}
// ========================================
// Module Subscription Functions
// ========================================
public function isModuleActive(string $moduleCode): bool
{
$orgId = session()->get('active_org_id');
if (!$orgId) {
return false;
}
$subscriptionService = new \App\Services\ModuleSubscriptionService();
return $subscriptionService->isModuleActive($moduleCode, $orgId);
}
public function isModuleAvailable(string $moduleCode): bool
{
$orgId = session()->get('active_org_id');
if (!$orgId) {
return false;
}
$subscriptionService = new \App\Services\ModuleSubscriptionService();
return $subscriptionService->isModuleAvailable($moduleCode, $orgId);
}
public function statusBadge(string $status): string
{
$colors = [
'active' => 'bg-success',
'pending' => 'bg-warning text-dark',
'blocked' => 'bg-danger',
];
$labels = [
'active' => 'Активен',
'pending' => 'Ожидает',
'blocked' => 'Заблокирован',
];
$color = $colors[$status] ?? 'bg-secondary';
$label = $labels[$status] ?? $status;
return '' . esc($label) . '';
}
public function getAvatarUrl($avatar = null, $size = 32): string
{
if (empty($avatar)) {
return '';
}
return base_url('/uploads/avatars/' . $avatar);
}
public function getAvatar($user = null, $size = 32, $class = ''): string
{
if (!$user) {
$session = session();
$userId = $session->get('user_id');
if (!$userId) {
return '';
}
$userModel = new \App\Models\UserModel();
$user = $userModel->find($userId);
}
$name = $user['name'] ?? 'U';
$avatar = $user['avatar'] ?? null;
if ($avatar) {
$url = base_url('/uploads/avatars/' . $avatar);
$style = "width: {$size}px; height: {$size}px; object-fit: cover; border-radius: 50%;";
return '';
}
// Генерируем фон на основе имени
$colors = ['667eea', '764ba2', 'f093fb', 'f5576c', '4facfe', '00f2fe'];
$color = $colors[crc32($name) % count($colors)];
$initial = strtoupper(substr($name, 0, 1));
$style = "width: {$size}px; height: {$size}px; background: linear-gradient(135deg, #{$color} 0%, #{$color}dd 100%); border-radius: 50%; display: flex; align-items: center; justify-content: center; color: white; font-weight: bold; font-size: " . ($size / 2) . "px;";
return '