<?php
use App\Core\RoleMatrix;

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}
//require_once BASE_PATH . '/app/views/partials/sidebar_translate.php';
//require_once BASE_PATH . '/app/helpers/easychair_translate.php';

if (defined('BASE_PATH') && file_exists(BASE_PATH . '/app/views/partials/i18n_sidebar_labels.php')) {
    require_once BASE_PATH . '/app/views/partials/i18n_sidebar_labels.php';
}

/*
-----------------------------------------
FORCE ROLE RESTORE (CRITICAL)
-----------------------------------------
*/

if (!isset($_SESSION['role'])) {
    $_SESSION['role'] = $_SESSION['conference_role'] ?? 'author';
}

/*
-----------------------------------------
SUPERADMIN DETECTION
-----------------------------------------
*/

$isSuper = (
    ($_SESSION['role'] === 'super_admin') ||
    (($_SESSION['user']['role_id'] ?? 0) == 1)
);
?>
<?php
// Pack511: enforce automatic conference scope for non-superadmin layouts.
if (defined('BASE_PATH') && file_exists(BASE_PATH . '/app/helpers/conference_scope.php')) {
    require_once BASE_PATH . '/app/helpers/conference_scope.php';
}

if (defined('BASE_PATH') && file_exists(BASE_PATH . '/app/helpers/org_scope.php')) {
    require_once BASE_PATH . '/app/helpers/org_scope.php';
}

if (function_exists('ec_scope_role') && ec_scope_role() !== 'super_admin' && function_exists('ec_scope_auto_select_conference')) {
    ec_scope_auto_select_conference(false);
}
?>




<?php
//use App\Core\RoleMatrix;
// Pack503I: final admin output RBAC filter.
if (!function_exists('ec_admin_output_rbac_filter')) {
    function ec_admin_output_rbac_filter(string $html): string
    {
        if (session_status() !== PHP_SESSION_ACTIVE) {
            @session_start();
        }

/*
-----------------------------------------
BACKUP-COMPAT ROLE FIX
-----------------------------------------
*/

       if (defined('BASE_PATH') && file_exists(BASE_PATH . '/app/helpers/permissions.php')) {
            require_once BASE_PATH . '/app/helpers/permissions.php';
        }

        $role = (string)($_SESSION['conference_role'] ?? $_SESSION['role'] ?? $_SESSION['user_role'] ?? '');

        if ($role === 'super_admin') {
            return $html;
        }

        $blocked = [
            '/easychair/admin/email-templates',
            '/easychair/admin/test-email',
            '/easychair/admin/rbac-matrix',
            '/admin/email-templates',
            '/admin/test-email',
            '/admin/rbac-matrix',
        ];

        foreach ($blocked as $url) {
            $full = str_starts_with($url, '/easychair/') ? $url : '/easychair' . $url;
            $allowed = function_exists('ec_can_menu') ? ec_can_menu($full) : false;

            if ($allowed) {
                continue;
            }

            // Remove common anchor wrappers containing the denied href.
            $q = preg_quote($url, '~');

            $patterns = [
                '~<li\b[^>]*>\s*<a\b[^>]*href=["\'][^"\']*' . $q . '[^"\']*["\'][\s\S]*?</a>\s*</li>~i',
                '~<a\b[^>]*href=["\'][^"\']*' . $q . '[^"\']*["\'][\s\S]*?</a>~i',
                '~<div\b[^>]*>\s*<a\b[^>]*href=["\'][^"\']*' . $q . '[^"\']*["\'][\s\S]*?</a>\s*</div>~i',
            ];

            foreach ($patterns as $pat) {
                $html = preg_replace($pat, '', $html);
            }
        }

        return $html;
    }
}

if (!defined('PACK503I_OUTPUT_FILTER_STARTED')) {
    define('PACK503I_OUTPUT_FILTER_STARTED', true);
    ob_start('ec_admin_output_rbac_filter');
}
?>



<!-- PACK503I_ACTIVE_ADMIN_LAYOUT role=<?php echo htmlspecialchars((string)($_SESSION['conference_role'] ?? $_SESSION['role'] ?? $_SESSION['user_role'] ?? ''), ENT_QUOTES, 'UTF-8'); ?> -->
<?php require BASE_PATH . '/app/views/layouts/conference_role_layout.php'; ?>


<?php
if (defined('BASE_PATH') && file_exists(BASE_PATH . '/app/views/partials/toast.php')) {
    require BASE_PATH . '/app/views/partials/toast.php';
}
?>


<li class="nav-item">

<a
class="nav-link"
href="/easychair/admin/homepage-studio"
>

<i class="fa fa-globe"></i>

<span>

<?= $lang === 'ar'
? 'استوديو الصفحة الرئيسية'
: 'Homepage Studio' ?>

</span>

</a>

</li>
