<?php

class Router {
  public $_script;
  public $_action;
  public $_param1;

  public $_controller;
  public $_model;
  public $_view;
  public $_js;

  public $_page;                //! Configuration url /page unique
  public $_titre;
  public $_description;
  public $_keywords;

  public $_admtools;
  public $_enmaintenance;

  public $_mail;
  public $_form;
  public $_sidebar;
  public $_chart;
  public $_agenda;
  public $_autocomplete;
  public $_scheduler;
  public $_osm;
  public $_vuejs;
  public $_files;
  public $_layout;

  public function __construct($GET, $Conf) {
    if (isset($GET["sc"])) {
      $this->_script = strtolower(trim(htmlspecialchars($GET["sc"])));
      if ($this->_script == "") {
        $this->_script = $Conf->_appscript;
      }
    } else {
      $this->_script = $Conf->_appscript;
    }
    //! pour ne lancer qu'un seul script pour tout un site
    if (isset($Conf->_page)) {
      $this->_page = $Conf->_page;
    } else {
      $this->_page = 0;
    }
    if ($this->_page == 1) {
      $this->_controller = "controllers/cpage.php";
      $this->_model = "models/mpage.php";
      $this->_view = "views/vpage.php";
      $this->_js = "views/js/jpage.js";
    } else {
      $this->_controller = "controllers/c" . $this->_script . ".php";
      $this->_model = "models/m" . $this->_script . ".php";
      $this->_view = "views/v" . $this->_script . ".php";
      $this->_js = "views/js/j" . $this->_script . ".js";
    }

    //! on va chercher les infos de la page
    $this->getPage($Conf);

    if (isset($GET["ac"])) {
      $this->_action = strtolower(htmlspecialchars($GET["ac"]));
      if ($this->_action == "") {
        $this->_action = "index";
      }
    } else {
      $this->_action = "index";
    }

    if (isset($GET["param1"])) {
      $this->_param1 = $GET["param1"];
    } else {
      $this->_param1 = "";
    }

  }

  function getPage($Conf) {
    if ($this->_page == 1) {
      $sql = 'SELECT * FROM y_pages WHERE script = "page" AND admin = ' . $Conf::admin . ' AND active=1;';
    } else {
      $sql = 'SELECT * FROM y_pages WHERE script = "' . $this->_script . '" AND admin = ' . $Conf::admin . ' AND active=1;';
    }
    $LaPage = getinfos($sql, "frontal");
    if (count($LaPage) == 0) {
      eLog("ROUTER : ERREUR pas de page trouvée pour " . $this->_script);
    } else {
      $LaPage = $LaPage[0];
      $this->_titre = $LaPage["titre"];
      $this->_description = $LaPage["description"];
      $this->_keywords = $LaPage["keywords"];

      if (isset($LaPage["mail"])) {
        $this->_mail = $LaPage["mail"];
      } else {
        $this->_mail = 0;
      }

      if (isset($LaPage["admtools"])) {
        $this->_admtools = $LaPage["admtools"];
      } else {
        $this->_admtools = 0;
      }
      if (isset($LaPage["enmaintenance"])) {
        $this->_enmaintenance = $LaPage["enmaintenance"];
      } else {
        $this->_enmaintenance = 0;
      }

      if (isset($LaPage["sidebar"])) {
        $this->_sidebar = $LaPage["sidebar"];
      } else {
        $this->_sidebar = 0;
      }
      if (isset($LaPage["chart"])) {
        $this->_chart = $LaPage["chart"];
      } else {
        $this->_chart = 0;
      }
      if (isset($LaPage["autocomplete"])) {
        $this->_autocomplete = $LaPage["autocomplete"];
      } else {
        $this->_autocomplete = 0;
      }
      if (isset($LaPage["agenda"])) {
        $this->_agenda = $LaPage["agenda"];
      } else {
        $this->_agenda = 0;
      }
      if (isset($LaPage["scheduler"])) {
        $this->_scheduler = $LaPage["scheduler"];
      } else {
        $this->_scheduler = 0;
      }
      if (isset($LaPage["osm"])) {
        $this->_osm = $LaPage["osm"];
      } else {
        $this->_osm = 0;
      }
      if (isset($LaPage["files"])) {
        $this->_files = $LaPage["files"];
      } else {
        $this->_files = 0;
      }
      if (isset($LaPage["form"])) {
        $this->_form = $LaPage["form"];
      } else {
        $this->_form = 0;
      }
      $this->_layout = LAYROOT . DS . $LaPage["layout"];
    }
  }
}
