347 lines
14 KiB
PHP
347 lines
14 KiB
PHP
<?php
|
|
/**
|
|
* Please read the terms of the CLUF license attached to this module(cf "licences" folder)
|
|
*
|
|
* @author Línea Gráfica E.C.E. S.L.
|
|
* @copyright Lineagrafica.es - Línea Gráfica E.C.E. S.L. all rights reserved.
|
|
* @license https://www.lineagrafica.es/licenses/license_en.pdf
|
|
* https://www.lineagrafica.es/licenses/license_es.pdf
|
|
* https://www.lineagrafica.es/licenses/license_fr.pdf
|
|
*/
|
|
|
|
class LgCommentsCronModuleFrontController extends ModuleFrontController
|
|
{
|
|
const ERROR = 0;
|
|
|
|
public function postProcess()
|
|
{
|
|
$secure_key_get = pSQL(Tools::getValue('secureKey'));
|
|
$secure_key = md5(_COOKIE_KEY_ . Configuration::get('PS_SHOP_NAME'));
|
|
|
|
if ($secure_key === $secure_key_get) {
|
|
$this->executeCron();
|
|
}
|
|
}
|
|
|
|
private function executeCron()
|
|
{
|
|
$module = new LGComments();
|
|
$orders = $this->getOrders();
|
|
$sended = 0;
|
|
$orders_mail = '';
|
|
$orders_screen = array();
|
|
|
|
if (count($orders) > 0) {
|
|
foreach ($orders as $order) {
|
|
$preproducts = $this->getCartProducts((int)$order['id_cart']);
|
|
$subject = Configuration::get('PS_LGCOMMENTS_SUBJECTen');
|
|
$products = '';
|
|
|
|
foreach ($preproducts as $preproduct) {
|
|
$product_name = Product::getProductName(
|
|
(int)$preproduct['id_product'],
|
|
(int)$preproduct['id_product_attribute']
|
|
);
|
|
$products .= 'product:' . $preproduct['id_product'] . '|' . $product_name . nl2br("\n") . "\n";
|
|
}
|
|
$customer_data = $this->getCustomerData((int)$order['id_customer']);
|
|
$hash = $this->getHash($order['id_order']);
|
|
$link = Context::getContext()->link->getModuleLink(
|
|
'lgcomments',
|
|
'account',
|
|
array('id_order' => $order['id_order'], 'lghash' => $hash),
|
|
null,
|
|
$order['id_lang'],
|
|
$order['id_shop'],
|
|
false
|
|
);
|
|
|
|
$template_vars = array(
|
|
'{firstname}' => $customer_data['firstname'],
|
|
'{lastname}' => $customer_data['lastname'],
|
|
'{storename}' => Configuration::get('PS_SHOP_NAME'),
|
|
'{email}' => $customer_data['email'],
|
|
'{id_order}' => $order['id_order'],
|
|
'{link}' => $link,
|
|
'{productos}' => $products
|
|
);
|
|
|
|
$langs = Language::getLanguages();
|
|
foreach ($langs as $lang) {
|
|
if ($order['id_lang'] == $lang['id_lang']) {
|
|
$subject = Configuration::get('PS_LGCOMMENTS_SUBJECT' . $lang['iso_code']);
|
|
}
|
|
}
|
|
|
|
// Check if email template exists for current iso code. If not, use English template.
|
|
$module_path = _PS_MODULE_DIR_ . 'lgcomments/mails/';
|
|
$template_path = _PS_THEME_DIR_ .
|
|
'modules/lgcomments/mails/' . Language::getIsoById($order['id_lang']) . '/';
|
|
|
|
if (is_dir($module_path) || is_dir($template_path)) {
|
|
$id_lang = $order['id_lang'];
|
|
$mail_dir = $module_path;
|
|
} else {
|
|
$id_lang = (int)Language::getIdByIso('en');
|
|
$mail_dir = _PS_MODULE_DIR_ . 'lgcomments/mails/en/';
|
|
}
|
|
|
|
if (!$this->existOrderComment((int)$order['id_order'])) {
|
|
if (Mail::Send(
|
|
$id_lang,
|
|
'opinion-request',
|
|
$subject,
|
|
$template_vars,
|
|
$customer_data['email'],
|
|
null,
|
|
null,
|
|
Configuration::get('PS_SHOP_NAME'),
|
|
null,
|
|
null,
|
|
$mail_dir
|
|
)
|
|
) {
|
|
$this->saveCommentOrder($order['id_order'], $order['id_customer'], $hash);
|
|
|
|
$sended++;
|
|
$orders_mail .= $order['id_order'] . ', ';
|
|
$orders_screen['once'] = $module->l('Order') . ' #' . $order['id_order'] . ': ' .
|
|
$module->l('Email sent (first time)');
|
|
} else {
|
|
$orders_screen['send_error'] = $module->l('Order') . ' #' . $order['id_order'] . ': ' .
|
|
$module->l('Email not sent: problem with your email configuration');
|
|
}
|
|
} elseif ($this->needSendAgain((int)$order['id_order'])) {
|
|
if (Mail::Send(
|
|
$id_lang,
|
|
'opinion-request',
|
|
$subject,
|
|
$template_vars,
|
|
$customer_data['email'],
|
|
null,
|
|
null,
|
|
Configuration::get('PS_SHOP_NAME'),
|
|
null,
|
|
null,
|
|
$mail_dir
|
|
)
|
|
) {
|
|
$this->markOrderAsSendTwice($order['id_order']);
|
|
$sended++;
|
|
$orders_mail .= $order['id_order'] . ', ';
|
|
$orders_screen['twice'] = $module->l('Order') . ' #' . $order['id_order'] . ': ' .
|
|
$module->l('Email sent (second time)');
|
|
} else {
|
|
$orders_screen['send_error'] = $module->l('Order') . ' #' . $order['id_order'] . ': ' .
|
|
$module->l('Email not sent: problem with your email configuration');
|
|
}
|
|
} else {
|
|
$order_already_sended = $module->l('Order') . ' #' . $order['id_order'] . ': ' .
|
|
$module->l('Email already send');
|
|
|
|
if ($order['date_email2'] != '0000-00-00 00:00:00') {
|
|
$order_already_sended .= ' - ' .
|
|
date("d/m/Y H:i", strtotime($order['date_email2'])) . '';
|
|
}
|
|
$orders_screen['already_sended'] = $order_already_sended;
|
|
}
|
|
}
|
|
|
|
/** Email confirmación cron */
|
|
|
|
$email_cron = Configuration::get('PS_LGCOMMENTS_EMAIL_CRON');
|
|
$email_alerts = Configuration::get('PS_LGCOMMENTS_EMAIL_ALERTS');
|
|
|
|
if ($sended && $email_cron && $email_alerts == 1) {
|
|
$template_vars = array(
|
|
'{pedidos}' => $orders_mail
|
|
);
|
|
|
|
// Check if email template exists for current iso code. If not, use English template.
|
|
$id_lang_default = Configuration::get('PS_LANG_DEFAULT');
|
|
$module_path = _PS_MODULE_DIR_ . 'lgcomments/mails/' . Language::getIsoById($id_lang_default) . '/';
|
|
$template_path = _PS_THEME_DIR_ .
|
|
'modules/lgcomments/mails/' . Language::getIsoById($id_lang_default) . '/';
|
|
|
|
if (is_dir($module_path) || is_dir($template_path)) {
|
|
$id_lang = $id_lang_default;
|
|
$mail_dir = $module_path;
|
|
} else {
|
|
$id_lang = (int)Language::getIdByIso('en');
|
|
$mail_dir = _PS_MODULE_DIR_ . 'lgcomments/mails/en/';
|
|
}
|
|
|
|
Mail::Send(
|
|
$id_lang,
|
|
'cron-confirmation',
|
|
Configuration::get('PS_LGCOMMENTS_SUBJECT_CRON'),
|
|
$template_vars,
|
|
$email_cron,
|
|
null,
|
|
null,
|
|
Configuration::get('PS_SHOP_NAME'),
|
|
null,
|
|
null,
|
|
$mail_dir
|
|
);
|
|
}
|
|
} else {
|
|
$orders_screen['no_orders'][0] = $module->l('No email sent:') .
|
|
$module->l('you don\'t have any order that corresponds to the selected criteria.') .
|
|
$module->l('Please modify your settings and expand your range of selection.');
|
|
}
|
|
|
|
if (version_compare(_PS_VERSION_, '1.7', '<')) {
|
|
echo Tools::jsonEncode($orders_screen);
|
|
} else {
|
|
json_decode($orders_screen); // Tools::jsonEncode Deprecated on Prestashop 1.7
|
|
}
|
|
die();
|
|
}
|
|
|
|
private function getCartProducts($id_cart)
|
|
{
|
|
$sql = 'SELECT `id_product`, `id_product_attribute`, `quantity`
|
|
FROM `' . _DB_PREFIX_ . 'cart_product`
|
|
WHERE `id_cart` = ' . (int)$id_cart;
|
|
|
|
return Db::getInstance()->executeS($sql);
|
|
}
|
|
|
|
private function getCustomerData($id_customer)
|
|
{
|
|
$sql = 'SELECT `firstname`, `lastname`, `email`
|
|
FROM `' . _DB_PREFIX_ . 'customer`
|
|
WHERE id_customer = ' . (int)$id_customer;
|
|
|
|
return Db::getInstance()->getRow($sql);
|
|
}
|
|
|
|
private function getHash($id_order)
|
|
{
|
|
$hash = $this->getOrderHash($id_order);
|
|
if (!$hash) {
|
|
$hash = $this->generateHash();
|
|
}
|
|
return $hash;
|
|
}
|
|
|
|
private function generateHash()
|
|
{
|
|
$number_range = array('min' => 48, 'max' => 57);
|
|
$upper_range = array('min' => 65, 'max' => 90);
|
|
$lower_range = array('min' => 97, 'max' => 122);
|
|
$hash = '';
|
|
|
|
for ($i = 0; $i < 59; $i++) {
|
|
switch (rand(0, 2)) {
|
|
case 0:
|
|
$hash .= chr(rand($number_range['min'], $number_range['max']));
|
|
break;
|
|
case 1:
|
|
$hash .= chr(rand($upper_range['min'], $upper_range['max']));
|
|
break;
|
|
case 2:
|
|
$hash .= chr(rand($lower_range['min'], $lower_range['max']));
|
|
break;
|
|
}
|
|
}
|
|
return $hash;
|
|
}
|
|
|
|
private function getOrderHash($id_order)
|
|
{
|
|
$sql = 'SELECT hash
|
|
FROM `' . _DB_PREFIX_ . 'lgcomments_orders`
|
|
WHERE id_order = ' . (int)$id_order;
|
|
|
|
return Db::getInstance()->getValue($sql);
|
|
}
|
|
|
|
private function getOrders()
|
|
{
|
|
$days = Configuration::get('PS_LGCOMMENTS_DIAS');
|
|
$days2 = Configuration::get('PS_LGCOMMENTS_DIAS2');
|
|
|
|
if (Configuration::get('PS_LGCOMMENTS_BOXES') == 2) {
|
|
$boxes_checked = 'AND c.newsletter = 1 ';
|
|
} elseif (Configuration::get('PS_LGCOMMENTS_BOXES') == 3) {
|
|
$boxes_checked = 'AND c.optin = 1 ';
|
|
} elseif (Configuration::get('PS_LGCOMMENTS_BOXES') == 4) {
|
|
$boxes_checked = 'AND c.newsletter = 1 AND c.optin = 1 ';
|
|
} else {
|
|
$boxes_checked = '';
|
|
}
|
|
|
|
if ($days2 < 1) {
|
|
$where_days2 = 'AND o.`date_add` <= DATE_SUB(NOW(), INTERVAL ' . (int)$days2 . ' DAY)';
|
|
} else {
|
|
$where_days2 = '';
|
|
}
|
|
|
|
$sql = 'SELECT DISTINCT o.`id_order`, o.`id_customer`, o.`id_cart`, o.`id_lang`,
|
|
o.`id_shop`, lo.`date_email`, lo.`sent`, lo.`date_email2`
|
|
FROM `' . _DB_PREFIX_ . 'orders` AS o
|
|
INNER JOIN `' . _DB_PREFIX_ . 'lgcomments_status` AS ek ON (o.`current_state` = ek.`id_order_status`)
|
|
LEFT JOIN `' . _DB_PREFIX_ . 'lgcomments_orders` AS lo ON (o.`id_order` = lo.`id_order`)
|
|
RIGHT JOIN `' . _DB_PREFIX_ . 'customer_group` AS cg ON (o.`id_customer` = cg.`id_customer`)
|
|
RIGHT JOIN `' . _DB_PREFIX_ . 'customer` AS c ON (o.`id_customer` = c.`id_customer`)
|
|
INNER JOIN `' . _DB_PREFIX_ . 'lgcomments_customergroups` AS lcg
|
|
ON (cg.`id_group` = lcg.`id_customer_group`)
|
|
RIGHT JOIN `' . _DB_PREFIX_ . 'lgcomments_multistore` AS lm ON (o.`id_shop` = lm.`id_shop`)
|
|
WHERE o.`date_add` >= DATE_SUB(NOW(), INTERVAL ' . (int)$days . ' DAY)
|
|
'.$where_days2.'
|
|
' . $boxes_checked . '
|
|
ORDER BY o.`id_order` DESC';
|
|
|
|
return Db::getInstance()->executeS($sql);
|
|
}
|
|
|
|
private function existOrderComment($id_order)
|
|
{
|
|
$sql = 'SELECT `id_order`
|
|
FROM `'._DB_PREFIX_.'lgcomments_orders`
|
|
WHERE `id_order` = ' . (int)$id_order;
|
|
|
|
return (int)Db::getInstance()->getValue($sql);
|
|
}
|
|
|
|
private function needSendAgain($id_order)
|
|
{
|
|
$sendtwice = Configuration::get('PS_LGCOMMENTS_EMAIL_TWICE');
|
|
$daysafter = Configuration::get('PS_LGCOMMENTS_DAYS_AFTER');
|
|
$sql = 'SELECT `id_order`
|
|
FROM `' . _DB_PREFIX_ . 'lgcomments_orders`
|
|
WHERE `id_order` = ' . (int)$id_order . '
|
|
AND voted < 1
|
|
AND sent < 2
|
|
AND date_email <= DATE_SUB(NOW(), INTERVAL ' . $daysafter . ' DAY)';
|
|
|
|
return $sendtwice && (bool)Db::getInstance()->getValue($sql);
|
|
}
|
|
|
|
private function saveCommentOrder($id_order, $id_customer, $hash)
|
|
{
|
|
$sql = 'INSERT INTO `' . _DB_PREFIX_ . 'lgcomments_orders`
|
|
VALUES (
|
|
' . (int)$id_order . ',
|
|
' . (int)$id_customer . ',
|
|
"' . pSQL($hash) . '",
|
|
0,
|
|
1,
|
|
NOW(),
|
|
0
|
|
)';
|
|
return Db::getInstance()->execute($sql);
|
|
}
|
|
|
|
private function markOrderAsSendTwice($id_order)
|
|
{
|
|
$sql = 'UPDATE `' . _DB_PREFIX_ . 'lgcomments_orders`
|
|
SET sent = "2", date_email2 = NOW()
|
|
WHERE id_order = ' . (int)$id_order;
|
|
|
|
Db::getInstance()->execute($sql);
|
|
}
|
|
}
|