<?php
// +-------------------------------------------------+
// © 2002-2014 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: nomenclature_record_child.class.php,v 1.9 2015-04-10 09:26:25 dgoron Exp $

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


/**
 * class nomenclature_record_child
 */

class nomenclature_record_child {

	/** Aggregations: */

	/** Compositions: */

	 /*** Attributes: ***/
	
	protected $nature;
	protected $num_formation;
	protected $formation;
	protected $num_type;
	protected $type;
	protected $num_musicstand;		
	protected $musicstand;		
	protected $num_instrument;
	protected $instrument;			
	protected $effective;		
	protected $order;			
	protected $other;		
	protected $num_voice;	
	protected $voice;		
	protected $num_workshop;	
	protected $workshop;	
		
	/**
	 * Constructeur
	 *
	 * @param int id de la notice
	 
	 * @return void
	 * @access public
	 */
	public function __construct($id=0) {
		$this->id=$id*1;
		$this->fetch_datas();
	} // end of member function __construct
	
	protected function fetch_datas(){
		global $dbh;
		global $pmb_nomenclature_record_children_link;
		
		$this->nature=0;
		$this->set_num_formation(0);
		$this->set_num_type(0);
		$this->set_num_musicstand(0);
		$this->set_num_instrument(0);
		$this->set_effective(0);
		$this->set_order(0);
		$this->set_other("");
		$this->set_num_voice(0);
		$this->set_num_workshop(0);
		if($this->id){
			$query = "select * from nomenclature_children_records where child_record_num_record = ".$this->id;
			$result = pmb_mysql_query($query,$dbh);
			if(pmb_mysql_num_rows($result)){
				if($row = pmb_mysql_fetch_object($result)){
					$this->set_num_formation($row->child_record_num_formation);			
					$this->set_num_type($row->child_record_num_type);							
					$this->set_num_musicstand($row->child_record_num_musicstand);					
					$this->set_num_instrument($row->child_record_num_instrument);
					$this->set_effective($row->child_record_effective);
					$this->set_order($row->child_record_order);
					$this->set_other($row->child_record_other);
					$this->set_num_voice($row->child_record_num_voice);
					$this->set_num_workshop($row->child_record_num_workshop);
				}		
				$query="select notice_nomenclature_label, linked_notice from notices_relations, nomenclature_notices_nomenclatures where 
				linked_notice= notice_nomenclature_num_notice	and num_notice='".$this->id."' and notice_nomenclature_num_formation=".$row->child_record_num_formation." 				
				and relation_type='".$pmb_nomenclature_record_children_link."'";				
				$result_parent = pmb_mysql_query($query,$dbh);
				if(pmb_mysql_num_rows($result_parent)){
					if($row_parent = pmb_mysql_fetch_object($result_parent)){
						$this->formation_label=$row_parent->notice_nomenclature_label;
						$this->id_parent=$row_parent->linked_notice;
					}
				}				
			}
		}
	}
	
	public function get_data(){
		return(
			array(
				"nature"=>$this->nature,
				"num_formation"=>$this->get_num_formation(),
				"formation_name"=>$this->formation->get_name(),
				"formation_label"=>$this->formation_label,	
				"id_parent"=>$this->id_parent,					
				"num_type"=>$this->get_num_type(),
				"type_name"=>$this->type->get_name(),
				"num_musicstand"=>$this->get_num_musicstand(),
				"musicstand_name"=>$this->musicstand->get_name(),
				"num_instrument"=>$this->get_num_instrument(),
				"instrument_name"=>$this->instrument->get_name(),
				"effective"=>$this->get_effective(),
				"order"=>$this->get_order(),
				"other"=>$this->get_other(),
				"num_voice"=>$this->get_num_voice(),
				"voice_name"=>$this->voice->get_name(),
				"num_workshop"=>$this->get_num_workshop(),
				"workshop_name"=>$this->workshop->get_name(),
			)
		);		
	}	

	public function get_num_formation(){
		return $this->num_formation;		
	}	
	
	public function get_num_type(){
		return $this->num_type;
	}
	
	public function get_num_musicstand(){
		return $this->num_musicstand;		
	}	
	
	public function get_num_instrument(){
		return $this->num_instrument;		
	}	
	
	public function get_effective(){
		return $this->effective;
	}
	
	public function get_order(){
		return $this->order;
	}
	
	public function get_other(){
		return $this->other;
	}
	
	public function get_num_voice(){
		return $this->num_voice;
	}
	
	public function get_num_workshop(){
		return $this->num_workshop;
	}
	
	public function get_id(){
		return $this->id;
	}	
	
	public function set_num_formation($num_formation){
		$this->num_formation=$num_formation;
		$this->formation = new nomenclature_formation($num_formation);
	}
	
	public function set_num_type($num_type){
		$this->num_type=$num_type;
		$this->type = new nomenclature_type($num_type);
	}
	
	public function set_num_musicstand($num_musicstand){
		$this->num_musicstand=$num_musicstand;
		$this->musicstand = new nomenclature_musicstand($num_musicstand);
	}
	
	public function set_num_instrument($num_instrument){
		$this->num_instrument=$num_instrument;
		$this->instrument = new nomenclature_instrument($num_instrument);
	}
	
	public function set_effective($effective){
		$this->effective=$effective;
	}
	
	public function set_order($order){
		$this->order=$order;
	}
	
	public function set_other($other){
		$this->other=$other;
	}
	
	public function set_num_voice($num_voice){
		$this->num_voice=$num_voice;
		if($num_voice)$this->nature=1; else $this->nature=0;
		$this->voice = new nomenclature_voice($num_voice);
	}
	
	public function set_num_workshop($num_workshop){
		$this->num_workshop=$num_workshop;
		$this->workshop = new nomenclature_workshop($num_workshop);
	}
	
	public function save_form(){
		if(!$this->id)return; 
		$this->delete();
		global $nomenclature_record_partial_formation;
		global $nomenclature_record_partial_musicstand;
		global $nomenclature_record_partial_workshop;
		global $nomenclature_record_partial_num_instrument;
		global $nomenclature_record_partial_other_instruments;
		global $nomenclature_record_partial_num_voice;
		global $nomenclature_record_partial_effectve;
		global $nomenclature_record_partial_order;
		
		$formation_detail = explode("_",$nomenclature_record_partial_formation);
		$data["num_formation"]	= $formation_detail[0]*1;
		$data["num_type"]		= $formation_detail[1]*1;
		$data["num_musicstand"]	= $nomenclature_record_partial_musicstand*1;
		$data["num_instrument"]	= $nomenclature_record_partial_num_instrument*1;
		$data["effective"]		= $nomenclature_record_partial_order*1;
		$data["order"]			= $nomenclature_record_partial_effectve*1;
		$data["num_voice"]		= $nomenclature_record_partial_num_voice*1;
		$data["num_workshop"]	= $nomenclature_record_partial_workshop*1;		
		$data["other"]			= stripslashes($nomenclature_record_partial_other_instruments);
		return $this->save($data);
	}
	
	public function save($data){
		global $dbh;
		
		if(!$this->id)return;
		$fields="
		child_record_num_record='".$this->id."',
		child_record_num_formation='".$data["num_formation"]."',
		child_record_num_type='".$data["num_type"]."',
		child_record_num_musicstand='".$data["num_musicstand"]."',
		child_record_num_instrument='".$data["num_instrument"]."',
		child_record_effective='".$data["effective"]."',
		child_record_order='".$data["order"]."',
		child_record_other='".addslashes($data["other"])."',
		child_record_num_voice='".$data["num_voice"]."',
		child_record_num_workshop='".$data["num_workshop"]."'
		";
		
		$req="INSERT INTO nomenclature_children_records SET $fields ";
		pmb_mysql_query($req, $dbh);
		$this->fetch_datas();
		return $this->id;
	}
	
	public function delete(){	
		global $dbh;
		
		if(!$this->id)return;
		$req="DELETE from nomenclature_children_records WHERE child_record_num_record = ".$this->id;
		pmb_mysql_query($req, $dbh);
		
		$this->fetch_datas();
	}
	
	// retrouve la notice fille liée au données d'un instrument...
	public function get_child($id_parent,$data){
		global $dbh;
		global $pmb_nomenclature_record_children_link;
		
		$query="select * from notices_relations where linked_notice='".$id_parent."' and relation_type='".$pmb_nomenclature_record_children_link."'";
		$result = pmb_mysql_query($query,$dbh);
		if(pmb_mysql_num_rows($result)){
			while($row = pmb_mysql_fetch_object($result)){
				$child_id=$row->num_notice;
				$child=new nomenclature_record_child($child_id);
				$child_data=$child->get_data();
				
				if($child_data["num_formation"]!=$data["num_formation"])continue;
				if($child_data["num_musicstand"]!=$data["num_musicstand"])continue;
				if($child_data["num_instrument"]!=$data["num_instrument"])continue;
				if($child_data["num_voice"]!=$data["num_voice"])continue;
				if($child_data["other"]!=$data["other"])continue;
				if($child_data["effective"]!=$data["effective"])continue;
				if($child_data["order"]!=$data["order"])continue;
				return $child_id;
			}
		}
		return 0;	
	}
	
	public function create_record_child($id_parent,$data){
		global $dbh;
		global $pmb_nomenclature_record_children_link;
	
		$tit1="Notice fille temporaire de la nomenclature $id_parent";
		
		$fields="
		tit1='".addslashes($tit1)."'
		";
	
		$req="INSERT INTO notices SET $fields ";
		pmb_mysql_query($req, $dbh);
		$this->id=pmb_mysql_insert_id();
		if(!$this->id) return 0;
		if(!$data["rank"]) $data["rank"]=1;
		$requete='INSERT INTO notices_relations VALUES("'.$this->id.'","'.$id_parent.'","'.$pmb_nomenclature_record_children_link.'","'.$data["rank"].'")';
		pmb_mysql_query($requete,$dbh);
	
		$this->save($data);
		
		$this->fetch_datas();
		$data=$this->get_data();
		
		$tit1=$data["instrument_name"].$data["voice_name"];
		if($data["musicstand_name"])$tit1.=" / ".$data["musicstand_name"];
		if($data["formation_label"])$tit1.=" / ".$data["formation_label"];
		
		$fields="
		tit1='".addslashes($tit1)."'
		";
		
		$req="UPDATE notices SET $fields where notice_id= ".$this->id;
		pmb_mysql_query($req, $dbh);
		
		// permet de charger la bonne langue, mot vide...
		$info=notice::indexation_prepare($this->id);
		// Mise a jour des index de la notice
		notice::majNotices($this->id);
		// Mise a jour de la table notices_global_index
		notice::majNoticesGlobalIndex($this->id);
		// Mise a jour de la table notices_mots_global_index
		notice::majNoticesMotsGlobalIndex($this->id);
		// restaure l'environnement de langue
		notice::indexation_restaure($info);
		return array('id'=>$this->id, 'title'=> $tit1);
	}
		
	public function get_index() {
		$mots = " ".$this->formation->get_name();
		$mots .= " ".$this->type->get_name();
		$mots .= " ".$this->musicstand->get_name();
		$mots .= " ".$this->instrument->get_name();
		$mots .= " ".$this->get_effective();
		$mots .= " ".$this->voice->get_name();
		$mots .= " ".$this->workshop->get_name();
		$mots .= " ";
		return $mots;
	}
	
	public function get_possible_values($num_parent){
		global $dbh,$msg;
		$possible_values = array();
		$num_parent+=0;
		
		//formations
		$query = "select 
				nomenclature_notices_nomenclatures.notice_nomenclature_num_formation,
				nomenclature_notices_nomenclatures.notice_nomenclature_num_type,
				nomenclature_notices_nomenclatures.notice_nomenclature_label,
				nomenclature_formations.formation_name,
				nomenclature_types.type_name,
				nomenclature_formations.formation_nature
		from nomenclature_notices_nomenclatures 
		join nomenclature_formations on nomenclature_formations.id_formation = nomenclature_notices_nomenclatures.notice_nomenclature_num_formation
		left join nomenclature_types on nomenclature_types.id_type = nomenclature_notices_nomenclatures.notice_nomenclature_num_type
		where notice_nomenclature_num_notice = ".$num_parent." order by notice_nomenclature_order, notice_nomenclature_label";
		$result = pmb_mysql_query($query,$dbh);
		if(pmb_mysql_num_rows($result)){
			while($row = pmb_mysql_fetch_object($result)){
				$possible_values['formations'][$row->notice_nomenclature_num_formation."_".$row->notice_nomenclature_num_type] = $row->formation_name.($row->type_name ? " / ".$row->type_name : "").($row->notice_nomenclature_label ? " - ".$row->notice_nomenclature_label : "");
			}
		}
		//pupitres..
		$query = "select nomenclature_musicstands.id_musicstand, nomenclature_musicstands.musicstand_name from nomenclature_musicstands join nomenclature_families on musicstand_famille_num = id_family order by family_order,musicstand_order";
		$result = pmb_mysql_query($query,$dbh);
		if(pmb_mysql_num_rows($result)){
			while($row = pmb_mysql_fetch_object($result)){
				$possible_values['musicstands'][$row->id_musicstand] = $row->musicstand_name;
			}
		}
		//workshops
		$query = "select nomenclature_workshops.id_workshop, nomenclature_workshops.workshop_label, nomenclature_workshops.workshop_order from nomenclature_workshops join nomenclature_notices_nomenclatures on nomenclature_workshops.workshop_num_nomenclature= nomenclature_notices_nomenclatures.id_notice_nomenclature where nomenclature_notices_nomenclatures.notice_nomenclature_num_notice = ".$num_parent;
		$result = pmb_mysql_query($query,$dbh);
		if(pmb_mysql_num_rows($result)){
			while($row = pmb_mysql_fetch_object($result)){
				$possible_values['workshops'][$row->id_workshop] = ($row->workshop_label ? $row->workshop_label : $msg['nomenclature_isbd_child_workshop']." ".$row->workshop_order) ;
			}
		}
		return $possible_values;
	} 
} // end of nomenclature_record_child

