<?php
// +-------------------------------------------------+
// © 2002-2004 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: fiche.class.php,v 1.22.10.1.2.2 2025/12/05 08:33:03 dgoron Exp $

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

global $class_path, $include_path;
require_once($include_path."/templates/fiche.tpl.php");
require_once($class_path."/parametres_perso.class.php");
require_once ($class_path."/spreadsheetPMB.class.php");

class fiche{

	public $id_fiche = 0;

	/**
	 * Parametres persos
	 *
	 * @var parametres_perso
	 */
	public $p_perso;

	public $liste_ids = array();

	public $page = 0;

	public $fiche_prec = 0;
	public $i_fiche_prec = 0;

	public $fiche_suiv = 0;
	public $i_fiche_suiv = 0;
	
	protected $url_base = '';

	public function __construct($id=0){
		global $prefix;

		$this->id_fiche = intval($id);
		$this->p_perso = new parametres_perso($prefix);
	}

	public function get_content_form($perso_) {
	    $content_form = "";
	    if (!$this->p_perso->no_special_fields) {
	        $content_form.=$perso_['CHECK_SCRIPTS'];
            $interface_content_form = new interface_entity_content_form(static::class);
	        $settings = ['uid_prefix' => 'pperso_', 'field_property' => 'NAME'];
	        $elements = $interface_content_form->add_custom_fields_elements($perso_['FIELDS'], $settings);
	        foreach ($elements as $element) {
	            $content_form .= $element->get_display();
	        }
	    }
	    return $content_form;
	}
	
	/*
	 * Formulaire d'édition d'une fiche
	 */
	public function get_form(){
		global $msg, $action;
		global $perso_word,$page,$nb_per_page,$i_search;

		if($action == 'save_and_new') {
		    $perso_ = $this->p_perso->show_editable_fields(0);
		} else {
		    $perso_ = $this->p_perso->show_editable_fields($this->id_fiche);
		}
		$interface_form = new interface_fiches_form('fiche_form');
		if(!$this->id_fiche  || $action=='save_and_new'){
		    $interface_form->set_label($msg['fichier_form_saisie']);
		} else{
		    $interface_form->set_label($msg['fichier_form_modify']);
		    $interface_form->set_object_id($this->id_fiche);
		}
		$interface_form->set_content_form($this->get_content_form($perso_));
		if (isset($perso_['FIELDS'][0]['NAME'])) {
		    $interface_form->set_field_focus($perso_['FIELDS'][0]['NAME']);
		}
		$interface_form->add_url_base("&perso_word=$perso_word&i_search=$i_search&page=$page&nb_per_page=$nb_per_page");
        return $interface_form->get_display();
	}

	/*
	 * Affiche le formulaire de consultation d'une fiche
	 */
	public function show_fiche_form(){
		global $form_edit_fiche,$msg,$charset, $categ;
		global $perso_word;
		global $nb_per_page,$i_search;

		if(!$this->id_fiche) {
		    return;
		}
		if (empty($this->url_base)) {
		    $this->url_base = "./fichier.php?categ=".(!empty($categ)? $categ : "search");
		}

		$values = $this->get_values($this->id_fiche);
		$this->get_info_navigation();

		$display = '';
		foreach($values as $key=>$val){
			$display .= "<div class='row'>
				<div class='colonne3'>
					<label class='etiquette'>".htmlentities($this->p_perso->t_fields[$key]['TITRE']." : ",ENT_QUOTES,$charset)."</label>
				</div>
				<div class='colonne_suite'>";
			for($i=0;$i<count($val);$i++){
				$display.= "<span>".$val[$i]."</span>";
			}
			$display .= "</div></div>";
		}

		$btn = "<input type='button' class='bouton' value='".htmlentities($msg[62],ENT_QUOTES,$charset)."' onclick=\"
			document.location='".$this->url_base."&action=edit&perso_word=$perso_word&page=".$this->page."&nb_per_page=$nb_per_page&i_search=".$i_search."&idfiche=".$this->id_fiche."';\" />";
		$form_edit_fiche=str_replace('!!perso_fields!!', $display, $form_edit_fiche);
		$form_edit_fiche=str_replace('!!btn!!',$btn, $form_edit_fiche);
		$form_edit_fiche=str_replace('!!btn_cancel!!',"<input type='button' class='bouton' value='".htmlentities($msg[76],ENT_QUOTES,$charset)."'
			onclick=\"document.location='".$this->url_base."&perso_word=$perso_word&page=".$this->page."&nb_per_page=$nb_per_page';\" />",$form_edit_fiche);
		$form_edit_fiche=str_replace('!!form_titre!!', $msg['fichier_form_consult'], $form_edit_fiche);
		$form_edit_fiche=str_replace('!!hidden_id!!',$this->id_fiche,$form_edit_fiche);


		if($this->fiche_prec){
			$form_edit_fiche=str_replace('!!visibility_prec!!',"",$form_edit_fiche);
			$form_edit_fiche=str_replace('!!action_prec!!',
			"onclick=\"document.location='".$this->url_base."&action=view&perso_word=$perso_word&idfiche=".$this->fiche_prec."&i_search=".$this->i_fiche_prec."&nb_per_page=$nb_per_page';\"",$form_edit_fiche);
		} else {
			$form_edit_fiche=str_replace('!!visibility_prec!!',"style='display:none';",$form_edit_fiche);
		}
		if($this->fiche_suiv){
			$form_edit_fiche=str_replace('!!visibility_suiv!!',"",$form_edit_fiche);
			$form_edit_fiche=str_replace('!!action_suiv!!',
			"onclick=\"document.location='".$this->url_base."&action=view&perso_word=$perso_word&idfiche=".$this->fiche_suiv."&i_search=".$this->i_fiche_suiv."&nb_per_page=$nb_per_page';\"",$form_edit_fiche);
		} else {
			$form_edit_fiche=str_replace('!!visibility_suiv!!',"style='display:none';",$form_edit_fiche);
		}

		$form_edit_fiche=str_replace('!!visibility_prec!!',"style='display:none';",$form_edit_fiche);
		$form_edit_fiche=str_replace('!!visibility_suiv!!',"style='display:none';",$form_edit_fiche);
		$form_edit_fiche=str_replace('!!action_prec!!',"",$form_edit_fiche);
		$form_edit_fiche=str_replace('!!action_suiv!!',"",$form_edit_fiche);
		$form_edit_fiche=str_replace('!!btn_del!!',"",$form_edit_fiche);

		return $form_edit_fiche;
	}

	/*
	 * Permmet lors de l'affichage d'une fiche de trouvé les infos de navigation en tenant compte de la recherche:
	 * id fiche précédante,
	 * id fiche suivante
	 * page du retour à la liste, et du retour sur effacement
	 */
	public function get_info_navigation(){
		global $perso_word;
		global $i_search;
		global $nb_per_page_search,$nb_per_page;

		$nb_per_page = intval($nb_per_page);
		if (!$nb_per_page){
			$nb_per_page = intval($nb_per_page_search);
		}
		if (!isset($perso_word)) {
		    $perso_word = '';
		}
		$perso_word = stripslashes($perso_word);
		$search_word = str_replace('*','%', $perso_word);

		$requete = "SELECT count(1) FROM fiche where infos_global like '%".addslashes($search_word)."%' or index_infos_global like '%".addslashes($perso_word)."%'";
		$res = pmb_mysql_query($requete);
		pmb_mysql_result($res, 0, 0);

		if(!$i_search) $limit="limit 0, 2";
		else $limit="limit ".($i_search-1).", 3";

		$req = "select id_fiche from fiche where infos_global like '%".addslashes($search_word)."%' or index_infos_global like '%".addslashes($perso_word)."%' $limit ";
		$res = pmb_mysql_query($req);
		$this->fiche_prec=0;
		$this->fiche_suiv=0;
		if ($nb=pmb_mysql_num_rows($res)) {
			$result=array();
			while($fic = pmb_mysql_fetch_object($res)){
				$result[] = $fic->id_fiche;
			}
			if($i_search<1 && $nb>1){
				$this->fiche_suiv=$result[1];
				$this->i_fiche_suiv=$i_search+1;
			}
			if($i_search && $nb>1){
				$this->fiche_prec=$result[0];
				$this->i_fiche_prec=$i_search-1;
				if($nb>2){
					$this->fiche_suiv=$result[2];
					$this->i_fiche_suiv=$i_search+1;
				}
			}
		}
		$this->page=(int)(($i_search)/$nb_per_page)+1;
	}

	/*
	 * Enregistrement d'une fiche
	 */
	public function save(){
		global $msg,$charset;

		if(!$this->id_fiche){
			$req = "insert into fiche set infos_global='', index_infos_global=''";
			pmb_mysql_query($req);
			$this->id_fiche = pmb_mysql_insert_id();
			print "<div class='row'><b>".htmlentities($msg['fiche_saved'],ENT_QUOTES,$charset)."</b></div>";
		} else {
			$req = "update fiche set infos_global='', index_infos_global='' where id_fiche='".$this->id_fiche."'";
			pmb_mysql_query($req);
		}
		//On met à jour les champs persos
		$this->p_perso->check_submited_fields();
		$this->p_perso->rec_fields_perso($this->id_fiche);

		//On met à jour l'index de la fiche
		$this->update_global_index($this->id_fiche);
	}

	/*
	 * suppression d'une fiche
	 */
	public function delete(){
		$query = "delete from fiche where id_fiche = ".$this->id_fiche;
		pmb_mysql_query($query);
		$query = "delete from ".$this->p_perso->prefix."_custom_values where  ".$this->p_perso->prefix."_custom_origine = ".$this->id_fiche;
		pmb_mysql_query($query);
	}

	/*
	 * Mis à jour de l'index d'une fiche
	 */
	public function update_global_index($id){
	    $id = intval($id);
		$infos_global = '';
		$infos_global_index = '';
		$mots_perso=$this->p_perso->get_fields_recherche($id);
		if($mots_perso) {
			$infos_global.= $mots_perso.' ';
			$infos_global_index.= strip_empty_words($mots_perso).' ';
		}
		$query = "update fiche set infos_global='".addslashes($infos_global)."', index_infos_global='".addslashes($infos_global_index)."' where id_fiche=$id";
		pmb_mysql_query($query);
	}

	/*
	 * Reindexation globale
	 */
	public function reindex_all(){
	    $query = "select id_fiche from fiche";
	    $res = pmb_mysql_query($query);
		while($fiche = pmb_mysql_fetch_object($res)){
			$this->update_global_index($fiche->id_fiche);
		}
	}

	/*
	 * Affiche le formulaire de reindexation
	 */
	public function show_reindex_form(){
		global $form_reindex;

		print $form_reindex;
	}

	/*
	 * On récupère les valeurs des champs visibles correspondant à la fiche
	 */
	public function get_values($id_fiche,$visible=0){
		$values=array();
		$tabl_val=$this->p_perso->show_fields($id_fiche);
		foreach ( $tabl_val["FIELDS"] as $value ) {
       		if((!$visible || $value["OPAC_SHOW"]) && ($value["AFF"] !== "")){
       			$values[$value["ID"]][]=$value["AFF"];
       		}
		}
		return $values;
	}

	public function display_results_tableau($liste_result,$back_url="",$i_search_deb=0,$export = false){
		global $charset, $msg;
		global $perso_word,$page;
		global $nb_per_page;
		
		$query = "select * from ".$this->p_perso->prefix."_custom where multiple=1 order by ordre";//where multiple=1";
		$result = pmb_mysql_query($query);
		if($export){
			$display = "<table id='result_table' style='width:100%'><tr>";
		}else{
			$display = "<script type='text/javascript' src='./javascript/sorttable.js'></script>\n<table id='result_table' style='width:100%' class=\"sortable\"><tr>";
		}

		$field_id=array();
		while($champ = pmb_mysql_fetch_object($result)){
			$field_id[]=$champ->idchamp;
			if($champ->multiple)$display .= "<th>".htmlentities($champ->titre,ENT_QUOTES,$charset)."</th>";
		}
		$display .= "</tr>";
		$cpt_ligne=0;
		foreach($liste_result as $index=>$liste){
		    if(!$cpt_ligne++%2) {
		        $class = "class='odd'";
		    } else {
		        $class = "class='even'";
		    }
			$this->liste_ids[] = $index;
			if($export){
				$display .= "<tr>";
			}else{
				$display .= "<tr style='cursor: pointer' $class onclick=\"document.location='./fichier.php?categ=search_advanced&action=view&perso_word=$perso_word&nb_per_page=$nb_per_page&page=$page&idfiche=$index&i_search=".$i_search_deb++."';\">";
			}
			foreach($field_id as $idchamp ){
			 	$display.= "<td>";
			 	if (!empty($liste[$idchamp]) && is_countable($liste[$idchamp])){
					$cpt=0;
					foreach($liste[$idchamp] as $valeur){
						if($cpt)$display.="<br />";
						$display.= $valeur;
						$cpt++;
					}
				}
			 	$display.= "</td>";
			}
			$display .= "</tr>";
		}
		$display .= "</table>";

		if($this->liste_ids)
			$display.="<input type='hidden' id='liste_ids' name='liste_ids' value='".implode(",",$this->liste_ids)."' />";

		if($back_url){
			$display .= "
			<div class='row'>
				<input type='button' class='bouton' value='".htmlentities($msg['fichier_result_list_return'],ENT_QUOTES,$charset)."' onclick='document.location=\"$back_url\"' />
			</div>
			";
		}
		return $display;
	}

	public function print_results_tableau($liste_result){
		global $charset;

		$worksheet = new spreadsheetPMB();
		$bold = array(
				'font'  => array(
						'bold'  => true
				)
		);

		$query = "select * from ".$this->p_perso->prefix."_custom where multiple=1 order by ordre";
		$result = pmb_mysql_query($query);
		$num_col=0;
		$num_ligne=0;
		$field_id=array();
		while($champ = pmb_mysql_fetch_object($result)){
			$field_id[]=$champ->idchamp;
			$worksheet->write($num_ligne,$num_col,$champ->titre,$bold);
			$num_col++;
		}
		$num_ligne++;
		if (is_countable($liste_result)) {
    		foreach($liste_result as $liste){
    			$num_col=0;
    			foreach($field_id as $idchamp ){
    			 	$val="";
    			 	if (!empty($liste[$idchamp]) && is_countable($liste[$idchamp])){
    					foreach($liste[$idchamp] as $valeur){
    						if($val)$val.="\n";
    						$val.= $valeur;
    					}
    				}
    				$worksheet->write($num_ligne,$num_col,html_entity_decode($val, ENT_QUOTES, $charset));
    			 	$num_col++;
    			}
    			$num_ligne++;
    		}
		}
		$worksheet->download('Tableau.xls');
	}
}