2017-08-29 10:46:12 +02:00

304 lines
11 KiB
PHP

<?php
/**
* Module Ekomi - Main file
*
* @category Advertising and Marketing
* @author Web in Color <addons@webincolor.fr>
* @copyright 2013 WebInColor
* @version 1.0
* @link http://www.webincolor.fr/
* @since File available since Release 1.0
*/
// eKomi Version // Please start with "cust-" followed by you own version number. max 10 chars.
define( 'EKOMI_VERSION', 'cust-1.0.0');
define( 'EKOMI_API', 'http://api.ekomi.de/v2/wsdl');
class EkomiObject extends ObjectModel{
/** @var integer ekomi id*/
public $id;
/** @var integer id shop*/
public $id_shop;
/** @var string api_id*/
public $api_id;
/** @var string api_key*/
public $api_key;
/** @var string api_script*/
public $api_script;
/** @var integer display block*/
public $display_block;
/** @var integer display reviews*/
public $display_reviews;
/** @var integer display rich snippet*/
public $display_richsnippet;
/** @var string picto*/
public $picto;
/** @var integer sending*/
public $sending;
/** @var string range*/
public $range;
/** @var string hook*/
public $hook;
/** @var integer sending*/
public $id_state;
/**
* @see ObjectModel::$definition
*/
public static $definition = array(
'table' => 'ekomi',
'primary' => 'id_ekomi',
'multilang' => true,
'fields' => array(
'id_shop' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt', 'required' => true),
'display_block' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'),
'display_reviews' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'),
'display_richsnippet' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'),
'picto' => array('type' => self::TYPE_STRING, 'validate' => 'isString'),
'sending' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt'),
'range' => array('type' => self::TYPE_STRING, 'validate' => 'isString'),
'hook' => array('type' => self::TYPE_STRING, 'validate' => 'isString', 'required' => true),
'id_state' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt', 'required' => true),
// Lang fields
'api_id' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isString'),
'api_key' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isString'),
'api_script' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isString'),
)
);
static public function getByIdShop($id_shop)
{
$id = Db::getInstance()->getValue('SELECT `id_ekomi` FROM `'._DB_PREFIX_.'ekomi` WHERE `id_shop` ='.(int)$id_shop);
if ($id)
return new EkomiObject((int)$id);
else
return false;
}
public function getConfigurationByIdLand()
{
$result = Db::getInstance()->ExecuteS('SELECT `api_id`,`api_key` FROM `'._DB_PREFIX_.'ekomi_lang` WHERE `id_shop` ='.(int)$this->id_shop.' AND `id_lang` = '.$this->context->language->id);
return result;
}
public function copyFromPost()
{
/* Classical fields */
foreach ($_POST AS $key => $value)
if (key_exists($key, $this) AND $key != 'id_'.$this->table)
$this->{$key} = $value;
/* Multilingual fields */
if (sizeof($this->fieldsValidateLang))
{
$languages = Language::getLanguages(false);
foreach ($languages AS $language)
foreach ($this->fieldsValidateLang AS $field => $validation)
if (isset($_POST[$field.'_'.(int)($language['id_lang'])]))
$this->{$field}[(int)($language['id_lang'])] = $_POST[$field.'_'.(int)($language['id_lang'])];
}
}
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.5) return 1.5;
return 1.3;
}
public function send_product( $product, $id_lang, $link ) {
$productObj = new Product(intval($product['product_id']), true);
$imageObj = new Image($product['image']->id_image);
if (Validate::isLoadedObject($productObj))
{
$productData = array(
'product_id' => $productObj->id,
'product_name' => $productObj->name[(int)Configuration::get('PS_LANG_DEFAULT')],
'product_other' => array(
'image_url'=> (Configuration::get('PS_SSL_ENABLED') ? 'https://'.$_SERVER['HTTP_HOST']._THEME_PROD_DIR_.$imageObj->getExistingImgPath().'-'.($this->getVersion() > 1.4 ? 'thickbox_default':'thickbox').'.jpg' : ''), // HTTPS(!!!), max. 150x150px (for ekomi review page)
'brand_name'=>$productObj->manufacturer_name, // Brand name (for google products)
'product_ids'=>array( // (for google products)
'mpn'=>'', // Manufacturer's Part Number
'upc'=>$productObj->upc, // Universal Product Code
'ean'=>$productObj->ean13, // European Article Number
'isbn'=>'', // International Standard Book Number
'gbase'=>'' // Google BaseID
), // Product IDs
'links'=>array(
array('rel'=>'canonical', 'type'=>'text/html', 'href'=>$link->getProductLink(intval($productObj->id), $productObj->link_rewrite[(int)Configuration::get('PS_LANG_DEFAULT')], $productObj->ean13)), // Link to product (for google products)
array('rel'=>'related', 'type'=>'image/(gif|jpg)', 'href'=>Tools::getProtocol().$_SERVER['HTTP_HOST']._THEME_PROD_DIR_.$imageObj->getExistingImgPath().'-'.($this->getVersion() > 1.4 ? 'thickbox_default':'thickbox').'.jpg') // Product image, can occur several times (for google products) // PLEASE MAKE SURE TO SET CORRECT FILE TYPE (image/gif, image/jpg etc.)
), // ProductLinks
),
);
}
else
return false;
$wsdl = new SoapClient(EKOMI_API, array('exceptions' => 0));
$send_product = $wsdl->putProduct($this->api_id[$id_lang].'|'.$this->api_key[$id_lang], EKOMI_VERSION, utf8_encode($productData['product_id']), utf8_encode($productData['product_name']), utf8_encode(serialize($productData['product_other'])));
$ret = unserialize( utf8_decode( $send_product ) );
return $ret;
}
public function send_order( $order_id, $id_lang ) {
$order = new Order((int)$order_id);
if($this->sending)
{
if (Validate::isLoadedObject($order))
$products = $order->getProducts();
else
return false;
if(isset($products))
{
$product_ids = '';
foreach($products as $product)
{
$product_ids .= $product['product_id'].',';
}
$product_ids = substr($product_ids, 0, -1);
}
}
else
$product_ids = '';
$wsdl = new SoapClient(EKOMI_API, array('exceptions' => 0));
$send_order = $wsdl->putOrder($this->api_id[$id_lang].'|'.$this->api_key[$id_lang], EKOMI_VERSION, $order_id, $product_ids);
$ret = unserialize( utf8_decode( $send_order ) );
return $ret;
}
public function putEmailData ( $order_id, $customer_id, $link, $id_lang)
{
$customer = new Customer((int)$customer_id);
//We insert data in cron tab Job
if (Validate::isLoadedObject($customer))
{
$sql = 'INSERT INTO `'._DB_PREFIX_.'wic_ekomi_order_email` (`id_order`, `id_customer`, `ekomi_link`, `date_add`, `id`, `id_shop`, `id_lang`) VALUES ('.(int)$order_id.', '.(int)$customer->id.', \''.$link.'\', NOW(), NULL, '.$this->id_shop.', '.(int)$id_lang.');';
Db::getInstance()->Execute($sql);
}
}
public function send_settings_request($id_lang)
{
$wsdl = new SoapClient(EKOMI_API, array('exceptions' => 0));
$send_settings_request = $wsdl->getSettings($this->api_id[$id_lang].'|'.$this->api_key[$id_lang], EKOMI_VERSION);
$ret = unserialize( utf8_decode( $send_settings_request ) );
return $ret;
}
public function getOrders($delay, $id_lang)
{
$sql = 'SELECT * FROM `'._DB_PREFIX_.'wic_ekomi_order_email` WHERE DATE_SUB(NOW(),INTERVAL '.(int)$delay.' DAY) >= `date_add` AND `date_send` IS NULL AND `id_lang` = '.(int)$id_lang.' AND `id_shop` = '.$this->id_shop;
$ret = Db::getInstance()->Executes($sql);
if($ret)
return $ret;
else
return false;
}
public function updateSendEmailOrder ( $order_id, $ekomi_send_order_id)
{
$sql = 'UPDATE `'._DB_PREFIX_.'wic_ekomi_order_email` SET `date_send` = NOW() WHERE `id` = '.$ekomi_send_order_id.' AND `id_order` = '.$order_id.';';
Db::getInstance()->Execute($sql);
}
/*
* Access the eKomi API to check for new feedback
*
* You can find your eKomi API inteface ID and interface password in your
* eKomi customer area on the right hand side.
*/
public function check_product_feedback()
{
Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'wic_ekomi_product_reviews` WHERE `id_shop` = '.$this->id_shop);
foreach($this->api_id as $key=>$value)
{
$url = 'http://api.ekomi.de/get_productfeedback.php?interface_id='.$value.'&interface_pw='.$this->api_key[$key].'&version='.EKOMI_VERSION.'&type=csv'.($this->range ? '&range='.$this->range : '').'&charset=utf-8';
if (($handle = fopen($url, "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 2048, ",")) !== FALSE)
{
if(isset($data[0]))
{
$sql = 'INSERT INTO `'._DB_PREFIX_.'wic_ekomi_product_reviews`
(`date_add`,`order_id`,`product_id`,`stars`,`review`,`id_shop`,`id_lang` )
VALUES
(\'' . date('Y-m-d H:i:s',$data[0]) .'\',
\'' . pSql($data[1]) . '\',
\'' . pSql($data[2]) . '\',
\'' . pSql($data[3]) . '\',
\'' . pSql($data[4]) . '\',
\'' . $this->id_shop . '\',
\'' . (int)$key . '\'
);';
Db::getInstance()->Execute($sql);
}
}
fclose($handle);
}
}
return true;
}
public function getReviews($id_product,$id_lang)
{
$sql= 'SELECT * FROM `'._DB_PREFIX_.'wic_ekomi_product_reviews` WHERE `product_id` = '.(int)$id_product.' AND `id_shop` ='.(int)$this->id_shop.' AND `id_lang` = '.(int)$id_lang;
$ret = Db::getInstance()->ExecuteS($sql);
if($ret)
return $ret;
return false;
}
public function getRating($id_product,$id_lang)
{
$sql= 'SELECT AVG( `stars` ) AS `avg` FROM `'._DB_PREFIX_.'wic_ekomi_product_reviews` WHERE `product_id` = '.(int)$id_product.' AND `id_shop` ='.(int)$this->id_shop.' AND `id_lang` = '.(int)$id_lang;
$ret = Db::getInstance()->getValue($sql);
if($ret)
return $ret;
return false;
}
public function getCount($id_product,$id_lang)
{
$sql= 'SELECT COUNT( `id` ) FROM `'._DB_PREFIX_.'wic_ekomi_product_reviews` WHERE `product_id` = '.(int)$id_product.' AND `id_shop` ='.(int)$this->id_shop.' AND `id_lang` = '.(int)$id_lang;
$ret = Db::getInstance()->getValue($sql);
if($ret)
return $ret;
return 0;
}
}
?>