Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 1034 | Rev 1499 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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