<?php
// +-------------------------------------------------+
// © 2002-2005 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: categories.class.php,v 1.36.2.1 2016-06-13 14:51:08 dgoron Exp $

if (stristr($_SERVER['REQUEST_URI'], ".class.php")) die("no access");

require_once($class_path."/noeuds.class.php");
require_once($class_path."/thesaurus.class.php");
require_once($class_path."/notice.class.php");
require_once($class_path."/indexation.class.php");
require_once($class_path."/vedette/vedette_composee.class.php");

class categories{
	
	
	var $num_noeud;					//Identifiant du noeud de rattachement
	var $langue;
	var $libelle_categorie = '';
	var $note_application = '';
	var $comment_public = '';
	var	$comment_voir = '';
	var $index_categorie = '';

	
	//Constructeur	 
	function categories($num_noeud, $langue) {

		global $dbh;
		$this->num_noeud = $num_noeud;				
		$this->langue = $langue;
		$q = "select count(1) from categories where num_noeud = '".$this->num_noeud."' and langue = '".$this->langue."' ";
		$r = pmb_mysql_query($q, $dbh);
		if (pmb_mysql_result($r, 0, 0) != 0) {
			$this->load();
		} else {
			$defaultLibelle="-";
			$q = "SELECT libelle_categorie FROM categories JOIN thesaurus ON id_thesaurus = num_thesaurus AND langue_defaut=langue AND num_noeud='".$this->num_noeud."'";
			$r = pmb_mysql_query($q, $dbh);
			if(pmb_mysql_num_rows($r)){
				$row=pmb_mysql_fetch_object($r);
				$defaultLibelle = $row->libelle_categorie;
			}
			$q = "insert into categories set num_noeud = '".$this->num_noeud."', langue = '".$langue."', ";
			$q.= "libelle_categorie = '".addslashes($defaultLibelle)."', note_application = '', comment_public = '', ";
			$q.= "comment_voir = '', index_categorie = '' ";
			$r = pmb_mysql_query($q, $dbh);
			//pour les appels en enregistrement de notice, la méthode save n'est pas appelée, donc on force
			$this->libelle_categorie = $defaultLibelle;
			$this->save();
		} 
	}


	// charge la catégorie à partir de la base si elle existe.
	function load(){
	
		global $dbh;

		$q = "select * from categories where num_noeud = '".$this->num_noeud."' and langue = '".$this->langue."' limit 1";
		$r = pmb_mysql_query($q, $dbh);
		$obj = pmb_mysql_fetch_object($r);
		$this->libelle_categorie = $obj->libelle_categorie;				
		$this->note_application = $obj->note_application;				
		$this->comment_public = $obj->comment_public;				
		$this->comment_voir = $obj->comment_voir;
		$this->index_categorie = $obj->index_categorie;

	}
	
	// enregistre la catégorie en base.
	function save(){
		
		global $dbh;
		global $msg;
		global $include_path;
		
		$no = new noeuds($this->num_noeud);
		$num_thesaurus = $no->num_thesaurus; 
		
		//On teste si la categorie existe à ce niveau 
		//$categ_exist = $this->searchLibelle($this->libelle_categorie,$num_thesaurus,$this->langue,$no->num_parent);
		//if ($categ_exist != 0) {
			//require_once("$include_path/user_error.inc.php");
			//warning($msg['create_category'],$msg['create_category_double']);
			//on supprime les relations dans la base
			//$this->delete($this->num_noeud,$this->langue);
			//$no->delete($this->num_noeud);
			//return FALSE;
		//} else {		
			$q = "update categories set ";
			$q.= "num_thesaurus = '".$num_thesaurus."', ";
			$q.= "libelle_categorie = '".addslashes($this->libelle_categorie)."', ";
			$q.= "note_application = '".addslashes($this->note_application)."', ";
			$q.= "comment_public = '".addslashes($this->comment_public)."', ";
			$q.= "comment_voir = '".addslashes($this->comment_voir)."', ";
			$q.= "index_categorie = ' ".addslashes(strip_empty_words($this->libelle_categorie,$this->langue))." ' ";
			$q.= "where num_noeud = '".$this->num_noeud."' and langue = '".$this->langue."' "; 
			$r = pmb_mysql_query($q, $dbh);
			categories::update_index($this->num_noeud);
			$this->update_index_path_word();

			// Mise à jour des vedettes composées contenant cette autorité
			vedette_composee::update_vedettes_built_with_element($this->num_noeud, "category");
		//}
	}
	
	function update_index_path_word(){
		global $dbh;
		global $msg;
		global $include_path;	
		global $thesaurus_auto_postage_search;
		global $thesaurus_auto_postage_search_nb_descendant,$thesaurus_auto_postage_search_nb_montant;	

		/*	auto_postage_descendant:
		* 		Soit categ : Europe:France:Sarthe
		* 		et une notice sous la categ Sarthe.
		* 	la recherche tous champs de Europe va sortir la notice sous la categ Sarthe 
		*/				 
		$no = new noeuds($this->num_noeud);
		$num_thesaurus = $no->num_thesaurus;	
		$path=$no->path;	
		$liste_num_noeud=explode('/',$path);
		// pour l'index coté gestion
		$lib_list=array();	
		if($thesaurus_auto_postage_search){	
			$limit=$thesaurus_auto_postage_search_nb_descendant;		
			if($limit){				
				$liste_num_noeud=explode('/',$path);
				if($limit != '*') array_splice($liste_num_noeud,0,count($liste_num_noeud)-$limit-1);
				$select_num_noeud=implode(',',$liste_num_noeud);
				$q = "select libelle_categorie from categories where num_noeud in( $select_num_noeud ) and langue = '".$this->langue."' and num_thesaurus=$num_thesaurus";
				$r = pmb_mysql_query($q, $dbh);
				while ($row = pmb_mysql_fetch_object($r))	{
					$lib_list[]= $row->libelle_categorie; 
				}
			}
		}		
		
		/*	auto_postage_montant:
		 * 		Soit categ : Europe:France:Sarthe
		 * 		et une notice sous la categ Europe.
		 * 	la recherche tous champs de Sarthe va sortir la notice sous la categ Europe 
		 */ 
		$liste_fils="";
		if($thesaurus_auto_postage_search){	
			$limit=$thesaurus_auto_postage_search_nb_montant;		
			if($limit){	
				if( is_numeric($limit))
					$liste_fils=" path regexp '^$path(\\/[0-9]*){0,$limit}$' ";
				elseif($limit == '*') 
					$liste_fils=" (path like '$path/%' or  path = '$path') ";	
				if($liste_fils)	{
					$q = "select libelle_categorie from categories,noeuds where id_noeud=num_noeud
					and $liste_fils and langue = '".$this->langue."' and categories.num_thesaurus=$num_thesaurus and noeuds.num_thesaurus=$num_thesaurus";
					$r = pmb_mysql_query($q, $dbh);
					while ($row = pmb_mysql_fetch_object($r))	{
						$lib_list[]= $row->libelle_categorie; 
					}
				}			
			}				
		}
				
		// Si rien, on ne met que le libelle de la categ
		if(!count($lib_list))$lib_list[]=$this->libelle_categorie;
		//$lib_list=array_unique  ($lib_list);
		$index=implode(" ",$lib_list);		
		$clean_index=strip_empty_words($index);
		
		$q = "update categories set ";
		$q.= "path_word_categ = ' ".trim(addslashes($index))." ', ";		
		$q.= "index_path_word_categ = ' ".trim(addslashes($clean_index))." ' ";
		$q.= "where num_noeud = '".$this->num_noeud."' and langue = '".$this->langue."' and num_thesaurus=$num_thesaurus"; 
		$r = pmb_mysql_query($q, $dbh);		
	}
	
	//verifie si une categorie existe dans la langue concernée
	static function exists($num_noeud, $langue) {
	
		global $dbh;
		
		$q = "select count(1) from categories where num_noeud = '".$num_noeud."' and langue = '".$langue."' ";
		$r = pmb_mysql_query($q, $dbh);
		if (pmb_mysql_result($r, 0, 0) == 0) return FALSE;
			else return TRUE;		
	}
	
	//supprime une categorie en base.
	function delete($num_noeud, $langue) {

		global $dbh;
		
		$q = "delete from categories where num_noeud = '".$num_noeud."' and langue = '".$langue."' ";
		$r = @pmb_mysql_query($q, $dbh);
	}		


	//Liste les libelles des ancetres d'une categorie dans la langue concernée 
	//a partir de la racine du thesaurus
	static function listAncestorNames($num_noeud=0, $langue) {
		
		global $dbh;
			
		/*if(!$num_noeud) {// Pas de $this dans une static
			$num_noeud = $this->num_noeud;
			$langue = $this->langue;
		}*/
		$thes = thesaurus::getByEltId($num_noeud);
		$id_list = noeuds::listAncestors($num_noeud);
		$id_list = array_reverse($id_list);
		$lib_list = '';
		
		foreach($id_list as $dummykey=>$id) {
			if (categories::exists($id, $langue)) $lg=$langue; 
			else $lg=$thes->langue_defaut; 
			$q = "select libelle_categorie from categories where num_noeud = '".$id."' ";
			$q.= "and langue = '".$lg."' limit 1";
			$r = pmb_mysql_query($q, $dbh);
			if (pmb_mysql_num_rows($r))	{
				$lib_list.= pmb_mysql_result($r, 0, 0); 
				if ($id != $num_noeud) $lib_list.= ':';
			}
		}
		return $lib_list;
	
	}


	//Retourne un tableau des ancetres d'une categorie dans la langue concernée 
	//a partir de la racine du thesaurus
	static function listAncestors($num_noeud=0, $langue='') {
		
		global $dbh;
			
		/*if(!$num_noeud) {// Pas de $this dans une static
			$num_noeud = $this->num_noeud;
			$langue = $this->langue;
		}*/
		$thes = thesaurus::getByEltId($num_noeud);
		$id_list = noeuds::listAncestors($num_noeud);
		$id_list = array_reverse($id_list);
		$anc_list = array();

		foreach($id_list as $key=>$id) {
			if (categories::exists($id, $langue)) $lg=$langue; 
			else $lg=$thes->langue_defaut; 
			$q = "select * from noeuds, categories ";
			$q.= "where categories.num_noeud = '".$id."' ";
			$q.= "and categories.langue = '".$lg."' ";
			$q.= "and categories.num_noeud = noeuds.id_noeud ";
			$q.= "limit 1";
			$r = pmb_mysql_query($q, $dbh);
			
			while ($row = pmb_mysql_fetch_object($r))	{
				$anc_list[$id]['num_noeud'] = $row->num_noeud;
				$anc_list[$id]['num_parent'] = $row->num_parent;
				$anc_list[$id]['num_renvoi_voir'] = $row->num_renvoi_voir;
				$anc_list[$id]['visible'] = $row->visible;
				$anc_list[$id]['num_thesaurus'] = $row->num_thesaurus;
				$anc_list[$id]['langue'] = $row->langue;
				$anc_list[$id]['libelle_categorie'] = $row->libelle_categorie;
				$anc_list[$id]['note_application'] = $row->note_application;
				$anc_list[$id]['comment_public'] = $row->comment_public;
				$anc_list[$id]['comment_voir'] = $row->comment_voir;
				$anc_list[$id]['index_categorie'] = $row->index_categorie;
				$anc_list[$id]['autorite'] = $row->autorite;
			}
		}
		return $anc_list;
	}
	

	//Retourne un resultset des enfants d'une categorie dans la langue concernée 
	function listChilds($num_noeud=0, $langue, $keep_tilde=1, $ordered=0) {
		
		global $dbh;
			
		if(!$num_noeud) {
			$num_noeud = $this->num_noeud;
			$langue = $this->langue;
		}
		$thes = thesaurus::getByEltId($num_noeud);
		$list = array();

		$q = "select ";
		$q.= "catdef.num_noeud, noeuds.autorite, noeuds.num_parent, noeuds.num_renvoi_voir, noeuds.visible, noeuds.num_thesaurus, ";
		$q.= "if (catlg.num_noeud is null, catdef.langue, catlg.langue ) as langue, ";
		$q.= "if (catlg.num_noeud is null, catdef.libelle_categorie, catlg.libelle_categorie ) as libelle_categorie, ";
		$q.= "if (catlg.num_noeud is null, catdef.note_application, catlg.note_application ) as note_application, ";
		$q.= "if (catlg.num_noeud is null, catdef.comment_public, catlg.comment_public ) as comment_public, ";
		$q.= "if (catlg.num_noeud is null, catdef.comment_voir, catlg.comment_voir ) as comment_voir, ";
		$q.= "if (catlg.num_noeud is null, catdef.index_categorie, catlg.index_categorie ) as index_categorie ";
		$q.= "from noeuds left join categories as catdef on noeuds.id_noeud=catdef.num_noeud and catdef.langue = '".$thes->langue_defaut."' "; 
		$q.= "left join categories as catlg on catdef.num_noeud = catlg.num_noeud and catlg.langue = '".$langue."' "; 
		$q.= "where ";
		$q.= "noeuds.num_parent = '".$num_noeud."' ";
		if (!$keep_tilde) $q.= "and catdef.libelle_categorie not like '~%' ";
		if ($ordered !== 0) $q.= "order by ".$ordered." ";
		$r = pmb_mysql_query($q, $dbh);

		return $r;
	}


	//optimization de la table categories
	function optimize() {
		
		global $dbh;
		
		$opt = pmb_mysql_query('OPTIMIZE TABLE categories', $dbh);
		return $opt;
		
	}			


	//recherche $libelle dans les libellés de la table categories et retourne 0 si non trouvé
	//sinon retourne identifiant de categorie
	function searchLibelle($libelle, $id_thesaurus=0, $lg=0, $num_parent=0) {
		
		global $dbh;
		global $lang;
		global $thesaurus_defaut;
		
		if (!$lg) $lg = $lang;
		if (!$id_thesaurus) $id_thesaurus = $thesaurus_defaut;
		
		$q = "select id_noeud from noeuds, categories where 1 ";
		if ($id_thesaurus != -1) $q.= "and noeuds.num_thesaurus = '".$id_thesaurus."' ";
		if ($num_parent) $q.= "and noeuds.num_parent = '".$num_parent."' ";
		if ($lg != -1) $q.= "and categories.langue = '".$lg."' ";
		$q.= "and categories.libelle_categorie = '".$libelle."' ";
		$q.= "and noeuds.id_noeud = categories.num_noeud ";
		$q.= "limit 1";
		$r = pmb_mysql_query($q, $dbh); 
		if (pmb_mysql_num_rows($r)) return pmb_mysql_result($r, 0, 0);
			else return 0;
		
	}

		
	//---------------------------------------------------------------
	// update_index($id) : maj des n-uplets la table notice_global_index 
	// en rapport avec cette catégorie	
	//---------------------------------------------------------------
	static function update_index($id) {
		global $dbh;
		
		//ajout des mots des termes dans la table words pour l autoindexation
		$q = "select trim(index_categorie) as index_categorie, langue from categories where num_noeud=".$id;
		$r = pmb_mysql_query($q,$dbh);
		$i=0;
		$t_words=array();
		if(pmb_mysql_num_rows($r)) {
			while ($row =pmb_mysql_fetch_object($r)) {
				$t_row = explode(' ',$row->index_categorie);
				if( is_array($t_row) && count($t_row) ) {
					$t_row = array_unique($t_row);
					foreach($t_row as $w) {
						if($w) {
							$t_words[$i]['word'] = $w;
							$t_words[$i]['lang'] = $row->langue;
							$i++;
						}
					}
				}
			}
		}
		if(count($t_words)) {
			//calcul de stem et double_metaphone
			foreach ($t_words as $i=>$w) {
				$q1 = "select id_word from words where word='".addslashes($w['word'])."' and lang='".addslashes($w['lang'])."' limit 1";
				$r1 = pmb_mysql_query($q1, $dbh);
				if(pmb_mysql_num_rows($r1)) {
					//le mot existe
					$t_words[$i]['allready_exists']=1;
				} else {
					//le mot n'existe pas
					$dmeta = new DoubleMetaPhone($w['word']);
					if($dmeta->primary || $dmeta->secondary){
						$t_words[$i]['double_metaphone'] = $dmeta->primary." ".$dmeta->secondary;
					}
					if($w['lang']=='fr_FR') {
						$stemming = new stemming($w['word']);
						$t_words[$i]['stem']=$stemming->stem;
					} else {
						$t_words[$i]['stem']='';
					}
				}
			}
			foreach($t_words as $i=>$w) {
				if (!$w['allready_exists']) {
					$q2 = "insert ignore into words (word, lang, double_metaphone, stem) values ('".$w['word']."', '".$w['lang']."', '".$w['double_metaphone']."', '".$w['stem']."') ";
					pmb_mysql_query($q2,$dbh);
				}	
			}
		}
		
		// On cherche tous les n-uplet de la table notice correspondant à cette catégorie.
		$found = pmb_mysql_query("select distinct notcateg_notice from notices_categories where num_noeud='".$id ."' ",$dbh);
		// Pour chaque n-uplet trouvés on met a jour la table notice_global_index avec l'auteur modifié :
		$num = pmb_mysql_num_rows($found);
	   	for($j=0;$j < $num; $j++) {
	   		$mesNotices = pmb_mysql_fetch_object($found);
	   		$notice_id = $mesNotices->notcateg_notice;
			notice::majNoticesGlobalIndex($notice_id);
			notice::majNoticesMotsGlobalIndex($notice_id,'subject');
	   	}
	   	//on cherche les questions correspondantes...
	   	$query = "select num_faq_question from faq_questions_categories where num_categ = ".$id;
	   	$result = pmb_mysql_query($query);
	   	if(pmb_mysql_num_rows($result)){
	   		global $include_path;
	   		$index = new indexation($include_path."/indexation/faq/question.xml", "faq_questions");
	   		while($row = pmb_mysql_fetch_object($result)){
	   			$index->maj($row->num_faq_question,"categories");
	   		}
	   	}
	}
	
	static function getlibelle($num_noeud=0, $langue=""){
		global $dbh;
		$lib="";
		$thes = thesaurus::getByEltId($num_noeud);
		if (categories::exists($num_noeud, $langue)) $lg=$langue; 
		else $lg=$thes->langue_defaut; 
		$q = "select libelle_categorie from categories where num_noeud = '".$num_noeud."' ";
		$q.= "and langue = '".$lg."' limit 1";
		$r = pmb_mysql_query($q, $dbh);
		if (pmb_mysql_num_rows($r))	{
			$lib= pmb_mysql_result($r, 0, 0); 
		}
		
		return $lib;
	}

	function process_categ_index() {
	global $dbh;
			
	$q = "select * from categories ";
	$r = pmb_mysql_query($q, $dbh);
	while ($obj = pmb_mysql_fetch_object($r)) {	
		$thes = new categories($obj->num_noeud,$obj->langue);
		$thes->update_index_path_word();		
	}	
}
}
?>