Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

<?php
class TaxonDao extends Bdd {
        // tableau de la forme $taxons[num][flore][id]
        // $taxons[num][flore][nom]
        protected $taxons = array();
        private $id = 0;
        private $flores = array('syntri', 'ciff', 'codefr94', 'bdnff', 'bryo', 'floeur', 'algues', 'characees', 'lichen', 'fournier');
        private $table_flores = array('bdnff', 'fournier', 'ciff', 'syntri', 'codefr94',  'bryo', 'floeur');
        
        public function __construct() {
                Bdd::__construct();
                $this->id = $this->chargerId() + 1;
                $this->taxons = $this->chargerTaxon();
        }
        
        // recherche si un taxon existe sinon le crée et renvoie son id
        public function getId($num_taxon, $flore = 'ind', $nom = null, $flore_supp = null, $num_supp = null, $remarques = '') {
        
                $idTaxon = null; $nom_supp = $nom;
                if ($nom == null && $remarques != '') {
                        $nom = "rem:".$flore_supp.$num_supp.'/'.$remarques;
                }

                if (isset($this->taxons['ind'][$nom]['id'])) {
                        $idTaxon = $this->taxons['ind'][$nom]['id'];
                } elseif (isset($this->taxons[$flore][$num_taxon]['id']) && $nom == null) {
                        $idTaxon = $this->taxons[$flore][$num_taxon]['id'];
                } else {
                        $idTaxon = $this->ajouterTaxon($num_taxon, $flore, $nom_supp, $flore_supp, $num_supp, $remarques);
                }
                return $idTaxon;
        }

        // renvoie un nom d'apres son id
        public function getNom($id) {
                foreach ($this->taxons['ind'] as $nom=>$param) {
                        if ($param['id'] === $id) {
                                if (preg_match('/rem:[a-z]+\d+\/(.*)/', $nom, $match)) {
                                        $nom = $match[1];
                                }
                                return $nom;
                        } 
                }
                return '';
        }
        
        // Ajoute taxon avec pour clé le numéro taxon, le nom ou la remarque
        public function ajouterTaxon($num_taxon, $flore, $nom, $flore_supp, $num_supp, $remarques) {
                if ($nom != null) { 
                        $cle = 'ind';
                        $num = $nom;
                } elseif ($remarques != '') {
                        $cle = 'ind';
                        $num = "rem:".$flore_supp.$num_supp.'/'.$remarques;
                } else {
                        $cle = $flore;
                        $num = $num_taxon;
                }
                $this->taxons[$cle][$num]['id'] = $this->id;
                foreach ($this->flores as $nom_flore) {
                        if ($nom_flore == $flore) {
                                $this->taxons[$cle][$num][$nom_flore] = $num_taxon; 
                        } elseif ($nom_flore == $flore_supp) {
                                $this->taxons[$cle][$num][$nom_flore] = $num_supp;
                        } else {
                                $this->taxons[$cle][$num][$nom_flore] = 'NULL';
                        }
                }
                $this->taxons[$cle][$num]['nom'] = $this->ajouterNomTaxon($num_taxon, $flore, $nom, $flore_supp, $num_supp);
                $this->taxons[$cle][$num]['remarques'] = $remarques;
                $this->id++;
                return $this->taxons[$cle][$num]['id'];
        }
        
        public function ajouterNomTaxon($num_taxon, $flore, $nom_supp, $flore_supp, $num_supp) {
                $nom = '';
                $nomCherche = false;
                foreach ($this->table_flores as $nom_flore) {
                        if ($nomCherche == false) {
                                if ($nom_flore == $flore) {
                                        $nom = $this->rechercherNomTaxon($nom_flore, $num_taxon);
                                        if ($nom != false) {
                                                $nomCherche = true;
                                        }       
                                } elseif ($nom_flore == $flore_supp) {
                                        $nom = $this->rechercherNomTaxon($nom_flore, $num_supp);
                                        if ($nom != false) {
                                                $nomCherche = true;
                                        }
                                }
                        }
                }
                if ($nom == false && $nom_supp != null) {
                        $nom = $nom_supp;
                } elseif ($nom == '' && $nom_supp != null) {
                        $nom = $nom_supp;
                }
                return $nom;
        }
        
        public function rechercherNomTaxon($nom_flore, $num_taxon) {
                $requete2 = null;
                switch ($nom_flore) {
                        case 'bdnff' :
                                $requete = "SELECT sb_nom_complet AS nom FROM sophy_bdnff 
                                        WHERE sb_num_tax = {$num_taxon} AND sb_id_num_bdnff = sb_num_nom_retenu; ";
                                break;
                        case 'fournier' :
                                $requete = "SELECT sb_nom_complet AS nom FROM sophy_bdnff, sophy_fournier_bdnff 
                                        WHERE sfb_id_num_fournier = {$num_taxon} AND sb_num_tax = sfb_id_num_bdnff AND sb_id_num_bdnff = sb_num_nom_retenu;";
                                $requete2 = "SELECT sf_nom_fournier AS nom FROM sophy_fournier WHERE sf_id_num_fournier = {$num_taxon};";
                                break;  
                        case 'ciff' :   
                                $requete = "SELECT sb_nom_complet AS nom 
                                        FROM sophy_bdnff, sophy_ciff_bdnff
                                        WHERE scb_id_num_ciff = {$num_taxon} AND scb_id_num_bdnff = sb_id_num_bdnff;";
                                $requete2 = "SELECT sci_nom_ciff AS nom FROM sophy_ciff WHERE sci_id_num_ciff = {$num_taxon};";
                                break;
                        case 'syntri' :
                                $requete = "SELECT sb_nom_complet AS nom 
                                        FROM sophy_bdnff, sophy_syntri_fournier, sophy_fournier_bdnff
                                        WHERE ssf_id_num_syntri = {$num_taxon} AND ssf_id_num_fournier = sfb_id_num_fournier
                                        AND sb_num_tax = sfb_id_num_bdnff AND sb_id_num_bdnff = sb_num_nom_retenu;";
                                $requete2 = "SELECT ssyn_nom_supp AS nom FROM sophy_syntri WHERE ssyn_id_num_supp = {$num_taxon};";
                                break;  
                        case 'codefr94' :
                                $requete = "SELECT sc_nom_codefr AS nom FROM sophy_codefr94 WHERE sc_id_num_codefr = {$num_taxon};";
                                break;
                        case 'bryo' :
                                $requete = "SELECT sbr_nom_bryo AS nom FROM sophy_bryophyte WHERE sbr_id_num_bryo = {$num_taxon};";
                                break;  
                        case 'floeur' :
                                $requete = "SELECT sfe_nom_floeur AS nom FROM sophy_flora_europea WHERE sfe_id_num_floeur = {$num_taxon};";
                                break;  
                }
                $resultat_requete = $this->recuperer($requete);
                if ($resultat_requete['nom'] == false && $requete2 != null) {
                        $resultat_requete = $this->recuperer($requete2);
                }
                return $resultat_requete['nom'];
        }
        // recherche le dernier id de la base
        public function chargerId() {
                $id = 0;
                $requete_select_id = "SELECT MAX(st_id_taxon) AS idMax FROM sophy_taxon;";
                $resultat_requete_id = $this->recuperer($requete_select_id);
                if ($resultat_requete_id['idMax'] != false) {
                        $id = $resultat_requete_id['idMax'];
                }
                return $id;
        }
        
        // Regarde si il y a des taxons dans la base, retourne le tableau de valeur et vide la base
        public function chargerTaxon() {
                $resultat = null;
                $retour = null;
                $requete_select = "SELECT * FROM sophy_taxon;";
                $resultat = $this->recupererTous($requete_select);
                if ($resultat != false) {
                        foreach ($resultat as $result) {
                                if ($result['st_nom_supp'] != null) {
                                        $retour['ind'][$result['st_nom_supp']]['id'] = $result['st_id_taxon'];
                                        foreach ($this->flores as $nom_flore) {
                                                $retour['ind'][$result['st_nom_supp']][$nom_flore] = $result["st_ce_num_".$nom_flore];
                                        }                                       
                                        $retour['ind'][$result['st_nom_supp']]['remarques'] = $result['st_nom_supp'];
                                } elseif ($result['st_remarques'] != null) { 
                                        $nom = $result['st_remarques'];
                                        $retour['ind'][$nom]['id'] = $result['st_id_taxon'];
                                        foreach ($this->flores as $nom_flore) {
                                                $retour['ind'][$result['st_nom_supp']][$nom_flore] = $result["st_ce_num_".$nom_flore];
                                        }
                                        $retour['ind'][$nom]['remarques'] = $result['st_nom_supp'];
                                } elseif ($result['st_ce_num_syntri'] != null) {
                                        $retour['syntri'][$result['st_ce_num_syntri']]['id'] = $result['st_id_taxon'];
                                        $retour['syntri'][$result['st_ce_num_syntri']]['num_supp'] = $result['st_num_supp'];
                                } elseif ($result['st_ce_num_floeur'] != null) {
                                        $retour['floeur'][$result['st_ce_num_floeur']]['id'] = $result['st_id_taxon'];
                                } elseif ($result['st_ce_num_bdnff'] != null) {
                                        $retour['bdnff'][$result['st_ce_num_bdnff']]['id'] = $result['st_id_taxon'];
                                } elseif ($result['st_ce_num_codefr94'] != null) {
                                        $retour['codefr94'][$result['st_ce_num_codefr94']]['id'] = $result['st_id_taxon'];              
                                } elseif ($result['st_ce_num_bryo'] != null) {
                                        $retour['bryo'][$result['st_ce_num_bryo']]['id'] = $result['st_id_taxon'];
                                } elseif ($result['st_ce_num_ciff'] != null) {
                                        $retour['ciff'][$result['st_ce_num_ciff']]['id'] = $result['st_id_taxon'];
                                }  elseif ($result['st_ce_num_fournier'] != null) {
                                        $retour['fournier'][$result['st_ce_num_fournier']]['id'] = $result['st_id_taxon'];
                                }
                        }       
                }
                $requete = "TRUNCATE TABLE `sophy_taxon`; ";
                $res = $this->requeter($requete);
                return $retour;
        }
// +-------------------------------------------------------------------------------------------------------------------+
// Requête sur table sophy_taxon et toutes les tables flore de sophy  
        public function integrerTaxons() {
                foreach ($this->taxons as $flore=>$taxons) {
                        switch ($flore) {
                                case 'ind' : 
                                        // insertion par 1000 pour éviter que la requête soit trop lourde
                                        $i = 0; $j = 1000;
                                        $requete = "INSERT INTO `sophy_taxon` (`st_id_taxon`, `st_nom`, `st_nom_supp`,";
                                        foreach ($this->flores as $nom_flore) {
                                                $requete .= "st_ce_num_{$nom_flore}, ";
                                        }
                                        $requete .= " st_remarques) VALUES ";
                                        foreach ($taxons as $taxon=>$valeur) {
                                                if (!isset($valeur['remarques'])){
                                                        $valeur['remarques'] = 'NULL';
                                                        $taxon = $this->proteger($taxon);
                                                } elseif (substr_compare($taxon, 'rem:', 0, 4) == 0){
                                                        $valeur['remarques'] = $this->proteger($taxon);
                                                        $taxon = 'NULL';
                                                } else {
                                                        $taxon = $this->proteger($taxon);
                                                        $valeur['remarques'] = $this->proteger($valeur['remarques']);
                                                }
                                                
                                                if ($i < $j) {
                                                        $requete .= " (".$valeur['id'].", ".$this->proteger($valeur['nom']).", ".$taxon.", ";
                                                        foreach ($this->flores as $nom_flore) {
                                                                $requete .= $valeur[$nom_flore].", ";
                                                        }
                                                        $requete .= " ".$valeur['remarques']."),";
                                                } elseif ($i == $j) {$j += 1000;
                                                        $requete = substr($requete,0,-1).";";
                                                        $resultat = $this->requeter($requete);
                                                        if ($resultat == false) {
                                                                echo $flore.' : '.$i;
                                                        }
                                                        $requete = "INSERT INTO `sophy_taxon` (`st_id_taxon`, `st_nom`, `st_nom_supp`,";
                                                        foreach ($this->flores as $nom_flore) {
                                                                $requete .= "st_ce_num_{$nom_flore}, ";
                                                        }
                                                        $requete .= " st_remarques) VALUES (".$valeur['id'].", ".$this->proteger($valeur['nom']).
                                                                ", ".$taxon.", ";
                                                        foreach ($this->flores as $nom_flore) {
                                                                $requete .= $valeur[$nom_flore].", ";
                                                        }
                                                        $requete .= " ".$valeur['remarques']."),";
                                                }
                                                $i++;
                                        }
                                        $requete = substr($requete,0,-1).";";
                                        break;
                                case 'syntri' :
                                        $i = 0;
                                        $requete = "INSERT INTO `sophy_taxon` (`st_id_taxon`, ";
                                        foreach ($this->flores as $nom_flore) {
                                                $requete .= "st_ce_num_{$nom_flore}, ";
                                        }
                                        $requete .= "`st_nom`) VALUES ";
                                        foreach ($taxons as $taxon=>$valeur) {
                                                $requete .= " (".$valeur['id'].", ";
                                                foreach ($this->flores as $nom_flore) {
                                                                $requete .= $valeur[$nom_flore].", ";
                                                }
                                                $requete .= $this->proteger($valeur['nom'])."), ";
                                        }
                                        $requete = substr($requete,0,-2).";"; 
                                        break;
                                default: 
                                        $requete = "INSERT INTO `sophy_taxon` (`st_id_taxon`, `st_nom`, st_ce_num_{$flore}) VALUES ";
                                        foreach ($taxons as $numTaxon=>$valeur) {
                                                $requete .= " (".$valeur['id'].", ".$this->proteger($valeur['nom']).", ".$numTaxon."),"; 
                                        }
                                        $requete = substr($requete,0,-1).";";
                                        break;
                        }
                        $resultat = $this->requeter($requete);
                        if ($resultat == false) {
                                echo " - flore : ".$flore;
                        }
                }
        }
}
?>