<?php 
class ControllerMadeshopMinprice extends Controller {
	private $error = array();
	public function index() {
        $this->load->language('madeshop/minprice');
		$data['heading_title'] = $this->language->get('heading_title');
		$data['text_mail'] = $this->language->get('text_mail');
		$data['text_url'] = $this->language->get('text_url');
        $data['text_name'] = $this->language->get('text_name');
		$data['text_close'] = $this->language->get('text_close');
		$data['text_back'] = $this->language->get('text_back');
		$data['text_loading'] = $this->language->get('text_loading');
		$data['text_success'] = $this->language->get('text_success');
		$data['text_alert'] = $this->language->get('text_alert');
        $data['minprice_button'] = $this->language->get('minprice_button');
		
		$data['product_id'] = $this->request->get['product_id'];

		if ($this->customer->isLogged()) {
			$data['minprice_name'] = $this->customer->getFirstName();
			$data['minprice_mail'] = $this->customer->getEmail();
			
		} else {
			$data['minprice_name'] = '';
			$data['minprice_mail'] = '';
		}
		
		if ($this->config->get('theme_madeshop_minprice_id')) {
			$this->load->model('catalog/information');

			$information_info = $this->model_catalog_information->getInformation($this->config->get('theme_madeshop_minprice_id'));

			if ($information_info) {
				$data['text_agree'] = sprintf($this->language->get('text_agree'), $this->url->link('information/information/agree', 'information_id=' . $this->config->get('theme_madeshop_minprice_id'), true), $information_info['title'], $information_info['title']);
			} else {
				$data['text_agree'] = '';
			}
		} else {
			$data['text_agree'] = '';
		}

		if (isset($this->request->post['agree'])) {
			$data['agree'] = $this->request->post['agree'];
		} else {
			$data['agree'] = false;
		}

	
		
		// Captcha
		if ($this->config->get('theme_madeshop_call_captcha')) {
			$data['captcha'] = $this->load->controller('extension/captcha/' . $this->config->get('config_captcha'), $this->error);
		} else {
			$data['captcha'] = '';
		}
		
		return $this->load->view('madeshop/minprice', $data);
		
	}
	
	public function minprice() {
    			
	$json = array();
	$this->load->model('madeshop/minprice');
	
	if((utf8_strlen($this->request->post['minprice_name']) < 1) || (utf8_strlen($this->request->post['minprice_name']) > 35)){
		$json['error']['name'] = true;		
	}

	if ((utf8_strlen($this->request->post['minprice_mail']) > 96) || !filter_var($this->request->post['minprice_mail'], FILTER_VALIDATE_EMAIL)) {
		$json['error']['mail'] = $this->language->get('error_mail');
	}
	
	if(empty($json['error'])){
		
		$data = array (
			'name'  => $this->request->post['minprice_name'],
			'mail'  => $this->request->post['minprice_mail'],
			'url'	=> $this->request->post['minprice_url'],
			'shop_url'	=> $this->request->post['shop_url'],
		);
		
		$this->model_madeshop_minprice->addMinprice($data);
		
		$json['success'] = true;
	}
   	
	$this->response->addHeader('Content-Type: application/json');
	$this->response->setOutput(json_encode($json));
    }
}
