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

		$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_status_pro', $this->request->post);		
					
			$this->session->data['success'] = $this->language->get('text_success');

			$this->cache->delete('stock_status_pro.' . (int)$this->config->get('config_language_id'));
			$this->cache->delete('stock_status_pro_options');

			$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_settings'] = $this->language->get('text_settings');
		$this->data['text_list'] = $this->language->get('text_list');

		$this->data['entry_name'] = $this->language->get('entry_name');
		
		$this->data['entry_status_info'] = $this->language->get('entry_status_info');
		$this->data['entry_stockstatus'] = $this->language->get('entry_stockstatus');
		$this->data['entry_options'] = $this->language->get('entry_options');
		$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');
		$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['quantity'])) {
			$this->data['error_quantity'] = $this->error['quantity'];
		} else {
			$this->data['error_quantity'] = 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' => ' :: '
   		);
		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('module/stock_status_pro', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		$this->data['action'] = $this->url->link('module/stock_status_pro', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['localisation_stock_status'] = $this->url->link('localisation/stock_status', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['setting_setting'] = $this->url->link('setting/setting', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['modules'] = array();
		
		$this->load->model('localisation/language');
		
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
		
		if (isset($this->request->post['stock_status_pro_options'])) {
			$this->data['modules'] = $this->request->post['stock_status_pro_options'];
		} elseif ($this->config->get('stock_status_pro_options')) { 
			
			foreach ($this->config->get('stock_status_pro_options') as $quantity => $stock_status_id) {
				$rebuild_stock_status_pro_options[$quantity]['stock_status_id'] = $stock_status_id;
				$rebuild_stock_status_pro_options[$quantity]['quantity'] = $quantity;
			}
			
			$this->data['modules'] = $rebuild_stock_status_pro_options;
		}

		$this->data['check_status'] = $this->config->get('stock_status_pro_status');
		$this->data['config_stock_display_true'] = $this->config->get('config_stock_display');
		
		$this->load->model('localisation/stock_status');
		
		$this->data['stock_status_pro'] = $this->model_localisation_stock_status->getStockStatuses();
		$this->data['stock_status_pro_text'] = (($this->config->get('stock_status_pro_text')));
		
		
		

		$this->template = 'module/stock_status_pro.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}

		protected function validate() {
		if (!$this->user->hasPermission('modify', 'module/stock_status_pro')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
			
		if (isset($this->request->post['stock_status_pro_options'])) {
			foreach ($this->request->post['stock_status_pro_options'] as $key => $value) {
				if (!$value['quantity']) {
					$this->error['quantity'][$key] = $this->language->get('error_quantity');
				} else {
					$stock_status_pro_options[$value['quantity']] = $value['stock_status_id'];
				}
			}
		}			
						
		if (!$this->error) {
			ksort($stock_status_pro_options);	
			$this->request->post['stock_status_pro_options'] = $stock_status_pro_options;
			return true;
		} else {
			return false;
		}	
	}
}