<?php
// +-------------------------------------------------+
// © 2002-2004 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: entities_analysis_expl_controller.class.php,v 1.1.2.2 2026/05/13 07:25:59 dgoron Exp $

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

global $class_path;
require_once ($class_path."/entities/entities_analysis_controller.class.php");

class entities_analysis_expl_controller extends entities_analysis_controller {

    protected $cb;

	/**
	 * 8 = droits de modification
	 */
	protected function get_acces_m() {
		$acces_m = 1;
		$exemplaire = new exemplaire($this->cb, $this->id,$this->analysis_id);
		if ($exemplaire->explr_acces_autorise == "INVIS") {
		    $acces_m = 0;
		}
		if($acces_m == 0) {
			$this->error_message = 'mod_expl_error';
		}
		return $acces_m;
	}
	
	public function proceed_expl_form() {
	    global $msg;
	    global $pmb_droits_explr_localises, $explr_visible_mod;
	    global $option_num_auto;
	    
	    $action_link = $this->url_base."&action=expl_update&bul_id=".$this->bulletin_id."&analysis_id=".$this->analysis_id;
	    
	    $myAnalysis = new analysis($this->analysis_id, $this->bulletin_id);
	    print "<div class='row'><div class='perio-barre'>".$this->get_link_parent()."<h3>".$myAnalysis->tit1."</h3></div></div><br />";
	    
	    if ($this->id) {
    	    print "<div class='row'>";
    	    $exemplaire = new exemplaire($this->cb, $this->id,$this->analysis_id);
    	    
    	    print $exemplaire->expl_form($action_link."&expl_id=".$this->id, notice::get_permalink($this->analysis_id));
    	    print "</div>";
	        print "<div class='row notice-analysis expl-carts'>";
	        print caddie_controller::get_display_list_from_item('display', 'EXPL', $this->id);
	        print "</div>";
	    } else {
	        //création d'un exemplaire
	        // on checke si l'exemplaire n'existe pas déjà
	        $query = "SELECT count(1) FROM exemplaires WHERE expl_cb='".addslashes($this->cb)."'";
	        $result = pmb_mysql_query($query);
	        if((!pmb_mysql_result($result, 0, 0)) || (!empty($option_num_auto) && ($this->cb == ''))) {
	            // visibilité des exemplaires
	            // On ne vérifie que si l'utlisateur peut créer sur au moins une localisation :
	            if (!$pmb_droits_explr_localises||$explr_visible_mod) {
	                $exemplaire = new exemplaire($this->cb, 0, $this->analysis_id);
	                print "<div class='row'>";
	                print $exemplaire->expl_form($action_link, notice::get_permalink($this->analysis_id));
	                print "</div>";
	            }
	        } else {
	            error_message($msg[301], $msg[302], 1, $this->get_permalink());
	        }
	    }
	}
	
	public function proceed_expl_duplicate() {
	    print "<div class='row'>";
	    $exemplaire = new exemplaire($this->cb, $this->id,$this->analysis_id);
	    $exemplaire->cb="";
	    $exemplaire->expl_id=0;
	    $action_link = $this->url_base."&sub=analysis&action=expl_update&bul_id=".$this->bulletin_id."&analysis_id=".$this->analysis_id;
	    print $exemplaire->expl_form($action_link, notice::get_permalink($this->analysis_id));
	    print "</div>";
	}
	
	protected function is_expl_deletable() {
		global $msg;
		
		$query = "select 1 from pret where pret_idexpl='".$this->id."' ";
		$result = pmb_mysql_query($query);
		if (pmb_mysql_num_rows($result)) {
			// gestion erreur prêt en cours
			error_message($msg[416], $msg['impossible_expl_del_pret'], 1, $this->get_permalink());
			return false;
		} else {
			return true;
		}
	}
	
	public function proceed_expl_update() {
	    $exemplaire = new exemplaire($this->cb, $this->id,$this->analysis_id);
	    $exemplaire->set_properties_from_form();
	    $exemplaire->save();
	    
	    print $this->get_redirection_form();
	}
	
	public function proceed_expl_delete() {
		global $msg;
	
		if ($this->id) {
		    exemplaire::del_expl($this->id);
		}
		print "<div class='row'><div class='msg-perio'>".$msg['maj_encours']."</div></div>";
		print $this->get_redirection_form();
	}
	
	protected function get_permalink($id=0) {
		if(!$id) $id = $this->analysis_id;
		return analysis::get_permalink($id, $this->bulletin_id);
	}
	
	public function set_cb($cb) {
		$this->cb = $cb;
	}

	public function set_analysis_id($analysis_id=0) {
	    $this->analysis_id = (int) $analysis_id;
	}
}
