<?php
class ControllerModuleStockStatusOnMailOrder extends Controller {
	
	private $error = array();
	
	public function index() {
		
		$this->load->language('module/stock_status_on_mail_order');
		
		$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_on_mail_order', $this->request->post);
	
			$this->session->data['success'] = $this->language->get('text_success');
			
			$this->response->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
		}
		$data['heading_title'] = $this->language->get('heading_title');
		
		$data['text_stock'] = $this->language->get('text_stock');
		$data['text_settings'] = $this->language->get('text_settings');
		$data['help_stock_display'] = $this->language->get('help_stock_display');
		$data['help_stock_warning'] = $this->language->get('help_stock_warning');
		$data['help_stock_checkout'] = $this->language->get('help_stock_checkout');
		
		$data['text_yes'] = $this->language->get('text_yes');
		$data['text_no'] = $this->language->get('text_no');
		
		$data['entry_stock_display'] = $this->language->get('entry_stock_display');
		
		$data['text_edit'] = $this->language->get('text_edit');
		$data['text_enabled'] = $this->language->get('text_enabled');
		$data['text_disabled'] = $this->language->get('text_disabled');
		$data['text_list'] = $this->language->get('text_list');
		$data['text_user_guide'] = $this->language->get('text_user_guide');
		
		$data['entry_status'] = $this->language->get('entry_status');
		$data['entry_name']   = $this->language->get('entry_name');
		$data['entry_options'] = $this->language->get('entry_options');
		
		$data['button_save'] = $this->language->get('button_save');
		$data['button_cancel'] = $this->language->get('button_cancel');
		
		$data['button_module_add'] = $this->language->get('button_module_add');
		$data['button_remove'] = $this->language->get('button_remove');
		
		$data['thead_entry'] = $this->language->get('thead_entry');
		$data['thead_info'] = $this->language->get('thead_info');
		$data['error_quantity'] = $this->language->get('error_quantity');

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		}
		else {
			$data['error_warning'] = '';
		}
		
		if (isset($this->error['has_error'])) {
			$data['has_error'] = $this->error['has_error'];
		}
		
		$data['breadcrumbs'] = array();
		
		$data['breadcrumbs'][] = array(
		'text' => $this->language->get('text_home'),
		'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
		);
		
		$data['breadcrumbs'][] = array(
		'text' => $this->language->get('text_module'),
		'href' => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL')
		);
		
		$data['breadcrumbs'][] = array(
		'text' => $this->language->get('heading_title'),
		'href' => $this->url->link('module/stock_status_on_mail_order', 'token=' . $this->session->data['token'], 'SSL')
		);
		
		$data['action'] = $this->url->link('module/stock_status_on_mail_order', 'token=' . $this->session->data['token'], 'SSL');
		
		$data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
		
		$data['link_to_list_of_stock_status'] = $this->url->link('localisation/stock_status', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->load->model('localisation/language');
		$data['languages'] = $this->model_localisation_language->getLanguages();
		
		if (isset($this->request->post['stock_status_on_mail_order_status'])) {
			$data['stock_status_on_mail_order_status'] = $this->request->post['stock_status_on_mail_order_status'];
			} else {
			$data['stock_status_on_mail_order_status'] = $this->config->get('stock_status_on_mail_order_status');
		}
		
		if (isset($this->request->post['stock_status_on_mail_order_options'])) {
			$data['stock_status_on_mail_order_options'] = $this->request->post['stock_status_on_mail_order_options'];
			} else {
			$data['stock_status_on_mail_order_options'] = array();
			if(is_array($this->config->get('stock_status_on_mail_order_options'))){
				$data['stock_status_on_mail_order_options'] = $this->config->get('stock_status_on_mail_order_options');
			}
		}

		$this->load->model('localisation/stock_status');

		$data['list_of_stock_status'] = $this->model_localisation_stock_status->getStockStatuses();

		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');

		$this->response->setOutput($this->load->view('module/stock_status_on_mail_order.tpl', $data));
	}

	protected function validate() {
		if (!$this->user->hasPermission('modify', 'module/stock_status_on_mail_order')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		if (isset($this->request->post['stock_status_on_mail_order_options'])) {
			foreach ($this->request->post['stock_status_on_mail_order_options'] as $key => $value) {
				if ($quantity = empty($value['quantity'])) {
					$this->error['has_error'][$key] = $key;
				}
			}
		}

		if($validated = !$this->error){
			usort($this->request->post['stock_status_on_mail_order_options'], function($a, $b) {
				return $a['quantity'] - $b['quantity'];
			});
		}

		return $validated;
	}
}
