<?php
class notiz
{
	var $id;
	var $usr; // user created notice
	var $dat; // timestamp created
	var $capt; // title
	var $note; // notice
	var $read_status;
	var $date_termin;

	function notiz($id){
		$this->id = $id;
		$this->unset_termin();
	}

	function unset_termin(){
		$query_raw = sprintf("UPDATE Notice_status SET date_termin=0 WHERE usr='%s' and notes_id='%s' and date_termin<now() and date_termin>0",
			$_SESSION['customer_id'],
			$this->id
		);
		xtc_db_query($query_raw);
	}

	function get_data(){
		$notice_query_raw = sprintf("select n.capt,n.usr,n.dat,s.read_status,s.date_termin from Notice_notes n,Notice_status s where n.id='%s' and s.usr='%s' and s.notes_id=n.id",
			$this->id,
			$_SESSION['customer_id']
		);
		$notice_query = xtc_db_query($notice_query_raw);
		$notice = xtc_db_fetch_array($notice_query);

		$this->usr = $notice['usr'];
		$this->dat = $notice['dat'];
		$this->capt = xtc_db_prepare_input($notice['capt']);
		$this->read_status = $notice['read_status'];
		$this->date_termin = $notice['date_termin'];
	}

	function set_read_status($status=1){
		$update_query_raw = sprintf("UPDATE Notice_status SET read_status=1 WHERE usr='%s' and notes_id='%s'",$_SESSION['customer_id'],$this->id);
		xtc_db_query($update_query_raw);
	}

	function save_forward($cID){
		$check_query = xtc_db_query(sprintf("SELECT usr FROM Notice_status WHERE usr='%s' and notes_id='%s'",$cID,$this->id));
		if(!xtc_db_num_rows($check_query)>0){
			xtc_db_query(sprintf("INSERT INTO Notice_status SET notes_id='%s',usr='%s'",$this->id,$cID));
		}
	}

	function move_to_folder($folder_id){
		$notice_update_query_raw = sprintf("UPDATE Notice_status SET Notice_folders_id='%s' WHERE notes_id='%s' and usr='%s'",
			$folder_id,
			$this->id,
			$_SESSION['customer_id']
		);
		xtc_db_query($notice_update_query_raw);
	}

	function delete(){
		$delete_sql_raw = sprintf("DELETE FROM Notice_status WHERE notes_id='%s' and usr='%s'",
			$this->id,
			$_SESSION['customer_id']
		);
		xtc_db_query($delete_sql_raw);

		// FALLS KEIN ANDERER DIESE NOTIZ HAT -> loeschen!
		$another_query = xtc_db_query(sprintf("SELECT notes_id FROM Notice_status WHERE notes_id='%s'",$this->id));
		if( xtc_db_num_rows($another_query) < 1){
			xtc_db_query(sprintf("DELETE FROM Notice_status WHERE notes_id='%s'",$this->id));
		}
	}

	function get_note(){
		$query_raw = sprintf("select n.note from Notice_notes n,Notice_status s where n.id='%s' and s.usr='%s' and s.notes_id=n.id",
			$this->id,
			$_SESSION['customer_id']
		);
		$query = xtc_db_query($query_raw);
		$row = xtc_db_fetch_array($query);
		return xtc_db_prepare_input($row['note']);
	}

	function get_recipients(){
		$array = array();
		$query_raw = sprintf("SELECT usr FROM Notice_status WHERE notes_id='%s' and usr!='%s' and usr!='0'",
			$this->id,
			$_SESSION['customer_id']
		);
		$query = xtc_db_query($query_raw);
		while($row = xtc_db_fetch_array($query) ){
			if($this->usr != $row['usr']){
				$name = xtc_get_editor_name($row['usr']);
				if(xtc_not_null($name))
					$array[] = $name;
			}
		}
		if( sizeOf($array) < 1 ){
			$array[] = '<i>Keine weiteren Empf&auml;nger</i>';
		}
		return $array;
	}

	function get_forward_recipients(){
		$array = array();
		$excl = array(0=>$_SESSION['customer_id'],1=>0);
		$nid_query = xtc_db_query(sprintf("SELECT n.usr FROM Notice_status n WHERE n.notes_id='%s'",$this->id));
		while($nid = xtc_db_fetch_array($nid_query)){
			$excl[] = $nid['usr'];
		}
		
		$excl_strg = '';
		for($i=0;$i<sizeOf($excl);$i++){
			$excl_strg .= " c.customers_id != '".$excl[$i]."' and ";
		}
		$excl_strg = substr($excl_strg,0,(strlen($excl_strg)-4));
		$com = sprintf("SELECT c.customers_firstname,c.customers_lastname,c.customers_id FROM customers c WHERE customers_status=0 and (%s)",$excl_strg);
		$query = xtc_db_query($com);
		while($res = xtc_db_fetch_array($query)){
			$array[] = $res;
		}
		return $array;
	}

	function draw_recipients($default){
		$com='SELECT customers_firstname,customers_lastname,customers_id FROM customers WHERE customers_status=0';
		$query = xtc_db_query($com);
		$counter = 1;
		$array = array();
		$string = '';
		while($row=xtc_db_fetch_array($query)){
			if($_SESSION['customer_id'] != $row['customers_id']){
				$txt = $row['customers_firstname'] . ' ' . $row['customers_lastname'];
				$array[$counter] = array('id'=>$row['customers_id'],'text'=>xtc_db_prepare_input($txt));
				$counter++;
			}else{
				$array[0] = array('id'=>$row['customers_id'],'text'=>'Nur Eigene Notiz');
			}
		}
		ksort($array);
		
		foreach($array as $k=>$v){
			$string .= '<input type="checkbox" value="'.$v['id'].'" name="rec[]" id="rec[]"';
			if($default == $v['id']){
				$string .= ' CHECKED';
			}
			$string .= '>'.$v['text'].'<br>';
		}
		return $string;
	}

	function save_new($capt,$note,$usr,$termin,$rec_array){
		if(!xtc_not_null($capt))
			return false;
		if(!xtc_not_null($note))
			return false;
		if(!is_array($rec_array) or sizeOf($rec_array)==0)
			return false;

		$insert_sql_array = array(
			'dat'=>time(),
			'capt'=>$capt,
			'note'=>$note,
			'usr'=>$usr
		);

		xtc_db_perform('Notice_notes',$insert_sql_array);
		$lid=xtc_db_insert_id();
		

		for($i=0;$i<sizeOf($rec_array);$i++){
			$com='insert Notice_status (notes_id,usr,read_status,date_termin) values (\''.$lid.'\',\''.$rec_array[$i].'\',0,\''.$termin.'\');';
			xtc_db_query($com);
		}
		return true;
	}
}
