Subversion Repositories eFlore/Applications.cel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
416 aurelien 1
<?php
985 aurelien 2
//TODO migrer cette classe lorsqu'elle deviendra utile à nouveau
1477 aurelien 3
/****
4
 *
5
 * Attention classe non migrée !!!!
6
 */
1610 raphael 7
 
8
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(dirname(realpath(__FILE__))) . '/lib');
9
// la sortie est binaire (xls), mais OLE n'est pas compatible E_ALL en PHP-5.4
10
error_reporting(error_reporting() ^ E_STRICT);
11
require_once("lib/OLE.php");
12
require_once("lib/Spreadsheet/Excel/Writer.php");
13
 
416 aurelien 14
Class InventoryImageExport extends DBAccessor {
15
 
16
	private $extendSpreadsheetProductor;
17
 
18
	private $archive;
19
	private $workbook;
20
	private $worksheet;
21
	private $chemin_export_liste;
22
 
23
    function InventoryImageExport($config) {
24
 
25
    	parent::__construct($config);
26
 
27
        $this->config=$config;
28
        $this->extendSpreadsheetProductor = new SpreadsheetProductor();
29
		$this->extendSpreadsheetProductor->initSpreadsheet();
30
    }
31
 
32
    function getElement($uid){
33
 
34
      	$this->authentifier();
35
 
36
      	$tableau = array();
37
        $tag = $uid[0];
38
 
39
        $limite = 0;
40
        $pas = 0;
41
 
42
        //ini_set('max_execution_time',120);
43
 
44
        /*if(isset($uid[1]) && isset($uid[2])) {
45
			$limite = $uid[]
46
        }*/
47
 
48
       	$taille_archive_courante = 0;
49
		$index_archive_courante = 0;
1391 aurelien 50
		$taille_max_archive = $this->config['cel']['taille_max_archive'];
416 aurelien 51
		$liens_archives = array();
52
 
1391 aurelien 53
        $DB=$this->connectDB($this->config,'cel');
416 aurelien 54
 
55
        $query_id_id_img = 'SELECT cmc_id_mot_cle_utilisateur, cmc_id_proprietaire FROM cel_mots_cles_images WHERE cmc_id_mot_cle_general = md5("'.$DB->escapeSimple($tag).'")' ;
56
 
57
        $res =& $DB->query($query_id_id_img);
58
 
59
        if (DB::isError($res)) {
60
			$this->logger("InventoryImageExport",'Erreur de requete '.$query_id_id_img);
61
            die($res->getMessage());
62
        }
63
 
64
 
65
        $query='SELECT * FROM cel_images';
66
 
67
        $premier_item = true ;
68
        while ($row =& $res->fetchrow(DB_FETCHMODE_ASSOC)) {
69
 
70
        	$tableau['mots cles'] = array('utilisateur' => $row['cmc_id_proprietaire'], 'mot cle' => $row['cmc_id_mot_cle_utilisateur']);
71
 
72
            if($premier_item) {
73
                $query .= ' WHERE ';
74
                $premier_item = false ;
75
            }
76
            else{
77
                $query .= ' OR ';
78
            }
79
 
80
            $query .= '(ci_meta_mots_cles LIKE "%'.$row['cmc_id_mot_cle_utilisateur'].'%" AND ci_ce_utilisateur ="'.$row['cmc_id_proprietaire'].'")' ;
81
        }
82
 
83
        $query .= ' ORDER BY ci_meta_date_ajout' ;
84
 
85
        $res =& $DB->query($query);
86
 
87
        if (DB::isError($res)) {
88
            die($res->getMessage());
89
        }
90
 
91
        $this->logger('Requetes',$query);
92
 
93
        // création d'un objet 'zipfile'
94
		$this->archive = new zipfile();
95
 
96
		$i = 1;
97
 
98
		$this->initialiserWorkBook($index_archive_courante);
99
 
100
        while ($image =& $res->fetchrow(DB_FETCHMODE_ASSOC)) {
101
 
102
           $image['ci_nom_original'] = htmlspecialchars($image['ci_nom_original']);
103
           $image['ci_id_image'] = htmlspecialchars($image['ci_id_image']);
104
           $image['ci_meta_date_ajout'] = htmlspecialchars($image['ci_meta_date_ajout']);
105
           $image['ci_ce_utilisateur'] = htmlspecialchars($image['ci_ce_utilisateur']);
106
           $image['ci_meta_user_comment'] = htmlspecialchars($image['ci_meta_user_comment']);
107
           $image['ci_note_image'] = htmlspecialchars($image['ci_note_image']);
108
 
109
           $id = $image['ci_id_image'];
110
 
111
           $tableau[$id]['image'] = $image;
112
 
113
           if($filename = $this->renvoyerCheminSurServeur($id,false)) {
114
 
115
				// appel de la classe
116
			    // nom du fichier à ajouter dans l'archive
117
			    // contenu du fichier
118
			    $fp = fopen ($filename, 'r');
119
			    $content = fread($fp, filesize($filename));
120
			    fclose ($fp);
121
 
122
			    // ajout du fichier dans cet objet
123
			    if(preg_match('/\.(?:jpg|jpeg)$/i',$image['ci_nom_original'])) {
124
			    	$nom_fichier_image = preg_replace('/\.(?:jpg|jpeg)$/i','_'.$id.'.jpg', $image['ci_nom_original']);
125
			    } else {
126
			    	$nom_fichier_image = $image['ci_nom_original'].'_'.$id.'.jpg';
127
			    }
128
 
1391 aurelien 129
			    $chemin_sur_serveur = $this->config['cel']['url_images'];
416 aurelien 130
 
131
			    $id = sprintf('%09s', $id) ;
132
            	$id = wordwrap($id, 3 , '_', true) ;
996 aurelien 133
			    $niveauDossier = explode("_", $id) ;
416 aurelien 134
 
135
	            $dossierNiveau1 = $niveauDossier[0] ;
136
	            $dossierNiveau2 = $niveauDossier[1] ;
137
 
138
	            $chemin_sur_serveur_final = $chemin_sur_serveur.'/'.$dossierNiveau1.'/'.$dossierNiveau2 ;
139
 
666 aurelien 140
	            $chemin_fichier = $chemin_sur_serveur_final.'/O/'.$id."_O.jpg" ;
416 aurelien 141
 
142
			    $obs_liee['url_image_liee'] = $chemin_fichier;
143
 
144
			    $taille_fichier = filesize($filename);
145
 
146
            	$req_liaison = 'SELECT * FROM cel_inventory WHERE ordre IN (SELECT coi_ce_observation FROM cel_obs_images WHERE coi_ce_image = "'.$image['ci_id_image'].'") AND identifiant = "'.$image['ci_ce_utilisateur'].'"' ;
147
            	$res_liaison =& $DB->query($req_liaison);
148
 
149
	            if (DB::isError($res_liaison)) {
150
	                die($res_liaison->getMessage());
151
	            }
152
 
153
	            if (DB::isError($res_liaison)) {
154
	                die($res_liaison->getMessage());
155
	            }
156
 
157
	          while($obs_liee = & $res_liaison->fetchrow(DB_FETCHMODE_ASSOC)) {
158
 
159
          	  	$tableau[$id]['obs'] = $obs_liee;
160
          	  	$obs_liee['nom_image_liee'] = $nom_fichier_image;
161
          	  	$obs_liee['url_image_liee'] = $chemin_fichier;
162
 
163
			  	$this->ecrireLigneWorkBook($i,$obs_liee);
164
 
165
		  		$i++;
166
	          }
167
 
168
	          //$this->archive->addfile($content, $nom_fichier_image);
169
			  //$taille_archive_courante += $taille_fichier;
170
 
171
	          if($taille_archive_courante <= $taille_max_archive) {
172
 
173
 
174
				} else {
175
 
176
					// fermeture du workbook
177
					$this->workbook->close();
178
					$i = 1;
179
 
180
					// ajout du fichier liste dans cet objet
181
					// contenu du fichier
182
				    $fp = fopen($this->chemin_export_liste, 'r');
183
				    $contenu = fread($fp, filesize($this->chemin_export_liste));
184
				    fclose ($fp);
185
 
186
					$this->archive->addfile($contenu,'liste.'.md5($tag).'_'.$index_archive_courante.'.xls');
187
 
188
					$liens_archives[] = $this->enregistrerArchive($this->archive,$index_archive_courante,$tag);
189
					$index_archive_courante++;
190
					$taille_archive_courante = 0;
191
 
192
					$this->archive = new zipfile();
193
					$this->initialiserWorkBook($index_archive_courante);
194
				}
195
	       }
196
 
197
      		//$this->verifierOuRelancerExecution();
198
      		//$this->logger('InventoryImageExport'," Temps d'éxécution à l'image ".$id." : ".$this->getTempsEcoule());
199
        }
200
 
201
        // fermeture du workbook
202
		$this->workbook->close();
203
		$i = 1;
204
 
205
		// ajout du fichier liste dans cet objet
206
		// contenu du fichier
207
	    $fp = fopen($this->chemin_export_liste, 'r');
208
	    $contenu = fread($fp, filesize($this->chemin_export_liste));
209
	    fclose ($fp);
210
 
211
		$this->archive->addfile($contenu,'liste.'.md5($tag).'_'.$index_archive_courante.'.xls');
212
 
213
		$liens_archives[] = $this->enregistrerArchive($this->archive,$index_archive_courante,$tag);
214
		$index_archive_courante++;
215
 
216
		$j = 1;
217
		$sortie = '<div><ul>';
218
		foreach($liens_archives as $lien) {
219
			$sortie .= '<li> <a href = "'.$lien.'"> Partie '.$j.'</a></li>';
220
			$j++;
221
		}
222
 
223
		$sortie .= '</div></ul>';
224
 
225
		//$this->logger('InventoryImageExport',count($tableau));
226
 
227
		//$this->logger('InventoryImageExport',print_r($tableau,true));
228
 
229
		header('Content-Type: text/html');
230
		echo $sortie;
231
 
232
    }
233
 
234
 
235
    function getRessource(){
236
		//$this->getElement();
237
    }
238
 
239
    public function authentifier() {
240
        if (!isset($_SERVER['PHP_AUTH_USER'])) {
241
        header('WWW-Authenticate: Basic realm="www.tela-botanica.org"');
242
        header('HTTP/1.0 401 Unauthorized');
243
        header('Content-type: text/html; charset=UTF-8');
244
        echo 'Accès interdit';
245
        exit;
246
        } else {
247
            if($this->verifierAcces($_SERVER['PHP_AUTH_USER'])) {
248
                return ;
249
            }
250
            else
251
            {
252
                header('WWW-Authenticate: Basic realm="www.tela-botanica.org"');
253
                header('HTTP/1.0 401 Unauthorized');
254
                header('Content-type: text/html; charset=UTF-8');
255
                echo 'Accès interdit';
256
                exit ;
257
            }
258
        }
259
    }
260
 
261
    public function verifierAcces($id) {
262
 
263
        $DB=$this->connectDB($this->config,'database_ident');
264
        $query="SELECT ".$this->config['database_ident']['ann_id']." as name FROM ".$this->config['database_ident']['annuaire']." WHERE ".$this->config['database_ident']['ann_id']." ='".$DB->escapeSimple($id)
265
        ."' AND ".$this->config['database_ident']['ann_pwd']." = ".$this->config['database_ident']['pass_crypt_funct']."('".$DB->escapeSimple($_SERVER['PHP_AUTH_PW'])."')" ;
266
 
267
        $res =& $DB->getOne($query);
268
 
269
        if($res == "") {
270
            return false ;
271
        }
272
 
273
        if (DB::isError($res)) {
274
            die($res->getMessage());
275
        }
276
 
277
        return $this->isAdmin($id) ;
278
 
279
    }
280
 
281
    private function renvoyerCheminSurServeur($id,$url = true) {
282
 
283
		if($url) {
1391 aurelien 284
			$chemin_sur_serveur = $this->config['cel']['url_images'];
416 aurelien 285
		} else {
1391 aurelien 286
			$chemin_sur_serveur = $this->config['cel']['chemin_images'];
416 aurelien 287
		}
288
 
289
		$id = sprintf('%09s', $id) ;
290
        $id = wordwrap($id, 3 , '_', true) ;
291
 
292
        $id_fichier = $id.".jpg" ;
293
 
996 aurelien 294
        $niveauDossier = explode("_", $id) ;
416 aurelien 295
 
296
        $dossierNiveau1 = $niveauDossier[0] ;
297
        $dossierNiveau2 = $niveauDossier[1] ;
298
 
299
        $chemin_sur_serveur_final = $chemin_sur_serveur.'/'.$dossierNiveau1.'/'.$dossierNiveau2 ;
300
 
301
        $chemin_fichier = $chemin_sur_serveur_final.'/L/'.$id."_L.jpg" ;
302
 
303
        return $chemin_fichier;
304
    }
305
 
306
    private function renvoyerCheminExport($url = true) {
307
    	if($url) {
1391 aurelien 308
    		return $this->config['cel']['url_export'];
416 aurelien 309
    	} else {
1391 aurelien 310
			return $this->config['cel']['chemin_export'];
416 aurelien 311
    	}
312
    }
313
 
314
    private function enregistrerArchive($zip,$index,$tag) {
315
 
316
		// production de l'archive' Zip
317
	    $this->archive = $zip->file();
318
		$chemin_export = $this->renvoyerCheminExport(false);
319
		$url_export = $this->renvoyerCheminExport(true);
320
 
321
		$chemin_archive = $chemin_export.'/'.md5($tag).'_'.$index.'.zip';
322
		$url_archive = $url_export.'/'.md5($tag).'_'.$index.'.zip';
323
 
324
		$fp = fopen($chemin_archive,'w+');
325
	    $ecriture = fwrite($fp,$this->archive);
326
	    fclose($fp);
327
 
328
	    if($ecriture) {
329
			return $url_archive;
330
	    } else {
331
	    	return false;
332
	    }
333
 
334
    }
335
 
336
    private function initialiserWorkBook($index) {
337
 
338
    	// Creating a workbook
339
        $this->chemin_export_liste = $this->renvoyerCheminExport(false).'/liste'.$index.'.xls';
340
		$this->workbook = new Spreadsheet_Excel_Writer($this->chemin_export_liste);
1609 raphael 341
		$workbook->setVersion(8);
416 aurelien 342
 
343
		// Creating a worksheet
344
		$this->worksheet = $this->workbook->addWorksheet('Liste');
1609 raphael 345
		$worksheet->setInputEncoding('utf-8');
416 aurelien 346
 
347
		$this->worksheet->write(0,0,'url de l\'image');
348
		$this->worksheet->write(0,1,'Nom original de l\'image');
349
        //$this->worksheet->write(0,1,'Nom saisi');
350
        //$this->worksheet->write(0,2,'Numero nomenclatural');
351
        $this->worksheet->write(0,2,'Nom retenu');
352
        $this->worksheet->write(0,3,'Numero nomenclatural nom retenu');
353
        $this->worksheet->write(0,4,'Numero taxonomique');
354
        $this->worksheet->write(0,5,'Famille');
355
        //$this->worksheet->write(0,7,'Commune');
356
        //$this->worksheet->write(0,8,'Identifiant Commune');
357
        $this->worksheet->write(0,6,'Date Observation');
358
       // $this->worksheet->write(0,10,'Lieu dit');
359
        //$this->worksheet->write(0,11,'Station');
360
        //$this->worksheet->write(0,12,'Milieu');
361
        $this->worksheet->write(0,7,'Contributeur');
362
        $this->worksheet->write(0,8,'Commentaire');
363
    }
364
 
365
    private function ecrireLigneWorkBook($index, $observation) {
366
 
367
    	$this->worksheet->write($index,0,$observation['url_image_liee']);
368
    	$this->worksheet->write($index,1,$observation['nom_image_liee']);
369
	  	//$this->worksheet->write($index,1,$observation['nom_sel']);
370
		//$this->worksheet->write($index,2,$observation['num_nom_sel']);
371
		$this->worksheet->write($index,2,$observation['nom_ret']);
372
		$this->worksheet->write($index,3,$observation['num_nom_ret']);
373
		$this->worksheet->write($index,4,$observation['num_taxon']);
374
		$this->worksheet->write($index,5,$observation['famille']);
375
		//$this->worksheet->write($index,7,$observation['location']);
376
		//$this->worksheet->write($index,8,$observation['id_location']);
377
		$this->worksheet->write($index,6,$observation['date_observation']);
378
		//$this->worksheet->write($index,10,$observation['lieudit']);
379
		//$this->worksheet->write($index,11,$observation['station']);
380
	 	//$this->worksheet->write($index,12,$observation['milieu']);
381
	 	$this->worksheet->write($index,7,$observation['identifiant']);
382
		$this->worksheet->write($index,8,$observation['commentaire']);
383
    }
384
}
385
 
386
 
387
/* +--Fin du code ---------------------------------------------------------------------------------------+
388
* $Log$
389
* Revision 1.5  2008-11-13 11:29:12  ddelon
390
* Reecriture gwt-ext
391
*
392
* Revision 1.4  2007-06-06 13:31:16  ddelon
393
* v0.09
394
*
395
* Revision 1.3  2007-05-22 12:54:09  ddelon
396
* Securisation acces utilisateur
397
*
398
*
399
*
400
*/
401
 
402
?>