<?php
class ControllerModuleStockAlert extends Controller {
	private $error = array(); 
	
	public function index() {   
		$this->load->language('module/stock_alert');

		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('setting/setting');
				
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) {
			$this->model_setting_setting->editSetting('stock_alert', $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['entry_message'] = $this->language->get('entry_message');
		$this->data['entry_quantity'] = $this->language->get('entry_quantity');
		$this->data['entry_option_quantity'] = $this->language->get('entry_option_quantity');
		$this->data['entry_email'] = $this->language->get('entry_email');
		$this->data['entry_status'] = $this->language->get('entry_status');
		
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
 		if (isset($this->error['code_quantity'])) {
			$this->data['error_code_quantity'] = $this->error['code_quantity'];
		} else {
			$this->data['error_code_quantity'] = '';
		}
		
		if (isset($this->error['code_option_quantity'])) {
			$this->data['error_code_option_quantity'] = $this->error['code_option_quantity'];
		} else {
			$this->data['error_code_option_quantity'] = '';
		}
		
		if (isset($this->error['code_email'])) {
			$this->data['error_code_email'] = $this->error['code_email'];
		} else {
			$this->data['error_code_email'] = '';
		}
		
		$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' => ' :: '
   		);
		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('module/stock_alert', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		$this->data['action'] = $this->url->link('module/stock_alert', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
		
  		
		if (isset($this->request->post['stock_alert_message'])) {
			$this->data['stock_alert_message'] = $this->request->post['stock_alert_message'];
		} else {
			$this->data['stock_alert_message'] = $this->config->get('stock_alert_message');
		}	
		
		if (isset($this->request->post['stock_alert_quantity'])) {
			$this->data['stock_alert_quantity'] = $this->request->post['stock_alert_quantity'];
		} else {
			$this->data['stock_alert_quantity'] = $this->config->get('stock_alert_quantity');
		}
		
		if (isset($this->request->post['stock_alert_option_quantity'])) {
			$this->data['stock_alert_option_quantity'] = $this->request->post['stock_alert_option_quantity'];
		} else {
			$this->data['stock_alert_option_quantity'] = $this->config->get('stock_alert_option_quantity');
		}
		
		if (isset($this->request->post['stock_alert_email'])) {
			$this->data['stock_alert_email'] = $this->request->post['stock_alert_email'];
		} else {
			$this->data['stock_alert_email'] = $this->config->get('stock_alert_email');
		}

		if (isset($this->request->post['stock_alert_status'])) {
			$this->data['stock_alert_status'] = $this->request->post['stock_alert_status'];
		} else {
			$this->data['stock_alert_status'] = $this->config->get('stock_alert_status');
		}		
		
		$this->template = 'module/stock_alert.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render());
	}
	
	private function validate() {
		if (!$this->user->hasPermission('modify', 'module/stock_alert')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		if (!$this->request->post['stock_alert_quantity']) {
			$this->error['code_quantity'] = $this->language->get('error_code_quantity');
		}
		
		if (!$this->request->post['stock_alert_option_quantity']) {
			$this->error['code_option_quantity'] = $this->language->get('error_code_option_quantity');
		}
		
		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}	
	}
}
?>