<?php
class ControllerAffiliateBanners extends Controller {
	private $error = array();

	public function index() {
		if (!$this->affiliate->isLogged()) {
      		$this->redirect(HTTP_SERVER . 'index.php?route=affiliate/login');
    	}
		$this->load->model('affiliate/affiliate');
		if(isset($this->request->get['add']) and $this->request->get['add']='yes')
		{
			$this->data['previous_url'] = $_SERVER['HTTP_REFERER'];
			$this->model_affiliate_affiliate->modifyBanner($this->affiliate->getId(),$_SERVER['HTTP_REFERER']);
			$this->redirect($this->data['previous_url']);
		}


		$this->load->model('affiliate/affiliate');

		$this->data['imagebanner'] = array();
		
		$results = $this->model_affiliate_affiliate->getImagebanner();

    	foreach ($results as $result) {

      		$this->data['imagebanner'][] = array(
					'image'  => $result['image']
      		);
    	}
		$this->data['flashbanner'] = array();
		
		$results = $this->model_affiliate_affiliate->getFlashbanner();

    	foreach ($results as $result) {

      		$this->data['flashbanner'][] = array(
					'image'  => $result['image']
      		);
    	}
		$this->data['textbanner'] = array();
		
		$results = $this->model_affiliate_affiliate->getTextbanner();

    	foreach ($results as $result) {

      		$this->data['textbanner'][] = array(
					'title'  => $result['title']
      		);
    	}

		if(isset($this->request->get['add']) and $this->request->get['add']='yes')
		{
			$this->data['previous_url'] = $_SERVER['HTTP_REFERER'];
			$this->model_affiliate_affiliate->modifyBanner($this->affiliate->getId(),$_SERVER['HTTP_REFERER']);
			$this->redirect($this->data['previous_url']);
		}


        $this->data['affiliate_id']=$this->affiliate->isLogged();
    	$this->language->load('affiliate/banners');

    	$this->data['heading_title'] = $this->language->get('heading_title');
    	$this->data['entry_choosebanner'] = $this->language->get('entry_choosebanner');
    	$this->data['entry_activebanner'] = $this->language->get('entry_activebanner');
    	$this->data['entry_flashbanner'] = $this->language->get('entry_flashbanner');
    	$this->data['entry_bannerimage'] = $this->language->get('entry_bannerimage');
    	$this->data['entry_htmlbanner'] = $this->language->get('entry_htmlbanner');
    	$this->data['entry_textlink'] = $this->language->get('entry_textlink');
	
	if (isset($this->request->post['next'])) {
				$this->data['next'] = $this->request->post['next'];
			} else {
				$this->data['next'] = '';
			}
			

		$this->data['config_bannerimgurl'] = $this->config->get('config_bannerimgurl');
		if(!isset($this->data['config_bannerimgurl']))
		{
			$this->data['config_bannerimgurl']=$this->config->get('config_logo');
		}
		$this->data['config_fullbannerimgurl']=$this->config->get('config_url').'image/'.$this->data['config_bannerimgurl'];


		$this->data['config_bannertext'] = $this->config->get('config_bannertext');

		$this->data['config_htmlbannertext']=html_entity_decode($this->data['config_bannertext'], ENT_QUOTES, 'UTF-8');
		
		$this->data['button_continue'] = $this->language->get('button_continue');
		$this->data['continue'] = $this->url->link('affiliate/account', '', 'SSL');


		$this->data['config_bannerflashurl']=$this->config->get('config_bannerflashurl');
		if (!isset( $this->data['config_bannerflashurl'] ))
		{
			$this->data['config_fullbannerflashurl']=$this->config->get('config_url').'image/'.$this->data['config_bannerflashurl'];
		}else{
			$this->data['config_fullbannerflashurl']=$this->data['config_fullbannerimgurl'];
		}
	//	if ($this->affiliate->affiliate_grp() != 1 )exit;
		$this->data['my_home']=$this->config->get('config_url').'index.php?route=common/home&tracking='.$this->affiliate->getCode();
		
		$this->data['home']=$this->config->get('config_url').'image/';

		$this->data['mybanner'] = $this->model_affiliate_affiliate->getBannerURL($this->affiliate->getId());
		
		

		$this->id       = 'content';

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/affiliate/banners.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/affiliate/banners.tpl';
		} else {
			$this->template = 'default/template/affiliate/banners.tpl';
		}

		$this->children = array(
			'common/header',
			'common/footer',
			'common/column_left',
			'common/column_right'
		);

		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));

		}

  	private function validate() {
    	if (!$this->customer->login($this->request->post['email'], $this->request->post['password'])) {
      		$this->error['message'] = $this->language->get('error_login');
    	}

    	if (!$this->error) {
      		return TRUE;
    	} else {
      		return FALSE;
    	}
  	}
}
?>