Subversion Repositories Applications.gtt

Rev

Rev 104 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php

abstract class aControlleurAction {

    private $suivant;

        public function getRegistre()
        {
                return Registre::getRegistre();
        }

    // Suivant
    public function getSuivant()
        {
                return $this->suivant;
        }
        public function setSuivant($s, $position = null)
        {
                if (is_array($s)){
                        $this->suivant = $s;
                } else {
                        if (!is_null($position)) {
                                $tab_fin = array_slice($this->suivant, $position);
                                $tab_deb = array_slice($this->suivant, 0, $position);
                                $this->suivant = array_merge($tab_deb, array($s), $tab_fin);
                        } else {
                                $this->suivant[] = $s;
                        }
                }
        }

        public function demarrer()
    {
                if (!is_null($this->getSuivant())) {
                        // ATTENTION :
                        // Il est important de laisser "count($this->getSuivant())" $this->getSuivant() peut varier de taille
                        for ($i = 0; $i < count($this->getSuivant()) ; $i++) {
                                //echo '<pre>'.print_r($this->getSuivant(), true).'</pre>';
                                if ($this->getRegistre()->get('action_finale')) {
                                        // Si l'action met fin au script prématurément nous arrétons
                                        break;
                                } else {
                                        $liste_actions = $this->getSuivant();
                                        //echo '<pre>'.print_r($liste_actions[$i], true).'</pre>';
                                        if ($liste_actions[$i] instanceof aControlleurAction) {
                                                $liste_actions[$i]->demarrer();
                                        } else {
                                                if (isset($_POST) || isset($_GET)) {
                                                        // Méthode "vérifier" générale présente dans aControlleurAction
                                                        $this->verifier();
                                                        $methode_verif = 'verifier'.$liste_actions[$i];
                                                        if (method_exists($this, $methode_verif)) {
                                                                // Méthode "vérifier" spécifique à une action
                                                                $this->$methode_verif();
                                                        }
                                                }
                                                if ($liste_actions[$i] == '__defaut__') {
                                                        $methode = 'executer';
                                                } else {
                                                        $methode = 'executer'.$liste_actions[$i];
                                                }
                                                if (method_exists($this, $methode)) {
                                                        $this->$methode();
                                                } else {
                                                        $m = "La méthode $methode de la classe ".get_class($this)." est introuvable!";
                                                trigger_error($m, E_USER_ERROR);
                                                }
                                        }
                                }
                        }
                } else {
                        $m = "Le registre ne  contient aucune action!";
                trigger_error($m, E_USER_ERROR);
                }
    }

        public function verifier()
    {
        // Nous rassemblons les valeurs du tableau _POST contenant des : dans des sous-tableau de _POST.
        foreach ($_POST as $cle => $val) {
                $morceau = array();
                if (preg_match('/^(.+?)(:.+)+$/', $cle, $morceau)) {
                        $table = '';
                        foreach (explode(':', trim($morceau[2], ':')) as $c) {
                                $table .= '['.$c.']';
                        }
                        eval('$_POST[$morceau[1]]'.$table.' = $val;');
                        unset($_POST[$cle]);
                }
        }
    }

    abstract protected function executer();
}
?>