Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 1029 | Rev 1031 | 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() {
113
		if (!empty($_POST['bloc'])) {
114
			$this->blocs = $_POST['bloc'];
115
		} else {
1030 jpm 116
			$this->blocs = array('description', 'ecologie', 'ethnobotanique', 'statuts', 'illustrations',
117
				'bibliographie', 'repartition', 'nomenclature');
652 mathilde 118
		}
119
	}
1006 jpm 120
 
652 mathilde 121
	public function executerActionParDefaut() {
122
		$this->executerPdfExport();
123
	}
1006 jpm 124
 
652 mathilde 125
	public function executerPdfExport(){
1030 jpm 126
		$nom_fichier = $this->getNomFichierValide();
127
		$this->fichier_html = $this->chemin_pdf.''.$nom_fichier.'.html';
128
		$this->fichier_pdf = $this->chemin_pdf.''.$nom_fichier.'.pdf';
129
 
130
		if (file_exists($this->fichier_pdf) == false) {
131
			$this->capturerParametresFormulaire();
132
			$donnees = $this->obtenirDonnees();
133
			$this->construireHtml($donnees);
134
			if ($this->transformerHtmlEnPdf() == false) {
135
				die('Erreur de generation du fichier PDF');
136
			}
1006 jpm 137
		}
1030 jpm 138
		$this->envoyerPdfAuNavigateur();
139
		// ATTENTION : on doit stopper l'exécution de Papyrus sinon du HTML inutile est ajouté à la fin du fichier PDF
1011 raphael 140
		exit;
652 mathilde 141
	}
1006 jpm 142
 
652 mathilde 143
	private function construireHtml($donnees) {
681 mathilde 144
		$this->html = $this->getVue('pdf_header', $donnees);
652 mathilde 145
		foreach ($this->blocs as $bloc) {
146
			$this->ajouterHtml($bloc, $donnees);
147
		}
148
		$this->html .= '</body></html>';
149
	}
1006 jpm 150
 
652 mathilde 151
	private function ajouterHtml($bloc, $donnees) {
152
		if (!empty($donnees[$bloc])) {
153
			$this->html .= $this->getVue('pdf_'.$bloc, $donnees);
154
		}
155
	}
1006 jpm 156
 
157
//+-------------------------------------récupération des données---------------------------------------------+
652 mathilde 158
	private function obtenirDonnees() {
159
		$donnees = array();
715 mathilde 160
		$version = $this->Nomen->obtenirVersionDonnees();
1006 jpm 161
		$donnees['donnees_pdf'] = array(
162
			'nom' => $this->getNomRetenuHTML(),
163
			'chemin_css' => Config::get('url_css_pdf'),
164
			'version' => $version['version']);
695 mathilde 165
		$donnees['description'] = $this->Desc->obtenirDonneesExport();
166
		$donnees['ecologie'] = $this->Ecolo->obtenirDonneesExport();
652 mathilde 167
		$donnees['statuts'] = $this->Stat->obtenirDonnees();
168
		$donnees['ethnobotanique'] = $this->Ethno->obtenirDonnees();
695 mathilde 169
		$donnees['illustrations'] = $this->Illus->obtenirDonneesExport();
652 mathilde 170
		$donnees['nomenclature'] = $this->Nomen->obtenirDonnees();
695 mathilde 171
		$donnees['repartition'] = $this->Reparti->obtenirDonneesExport();
652 mathilde 172
		$donnees['bibliographie'] = $this->Biblio->obtenirDonnees();
672 mathilde 173
		$donnees['premier'] = $this->blocs[0];// pour page-break : éviter 1ere page vide si bloc trop long
652 mathilde 174
		return $donnees;
175
	}
664 mathilde 176
 
1006 jpm 177
//+------------------------------------------------export en pdf---------------------------------------------+
652 mathilde 178
	private function envoyerPdfAuNavigateur() {
686 mathilde 179
		$nom_fichier = $this->getNomFichierValide();
652 mathilde 180
		header('Content-type: application/pdf');
686 mathilde 181
		header('Content-Disposition: attachment; filename='.$nom_fichier.'.pdf');
652 mathilde 182
		readfile($this->fichier_pdf);
1029 jpm 183
		$this->nettoyerFichiersTmp();
652 mathilde 184
	}
1006 jpm 185
 
1029 jpm 186
	private function nettoyerFichiersTmp() {
187
		$dossierStockage = $this->chemin_pdf;
188
		if (is_dir($dossierStockage)) {
189
			$objets = scandir($dossierStockage);
190
			if ($objets !== false) {
191
				foreach ($objets as $objet) {
192
					$chemin = $dossierStockage.$objet;
193
					if (is_file($chemin)) {
194
						$filemtime = @filemtime($chemin);
195
						if ($filemtime !== false) {
196
							$suppression = (time() - $filemtime >= self::DUREE_DE_VIE_PDF) ? true : false;
197
							if ($suppression === true) {
198
								unlink($chemin);
199
							}
200
						}
201
					}
202
				}
203
			}
204
		}
205
	}
206
 
652 mathilde 207
	//version WKHTMLtoPDF en ligne de commande
208
	private function transformerHtmlEnPdf() {
701 mathilde 209
		$nom = $this->getNomRetenu();
664 mathilde 210
		file_put_contents($this->fichier_html, $this->html);
703 mathilde 211
		$commande =	"{$this->wkhtml}  --replace 'nom' '$nom' --encoding utf-8  --footer-html {$this->piedepage} {$this->fichier_html} {$this->fichier_pdf}";
1005 raphael 212
		$ret = -1;
213
		$debug = array();
214
		exec($commande, $debug, $ret);
215
		//print_r( $debug ); echo $ret;
1030 jpm 216
		$ok = ($ret == 0 || $ret == 2) ? true : false;
217
		return $ok;
652 mathilde 218
	}
219
}
220
?>