<?php
/*
*  
* 	http://okom3pom.com
*	Module Ask question on product page for Prestashop 1.5 && 1.6
* 
*  Captcha https://www.phpcaptcha.org/
* 
*  Basé sur le module BetterPrice par Math75 sur le forum prestashop
*  http://www.prestashop.com/forums/topic/253939-module-gratuit-moins-chere-ailleurs/
*  
*	Released under the GNU General Public License
*
*	Author Okom3pom.com -> Thomas Roux
*	Version 1.8 - 01/09/2014
* 
*/

if (!defined('_CAN_LOAD_FILES_'))
	exit;

class okom_moreinfo extends Module
{
	
	private $_html = '';
	
	
	public function __construct()
	{
		$this->name = 'okom_moreinfo';
		$this->tab = 'Tools';
		$this->version = 1.8;
		$this->author = 'Okom3pom';
		$this->bootstrap = true;
		$this->generic_name = 'okom_moreinfo';
		
		parent::__construct();

		$this->displayName = $this->l('More info from product');
		$this->description = $this->l('Customer ask a question on product page');
		$this->checkforupdates();
		
		
	}

	public function install()
	{
		if (Shop::isFeatureActive())
			Shop::setContext(Shop::CONTEXT_ALL);

		return parent::install()
		&& $this->registerHook('displayHeader')
		&& $this->registerHook('displayLeftColumnProduct')
		&& $this->registerHook('displayFooterProduct')
		&& Configuration::updateValue('OKOM_MOREINFO_ACTIVATE',1) 
		&& Configuration::updateValue('OKOM_MOREINFO_EMAIL', Configuration::get('PS_SHOP_EMAIL'))
		&& Configuration::updateValue('OKOM_MOREINFO_CAPTCHA',1)
		&& Configuration::updateValue('OKOM_MOREINFO_MESSAGE', array((int)Configuration::get('PS_LANG_DEFAULT') => "")) 
		&& Configuration::updateValue('OKOM_MOREINFO_TEL',Configuration::get('BLOCKCONTACT_TELNUMBER'))
		&& Configuration::updateValue('OKOM_MOREINFO_TELH',array((int)Configuration::get('PS_LANG_DEFAULT') => ""));
		
	}
	
	public function uninstall()
	{
		Configuration::deleteByName('OKOM_MOREINFO_ACTIVATE');
		Configuration::deleteByName('OKOM_MOREINFO_EMAIL');
		Configuration::deleteByName('OKOM_MOREINFO_MESSAGE');
		Configuration::deleteByName('OKOM_MOREINFO_CAPTCHA');
		Configuration::deleteByName('OKOM_MOREINFO_TEL');
		Configuration::deleteByName('OKOM_MOREINFO_TELH');
		return parent::uninstall();
	
	}

	public function getContent()
	{
		$output = null;

		if (Tools::isSubmit('submit'.$this->name))
		{
			$OKOM_MOREINFO_EMAIL = strval(Tools::getValue('OKOM_MOREINFO_EMAIL'));
			if (!$OKOM_MOREINFO_EMAIL  || empty($OKOM_MOREINFO_EMAIL) || !Validate::isEmail($OKOM_MOREINFO_EMAIL))
				$output .= $this->displayError($this->l('Invalid email'));
				
			$OKOM_MOREINFO_ACTIVATE = intval(Tools::getValue('OKOM_MOREINFO_ACTIVATE'));	
			$OKOM_MOREINFO_FAQ = intval(Tools::getValue('OKOM_MOREINFO_FAQ'));
			$OKOM_MOREINFO_CAPTCHA = intval(Tools::getValue('OKOM_MOREINFO_CAPTCHA'));		
			$OKOM_MOREINFO_TEL = strval(Tools::getValue('OKOM_MOREINFO_TEL'));			
				
			
			$OKOM_MOREINFO_TELH = array();
			$OKOM_MOREINFO_MESSAGE = array();
			$languages = Language::getLanguages();
			foreach ($languages as $language) {
				$lang = (int)$language['id_lang'];
				
				$OKOM_MOREINFO_MESSAGE[$lang] = Tools::getValue('OKOM_MOREINFO_MESSAGE_'.$lang);
					if (  !Validate::isCleanHtml($OKOM_MOREINFO_MESSAGE[$lang])) {
						$output  .= $this->displayError(sprintf($this->l('Invalid terms for %s'), $language['name']));
						unset($OKOM_MOREINFO_MESSAGE[$lang]);
					}
					
				$OKOM_MOREINFO_TELH[$lang] = Tools::getValue('OKOM_MOREINFO_TELH_'.$lang);
					if (  !Validate::isCleanHtml($OKOM_MOREINFO_TELH[$lang])) {
						$output  .= $this->displayError(sprintf($this->l('Invalid terms for %s'), $language['name']));
						unset($OKOM_MOREINFO_TELH[$lang]);
					}
			}
			Configuration::updateValue('OKOM_MOREINFO_MESSAGE', $OKOM_MOREINFO_MESSAGE, true);	
			Configuration::updateValue('OKOM_MOREINFO_TELH', $OKOM_MOREINFO_TELH, true);			

			if (!$output)
			{				 
				
				if( Module::isEnabled('faq')  ) 
				Configuration::updateValue('OKOM_MOREINFO_FAQ',$OKOM_MOREINFO_FAQ);				
				Configuration::updateValue('OKOM_MOREINFO_TEL',$OKOM_MOREINFO_TEL);				
				Configuration::updateValue('OKOM_MOREINFO_CAPTCHA',$OKOM_MOREINFO_CAPTCHA);				

				if( Configuration::updateValue('OKOM_MOREINFO_ACTIVATE',$OKOM_MOREINFO_ACTIVATE) )
				{
				
					if(	$OKOM_MOREINFO_ACTIVATE == 1 )
					{						
							$this->registerHook('displayHeader');
							$this->registerHook('displayLeftColumnProduct');
							$this->registerHook('displayFooterProduct');				
					
					}
					else
					{
							$this->unregisterHook('displayHeader');
							$this->unregisterHook('displayLeftColumnProduct');
							$this->unregisterHook('displayFooterProduct');
					
					}
				
				}
				Configuration::updateValue('OKOM_MOREINFO_EMAIL', $OKOM_MOREINFO_EMAIL);
				$this->_html .= $this->displayConfirmation($this->l('Settings updated'));
			}
		}
		return $this->_html.$output.$this->renderForm();
	}

	public function renderForm()
	{
		
		$radio = 'switch';
		$icon = 'icon-cogs';
		$class = '';
		$type = 'icon';
		


		if(version_compare(_PS_VERSION_, '1.6.0.0', '<'))
		{
			$radio = 'radio';
			$icon = _PS_ADMIN_IMG_ .'cog.gif';
			$class = 't';
			$type = 'image';
		}
		
					

			$fields_form[0]['form'] = array(
			'legend' => array(
				'title' => $this->l('Settings'),
				'$type' => $icon
			),
			
			
			
			'input' => array(
			
				array(
					'name' => 'OKOM_MOREINFO_ACTIVATE',
					'type' => $radio,
					'class' => $class,
					'label' => $this->l('Activate'),
					'desc' => $this->l('Turn off the module when you can not answer the questions. Ex : Holidays ! '),
					'is_bool' => true,
					'values' => array(
						array(
							'id' => 'active_on',
							'value' => 1,
							'label' => $this->l('Enabled')
							),
						array(
							'id' => 'active_off',
							'value' => 0,
							'label' => $this->l('Disabled')
							)
						)
					),
					
				array(
					'name' => 'OKOM_MOREINFO_CAPTCHA',
					'type' => $radio,
					'class' => $class,
					'label' => $this->l('Activate Captcha'),
					'desc' => $this->l('Secure your form with a captcha'),
					'is_bool' => true,
					'values' => array(
						array(
							'id' => 'active_on',
							'value' => 1,
							'label' => $this->l('Enabled')
							),
						array(
							'id' => 'active_off',
							'value' => 0,
							'label' => $this->l('Disabled')
							)
						)
					),

				array(
					'name' => 'OKOM_MOREINFO_FAQ',
					'type' => $radio,
					'class' => $class,
					'label' => $this->l('Activate faq link'),
					'is_bool' => true,
					'hint' => $this->l('Only works if faq module activate'),
					'values' => array(
						array(
							'id' => 'active_on',
							'value' => 1,
							'label' => $this->l('Enabled')
							),
						array(
							'id' => 'active_off',
							'value' => 0,
							'label' => $this->l('Disabled')
							)
						)
					),
			

			array(
					'type' => 'textarea',
					'label' => $this->l('Message'),
					'desc' => $this->l('Message on fancybox. Ex : please read faq befor ask a question !'),
					'lang' => true,
					'autoload_rte' => true,
					'rows' => 10,
					'cols' => 100,
					'name' => 'OKOM_MOREINFO_MESSAGE',


				),
				array(
					'type' => 'text',
					'label' => $this->l('Email'),
					'desc' => $this->l('Email address use to receive customer request.'),
					'name' => 'OKOM_MOREINFO_EMAIL',
					'size' => 60,
					'required' => true
				),
				array(
					'type' => 'text',
					'label' => $this->l('Phone Number'),
					'desc' => $this->l('Phone number Hotline.'),
					'name' => 'OKOM_MOREINFO_TEL',
					'size' => 60,


				),
				array(

					'type' => 'textarea',									
					'label' => $this->l('Hotline available'),
					'desc' =>  $this->l('Hotline available'),
					'lang' => true,
					'autoload_rte' => true,
					'rows' => 10,
					'cols' => 100,
					'name' => 'OKOM_MOREINFO_TELH',	


				)
				
				
			),
			'submit' => array(
				'title' => $this->l('Save'),
			)
		);
		
		$languages = Language::getLanguages(false);
		foreach ($languages as $k => $language)
			$languages[$k]['is_default'] = (int)$language['id_lang'] == Configuration::get('PS_LANG_DEFAULT');
		

		$helper = new HelperForm();
		$helper->module = $this;
		$helper->name_controller = $this->name;
		$helper->token = Tools::getAdminTokenLite('AdminModules');
		$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
		$helper->languages = $languages;
		$helper->default_form_language = (int)Configuration::get('PS_LANG_DEFAULT');
		$helper->allow_employee_form_lang = true;
		$helper->title = $this->displayName;
		$helper->submit_action = 'submit'.$this->name;		
		$helper->tpl_vars = array(
			'uri' => $this->getPathUri(),
			'fields_value' => $this->getConfigFieldsValues(),
			'languages' => $this->context->controller->getLanguages(),
			'id_language' => $this->context->language->id
			);

		return $helper->generateForm($fields_form);
	}
	
	
	protected function getConfigFieldsValues()
	{
		$languages = Language::getLanguages(false);
		$fields = array();

		foreach ($languages as $lang)
		{
			$fields['OKOM_MOREINFO_MESSAGE'][$lang['id_lang']] = strval(Tools::getValue('OKOM_MOREINFO_MESSAGE_'.$lang['id_lang'], Configuration::get('OKOM_MOREINFO_MESSAGE', $lang['id_lang'])));
			$fields['OKOM_MOREINFO_TELH'][$lang['id_lang']] = strval(Tools::getValue('OKOM_MOREINFO_TELH_'.$lang['id_lang'], Configuration::get('OKOM_MOREINFO_TELH', $lang['id_lang'])));
		}
		
			return array(
				'OKOM_MOREINFO_MESSAGE' => $fields['OKOM_MOREINFO_MESSAGE'],
				'OKOM_MOREINFO_FAQ' => Tools::getValue('OKOM_MOREINFO_FAQ', Configuration::get('OKOM_MOREINFO_FAQ')),
				'OKOM_MOREINFO_ACTIVATE' => Tools::getValue('OKOM_MOREINFO_ACTIVATE', Configuration::get('OKOM_MOREINFO_ACTIVATE')),
				'OKOM_MOREINFO_CAPTCHA' => Tools::getValue('OKOM_MOREINFO_CAPTCHA', Configuration::get('OKOM_MOREINFO_CAPTCHA')),
				'OKOM_MOREINFO_EMAIL' => Tools::getValue('OKOM_MOREINFO_EMAIL', Configuration::get('OKOM_MOREINFO_EMAIL')),
				'OKOM_MOREINFO_TEL' => strval(Tools::getValue('OKOM_MOREINFO_TEL', Configuration::get('OKOM_MOREINFO_TEL'))),
				'OKOM_MOREINFO_TELH' => $fields['OKOM_MOREINFO_TELH']				
			);
	}
	
	
	
	public function hookDisplayHeader($params)
	{
		if (get_class($this->context->controller) == 'ProductController') 
		{
			
				if (version_compare(_PS_VERSION_, '1.6.0.0', '>='))		
					$this->context->controller->addCSS($this->_path.'views/css/okom_moreinfo.css', 'all');
				else
					$this->context->controller->addCSS($this->_path.'views/css/okom_moreinfo15.css', 'all');
			
				$this->context->controller->addJS($this->_path.'views/js/okom_moreinfo.js');			
				$this->context->controller->addJS(_PS_JS_DIR_.'validate.js');
		
		
		}
	}
		
	public function hookDisplayFooterProduct($params)
	{
		
			$faq_link = false;
			if( Module::isEnabled('faq')  && Configuration::get('OKOM_MOREINFO_FAQ') == 1 ) 
			{
					$url = (Configuration::get("PS_REWRITING_SETTINGS") == 1 && $this->getMeta()!= false) ? $this->getMeta():'index.php?fc=module&module=faq&controller=default';
					$faq_link =  __PS_BASE_URI__.$url;
			}
			
			$phone_number = Configuration::get('OKOM_MOREINFO_TEL');
			$schedule = Configuration::get('OKOM_MOREINFO_TELH', (int)$this->context->language->id);
			
			$secure_image = false;
			
			if( Configuration::get('OKOM_MOREINFO_CAPTCHA') == 1 )
			{
				
				if( !class_exists('Securimage') )
					require_once 'securimage/securimage.php';
				
				$options = array();
				$options['image_id'] = 'moreinfo_captcha';
				$options['input_name'] = 'ct_captcha'; 
				$options['input_class'] = 'form-control grey';
				$options['input_text'] = $this->l('Type the secure text');
				$secure_image = Securimage::getCaptchaHtml($options);
				
			}
			


			
			
			
			
			$this->context->smarty->assign(array(
				'product' => $params['product'],
				'cover' => Product::getCover((int)$params['product']->id),
				'message' => Configuration::get('OKOM_MOREINFO_MESSAGE', (int)$this->context->language->id),
				'faq_link' => $faq_link ,
				'secure_image' => $secure_image,
				'phone_number' => $phone_number ,
				'schedule' =>  $schedule,
				'fileupload' => Configuration::get('PS_CUSTOMER_SERVICE_FILE_UPLOAD')
			));

		
		if (version_compare(_PS_VERSION_, '1.6.0.0', '>='))		
				return $this->display(__FILE__, 'views/templates/hooks/productfooter.tpl');
		else
				return $this->display(__FILE__, 'views/templates/hooks/productfooter15.tpl');
		
	}
	
	public function hookDisplayLeftColumnProduct($params)
	{
		return $this->display(__FILE__, 'views/templates/hooks/leftcolumnproduct.tpl');
	}
	
	function checkforupdates()
	{

		if (ini_get("allow_url_fopen")) 
		{
			if (function_exists("file_get_contents"))
			{					

				$lastversion = (float)@file_get_contents('http://www.okom3pom.com/dev-modules/'.$this->generic_name.'.version');
													
					if( number_format($lastversion,2) != number_format($this->version,2) )
					{
						$this->warning = $this->l('A new version of the module is available at: http://okom3pom.com');
						
						$this->_html = '<div class="alert alert-warning"><button type="button" class="close" data-dismiss="alert">×</button>'.$this->l('A new version of the module is available at: http://okom3pom.com').'</div>';
					}
			}
		}

	}
	
	/*
	 *  function copy and past
	 *  Faq module by Eolia ! 
	 */
	 
	private function getMeta()
	{
		global  $cookie;
		
		if (!$meta = Db::getInstance()->ExecuteS('SELECT `id_meta` FROM '._DB_PREFIX_.'meta WHERE `page`= \'module-faq-default\' '))
			return false;
		if (!$metaUrlLang = Db::getInstance()->ExecuteS('SELECT `url_rewrite` FROM '._DB_PREFIX_.'meta_lang WHERE `id_meta`= '.$meta[0]['id_meta'].' AND `id_lang` = '.$cookie->id_lang.' AND `id_shop` = '.$this->context->shop->id ))
			return false;
			
		return $metaUrlLang[0]['url_rewrite'];
	}
	
	
	
}

?>
