<?php

class askinfoproduct extends Module
{
 	function __construct()
 	{
 	 	$this->name = 'askinfoproduct';
 	 	$this->version = '0.3';
 	 	$this->tab = 'Products';

		parent::__construct();

		$this->displayName = $this->l('Ask info about a product module');
		$this->description = $this->l('Allows customers to ask more information about a product');
 	}

	function install()
	{
	 	if (!parent::install() OR !Configuration::updateValue('EMAILSEND_ASKINFO', Configuration::get('PS_SHOP_EMAIL')))
	 		return false;
	 	return $this->registerHook('productActions');
	}

	function hookProductActions($params)
	{
		global $smarty;
		$smarty->assign('this_path', $this->_path);
		return $this->display(__FILE__, 'product_page.tpl');
	}

	public function getContent()
	{
		$output = '<h2>'.$this->displayName.'</h2>';
		if (Tools::isSubmit('submitemail'))
		{
			$email_notify = Tools::getValue('email_notify');
			if (!Validate::isEmail($email_notify))
				$errors[] = $this->l('Invalid e-mail address');
			else
				Configuration::updateValue('EMAILSEND_ASKINFO', $email_notify);
			if (isset($errors) AND sizeof($errors))
				$output .= $this->displayError(implode('<br />', $errors));
			else
				$output .= $this->displayConfirmation($this->l('Settings updated'));
		}
		return $output.$this->displayForm();
	}

	public function displayForm()
	{
		$output = '
		<form action="'.$_SERVER['REQUEST_URI'].'" method="post">
			<fieldset><legend><img src="'.$this->_path.'logo.gif" alt="" title="" />'.$this->l('Settings').'</legend>
				<label>'.$this->l('Email address for receive messages').'</label>
				<div class="margin-form">
					<input type="text" size="25" name="email_notify" value="'.Tools::getValue('email_notify', Configuration::get('EMAILSEND_ASKINFO')).'" />
				</div>
				<center><input type="submit" name="submitemail" value="'.$this->l('Save').'" class="button" /></center>
			</fieldset>
		</form>';
		return $output;
	}

	public function displayFrontForm()
	{
		global $smarty;
		$error = false;
		$confirm = false;

		if (isset($_POST['submitaskinfoproduct']))
		{
			global $cookie, $link;
			/* Product informations */
			$product = new Product(intval(Tools::getValue('id_product')), false, intval($cookie->id_lang));
			$productLink = $link->getProductLink($product);
			
			/* Fields verifications */
			if (empty($_POST['email']) OR empty($_POST['name']))
				$error = $this->l('You must fill all fields.');
			elseif (!Validate::isEmail($_POST['email']))
				$error = $this->l('Your email is invalid.');
			elseif (!Validate::isName($_POST['name']))
				$error = $this->l('Your name is invalid.');
			elseif (!Validate::isMessage($_POST['message']))
				$error = $this->l('Your message contains invalid characters.');
			else
			{
				/* Email generation */
				$subject = $this->l('Informations about').' '.$product->name;
                $to = Configuration::get('EMAILSEND_ASKINFO');
				$templateVars = array(
					'{product}' => $product->name,
                    '{email}' => Tools::safeOutput($_POST['email']),
                    '{message}' => Tools::safeOutput($_POST['message']),
					'{name}' => Tools::safeOutput($_POST['name'])
				);

				/* Email sending */
				if (!Mail::Send(intval($cookie->id_lang), 'askinfoproduct', $subject, $templateVars, $to, NULL, NULL, NULL, NULL, NULL, dirname(__FILE__).'/mails/'))
					$error = $this->l('An error occurred during the process.');
				else
					$confirm = $this->l('An email has been sent successfully');
			}
		}
		else
		{
			global $cookie, $link;
			/* Product informations */
			$product = new Product(intval(Tools::getValue('id_product')), false, intval($cookie->id_lang));
			$productLink = $link->getProductLink($product);
		}
		
		/* Image */
		$images = $product->getImages(intval($cookie->id_lang));
		foreach ($images AS $k => $image)
			if ($image['cover'])
			{
				$cover['id_image'] = intval($product->id).'-'.intval($image['id_image']);
				$cover['legend'] = $image['legend'];
			}
		
		if (!isset($cover))
			$cover = array('id_image' => Language::getIsoById(intval($cookie->id_lang)).'-default', 'legend' => 'No picture');
		
		$smarty->assign(array(
			'cover' => $cover,
			'errors' => $error,
			'confirm' => $confirm,
			'product' => $product,
			'productLink' => $productLink
		));

		return $this->display(__FILE__, 'askinfoproduct.tpl');
	}
}
