<?php

/**
 * @version      $Id: website_contact.php
 * @package      Easyshop
 * @copyright    Copyright (C) 2005 - 2011 TA-EDV
 * @license      proprietary
 * @author       Richard Kammermayer <rk@ta-edv.de>
 * Easyshop is a web shop system
 */
class website_contact {

	private $base_object;
	private $layout_object;
	private $mandatoryfields = array('forename', 'surname', 'e_mail', 'call_number_private');

	public function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->layout_object = $layout_object;
	}

	function run() {

		if ($_POST['account_contact'] == 'ready_to_fly') {
			$this->layout_object->assign('contactdata', $_POST['contact']);

			$json = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=6LfR26UZAAAAACn88_M7nYay62q7NZL6v0oh4lK3&response='.$_POST['g-recaptcha-response']);
			$data_check = json_decode($json);
			
			if ($data_check->success && $this->check_mandatory($_POST['contact'])) {
//				include './core/mail.class.php';

				$mailer = new mail_tools($this->base_object);

				$message = $this->layout_object->_fetch('content_contact_mail.tpl');

				$subject = 'Kontakt Website';

				if (isset($_POST['contact']['callback']) && $_POST['contact']['callback']) {
					$_POST['contact']['callback'] = preg_replace("@[^0-9\.]@si", '', $_POST['contact']['callback']);
					$subject .= ' (Rückruf: ' . $_POST['contact']['callback'] . ')';
				}

				$sent = $mailer->send_mail($subject, $message, $this->base_object->config->shopConfiguration['mailRecipients'], 'intern');

				if ($sent) {
					$this->layout_object->assign('contactSTATUS', 1);
				} else {
					$this->layout_object->assign('contactSTATUS', 2);
				}
			} else {
				$this->layout_object->assign('contactSTATUS', 3);
			}
		}

		return $this->layout_object->_fetch('content_contact.tpl');
	}

	public function check_mandatory(&$data) {
		if (is_array($this->mandatoryfields)) {
			$checked = true;

			foreach ($this->mandatoryfields as $field) {
				$input = trim($data[$field]);
				$checked &=!empty($input);

				$data[$field] = $input;
			}

			return $checked;
		} else {
			return true;
		}
	}

}
