<?php
// +-------------------------------------------------+
// © 2002-2004 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: collstate.class.php,v 1.17.4.1 2016-01-04 09:44:35 jpermanne Exp $

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

// classes de gestion des états de collection de périodique

require_once($class_path."/parametres_perso.class.php");
require_once($include_path."/templates/collstate.tpl.php");

class collstate {

	// classe de la notice chapeau des périodiques
	var $id    			 = 0;       // id de l'état de collection
	var $serial_id       = 0;         // id du périodique lié
	var $location_id = 0;
	var $location_libelle = '';
	var $surloc_id		= 0;
	var $state_collections = '';
	var $emplacement	= 0;
	var $emplacement_libelle = '';
	var $type			= 0;
	var $type_libelle	= '';
	var $origine		= '';
	var $note			= '';
	var $cote			= '';
	var $archive		= '';
	var $lacune			= '';
	var $statut			= 0;
	var $statut_gestion_libelle = '';
	var $statut_opac_libelle = '';
	var $statut_visible_opac = '';
	var $statut_visible_opac_abon = '';
	var $statut_visible_gestion = '';
	var $statut_class_html = '';
	var $surloc_libelle = '';

// constructeur
function collstate($id=0,$serial_id=0) {
	// si id, allez chercher les infos dans la base
	if($id) {
		$this->id = $id;
		$this->fetch_data();
	}
	if($serial_id) {
		$this->serial_id = $serial_id;
	}
	return $this->id;
}

// récupération des infos en base
function fetch_data() {
	global $dbh;
	global $explr_invisible, $explr_visible_unmod, $explr_visible_mod, $pmb_droits_explr_localises ;
	global $pmb_sur_location_activate;

	$myQuery = pmb_mysql_query("SELECT * FROM collections_state WHERE collstate_id='".$this->id."' LIMIT 1", $dbh);
	$mycoll= pmb_mysql_fetch_object($myQuery);

	$this->serial_id=$mycoll->id_serial;
	$this->location_id=$mycoll->location_id;
	$this->state_collections=$mycoll->state_collections;
	$this->emplacement=$mycoll->collstate_emplacement;
	$this->type=$mycoll->collstate_type;
	$this->origine=$mycoll->collstate_origine;
	$this->note=$mycoll->collstate_note;
	$this->cote=$mycoll->collstate_cote;
	$this->archive=$mycoll->collstate_archive;
	$this->lacune=$mycoll->collstate_lacune;
	$this->statut=$mycoll->collstate_statut;

	$myQuery = pmb_mysql_query("SELECT * FROM arch_emplacement WHERE archempla_id='".$this->emplacement."' LIMIT 1", $dbh);
	$myempl= pmb_mysql_fetch_object($myQuery);
	$this->emplacement_libelle=$myempl->archempla_libelle;

	$myQuery = pmb_mysql_query("SELECT * FROM arch_type WHERE archtype_id='".$this->type."' LIMIT 1", $dbh);
	$mytype= pmb_mysql_fetch_object($myQuery);
	$this->type_libelle=$mytype->archtype_libelle;

	// Lecture des statuts
	$myQuery = pmb_mysql_query("SELECT * FROM arch_statut WHERE archstatut_id='".$this->statut."' LIMIT 1", $dbh);
	$mystatut= pmb_mysql_fetch_object($myQuery);
	$this->statut_gestion_libelle=$mystatut->archstatut_gestion_libelle;
	$this->statut_opac_libelle=$mystatut->archstatut_opac_libelle;
	$this->statut_visible_opac=$mystatut->archstatut_visible_opac;
	$this->statut_visible_opac_abon=$mystatut->archstatut_visible_opac_abon;
	$this->statut_visible_gestion=$mystatut->archstatut_visible_gestion;
	$this->statut_class_html=$mystatut->archstatut_class_html;

	$myQuery = pmb_mysql_query("select location_libelle, surloc_num from docs_location where idlocation='".$this->location_id."' LIMIT 1", $dbh);
	$mylocation= pmb_mysql_fetch_object($myQuery);
	$this->location_libelle=$mylocation->location_libelle;

	if ($pmb_droits_explr_localises) {
		$tab_invis=explode(",",$explr_invisible);
		$tab_unmod=explode(",",$explr_visible_unmod);

		$as_invis = array_search($this->location_id,$tab_invis);
		$as_unmod = array_search($this->location_id,$tab_unmod);
		if ($as_invis!== FALSE && $as_invis!== NULL) $this->explr_acces_autorise="INVIS" ;
		elseif ($as_unmod!== FALSE && $as_unmod!== NULL) $this->explr_acces_autorise="UNMOD" ;
		else $this->explr_acces_autorise="MODIF" ;
	} else $this->explr_acces_autorise="MODIF" ;
	
	if ($pmb_sur_location_activate) {
		$this->surloc_id = $mylocation->surloc_num;
		$myQuery = pmb_mysql_query("select surloc_libelle from sur_location where surloc_id='".$this->surloc_id."' LIMIT 1", $dbh);
		$mysurloc = pmb_mysql_fetch_object($myQuery);
		$this->surloc_libelle=$mysurloc->surloc_libelle;
	}
}

//Récupération de l'affichage dans l'isbd
function get_callstate_isbd() {
	global $msg, $pmb_etat_collections_localise,$pmb_droits_explr_localises,$explr_visible_mod;

	if ($pmb_etat_collections_localise && $pmb_droits_explr_localises && $explr_visible_mod) {
		$restrict_location=" and location_id in (".$explr_visible_mod.") and idlocation=location_id";
		$table_location=",docs_location";
		$select_location=",location_libelle";
	}
	$rqt="select state_collections $select_location from collections_state $table_location where id_serial=".$this->serial_id.$restrict_location;
	$execute_query=pmb_mysql_query($rqt);
	if (pmb_mysql_num_rows($execute_query)) {
		$bool=false;
		$affichage="<br /><strong>".$msg["4001"]."</strong><br />";
		while (($r=pmb_mysql_fetch_object($execute_query))) {
			if ($r->state_collections) {
				if ($r->location_libelle) $affichage .= "<strong>".$r->location_libelle."</strong> : ";
				$affichage .= str_replace("\n","<br />",$r->state_collections)."<br />\n";
				$bool=true;
			}
		}
		if ($bool==true) return($affichage);
	}
	return "";
}

//Récupérer de l'affichage complet
function get_display_list($base_url,$filtre,$debut=0,$page=0, $type=0,$form=1,$no_pagination=false) {
	global $dbh, $msg,$nb_per_page_a_search,$tpl_collstate_liste,$tpl_collstate_liste_line,$tpl_collstate_liste_form, $tpl_collstate_surloc_liste, $tpl_collstate_surloc_liste_line;
	global $explr_invisible,$pmb_droits_explr_localises,$pmb_etat_collections_localise,$deflt_docs_location;
	global $pmb_sur_location_activate;
	global $pmb_collstate_data,$class_path,$collstate_list_header,$collstate_list_footer;

	$location=$filtre->location;
	if (!$pmb_etat_collections_localise) {
		 $location="";
	}
	if (($pmb_droits_explr_localises)&&($explr_invisible)) $restrict_location=" location_id not in (".$explr_invisible.") and ";
	else  $restrict_location="";

	if ($pmb_sur_location_activate) {
		$join_sur_loc=" left join sur_location on docs_location.surloc_num=sur_location.surloc_id ";
		$order_sur_loc="surloc_libelle,";
	}else{
		$join_sur_loc="";
		$order_sur_loc="";
	}
	
	//On compte les bulletins à afficher
	$rqt="SELECT count( collstate_id) FROM collections_state WHERE $restrict_location ".($location?"(location_id='$location') and ":"")." id_serial='".$this->serial_id."' ";
	$myQuery = pmb_mysql_query($rqt, $dbh);
	$nbr_lignes = pmb_mysql_result($myQuery,0,0);

	$req="SELECT  collstate_id , location_id FROM collections_state LEFT JOIN docs_location ON location_id=idlocation ".$join_sur_loc." LEFT JOIN arch_emplacement ON collstate_emplacement=archempla_id WHERE $restrict_location ".($location?"(location_id='$location') and ":"")."
	id_serial='".$this->serial_id."' ORDER BY ".$order_sur_loc." ".($pmb_etat_collections_localise?"location_libelle, ":"")."archempla_libelle, collstate_cote "
	.(($no_pagination)?'':"LIMIT $debut,$nb_per_page_a_search");
	$myQuery = pmb_mysql_query($req, $dbh);
	if((pmb_mysql_num_rows($myQuery))) {
		
		if ($pmb_sur_location_activate) {
			$tpl_collstate_liste[$type] = str_replace('<!-- surloc -->',$tpl_collstate_surloc_liste,$tpl_collstate_liste[$type]);
			$tpl_collstate_liste_line[$type] = str_replace('<!-- surloc -->',$tpl_collstate_surloc_liste_line,$tpl_collstate_liste_line[$type]);
		}
		
		if ($pmb_collstate_data) {
			if (strstr($pmb_collstate_data, "#")) {
				require_once($class_path."/parametres_perso.class.php");
				$cp=new parametres_perso("collstate");
			}
			$colonnesarray=explode(",",$pmb_collstate_data);
			$collstate_list_header_deb="<tr>";
			
			for ($i=0; $i<count($colonnesarray); $i++) {
				if (substr($colonnesarray[$i],0,1)=="#") {
					//champs personnalisés
					$id=substr($colonnesarray[$i],1);
					if (!$cp->no_special_fields) {
						$collstate_list_header_deb.="<th class='collstate_header_".$colonnesarray[$i]."'>".htmlentities($cp->t_fields[$id]["TITRE"],ENT_QUOTES, $charset)."</th>";
					}
				}else{
					eval ("\$colencours=\$msg['collstate_header_".$colonnesarray[$i]."'];");
					$collstate_list_header_deb.="<th class='collstate_header_".$colonnesarray[$i]."'>".htmlentities($colencours,ENT_QUOTES, $charset)."</th>";
				}
			}
			$collstate_list_header_deb.="</tr>";
			$parity=1;
			$liste="";
			while(($coll = pmb_mysql_fetch_object($myQuery))) {
				$my_collstate=new collstate($coll->collstate_id);
				if ($parity++ % 2) $pair_impair = "even"; else $pair_impair = "odd";
				$tr_surbrillance = "onmouseover=\"this.className='surbrillance'\" onmouseout=\"this.className='".$pair_impair."'\" ";
				$liste.="<tr class='".$pair_impair."' style='cursor: pointer' ".$tr_surbrillance." >";
				$colencours="";
				for ($i=0; $i<count($colonnesarray); $i++) {
					if($my_collstate->explr_acces_autorise=="MODIF") {
						$tr_javascript="  onmousedown=\"document.location='./catalog.php?categ=serials&sub=collstate_form&id=".$coll->collstate_id."&serial_id=".$this->serial_id."';\" ";
					} else {
						$tr_javascript="";
					}
					if (substr($colonnesarray[$i],0,1)=="#") {
						//champs personnalisés
						$id=substr($colonnesarray[$i],1);
						$cp->get_values($coll->collstate_id);
						if (!$cp->no_special_fields) {
							$temp=$cp->get_formatted_output($cp->values[$id], $id);
							if (!$temp) $temp=" ";
							$liste.="<td class='".$colonnesarray[$i]."' ".$tr_javascript." >".htmlentities($temp,ENT_QUOTES, $charset)."</td>";
						}
					}else{
						eval ("\$colencours=\$my_collstate->".$colonnesarray[$i].";");
						$liste.="<td class='".$colonnesarray[$i]."' ".$tr_javascript." >".htmlentities($colencours,ENT_QUOTES, $charset)."</td>";
					}
				}
				$liste.="</tr>";
			}
			$liste = $collstate_list_header.$collstate_list_header_deb.$liste.$collstate_list_footer;
		} else {
			$parity=1;
			while(($coll = pmb_mysql_fetch_object($myQuery))) {
				$my_collstate=new collstate($coll->collstate_id);
			/*
				Avoir comment gerer un + pour des grands etats de collections
						if (count($my_collstate->state_collections)>80 || count($my_collstate->lacune)>80) {
							$plus_statecollection="<img src='images/plus.gif' class='img_plus' onClick='if (event) e=event; else e=window.event; e.cancelBubble=true; if (e.stopPropagation) e.stopPropagation(); show_sources(\"!!id!!\"); '/>";
							$texte_statecollection="<tr class='$pair_impair' style='display:none' id='".$coll->collstate_id."'><td>&nbsp;</td><td colspan='3'><table style='border:1px solid'>
							<td>".str_replace("\n","<br />",$my_collstate->state_collections)."</td>
							<td>".str_replace("\n","<br />",$my_collstate->lacune)."</td>";
						} else $plus_statecollection="<td>&nbsp;</td>";
			*/
	
	
				if ($parity++ % 2) $pair_impair = "even"; else $pair_impair = "odd";
				// Si modifiable, ajout du lien vers le formulaire
				if($my_collstate->explr_acces_autorise=="MODIF") {
		        	$tr_javascript="  onmousedown=\"document.location='./catalog.php?categ=serials&sub=collstate_form&id=".$coll->collstate_id."&serial_id=".$this->serial_id."';\" ";
				} else {
					$tr_javascript="";
				}
		        $tr_surbrillance = "onmouseover=\"this.className='surbrillance'\" onmouseout=\"this.className='".$pair_impair."'\" ";
		        
		        $line = str_replace('!!tr_javascript!!',$tr_javascript , $tpl_collstate_liste_line[$type]);
		        $line = str_replace('!!tr_surbrillance!!',$tr_surbrillance , $line);
		        $line = str_replace('!!pair_impair!!',$pair_impair , $line);
				if ($pmb_sur_location_activate) {
		        	$line = str_replace('!!surloc!!', $my_collstate->surloc_libelle, $line);
		        }
		        $line = str_replace('!!localisation!!', $my_collstate->location_libelle, $line);
		        $line = str_replace('!!cote!!', $my_collstate->cote, $line);
		        $line = str_replace('!!type_libelle!!', $my_collstate->type_libelle, $line);
		        $line = str_replace('!!emplacement_libelle!!', $my_collstate->emplacement_libelle, $line);
		        $line = str_replace('!!statut_libelle!!', "<span class='".$my_collstate->statut_class_html."'  style='margin-right: 3px;'><img src='./images/spacer.gif' width='10' height='10' /></span>".$my_collstate->statut_gestion_libelle, $line);
		        $line = str_replace('!!origine!!', $my_collstate->origine, $line);
		        $line = str_replace('!!state_collections!!',str_replace("\n","<br />",$my_collstate->state_collections), $line);
		        $line = str_replace('!!archive!!',$my_collstate->archive, $line);
		        $line = str_replace('!!lacune!!', str_replace("\n","<br />",$my_collstate->lacune), $line);
		        $liste.=$line;
			}
			$liste = str_replace('!!collstate_liste!!',$liste , $tpl_collstate_liste[$type]);
		}
	} else {
		$liste= $msg["collstate_no_collstate"];
	}

	if($form)$liste = str_replace('!!collstate_table!!',$liste , $tpl_collstate_liste_form);
	$liste = str_replace('!!base_url!!', $base_url, $liste);
	$liste = str_replace('!!location!!', $location, $liste);

	$this->liste=$liste;
	$this->nbr=$nbr_lignes;
	// barre de navigation par page
	$this->pagination = aff_pagination ($base_url."&location=$location", $nbr_lignes, $nb_per_page_a_search, $page, 10, false, true) ;
}

// fonction de mise à jour ou de création d'état de collection
function update($value) {
	global $dbh,$msg;
	$fields="";
	$id_serial=0;
	foreach($value as $key => $val) {
		if($fields) $fields.=",";
		$fields.=" $key='".addslashes($val)."' ";
		if($key == "id_serial"){
			$id_serial=addslashes($val);
		}
	}
	if($this->id) {
		// modif
		$no_erreur=pmb_mysql_query("UPDATE collections_state SET $fields WHERE collstate_id=".$this->id, $dbh);
		if(!$no_erreur) {
			error_message($msg["collstate_add_collstate"], $msg["collstate_add_error"],1);
			exit;
		}

	} else {
		// create
		$no_erreur=pmb_mysql_query("INSERT INTO collections_state SET $fields ", $dbh);
		$this->id = pmb_mysql_insert_id($dbh);
		if(!$no_erreur) {
			error_message($msg["collstate_edit_collstate"], $msg["collstate_add_error"],1);
			exit;
		}
	}
	
	if($id_serial){
		notice::majNoticesMotsGlobalIndex($id_serial,'collstate');
	}
	return $this->id;
}

function update_from_form() {
	global $state_collections,$origine,$archive,$cote,$note,$lacune,$serial_id,$archstatut_id,$archtype_id,$location_id,$archempla_id;
	global $deflt_docs_location;
	
	$value = new stdClass();
	$value->id_serial=stripslashes($serial_id);
	if(!$location_id) $location_id=$deflt_docs_location;
	$value->location_id=stripslashes($location_id);
	$value->state_collections=stripslashes($state_collections);
	$value->collstate_emplacement=stripslashes($archempla_id);
	$value->collstate_type=stripslashes($archtype_id);
	$value->collstate_origine=stripslashes($origine);
	$value->collstate_note=stripslashes($note);
	$value->collstate_cote=stripslashes($cote);
	$value->collstate_archive=stripslashes($archive);
	$value->collstate_lacune=stripslashes($lacune);
	if(!$archstatut_id)$archstatut_id=1;
	$value->collstate_statut=stripslashes($archstatut_id);
	$this->update($value);

	//Traitement des champs perso
	$p_perso=new parametres_perso("collstate");
	$p_perso->check_submited_fields();
	$p_perso->rec_fields_perso($this->id);
	
	if($value->id_serial){
		notice::majNoticesMotsGlobalIndex($value->id_serial,'collstate');
	}
}
// fonction générant le form de saisie de notice chapeau
function do_form() {
	global $msg;
	global $collstate_form,$statut_field,$emplacement_field, $location_field, $support_field;
	global $deflt_docs_location;
	global 	$deflt_arch_statut,$deflt_arch_emplacement,$deflt_arch_type;
	global $charset;
	global $pmb_etat_collections_localise;
	// titre formulaire
	if($this->id) {
		$libelle=$libelle=$msg["collstate_edit_collstate"];
		$link_delete="<input type='button' class='bouton' value='$msg[63]' onClick=\"confirm_delete();\" />";

	} else {
		$libelle=$msg["collstate_add_collstate"];
		$link_delete="";
	}
	$collstate_form = str_replace('!!id!!', htmlentities($this->id,ENT_QUOTES,$charset), $collstate_form);
	$collstate_form = str_replace('!!location_id!!', htmlentities($this->location_id,ENT_QUOTES,$charset), $collstate_form);
	$collstate_form = str_replace('!!serial_id!!', htmlentities($this->serial_id,ENT_QUOTES,$charset), $collstate_form);
	$action="./catalog.php?categ=serials&sub=collstate_update&serial_id=".rawurlencode($this->serial_id)."&id=".rawurlencode($this->id);
	$collstate_form = str_replace('!!action!!', $action, $collstate_form);
	$collstate_form = str_replace('!!delete!!', $link_delete, $collstate_form);
	$collstate_form = str_replace('!!libelle!!',$libelle , $collstate_form);

	$collstate_form = str_replace('!!origine!!',htmlentities($this->origine,ENT_QUOTES,$charset) , $collstate_form);
	$collstate_form = str_replace('!!archive!!',htmlentities($this->archive,ENT_QUOTES,$charset) , $collstate_form);
	$collstate_form = str_replace('!!cote!!',htmlentities($this->cote,ENT_QUOTES,$charset) , $collstate_form);
	$collstate_form = str_replace('!!note!!',htmlentities($this->note,ENT_QUOTES,$charset) , $collstate_form);
	$collstate_form = str_replace('!!lacune!!',htmlentities($this->lacune,ENT_QUOTES,$charset) , $collstate_form);
	$collstate_form = str_replace('!!state_collections!!',htmlentities($this->state_collections,ENT_QUOTES,$charset) , $collstate_form);

	// champs des localisations
	if($pmb_etat_collections_localise) {
		if(!$this->location_id) $this->location_id=$deflt_docs_location;
		$select = gen_liste("select distinct idlocation, location_libelle from docs_location order by 2 ", "idlocation", "location_libelle", 'location_id', "", $this->location_id, "", "","","",0);
		$field="";
		if($select) $field = str_replace('!!location!!',$select, $location_field);
		$collstate_form = str_replace('!!location_field!!',$field, $collstate_form);
	}else{
		$field="<input type='hidden' name='location_id' id='location_id' value=''/> ";
		$collstate_form = str_replace('!!location_field!!',$field, $collstate_form);
	}


	// champs des emplacements
	if(!$this->emplacement) $this->emplacement=$deflt_arch_emplacement;
	$select = gen_liste("select archempla_id, archempla_libelle from arch_emplacement order by 2", "archempla_id", "archempla_libelle", "archempla_id", "",$this->emplacement, "",  "", "","","",0) ;
	$field="";
	if($select) $field = str_replace('!!emplacement!!',$select, $emplacement_field);
	$collstate_form = str_replace('!!emplacement_field!!',$field, $collstate_form);

	// champs des supports
	if(!$this->type) $this->type=$deflt_arch_type;
	$select = gen_liste("select archtype_id, archtype_libelle from arch_type order by 2", "archtype_id", "archtype_libelle", "archtype_id", "", $this->type, "", "","","",0) ;
	$field="";
	if($select) $field = str_replace('!!support!!',$select, $support_field);
	$collstate_form = str_replace('!!support_field!!',$field, $collstate_form);

	// champs des statuts
	if(!$this->statut) $this->statut=$deflt_arch_statut;
	$select = gen_liste("select archstatut_id, archstatut_gestion_libelle from arch_statut order by 2", "archstatut_id", "archstatut_gestion_libelle", "archstatut_id", "", $this->statut, "", "","","",0) ;
	$field="";
	if($select) $field = str_replace('!!statut!!',$select, $statut_field);
	$collstate_form = str_replace('!!statut_field!!',$field, $collstate_form);

	// Champs perso
	$p_perso=new parametres_perso("collstate");
	$parametres_perso="";
	if (!$p_perso->no_special_fields) {
		$perso_=$p_perso->show_editable_fields($this->id);
		$perso="";
		for ($i=0; $i<count($perso_["FIELDS"]); $i++) {
			$p=$perso_["FIELDS"][$i];
			$perso.="
				<div class='row'>
					<label for='".$p["NAME"]."' class='etiquette'>".$p["TITRE"]."</label>
				</div>
				<div class='row'>
					".$p["AFF"]."
				</div>";
		}
		$perso.=$perso_["CHECK_SCRIPTS"];
		$parametres_perso.=$perso;
	}
	$collstate_form = str_replace('!!parametres_perso!!',$parametres_perso , $collstate_form);

	$link_annul = "onClick=\"unload_off();history.go(-1);\"";
	$collstate_form = str_replace('!!annul!!', $link_annul, $collstate_form);

	//vérification de la présence de champs perso
	//si non, on confirme la soumission du formulaire
	if($p_perso->no_special_fields)
		$return_form = "return true";
	//sinon, on vérifie leurs valeurs
	else $return_form = "return check_form()";
	$collstate_form = str_replace('!!return_form!!',$return_form, $collstate_form);

	return $collstate_form;
}

// suppression d'une collection ou de toute les collections d'un périodique
function delete() {
	global $dbh;

	if($this->id) {
		//On nettoye l'index
		if(!$this->serial_id){
			$req="SELECT id_serial FROM collections_state WHERE collstate_id='".$this->id."'";
			$res=pmb_mysql_query($req,$dbh);
			if($res && pmb_mysql_num_rows($res)){
				$this->serial_id=pmb_mysql_result($res,0,0);
			}
		}
		//elimination des champs persos
		$p_perso=new parametres_perso("collstate");
		$p_perso->delete_values($this->id);
		pmb_mysql_query("DELETE from collections_state WHERE collstate_id='".$this->id."' ", $dbh);
	} else if($this->serial_id) {
		$myQuery = pmb_mysql_query("SELECT collstate_id FROM collections_state WHERE id_serial='".$this->serial_id."' ", $dbh);
		if((pmb_mysql_num_rows($myQuery))) {
			while(($coll = pmb_mysql_fetch_object($myQuery))) {
				$my_collstate=new collstate($coll->collstate_id);
				$my_collstate->delete();
			}
		}
	}
	//On nettoye l'index
	notice::majNoticesMotsGlobalIndex($this->serial_id,'collstate');
}

} // fin définition classe
