<?php
// +-------------------------------------------------+
// © 2002-2004 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// © 2006 mental works / www.mental-works.com contact@mental-works.com
// 	repris et corrigé par PMB Services 
// +-------------------------------------------------+
// $Id: tags.class.php,v 1.11 2026/01/14 10:42:41 dgoron Exp $

// définition de la classe d'affichage des 'tags'



class tags {

	// ---------------------------------------------------------------
	//		propriétés de la classe
	// ---------------------------------------------------------------
	public $search_tag=''; 
	// ---------------------------------------------------------------
	//		constructeur
	// ---------------------------------------------------------------
	public function __construct() {		
	}
	
	public function get_array($start='', $pos_cursor=0){
		global $pmb_keyword_sep;
		
		$liste_mots = array();
		$liste_res = array();
		$liste_finale = array();
		
		$deb_chaine='';
		$fin_chaine='';

	 	if(pmb_strlen($start)==$pos_cursor){
	 		$liste_mots=explode($pmb_keyword_sep,$start);
			$mot = array_pop($liste_mots);
			$deb_chaine = implode($pmb_keyword_sep,$liste_mots);
			if(trim($deb_chaine)!=='') $deb_chaine.=$pmb_keyword_sep;			
	 	} else {
	 		$liste_mots = explode($pmb_keyword_sep,pmb_substr($start,0,$pos_cursor));
	 		$mot = array_pop($liste_mots);
	 		$deb_chaine = implode($pmb_keyword_sep,$liste_mots);
	 		if (trim($deb_chaine)!=='') $deb_chaine.=$pmb_keyword_sep;
	 		$liste_mots = explode($pmb_keyword_sep,pmb_substr($start,$pos_cursor));
	 		array_shift($liste_mots);
	 		$fin_chaine = $pmb_keyword_sep.implode($pmb_keyword_sep,$liste_mots);	
	 	}
	 	$mot=trim($mot);
	 	if ($mot==='') return $liste_finale;
	 	
		$this->search_tag = $mot;

		$requete = "select distinct index_l from notices where index_l is not null and index_l like '".addslashes($mot)."%' or index_l like '%".$pmb_keyword_sep.addslashes($mot)."%' ";
		$res = pmb_mysql_query($requete);
		while(($mot_trouve=pmb_mysql_fetch_object($res))){
			$liste_tmp = explode($pmb_keyword_sep,$mot_trouve->index_l);
			foreach($liste_tmp as $v) {
				if (strip_empty_chars(pmb_substr($v,0,pmb_strlen($mot))) == strip_empty_chars($mot)) $liste_res[]=$v;
			}
		}
		$liste_res=array_unique($liste_res);
		asort($liste_res);
		
		foreach($liste_res as $v) {
			$liste_finale[] = array($v=>$deb_chaine.$v.$fin_chaine); 
		}
		return $liste_finale;
	}
	
	
	public function get_taille_search(){
		return strlen($this->search_tag);
	}
	
	/**
	 * droits d'acces utilisateur/notice (modification)
	 * @return string
	 */
	public static function get_acces_jm() {
	    global $gestion_acces_active, $gestion_acces_user_notice, $PMBuserid;
	    $acces_jm = '';
	    if ($gestion_acces_active == 1 && $gestion_acces_user_notice == 1) {
	        $ac = new acces();
	        $dom_1 = $ac->setDomain(1);
	        $acces_jm = $dom_1->getJoin($PMBuserid, 8, 'num_notice');
	    }
	    return $acces_jm;
	}
	
	public static function validate($id_tags = []) {
	    global $pmb_keyword_sep;
	    
	    if (empty($id_tags) || !is_countable($id_tags)) {
	        return;
	    }
	    
	    $acces_jm = static::get_acces_jm();
	    
	    $nb_valid_id_tags = count($id_tags);
	    for ($i = 0; $i < $nb_valid_id_tags; $i++) {
	        $id_tags[$i] = intval($id_tags[$i]);
	        $acces_m = 1;
	        if (!empty($acces_jm)) {
	            $q = "select count(1) from tags $acces_jm where id_tag=".$id_tags[$i];
	            $r = pmb_mysql_query($q);
	            if (pmb_mysql_result($r, 0, 0) == 0) {
	                $acces_m = 0;
	            }
	        }
	        if (!empty($acces_m)) {
	            $requete = "select libelle, num_notice, index_l from tags left join notices on notice_id=num_notice where id_tag=".$id_tags[$i];
	            $r = pmb_mysql_query($requete);
	            if (pmb_mysql_num_rows($r)) {
	                $loc = pmb_mysql_fetch_object($r);
	                $tab_index_l = array();
	                $tab_index_l = explode($pmb_keyword_sep, trim($loc->index_l));
	                $tab_index_l[] = stripslashes($loc->libelle);
	                $nb_tab_index_l = count($tab_index_l);
	                for ($j = 0; $j < $nb_tab_index_l; $j++) {
	                    if (empty($tab_index_l[$j])) {
	                        unset($tab_index_l[$j]);
	                    }
	                }
	                $index_l = addslashes(implode($pmb_keyword_sep, $tab_index_l));
	                $index_matieres = " ".strip_empty_words($index_l)." ";
	                
	                $requete = "update notices set index_l='$index_l',index_matieres='$index_matieres' where notice_id='$loc->num_notice'";
	                pmb_mysql_query($requete);
	                indexation_stack::push($loc->num_notice, TYPE_NOTICE);
	                $requete = "delete from tags where id_tag='".$id_tags[$i]."'";
	                pmb_mysql_query($requete);
	            }
	        }
	    }
	}
	
	public static function delete($id_tags = []) {
	    if (empty($id_tags) || !is_countable($id_tags)) {
	        return;
	    }
	    
	    $acces_jm = static::get_acces_jm();
	    
	    $nb_id_tags = count($id_tags);
	    for ($i = 0; $i < $nb_id_tags; $i++) {
	        $id_tags[$i] = intval($id_tags[$i]);
	        $acces_m = 1;
	        if (!empty($acces_jm)) {
	            $q = "select count(1) from tags $acces_jm where id_tag=".$id_tags[$i];
	            $r = pmb_mysql_query($q);
	            if (pmb_mysql_result($r, 0, 0) == 0) {
	                $acces_m = 0;
	            }
	        }
	        if (!empty($acces_m)) {
	            $rqt = "delete from tags where id_tag='".$id_tags[$i]."' ";
	            pmb_mysql_query($rqt);
	        }
	    }
	}
}