Subversion Repositories eFlore/Applications.cel

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

<?php


Class InventoryImageExport extends DBAccessor {

        private $extendSpreadsheetProductor;

        private $archive;
        private $workbook;
        private $worksheet;
        private $chemin_export_liste;

    function InventoryImageExport($config) {

        parent::__construct($config);

                require_once('lib/zip.php');

        $this->config=$config;
        $this->extendSpreadsheetProductor = new SpreadsheetProductor();
                $this->extendSpreadsheetProductor->initSpreadsheet();
    }

    function getElement($uid){
        
        $this->authentifier();

        $tableau = array();

        $tag = $uid[0];

        $limite = 0;
        $pas = 0;

        //ini_set('max_execution_time',120);

        /*if(isset($uid[1]) && isset($uid[2])) {
                        $limite = $uid[]
        }*/

        $taille_archive_courante = 0;
                $index_archive_courante = 0;
                $taille_max_archive = $this->config['cel_db']['taille_max_archive'];
                $liens_archives = array();

        $DB=$this->connectDB($this->config,'cel_db');

        $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).'")' ;

        $res =& $DB->query($query_id_id_img);

        if (DB::isError($res)) {
                        $this->logger("InventoryImageExport",'Erreur de requete '.$query_id_id_img);
            die($res->getMessage());
        }


        $query='SELECT * FROM cel_images';

        $premier_item = true ;
        while ($row =& $res->fetchrow(DB_FETCHMODE_ASSOC)) {

                $tableau['mots cles'] = array('utilisateur' => $row['cmc_id_proprietaire'], 'mot cle' => $row['cmc_id_mot_cle_utilisateur']);

            if($premier_item) {
                $query .= ' WHERE ';
                $premier_item = false ;
            }
            else{
                $query .= ' OR ';
            }

            $query .= '(ci_meta_mots_cles LIKE "%'.$row['cmc_id_mot_cle_utilisateur'].'%" AND ci_ce_utilisateur ="'.$row['cmc_id_proprietaire'].'")' ;
        }

        $query .= ' ORDER BY ci_meta_date_ajout' ;

        $res =& $DB->query($query);

        if (DB::isError($res)) {
            die($res->getMessage());
        }

        $this->logger('Requetes',$query);

        // création d'un objet 'zipfile'
                $this->archive = new zipfile();

                $i = 1;

                $this->initialiserWorkBook($index_archive_courante);

        while ($image =& $res->fetchrow(DB_FETCHMODE_ASSOC)) {

           $image['ci_nom_original'] = htmlspecialchars($image['ci_nom_original']);
           $image['ci_id_image'] = htmlspecialchars($image['ci_id_image']);
           $image['ci_meta_date_ajout'] = htmlspecialchars($image['ci_meta_date_ajout']);
           $image['ci_ce_utilisateur'] = htmlspecialchars($image['ci_ce_utilisateur']);
           $image['ci_meta_user_comment'] = htmlspecialchars($image['ci_meta_user_comment']);
           $image['ci_note_image'] = htmlspecialchars($image['ci_note_image']);

           $id = $image['ci_id_image'];

           $tableau[$id]['image'] = $image;
                   
           if($filename = $this->renvoyerCheminSurServeur($id,false)) {

                                // appel de la classe
                            // nom du fichier à ajouter dans l'archive
                            // contenu du fichier
                            $fp = fopen ($filename, 'r');
                            $content = fread($fp, filesize($filename));
                            fclose ($fp);

                            // ajout du fichier dans cet objet
                            if(preg_match('/\.(?:jpg|jpeg)$/i',$image['ci_nom_original'])) {
                                $nom_fichier_image = preg_replace('/\.(?:jpg|jpeg)$/i','_'.$id.'.jpg', $image['ci_nom_original']);
                            } else {
                                $nom_fichier_image = $image['ci_nom_original'].'_'.$id.'.jpg';
                            }
                            
                            $chemin_sur_serveur = $this->config['cel_db']['url_images'];
                            
                            $id = sprintf('%09s', $id) ;
                $id = wordwrap($id, 3 , '_', true) ;
                            $niveauDossier = split("_", $id) ;

                    $dossierNiveau1 = $niveauDossier[0] ;
                    $dossierNiveau2 = $niveauDossier[1] ;
        
                    $chemin_sur_serveur_final = $chemin_sur_serveur.'/'.$dossierNiveau1.'/'.$dossierNiveau2 ;
        
                    $chemin_fichier = $chemin_sur_serveur_final.'/L/'.$id."_L.jpg" ;
                            
                            $obs_liee['url_image_liee'] = $chemin_fichier;

                            $taille_fichier = filesize($filename);

                $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'].'"' ;
                $res_liaison =& $DB->query($req_liaison);

                    if (DB::isError($res_liaison)) {
                        die($res_liaison->getMessage());
                    }

                    if (DB::isError($res_liaison)) {
                        die($res_liaison->getMessage());
                    }

                  while($obs_liee = & $res_liaison->fetchrow(DB_FETCHMODE_ASSOC)) {

                        $tableau[$id]['obs'] = $obs_liee;
                        $obs_liee['nom_image_liee'] = $nom_fichier_image;
                        $obs_liee['url_image_liee'] = $chemin_fichier;

                                $this->ecrireLigneWorkBook($i,$obs_liee);

                                $i++;
                  }

                  //$this->archive->addfile($content, $nom_fichier_image);
                          //$taille_archive_courante += $taille_fichier;

                  if($taille_archive_courante <= $taille_max_archive) {


                                } else {

                                        // fermeture du workbook
                                        $this->workbook->close();
                                        $i = 1;

                                        // ajout du fichier liste dans cet objet
                                        // contenu du fichier
                                    $fp = fopen($this->chemin_export_liste, 'r');
                                    $contenu = fread($fp, filesize($this->chemin_export_liste));
                                    fclose ($fp);

                                        $this->archive->addfile($contenu,'liste.'.md5($tag).'_'.$index_archive_courante.'.xls');
                                        
                                        $liens_archives[] = $this->enregistrerArchive($this->archive,$index_archive_courante,$tag);
                                        $index_archive_courante++;
                                        $taille_archive_courante = 0;

                                        $this->archive = new zipfile();
                                        $this->initialiserWorkBook($index_archive_courante);
                                }
               }

                //$this->verifierOuRelancerExecution();
                //$this->logger('InventoryImageExport'," Temps d'éxécution à l'image ".$id." : ".$this->getTempsEcoule());
        }
        
        // fermeture du workbook
                $this->workbook->close();
                $i = 1;

                // ajout du fichier liste dans cet objet
                // contenu du fichier
            $fp = fopen($this->chemin_export_liste, 'r');
            $contenu = fread($fp, filesize($this->chemin_export_liste));
            fclose ($fp);

                $this->archive->addfile($contenu,'liste.'.md5($tag).'_'.$index_archive_courante.'.xls');
                
                $liens_archives[] = $this->enregistrerArchive($this->archive,$index_archive_courante,$tag);
                $index_archive_courante++;

                $j = 1;
                $sortie = '<div><ul>';
                foreach($liens_archives as $lien) {
                        $sortie .= '<li> <a href = "'.$lien.'"> Partie '.$j.'</a></li>';
                        $j++;
                }

                $sortie .= '</div></ul>';

                //$this->logger('InventoryImageExport',count($tableau));

                //$this->logger('InventoryImageExport',print_r($tableau,true));

                header('Content-Type: text/html');
                echo $sortie;

    }


    function getRessource(){
                //$this->getElement();
    }

    public function authentifier() {
        if (!isset($_SERVER['PHP_AUTH_USER'])) {
        header('WWW-Authenticate: Basic realm="www.tela-botanica.org"');
        header('HTTP/1.0 401 Unauthorized');
        header('Content-type: text/html; charset=UTF-8');
        echo 'Accès interdit';
        exit;
        } else {
            if($this->verifierAcces($_SERVER['PHP_AUTH_USER'])) {
                return ;
            }
            else
            {
                header('WWW-Authenticate: Basic realm="www.tela-botanica.org"');
                header('HTTP/1.0 401 Unauthorized');
                header('Content-type: text/html; charset=UTF-8');
                echo 'Accès interdit';
                exit ;
            }
        }
    }

    public function verifierAcces($id) {

        $DB=$this->connectDB($this->config,'database_ident');
        $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)
        ."' AND ".$this->config['database_ident']['ann_pwd']." = ".$this->config['database_ident']['pass_crypt_funct']."('".$DB->escapeSimple($_SERVER['PHP_AUTH_PW'])."')" ;

        $res =& $DB->getOne($query);

        if($res == "") {
            return false ;
        }

        if (DB::isError($res)) {
            die($res->getMessage());
        }

        return $this->isAdmin($id) ;

    }

    private function renvoyerCheminSurServeur($id,$url = true) {

                if($url) {
                        $chemin_sur_serveur = $this->config['cel_db']['url_images'];
                } else {
                        $chemin_sur_serveur = $this->config['cel_db']['chemin_images'];
                }

                $id = sprintf('%09s', $id) ;
        $id = wordwrap($id, 3 , '_', true) ;

        $id_fichier = $id.".jpg" ;

        $niveauDossier = split("_", $id) ;

        $dossierNiveau1 = $niveauDossier[0] ;
        $dossierNiveau2 = $niveauDossier[1] ;

        $chemin_sur_serveur_final = $chemin_sur_serveur.'/'.$dossierNiveau1.'/'.$dossierNiveau2 ;

        $chemin_fichier = $chemin_sur_serveur_final.'/L/'.$id."_L.jpg" ;

        return $chemin_fichier;
    }

    private function renvoyerCheminExport($url = true) {
        if($url) {
                return $this->config['cel_db']['url_export'];
        } else {
                        return $this->config['cel_db']['chemin_export'];
        }
    }

    private function enregistrerArchive($zip,$index,$tag) {

                // production de l'archive' Zip
            $this->archive = $zip->file();
                $chemin_export = $this->renvoyerCheminExport(false);
                $url_export = $this->renvoyerCheminExport(true);

                $chemin_archive = $chemin_export.'/'.md5($tag).'_'.$index.'.zip';
                $url_archive = $url_export.'/'.md5($tag).'_'.$index.'.zip';

                $fp = fopen($chemin_archive,'w+');
            $ecriture = fwrite($fp,$this->archive);
            fclose($fp);

            if($ecriture) {
                        return $url_archive;
            } else {
                return false;
            }

    }

    private function initialiserWorkBook($index) {

        // Creating a workbook
        $this->chemin_export_liste = $this->renvoyerCheminExport(false).'/liste'.$index.'.xls';
                $this->workbook = new Spreadsheet_Excel_Writer($this->chemin_export_liste);

                // Creating a worksheet
                $this->worksheet = $this->workbook->addWorksheet('Liste');

                $this->worksheet->write(0,0,'url de l\'image');
                $this->worksheet->write(0,1,'Nom original de l\'image');
        //$this->worksheet->write(0,1,'Nom saisi');
        //$this->worksheet->write(0,2,'Numero nomenclatural');
        $this->worksheet->write(0,2,'Nom retenu');
        $this->worksheet->write(0,3,'Numero nomenclatural nom retenu');
        $this->worksheet->write(0,4,'Numero taxonomique');
        $this->worksheet->write(0,5,'Famille');
        //$this->worksheet->write(0,7,'Commune');
        //$this->worksheet->write(0,8,'Identifiant Commune');
        $this->worksheet->write(0,6,'Date Observation');
       // $this->worksheet->write(0,10,'Lieu dit');
        //$this->worksheet->write(0,11,'Station');
        //$this->worksheet->write(0,12,'Milieu');
        $this->worksheet->write(0,7,'Contributeur');
        $this->worksheet->write(0,8,'Commentaire');
    }

    private function ecrireLigneWorkBook($index, $observation) {

        $this->worksheet->write($index,0,$observation['url_image_liee']);
        $this->worksheet->write($index,1,$observation['nom_image_liee']);
                //$this->worksheet->write($index,1,$observation['nom_sel']);
                //$this->worksheet->write($index,2,$observation['num_nom_sel']);
                $this->worksheet->write($index,2,$observation['nom_ret']);
                $this->worksheet->write($index,3,$observation['num_nom_ret']);
                $this->worksheet->write($index,4,$observation['num_taxon']);
                $this->worksheet->write($index,5,$observation['famille']);
                //$this->worksheet->write($index,7,$observation['location']);
                //$this->worksheet->write($index,8,$observation['id_location']);
                $this->worksheet->write($index,6,$observation['date_observation']);
                //$this->worksheet->write($index,10,$observation['lieudit']);
                //$this->worksheet->write($index,11,$observation['station']);
                //$this->worksheet->write($index,12,$observation['milieu']);
                $this->worksheet->write($index,7,$observation['identifiant']);
                $this->worksheet->write($index,8,$observation['commentaire']);
    }
}


/* +--Fin du code ---------------------------------------------------------------------------------------+
* $Log$
* Revision 1.5  2008-11-13 11:29:12  ddelon
* Reecriture gwt-ext
*
* Revision 1.4  2007-06-06 13:31:16  ddelon
* v0.09
*
* Revision 1.3  2007-05-22 12:54:09  ddelon
* Securisation acces utilisateur
*
*
*
*/

?>