Subversion Repositories eFlore/Applications.eflore-consultation

Rev

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