Files
2019-11-17 19:14:07 +01:00

271 lines
12 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
*/
require_once(
_PS_MODULE_DIR_ .
DIRECTORY_SEPARATOR .
'lgcomments' .
DIRECTORY_SEPARATOR .
'classes' .
DIRECTORY_SEPARATOR .
'LGProductComment.php'
);
require_once(
_PS_MODULE_DIR_ .
DIRECTORY_SEPARATOR .
'lgcomments' .
DIRECTORY_SEPARATOR .
'classes' .
DIRECTORY_SEPARATOR .
'LGStoreComment.php'
);
class LGCSV
{
public static function importProductComments($module)
{
$html = '';
$separator1 = (int)Tools::getValue('separator1', 1);
$tokenPC = Tools::getAdminTokenLite('AdminLGCommentsProducts');
if ($separator1 == 2) {
$sp = ',';
} else {
$sp = ';';
}
$encoding1 = (int)Tools::getValue('encoding1', 1);
if (is_uploaded_file($_FILES['csv1']['tmp_name'])) {
$type = explode(".", $_FILES['csv1']['name']);
if (Tools::strtolower(end($type)) == 'csv') {
if (move_uploaded_file(
$_FILES['csv1']['tmp_name'],
dirname(dirname(__FILE__)) . '/csv/' . $_FILES['csv1']['name']
)) {
$archivo = $_FILES['csv1']['name'];
$fp = fopen(dirname(dirname(__FILE__)) . '/csv/' . $archivo, 'r');
while (($datos = fgetcsv($fp, 1000, '' . $sp . '')) !== false) {
$datos[0] = str_replace('/', '-', $datos[0]);
$date = strtotime($datos[0]);
$date = date('Y-m-d', $date);
if ($encoding1 == 2) {
$csv_comment = mb_convert_encoding($datos[4], 'UTF-8', 'auto');
$csv_title = mb_convert_encoding($datos[8], 'UTF-8', 'auto');
$csv_answer = mb_convert_encoding($datos[9], 'UTF-8', 'auto');
} else {
$csv_comment = utf8_encode($datos[4]);
$csv_title = utf8_encode($datos[8]);
$csv_answer = utf8_encode($datos[9]);
}
Db::getInstance()->Execute(
'INSERT INTO ' . _DB_PREFIX_ . 'lgcomments_productcomments ' .
'(date, id_customer, id_product, stars, comment, id_lang, ' .
'active, position, title, answer ) ' .
'VALUES (
"' . $date . '",
"' . (int)$datos[1] . '",
"' . (int)$datos[2] . '",
"' . (int)$datos[3] . '",
"' . pSQL($csv_comment) . '",
"' . (int)$datos[5] . '",
"' . (int)$datos[6] . '",
"' . (int)$datos[7] . '",
"' . pSQL($csv_title) . '",
"' . pSQL($csv_answer) . '"
)'
);
}
fclose($fp);
$html .= $module->displayConfirmation(
$module->l('The comments have been successfully added') .
'&nbsp;'.
$module->getAnchor(
array(
'lgcomments_warning_link_href' => 'index.php?controller=AdminLGCommentsProducts&token='
.$tokenPC,
'lgcomments_warning_link_target' => '_blank',
'lgcomments_warning_link_message' => $module->l(
'Click here to manage your product reviews'
),
)
)
);
}
} else {
$html .= $module->displayError(
$module->l('The format of the file is not valid, it must be saved in ".csv" format.')
);
}
} else {
$html .= $module->displayError($module->l('An error occurred while uploading the CSV file'));
}
return $html;
}
public static function exportProductComments($module)
{
$html = '';
$separator1 = (int)Tools::getValue('separator1', 1);
if ($separator1 == 2) {
$sp = ',';
} else {
$sp = ';';
}
$ln = "\n";
$fp = fopen(_PS_ROOT_DIR_ . '/modules/' . $module->name . '/csv/save_products.csv', 'w');
$prodComments = LGProductComment::getAllProductComments();
foreach ($prodComments as $prodComment) {
fwrite(
$fp,
$prodComment['date'] . $sp . $prodComment['id_customer'] . $sp . $prodComment['id_product'] .
$sp . $prodComment['stars'] . $sp . utf8_decode($prodComment['comment']) . '' .
$sp . $prodComment['id_lang'] . $sp . $prodComment['active'] . $sp . $prodComment['position'] .
$sp . utf8_decode($prodComment['title']) . $sp . utf8_decode($prodComment['answer']) . $ln
);
}
fclose($fp);
if ($prodComments != false) {
$html .= $module->displayConfirmation(
$module->l('The CSV file has been successfully generated,') . '&nbsp;'.
$module->getAnchor(
array(
'lgcomments_warning_link_href' => '../modules/' . $module->name . '/csv/save_products.csv',
'lgcomments_warning_link_message' => $module->l('click here to download it'),
)
)
);
} else {
$html .= $module->displayError($module->l('There are no product comments to export'));
}
return $html;
}
public static function importStorecomments($module)
{
$html = '';
$separator2 = (int)Tools::getValue('separator2', 1);
$tokenSC = Tools::getAdminTokenLite('AdminLGCommentsStore');
if ($separator2 == 2) {
$sp = ',';
} else {
$sp = ';';
}
$encoding2 = (int)Tools::getValue('encoding2', 1);
if (is_uploaded_file($_FILES['csv2']['tmp_name'])) {
$type = explode(".", $_FILES['csv2']['name']);
if (Tools::strtolower(end($type)) == 'csv') {
if (move_uploaded_file(
$_FILES['csv2']['tmp_name'],
dirname(dirname(__FILE__)) . '/csv/' . $_FILES['csv2']['name']
)) {
$archivo = $_FILES['csv2']['name'];
$fp = fopen(dirname(dirname(__FILE__)) . '/csv/' . $archivo, 'r');
while (($datos = fgetcsv($fp, 1000, '' . $sp . '')) !== false) {
$datos[0] = str_replace('/', '-', $datos[0]);
$date = strtotime($datos[0]);
$date = date('Y-m-d', $date);
if ($encoding2 == 2) {
$csv_comment = mb_convert_encoding($datos[4], 'UTF-8', 'auto');
$csv_title = mb_convert_encoding($datos[8], 'UTF-8', 'auto');
$csv_answer = mb_convert_encoding($datos[9], 'UTF-8', 'auto');
} else {
$csv_comment = utf8_encode($datos[4]);
$csv_title = utf8_encode($datos[8]);
$csv_answer = utf8_encode($datos[9]);
}
Db::getInstance()->Execute(
'INSERT INTO ' . _DB_PREFIX_ . 'lgcomments_storecomments ' .
'(date, id_customer, id_order, stars, comment, id_lang, active, ' .
'position, title, answer ) ' .
'VALUES (
"' . $date . '",
"' . (int)$datos[1] . '",
"' . (int)$datos[2] . '",
"' . (int)$datos[3] . '",
"' . pSQL($csv_comment) . '",
"' . (int)$datos[5] . '",
"' . (int)$datos[6] . '",
"' . (int)$datos[7] . '",
"' . pSQL($csv_title) . '",
"' . pSQL($csv_answer) . '"
)'
);
}
fclose($fp);
$html .= $module->displayConfirmation(
$module->l('The comments have been successfully added') . '.&nbsp;'.
$module->getAnchor(
array(
'lgcomments_warning_link_href' => 'index.php?controller=AdminLGCommentsStore&token='
.$tokenSC,
'lgcomments_warning_link_target' => '_blank',
'lgcomments_warning_link_message' => $module->l(
'Click here to manage your store reviews'
),
)
)
);
}
} else {
$html .= $module->displayError(
$module->l('The format of the file is not valid, it must be saved in ".csv" format.')
);
}
} else {
$html .= $module->displayError($module->l('An error occurred while uploading the CSV file'));
}
return $html;
}
public static function exportStoreComments($module)
{
$html = '';
$separator2 = (int)Tools::getValue('separator2', 1);
if ($separator2 == 2) {
$sp = ',';
} else {
$sp = ';';
}
$ln = "\n";
$fp = fopen(_PS_ROOT_DIR_ . '/modules/' . $module->name . '/csv/save_store.csv', 'w');
$storeComments = LGStoreComment::getAllStoreComments();
foreach ($storeComments as $storeComment) {
fwrite(
$fp,
$storeComment['date'] . $sp . $storeComment['id_customer'] . $sp . $storeComment['id_order'] .
$sp . $storeComment['stars'] . $sp . utf8_decode($storeComment['comment']) . '' .
$sp . $storeComment['id_lang'] . $sp . $storeComment['active'] . $sp . $storeComment['position'] .
$sp . utf8_decode($storeComment['title']) . $sp . utf8_decode($storeComment['answer']) . $ln
);
}
fclose($fp);
if ($storeComments != false) {
$html .= $module->displayConfirmation(
$module->l('The CSV file has been successfully generated,') . '&nbsp;'
.$module->getAnchor(
array(
'lgcomments_warning_link_href' => 'i../modules/' . $module->name . '/csv/save_store.csv',
'lgcomments_warning_link_message' => $module->l('click here to download it'),
)
)
);
} else {
$html .= $module->displayError($module->l('There are no store comments to export'));
}
return $html;
}
}