* @copyright 2013 WebInColor * @version 2.8 * @link http://www.webincolor.fr/ * @since File available since Release 1.0 */ if (!defined('_PS_VERSION_')) exit; class Ekomi extends Module { private $_html = ''; private $_postErrors = array(); public $_ekomi_object; public $id_lang; public $iso_lang; function __construct() { $this->name = 'ekomi'; $this->tab = 'advertising_marketing'; $this->author = 'Web In Color'; $this->version = '2.8'; $this->need_instance = 0; $this->module_key = '0fbd74e72a982dfbb3ee49f3ccec117a'; parent::__construct(); $this->displayName = $this->l('eKomi'); $this->description = $this->l('Adds an eKomi block, Ekomi product reviews'); $this->confirmUninstall = $this->l('Are you sure you want to uninstall?'); if (self::isInstalled($this->name)) { $this->id_lang = (int)Configuration::get('PS_LANG_DEFAULT'); $this->iso_lang = pSQL(Language::getIsoById($this->id_lang)); /* Check Mail Directory */ if (!is_dir('../modules/'.$this->name.'/mails/'.$this->iso_lang.'/')) $this->warning .= $this->l('directory').' "'.$this->iso_lang.'" does not exist '.'../modules/'.$this->name.'/mails/'.$this->iso_lang.'/'; } if($this->getVersion() > 1.4) $this->default_hook = 'displayFooter'; else $this->default_hook = 'footer'; //including Ekomi Class $path = dirname(__FILE__); if (strpos(__FILE__, 'Module.php') !== false) $path .= '/../modules/'.$this->name; if($this->getVersion() > 1.4) include_once($path.'/lib/ekomi_class.php'); else include_once($path.'/lib/ekomi_class_backward.php'); /* Backward compatibility */ if (_PS_VERSION_ < '1.5' AND _PS_VERSION_ >= '1.4') require(_PS_MODULE_DIR_.$this->name.'/backward_compatibility/backward.php'); // Retrocompatibility $this->initContext(); } // Retrocompatibility 1.4/1.5 private function initContext() { if(!$this->context->shop->id) $this->context->shop->id = 1; } public function install() { include(dirname(__FILE__).'/sql/install.php'); foreach ($sql as $s) if (!Db::getInstance()->execute($s)) return false; if($this->getVersion() > 1.4) return (parent::install() AND $this->registerHook('displayFooter') AND $this->registerHook('displayHeader') AND $this->registerHook('actionOrderStatusUpdate') AND $this->registerHook('displayProductTab') AND $this->registerHook('displayProductTabContent')); else return (parent::install() AND $this->registerHook('footer') AND $this->registerHook('header') AND $this->registerHook('updateOrderStatus') AND $this->registerHook('productTab') AND $this->registerHook('productTabContent')); } public function uninstall() { include(dirname(__FILE__).'/sql/uninstall.php'); foreach ($sql as $s) if (!Db::getInstance()->execute($s)) return false; if (!parent::uninstall()) return false; return true; } public function getVersion(){ if(_PS_VERSION_ > 1.3 AND _PS_VERSION_ <= 1.4) return 1.4; if(_PS_VERSION_ > 1.4 AND _PS_VERSION_ >= 1.6) return 1.5; return 1.3; } public function fetchTemplate($path, $name, $extension = false) { return $this->display(__FILE__,$path.$name.'.'.($extension ? $extension : 'tpl')); } public function getContent() { $ekomiObj = EkomiObject::getByIdShop($this->context->shop->id); if(!Validate::isLoadedObject($ekomiObj)) Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'ekomi`(`id_ekomi`,`id_shop`,`display_block`,`display_reviews`,`picto`,`sending`,`range`,`hook`,`id_state`) VALUES (\'\','.$this->context->shop->id.',0,0,\'1-star-yellow\',0,0,\''.$this->default_hook.'\',5);'); if($this->getVersion() > 1.4) $this->_html = ''; else $this->_html = ''; $this->_html .= '

'.$this->l('Expertise e-commerce Prestashop').'

'.$this->l('Download the documentation').'
'; if($this->getVersion() <= 1.4) $this->_html .= ''; if(!extension_loaded('soap')) $this->_html .= $this->displayError($this->l('php-soap extension is not loaded. Thank you to contact your network administrator.')); if (Tools::isSubmit('submitEkomi')) { $ekomiObj = EkomiObject::getByIdShop($this->context->shop->id); $ekomiObj->copyFromPost(); $ekomiObj->update(); if($this->getVersion() > 1.4) { if ($this->isRegisteredInHook('displayFooter')) $this->unregisterHook('displayFooter'); if ($this->isRegisteredInHook('displayLeftColumn')) $this->unregisterHook('displayLeftColumn'); if ($this->isRegisteredInHook('displayRightColumn')) $this->unregisterHook('displayRightColumn'); } else { if ($this->isRegisteredInHook('footer')) $this->unregisterHook('footer'); if ($this->isRegisteredInHook('leftColumn')) $this->unregisterHook('leftColumn'); if ($this->isRegisteredInHook('rightColumn')) $this->unregisterHook('rightColumn'); } $this->registerHook(Tools::getValue('hook')); $this->_html .= '
'.$this->l('Settings updated').'
'; } return $this->_html.$this->displayForm(); } public function displayForm() { $states = OrderState::getOrderStates($this->context->language->id); $languages = Language::getLanguages(false); $divLangName = 'apiId¤apiKey¤apiScript'; $ekomiObj = EkomiObject::getByIdShop($this->context->shop->id); $this->_html = '
'.$this->l('Ekomi settings').'
'.$this->l('Please fill the form with the data that eKomi gives you.').'
'; foreach ($languages as $language) { $this->_html .='
'; } $this->_html .= $this->displayFlags($languages, $this->id_lang, $divLangName, 'apiId', true); $this->_html .= '

'; foreach ($languages as $language) { $this->_html .= '
'; } $this->_html .= $this->displayFlags($languages, $this->id_lang, $divLangName, 'apiKey', true); $this->_html .= '

'; foreach ($languages as $language) { $this->_html .= '
'; } $this->_html .= $this->displayFlags($languages, $this->id_lang, $divLangName, 'apiScript', true); $this->_html .= '

'.$this->l('Do not forget to log in to your eKomi interface to configure your email type as well as the time of sending of this email.').'
display_block ? 'checked="checked"' : '').' /> display_block ? 'checked="checked"' : '').' />

'.$this->l('Show or don\'t show the block (orders will be sent to eKomi whether you choose to hide or display the block).').'

display_richsnippet ? 'checked="checked"' : '').' /> display_richsnippet ? 'checked="checked"' : '').' />

'.$this->l('Show rich snippet block on product page.').'

display_reviews ? 'checked="checked"' : '').' /> display_reviews ? 'checked="checked"' : '').' />

'.$this->l('Show product reviews on product page.').'

'.$this->l('Select the display period of the reviews').'

sending ? 'checked="checked"' : '').' /> sending ? 'checked="checked"' : '').' />

'.$this->l('If you disable this option, only the rating of the store will be required.').'

1-star-yellow 2-star-green 3-star-blue 4-thumbs-yellow 5-thumbs-green 6-thumbs-blue

'.$this->l('Select the status to which we send the ratting request to the customer').'

'; $this->_html .= ''; $this->_html .= ''; return $this->_html; } public function hookHeader($params) { if (version_compare(_PS_VERSION_,'1.5','<')) { Tools::addCSS(($this->_path).'css/jquery.star-rating.css', 'all'); Tools::addCSS(($this->_path).'css/module.css', 'all'); } else { $this->context->controller->addCSS(($this->_path).'css/jquery.star-rating.css', 'all'); $this->context->controller->addCSS(($this->_path).'css/module.css', 'all'); } $this->context->smarty->assign(array( 'version' => version_compare(_PS_VERSION_, '1.4.1'), 'pathJS' => $this->_path.'js/', )); return $this->fetchTemplate('/views/templates/hooks/', 'header'); } public function hookDisplayHeader($params) { return $this->hookHeader($params); } public function hookFooter($params) { $ekomiObj = EkomiObject::getByIdShop($this->context->shop->id); if(Validate::isLoadedObject($ekomiObj)) { if($ekomiObj->display_richsnippet) { if(Tools::getValue('id_product')) { $product = new Product(Tools::getValue('id_product'),true,$this->context->language->id); if(Validate::isLoadedObject($product)) { //We retrieve rating $this->context->smarty->assign(array( 'product' => $product, 'rating' => $ekomiObj->getRating($product->id,$this->context->language->id), 'count' => $ekomiObj->getCount(Tools::getValue('id_product'),$this->context->language->id), 'currencySign' => $this->context->currency->sign, 'currencyIso' => $this->context->currency->iso_code, 'pathStar' => $this->_path.'img/picto/'.$ekomiObj->picto.'/picto.gif', )); } } else $this->context->smarty->assign(array( 'display_reviews' => false, )); } else $this->context->smarty->assign(array( 'display_reviews' => false, )); if ($ekomiObj->display_block AND $ekomiObj->api_script[$this->context->language->id]) { $this->context->smarty->assign(array( 'badge' => stripslashes(html_entity_decode($ekomiObj->api_script[$this->context->language->id])).'

', )); } return $this->fetchTemplate('/views/templates/hooks/', 'badge'); } else return false; } public function hookDisplayFooter($params) { return $this->hookFooter($params); } public function hookLeftColumn($params) { return $this->hookFooter($params); } public function hookDisplayLeftColumn($params) { return $this->hookFooter($params); } public function hookRightColumn($params) { return $this->hookFooter($params); } public function hookDisplayRightColumn($params) { return $this->hookFooter($params); } public function hookUpdateOrderStatus($params) { $ekomiObj = EkomiObject::getByIdShop($this->context->shop->id); $orderState = $params['newOrderStatus']; if($ekomiObj->id_state) { if($ekomiObj->id_state == $orderState->id) { /* We send Product to update database Ekomi product*/ $order = new Order($params['id_order']); $products = $order->getProducts(); foreach ($products as $product) $ekomiObj->send_product($product,$order->id_lang, $this->context->link); $ekomiData = $ekomiObj->send_order($params['id_order'],$order->id_lang); if(isset($ekomiData['link'])) { //We insert order data to sending email via cronjob $ekomiObj->putEmailData($order->id,$order->id_customer,$ekomiData['link'],$order->id_lang); } } } return true; } public function hookActionOrderStatusUpdate($params) { return $this->hookUpdateOrderStatus($params); } public function hookProductTab($params) { $ekomiObj = EkomiObject::getByIdShop($this->context->shop->id); if($ekomiObj->display_reviews) { $this->context->smarty->assign(array( 'count' => $ekomiObj->getCount(Tools::getValue('id_product'),$this->context->language->id), )); return $this->fetchTemplate('/views/templates/hooks/', 'tab'); } else return false; } public function hookDisplayProductTab($params) { return $this->hookProductTab($params); } public function hookProductTabContent($params) { $ekomiObj = EkomiObject::getByIdShop($this->context->shop->id); if($ekomiObj->display_reviews) { /* Get product reviews*/ $reviews = $ekomiObj->getReviews(Tools::getValue('id_product'),$this->context->language->id); $rating = $ekomiObj->getRating(Tools::getValue('id_product'),$this->context->language->id); $this->context->smarty->assign(array( 'pathEkomi' => $this->_path, )); if($reviews) { $this->context->smarty->assign(array( 'eReviews' => $reviews, 'rating' => $rating, 'pathStar' => $this->_path.'img/picto/'.$ekomiObj->picto.'/picto.gif', )); } return $this->fetchTemplate('/views/templates/hooks/', 'productcomment'); } else return false; } public function hookDisplayProductTabContent($params) { return $this->hookProductTabContent($params); } public function cronSendEmail() { $ekomiObj = EkomiObject::getByIdShop($this->context->shop->id); foreach($ekomiObj->api_id as $key=>$value) { if (!file_exists(dirname(__FILE__.'/'.Language::getIsoById((int)$key).'/'))) di('error sending email, template does not exist'); $ekomiSettings = $ekomiObj->send_settings_request($key); if($ekomiSettings['done']) { $subject = utf8_encode($ekomiSettings['mail_subject']); /* We retrieve all order to send an ekomi email */ $orders = $ekomiObj->getOrders($ekomiSettings['mail_delay'], $key); if($orders) { foreach($orders as $order) { $customer = new Customer((int)$order['id_customer']); $notSending = Db::getInstance()->ExecuteS('SELECT `id_customer` FROM `'._DB_PREFIX_.'ekomi_unsubscribe` WHERE `id_customer` = '.(int)$customer->id.';'); if (!preg_match("/amazon/i", $customer->email) AND !preg_match("/ebay/i", $customer->email) AND !isset($notSending[0]['id_customer'])) { /* HTML email */ $htmlContent = str_replace('{nachname}', $customer->lastname, utf8_encode($ekomiSettings['mail_html'])); $htmlContent = str_replace('{vorname}', $customer->firstname.' ', $htmlContent); $htmlContent = str_replace('{ekomilink}', ''.$order['ekomi_link'].'', $htmlContent); /* TXT email */ $plainContent = str_replace('{nachname}', $customer->lastname, utf8_encode($ekomiSettings['mail_plain'])); $plainContent = str_replace('{vorname}', $customer->firstname.' ', $htmlContent); $plainContent = str_replace('{ekomilink}', $order['ekomi_link'], $htmlContent); if($order['ekomi_link']) { /* Email generation */ if (version_compare(_PS_VERSION_,'1.5','<')) { $templateVars = array( '{htmlContent}' => $htmlContent, '{plainContent}' => $plainContent, '{idcustomer}' => md5($customer->id), '{url}' => 'http://'.$_SERVER['HTTP_HOST'].'/modules/ekomi/ekomi-unsubscribe-backward.php', ); } else { $templateVars = array( '{htmlContent}' => $htmlContent, '{plainContent}' => $plainContent, '{idcustomer}' => md5($customer->id), '{url}' => 'http://'.$_SERVER['HTTP_HOST'].'/module/ekomi/unsubscribe', ); } /* Email sending */ if (!Mail::Send((int)$key, 'ekomi', $subject, $templateVars, $customer->email, NULL, Configuration::get('PS_SHOP_EMAIL'), Configuration::get('PS_SHOP_NAME'), NULL, NULL, dirname(__FILE__).'/mails/')) die('error to sending email'); /* We update database ekomi order send email field */ $orders = $ekomiObj->updateSendEmailOrder($order['id_order'],$order['id']); echo 'Email sent to order '.$order['id'].'
'; } } else continue; } } } } return true; } public function cronProductFeedBack() { $ekomiObj = EkomiObject::getByIdShop($this->context->shop->id); $ekomiObj->check_product_feedback(); return true; } }