Creating new plugin

This commit is contained in:
Roman Pyrih
2025-12-02 15:59:27 +01:00
parent 49f474475b
commit f1cfd1819e
3 changed files with 258 additions and 0 deletions

View File

@@ -0,0 +1,144 @@
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class AdminEksportHistoriiController extends ModuleAdminController
{
public function __construct()
{
$this->bootstrap = true;
parent::__construct();
}
public function initContent()
{
if (Tools::isSubmit('submitExportHistory')) {
$this->processExport();
return;
}
parent::initContent();
$this->content = $this->renderExportForm();
$this->context->smarty->assign([
'content' => $this->content,
]);
}
protected function renderExportForm()
{
$action = self::$currentIndex . '&token=' . $this->token;
$action = Tools::safeOutput($action);
$html = '
<div class="panel">
<h3><i class="icon-download"></i> ' . $this->l('Eksport historii zamówień') . '</h3>
<form method="post" action="' . $action . '" class="form-horizontal">
<div class="form-group">
<label class="control-label col-lg-2">
' . $this->l('Data od') . '
</label>
<div class="col-lg-3">
<input type="date" name="date_from" class="form-control" required="required" />
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-2">
' . $this->l('Data do') . '
</label>
<div class="col-lg-3">
<input type="date" name="date_to" class="form-control" required="required" />
</div>
</div>
<div class="panel-footer">
<button type="submit" name="submitExportHistory" class="btn btn-primary">
<i class="icon-download"></i> ' . $this->l('Eksport do Excel (CSV)') . '
</button>
</div>
</form>
</div>
';
return $html;
}
/**
* CSV
*/
public function processExport()
{
$date_from = Tools::getValue('date_from');
$date_to = Tools::getValue('date_to');
if (empty($date_from) || empty($date_to)) {
die($this->l('Obie daty są wymagane.'));
}
// początek / koniec dnia
$date_from .= ' 00:00:00';
$date_to .= ' 23:59:59';
// Zapytanie SQL: zamówienia + klient + produkty
$sql = new DbQuery();
$sql->select('
o.id_order AS order_id,
c.id_customer AS client_id,
c.email,
c.firstname AS first_name,
c.lastname AS last_name,
od.product_id,
od.product_name
');
$sql->from('orders', 'o');
$sql->innerJoin('customer', 'c', 'c.id_customer = o.id_customer');
$sql->innerJoin('order_detail', 'od', 'od.id_order = o.id_order');
$sql->where('o.date_add BETWEEN "' . pSQL($date_from) . '" AND "' . pSQL($date_to) . '"');
$sql->orderBy('o.id_order ASC');
$rows = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
$filename = 'orders_export_' . date('Y-m-d_His') . '.csv';
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Pragma: no-cache');
$output = fopen('php://output', 'w');
fprintf($output, chr(0xEF) . chr(0xBB) . chr(0xBF));
fputcsv(
$output,
['order_id', 'client_id', 'email', 'first_name', 'last_name', 'product_id', 'product_name'],
';'
);
if (!empty($rows)) {
foreach ($rows as $row) {
fputcsv(
$output,
[
$row['order_id'],
$row['client_id'],
$row['email'],
$row['first_name'],
$row['last_name'],
$row['product_id'],
$row['product_name'],
],
';'
);
}
}
fclose($output);
exit;
}
}

View File

@@ -0,0 +1,80 @@
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class EksportHistorii extends Module
{
public function __construct()
{
$this->name = 'eksporthistorii';
$this->tab = 'administration';
$this->version = '1.0.0';
$this->author = 'Custom';
$this->need_instance = 0;
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Eksport historii zamówień');
$this->description = $this->l('Eksport historii zamówień do pliku Excel (CSV).');
}
public function install()
{
return parent::install()
&& $this->installTab();
}
public function uninstall()
{
return $this->uninstallTab()
&& parent::uninstall();
}
/**
* Dodanie zakładki jako podmenu w "Zamówienia"
*/
protected function installTab()
{
// rodzic: Orders / Zamówienia
$id_parent = (int) Tab::getIdFromClassName('AdminParentOrders');
$tab = new Tab();
$tab->active = 1;
$tab->class_name = 'AdminEksportHistorii';
$tab->name = [];
foreach (Language::getLanguages(false) as $lang) {
$tab->name[$lang['id_lang']] = 'Eksport historii';
}
$tab->id_parent = $id_parent;
$tab->module = $this->name;
return (bool) $tab->add();
}
/**
* Usunięcie zakładki przy deinstalacji
*/
protected function uninstallTab()
{
// Usuwamy naszą aktualną zakładkę
$id_tab = (int) Tab::getIdFromClassName('AdminEksportHistorii');
if ($id_tab) {
$tab = new Tab($id_tab);
$tab->delete();
}
// Sprzątamy ewentualną starą zakładkę (ze starego modułu)
$old_id_tab = (int) Tab::getIdFromClassName('AdminEksportowanieIstorii');
if ($old_id_tab) {
$old_tab = new Tab($old_id_tab);
$old_tab->delete();
}
return true;
}
}

View File

@@ -0,0 +1,34 @@
<?php
/**
* 2007-2020 PrestaShop SA and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;