Rev 2027 | Rev 2042 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?php
/**
* @package jrest
* @author Aurélien Peronnet <aurelien@tela-botania.org>
* @copyright 2010, 2013 Tela-Botanica
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
*
* Librairie de liaison d'images et d'observation à des mots clés en utilisant la méthode
* path enumeration
*/
class GestionMotsClesChemin {
private $config;
private $mode;
private $table_liaison;
private $table_mots_cles;
//TODO: trigger pour les tables liaisons
public function GestionMotsClesChemin($config, $mode = 'obs') {
$this->config = $config;
//TODO: switch suivant mode
$this->mode = $mode;
if($mode == 'obs') {
$this->table_liaison = 'cel_obs_tags_path_liaison';
$this->table_mots_cles = 'cel_obs_tags_path';
} else {
$this->table_liaison = 'cel_images_tags_path_liaison';
$this->table_mots_cles = 'cel_images_tags_path';
}
}
public function construireRequeteOr($mots_cles) {
return $this->construireRequete($mots_cles, 'OR');
}
public function construireRequeteCheminOr($mots_cles) {
return $this->construireRequeteChemin($mots_cles, 'OR');
}
public function construireRequeteAnd($mots_cles) {
return $this->construireRequete($mots_cles, 'AND');
}
public function construireRequeteCheminAnd($mots_cles) {
return $this->construireRequeteChemin($mots_cles, 'AND');
}
public function construireRequete($mots_cles, $operateur = 'AND') {
$criteres = array();
$premier = array_pop($mots_cles);
$requete = "SELECT * FROM ".$this->table_liaison." lmc ".
"INNER JOIN ".$this->table_mots_cles." mc ".
"ON mc.tag = ".Cel::db()->proteger($premier)." AND lmc.id_tag = mc.id_tag ";
foreach ($mots_cles as $mot_cle) {
$requete .= " ".$operateur." id_element_lie IN (SELECT id_element_lie FROM ".$this->table_liaison." lmc ".
"INNER JOIN ".$this->table_mots_cles." mc ".
"ON mc.tag = ".Cel::db()->proteger($mot_cle)." AND lmc.id_tag = mc.id_tag ".
") ";
}
$images = Cel::db()->requeter($requete);
return $images;
}
private function construireRequeteChemin($mots_cles, $operateur = 'AND') {
$criteres = array();
$premier = array_pop($mots_cles);
$requete = "SELECT * FROM ".$this->table_liaison." lmc ".
"INNER JOIN ".$this->table_mots_cles." mc ".
"ON mc.chemin LIKE ".Cel::db()->proteger('%'.$premier.'%')." AND lmc.id_tag = mc.id_tag ";
foreach ($mots_cles as $mot_cle) {
$requete .= " ".$operateur." id_element_lie IN (SELECT id_element_lie FROM ".$this->table_liaison." lmc ".
"INNER JOIN ".$this->table_mots_cles." mc ".
"ON mc.chemin LIKE ".Cel::db()->proteger('%'.$mot_cle.'%')." AND lmc.id_tag = mc.id_tag ".
") ";
}
$images = Cel::db()->requeter($requete);
return $images;
}
public function obtenirArbre($id_utilisateur, $chemin = "/") {
$requete = "SELECT * FROM ".$this->table_mots_cles." ".
"WHERE id_utilisateur = ".Cel::db()->proteger($id_utilisateur)." AND ".
"chemin LIKE ".Cel::db()->proteger($chemin."%")." ".
"ORDER BY chemin";
$arbre = Cel::db()->requeter($requete.' -- '.__FILE__.':'.__LINE__);
usort($arbre, array('GestionMotsClesChemin', 'comparerProfNoeuds'));
return $arbre;
}
public function insererParChemin($mot_cle, $chemin_parent, $id_utilisateur) {
$chemin_mot_cle = self::getCheminHarmonise($chemin_parent, $mot_cle);
$requete = "INSERT IGNORE INTO ".$this->table_mots_cles." (chemin, id_utilisateur, tag) ".
"VALUES (".
Cel::db()->proteger($chemin_mot_cle).", ".
Cel::db()->proteger($id_utilisateur).", ".
Cel::db()->proteger($mot_cle)." ".
")";
$insertion = Cel::db()->executer($requete.' -- '.__FILE__.':'.__LINE__);
return $insertion;
}
public function insererParIdParent($mot_cle, $id_parent, $id_utilisateur) {
$mot_cle_simp = self::simplifier($mot_cle);
$sous_requete_chemin = "(SELECT chemin FROM ".$this->table_mots_cles." ctp ".
"WHERE ctp.id_tag = ".Cel::db()->proteger($id_parent).")";
$requete = "INSERT INTO ".$this->table_mots_cles."(chemin, id_utilisateur, tag) ".
"VALUES (".
"CONCAT(".$sous_requete_chemin.",".Cel::db()->proteger($mot_cle_simp).",'/'), ".
Cel::db()->proteger($id_utilisateur).", ".
Cel::db()->proteger($mot_cle)." ".
")";
$insertion = Cel::db()->executer($requete.' -- '.__FILE__.':'.__LINE__);
return $insertion;
}
public function lierParId($id_mot_cle, $id_element_lie) {
$requete = "INSERT INTO ".$this->table_liaison." (id_element_lie, id_tag) ".
"VALUES (".
Cel::db()->proteger($id_element_lie).", ".
Cel::db()->proteger($id_mot_cle)." ".
") ".
"ON DUPLICATE KEY UPDATE id_element_lie = id_element_lie ";
$liaison = Cel::db()->executer($requete.' -- '.__FILE__.':'.__LINE__);
return $liaison;
}
public function lierParChemin($chemin, $id_element_lie, $id_utilisateur) {
$sous_requete_chemin = '(SELECT id_tag FROM '.$this->table_mots_cles.' '.
'WHERE chemin = '.Cel::db()->proteger(self::harmoniserChemin($chemin)).' '.
'AND id_utilisateur = '.Cel::db()->proteger($id_utilisateur).' '.
')';
$requete = "INSERT INTO ".$this->table_liaison." (id_element_lie, id_tag) ".
"VALUES (".
Cel::db()->proteger($id_element_lie).", ".
$sous_requete_chemin." ".
") ".
"ON DUPLICATE KEY UPDATE id_element_lie = id_element_lie ";
$liaison = Cel::db()->executer($requete.' -- '.__FILE__.':'.__LINE__);
return $liaison;
}
// OK jusqu'ici !
// Déplacer un mot clé et le renommer est une même opération, dans le cas du renommage,
// seule la fin du chemin change
public function renommerChemin($ancien_chemin, $nouveau_chemin, $id_utilisateur) {
$ancien_chemin = self::harmoniserChemin($ancien_chemin);
$nouveau_chemin = self::harmoniserChemin($nouveau_chemin);
$ancien_chemin_p = Cel::db()->proteger($ancien_chemin);
$nouveau_chemin_p = Cel::db()->proteger($nouveau_chemin);
// TODO : triggers pour les tables liées ?
$requete = "UPDATE ".$this->table_mots_cles." ".
"SET chemin = REPLACE(chemin,".$ancien_chemin_p.", ".$nouveau_chemin_p.") ".
"WHERE chemin LIKE ".Cel::db()->proteger($ancien_chemin."%")." AND ".
"id_utilisateur = ".Cel::db()->proteger($id_utilisateur)." ";
$deplacement = Cel::db()->executer($requete.' -- '.__FILE__.':'.__LINE__);
return $deplacement;
}
public function supprimerChemin($chemin, $id_utilisateur) {
// TODO : triggers pour les tables liées ?
$requete = "DELETE FROM ".$this->$table_mots_cles." ".
"WHERE chemin LIKE ".Cel::db()->proteger($chemin."%")." AND ".
"id_utilisateur = ".Cel::db()->proteger($id_utilisateur)." ";
$suppression = Cel::db()->executer($requete.' -- '.__FILE__.':'.__LINE__);
return $suppression;
}
// Fonctions utilitaires
static public function comparerProfNoeuds($a, $b) {
$nb_slashs_a = substr_count($a['chemin'], '/');
$nb_slashs_b = substr_count($a['chemin'], '/');
$cmp = 0;
if($nb_slashs_a == $nb_slashs_b) {
$cmp = strcmp($a['chemin'], $b['chemin']);
} else {
$cmp = ($a['chemin'] > $b['chemin']) ? +1 : -1;
}
return $cmp;
}
static public function getCheminHarmonise($chemin_parent, $mot_cle) {
return self::harmoniserChemin($chemin_parent.'/'.self::simplifier($mot_cle).'/');
}
static public function harmoniserChemin($chemin) {
$chemin = self::startsWith($chemin,'/') ? $chemin : '/'.$chemin;
$chemin = self::endsWith($chemin,'/') ? $chemin : $chemin.'/';
return str_replace('//', '/', $chemin);
}
// fonction de slugification du mot clé
// http://stackoverflow.com/questions/2955251/php-function-to-make-slug-url-string
static public function simplifier($text)
{
return trim(str_replace(array('\\','/'), '', $text));
// replace non letter or digits by -
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
// trim
$text = trim($text, '-');
// transliterate
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
// lowercase
$text = strtolower($text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
if (empty($text))
{
return 'n-a';
}
return $text;
}
static public function startsWith($haystack, $needle) {
return $needle === "" || strpos($haystack, $needle) === 0;
}
static public function endsWith($haystack, $needle) {
return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
}
}
?>