<?
/**
* adada
*/
class banner extends engine{

	var $call_sql_class = array('banners', 'clients');
	var $banner_position = array('RIGHT', 'TOPOFFER');
	var $call_class = array('File', 'Functions', 'Image', 'Form');

	/**
	* adada
	*/
	function get_public_banner_data($position){

		$this->banners->banners_join_clients(
			"WHERE (NOW() BETWEEN DATE_ADD(banners.date_at, INTERVAL -1 DAY) AND DATE_ADD(banners.date_to, INTERVAL 1 DAY) OR never_expires = 1) AND banners.is_active = 1 AND banners.position = '".$position."' ".
			"ORDER BY RAND() "
		);
		return $this;
	}
	function get_dashboard_data(){

		$this->banners->Select("ORDER BY date_to DESC ");

		return $this;
	}

	/**
	* adada
	*/
	function get_view_stat_data(){

		$this->banners->Select("WHERE client_id = ".$_SESSION['UserData']['client_id']." ORDER BY date_to DESC ");

		return $this;
	}

	function get_manage_banner_data(){

		$this->banners->toDate(array('date_at', 'date_to'));
		$this->banners->banners_join_clients(
			"ORDER BY banners.date_to, banners.id DESC ",
			true
		);

		$this->clients->dataSeek();
		$this->clients->clients_join_users("WHERE p2.type = 'COMPANY' ORDER BY company ");
		return $this;
	}

	function get_ajax_images_data(){

		$this->banners->Select("WHERE id = ".$_GET['data_id'], true);

		return $this;
	}

	//updates
	function insert_banner(){

		$this->banners->setDefaults();
		$_POST['created_at'] = 'NOW()';
		if($_FILES['file']['size'] > 0)
			$_POST['image'] = $this->upload_image();

		$this->banners->Insert($_POST);
	}

	function update_banner(){

		$this->banners->setDefaults();
		unset($_POST['image']);
		if($_FILES['file']['size'] > 0)
			$_POST['image'] = $this->upload_image();

		$this->banners->Update($_POST, "WHERE id = ".$_GET['id']);

		die($this->Redirect($this->Link()));
	}

	function no_active(){

		$this->banners->Update(array('is_active' => '0'), "WHERE id = ".$_GET['id']);
	}

	function active(){

		$this->banners->Update(array('is_active' => '1'), "WHERE id = ".$_GET['id']);
	}

	function no_never_expires(){

		$this->banners->Update(array('never_expires' => '0'), "WHERE id = ".$_GET['id']);
	}

	function never_expires(){

		$this->banners->Update(array('never_expires' => '1'), "WHERE id = ".$_GET['id']);
	}

	function delete(){

		$this->banners->Delete("WHERE id = ".$_GET['id']);
	}

	function upload_image(){

		$fname = $this->Functions->RandomString(10);
		$fname = $this->File->UploadFile('file', _RootFolder.'/files/banners', $fname, array('png', 'gif', 'jpg', 'jpeg', 'swf', 'flv'), 0);

		return $fname;
		//$this->banners->Update(array('image' => $fname), "WHERE id = ".$_GET['data_id']);
	}

	function delete_image(){

		unlink(_RootFolder.'/files/banners/'.$_GET['file']);
		$this->banners->Update(array('image' => ''), "WHERE id = ".$_GET['data_id']);
	}

	function set_view_banner_data($id){

		$this->banners->Query("UPDATE LOW_PRIORITY banners SET viewed = viewed+1 WHERE id = ".$id);
	}
}
?>