Subversion Repositories eFlore/Applications.cel

Rev

Rev 2025 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2025 Rev 2026
Line 15... Line 15...
15
	private $mode;
15
	private $mode;
Line 16... Line 16...
16
	
16
	
17
	private $table_liaison;
17
	private $table_liaison;
Line 18... Line 18...
18
	private $table_mots_cles;
18
	private $table_mots_cles;
19
	
19
	
20
	public function GestionMotsClesChemin($config, $mode) {
20
	public function GestionMotsClesChemin($config, $mode = 'obs') {
21
		$this->config = $config;
21
		$this->config = $config;
22
		//TODO: switch suivant mode
22
		//TODO: switch suivant mode
-
 
23
		$this->table_liaison = 'cel_obs_tags_path_liaison';
-
 
24
		$this->table_mots_cles = 'cel_obs_tags_path';
-
 
25
	}
-
 
26
	
-
 
27
	public function viderTables() {
-
 
28
		$requete = "TRUNCATE ".$this->table_mots_cles." ";
-
 
29
		$vidage = Cel::db()->executer($requete.' -- '.__FILE__.':'.__LINE__);
-
 
30
		
-
 
31
		$requete = "TRUNCATE ".$this->table_liaison." ";
-
 
32
		$vidage = Cel::db()->executer($requete.' -- '.__FILE__.':'.__LINE__);
-
 
33
		
-
 
34
		return $vidage;
-
 
35
	}
-
 
36
	
-
 
37
	public function convertirNestedSetVersPathEnum() {
-
 
38
		
-
 
39
		$time_start = microtime();
-
 
40
		
-
 
41
		$requete_arbre = "SELECT * FROM cel_mots_cles_obs_srv ORDER BY id_utilisateur, niveau";
-
 
42
		$mots_cles = Cel::db()->requeter($requete_arbre);
-
 
43
		
-
 
44
		$requete_liaisons = "SELECT comc.id_observation, comc.id_mot_cle_obs, o.ce_utilisateur as id_utilisateur ".
-
 
45
									"FROM cel_obs_mots_cles comc ".
-
 
46
									"INNER JOIN cel_obs o ON o.id_observation = comc.id_observation ";
-
 
47
		
-
 
48
		$liaisons = Cel::db()->requeter($requete_liaisons);
-
 
49
	
-
 
50
		$mots_cles_hierarchiques = array();
-
 
51
		$correspondances_id_mots_cles_obs = array();
-
 
52
		
-
 
53
		foreach($mots_cles as &$mot) {
-
 
54
 
-
 
55
			$chemin = '/';
-
 
56
			if(isset($mots_cles_hierarchiques[$mot['ce_mot_cle_obs_parent']])) {
-
 
57
				$chemin = $mots_cles_hierarchiques[$mot['ce_mot_cle_obs_parent']]['chemin'];
-
 
58
			}
-
 
59
			$chemin .= self::simplifier($mot['mot_cle']).'/';
-
 
60
			$chemin = str_replace("//", "", $chemin);
-
 
61
			
-
 
62
			$cle = $mot['id_mot_cle_obs'].'-'.$mot['id_utilisateur'];
-
 
63
			
-
 
64
			$mots_cles_hierarchiques[$cle] = array(
-
 
65
					'id_utilisateur' => $mot['id_utilisateur'],
-
 
66
					'chemin' => $chemin,
-
 
67
					'tag' => $mot['mot_cle']
-
 
68
			);
-
 
69
		}
-
 
70
		
-
 
71
		$time_end = microtime();
-
 
72
		$time = $time_end - $time_start;
-
 
73
			
-
 
74
		$mots_cles_slice = array_chunk($mots_cles_hierarchiques, 800, true);
-
 
75
		foreach($mots_cles_slice as $mots_cles_tranche) {
-
 
76
			$requete_insertion_mots_cles = "INSERT INTO ".$this->table_mots_cles." (chemin, id_utilisateur, tag) ".
-
 
77
			                     			" VALUES ";
-
 
78
			
-
 
79
			foreach($mots_cles_tranche as $cle => $mot_cle_tranche) {
-
 
80
				$requete_insertion_mots_cles .= "(".
-
 
81
					cel::db()->proteger($mot_cle_tranche['chemin']).", ".
-
 
82
					cel::db()->proteger($mot_cle_tranche['id_utilisateur']).", ".
-
 
83
					cel::db()->proteger($mot_cle_tranche['tag'])." ".	
-
 
84
				"), ";
-
 
85
			}
-
 
86
			
-
 
87
			$requete_insertion_mots_cles = rtrim($requete_insertion_mots_cles, ", ");
-
 
88
			cel::db()->executer($requete_insertion_mots_cles);
-
 
89
		}
-
 
90
		
-
 
91
		$liaisons_slice = array_chunk($liaisons, 800, true);
-
 
92
		foreach($liaisons_slice as &$liaison_tranche) {
-
 
93
			$requete_insertion_liaisons = "INSERT INTO ".$this->table_liaison." (id_obs, chemin, id_utilisateur) ".
-
 
94
									      " VALUES ";
-
 
95
			$lier = false;
-
 
96
			
-
 
97
			foreach($liaison_tranche as &$liaison_obs_mot_cle) {
-
 
98
				$cle = $liaison_obs_mot_cle['id_mot_cle_obs'].'-'.$liaison_obs_mot_cle['id_utilisateur'];
-
 
99
				if(isset($mots_cles_hierarchiques[$cle])) {
-
 
100
					$requete_insertion_liaisons .= "(".
-
 
101
						cel::db()->proteger($liaison_obs_mot_cle['id_observation']).", ".
-
 
102
						cel::db()->proteger($mots_cles_hierarchiques[$cle]['chemin']).", ".
-
 
103
						cel::db()->proteger($liaison_obs_mot_cle['id_utilisateur'])." ".
-
 
104
					"), ";
-
 
105
					$lier = true;
-
 
106
				}
-
 
107
			}
-
 
108
			
-
 
109
			if($lier) {
-
 
110
				$requete_insertion_liaisons = rtrim($requete_insertion_liaisons, ", ");
-
 
111
				cel::db()->executer($requete_insertion_liaisons);
-
 
112
			}
-
 
113
		}
-
 
114
		
23
		$this->table_liaison = 'cel_obs_tag_path_liaison';
115
		echo "La génération d'un tableau de ".count($mots_cles_hierarchiques).' éléments a pris '.$time.' secondes';
Line 24... Line 116...
24
		$this->table_mots_cles = 'cel_obs_tag_path';
116
		exit;
Line 25... Line 117...
25
	}
117
	}
26
	
118
	
27
	public function obtenirArbre($id_utilisateur, $chemin = "/") {
119
	public function obtenirArbre($id_utilisateur, $chemin = "/") {
28
 
120
 
Line 29... Line 121...
29
		$requete = "SELECT * FROM ".$this->table_mots_cles." ".
121
		$requete = "SELECT * FROM ".$this->table_mots_cles." ".
30
		"WHERE id_utilisateur = ".Cel::db()->proteger($id_utilisateur)." AND ".
122
		"WHERE id_utilisateur = ".Cel::db()->proteger($id_utilisateur)." AND ".
Line 31... Line 123...
31
		"chemin LIKE = ".Cel::db()->proteger($chemin."%")." ".
123
		"chemin LIKE ".Cel::db()->proteger($chemin."%")." ".
32
		"ORDER BY chemin"
124
		"ORDER BY chemin";
Line 33... Line 125...
33
		
125
		
Line 34... Line 126...
34
		$arbre = Cel::db()->executer($requete.' -- '.__FILE__.':'.__LINE__);	
126
		$arbre = Cel::db()->requeter($requete.' -- '.__FILE__.':'.__LINE__);	
Line 35... Line 127...
35
		usort($arbre, array('GestionMotsClesChemin', 'comparerProfNoeuds');
127
		usort($arbre, array('GestionMotsClesChemin', 'comparerProfNoeuds'));
36
		
128
		
37
		return $arbre;
129
		return $arbre;
38
	}
130
	}
39
	
131
	
40
	public function insererParChemin($mot_cle, $chemin, $id_utilisateur) {
132
	public function insererParChemin($mot_cle, $chemin_parent, $id_utilisateur) {
Line 41... Line 133...
41
		
133
		
Line 42... Line 134...
42
		$chemin_mot_cle = rtrim($chemin, '/').'/'.self::simplifier($mot_cle);
134
		$chemin_mot_cle = self::getCheminHarmonise($chemin_parent, $mot_cle);
Line 56... Line 148...
56
	public function insererParIdParent($mot_cle, $id_parent, $id_utilisateur) {
148
	public function insererParIdParent($mot_cle, $id_parent, $id_utilisateur) {
Line 57... Line 149...
57
		
149
		
Line 58... Line 150...
58
		$mot_cle_simp = self::simplifier($mot_cle);
150
		$mot_cle_simp = self::simplifier($mot_cle);
59
		
151
		
Line 60... Line 152...
60
		$sous_requete_chemin = "(SELECT chemin FROM ".$this->table_mots_cles." ".
152
		$sous_requete_chemin = "(SELECT chemin FROM ".$this->table_mots_cles." ".
61
		                       "WHERE id_tag = "Cel::db()->proteger($id_parent).")";
153
		                       "WHERE id_tag = ".Cel::db()->proteger($id_parent).")";
62
		
154
		
63
		$requete = "INSERT INTO ".$this->table_mots_cles."(chemin, id_utilisateur, tag) ".
155
		$requete = "INSERT INTO ".$this->table_mots_cles."(chemin, id_utilisateur, tag) ".
64
		           "VALUES (".
156
		           "VALUES (".
65
						"CONCAT($sous_requete_chemin.",",".Cel::db()->proteger($mot_cle_simp).",'/') ".
157
						"CONCAT(".$sous_requete_chemin.",".Cel::db()->proteger($mot_cle_simp).",'/') ".
Line 66... Line 158...
66
						Cel::db()->proteger($id_utilisateur).", ".
158
						Cel::db()->proteger($id_utilisateur).", ".
Line 78... Line 170...
78
				   		"WHERE id_tag = ".Cel::db()->proteger($id_mot_cle).") ";
170
				   		"WHERE id_tag = ".Cel::db()->proteger($id_mot_cle).") ";
Line 79... Line 171...
79
		
171
		
80
		$requete = "INSERT INTO ".$this->table_liaison." (id_obs, chemin, id_utilisateur) ".
172
		$requete = "INSERT INTO ".$this->table_liaison." (id_obs, chemin, id_utilisateur) ".
81
					"VALUES (".
173
					"VALUES (".
82
				   		Cel::db()->proteger($id_observation).", ".
174
				   		Cel::db()->proteger($id_observation).", ".
83
				   		$sous_requete_chemin.","
175
				   		$sous_requete_chemin.",".
84
				   		Cel::db()->proteger($id_utilisateur)." ".
176
				   		Cel::db()->proteger($id_utilisateur)." ".
85
				   	") ".
177
				   	") ".
Line 86... Line 178...
86
					"ON DUPLICATE KEY UPDATE id_obs = id_obs; ";
178
					"ON DUPLICATE KEY UPDATE id_obs = id_obs; ";
Line 106... Line 198...
106
	}
198
	}
Line 107... Line 199...
107
	
199
	
108
	// Déplacer un mot clé et le renommer est une même opération, dans le cas du renommage,
200
	// 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
201
	// seule la fin du chemin change
-
 
202
	public function renommerChemin($ancien_chemin, $nouveau_chemin, $id_utilisateur) {
-
 
203
		
-
 
204
		$ancien_chemin = self::harmoniserChemin($ancien_chemin);
-
 
205
		$nouveau_chemin = self::harmoniserChemin($nouveau_chemin);
-
 
206
		
-
 
207
		$ancien_chemin_p = Cel::db()->proteger($ancien_chemin);
-
 
208
		$nouveau_chemin_p = Cel::db()->proteger($nouveau_chemin);
110
	public function renommerChemin($ancien_chemin, $nouveau_chemin, $id_utilisateur) {
209
		
111
		// TODO : triggers pour les tables liées ?
210
		// TODO : triggers pour les tables liées ?
112
		$requete = "UPDATE ".$this->$table_mots_cles." ".
211
		$requete = "UPDATE ".$this->table_mots_cles." ".
113
		       //    "SET tag = "                                   
212
		       	   "SET chemin = REPLACE(chemin,".$ancien_chemin_p.", ".$nouveau_chemin_p.") ".                              
114
				   "WHERE chemin LIKE ".Cel::db()->proteger($ancien_chemin."%")." AND ".
213
				   "WHERE chemin LIKE ".Cel::db()->proteger($ancien_chemin."%")." AND ".
-
 
214
		           "id_utilisateur = ".Cel::db()->proteger($id_utilisateur)." ";   
-
 
215
		echo $requete.'<br />';
Line -... Line 216...
-
 
216
		$deplacement = Cel::db()->executer($requete.' -- '.__FILE__.':'.__LINE__);
115
		           "id_utilisateur = ".Cel::db()->proteger($id_utilisateur)." ";   
217
		
Line 116... Line 218...
116
		
218
		return $deplacement;
117
	}
219
	}
118
	
220
	
Line 168... Line 270...
168
			DELETE FROM cel_obs_tag_path_liaison
270
			DELETE FROM cel_obs_tag_path_liaison
169
			WHERE cel_obs_tag_path_liaison.id_obs = OLD.id_observation
271
			WHERE cel_obs_tag_path_liaison.id_obs = OLD.id_observation
170
		END;
272
		END;
171
	*/
273
	*/
Line -... Line 274...
-
 
274
	
-
 
275
	static public function getCheminHarmonise($chemin_parent, $mot_cle) {
-
 
276
		return self::harmoniserChemin($chemin_parent.'/'.self::simplifier($mot_cle).'/');
-
 
277
	}
-
 
278
	
-
 
279
	static public function harmoniserChemin($chemin) {
-
 
280
		$chemin = self::startsWith($chemin,'/') ? $chemin : '/'.$chemin;
-
 
281
		$chemin = self::endsWith($chemin,'/') ? $chemin : $chemin.'/';
-
 
282
		return str_replace('//', '/', $chemin);
-
 
283
	}
172
	
284
	
173
	// fonction de slugification du mot clé
285
	// fonction de slugification du mot clé
174
	// http://stackoverflow.com/questions/2955251/php-function-to-make-slug-url-string
286
	// http://stackoverflow.com/questions/2955251/php-function-to-make-slug-url-string
175
	static public function simplifier($text)
287
	static public function simplifier($text)
-
 
288
	{
176
	{
289
		return trim(str_replace(array('\\','/'), '', $text));
177
		// replace non letter or digits by -
290
		// replace non letter or digits by -
Line 178... Line 291...
178
		$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
291
		$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
179
	
292
	
Line 194... Line 307...
194
			return 'n-a';
307
			return 'n-a';
195
		}
308
		}
Line 196... Line 309...
196
	
309
	
197
		return $text;
310
		return $text;
-
 
311
	}
-
 
312
	
-
 
313
	static public function startsWith($haystack, $needle) {
-
 
314
		return $needle === "" || strpos($haystack, $needle) === 0;
-
 
315
	}
-
 
316
	
-
 
317
	static public function endsWith($haystack, $needle) {
-
 
318
		return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
198
	}
319
	}
Line 199... Line 320...
199
}
320
}
200
 
321