<?
////////////////////////////////////////////////////////////////////////////
//
//	Business & Control 2004-2008.
//
//	osztály neve: class_poll.php
//  célja: szavazás
//	osztályváltozók:  
//
//	wait					=> várakozási idő
//  bar_width            	=> doboz szélessége
//
////////////////////////////////////////////////////////////////////////////

class poll{
	
    /**
     * Construct
     *
     */
	function poll(){
		$this->wait=3600;
		$this->bar_width=135;
	}
	
	/**
	 * Reading polls from database
	 * 
     * @return array|false
     * @todo felĂĽlvizsgĂˇlni ĂĽres adatra 
	 */
	function getPolls() {
	
		global $dbi;
	
		$sql="select id,text,active,date from poll order by date desc";
		$res=sql_query($sql,$dbi);
		while(list($id,$text,$active,$date)=sql_fetch_array($res,$dbi)){
			$data[]=array(id=>$id,text=>$text,active=>$active,date=>$date);
		}
		if (isset($data))
		{
			return $data;
		} else
		{
			return false;
		}
	}

	/**
	 * Get poll id from database
	 * 
	 * @return integer|false
	 * @todo felĂĽlvizsgĂˇlni a visszatĂ©rĂ©si Ă©rtĂ©ket
	 */
	function getFrontPoll() {
		
		global $dbi;
		
		$sql="select id from poll where active";
		$res=sql_query($sql,$dbi);
		list($id)=sql_fetch_array($res,$dbi);
		if (isset($id))
		{
			return $id;
		} else
		{
			return false;
		}
	}	
	
	/**
	 * Get all poll data from databe
	 * 
	 * @param integer|false $id id of poll
	 * @param boolean $order ordering condicion
	 * @return array|false
	 */
	function getPollData($id,$order=false) {
	
		global $dbi;

		if (is_numeric($id))
		{
			$sql="select max(time) from poll_vote where poll=".$id." and ip='$_SERVER[REMOTE_ADDR]'";
			$res=sql_query($sql,$dbi);
			list($time)=sql_fetch_array($res,$dbi);
			if(time()<$time+$this->wait) $wait=true;
		
			$sql="select id,text,date from poll where id=".$id;
			$res=sql_query($sql,$dbi);
			list($id,$title)=sql_fetch_array($res,$dbi);
		
			$sql="select i.item,text,count(v.item) as c from poll_item i ".
				"left outer join poll_vote v on (i.item=v.item and i.poll=v.poll) ".
				"where i.poll=$id group by i.item";
			if($order) $sql.=" order by c desc";
			$res=sql_query($sql,$dbi);
		
			$max=1;
			while(list($item,$text,$votes)=sql_fetch_array($res,$dbi)){
				$items[]=array(id=>$item,text=>$text,votes=>$votes);
				if($votes>$max) $max=$votes;
			}
		
			$poll=array(id=>$id,title=>$title,items=>$items,max=>$max,wait=>$wait);

			return $poll;
		} else
		{
			return false;
		}
	}

	/**
	 * save poll data
	 * 
	 * @param array $post new post data
	 * @return boolean
	 * @todo tranzakciĂł beĂ©pitĂ©se
	 */
	function saveNewPoll($post) {
		if (isset($post))
		{
			global $dbi;
			
			$sql="insert into poll (text,date) values ('$post[title]','".date("Y-m-d")."')";
			sql_query($sql,$dbi);
			
			$sql="select max(id) from poll";
			$res=sql_query($sql,$dbi);
			list($id)=sql_fetch_array($res,$dbi);
			if (!is_numeric($id))
			{
				$id = 1;
			}
			$item=0;
			
			if (is_array($post['item']))
			{
				foreach($post[item] as $v) {
					if($v){
						$sql="insert into poll_item (poll,item,text) values ($id,$item,'$v')";
						sql_query($sql,$dbi);
						$item++;
					}
				}
			} else
			{
				$sql="insert into poll_item (poll,item,text) values ($id,$item,'".$post['item']."')";
				sql_query($sql,$dbi);
			}
			return true;
		} else
		{
			return false;
		}
	}

	/**
	 * Update poll data
	 *
	 * @param array $post new post data
	 * @return boolean
	 */
	function updatePoll($post) {
	
		global $dbi;
		
		if (is_numeric($post['id']))
		{	
			$sql="update poll set text='$post[title]' where id=".$post['id'];
			sql_query($sql,$dbi);
		
			$sql="delete from poll_item where poll=".$post['id'];
			sql_query($sql,$dbi);
		
			$item=0;
			if (is_array($post[item]))
			{
				foreach($post[item] as $v) {
					if($v){
						$sql="insert into poll_item (poll,item,text) values (".$post['id'].",$item,'$v')";
						sql_query($sql,$dbi);
						$item++;
					}
				}
			} else 
			{
				$sql="insert into poll_item (poll,item,text) values (".$post['id'].",$item,'".$post[item]."')";
				sql_query($sql,$dbi);
			}
			return true;
		} else 
		{
			return false;
		}
	}

	/**
	 * Delete poll
	 *
	 * @param integer $id
	 * @return boolean
	 */
	function deletePoll($id) {
		if (is_numeric($id))
		{
			global $dbi;
			$sql="delete from poll where id=$id";
			sql_query($sql,$dbi);
			$sql="delete from poll_item where poll=$id";
			sql_query($sql,$dbi);
			$sql="delete from poll_vote where poll=$id";
			sql_query($sql,$dbi);
			return true;
		} else 
		{
			return false;
		}
	}

	/**
	 * Save poll vote
	 *
	 * @param integer $poll
	 * @param integer $item
	 * @return boolean
	 */
	function saveVote($poll,$item) {
		if (is_numeric($item) and is_numeric($poll))
		{
			global $dbi;
			$sql="insert into poll_vote (poll,item,ip,time) values ($poll,$item,'$_SERVER[REMOTE_ADDR]',".time().")";
			sql_query($sql,$dbi);
			return true;
		} else 
		{
			return false;
		}
	}
}
?>