* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 7654 $
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
include(_PS_MODULE_DIR_.'trustedshops/lib/TrustedShopsSoapApi.php');
include(_PS_MODULE_DIR_.'trustedshops/lib/WidgetCache.php');
include(_PS_MODULE_DIR_.'trustedshops/lib/RatingAlert.php');
class TrustedShopsRating extends AbsTrustedShops
{
const APPLY_URL = 'https://www.trustedshops.com/buyerrating/signup.html';
const PARTNER_PACKAGE = 'presta';
const SHOP_SW = 'PrestaShop';
private $allowed_languages = array();
private $available_languages = array('en', 'fr', 'de');
private $rating_url_base = array('en' => 'https://www.trustedshops.com/buyerrating/rate_',
'de' => 'https://www.trustedshops.com/bewertung/bewerten_',
'fr' => 'https://www.trustedshops.com/evaluation/evaluer_');
private $apply_url_base = array('en' => 'https://www.trustedshops.com/buyerrating/signup.html',
'de' => 'https://www.trustedshops.com/bewertung/anmeldung.html',
'fr' => 'https://www.trustedshops.com/evaluation/inscription.html');
private $apply_url_tracker = array('en' => '&et_cid=53&et_lid=3361',
'de' => '',
'fr' => '&et_cid=53&et_lid=3362');
public function __construct()
{
$this->tab_name = $this->l('Trusted Shops customer rating');
// @todo : That gonna be change - Be worry it's false, countries have not the same ISO code as Languages, waiting Truste Shop's answer
$this->limited_countries = $this->available_languages;
}
public function install()
{
foreach ($this->available_languages AS $language)
{
Configuration::updateValue('TS_TAB0_ID_'.(int)(Language::getIdByIso($language)), '');
Configuration::updateValue('TS_TAB0_ID_ACTIVE_'.(int)(Language::getIdByIso($language)), '');
}
Configuration::updateValue('TS_TAB0_DISPLAY_IN_SHOP', '');
Configuration::updateValue('TS_TAB0_DISPLAY_RATING_FRONT_END', '');
Configuration::updateValue('TS_TAB0_DISPLAY_RATING_OC', '');
Configuration::updateValue('TS_TAB0_SEND_RATING', '');
Configuration::updateValue('TS_TAB0_SEND_SEPERATE_MAIL', '');
Configuration::updateValue('TS_TAB0_SEND_SEPERATE_MAIL_DELAY', '');
return (RatingAlert::createTable() AND Configuration::updateValue('PS_TS_TAB0_SECURE_KEY', strtoupper(Tools::passwdGen(16))));
}
public function uninstall()
{
foreach ($this->available_languages AS $language)
{
Configuration::deleteByName('TS_TAB0_ID_'.(int)(Language::getIdByIso($language)));
Configuration::deleteByName('TS_TAB0_ID_ACTIVE_'.(int)(Language::getIdByIso($language)));
}
Configuration::deleteByName('TS_TAB0_DISPLAY_IN_SHOP');
Configuration::deleteByName('TS_TAB0_DISPLAY_RATING_FRONT_END');
Configuration::deleteByName('TS_TAB0_DISPLAY_RATING_OC');
Configuration::deleteByName('TS_TAB0_SEND_RATING');
Configuration::deleteByName('TS_TAB0_SEND_SEPERATE_MAIL');
Configuration::deleteByName('TS_TAB0_SEND_SEPERATE_MAIL_DELAY');
Configuration::deleteByName('PS_TS_TAB0_SECURE_KEY');
return (RatingAlert::dropTable());
}
private function _initAllowedLanguages()
{
$languages = Language::getLanguages();
foreach ($languages AS $key => $language)
{
if (in_array($language['iso_code'], $this->available_languages))
$this->allowed_languages[] = $languages[$key];
}
}
public static function getHttpHost($http = false, $entities = false)
{
if (method_exists('Tools', 'getHttpHost'))
return call_user_func(array('Tools', 'getHttpHost'), array($http, $entities));
$host = (isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST']);
if ($entities)
$host = htmlspecialchars($host, ENT_COMPAT, 'UTF-8');
if ($http)
$host = (Configuration::get('PS_SSL_ENABLED') ? 'https://' : 'http://').$host;
return $host;
}
private function _isTsIdActive($id_lang, $ts_id = NULL)
{
if (is_null($ts_id))
$ts_id = Configuration::get('TS_TAB0_ID_'.(int)($id_lang));
return (!empty($ts_id) AND ($ts_id == Configuration::get('TS_TAB0_ID_ACTIVE_'.$id_lang)));
}
// Return true if at least one TS_ID is active
private function _isConfigurable()
{
foreach ($this->allowed_languages AS $language)
if ($this->_isTsIdActive($language['id_lang']))
return true;
return false;
}
private function _getLastOrderId($id_customer)
{
return (int)(Db::getInstance()->getValue('
SELECT `id_order`
FROM `'._DB_PREFIX_.'orders`
WHERE `id_customer` = '.(int)($id_customer).'
ORDER BY `date_add` DESC'));
}
private function _getAllowedIsobyId($id_lang)
{
$lang = Language::getIsoById($id_lang);
$lang = in_array($lang, $this->available_languages) ? $lang : 'en';
return $lang;
}
public function getContent()
{
$this->_initAllowedLanguages();
$out = '';
if (is_writable(_PS_MODULE_DIR_.'trustedshops/cache') === FALSE)
$this->errors[] = $this->l('This module requires write and read permissions on the module cache directory.');
if (Tools::isSubmit('submitTrustedShops'))
{
$this->_validateForm();
if (empty($this->errors))
$this->_postProcess();
}
$out .= $this->displayInformationsPage();
$out .= $this->displayForm();
return $out;
}
private function _validateForm()
{
if (!extension_loaded('soap'))
{
$this->errors[] = $this->l('This module requires the SOAP PHP extension to function properly.');
return false;
}
$flag_return = true;
foreach ($this->allowed_languages AS $language)
{
$ts_id = Tools::getValue('trusted_shops_id_'.(int)($language['id_lang']));
if (!empty($ts_id))
{
if (!preg_match('/^[[:alnum:]]{33}$/', $ts_id))
{
$this->errors[] = $this->l('Invalid Trusted Shops ID').' ['.$language['iso_code'].']';
$flag_return = false;
}
elseif (!$this->_isTsIdActive((int)($language['id_lang']), $ts_id))
{
$error = $this->_validateTrustedShopId($ts_id, (int)($language['id_lang']));
if ($error != '') $this->errors[] = $error;
$flag_return = false;
}
}
}
if (Tools::getValue('send_seperate_mail') AND !Validate::isUnsignedInt(Tools::getValue('send_seperate_mail_delay')))
{
$this->errors[] = $this->l('Invalid delay');
$flag_return = false;
}
return $flag_return;
}
private function _validateTrustedShopId($ts_id, $iso_lang)
{
$error = '';
$result = TrustedShopsSoapApi::validate(self::PARTNER_PACKAGE, $ts_id);
if ($result != TrustedShopsSoapApi::RT_OK)
{
switch($result)
{
case TrustedShopsSoapApi::RT_INVALID_TSID:
$error = $this->l('Invalid Trusted Shops ID').' ['.Language::getIsoById($iso_lang).']. '.$this->l('Please register').' ' .$this->l('here').' '. $this->l('or contact service@trustedshops.co.uk.');
break;
case TrustedShopsSoapApi::RT_NOT_REGISTERED:
$error = $this->l('Customer Rating has not yet been activated for this Trusted Shops ID').' ['.Language::getIsoById($iso_lang).']. '.$this->l('Please register').' ' .$this->l('here').' '. $this->l('or contact service@trustedshops.co.uk.');
break;
default:
$error = $this->l('An error has occurred');
}
}
return empty($error) ? '' : $error;
}
private function _postProcess()
{
Configuration::updateValue('TS_TAB0_DISPLAY_IN_SHOP', (int)(Tools::getValue('display_in_shop')));
Configuration::updateValue('TS_TAB0_DISPLAY_RATING_FRONT_END', (int)(Tools::getValue('display_rating_front_end')));
Configuration::updateValue('TS_TAB0_DISPLAY_RATING_OC', (int)(Tools::getValue('display_rating_order_confirmation')));
Configuration::updateValue('TS_TAB0_SEND_RATING', (int)(Tools::getValue('send_rating')));
Configuration::updateValue('TS_TAB0_SEND_SEPERATE_MAIL', (int)(Tools::getValue('send_seperate_mail')));
foreach ($this->allowed_languages AS $language)
{
$ts_id = Tools::getValue('trusted_shops_id_'.(int)($language['id_lang']));
Configuration::updateValue('TS_TAB0_ID_'.(int)($language['id_lang']), $ts_id);
if (!empty($ts_id))
Configuration::updateValue('TS_TAB0_ID_ACTIVE_'.(int)($language['id_lang']), $ts_id);
}
if (Configuration::get('TS_TAB0_SEND_SEPERATE_MAIL'))
Configuration::updateValue('TS_TAB0_SEND_SEPERATE_MAIL_DELAY', (int)(Tools::getValue('send_seperate_mail_delay')));
else
RatingAlert::truncateTable();
$params = '';
$delim = '?';
$dataSync = '';
$key = 1;
foreach($this->allowed_languages AS $language)
{
if ($this->_isTsIdActive($language['id_lang']))
{
$params .= $delim.'lang'.$key.'='.$language['iso_code'].'&ts_id'.$key.'='.Configuration::get('TS_TAB0_ID_ACTIVE_'.$language['id_lang']);
$key++;
$delim = '&';
}
}
if (!empty($params))
$dataSync = '';
$this->confirmations[] = $this->l('Settings updated').$dataSync;
return true;
}
public function displayForm()
{
// I18N TS_ID
$i18n_ts_id_fields = '';
foreach ($this->allowed_languages AS $key => $language)
{
$i18n_ts_id_fields .= '
'.strtoupper($language['iso_code']).'
'.($this->_isTsIdActive($language['id_lang']) ? $this->l('Active') : $this->l('Inactive unless you haven\'t specified your Trusted Shops ID')).'
Jetzt bei Trusted Shops anmelden!