<?php
// +-------------------------------------------------+
// ï¿½ 2002-2004 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: serial_func.inc.php,v 1.111.4.5 2025/12/18 08:39:42 dgoron Exp $

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

global $class_path, $include_path;
require_once("$class_path/docs_location.class.php");
require_once ($include_path."/avis_notice.inc.php");
require_once($include_path.'/h2o/pmb_h2o.inc.php');
require_once($class_path.'/records_tabs.class.php');
require_once($class_path.'/notice.class.php');
require_once ($class_path."/caddie/caddie_controller.class.php");

function get_serial_title($serial_id) {
    global $msg;
    global $serial_action_bar;
    global $pmb_url_base;
    global $current;
    
    $serial_id = intval($serial_id);
    if (empty($serial_id)) {
        return '';
    }
    $myQuery = pmb_mysql_query("SELECT * FROM notices WHERE notice_id=$serial_id ");
    if (!pmb_mysql_num_rows($myQuery)) {
        return '';
    }
    //Bulletins
    $myPerio = pmb_mysql_fetch_object($myQuery);
    // function serial_display ($id, $level='1', $action_serial='', $action_analysis='', $action_bulletin='', $lien_suppr_cart="", $lien_explnum="", $bouton_explnum=1,$print=0,$show_explnum=1, $show_statut=0, $show_opac_hidden_fields=true ) {
    $isbd = new serial_display($myPerio, 5, "",                      "",                 "",                  "",                  "./catalog.php?categ=serials&sub=explnum_form&serial_id=!!serial_id!!&explnum_id=!!explnum_id!!");
    $perio_header = $isbd->header;
    
    // isbd du périodique
    $perio_isbd = $isbd->isbd;
    $isbd->get_etat_periodique();
    $perio_isbd.=$isbd->print_etat_periodique();
    
    global $avis_quoifaire,$valid_id_avis;
    $perio_isbd = str_replace('<!-- !!avis_notice!! -->', avis_notice($serial_id,$avis_quoifaire,$valid_id_avis), $perio_isbd);
    
    $perio_isbd = str_replace('<!-- !!caddies_notice!! -->', caddie_controller::get_display_list_from_item('display', 'NOTI', $serial_id), $perio_isbd);
    
    // lien d'ajout d'une notice mère à un caddie
    $cart_click_noti = "onClick=\"openPopUp('./cart.php?object_type=NOTI&item=!!item!!', 'cart')\"";
    $cart_link = "<img src='".get_url_icon('basket_small_20x20.gif')."' class='align_middle' alt='basket' title=\"{$msg[400]}\" $cart_click_noti>";
    
    if ($current!==false) {
        $print_action = "&nbsp;<a href='#' onClick=\"openPopUp('./print.php?current_print=$current&notice_id=".$serial_id."&action_print=print_prepare','print'); w.focus(); return false;\"><img src='".get_url_icon('print.gif')."' style='border:0px' class='center' alt=\"".$msg["histo_print"]."\" title=\"".$msg["histo_print"]."\"/></a>";
    } else {
        $print_action = "";
    }
    
    $visualise_click_notice="
	<script type=\"text/javascript\" src='./javascript/select.js'></script>
        
	<a href='#' onClick='show_frame(\"".$pmb_url_base."opac_css/notice_view.php?id=$serial_id\")'><img src='".get_url_icon('search.gif')."' class='align_middle' title=\"{$msg["noti_see_gestion"]}\" style='border:0px' /></a>";
    
    $serial_action_bar = str_replace('!!actions_buttons!!', $isbd->get_display_actions_buttons(), $serial_action_bar);
    
    $serial_action_bar = str_replace('!!serial_id!!', $serial_id, $serial_action_bar);
    
    $serial_action_bar = str_replace('!!issn!!', $myPerio->code, $serial_action_bar);
    
    // action_bar : serials.tpl.php...
    // mise à jour des info du javascript
    $serial_action_bar = str_replace('!!nb_bulletins!!', $isbd->serial_nb_bulletins, $serial_action_bar);
    $serial_action_bar = str_replace('!!nb_articles!!', $isbd->serial_nb_articles, $serial_action_bar);
    $serial_action_bar = str_replace('!!nb_expl!!', $isbd->serial_nb_exemplaires, $serial_action_bar);
    $serial_action_bar = str_replace('!!nb_etat_coll!!', $isbd->serial_nb_etats_collection, $serial_action_bar);
    $serial_action_bar = str_replace('!!nb_abo!!', $isbd->serial_nb_abo_actif, $serial_action_bar);
    
    // Titre de la page
    $serial_title = '<script type="text/javascript">document.title = "'.addslashes(pmb_bidi(strip_tags($isbd->notice->tit1))).'";</script>';
    
    // titre général du périodique
    $serial_title .= "
	<div class='row'>
		<div class='notice-perio'>
			<h3 style='display: inline;' class='notice-perio-title'>".$isbd->aff_statut.str_replace('!!item!!', $serial_id, $cart_link).$print_action.$visualise_click_notice." ".$perio_header."</h3>
			<div class='row'>".$perio_isbd."</div>
			<hr />
			<div class='row'>".$serial_action_bar."</div>
		</div>
	</div>";
    return $serial_title;
}

// résultat de recherche pour gestion des périodiques
function show_serial_info($serial_id, $page, $nbr_lignes) {
	global $msg;
	global $nb_per_page_a_search;
	global $charset;
	global $deflt_bulletinage_location,$location;
	global $pmb_droits_explr_localises,$explr_invisible,$explr_visible_unmod;
	// barre de restriction des bulletins affichés
	global $aff_bulletins_restrict_numero, $aff_bulletins_restrict_date, $aff_bulletins_restrict_periode ;
	global $sort_children;
	global $pmb_url_base, $categ, $sub, $quoi, $view, $tab_page, $tab_nb_per_page;
	global $bull_date_start,$bull_date_end;
	
	if($view == "collstate"){
	    
	}else{
        $location=((string)$location==""?$deflt_bulletinage_location:$location);
    }
	
	$url_suffix = "";
	if ($quoi) $url_suffix .= "&quoi=".$quoi;
	if ($tab_page) $url_suffix .= "&tab_page=".$tab_page."&tab_nb_per_page=".$tab_nb_per_page;
	 
	$base_url = serial::get_permalink($serial_id);
	if ($serial_id) $myQuery = pmb_mysql_query("SELECT * FROM notices WHERE notice_id=$serial_id ");
	
	if ($serial_id && pmb_mysql_num_rows($myQuery)) {
		if (!$page) $page=1;
		$debut = ($page-1)*$nb_per_page_a_search;
		switch ($view) {
			case "abon":
				$base_url = serial::get_permalink($serial_id)."&view=abon".$url_suffix;
				break;
			case "modele":
				break;
			case "collstate":
				$base_url = serial::get_permalink($serial_id)."&view=collstate".$url_suffix;
				break;				
			default:
				// barre de restriction des bulletins affichés
				$clause="";
				if ($aff_bulletins_restrict_numero) {
					$clause = " and bulletin_numero like '%".str_replace("*","%",$aff_bulletins_restrict_numero)."%' ";
					$base_url .= "&aff_bulletins_restrict_numero=".urlencode($aff_bulletins_restrict_numero) ;
				}			
				if ($aff_bulletins_restrict_date) {
					$aff_bulletins_restrict_date_traite = str_replace("*","%",$aff_bulletins_restrict_date) ;
					$tab_bulletins_restrict_date = explode ($msg['format_date_input_separator'],$aff_bulletins_restrict_date_traite) ;
					if(count($tab_bulletins_restrict_date)==3)$aff_bulletins_restrict_date_traite = $tab_bulletins_restrict_date[2]."-".$tab_bulletins_restrict_date[1]."-".$tab_bulletins_restrict_date[0];
					if(count($tab_bulletins_restrict_date)==2)$aff_bulletins_restrict_date_traite = $tab_bulletins_restrict_date[1]."-".$tab_bulletins_restrict_date[0];
					if(count($tab_bulletins_restrict_date)==1)$aff_bulletins_restrict_date_traite = $tab_bulletins_restrict_date[0];
					$clause .= " and date_date like '%".$aff_bulletins_restrict_date_traite."%'" ;
					$base_url .= "&aff_bulletins_restrict_date=".urlencode($aff_bulletins_restrict_date) ;
				}
				if ($aff_bulletins_restrict_periode) {
					$aff_bulletins_restrict_periode_traite = str_replace("*","%",$aff_bulletins_restrict_periode) ;
					$clause .= " and mention_date like '%".$aff_bulletins_restrict_periode_traite."%'" ;
					$base_url .= "&aff_bulletins_restrict_periode=".urlencode($aff_bulletins_restrict_periode) ;
				}
				$base_url .= $url_suffix;

				$filter_date=compare_date($bull_date_start,$bull_date_end);
				//On compte les expl de la localisation
// 				$rqt="SELECT COUNT(1) FROM bulletins ".($location?", exemplaires":"")." WHERE ".($location?"(expl_bulletin=bulletin_id and expl_location='$location' or expl_location is null) and ":"")." bulletin_notice='$serial_id' $filter_date ";
// 				$myQuery = pmb_mysql_query($rqt);
// 				$nb_expl_loc = pmb_mysql_result($myQuery,0,0);
		
				//On compte les bulletinsà afficher
				$rqt="SELECT count(distinct bulletin_id) FROM bulletins ".($location?", exemplaires":"")." WHERE ".($location?"(expl_bulletin=bulletin_id and expl_location='$location' or expl_location is null) and ":"")." bulletin_notice='$serial_id' $clause $filter_date ";
				$myQuery = pmb_mysql_query($rqt);
				$nbr_lignes = pmb_mysql_result($myQuery,0,0);
				break;
		}
		
		// titre général du périodique
        print get_serial_title($serial_id);
		
		// bulletinage
		print serial_display::get_display_tabs($serial_id);
		
		if(empty($view)) {
			print serial_display::get_display_bulletins($serial_id);
		} else {
		    switch($view) {
		        case "modele":
		            print serial_display::get_display_modeles($serial_id);
		            break;
		        case "abon":
		            print serial_display::get_display_abts($serial_id);
		            break;
		        case "collstate":
		            print serial_display::get_display_collstate($serial_id);
		            break;
		        default:
		            print serial_display::get_display_bulletins($serial_id);
		            break;
		    }
		}
		$template_path_serial_tabs =  "./includes/templates/records/records_elements_tabs.html";
		if(file_exists("./includes/templates/records/records_elements_tabs_subst.html")){
			$template_path_serial_tabs =  "./includes/templates/records/records_elements_tabs_subst.html";
		}
		if(file_exists($template_path_serial_tabs)){
			$h2o_serial_tabs = H2o_collection::get_instance($template_path_serial_tabs);
			$records_tabs = new records_tabs(new notice($serial_id));
			$records_list_ui = $records_tabs->get_record()->get_records_list_ui();
			if ($records_list_ui) {
			    $records_list_ui->set_current_url($pmb_url_base.'catalog.php?categ='.$categ.'&sub='.$sub.'&serial_id='.$serial_id.'&quoi='.$quoi.($view ? '&view='.$view : ''));
			}
			print $h2o_serial_tabs->render(array('records_tabs' => $records_tabs));
		}
	}
}

function compare_date($date_debut="", $date_fin="") {
	$restrict = '';
	if($date_debut && $date_fin) {
		if($date_fin<$date_debut) {
			$restrict = " and date_date between '".$date_fin."' and '".$date_debut."' ";
		} else if($date_fin == $date_debut) {
			$restrict = " and date_date='".$date_debut."' ";
		} else {
			$restrict = " and date_date between '".$date_debut."' and '".$date_fin."' ";
		}
	} else if($date_debut) {
		$restrict = " and date_date >='".$date_debut."' ";
	} else if($date_fin) {
		$restrict = " and date_date <='".$date_fin."' ";
	}
	return $restrict;
}

// affichage de la liste utilisateurs pour sélection
function list_serial($cb, $serial_list, $nav_bar) {
	global $serial_list_tmpl;
	$serial_list_tmpl = str_replace("!!cle!!", $cb, $serial_list_tmpl);
	$serial_list_tmpl = str_replace("!!list!!", $serial_list, $serial_list_tmpl);
	$serial_list_tmpl = str_replace("!!nav_bar!!", $nav_bar, $serial_list_tmpl);
	print pmb_bidi($serial_list_tmpl);
}
