Subversion Repositories eFlore/Applications.cel

Rev

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

Rev Author Line No. Line
2031 aurelien 1
<?php
2414 jpm 2
// declare(encoding='UTF-8');
3
/**
4
 * Contient les requêtes effecturant la migration des mots-clés.
5
 *
6
 * @category   CEL
7
 * @package    Scripts
8
 * @subpackage Migration : Sauvages
9
 * @author     Mathias CHOUET <mathias@tela-botanica.org>
10
 * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
11
 * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
12
 * @license    GPL v3 <http://www.gnu.org/licenses/gpl.txt>
13
 * @license    CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
14
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
15
 */
2031 aurelien 16
class Dao extends Bdd {
17
 
18
	private $mode;
19
	private $table_liaison;
20
	private $table_mots_cles;
2414 jpm 21
 
2031 aurelien 22
	public function __construct($mode) {
23
		parent::__construct();
24
		$this->mode = $mode;
2414 jpm 25
 
26
		if ($mode == 'obs') {
2057 aurelien 27
			$this->table_liaison = 'cel_mots_cles_obs_liaison';
28
			$this->table_mots_cles = 'cel_arbre_mots_cles_obs';
2031 aurelien 29
		} else {
2057 aurelien 30
			$this->table_liaison = 'cel_mots_cles_images_liaison';
31
			$this->table_mots_cles = 'cel_arbre_mots_cles_images';
2031 aurelien 32
		}
33
	}
2414 jpm 34
 
2031 aurelien 35
	public function viderTables() {
36
		$requete = "TRUNCATE ".$this->table_mots_cles." ";
37
		$vidage = $this->requeter($requete.' -- '.__FILE__.':'.__LINE__);
2414 jpm 38
 
2031 aurelien 39
		$requete = "TRUNCATE ".$this->table_liaison." ";
40
		$vidage = $this->requeter($requete.' -- '.__FILE__.':'.__LINE__);
2414 jpm 41
 
2031 aurelien 42
		return $vidage;
43
	}
2414 jpm 44
 
2031 aurelien 45
	public function obtenirMotsClesOriginaux() {
46
		if($this->mode == 'obs') {
2414 jpm 47
			$requete = "SELECT id_mot_cle_obs as id_mot_cle, ce_mot_cle_obs_parent as parent, id_utilisateur, mot_cle ".
48
				"FROM cel_mots_cles_obs WHERE niveau != 0 AND id_utilisateur != '' ORDER BY id_utilisateur, niveau";
2031 aurelien 49
		} else {
2414 jpm 50
			$requete = "SELECT id_mot_cle_image as id_mot_cle, ce_mot_cle_image_parent as parent, id_utilisateur, mot_cle ".
51
				"FROM cel_mots_cles_images WHERE niveau != 0 AND id_utilisateur != '' ORDER BY id_utilisateur, niveau";
2031 aurelien 52
		}
53
 
2414 jpm 54
		$mots_cles = $this->recupererTous($requete);
2031 aurelien 55
		return $mots_cles;
56
	}
2414 jpm 57
 
2031 aurelien 58
	public function obtenirLiaisonsMotsClesOriginaux() {
2414 jpm 59
		if ($this->mode == 'obs') {
60
			$requete = "SELECT comc.id_observation as id_element_lie, comc.id_mot_cle_obs as id_mot_cle, id_utilisateur as id_utilisateur ".
61
				"FROM cel_obs_mots_cles comc ".
62
				"INNER JOIN cel_obs o ON o.id_observation = comc.id_observation ";
2031 aurelien 63
		} else {
2414 jpm 64
			$requete = "SELECT comc.id_image as id_element_lie, comc.id_mot_cle_image as id_mot_cle, o.ce_utilisateur as id_utilisateur ".
65
				"FROM cel_images_mots_cles comc ".
66
				"INNER JOIN cel_images o ON o.id_image = comc.id_image ";
2031 aurelien 67
		}
2414 jpm 68
		$liaisons = $this->recupererTous($requete);
2031 aurelien 69
		return $liaisons;
70
	}
2414 jpm 71
 
2031 aurelien 72
	public function insererMotsCles($mots_cles) {
2414 jpm 73
		$requete = "INSERT INTO ".$this->table_mots_cles." (chemin, id_utilisateur, mot_cle) VALUES ";
74
 
75
		foreach ($mots_cles as $mot_cle) {
76
			$requete .= '('.
77
				$this->proteger($mot_cle['chemin']).', '.
78
				$this->proteger($mot_cle['id_utilisateur']).', '.
79
				$this->proteger($mot_cle['mot_cle']).' '.
80
			'), ';
2031 aurelien 81
		}
2414 jpm 82
 
83
		$requete = rtrim($requete, ', ');
84
		return $this->requeter($requete);
2031 aurelien 85
	}
2414 jpm 86
 
2031 aurelien 87
	public function obtenirInfosNouveauxMotsCles() {
2057 aurelien 88
		$requete = "SELECT id_mot_cle, id_utilisateur, chemin FROM ".$this->table_mots_cles;
2031 aurelien 89
		return $this->recupererTous($requete);
90
	}
2414 jpm 91
 
2031 aurelien 92
	public function insererLiaisonsMotsCles($mots_cles, $liaisons, $correspondances) {
2414 jpm 93
		$requete = "INSERT INTO ".$this->table_liaison." (id_element_lie, id_mot_cle) VALUES ";
2031 aurelien 94
		$lier = false;
95
		$mot_cles_inexistants = 0;
2414 jpm 96
 
97
		foreach ($liaisons as &$liaison_mot_cle) {
2031 aurelien 98
			$cle = $liaison_mot_cle['id_mot_cle'].'-'.$liaison_mot_cle['id_utilisateur'];
2414 jpm 99
			if (isset($mots_cles[$cle])) {
2031 aurelien 100
				$cle_id_mot_cle_migre = $mots_cles[$cle]['chemin'].'-'.$liaison_mot_cle['id_utilisateur'];
101
				if(isset($correspondances[$cle_id_mot_cle_migre])) {
2057 aurelien 102
					$id_mot_cle_migre = $correspondances[$cle_id_mot_cle_migre]['id_mot_cle'];
2414 jpm 103
					$requete .= "(".
2031 aurelien 104
						$this->proteger($liaison_mot_cle['id_element_lie']).", ".
105
						$this->proteger($id_mot_cle_migre)." ".
106
									"), ";
107
					$lier = true;
108
				} else {
109
					$mot_cles_inexistants++;
110
				}
111
			}
112
		}
113
 
2414 jpm 114
		if ($lier) {
115
			$requete = rtrim($requete, ', ');
116
			$this->requeter($requete);
2031 aurelien 117
		}
2414 jpm 118
		if ($mot_cles_inexistants != 0) {
2031 aurelien 119
			echo $mot_cles_inexistants."\n";
120
		}
121
	}
122
}