<?php
// +-------------------------------------------------+
// | 2002-2007 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: abts_grille_abt.class.php,v 1.1.2.2 2025/10/22 09:01:23 dgoron Exp $

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

class abts_grille_abt {
    
    protected $num_abt;
    
    public function __construct($num_abt) {
        $this->num_abt = intval($num_abt);
    }

    public function gen_serie($id,$nom,$nombre) {
        return "
		<div class='row'>
    		<input type='text' size='4' name='serie[$id]' id='$id' value='$nombre'/>
    		$nom
		</div>";
    }
    
    public function gen_hors_serie($id,$modele_name,$checked,$numero) {
        global $msg;
        
        return "
		<div class='row'>
            <input type='checkbox' value='2' $checked name='check_hors_serie[$id]'/>$modele_name
		</div>
		<div class='row'>
            <label for='numero'>".$msg["abonnements_attribuer_un_numero"]."</label>
		</div>
		<div class='row'>
            <input type='text' size='15' name='numero[$id]' id='numero' value='$numero'/>
		</div>";
    }
    
    public function get_form($date_parution) {
        global $current_module, $msg;
        
        $series="";
        $hors_series="";
        $requete = "SELECT  distinct (modele_name) ,abts_grille_abt.modele_id from abts_modeles, abts_grille_abt where num_abt='".$this->num_abt."' and abts_grille_abt.modele_id = abts_modeles.modele_id ";
        $resultat=pmb_mysql_query($requete);
        if(pmb_mysql_num_rows($resultat)) {
            while($r=pmb_mysql_fetch_object($resultat)){
                $modele_name=$r->modele_name;
                $modele_id=$r->modele_id;
                $nombre=0;
                $requete = "SELECT COUNT(*) as nombre from abts_grille_abt where num_abt='".$this->num_abt."' and date_parution ='".addslashes($date_parution)."' and modele_id = '$modele_id' and type='1' ";
                $resultat_nb=pmb_mysql_query($requete);
                if($r_nb=pmb_mysql_fetch_object($resultat_nb)){
                    $nombre=$r_nb->nombre;
                }
                $series .= $this->gen_serie($modele_id, $modele_name, $nombre);
                $checked="";
                $numero="";
                $requete = "SELECT numero from abts_grille_abt where num_abt='".$this->num_abt."' and date_parution ='".addslashes($date_parution)."' and modele_id = '$modele_id' and type='2' ";
                $resultat_nb=pmb_mysql_query($requete);
                if($r_nb=pmb_mysql_fetch_object($resultat_nb)){
                    $numero=$r_nb->numero;
                    $checked= "checked";
                }
                $hors_series .= $this->gen_hors_serie($modele_id, $modele_name, $checked, $numero);
            }
        }
        
        return "
        <form class='form-$current_module' id='form_modele' name='form_modele' method='post' action='./abonnement_parution_edition.php?abonnement_id=".$this->num_abt."&date_parution=$date_parution'>
        	<div class='row center'>".formatdate($date_parution)."</div>
        	<div class='row'>
        	   ".$msg["abonnements_edition_serie"]." :
        	</div>
        	".$series."
        	<hr />
    	   ".$msg["abonnements_hors_serie"]." :
    	   ".$hors_series."
    	   &nbsp;
        	<div class='row'>
        		<input type='hidden' id='act' name='act' value='' />
        		<div class='left'>
        			<input class='bouton_small' value='".$msg["77"]."' onclick=\"document.getElementById('act').value='update';this.form.submit();\" type='submit'>
        		</div>
        	</div>
    	</form>";
        
    }
    
    public function save_date_parution($date_parution) {
        global $serie, $check_hors_serie;
        global $numero;
        
        $query = "DELETE FROM abts_grille_abt WHERE num_abt='".$this->num_abt."' and date_parution ='".addslashes($date_parution)."'";
        pmb_mysql_query($query);
        $query = "SELECT  distinct (modele_name) ,abts_grille_abt.modele_id from abts_modeles, abts_grille_abt where num_abt='".$this->num_abt."' and abts_grille_abt.modele_id = abts_modeles.modele_id ";
        $resultat=pmb_mysql_query($query);
        while($r=pmb_mysql_fetch_object($resultat)){
            $modele_id=$r->modele_id;
            $nombre= $serie[$modele_id];
            /*			if($nombre){
             $requete = "INSERT INTO abts_grille_abt SET num_abt='$abonnement_id', date_parution ='$date_parution', modele_id = '$modele_id', type = '1', nombre='$nombre'";
             pmb_mysql_query($requete);
             $type_serie=1;
             }*/
            for($i=1;$i<=$nombre;$i++){
                $query = "INSERT INTO abts_grille_abt SET num_abt='".$this->num_abt."',
							date_parution ='".addslashes($date_parution)."',
							modele_id='".$modele_id."',
							type = '1',
							nombre='1',
							ordre='$i' ";
                pmb_mysql_query($query);
                $type_serie=1;
            }
            
            if (isset($check_hors_serie[$modele_id])){
                $numero= $numero[$modele_id];
                $query = "INSERT INTO abts_grille_abt SET num_abt='".$this->num_abt."', date_parution ='".addslashes($date_parution)."', modele_id = '$modele_id', type = '2', numero='".addslashes($numero)."', nombre='1', ordre='1' ";
                pmb_mysql_query($query);
                $type_horsserie=2;
            }
        }
        return $type_horsserie+$type_serie;
    }
}
