Subversion Repositories Sites.obs-saisons.fr

Rev

Rev 36 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
31 aurelien 1
<?php
2
// declare(encoding='UTF-8');
3
/**
4
 * Classe Controleur générale de l'application saisie.
5
 *
6
 * @category    php5.2
7
 * @package     saisie
8
 * @author      Aurélien Peronnet <aurelien@tela-botanica.org>
9
 * @copyright   2010 Tela-Botanica
10
 * @license     http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
11
 * @license     http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
12
 * @version     SVN: $Id: aControleur.php 152 2010-09-06 16:19:12Z aurelien $
13
 */
14
abstract class aControleur extends Controleur {
15
 
16
    const FMT_DATE = '%d/%m/%Y';// Supporte les formats de dates non valides (1989-00-00)
17
    const FMT_DATE_TXT = '%A %d %B %Y';
18
    const FMT_DATE_HEURE = '%d/%m/%Y %H:%i:%s';// Supporte les formats de dates non valides (1989-00-00 00:00:00)
19
    const FMT_DATE_HEURE_TXT = '%A %d %B %Yà%H:%M';
20
 
21
    const META_TITRE = 'titre';
22
    const META_DESCRIPTION = 'description';
23
    const META_TAGS = 'tags';
24
    const RENDU_TETE = 'tete';
25
    const RENDU_CORPS = 'corps';
26
    const RENDU_PIED = 'pied';
27
    const RENDU_NAVIGATION = 'navigation';
28
 
29
    const TYPE_AUTRE = 'AUTRE';
30
    const TYPE_TOTAL = 'TOTAL';
31
    const SEPARATEUR_TYPE_VALEUR = '##';
32
    const SEPARATEUR_VALEURS = ';;';
33
    const SEPARATEUR_DONNEES = '||';
34
    const VALEUR_NULL = 'NC';
35
 
36
    private $sortie = array();
37
    private $parametres = array();
38
 
39
    protected static $hierarchie_appels = array();
40
    // FIXME : voir s'il est plus intéressant d'utiliser une méthode dans les classes filles
41
    protected $url = null;
42
 
43
    public function __construct()  {
44
        $registre = Registre::getInstance();
45
        $this->parametres = $registre->get('parametres');
46
        $this->url = $this->parametres['url'];
47
        parent::__construct();
48
    }
49
 
50
    /**
51
     * Attribue une position de sortie à un contenu.
52
     */
53
    protected function setSortie($position, $contenu, $fusionner = false) {
54
        if ($this->verifierExistenceTypeSortie($position)) {
55
            if ($fusionner) {
56
                $this->sortie[$position] .= $contenu;
57
            } else {
58
                $this->sortie[$position] = $contenu;
59
            }
60
        }
61
    }
62
 
63
    /**
64
     * Vérifie l'existence du type de sortie indiqué pour son utilisation dans le tableau de sortie.
65
     * @param string le type de sortie à tester.
66
     * @return bool true si le type de sortie est valide, sinon false.
67
     */
68
    private function verifierExistenceTypeSortie($type) {
69
        $existe = true;
70
        if ($type != self::RENDU_TETE &&
71
            $type != self::RENDU_NAVIGATION &&
72
            $type != self::RENDU_CORPS &&
73
            $type != self::RENDU_PIED &&
74
            $type != self::META_TITRE &&
75
            $type != self::META_DESCRIPTION &&
76
            $type != self::META_TAGS) {
77
            trigger_error("Le type de sortie '$type' n'est pas une valeur prédéfinie.", E_USER_WARNING);
78
            $existe = false;
79
        }
80
        return $existe;
81
    }
82
 
83
    /**
84
     * Retourne le tableau de sortie à utiliser dans le controleur principal de l'application.
85
     */
86
    public function getSortie() {
87
        return $this->sortie;
88
    }
89
 
90
    /**
91
     * Execute l'action d'un module donnée et fusionne le résultat avec le tableau de sortie.
92
     */
93
    protected function executerAction($ClasseModule, $action) {
94
        $module = new $ClasseModule();
95
        $module->$action();
96
        $this->fusionnerSortie($module->getSortie());
97
 
98
    }
99
 
100
    /**
101
     * Fusionne un tableau de sortie par défaut avec le tableau passé en paramètre.
102
     * @param array le tableauàfusionner
103
     */
104
    private function fusionnerSortie($sortie) {
105
        $this->sortie = array_merge($this->sortie, $sortie);
106
    }
107
 
108
    protected function formaterParenthese($chaine_a_afficher) {
109
        if ($chaine_a_afficher != '') {
110
            $chaine_a_afficher = '('.$chaine_a_afficher.')';
111
        }
112
        return $chaine_a_afficher;
113
    }
114
 
115
    protected function formaterSautDeLigne($chaine_a_formater) {
116
        $txt_a_retourner = preg_replace('/\n/', '<br />', $chaine_a_formater);
117
        return $txt_a_retourner;
118
    }
119
 
120
    protected function formaterTableauDeTxt($tableau_de_txt, $majuscule = true, $point_final = true) {
121
        $chaine_a_afficher = '';
122
        $taille_du_tableau = count($tableau_de_txt);
123
        if ($taille_du_tableau > 0) {
124
            $index_avt_dernier = $taille_du_tableau - 1;
125
            for ($i = 0; $i < $taille_du_tableau; $i++) {
126
                $mot = $tableau_de_txt[$i];
127
                if ($i != $index_avt_dernier) {
128
                    $chaine_a_afficher .= $mot.', ';
129
                } else {
130
                    $chaine_a_afficher .= $this->nettoyerPointFinal($mot);
131
                    if ($point_final) {
132
                        $chaine_a_afficher .= '.';
133
                    }
134
                }
135
            }
136
        }
137
        if ($majuscule) {
138
            $chaine_a_afficher = ucfirst($chaine_a_afficher);
139
        }
140
        return $chaine_a_afficher;
141
    }
142
 
143
    protected function formaterOuiNon($chaine_a_formater) {
144
        $txt_a_retourner = '';
145
        if ($chaine_a_formater == '0') {
146
            $txt_a_retourner = 'non';
147
        } else if ($chaine_a_formater == '1') {
148
            $txt_a_retourner = 'oui';
149
        }
150
        return $txt_a_retourner;
151
    }
152
 
153
    protected function formaterDate($date, $format = self::FMT_DATE_HEURE) {
154
        if ($date == '' || $date == '0000-00-00' || $date == '0000-00-00 00:00:00') {
155
            $date = 'Inconnue';
156
        } else {
157
            if (preg_match('/^([0-9]{4})-([0-9]{2})-([0-9]{2})(?: ([0-9]{2}):([0-9]{2}):([0-9]{2})|)$/', $date, $match)) {// Date Heure
158
                $annee = $match[1];
159
                $mois = $match[2];
160
                $jour = $match[3];
161
                $heure = (isset($match[4])) ? $match[4] : '00';
162
                $minute = (isset($match[5])) ? $match[5] : '00';
163
                $seconde = (isset($match[6])) ? $match[6] : '00';
164
                if ($format == self::FMT_DATE && $jour == '00' && $mois == '00') {
165
                    $date = $annee;
166
                } else if ($format == self::FMT_DATE && $jour == '00') {
167
                    $date = strftime('%b', mktime(0, 0, 0, $mois, 1)).' '.$annee;
168
                } else {
169
                    $timestamp = strtotime($date);
170
                    if ($timestamp !== false) {
171
                        $date = strftime($format, $timestamp);
172
                    }
173
                }
174
            } else {
175
                $e = "La chaine '$date' n'est pas reconnue.";
176
                trigger_error($e, E_USER_WARNING);
177
            }
178
        }
179
        return $date;
180
    }
181
 
182
    protected function formaterCourriels($courriels) {
183
        $fmt_courriels = '';
184
        if (!empty($courriels)) {
185
            $courriels = (is_array($courriels)) ? $courriels : array($courriels);
186
            foreach ($courriels as $cle => $courriel) {
187
                $courriel = preg_replace('/@/', ' [arrobase] ', $courriel);
188
                $courriel = preg_replace('/[.]([^.]+)$/', " [point] $1", $courriel);
189
                $fmt_courriels[] = $this->getVue('courriel', array('courriel' => $courriel));
190
            }
191
            $fmt_courriels = implode(', ', $fmt_courriels);
192
        }
193
        return $fmt_courriels;
194
    }
195
 
196
    protected function nettoyerPointFinal($mot) {
197
        $mot = preg_replace('/[.]$/', '', $mot);
198
        return $mot;
199
    }
200
 
201
    protected function postraiterDonnees(&$tableau) {
202
        if (count($tableau) > 0) {
203
            foreach ($tableau as $cle => &$valeur) {
204
                if ($valeur == '') {
205
                    $valeur = '&nbsp;';
206
                } else if (is_string($valeur)) {
207
                    $valeur = $this->remplacerEsperluette($valeur);
208
                } else if (is_array($valeur)) {
209
                    $this->postraiterDonnees($valeur);
210
                }
211
            }
212
        }
213
    }
214
 
215
    private function remplacerEsperluette($txt) {
216
        $txt = preg_replace('/&(?!([a-z]+|#[0-9]+|#x[0-9a-f]+);)/i', '&amp;', $txt, -1);
217
        return $txt;
218
    }
219
 
220
    protected function chargerPiedDePage() {
221
        $donnees['appli'] = Application::getInfo();
222
        $this->setSortie(self::RENDU_PIED, $this->getVue('pied', $donnees));
223
    }
224
 
225
    public static function getUrlConsultationFicheStation($id_station) {
226
    	// TODO: gérer les urls comme dans l'annuaire
227
    	return '?module=Station&action=afficherInformationsStation&id_station='.$id_station;
228
    }
229
 
230
    public static function getUrlFormulaireSaisieStation() {
231
    	// TODO: gérer les urls comme dans l'annuaire
232
    	return '?module=Station&action=afficherFormulaireSaisieStation';
233
    }
234
 
235
    public static function getUrlValidationFormulaireSaisieStation() {
236
    	// TODO: gérer les urls comme dans l'annuaire
237
    	return '?module=Station&action=validerFormulaireSaisieStation';
238
    }
239
 
240
    public static function getUrlFormulaireModificationStation($id_station) {
241
    	// TODO: gérer les urls comme dans l'annuaire
242
    	return '?module=Station&action=afficherFormulaireModificationStation&id_station='.$id_station;
243
    }
244
 
245
    public static function getUrlConsultationFicheIndividu($id_station, $id_individu) {
246
    	// TODO: gérer les urls comme dans l'annuaire
247
    	return '?module=Individu&action=afficherInformationsIndividu&id_station='.$id_station.'&id_individu='.$id_individu;
248
 
249
    }
250
 
251
	public static function getUrlFormulaireAjoutIndividu($id_station, $id_espece) {
252
    	// TODO: gérer les urls comme dans l'annuaire
253
    	return '?module=Individu&action=afficherFormulaireAjoutIndividu&id_station='.$id_station.'&id_espece='.$id_espece;
254
    }
255
 
256
    public static function getUrlConsultationEspeceStation($id_station, $id_espece) {
257
    	// TODO: gérer les urls comme dans l'annuaire
258
    	return '?module=Individu&action=afficherListeIndividu&id_station='.$id_station.'&id_espece='.$id_espece;
259
 
260
    }
261
 
262
	public static function getUrlFormulaireAjoutEspece($id_station) {
263
    	// TODO: gérer les urls comme dans l'annuaire
264
    	return '?module=Espece&action=afficherFormulaireSaisieEspece&id_station='.$id_station;
265
 
266
	}
267
 
268
    public static function getUrlFormulaireModificationObservation($id_individu) {
269
    	// TODO: gérer les urls comme dans l'annuaire
270
    	return '?module=Observation&action=afficherFormulaireModificationObservation&id_individu='.$id_individu;
271
    }
272
}