<?php
/*
copyright: copyright 2004 Sam Yapp. All Rights Reserved. You may modify this file for use on your
					site running a licenced copy of the Php Image Host scripts.
					You must not distribute this file or derivations of it.
Nullified By CrAcKlOvE

*/
class adrotator{

	var $ads = array();
	var $ad = 0;

	function adrotator(&$ace){
		$this->ace =& $ace;
	}

	function getads($group = '', $orderby = 'name', $orderdir = 'asc', $limit = 0, $live = -1){
		$sql = "SELECT * FROM {pa_dbprefix}ads ";
		$wheres = array();
		if( $group != '' ) $wheres[] = " groupname='".addslashes($group)."' ";
		if( $live != -1 ) $wheres[] = " live=$live ";
		if( count($wheres) ) $sql .= "WHERE ".join(" AND ", $wheres)." ";
		$sql .="ORDER BY $orderby $orderdir ";
		if( $limit > 0 )$sql .= "LIMIT 0, $limit ";
		$res = $this->ace->query($sql, 'Get Ads');
		$ads = array();
		while( $a = mysql_fetch_object($res) ) $ads[] = $a;
		return $ads;
	}

	function getad($id){
		$sql = "SELECT * FROM {pa_dbprefix}ads WHERE ad_id=$id ";
		$res = $this->ace->query($sql, 'Get Ad');
		return mysql_fetch_object($res);
	}

	function deleteads($ids){
		$ids = $this->ace->getids($ids);
		if( count($ids) == 0 )return 0;
		$sql = "DELETE FROM {pa_dbprefix}ads WHERE ad_id IN (".join(",",$ids).") ";
		$this->ace->query($sql, 'Delete Ads');
		return mysql_affected_rows();
	}

	function changestatus($ids, $live){
		$ids = $this->ace->getids($ids);
		if( count($ids) ){
			$sql = "UPDATE {pa_dbprefix}ads SET live=$live WHERE ad_id IN (".join(",",$ids).") ";
			$this->ace->query($sql, 'Change Status');
			return mysql_affected_rows();
		}
		return 0;
	}

	function preload($group = '',$n = 1){
		$this->ads = $this->getads($group,'views', 'asc', $n,1);
		$ids = array();
		foreach( $this->ads as $a ) $ids[] = $a->ad_id;
		if( count($ids) > 0 ){
			$sql = "UPDATE {pa_dbprefix}ads SET views=views+1 WHERE ad_id IN (".join(",",$ids).") ";
			$this->ace->query($sql, 'Log Views');
		}
		$this->ad = 0;
	}

	function display($return = false){
		if( count($this->ads) <= $this->ad ) $this->preload();
		if( count($this->ads) <= $this->ad ) return '';
		if( !$return ) echo $this->ads[$this->ad]->content;
		$this->ad++;
		return $this->ads[$this->ad-1]->content;
	}

	function resetviews($ids){
		$ids = $this->ace->getids($ids);
		if( count($ids) ){
			$sql = "UPDATE {pa_dbprefix}ads SET views=0 WHERE ad_id IN (".join(",",$ids).") ";
			$this->ace->query($sql, 'Reset Views');
			return mysql_affected_rows();
		}
		return 0;
	}

	function addad($name, $group, $content, $live = 0){
		$name = addslashes($name);
		$group = addslashes($group);
		$content = addslashes($content);
		$sql = "INSERT INTO {pa_dbprefix}ads (name, groupname, content, live) VALUES ('$name', '$group', '$content', $live) ";
		$res = $this->ace->query($sql, 'Insert Ad');
		return mysql_insert_id();
	}

	function updatead($id, $group, $content, $live){
		settype($live, 'integer');
		$group = addslashes($group);
		$content = addslashes($content);
		$sql = "UPDATE {pa_dbprefix}ads SET groupname='$group', content='$content', live=$live WHERE ad_id=$id ";
		$res = $this->ace->query($sql, 'Upate Ad');
	}

}

?>
