<?php

class ControllerModuleDealOfTheDay extends Controller 
{
	private $error = array(); 
	private $_name = 'dealoftheday';
	private $_version = '1.5.1.3.1';
	
	public function index() 
	{   
		$this->load->language('module/' . $this->_name);

		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('setting/setting');
			
		$this->data[$this->_name . '_version'] = $this->_version;
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) {
			$this->model_setting_setting->editSetting($this->_name, $this->request->post);		
					
			$this->session->data['success'] = $this->language->get('text_success');
						
			$this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
		}
				
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_content_top'] = $this->language->get('text_content_top');
		$this->data['text_content_bottom'] = $this->language->get('text_content_bottom');		
		$this->data['text_column_left'] = $this->language->get('text_column_left');
		$this->data['text_column_right'] = $this->language->get('text_column_right');
		$this->data['text_module_settings'] = $this->language->get('text_module_settings');
		$this->data['text_add_special_at'] = $this->language->get('text_add_special_at');
		$this->data['text_click_to_add'] = $this->language->get('text_click_to_add');
		$this->data['text_powered_by'] = $this->language->get('text_powered_by');
		
		$this->data['text_yes'] = $this->language->get('text_yes');
		$this->data['text_no'] = $this->language->get('text_no');
		
		$this->data['entry_header'] = $this->language->get( 'entry_header' ); 
		$this->data['entry_title'] = $this->language->get( 'entry_title' );
		$this->data['entry_icon'] = $this->language->get( 'entry_icon' );
		$this->data['entry_box'] = $this->language->get('entry_box');
		$this->data['entry_yes'] = $this->language->get( 'entry_yes' ); 
		$this->data['entry_no']	= $this->language->get( 'entry_no' );
		$this->data['entry_layout'] = $this->language->get('entry_layout');
		$this->data['entry_position'] = $this->language->get('entry_position');
		$this->data['entry_image'] = $this->language->get('entry_image');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
		$this->data['entry_deal_link'] = $this->language->get( 'entry_deal_link' ); 
		$this->data['entry_product'] = $this->language->get('entry_product');
		
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['button_add_module'] = $this->language->get('button_add_module');
		$this->data['button_remove'] = $this->language->get('button_remove');

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->error['image'])) {
			$this->data['error_image'] = $this->error['image'];
		} else {
			$this->data['error_image'] = array();
		}
		
		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_module'),
			'href'      => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		$data = array(
			'sort'  => 'pd.name',
			'order' => 'ASC',
			'start' => 0,
		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('module/' . $this->_name, 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		$this->data['action'] = $this->url->link('module/dealoftheday', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
								
		// Product
		if(isset($this->request->post[$this->_name .'_product']))
		{
			$this->data[$this->_name .'_product'] = $this->request->post[$this->_name .'_product'];
		}
		else 
		{
			$this->data[$this->_name .'_product'] = $this->config->get($this->_name .'_product');
		}

		if($this->config->get($this->_name .'_radio'))
		{
			$this->data['yes_checked'] = 'checked="checked"';
			$this->data['no_checked'] = '';
		}
		else
		{
			$this->data['no_checked'] = 'checked="checked"';
			$this->data['yes_checked'] = '';
		}
		
		$aProducts = $this->getProductSpecials($data);
		
		
		$this->data['values_product'] = '';
		
		if(!empty($aProducts)) {
			
			$this->data['values_product'] = '<select name="dealoftheday_product">';
			foreach($aProducts as $pId => $aProduct) {
				if ($pId == $this->data[$this->_name .'_product']) {
                	$this->data['values_product'] .= '<option value="'.$pId.'" selected="selected">'.$aProduct['name'].'</option>';
				}
				else {
                	$this->data['values_product'] .= '<option value="'.$pId.'">'.$aProduct['name'].'</option>';
                }
			}
			$this->data['values_product'] .= '</select>';
		}
		else {
			$products_url = $this->url->link('catalog/product', 'token=' . $this->session->data['token'], 'SSL');
			$this->data['values_product'] .= '<font style="display:block;font-size:13px;color:red">'.$this->data['text_add_special_at'].'</font><br><a href="'.$products_url.'" title="'.$this->data['text_click_to_add'].'">'.$this->data['text_click_to_add'].'</a>';
		}
		
		$this->data['modules'] = array();
		
		if (isset($this->request->post[$this->_name . '_module'])) {
			$this->data['modules'] = $this->request->post[$this->_name . '_module'];
		} elseif ($this->config->get($this->_name . '_module')) { 
			$this->data['modules'] = $this->config->get($this->_name . '_module');
		}

		$this->load->model('design/layout');
		
		$this->data['layouts'] = $this->model_design_layout->getLayouts();

		$this->template = 'module/' . $this->_name . '.tpl';
		$this->children = array(
			'common/header',
			'common/footer',
		);
	
		$this->response->setOutput($this->render());
				
	}
	
	public function getProductSpecials($data = array()) {	
				
		$sql = "SELECT DISTINCT ps.product_id FROM " . DB_PREFIX . "product_special ps LEFT JOIN " . DB_PREFIX . "product p ON (ps.product_id = p.product_id) LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) WHERE p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND ps.date_end > NOW()) AND p.quantity>0 GROUP BY ps.product_id";

		$sort_data = array(
			'pd.name',
			'p.model',
			'ps.price',
			'rating',
			'p.sort_order'
		);
		
		$sql .= " ORDER BY LCASE(" . $data['sort'] . ")";
		
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
	
		$product_data = array();
		
		$query = $this->db->query($sql);
		
		foreach ($query->rows as $result) { 		
			$product_data[$result['product_id']] = $this->getProduct($result['product_id']);
		}
		
		return $product_data;
	}
	
	public function getProduct($product_id) {
		$query = $this->db->query("SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'product_id=" . (int)$product_id . "') AS keyword FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE p.product_id = '" . (int)$product_id . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
				
		return $query->row;
	}
	
	private function validate() {
		if (!$this->user->hasPermission('modify', 'module/dealoftheday')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}	
	}
}
?>