Current File : /var/www/vinorea/modules/ipexportimport/classes/export/EIAStoresExport.php |
<?php
/**
*
* NOTICE OF LICENSE
*
* @author SmartPresta <tehran.alishov@gmail.com>
* @copyright 2024 SmartPresta
* @license http://opensource.org/licenses/afl-3.0.php Commercial License!
*/
if (!defined('_PS_VERSION_')) {
exit;
}
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class EIAStoresExport extends EIAExport
{
protected $xmlMainTag = 'Store';
protected $xmlMainTagPl = 'Stores';
public function __construct($module)
{
parent::__construct($module);
$this->entityNamePl = $module->l('Stores', 'EIAStoresExport');
}
private function getImageLink($id_image)
{
$sup_img_path = realpath(_PS_STORE_IMG_DIR_ . $id_image . '.jpg');
if (file_exists($sup_img_path)) {
return $this->context->link->getBaseLink() . 'img/st/' . $id_image . '.jpg';
} else {
return '';
}
}
protected function getDataFromDb()
{
// Columns that do not exist directly in the DB
$absentColumns = array(
'store_image' => 'store.id_store',
'image_url' => 'store.id_store'
);
if (in_array($this->fileType, ['csv', 'xml', 'json']) && isset($this->selectedColumns['store_image'])) {
unset($this->selectedColumns['store_image']);
}
if ($this->auto) {
$stores = pSQL(implode(',', $this->datatables['stores']['data']));
$storesType = $this->datatables['stores']['type'];
} else {
$stores = pSQL(Tools::getValue('stores_data'));
$storesType = Tools::getValue('stores_type');
}
$eIHelper = new EIAHelper($this->module);
$this->sql = '
SELECT SQL_CALC_FOUND_ROWS ';
if (in_array($this->fileType, ['xml'])) {
$fieldsInTree = $eIHelper->getFieldsInTree('stores');
foreach ($this->selectedColumns as $k => $col) {
if (array_key_exists($k, $absentColumns)) {
$this->sql .= "
{$absentColumns[$k]} `{$fieldsInTree[$k]}`, ";
} else {
$this->sql .= "
$k AS `{$fieldsInTree[$k]}`, ";
}
}
} else {
foreach ($this->selectedColumns as $k => $col) {
if (array_key_exists($k, $absentColumns)) {
$this->sql .= "
{$absentColumns[$k]} `$col`, ";
} else {
$this->sql .= "
$k `$col`, ";
}
}
}
$this->sql = rtrim($this->sql, ', ');
$this->sql .= '
FROM
' . _DB_PREFIX_ . 'store store
LEFT JOIN
' . _DB_PREFIX_ . 'store_shop store_shop ON store.id_store = store_shop.id_store
AND store_shop.id_shop = ' . $this->shopId . '
LEFT JOIN
' . _DB_PREFIX_ . 'country_lang country_lang ON store.id_country = country_lang.id_country AND country_lang.id_lang = ' . $this->langId . '
LEFT JOIN
' . _DB_PREFIX_ . 'state state ON store.id_state = state.id_state';
if (!empty(Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS("SHOW TABLES LIKE '" . _DB_PREFIX_ . "store_lang'"))) {
$this->sql .= '
LEFT JOIN
' . _DB_PREFIX_ . 'store_lang store_lang ON store.id_store = store_lang.id_store
AND store_lang.id_lang = ' . $this->langId;
}
$this->sql .= '
WHERE 1
';
// Filter By Store
$storeCond = '';
if ($stores) {
if ($storesType === 'unselected') {
$storeCond = 'store.id_store NOT IN (' . $stores . ')';
} else {
$storeCond = 'store.id_store IN (' . $stores . ')';
}
}
if ($storeCond) {
$this->sql .= '
AND (' . $storeCond . ') ';
}
// Filter by fields data
$this->sql .= $eIHelper->getFieldsFilterString($this->auto, $this->inputs);
// Sort By ...
$this->sql .= ' ORDER BY ' . $this->sort . $this->sortWay;
if ($this->sort !== 'store.id_store') {
$this->sql .= ', store.id_store ASC';
}
$this->sql .= ' LIMIT ' . $this->offset . ', ' . $this->limit;
// ddd($this->sql);
return Db::getInstance()->executeS($this->sql);
}
protected function writeToCsv()
{
$handle = $this->openCsvFile();
foreach ($this->data as $value) {
if (!empty($value[$this->selectedColumns['image_url']])) {
$value[$this->selectedColumns['image_url']] = $this->getImageLink($value[$this->selectedColumns['image_url']]);
}
fputcsv($handle, $value, $this->dlm, $this->encl);
}
$this->closeCsvFile($handle);
}
protected function writeToJson()
{
if (file_exists($this->file)) {
$data = json_decode(Tools::file_get_contents($this->file), true);
} else {
$data = [];
}
foreach ($this->data as &$value) {
if (!empty($value[$this->selectedColumns['image_url']])) {
$value[$this->selectedColumns['image_url']] = $this->getImageLink($value[$this->selectedColumns['image_url']]);
}
}
$data = array_merge($data, $this->data);
file_put_contents($this->file, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
}
protected function writeToXml()
{
foreach ($this->data as &$value) {
if (!empty($value['Image_URL'])) {
$value['Image_URL'] = $this->getImageLink($value['Image_URL']);
}
}
// ddd($this->data);
$this->makeXmlHierarchy();
if (file_exists($this->file)) {
$data = $this->arrayToXml(array_merge(array('arrType' => $this->xmlMainTag), $this->data), null, simplexml_load_file($this->file));
} else {
$data = $this->arrayToXml(array_merge(array('arrType' => $this->xmlMainTag), $this->data), '<?xml version="1.0" encoding="UTF-8"?><' . $this->xmlMainTagPl . ' />');
}
$domxml = new DOMDocument('1.0');
$domxml->preserveWhiteSpace = false;
$domxml->formatOutput = true;
/* @var $xml SimpleXMLElement */
$domxml->loadXML($data);
$domxml->save($this->file, LIBXML_NOEMPTYTAG);
}
protected function writeToExcel()
{
require_once dirname(__FILE__) . '/../../vendor/autoload.php';
if (file_exists($this->file)) {
$spreadsheet = IOFactory::load($this->file);
$sheet = $spreadsheet->getActiveSheet();
$excelColumns = EIAHelper::createColumnsArray(count($this->data[0]));
} else {
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
// Set document properties
$spreadsheet->getProperties()->setCreator('Tehran Alishov')
->setLastModifiedBy('Tehran Alishov')
->setTitle('Office 2007 XLSX Stores Document')
->setSubject('Office 2007 XLSX Stores Document')
->setDescription('Stores document for Office 2007 XLSX, generated using PHP classes.')
->setKeywords('office 2007 openxml php')
->setCategory('Stores result file');
$spreadsheet->setActiveSheetIndex(0);
if (empty($this->data)) {
$sheet->setCellValue('A1', $this->module->l('No Data', 'EIAStoresExport'));
} else {
$excelColumns = EIAHelper::createColumnsArray(count($this->data[0]));
$sheet->getDefaultColumnDimension()->setWidth(21);
if (isset($this->selectedColumns['store_image'])) {
$sheet->getDefaultRowDimension()->setRowHeight(42);
} else {
$sheet->getDefaultRowDimension()->setRowHeight(30);
}
$spreadsheet->getDefaultStyle()->getAlignment()
->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER)
->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
$sheet->getStyle('A1:' . end($excelColumns) . (count($this->data)))
->getAlignment()->setWrapText(true);
$sheet->getStyle('A1:' . end($excelColumns) . '1')
->getFont()->setBold(true);
$sheet->getStyle('A1:' . end($excelColumns) . '1')
->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
->getStartColor()->setARGB('FFDCF0FF');
$sheet->getStyle('A1:' . end($excelColumns) . '1')->getBorders()
->getAllBorders()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
// Rename worksheet
$sheet->setTitle($this->module->l('Stores', 'EIAStoresExport'));
$headers = array_keys($this->data[0]);
foreach ($headers as $key => $header) {
$sheet->setCellValue($excelColumns[$key] . '1', $header);
}
if (isset($this->selectedColumns['image_url'])) {
$sheet->getColumnDimension($excelColumns[array_search($this->selectedColumns['image_url'], $headers)])->setWidth(40);
}
}
}
$small_image = $this->getImageType('medium');
$font = $sheet->getStyle('A1')->getFont();
foreach ($this->data as $key => $value) {
$i = 0;
foreach ($value as $k => $val) {
if ($k === $this->selectedColumns['store_image'] && $val) {
$image_path = realpath(_PS_STORE_IMG_DIR_ . $val . '-' . $small_image . '.jpg');
if (file_exists($image_path)) {
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setPath(realpath($image_path));
$height = \PhpOffice\PhpSpreadsheet\Shared\Drawing::pointsToPixels(42);
$drawing->setHeight($height);
$width = \PhpOffice\PhpSpreadsheet\Shared\Drawing::pixelsToCellDimension($drawing->getWidth(), $font);
$sheet->getColumnDimension($excelColumns[$i])->setWidth($width);
$drawing->setCoordinates($excelColumns[$i] . ($this->offset + $key + 2));
$drawing->setWorksheet($sheet);
$drawing->getShadow()->setVisible(true);
}
} elseif ($k === $this->selectedColumns['image_url'] && $val) {
$link = $this->getImageLink($val);
$cell = $excelColumns[$i] . ($this->offset + $key + 2);
$sheet->setCellValue($cell, $link);
if ($link) {
$sheet->getCell($cell)->getHyperlink()->setUrl($link);
$sheet->getStyle($cell)->getFont()->getColor()->setARGB('FF0000FF');
}
} else {
$sheet->setCellValue($excelColumns[$i] . ($this->offset + $key + 2), $val);
}
$i++;
}
}
$sheet->setSelectedCell('A1');
// Write to file
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($this->file);
}
}