Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 701 | Rev 715 | 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.
664 mathilde 4
 * Les fonctionnalités proposées sont l'export de toutes les parties de la fiche pdf
5
 * ou bien des parties choisies.
6
 * (voir le squelette fiche_pdf_lien.tpl.html pour le formulaire dans le module fiche.)
7
 *
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
 *
11
 * @category php 5.2
12
 * @package		eflore-consultation
13
 * @author	Mathilde Salthun-Lassalle <mathilde@tela-botanica.org>
14
 * @copyright	2012 Tela-Botanica
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
17
 * @version		$Id$
18
 *
664 mathilde 19
 *
652 mathilde 20
 */
21
 
22
 
23
class PdfExport extends aControleur {
24
 
25
 
26
	private $Conteneur;
27
	private $parametres = array();
28
	private $chemin_pdf;
29
	private $html;
30
	private $wkhtml;
31
	private $piedepage;
32
	private $fichier_pdf;
33
	private $blocs = array();
686 mathilde 34
 
652 mathilde 35
	private $Desc;
36
	private $Ecolo;
37
	private $Ethno;
38
	private $Classi;
39
	private $Illus;
40
	private $Nomen;
41
	private $Reparti;
42
	private $Stat;
43
	private $Biblio;
44
 
45
 
46
	public function initialiser() {
695 mathilde 47
		spl_autoload_register(array($this, 'chargerClassesOnglets'));
652 mathilde 48
		$this->capturerParametres();
49
		$this->conteneur = new Conteneur($this->parametres);
50
		$this->chemin_pdf = Config::get('dossier_pdf');
51
		$this->wkhtml = Config::get('WKHTMLTOPDF');
52
		$this->Desc = new Description($this->conteneur);
660 mathilde 53
		$this->Ecolo = new Ecologie($this->conteneur);
54
		$this->Ethno = new Ethnobotanique($this->conteneur);
55
		$this->Classi = new Classification($this->conteneur);
652 mathilde 56
		$this->Illus = new Illustrations($this->conteneur);
57
		$this->Nomen = new Nomenclature($this->conteneur);
58
		$this->Stat = new Statut($this->conteneur);
660 mathilde 59
		$this->Biblio = new Bibliographie($this->conteneur);
60
		$this->Reparti = new Repartition($this->conteneur);
652 mathilde 61
		$this->piedepage = Config::get('chemin_modules').'pdf_export/squelettes/footer.html';
660 mathilde 62
		}
695 mathilde 63
 
64
 
65
		private function chargerClassesOnglets($classe) {
66
			$base = dirname(__FILE__).DS;
67
			$cheminFormateurs = $base.'../fiche/formateurs'.DS;
68
			$dossiers = array($base, $cheminFormateurs);
69
			foreach ($dossiers as $chemin) {
70
				$fichierATester = $chemin.$classe.'.php';
71
				if (file_exists($fichierATester)) {
72
					include_once $fichierATester;
73
					return null;
74
				}
75
			}
76
		}
652 mathilde 77
 
701 mathilde 78
	private function getNomRetenu() {
686 mathilde 79
		$nom_retenu = $this->conteneur->getNomCourant()->getNomRetenu()->get('nom_sci');
80
		return $nom_retenu;
81
	}
652 mathilde 82
 
701 mathilde 83
	private function getNomRetenuHTML() {
686 mathilde 84
		$nom_retenu = '<span class="italique">'
85
					.$this->conteneur->getNomCourant()->getNomRetenu()->get('nom_sci')
86
					.'</span> '.$this->conteneur->getNomCourant()->getNomRetenu()->get('auteur');
87
		return $nom_retenu;
88
	}
89
 
90
	private function getNomFichierValide(){
91
		$nom_retenu = $this->conteneur->getNomCourant()->getNomRetenu()->get('nom_sci');
92
		$nom_retenu = str_replace(' ','_',$nom_retenu );
93
		$nom_retenu = preg_replace('/[\(\)\.\[\]]/','',$nom_retenu );
94
		return $nom_retenu;
95
	}
96
 
652 mathilde 97
	private function capturerParametres() {
98
		if (isset($_GET['num_nom'])) {
99
			$this->parametres['num_nom'] = $_GET['num_nom'];
100
		}
101
		if (isset($_GET['nom'])) {
102
			$this->parametres['nom'] = $_GET['nom'];
103
		}
104
		if (isset($_GET['type_nom'])) {
105
			$this->parametres['type_nom'] = $_GET['type_nom'];
106
		}
107
		if (isset($_GET['referentiel'])) {
108
			$this->parametres['referentiel'] = $_GET['referentiel'];
109
		}
110
 
111
		if (isset($_GET['niveau'])) {
112
			Registre::set('parametres.niveau', $_GET['niveau']);
113
		}
114
		if (isset($_GET['onglet'])) {
115
			$this->onglet = $_GET['onglet'];
116
		}
117
	}
118
 
119
	private function capturerParametresFormulaire() {
120
		if (!empty($_POST['bloc'])) {
121
			$this->blocs = $_POST['bloc'];
122
		} else {
123
			$this->blocs = array('description', 'ecologie', 'ethnobotanique',
124
								'statuts', 'illustrations', 'bibliographie',
125
								'classification', 'repartition', 'nomenclature');
126
		}
127
	}
128
 
129
	public function executerActionParDefaut() {
130
		$this->executerPdfExport();
131
	}
132
 
133
	public function executerPdfExport(){
134
			$this->capturerParametresFormulaire();
135
			$donnees = $this->obtenirDonnees();
136
			$this->construireHtml($donnees);
137
			$this->transformerHtmlEnPdf();
138
			$this->envoyerPdfAuNavigateur();
139
	}
140
 
141
 
142
	private function construireHtml($donnees) {
681 mathilde 143
		$this->html = $this->getVue('pdf_header', $donnees);
652 mathilde 144
		foreach ($this->blocs as $bloc) {
145
			$this->ajouterHtml($bloc, $donnees);
146
		}
147
		$this->html .= '</body></html>';
148
	}
149
 
150
 
151
	private function ajouterHtml($bloc, $donnees) {
152
		if (!empty($donnees[$bloc])) {
153
			$this->html .= $this->getVue('pdf_'.$bloc, $donnees);
154
		}
155
	}
156
 
157
	//++-------------------------------------récupération des données-----------------------------------++
158
 
159
	private function obtenirDonnees() {
160
		$donnees = array();
701 mathilde 161
		$donnees['donnees_pdf'] = array('nom' => $this->getNomRetenuHTML(),
660 mathilde 162
									'chemin_css' => Config::get('url_css_pdf'));
695 mathilde 163
		$donnees['description'] = $this->Desc->obtenirDonneesExport();
164
		$donnees['ecologie'] = $this->Ecolo->obtenirDonneesExport();
652 mathilde 165
		$donnees['statuts'] = $this->Stat->obtenirDonnees();
166
		$donnees['ethnobotanique'] = $this->Ethno->obtenirDonnees();
167
		$donnees['classification'] = $this->Classi->obtenirDonnees();
695 mathilde 168
		$donnees['illustrations'] = $this->Illus->obtenirDonneesExport();
652 mathilde 169
		$donnees['nomenclature'] = $this->Nomen->obtenirDonnees();
695 mathilde 170
		$donnees['repartition'] = $this->Reparti->obtenirDonneesExport();
652 mathilde 171
		$donnees['bibliographie'] = $this->Biblio->obtenirDonnees();
672 mathilde 172
		$donnees['premier'] = $this->blocs[0];// pour page-break : éviter 1ere page vide si bloc trop long
652 mathilde 173
		return $donnees;
174
	}
175
 
176
 //++ ------------------------------------------------export en pdf--------------------------------------++
177
 
664 mathilde 178
 
652 mathilde 179
	private function envoyerPdfAuNavigateur() {
686 mathilde 180
		$nom_fichier = $this->getNomFichierValide();
652 mathilde 181
		header('Content-type: application/pdf');
686 mathilde 182
		header('Content-Disposition: attachment; filename='.$nom_fichier.'.pdf');
652 mathilde 183
		readfile($this->fichier_pdf);
664 mathilde 184
		unlink($this->fichier_html);
185
		unlink($this->fichier_pdf);
652 mathilde 186
	}
187
 
188
 
189
 
190
	//version WKHTMLtoPDF en ligne de commande
191
	private function transformerHtmlEnPdf() {
686 mathilde 192
		$nom_fichier = $this->getNomFichierValide();
652 mathilde 193
		$time = $_SERVER['REQUEST_TIME'] ;
686 mathilde 194
		$this->fichier_html = $this->chemin_pdf.''.$nom_fichier.'-'.$time.'.html';
195
		$this->fichier_pdf = $this->chemin_pdf.''.$nom_fichier.'-'.$time.'.pdf';
701 mathilde 196
		$nom = $this->getNomRetenu();
664 mathilde 197
		file_put_contents($this->fichier_html, $this->html);
703 mathilde 198
		$commande =	"{$this->wkhtml}  --replace 'nom' '$nom' --encoding utf-8  --footer-html {$this->piedepage} {$this->fichier_html} {$this->fichier_pdf}";
652 mathilde 199
		$debug = exec($commande);
200
		//echo $debug;
201
	}
202
}
203
?>