Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 1030 | Rev 1032 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1030 Rev 1031
1
<?php
1
<?php
2
/**
2
/**
3
 * Classe PdfExport, réalise des exportations pdf des fiches de taxons.
3
 * Classe PdfExport, réalise des exportations pdf des fiches de taxons.
4
 * Les fonctionnalités proposées sont l'export de toutes les parties de la fiche pdf
4
 * Les fonctionnalités proposées sont l'export de toutes les parties de la fiche pdf
5
 * ou bien des parties choisies.
5
 * ou bien des parties choisies.
6
 * (voir le squelette fiche_pdf_lien.tpl.html pour le formulaire dans le module fiche.)
6
 * (voir le squelette fiche_pdf_lien.tpl.html pour le formulaire dans le module fiche.)
7
 *
7
 *
8
 * manuel wkhtmltopdf  : http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf_0.10.0_rc2-doc.html
8
 * manuel wkhtmltopdf  : http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf_0.10.0_rc2-doc.html
9
 * pour changer de librairie : changer la fonction transformerHtmlEnPdf()
9
 * pour changer de librairie : changer la fonction transformerHtmlEnPdf()
10
 *
10
 *
11
 * @category	php 5.2
11
 * @category	php 5.2
12
 * @package		eflore-consultation
12
 * @package		eflore-consultation
13
 * @author		Mathilde Salthun-Lassalle <mathilde@tela-botanica.org>
13
 * @author		Mathilde Salthun-Lassalle <mathilde@tela-botanica.org>
14
 * @copyright	2012 Tela-Botanica
14
 * @copyright	2012 Tela-Botanica
15
 * @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
15
 * @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
16
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
16
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
17
 * @version		$Id$
17
 * @version		$Id$
18
 */
18
 */
19
 
19
 
20
class PdfExport extends aControleur {
20
class PdfExport extends aControleur {
21
 
21
 
22
	const DUREE_DE_VIE_PDF = 86400;// 3600 * 24 * 2 = 172 800
22
	const DUREE_DE_VIE_PDF = 86400;// 3600 * 24 * 2 = 172 800
23
 
23
 
24
	private $Conteneur;
24
	private $Conteneur;
25
	private $parametres = array();
25
	private $parametres = array();
26
	private $chemin_pdf;
26
	private $chemin_pdf;
27
	private $html;
27
	private $html;
28
	private $wkhtml;
28
	private $wkhtml;
29
	private $piedepage;
29
	private $piedepage;
30
	private $fichier_pdf;
30
	private $fichier_pdf;
31
	private $blocs = array();
31
	private $blocs = array();
32
 
32
 
33
	private $Desc;
33
	private $Desc;
34
	private $Ecolo;
34
	private $Ecolo;
35
	private $Ethno;
35
	private $Ethno;
36
	private $Illus;
36
	private $Illus;
37
	private $Nomen;
37
	private $Nomen;
38
	private $Reparti;
38
	private $Reparti;
39
	private $Stat;
39
	private $Stat;
40
	private $Biblio;
40
	private $Biblio;
41
 
41
 
42
	public function initialiser() {
42
	public function initialiser() {
43
		spl_autoload_register(array($this, 'chargerClassesOnglets'));
43
		spl_autoload_register(array($this, 'chargerClassesOnglets'));
44
		$this->capturerParametres();
44
		$this->capturerParametres();
45
		$this->conteneur = new Conteneur($this->parametres);
45
		$this->conteneur = new Conteneur($this->parametres);
46
		$this->chemin_pdf = Config::get('dossier_pdf');
46
		$this->chemin_pdf = Config::get('dossier_pdf');
47
		$this->wkhtml = Config::get('WKHTMLTOPDF');
47
		$this->wkhtml = Config::get('WKHTMLTOPDF');
48
		$this->Desc = new Description($this->conteneur);
48
		$this->Desc = new Description($this->conteneur);
49
		$this->Ecolo = new Ecologie($this->conteneur);
49
		$this->Ecolo = new Ecologie($this->conteneur);
50
		$this->Ethno = new Ethnobotanique($this->conteneur);
50
		$this->Ethno = new Ethnobotanique($this->conteneur);
51
		$this->Illus = new Illustrations($this->conteneur);
51
		$this->Illus = new Illustrations($this->conteneur);
52
		$this->Nomen = new Nomenclature($this->conteneur);
52
		$this->Nomen = new Nomenclature($this->conteneur);
53
		$this->Stat = new Statut($this->conteneur);
53
		$this->Stat = new Statut($this->conteneur);
54
		$this->Biblio = new Bibliographie($this->conteneur);
54
		$this->Biblio = new Bibliographie($this->conteneur);
55
		$this->Reparti = new Repartition($this->conteneur);
55
		$this->Reparti = new Repartition($this->conteneur);
56
		$this->piedepage = Config::get('chemin_modules').'pdf_export/squelettes/footer.html';
56
		$this->piedepage = Config::get('chemin_modules').'pdf_export/squelettes/footer.html';
57
	}
57
	}
58
 
58
 
59
	private function chargerClassesOnglets($classe) {
59
	private function chargerClassesOnglets($classe) {
60
		$base = dirname(__FILE__).DS;
60
		$base = dirname(__FILE__).DS;
61
		$cheminFormateurs = $base.'../fiche/formateurs'.DS;
61
		$cheminFormateurs = $base.'../fiche/formateurs'.DS;
62
		$dossiers = array($base, $cheminFormateurs);
62
		$dossiers = array($base, $cheminFormateurs);
63
		foreach ($dossiers as $chemin) {
63
		foreach ($dossiers as $chemin) {
64
			$fichierATester = $chemin.$classe.'.php';
64
			$fichierATester = $chemin.$classe.'.php';
65
			if (file_exists($fichierATester)) {
65
			if (file_exists($fichierATester)) {
66
				include_once $fichierATester;
66
				include_once $fichierATester;
67
				return null;
67
				return null;
68
			}
68
			}
69
		}
69
		}
70
	}
70
	}
71
 
71
 
72
	private function getNomRetenu() {
72
	private function getNomRetenu() {
73
		$nom_retenu = $this->conteneur->getNomCourant()->getNomRetenu()->get('nom_sci');
73
		$nom_retenu = $this->conteneur->getNomCourant()->getNomRetenu()->get('nom_sci');
74
		return $nom_retenu;
74
		return $nom_retenu;
75
	}
75
	}
76
 
76
 
77
	private function getNomRetenuHTML() {
77
	private function getNomRetenuHTML() {
78
		$nom_retenu = '<span class="italique">'
78
		$nom_retenu = '<span class="italique">'
79
			.$this->conteneur->getNomCourant()->getNomRetenu()->get('nom_sci')
79
			.$this->conteneur->getNomCourant()->getNomRetenu()->get('nom_sci')
80
			.'</span> '.$this->conteneur->getNomCourant()->getNomRetenu()->get('auteur');
80
			.'</span> '.$this->conteneur->getNomCourant()->getNomRetenu()->get('auteur');
81
		return $nom_retenu;
81
		return $nom_retenu;
82
	}
82
	}
83
 
83
 
84
	private function getNomFichierValide(){
84
	private function getNomFichierValide(){
85
		$nom_retenu = $this->conteneur->getNomCourant()->getNomRetenu()->get('nom_sci');
85
		$nom_retenu = $this->conteneur->getNomCourant()->getNomRetenu()->get('nom_sci');
86
		$nom_retenu = str_replace(' ','_',$nom_retenu );
86
		$nom_retenu = str_replace(' ','_',$nom_retenu );
87
		$nom_retenu = preg_replace('/[\(\)\.\[\]]/','',$nom_retenu );
87
		$nom_retenu = preg_replace('/[\(\)\.\[\]]/','',$nom_retenu );
88
		return $nom_retenu;
88
		return $nom_retenu;
89
	}
89
	}
90
 
90
 
91
	private function capturerParametres() {
91
	private function capturerParametres() {
92
		if (isset($_GET['num_nom'])) {
92
		if (isset($_GET['num_nom'])) {
93
			$this->parametres['num_nom'] = $_GET['num_nom'];
93
			$this->parametres['num_nom'] = $_GET['num_nom'];
94
		}
94
		}
95
		if (isset($_GET['nom'])) {
95
		if (isset($_GET['nom'])) {
96
			$this->parametres['nom'] = $_GET['nom'];
96
			$this->parametres['nom'] = $_GET['nom'];
97
		}
97
		}
98
		if (isset($_GET['type_nom'])) {
98
		if (isset($_GET['type_nom'])) {
99
			$this->parametres['type_nom'] = $_GET['type_nom'];
99
			$this->parametres['type_nom'] = $_GET['type_nom'];
100
		}
100
		}
101
		if (isset($_GET['referentiel'])) {
101
		if (isset($_GET['referentiel'])) {
102
			$this->parametres['referentiel'] = $_GET['referentiel'];
102
			$this->parametres['referentiel'] = $_GET['referentiel'];
103
		}
103
		}
104
		if (isset($_GET['niveau'])) {
104
		if (isset($_GET['niveau'])) {
105
			Registre::set('parametres.niveau', $_GET['niveau']);
105
			Registre::set('parametres.niveau', $_GET['niveau']);
106
		}
106
		}
107
		if (isset($_GET['onglet'])) {
107
		if (isset($_GET['onglet'])) {
108
			$this->onglet = $_GET['onglet'];
108
			$this->onglet = $_GET['onglet'];
109
		}
109
		}
110
	}
110
	}
111
 
111
 
112
	private function capturerParametresFormulaire() {
112
	private function capturerParametresFormulaire() {
-
 
113
		$this->blocs = array('description', 'ecologie', 'ethnobotanique', 'statuts', 'illustrations',
-
 
114
			'bibliographie', 'repartition', 'nomenclature');
113
		if (!empty($_POST['bloc'])) {
115
		if (!empty($_POST['bloc'])) {
114
			$this->blocs = $_POST['bloc'];
116
			$this->blocs = $_POST['bloc'];
115
		} else {
-
 
116
			$this->blocs = array('description', 'ecologie', 'ethnobotanique', 'statuts', 'illustrations',
-
 
117
				'bibliographie', 'repartition', 'nomenclature');
-
 
118
		}
117
		}
119
	}
118
	}
120
 
119
 
121
	public function executerActionParDefaut() {
120
	public function executerActionParDefaut() {
122
		$this->executerPdfExport();
121
		$this->executerPdfExport();
123
	}
122
	}
124
 
123
 
125
	public function executerPdfExport(){
124
	public function executerPdfExport(){
-
 
125
		$this->capturerParametresFormulaire();
-
 
126
		$hash = crc32(implode('-', $this->blocs));
126
		$nom_fichier = $this->getNomFichierValide();
127
		$nom_fichier = $this->getNomFichierValide().'-'.$hash;
-
 
128
 
127
		$this->fichier_html = $this->chemin_pdf.''.$nom_fichier.'.html';
129
		$this->fichier_html = $this->chemin_pdf.''.$nom_fichier.'.html';
128
		$this->fichier_pdf = $this->chemin_pdf.''.$nom_fichier.'.pdf';
130
		$this->fichier_pdf = $this->chemin_pdf.''.$nom_fichier.'.pdf';
129
 
131
 
130
		if (file_exists($this->fichier_pdf) == false) {
132
		if (file_exists($this->fichier_pdf) == false) {
131
			$this->capturerParametresFormulaire();
-
 
-
 
133
 
132
			$donnees = $this->obtenirDonnees();
134
			$donnees = $this->obtenirDonnees();
133
			$this->construireHtml($donnees);
135
			$this->construireHtml($donnees);
134
			if ($this->transformerHtmlEnPdf() == false) {
136
			if ($this->transformerHtmlEnPdf() == false) {
135
				die('Erreur de generation du fichier PDF');
137
				die('Erreur de generation du fichier PDF');
136
			}
138
			}
137
		}
139
		}
138
		$this->envoyerPdfAuNavigateur();
140
		$this->envoyerPdfAuNavigateur();
139
		// ATTENTION : on doit stopper l'exécution de Papyrus sinon du HTML inutile est ajouté à la fin du fichier PDF
141
		// ATTENTION : on doit stopper l'exécution de Papyrus sinon du HTML inutile est ajouté à la fin du fichier PDF
140
		exit;
142
		exit;
141
	}
143
	}
142
 
144
 
143
	private function construireHtml($donnees) {
145
	private function construireHtml($donnees) {
144
		$this->html = $this->getVue('pdf_header', $donnees);
146
		$this->html = $this->getVue('pdf_header', $donnees);
145
		foreach ($this->blocs as $bloc) {
147
		foreach ($this->blocs as $bloc) {
146
			$this->ajouterHtml($bloc, $donnees);
148
			$this->ajouterHtml($bloc, $donnees);
147
		}
149
		}
148
		$this->html .= '</body></html>';
150
		$this->html .= '</body></html>';
149
	}
151
	}
150
 
152
 
151
	private function ajouterHtml($bloc, $donnees) {
153
	private function ajouterHtml($bloc, $donnees) {
152
		if (!empty($donnees[$bloc])) {
154
		if (!empty($donnees[$bloc])) {
153
			$this->html .= $this->getVue('pdf_'.$bloc, $donnees);
155
			$this->html .= $this->getVue('pdf_'.$bloc, $donnees);
154
		}
156
		}
155
	}
157
	}
156
 
158
 
157
//+-------------------------------------récupération des données---------------------------------------------+
159
//+-------------------------------------récupération des données---------------------------------------------+
158
	private function obtenirDonnees() {
160
	private function obtenirDonnees() {
159
		$donnees = array();
161
		$donnees = array();
160
		$version = $this->Nomen->obtenirVersionDonnees();
162
		$version = $this->Nomen->obtenirVersionDonnees();
161
		$donnees['donnees_pdf'] = array(
163
		$donnees['donnees_pdf'] = array(
162
			'nom' => $this->getNomRetenuHTML(),
164
			'nom' => $this->getNomRetenuHTML(),
163
			'chemin_css' => Config::get('url_css_pdf'),
165
			'chemin_css' => Config::get('url_css_pdf'),
164
			'version' => $version['version']);
166
			'version' => $version['version']);
165
		$donnees['description'] = $this->Desc->obtenirDonneesExport();
167
		$donnees['description'] = $this->Desc->obtenirDonneesExport();
166
		$donnees['ecologie'] = $this->Ecolo->obtenirDonneesExport();
168
		$donnees['ecologie'] = $this->Ecolo->obtenirDonneesExport();
167
		$donnees['statuts'] = $this->Stat->obtenirDonnees();
169
		$donnees['statuts'] = $this->Stat->obtenirDonnees();
168
		$donnees['ethnobotanique'] = $this->Ethno->obtenirDonnees();
170
		$donnees['ethnobotanique'] = $this->Ethno->obtenirDonnees();
169
		$donnees['illustrations'] = $this->Illus->obtenirDonneesExport();
171
		$donnees['illustrations'] = $this->Illus->obtenirDonneesExport();
170
		$donnees['nomenclature'] = $this->Nomen->obtenirDonnees();
172
		$donnees['nomenclature'] = $this->Nomen->obtenirDonnees();
171
		$donnees['repartition'] = $this->Reparti->obtenirDonneesExport();
173
		$donnees['repartition'] = $this->Reparti->obtenirDonneesExport();
172
		$donnees['bibliographie'] = $this->Biblio->obtenirDonnees();
174
		$donnees['bibliographie'] = $this->Biblio->obtenirDonnees();
173
		$donnees['premier'] = $this->blocs[0];// pour page-break : éviter 1ere page vide si bloc trop long
175
		$donnees['premier'] = $this->blocs[0];// pour page-break : éviter 1ere page vide si bloc trop long
174
		return $donnees;
176
		return $donnees;
175
	}
177
	}
176
 
178
 
177
//+------------------------------------------------export en pdf---------------------------------------------+
179
//+------------------------------------------------export en pdf---------------------------------------------+
178
	private function envoyerPdfAuNavigateur() {
180
	private function envoyerPdfAuNavigateur() {
179
		$nom_fichier = $this->getNomFichierValide();
181
		$nom_fichier = $this->getNomFichierValide();
180
		header('Content-type: application/pdf');
182
		header('Content-type: application/pdf');
181
		header('Content-Disposition: attachment; filename='.$nom_fichier.'.pdf');
183
		header('Content-Disposition: attachment; filename='.$nom_fichier.'.pdf');
182
		readfile($this->fichier_pdf);
184
		readfile($this->fichier_pdf);
183
		$this->nettoyerFichiersTmp();
185
		$this->nettoyerFichiersTmp();
184
	}
186
	}
185
 
187
 
186
	private function nettoyerFichiersTmp() {
188
	private function nettoyerFichiersTmp() {
187
		$dossierStockage = $this->chemin_pdf;
189
		$dossierStockage = $this->chemin_pdf;
188
		if (is_dir($dossierStockage)) {
190
		if (is_dir($dossierStockage)) {
189
			$objets = scandir($dossierStockage);
191
			$objets = scandir($dossierStockage);
190
			if ($objets !== false) {
192
			if ($objets !== false) {
191
				foreach ($objets as $objet) {
193
				foreach ($objets as $objet) {
192
					$chemin = $dossierStockage.$objet;
194
					$chemin = $dossierStockage.$objet;
193
					if (is_file($chemin)) {
195
					if (is_file($chemin)) {
194
						$filemtime = @filemtime($chemin);
196
						$filemtime = @filemtime($chemin);
195
						if ($filemtime !== false) {
197
						if ($filemtime !== false) {
196
							$suppression = (time() - $filemtime >= self::DUREE_DE_VIE_PDF) ? true : false;
198
							$suppression = (time() - $filemtime >= self::DUREE_DE_VIE_PDF) ? true : false;
197
							if ($suppression === true) {
199
							if ($suppression === true) {
198
								unlink($chemin);
200
								unlink($chemin);
199
							}
201
							}
200
						}
202
						}
201
					}
203
					}
202
				}
204
				}
203
			}
205
			}
204
		}
206
		}
205
	}
207
	}
206
 
208
 
207
	//version WKHTMLtoPDF en ligne de commande
209
	//version WKHTMLtoPDF en ligne de commande
208
	private function transformerHtmlEnPdf() {
210
	private function transformerHtmlEnPdf() {
209
		$nom = $this->getNomRetenu();
211
		$nom = $this->getNomRetenu();
210
		file_put_contents($this->fichier_html, $this->html);
212
		file_put_contents($this->fichier_html, $this->html);
211
		$commande =	"{$this->wkhtml}  --replace 'nom' '$nom' --encoding utf-8  --footer-html {$this->piedepage} {$this->fichier_html} {$this->fichier_pdf}";
213
		$commande =	"{$this->wkhtml}  --replace 'nom' '$nom' --encoding utf-8  --footer-html {$this->piedepage} {$this->fichier_html} {$this->fichier_pdf}";
212
		$ret = -1;
214
		$ret = -1;
213
		$debug = array();
215
		$debug = array();
214
		exec($commande, $debug, $ret);
216
		exec($commande, $debug, $ret);
215
		//print_r( $debug ); echo $ret;
217
		//print_r( $debug ); echo $ret;
216
		$ok = ($ret == 0 || $ret == 2) ? true : false;
218
		$ok = ($ret == 0 || $ret == 2) ? true : false;
217
		return $ok;
219
		return $ok;
218
	}
220
	}
219
}
221
}
220
?>
222
?>