2025 |
aurelien |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* @package jrest
|
|
|
4 |
* @author Aurélien Peronnet <aurelien@tela-botania.org>
|
|
|
5 |
* @copyright 2010, 2013 Tela-Botanica
|
|
|
6 |
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
|
|
|
7 |
*
|
|
|
8 |
* Librairie de liaison d'images et d'observation à des mots clés en utilisant la méthode
|
|
|
9 |
* path enumeration
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
class GestionMotsClesChemin {
|
|
|
13 |
|
|
|
14 |
private $config;
|
|
|
15 |
private $mode;
|
|
|
16 |
|
|
|
17 |
private $table_liaison;
|
|
|
18 |
private $table_mots_cles;
|
|
|
19 |
|
|
|
20 |
public function GestionMotsClesChemin($config, $mode) {
|
|
|
21 |
$this->config = $config;
|
|
|
22 |
//TODO: switch suivant mode
|
|
|
23 |
$this->table_liaison = 'cel_obs_tag_path_liaison';
|
|
|
24 |
$this->table_mots_cles = 'cel_obs_tag_path';
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
public function obtenirArbre($id_utilisateur, $chemin = "/") {
|
|
|
28 |
|
|
|
29 |
$requete = "SELECT * FROM ".$this->table_mots_cles." ".
|
|
|
30 |
"WHERE id_utilisateur = ".Cel::db()->proteger($id_utilisateur)." AND ".
|
|
|
31 |
"chemin LIKE = ".Cel::db()->proteger($chemin."%")." ".
|
|
|
32 |
"ORDER BY chemin"
|
|
|
33 |
|
|
|
34 |
$arbre = Cel::db()->executer($requete.' -- '.__FILE__.':'.__LINE__);
|
|
|
35 |
usort($arbre, array('GestionMotsClesChemin', 'comparerProfNoeuds');
|
|
|
36 |
|
|
|
37 |
return $arbre;
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
public function insererParChemin($mot_cle, $chemin, $id_utilisateur) {
|
|
|
41 |
|
|
|
42 |
$chemin_mot_cle = rtrim($chemin, '/').'/'.self::simplifier($mot_cle);
|
|
|
43 |
|
|
|
44 |
$requete = "INSERT INTO ".$this->table_mots_cles." (chemin, id_utilisateur, tag) ".
|
|
|
45 |
"VALUES (".
|
|
|
46 |
Cel::db()->proteger($chemin_mot_cle).", ".
|
|
|
47 |
Cel::db()->proteger($id_utilisateur).", ".
|
|
|
48 |
Cel::db()->proteger($tag).", ".
|
|
|
49 |
")";
|
|
|
50 |
|
|
|
51 |
$insertion = Cel::db()->executer($requete.' -- '.__FILE__.':'.__LINE__);
|
|
|
52 |
|
|
|
53 |
return $insertion;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
public function insererParIdParent($mot_cle, $id_parent, $id_utilisateur) {
|
|
|
57 |
|
|
|
58 |
$mot_cle_simp = self::simplifier($mot_cle);
|
|
|
59 |
|
|
|
60 |
$sous_requete_chemin = "(SELECT chemin FROM ".$this->table_mots_cles." ".
|
|
|
61 |
"WHERE id_tag = "Cel::db()->proteger($id_parent).")";
|
|
|
62 |
|
|
|
63 |
$requete = "INSERT INTO ".$this->table_mots_cles."(chemin, id_utilisateur, tag) ".
|
|
|
64 |
"VALUES (".
|
|
|
65 |
"CONCAT($sous_requete_chemin.",",".Cel::db()->proteger($mot_cle_simp).",'/') ".
|
|
|
66 |
Cel::db()->proteger($id_utilisateur).", ".
|
|
|
67 |
Cel::db()->proteger($tag).", ".
|
|
|
68 |
")";
|
|
|
69 |
|
|
|
70 |
$insertion = Cel::db()->executer($requete.' -- '.__FILE__.':'.__LINE__);
|
|
|
71 |
|
|
|
72 |
return $insertion;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
public function lierParId($id_mot_cle, $id_observation, $id_utilisateur) {
|
|
|
76 |
|
|
|
77 |
$sous_requete_chemin = "(SELECT chemin FROM ".$this->table_mots_cles." ".
|
|
|
78 |
"WHERE id_tag = ".Cel::db()->proteger($id_mot_cle).") ";
|
|
|
79 |
|
|
|
80 |
$requete = "INSERT INTO ".$this->table_liaison." (id_obs, chemin, id_utilisateur) ".
|
|
|
81 |
"VALUES (".
|
|
|
82 |
Cel::db()->proteger($id_observation).", ".
|
|
|
83 |
$sous_requete_chemin.","
|
|
|
84 |
Cel::db()->proteger($id_utilisateur)." ".
|
|
|
85 |
") ".
|
|
|
86 |
"ON DUPLICATE KEY UPDATE id_obs = id_obs; ";
|
|
|
87 |
|
|
|
88 |
$liaison = Cel::db()->executer($requete.' -- '.__FILE__.':'.__LINE__);
|
|
|
89 |
|
|
|
90 |
return $liaison;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
public function lierParChemin($chemin, $id_observation, $id_utilisateur) {
|
|
|
94 |
|
|
|
95 |
$requete = "INSERT INTO ".$this->table_liaison." (id_obs, chemin, id_utilisateur) ".
|
|
|
96 |
"VALUES (".
|
|
|
97 |
Cel::db()->proteger($id_observation).", ".
|
|
|
98 |
Cel::db()->proteger($sous_requete_chemin).", ".
|
|
|
99 |
Cel::db()->proteger($id_utilisateur)." ".
|
|
|
100 |
") ".
|
|
|
101 |
"ON DUPLICATE KEY UPDATE id_obs = id_obs; ";
|
|
|
102 |
|
|
|
103 |
$liaison = Cel::db()->executer($requete.' -- '.__FILE__.':'.__LINE__);
|
|
|
104 |
|
|
|
105 |
return $liaison;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
// Déplacer un mot clé et le renommer est une même opération, dans le cas du renommage,
|
|
|
109 |
// seule la fin du chemin change
|
|
|
110 |
public function renommerChemin($ancien_chemin, $nouveau_chemin, $id_utilisateur) {
|
|
|
111 |
// TODO : triggers pour les tables liées ?
|
|
|
112 |
$requete = "UPDATE ".$this->$table_mots_cles." ".
|
|
|
113 |
// "SET tag = "
|
|
|
114 |
"WHERE chemin LIKE ".Cel::db()->proteger($ancien_chemin."%")." AND ".
|
|
|
115 |
"id_utilisateur = ".Cel::db()->proteger($id_utilisateur)." ";
|
|
|
116 |
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
public function supprimerChemin($chemin, $id_utilisateur) {
|
|
|
120 |
// TODO : triggers pour les tables liées ?
|
|
|
121 |
$requete = "DELETE FROM ".$this->$table_mots_cles." ".
|
|
|
122 |
"WHERE chemin LIKE ".Cel::db()->proteger($chemin."%")." AND ".
|
|
|
123 |
"id_utilisateur = ".Cel::db()->proteger($id_utilisateur)." ";
|
|
|
124 |
|
|
|
125 |
$suppression = Cel::db()->executer($requete.' -- '.__FILE__.':'.__LINE__);
|
|
|
126 |
|
|
|
127 |
return $suppression;
|
|
|
128 |
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
// Fonctions utilitaires
|
|
|
132 |
static public function comparerProfNoeuds($a, $b) {
|
|
|
133 |
|
|
|
134 |
$nb_slashs_a = substr_count($a['chemin'], '/');
|
|
|
135 |
$nb_slashs_b = substr_count($a['chemin'], '/');
|
|
|
136 |
$cmp = 0;
|
|
|
137 |
|
|
|
138 |
if($nb_slashs_a == $nb_slashs_b) {
|
|
|
139 |
$cmp = strcmp($a['chemin'], $b['chemin']);
|
|
|
140 |
} else {
|
|
|
141 |
$cmp = ($a['chemin'] > $b['chemin']) ? +1 : -1;
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
return $cmp;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
/*
|
|
|
148 |
CREATE TRIGGER maj_chemin BEFORE UPDATE
|
|
|
149 |
ON cel_obs_tag_path FOR EACH ROW
|
|
|
150 |
BEGIN
|
|
|
151 |
UPDATE cel_obs_tag_path_liaison
|
|
|
152 |
SET cel_obs_tag_path_liaison.chemin = NEW.CHEMIN
|
|
|
153 |
WHERE cel_obs_tag_path_liaison.chemin LIKE CONCAT(OLD.chemin,'%')
|
|
|
154 |
AND cel_obs_tag_path_liaison.id_utilisateur = OLD.id_utilisateur
|
|
|
155 |
END;
|
|
|
156 |
|
|
|
157 |
CREATE TRIGGER suppr_chemin BEFORE UPDATE
|
|
|
158 |
ON cel_obs_tag_path FOR EACH ROW
|
|
|
159 |
BEGIN
|
|
|
160 |
DELETE FROM cel_obs_tag_path_liaison
|
|
|
161 |
WHERE cel_obs_tag_path_liaison.chemin LIKE CONCAT(OLD.chemin,'%')
|
|
|
162 |
AND cel_obs_tag_path_liaison.id_utilisateur = OLD.id_utilisateur
|
|
|
163 |
END;
|
|
|
164 |
|
|
|
165 |
CREATE TRIGGER suppr_obs BEFORE DELETE
|
|
|
166 |
ON cel_obs FOR EACH ROW
|
|
|
167 |
BEGIN
|
|
|
168 |
DELETE FROM cel_obs_tag_path_liaison
|
|
|
169 |
WHERE cel_obs_tag_path_liaison.id_obs = OLD.id_observation
|
|
|
170 |
END;
|
|
|
171 |
*/
|
|
|
172 |
|
|
|
173 |
// fonction de slugification du mot clé
|
|
|
174 |
// http://stackoverflow.com/questions/2955251/php-function-to-make-slug-url-string
|
|
|
175 |
static public function simplifier($text)
|
|
|
176 |
{
|
|
|
177 |
// replace non letter or digits by -
|
|
|
178 |
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
|
|
|
179 |
|
|
|
180 |
// trim
|
|
|
181 |
$text = trim($text, '-');
|
|
|
182 |
|
|
|
183 |
// transliterate
|
|
|
184 |
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
|
|
|
185 |
|
|
|
186 |
// lowercase
|
|
|
187 |
$text = strtolower($text);
|
|
|
188 |
|
|
|
189 |
// remove unwanted characters
|
|
|
190 |
$text = preg_replace('~[^-\w]+~', '', $text);
|
|
|
191 |
|
|
|
192 |
if (empty($text))
|
|
|
193 |
{
|
|
|
194 |
return 'n-a';
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
return $text;
|
|
|
198 |
}
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
?>
|