<?php

class FrontController
{
    function __construct()
    {
        global $dsn, $user, $pass, $vues;
        $listeAction_Administrator = array('goToAdministratorConnexion', 'verifValidation', 'ajoutSource', 'supprimerSource', 'setNbArticlesParPage', 'goToAdministratoristration', 'refreshData', 'quitterAdministrator');
        try {
            $mdlAdministrator = new MdlAdministrator();
            $Administrator = $mdlAdministrator->isAdministrator();
            $action = "";
            if (isset($_REQUEST['action'])) {
                $action = $_REQUEST['action'];
            }
            if (in_array($action, $listeAction_Administrator)) {
                if ($Administrator == NULL) {
                    new ControllerAdministrator();
                } else {
                    header("Location:" . $vues["AdministratorConnexion"]);
                }
            } else {
                $gatewayNews = new GatewayNews(new Connection($dsn, $user, $pass));
                $page = 1;
                if (isset($_GET['page'])) {
                    $page = $_GET['page'];
                }
                $gate = new GatewayConfigAdministrator(new Connection($dsn, $user, $pass));
                $nbArticlePages = $gate->getConfigAdministrator(1);
                $listeNews = $gatewayNews->getNews($page, $nbArticlePages);
                $pageAvant = $page - 1;
                $pageApres = $page + 1;


                $pageMAX = $gatewayNews->getNbNews() / $nbArticlePages + 1;
                require($vues['listeNews']);
            }
        } catch (Exception $e) {
            header("Location:" . $vues["erreur"]);
        }
    }
}
