Subversion Repositories eFlore/Applications.cel

Rev

Rev 2458 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
416 aurelien 1
<?php
2458 jpm 2
// declare(encoding='UTF-8');
885 aurelien 3
/**
2458 jpm 4
 * Service générant un PDF d'export des observations pour réaliser des étiquettes d'herbier.
5
 *
6
 * @internal   Mininum PHP version : 5.2
7
 * @category   CEL
8
 * @package    Services
9
 * @subpackage Export
10
 * @version    0.1
11
 * @author     Mathias CHOUET <mathias@tela-botanica.org>
12
 * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
13
 * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
14
 * @license    GPL v3 <http://www.gnu.org/licenses/gpl.txt>
15
 * @license    CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
16
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
17
 */
416 aurelien 18
 
19
/** Constante stockant l'URL de la page d'accueil de Photoflora.*/
20
define('EF_URL_PHOTOFLORA', 'http://photoflora.free.fr/');
21
/** Constante stockant l'URL de la page de Photoflora affichant toutes les images d'un taxon donn.es.*/
22
define('EF_URL_PHOTOFLORA_TAXON', EF_URL_PHOTOFLORA.'FiTax.php?NumTaxon=%s');
23
/** Constante stockant l'URL du dossier de photoflora contenant les images miniatures.*/
24
define('EF_URL_PHOTOFLORA_IMG_MIN', 'http://www.tela-botanica.org/~photoflo/photos/%s/min/%s');
25
/** Constante stockant l'URL du dossier de photoflora contenant les images normale.*/
26
define('EF_URL_PHOTOFLORA_IMG_MAX', 'http://www.tela-botanica.org/~photoflo/photos/%s/max/%s');
27
/** Constante stockant l'expression r.guli.re r.cup.rant l'abr.viation du photographe et le nom du fichier.*/
28
define('EF_URL_PHOTOFLORA_REGEXP', '/\/photos\/([^\/]+)\/max\/(.+)$/');
29
/** Constante stockant l'URL du service XML de Photoflora.*/
30
define('EF_URL_PHOTOFLORA_SERVICE', EF_URL_PHOTOFLORA.'ef_photoflora.php?nt=%s');
31
 
885 aurelien 32
class InventoryPDF extends Cel  {
416 aurelien 33
 
34
	var $extendPDFProductor;
2450 jpm 35
 
2458 jpm 36
	public function __construct($config) {
885 aurelien 37
		parent::__construct($config);
38
 
2458 jpm 39
		$this->config = $config;
416 aurelien 40
		// Pas d'heritage multiple en php :(
41
		$this->extendPDFProductor = new PDFProductor();
42
		$this->extendPDFProductor->initPDF();
43
	}
2450 jpm 44
 
885 aurelien 45
	/**
46
	* uid[0] : utilisateur obligatoire
47
	* uid[1] : si absent : valeur 'all' (commune)
48
	* uid[2] : si absent : valeur 'all' (date)
49
	* uid[3] : si absent : valeur 'all' (recherche libre)
50
	* uid[4] : si absent : valeur 'all' (station)
51
	*/
2458 jpm 52
	public function getElement($uid){
53
		// Controle detournement utilisateur
885 aurelien 54
	 	$this->controleUtilisateur($uid[0]);
2450 jpm 55
 
2458 jpm 56
		if (!isset($uid[1]) || $uid[1] == '' || $uid[1] == 'all' ) {
57
			$uid[1] = 'all';
58
			$requete_location = '';
59
		} else {
60
			$requete_location = ' AND location= '.Cel::db()->proteger($uid[1]).' ';
885 aurelien 61
		}
2450 jpm 62
 
2458 jpm 63
		if (!isset($uid[2]) || $uid[2] == '' || $uid[2] == 'all') {
64
			$uid[2] = 'all';
65
			$requete_date = '';
66
		} else {
67
			$requete_date = ' AND date_observation= '.Cel::db()->proteger($uid[2]).' ';
885 aurelien 68
		}
2450 jpm 69
 
2458 jpm 70
		if (!isset($uid[3]) || $uid[3] == '' || $uid[3] == 'all') {
71
			$uid[3] = 'all';
72
			$requete_libre = '';
73
		} else {
74
			$requete_libre = ' AND (nom_sel LIKE '.Cel::db()->proteger('%'.$uid[3].'%').
75
						' OR nom_ret LIKE '.Cel::db()->proteger('%'.$uid[3].'%').
76
						' OR station LIKE '.Cel::db()->proteger('%'.$uid[3].'%').
77
						' OR commentaire LIKE '.Cel::db()->proteger('%'.$uid[3].'%');
885 aurelien 78
		}
416 aurelien 79
 
2458 jpm 80
		if (!isset($uid[4]) || $uid[4] == '' || $uid[4] == 'all') {
81
			$uid[4] = 'all';
82
			$requete_station ='';
83
		} else {
84
			$requete_station = ' AND station= '.Cel::db()->proteger($uid[4]).' ';
885 aurelien 85
		}
2450 jpm 86
 
2458 jpm 87
		$value = array();
88
		$requete = 'SELECT ce_utilisateur, ordre, nom_sel, nom_sel_nn, nom_ret, nom_ret_nn, nt, famille, zone_geo, date_observation, '.
89
			'station, commentaire, transmission '.
90
			'FROM cel_obs '.
91
			'WHERE ce_utilisateur = '.Cel::db()->proteger($uid[0]).' '.
92
			$requete_location.
93
			$requete_date.
94
			$requete_libre.
95
			$requete_station.' '.
96
			'ORDER BY ordre '.
97
			' -- '.__FILE__.':'.__LINE__;
98
		$resultat = Cel::db()->requeter($requete);
2450 jpm 99
 
885 aurelien 100
		$observations = array();;
2458 jpm 101
		if (is_array($resultat)) {
102
			$observations = $resultat;
885 aurelien 103
		}
2450 jpm 104
 
885 aurelien 105
		// Set up the pdf object.
106
		$pdf = &File_PDF::factory(array('orientation' => 'P', 'format' => 'A4'));
107
		// DesActivate compression.
108
		$pdf->setCompression(false);
109
		$pdf->setMargins(0, 0);
110
		// Enable automatic page breaks.
111
		$pdf->setAutoPageBreak(true);
112
		// Start the document.
113
		$pdf->open();
114
		// Start a page.
115
		$pdf->addPage();
116
		$pdf->setFont('Times', '' , 12);
2450 jpm 117
 
2458 jpm 118
		$i = 1;
119
		$tempfn = tempnam('', '');
2450 jpm 120
 
885 aurelien 121
		foreach ($observations as $obs) {
2458 jpm 122
			// Denullifiage
885 aurelien 123
			foreach($obs as $k=>$v) {
2458 jpm 124
				if (($v == 'null') || ($v == '000null')) {
125
					$obs[$k] = '';
126
				} else {
127
					$obs[$k] = utf8_decode($v);
416 aurelien 128
				}
2450 jpm 129
			}
130
 
2458 jpm 131
			if ($obs['date_observation'] != '0000-00-00 00:00:00') {
132
				list($year,$month,$day)= explode('-', $obs['date_observation']);
133
				list($day)= explode(' ', $day);
134
				$obs['date_observation'] = "$day/$month/$year";
135
			} else {
136
				$obs['date_observation'] = '00/00/0000';
137
			}
2450 jpm 138
 
2458 jpm 139
			$text = $obs['nom_sel']." ".$obs['nom_sel_nn']." ".$obs['nom_ret']." ".$obs['nom_ret_nn']." ".$obs['nt']." ".
140
				$obs['famille']." ".$obs['zone_geo']." ".$obs['ce_zone_geo']." ".$obs['date_observation']." ".$obs['station'];
141
				$obs['commentaire'];
2450 jpm 142
 
885 aurelien 143
			$pdf->write(10, $text."\n");
2450 jpm 144
 
885 aurelien 145
			$projet_photo = 'photoflora';
2458 jpm 146
			$tab_retour[$projet_photo] = $this->analyserRdf(sprintf(EF_URL_PHOTOFLORA_SERVICE, $obs['nt']));
147
			$url_miniature = '';
885 aurelien 148
			foreach ($tab_retour[$projet_photo] as $cle => $illustration) {
149
				if (preg_match(EF_URL_PHOTOFLORA_REGEXP, $illustration['about'], $match)) {
2458 jpm 150
					$abreviation = $match[1];
151
					$fichier = $match[2];
152
					$url_miniature = sprintf(EF_URL_PHOTOFLORA_IMG_MIN, $abreviation, $fichier);;
2450 jpm 153
 
2458 jpm 154
					// Priorite aux images en png
885 aurelien 155
					if (strstr($fichier, '.png')) {
156
						break;
157
					}
158
				}
159
			}
160
 
2458 jpm 161
			if ($url_miniature != '') {
162
				list($debut,$ext)=explode('\.',basename($url_miniature));
163
				$temp = fopen($tempfn, 'w');
885 aurelien 164
				$buf=file_get_contents($url_miniature);
165
				fwrite($temp,$buf);
2458 jpm 166
				fclose($temp);
167
				$pdf->image($tempfn, 10, ($i*10), 0, 0, $ext);
885 aurelien 168
			}
2458 jpm 169
			$i++;
170
		}
171
		echo $pdf->output('Rapport');
416 aurelien 172
	}
173
 
2458 jpm 174
	private function analyserRdf($chemin) {
416 aurelien 175
		$aso_info = array();
176
		$dom = new DOMDocument();
177
		$dom->validateOnParse = true;
178
		if (preg_match('/^http:\/\//', $chemin)) {
179
			$dom->loadXML(file_get_contents($chemin));
180
		} else {
181
			$dom->load($chemin);
182
		}
2450 jpm 183
 
416 aurelien 184
		$tab_infos = array();
185
		foreach ($dom->getElementsByTagNameNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'Description') as $rdf_description) {
186
			$aso_info['about'] = $rdf_description->getAttribute('about');
187
			$aso_info['dc:identifier'] = $rdf_description->getAttribute('identifier');
188
			$aso_info['dc:title'] = utf8_decode($rdf_description->getAttribute('title'));
189
			$aso_info['dc:creator'] = utf8_decode($rdf_description->getAttribute('creator'));
190
			$aso_info['dc:contributor'] = utf8_decode($rdf_description->getAttribute('contributor'));
191
			$aso_info['dc:publisher'] = utf8_decode($rdf_description->getAttribute('publisher'));
192
			$aso_info['dc:type'] = utf8_decode($rdf_description->getAttribute('type'));
193
			$aso_info['dc:format'] = utf8_decode($rdf_description->getAttribute('format'));
194
			if (function_exists('date_default_timezone_set')) {
195
				date_default_timezone_set('Europe/Paris');
196
			}
197
			if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $rdf_description->getAttribute('created'))) {
2450 jpm 198
				$aso_info['dcterms:created'] = date('j-m-Y � H:i:s', strtotime($rdf_description->getAttribute('created')));
416 aurelien 199
			} else {
200
				$aso_info['dcterms:created'] = $rdf_description->getAttribute('created');
201
			}
202
			$aso_info['dcterms:dateSubmitted'] = utf8_decode($rdf_description->getAttribute('dateSubmitted'));
203
			$aso_info['dcterms:spatial'] = utf8_decode($rdf_description->getAttribute('spatial'));
204
			$aso_info['dcterms:licence'] = utf8_decode($rdf_description->getAttribute('licence'));
2450 jpm 205
			$tab_infos[$rdf_description->getAttribute('identifier')] = $aso_info;
416 aurelien 206
		}
207
		return $tab_infos;
2458 jpm 208
	}
209
}