feat(4.0): add product form/edit/delete events and history auto-prune

This commit is contained in:
nertyog 2026-10-20 17:22:40 +03:00
parent f798ce5141
commit 7cbfc019e7
4 changed files with 150 additions and 2 deletions

View File

@ -508,7 +508,7 @@ class EasyImportNat extends \Opencart\System\Engine\Controller {
array( array(
'code' => 'easy_import_nat_product_edit', 'code' => 'easy_import_nat_product_edit',
'description' => 'Easy Import/Export [NAT] Product Edit Sources', 'description' => 'Easy Import/Export [NAT] Product Edit Sources',
'trigger' => 'admin/model/catalog/product.editProduct/after', 'trigger' => 'admin/model/catalog/product' . $sep . 'editProduct/after',
'action' => 'extension/easy_import_nat/module/easy_import_nat' . $sep . 'eventProductEditAfter', 'action' => 'extension/easy_import_nat/module/easy_import_nat' . $sep . 'eventProductEditAfter',
'status' => 1, 'status' => 1,
'sort_order' => 1 'sort_order' => 1
@ -516,7 +516,7 @@ class EasyImportNat extends \Opencart\System\Engine\Controller {
array( array(
'code' => 'easy_import_nat_product_delete', 'code' => 'easy_import_nat_product_delete',
'description' => 'Easy Import/Export [NAT] Product Delete History', 'description' => 'Easy Import/Export [NAT] Product Delete History',
'trigger' => 'admin/model/catalog/product.deleteProduct/after', 'trigger' => 'admin/model/catalog/product' . $sep . 'deleteProduct/after',
'action' => 'extension/easy_import_nat/module/easy_import_nat' . $sep . 'eventProductDeleteAfter', 'action' => 'extension/easy_import_nat/module/easy_import_nat' . $sep . 'eventProductDeleteAfter',
'status' => 1, 'status' => 1,
'sort_order' => 1 'sort_order' => 1
@ -649,6 +649,150 @@ class EasyImportNat extends \Opencart\System\Engine\Controller {
} }
} }
public function eventProductFormAfter(string &$route, array &$args, string &$output): void {
$product_id = isset($this->request->get['product_id']) ? (int)$this->request->get['product_id'] : 0;
$nat_sources = '';
if (isset($this->request->post['nat_sources'])) {
$nat_sources = (string)$this->request->post['nat_sources'];
} elseif ($product_id) {
$q = $this->db->query("SELECT nat_sources FROM `" . DB_PREFIX . "product` WHERE product_id = '" . (int)$product_id . "'");
if ($q->num_rows) {
$nat_sources = (string)$q->row['nat_sources'];
}
}
$sources_list = array_filter(explode(';', trim($nat_sources, ';')));
$history = array();
if ($product_id) {
try {
$h_depth = (int)$this->config->get('module_easy_import_nat_history_depth');
if ($h_depth <= 0) $h_depth = 5;
$h_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "easy_import_product_history` WHERE product_id = '" . (int)$product_id . "' ORDER BY date_added DESC, history_id DESC LIMIT " . (int)$h_depth);
$history = $h_query->rows;
} catch (\Throwable $e) {}
}
$badges_html = '';
if (!empty($sources_list)) {
foreach ($sources_list as $src) {
$badges_html .= '<span class="badge bg-info text-dark" style="font-size: 12px; margin-right: 5px; padding: 5px 8px; display: inline-block; margin-bottom: 4px;"><i class="fa-solid fa-truck"></i> ' . htmlspecialchars($src, ENT_QUOTES, 'UTF-8') . '</span> ';
}
} else {
$badges_html = '<span class="text-muted" style="font-size: 12px;">—</span>';
}
$form_html = '
<div class="row mb-3">
<label class="col-sm-2 col-form-label" for="input-nat-sources"><span data-bs-toggle="tooltip" title="Все поставщики (импорты), когда-либо обновлявшие этот товар">Источники [NAT]</span></label>
<div class="col-sm-10">
<div style="margin-bottom: 8px;">' . $badges_html . '</div>
<input type="text" name="nat_sources" value="' . htmlspecialchars($nat_sources, ENT_QUOTES, 'UTF-8') . '" placeholder=";supplier1;supplier2;" id="input-nat-sources" class="form-control" />
<div class="form-text text-muted"><i class="fa-solid fa-circle-info"></i> Формат: <code>;supplier1;supplier2;</code> (разделитель точка с запятой)</div>
</div>
</div>';
if (!empty($history)) {
$batches_seen = array();
$batch_options = '<option value="">Все поставщики</option>';
foreach ($history as $h) {
if (!empty($h['batch_name']) && !in_array($h['batch_name'], $batches_seen)) {
$batches_seen[] = $h['batch_name'];
$batch_options .= '<option value="' . htmlspecialchars($h['batch_name'], ENT_QUOTES, 'UTF-8') . '">' . htmlspecialchars($h['batch_name'], ENT_QUOTES, 'UTF-8') . '</option>';
}
}
$rows_html = '';
foreach ($history as $h) {
$b_name = htmlspecialchars($h['batch_name'], ENT_QUOTES, 'UTF-8');
$b_badge = $h['batch_name'] ? '<span class="badge bg-primary" style="font-size: 11px;"><i class="fa-solid fa-tag"></i> ' . $b_name . '</span>' : '<span class="text-muted">—</span>';
$price_fmt = number_format((float)$h['price'], 2, '.', '');
$special_fmt = ($h['special'] !== null && $h['special'] !== '') ? '<span class="text-danger fw-bold">' . number_format((float)$h['special'], 2, '.', '') . '</span>' : '<span class="text-muted">—</span>';
$qty_badge = ((int)$h['quantity'] > 0) ? '<span class="badge bg-success">' . (int)$h['quantity'] . '</span>' : '<span class="badge bg-danger">0</span>';
$stat_badge = ((int)$h['status'] == 1) ? '<span class="badge bg-success">Вкл</span>' : '<span class="badge bg-secondary">Откл</span>';
$rows_html .= '<tr class="nat-history-row" data-batch="' . $b_name . '" style="font-size: 12px;">
<td class="text-start">' . htmlspecialchars($h['date_added'], ENT_QUOTES, 'UTF-8') . '</td>
<td class="text-start">' . $b_badge . '</td>
<td class="text-end fw-bold">' . $price_fmt . '</td>
<td class="text-end">' . $special_fmt . '</td>
<td class="text-center">' . $qty_badge . '</td>
<td class="text-center">' . $stat_badge . '</td>
</tr>';
}
$form_html .= '
<div class="row mb-3" style="margin-top: 25px; border-top: 1px solid #e2e8f0; padding-top: 20px;">
<label class="col-sm-2 col-form-label"><span data-bs-toggle="tooltip" title="Последние обновления цен и остатков [NAT]"><i class="fa-solid fa-clock-rotate-left"></i> Последние обновления цен и остатков [NAT]</span></label>
<div class="col-sm-10">
<div class="card card-body bg-light p-3 border-primary-subtle" style="border-radius: 6px;">
<div class="row mb-3">
<div class="col-sm-6">
<label style="font-weight: 600; font-size: 13px; color: #1e3a8a;"><i class="fa-solid fa-filter"></i> Фильтр по поставщику (Batch):</label>
<select id="filter-nat-history-batch" class="form-select form-select-sm d-inline-block" onchange="filterNatHistory(this.value);" style="max-width: 250px; margin-left: 8px;">
' . $batch_options . '
</select>
</div>
<div class="col-sm-6 text-end">
<span class="text-muted" style="font-size: 12px;"><i class="fa-solid fa-database"></i> Последние изменения (макс. ' . count($history) . ')</span>
</div>
</div>
<div class="table-responsive">
<table class="table table-bordered table-striped table-hover mb-0 bg-white">
<thead class="table-light">
<tr style="font-size: 12px;">
<th class="text-start" style="width: 160px;"><i class="fa-solid fa-calendar"></i> Дата / Время</th>
<th class="text-start"><i class="fa-solid fa-truck"></i> Поставщик (Batch)</th>
<th class="text-end" style="width: 120px;"><i class="fa-solid fa-tag"></i> Цена</th>
<th class="text-end" style="width: 120px;"><i class="fa-solid fa-percent"></i> Акция</th>
<th class="text-center" style="width: 100px;"><i class="fa-solid fa-boxes-stacked"></i> Остаток</th>
<th class="text-center" style="width: 100px;"><i class="fa-solid fa-power-off"></i> Статус</th>
</tr>
</thead>
<tbody>' . $rows_html . '</tbody>
</table>
</div>
</div>
</div>
</div>
<script type="text/javascript"><!--
function filterNatHistory(batch) {
var rows = document.querySelectorAll(".nat-history-row");
for (var i = 0; i < rows.length; i++) {
if (!batch || rows[i].getAttribute("data-batch") === batch) {
rows[i].style.display = "";
} else {
rows[i].style.display = "none";
}
}
}
//--></script>';
}
if (preg_match('#<div[^>]*class=[\'"][^\'"]*tab-pane[^\'"]*[\'"][^>]*id=[\'"]tab-(seo|links|design|option|attribute)[\'"]|id=[\'"]tab-(seo|links|design)[\'"][^>]*class=[\'"][^\'"]*tab-pane#i', $output, $m, PREG_OFFSET_CAPTURE)) {
$output = substr_replace($output, $form_html, $m[0][1], 0);
} elseif (preg_match('#</form>#i', $output, $m, PREG_OFFSET_CAPTURE)) {
$output = substr_replace($output, $form_html, $m[0][1], 0);
}
}
public function eventProductEditAfter(string &$route, array &$args, mixed &$output): void {
if (isset($args[0]) && isset($args[1]['nat_sources'])) {
$product_id = (int)$args[0];
$this->db->query("UPDATE `" . DB_PREFIX . "product` SET nat_sources = '" . $this->db->escape($args[1]['nat_sources']) . "' WHERE product_id = '" . $product_id . "'");
}
}
public function eventProductDeleteAfter(string &$route, array &$args, mixed &$output): void {
if (isset($args[0])) {
$product_id = (int)$args[0];
try {
$this->db->query("DELETE FROM `" . DB_PREFIX . "easy_import_product_history` WHERE product_id = '" . $product_id . "'");
} catch (\Throwable $e) {}
}
}
public function backup() { public function backup() {
if (!isset($this->request->get['user_token']) || !isset($this->session->data['user_token']) || $this->request->get['user_token'] !== $this->session->data['user_token'] || !$this->user->hasPermission('modify', 'extension/easy_import_nat/module/easy_import_nat')) { if (!isset($this->request->get['user_token']) || !isset($this->session->data['user_token']) || $this->request->get['user_token'] !== $this->session->data['user_token'] || !$this->user->hasPermission('modify', 'extension/easy_import_nat/module/easy_import_nat')) {
$this->load->language('extension/easy_import_nat/module/easy_import_nat'); $this->load->language('extension/easy_import_nat/module/easy_import_nat');
@ -1656,6 +1800,9 @@ public function importProcessChunk() {
$ident_key = trim((string)$header[$ident_col]); $ident_key = trim((string)$header[$ident_col]);
$count = 0; $count = 0;
if ($batch === 1) {
try { $this->model_extension_easy_import_nat_module_easy_import_nat->pruneProductHistory(60); } catch (\Throwable $e) {}
}
if ($batch === 1 && file_exists(DIR_CACHE . 'nat_import_log.txt')) { if ($batch === 1 && file_exists(DIR_CACHE . 'nat_import_log.txt')) {
@file_put_contents(DIR_CACHE . 'nat_import_log.txt', "--- IMPORT STARTED [" . date('Y-m-d H:i:s') . "] ---\n"); @file_put_contents(DIR_CACHE . 'nat_import_log.txt', "--- IMPORT STARTED [" . date('Y-m-d H:i:s') . "] ---\n");
} }

View File

@ -702,6 +702,7 @@ class EasyImportNatCron extends \Opencart\System\Engine\Controller {
$col_binding = isset($settings['col_binding']) && is_array($settings['col_binding']) ? $settings['col_binding'] : array(); $col_binding = isset($settings['col_binding']) && is_array($settings['col_binding']) ? $settings['col_binding'] : array();
try { $model->pruneProductHistory(60); } catch (\Throwable $e) {}
$batch_name_default = isset($settings['import_batch_name']) ? preg_replace('/[^a-zA-Z0-9_\-]/', '', $settings['import_batch_name']) : ''; $batch_name_default = isset($settings['import_batch_name']) ? preg_replace('/[^a-zA-Z0-9_\-]/', '', $settings['import_batch_name']) : '';
$missing_action = isset($settings['missing_action']) ? preg_replace('/[^a-zA-Z_]/', '', $settings['missing_action']) : 'skip'; $missing_action = isset($settings['missing_action']) ? preg_replace('/[^a-zA-Z_]/', '', $settings['missing_action']) : 'skip';
$log_mode = isset($settings['log_mode']) ? preg_replace('/[^a-zA-Z_]/', '', $settings['log_mode']) : 'changes'; $log_mode = isset($settings['log_mode']) ? preg_replace('/[^a-zA-Z_]/', '', $settings['log_mode']) : 'changes';

Binary file not shown.