bebeboutik/modules/trustedshopsbbb/trustedshopsbbb.php
2017-03-21 10:46:54 +01:00

255 lines
11 KiB
PHP

<?php
if (!defined('_PS_VERSION_'))
exit;
include_once (_PS_MODULE_DIR_.'trustedshopsbbb/lib/ReviewCollectorReview.php');
include_once (_PS_MODULE_DIR_.'trustedshopsbbb/lib/TrustedShopsCache.php');
include_once (_PS_MODULE_DIR_.'trustedshopsbbb/lib/ReviewIndicatorCollector.php');
include_once (_PS_MODULE_DIR_.'trustedshopsbbb/lib/TrustedShopsAPI.php');
include_once(_PS_MODULE_DIR_.'trustedshopsbbb/models/TrustedshopBbbAsync.php');
class TrustedShopsBbb extends Module
{
const TS_BEST_RATING = "5.00";
public function __construct()
{
$this->name = 'trustedshopsbbb';
$this->tab = 'payment_security';
$this->version = '1.0';
parent::__construct();
$this->displayName = $this->l('Trusted Shops BBB (specific)');
if ($this->id AND !Configuration::get('TRUSTED_SHOP_ID')) {
$this->warning = $this->l('You have not yet set your Trusted Shop ID');
}
$this->description = $this->l('Allows API calls when an order reach a specific order state');
$this->confirmUninstall = $this->l('Are you sure you want to delete all your settings?');
}
public function install()
{
if (!parent::install()) {
return FALSE;
}
if (!$this->registerHook('updateOrderStatus') ||
!$this->registerHook('footer') ||
!$this->registerHook('orderConfirmation')) {
return FALSE;
}
if (!$this->installDB()) {
return FALSE;
}
return TRUE;
}
private function installDB()
{
$result = true;
# Add tables
$query = '
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'trustedshop_bbb_async` (
`id_trustedshop_bbb_async` INTEGER NOT NULL AUTO_INCREMENT,
`id_order` INTEGER NOT NULL,
`processed` BOOLEAN NOT NULL DEFAULT FALSE,
`date_add` DATETIME NOT NULL,
`date_upd` DATETIME NOT NULL,
PRIMARY KEY(`id_trustedshop_bbb_async`),
KEY `trustedshop_bbb_o_index` (id_order)
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8
';
$result = (Db::getInstance()->Execute($query) and $result);
return $result;
}
public function uninstall()
{
if (!Configuration::deleteByName('TRUSTED_SHOP_FR_ID')) {
return FALSE;
}
if (!Configuration::deleteByName('TRUSTED_SHOP_ES_ID')) {
return FALSE;
}
if (!Configuration::deleteByName('TRUSTED_SHOP_STATUS_WATCHED')) {
return FALSE;
}
if (!Configuration::deleteByName('TRUSTED_SHOP_PASSWORD')) {
return FALSE;
}
if (!Configuration::deleteByName('TRUSTED_SHOP_USER')) {
return FALSE;
}
if (!Configuration::deleteByName('TRUSTED_SHOP_DAYS')) {
return FALSE;
}
return parent::uninstall();
}
public function getContent()
{
global $cookie;
$output = '<h2>Trusted Shop Configuration</h2>';
if (Tools::isSubmit('submitTrustedShop') AND ($tsfrid = Tools::getValue('trusted_shop_fr_id')) AND ($tsesid = Tools::getValue('trusted_shop_es_id')) AND ($tsdays = Tools::getValue('trusted_shop_days')) AND ($tssw = Tools::getValue('status_watched_id')) AND ($tspassword = Tools::getValue('trusted_shop_password')) AND ($tsuser = Tools::getValue('trusted_shop_user'))) {
Configuration::updateValue('TRUSTED_SHOP_FR_ID', trim($tsfrid));
Configuration::updateValue('TRUSTED_SHOP_ES_ID', trim($tsesid));
Configuration::updateValue('TRUSTED_SHOP_STATUS_WATCHED', $tssw);
Configuration::updateValue('TRUSTED_SHOP_PASSWORD', trim($tspassword));
Configuration::updateValue('TRUSTED_SHOP_USER', trim($tsuser));
Configuration::updateValue('TRUSTED_SHOP_DAYS', trim($tsdays));
$output .= '
<div class="conf confirm">
<img src="../img/admin/ok.gif" alt="" title="" />
' . $this->l('Settings updated') . '
</div>';
}
return $output.$this->displayForm((int)($cookie->id_lang));
}
public function displayForm($id_lang)
{
$states = OrderState::getOrderStates($id_lang);
$output = '
<form action="' . Tools::safeOutput($_SERVER['REQUEST_URI']) . '" method="post">
<fieldset class="width2">
<legend><img src="../img/admin/cog.gif" alt="" class="middle" />' . $this->l('Settings') . '</legend>
<label>' . $this->l('Your TSID FR') . '</label>
<div class="margin-form">
<input type="text" name="trusted_shop_fr_id" value="' . Tools::safeOutput(Tools::getValue('trusted_shop_fr_id', Configuration::get('TRUSTED_SHOP_FR_ID'))) . '" />
</div>
<label>' . $this->l('Your TSID ES') . '</label>
<div class="margin-form">
<input type="text" name="trusted_shop_es_id" value="' . Tools::safeOutput(Tools::getValue('trusted_shop_es_id', Configuration::get('TRUSTED_SHOP_ES_ID'))) . '" />
</div>
<label>' . $this->l('Your user name') . '</label>
<div class="margin-form">
<input type="text" name="trusted_shop_user" value="' . Tools::safeOutput(Tools::getValue('trusted_shop_user', Configuration::get('TRUSTED_SHOP_USER'))) . '" />
</div>
<label>' . $this->l('Your Password') . '</label>
<div class="margin-form">
<input type="text" name="trusted_shop_password" value="' . Tools::safeOutput(Tools::getValue('trusted_shop_password', Configuration::get('TRUSTED_SHOP_PASSWORD'))) . '" />
</div>
<label>' . $this->l('Days') . '</label>
<div class="margin-form">
<input type="text" name="trusted_shop_days" value="' . Tools::safeOutput(Tools::getValue('trusted_shop_days', Configuration::get('TRUSTED_SHOP_DAYS'))) . '" />
</div>
<div class="margin-form">
<select name="status_watched_id">';
foreach ($states AS $state) {
$output .= '<option value="' . $state['id_order_state'] . '"' . (($state['id_order_state'] == Configuration::get('TRUSTED_SHOP_STATUS_WATCHED')) ? ' selected="selected"' : '') . '>' . stripslashes($state['name']) . '</option>';
}
$output .= '</select>
<p class="clear">' . $this->l('The api call will be triggered when the order reach this status') . '</p>
</div>
<input type="submit" name="submitTrustedShop" value="' . $this->l('Update ID') . '" class="button" />
</fieldset>
</form>';
return $output;
}
public function hookUpdateOrderStatus($params)
{
if (isset($params['newOrderStatus'])
&& Validate::isUnsignedId($params['newOrderStatus']->id)
&& FALSE !== Configuration::get('TRUSTED_SHOP_STATUS_WATCHED')
&& (int)Configuration::get('TRUSTED_SHOP_STATUS_WATCHED') == (int)$params['newOrderStatus']->id
){
$order = new Order((int)$params['id_order']);
if (in_array(strtoupper(Language::getIsoById($order->id_lang)), array("FR","ES"), true)) {
TrustedShopBbbAsync::saveOrUpdate((int)$order->id);
}
}
}
public function hookLeftColumn($params){
if(_PS_MOBILE_) {
return false;
}
global $smarty, $cookie;
if($smarty->tpl_vars['page_name']->value == 'order') {
$filename = _PS_ROOT_DIR_.'/modules/trustedshopsbbb/views/widget'.$cookie->id_lang.'.txt';
$expire = time() -3600;
if(file_exists($filename) && filemtime($filename) > $expire) {
$data = file_get_contents($filename);
$widget = json_decode($data, TRUE);
} else {
$file = fopen($filename, 'w+');
$widget = $this->buildWidget($cookie->id_lang);
fwrite($file, json_encode($widget));
fclose($file);
}
$smarty->assign(array(
'widget' => $widget,
));
return $this->display(__FILE__, 'views/widget_leftcolumn.tpl');
}
}
public function hookFooter($params){
global $smarty, $cookie;
if($smarty->tpl_vars['page_name']->value == 'order') {
return false;
}
$filename = _PS_ROOT_DIR_.'/modules/trustedshopsbbb/views/widget_'.$cookie->id_lang.'.txt';
$expire = time() -3600;
if(file_exists($filename) && filemtime($filename) > $expire) {
$data = file_get_contents($filename);
$widget = json_decode($data, TRUE);
} else {
$file = fopen($filename, 'w+');
$widget = $this->buildWidget($cookie->id_lang);
fwrite($file, json_encode($widget));
fclose($file);
}
$smarty->assign(array(
'widget' => $widget,
));
return $this->display(__FILE__, 'views/widget.tpl');
}
public function hookOrderConfirmation($params)
{
global $smarty, $cookie;
if (false && in_array(strtoupper(Language::getIsoById($cookie->id_lang)), array("FR","ES"), true)) {
$tsId = Configuration::get('TRUSTED_SHOP_' . strtoupper(Language::getIsoById((int)$cookie->id_lang)) . '_ID');
$smarty->assign(array('tsId' => $tsId,));
return $this->display(__FILE__, 'views/trust_badge_order_confirmation.tpl');
}else{
return false;
}
}
public function buildWidget($id_lang){
$resultArray = array();
if (in_array(strtoupper(Language::getIsoById((int)$id_lang)), array("FR","ES"), true)) {
$tsId = Configuration::get('TRUSTED_SHOP_' . strtoupper(Language::getIsoById((int)$id_lang)) . '_ID');
$reviewIndicatorCollector = new ReviewIndicatorCollector((int)($id_lang));
$resultArray = $reviewIndicatorCollector->getResults();
}
if(count($resultArray)> 0 && (int)$resultArray['count'] > 0){
return array(
'shopName' => $resultArray['shop_name'],
'result' => $resultArray['result'],
'max' => self::TS_BEST_RATING,
'count' => $resultArray['count'],
'resultArray' => $resultArray,
'tsId' => $tsId
);
}
return $resultArray;
}
private function isTemplateCached(&$cache_id)
{
global $smarty;
Tools::enableCache();
$smarty->cache_lifetime = 0;
return $this->isCached('rich_snippets.tpl', $cache_id);
}
private function flushTemplateCache($cache_id)
{
global $smarty;
$display = $this->display(__FILE__, 'views/rich_snippets.tpl', $cache_id);
Tools::restoreCacheSettings();
$smarty->cache_lifetime = -1;
return $display;
}
}