Files
pta/app/Views/layouts/footer.php

61 lines
2.5 KiB
PHP

<?php
/**
* Pied de page commun : publication de la configuration JavaScript et injection
* des scripts.
*
* C'EST LE SEUL PONT ENTRE PHP ET LE JAVASCRIPT. Deux objets globaux sont
* publiés dans un <script> inline :
* - window.PTA_CONFIG : jeton CSRF, profil d'accès, permissions et
* référentiel des motifs — données valables pour toute l'application ;
* - window.PTA_PAGE_CONFIG : modules à initialiser et paramètres d'URL déjà
* assainis — données propres à la page courante.
* core.js recopie le premier dans `PTA.config` ; page-bootstrap.js consomme le
* second.
*
* Ce <script> porte le nonce de la CSP : sans lui, il serait bloqué et TOUTE
* l'interface cesserait de fonctionner. Tout script inline ajouté à une vue doit
* faire de même (voir App\Core\Security).
*
* ORDRE DE CHARGEMENT IMPOSÉ : core.js d'abord (il publie window.PTA), puis les
* modules de la page, puis page-bootstrap.js en dernier (il appelle leurs init).
*
* VERSION DES ASSETS : incrémenter le `?v=` ici, dans layouts/header.php ET
* dans Views/pages/role.php après toute modification de JS ou de CSS.
*/
/** @var string $csrfToken */
/** @var array $motifs */
/** @var array $scripts */
/** @var array $modules */
/** @var array $pageConfig */
/** @var array $accessProfile */
/** @var array $permissions */
/** @var string $cspNonce */
?>
</main>
<script nonce="<?= htmlspecialchars($cspNonce, ENT_QUOTES) ?>">
window.PTA_CONFIG = {
csrfToken: <?= json_encode($csrfToken, JSON_THROW_ON_ERROR) ?>,
access: <?= json_encode($accessProfile, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE) ?>,
permissions: <?= json_encode($permissions, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE) ?>,
motifs: <?= json_encode(array_map(static function (array $motif): array {
return [
'id' => (int) $motif['id_motif'],
'code' => (string) $motif['code'],
'label' => (string) $motif['libelle'],
'quota' => (int) $motif['compte_dans_quota'] === 1,
];
}, $motifs), JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE) ?>
};
window.PTA_PAGE_CONFIG = <?= json_encode([
'modules' => $modules,
'params' => $pageConfig,
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE) ?>;
</script>
<script src="assets/js/core.js?v=7.26"></script>
<?php foreach ($scripts as $script): ?>
<script src="assets/js/<?= htmlspecialchars($script) ?>?v=7.26"></script>
<?php endforeach; ?>
<script src="assets/js/page-bootstrap.js?v=7.26"></script>
</body>
</html>