Subversion Repositories eFlore/Applications.cel

Rev

Rev 2067 | Blame | Last modification | View Log | RSS feed

<?php
class Dao extends Bdd {

        private $mode;
        
        private $table_liaison;
        private $table_mots_cles;
        private $champ_id_element_lie;
        
        public function __construct($mode) {
                parent::__construct();
                $this->mode = $mode;
                
                if($mode == 'obs') {
                        $this->table_liaison = 'cel_mots_cles_obs_liaison';
                        $this->table_mots_cles = 'cel_arbre_mots_cles_obs';
                } else {
                        $this->table_liaison = 'cel_mots_cles_images_liaison';
                        $this->table_mots_cles = 'cel_arbre_mots_cles_images';
                }
        }
        
        public function viderTables() {
                $requete = "TRUNCATE ".$this->table_mots_cles." ";
                $vidage = $this->requeter($requete.' -- '.__FILE__.':'.__LINE__);
        
                $requete = "TRUNCATE ".$this->table_liaison." ";
                $vidage = $this->requeter($requete.' -- '.__FILE__.':'.__LINE__);
        
                return $vidage;
        }
        
        public function obtenirMotsClesOriginaux() {
        
                if($this->mode == 'obs') {
                        $requete_arbre = "SELECT id_mot_cle_obs as id_mot_cle, ce_mot_cle_obs_parent as parent, id_utilisateur, mot_cle ".
                                                                "FROM cel_mots_cles_obs WHERE niveau != 0 AND id_utilisateur != '' ORDER BY id_utilisateur, niveau";
                } else {
                        $requete_arbre = "SELECT id_mot_cle_image as id_mot_cle, ce_mot_cle_image_parent as parent, id_utilisateur, mot_cle ".
                                                                "FROM cel_mots_cles_images WHERE niveau != 0 AND id_utilisateur != '' ORDER BY id_utilisateur, niveau";
                }

                $mots_cles = $this->recupererTous($requete_arbre);      
                return $mots_cles;
        }
        
        public function obtenirLiaisonsMotsClesOriginaux() {
                if($this->mode == 'obs') {
                        $requete_liaisons = "SELECT comc.id_observation as id_element_lie, comc.id_mot_cle_obs as id_mot_cle, id_utilisateur as id_utilisateur ".
                                                                                                                        "FROM cel_obs_mots_cles comc ".
                                                                                                                        "INNER JOIN cel_obs o ON o.id_observation = comc.id_observation ";
                } else {
                        $requete_liaisons = "SELECT comc.id_image as id_element_lie, comc.id_mot_cle_image as id_mot_cle, o.ce_utilisateur as id_utilisateur ".
                                                                                                                                                "FROM cel_images_mots_cles comc ".
                                                                                                                                                "INNER JOIN cel_images o ON o.id_image = comc.id_image ";
                }
                $liaisons = $this->recupererTous($requete_liaisons);
                return $liaisons;
        }
        
        public function insererMotsCles($mots_cles) {
                $requete_insertion_mots_cles = "INSERT INTO ".$this->table_mots_cles." (chemin, id_utilisateur, mot_cle) ".
                                                                                        " VALUES ";
                
                foreach($mots_cles as $cle => $mot_cle) {
                        $requete_insertion_mots_cles .= "(".
                        $this->proteger($mot_cle['chemin']).", ".
                        $this->proteger($mot_cle['id_utilisateur']).", ".
                        $this->proteger($mot_cle['mot_cle'])." ".
                                                        "), ";
                }
                
                $requete_insertion_mots_cles = rtrim($requete_insertion_mots_cles, ", ");
                return $this->requeter($requete_insertion_mots_cles);
        }
        
        public function obtenirInfosNouveauxMotsCles() {
                $requete = "SELECT id_mot_cle, id_utilisateur, chemin FROM ".$this->table_mots_cles;
                return $this->recupererTous($requete);
        }
        
        public function insererLiaisonsMotsCles($mots_cles, $liaisons, $correspondances) {
                $requete_insertion_liaisons = "INSERT INTO ".$this->table_liaison." (id_element_lie, id_mot_cle) ".
                                                                                                      " VALUES ";
                $lier = false;
                $mot_cles_inexistants = 0;
                
                foreach($liaisons as &$liaison_mot_cle) {
                        $cle = $liaison_mot_cle['id_mot_cle'].'-'.$liaison_mot_cle['id_utilisateur'];
                        if(isset($mots_cles[$cle])) {
                                $cle_id_mot_cle_migre = $mots_cles[$cle]['chemin'].'-'.$liaison_mot_cle['id_utilisateur'];
                                if(isset($correspondances[$cle_id_mot_cle_migre])) {
                                        $id_mot_cle_migre = $correspondances[$cle_id_mot_cle_migre]['id_mot_cle'];
                                        $requete_insertion_liaisons .= "(".
                                                $this->proteger($liaison_mot_cle['id_element_lie']).", ".
                                                $this->proteger($id_mot_cle_migre)." ".
                                                                        "), ";
                                        $lier = true;
                                } else {
                                        $mot_cles_inexistants++;
                                }
                        }
                }

                if($lier) {
                        $requete_insertion_liaisons = rtrim($requete_insertion_liaisons, ", ");
                        $this->requeter($requete_insertion_liaisons);
                }
                if($mot_cles_inexistants != 0) {
                        echo $mot_cles_inexistants."\n";
                }
        }
}