feat: Add CronJob functionality and integrate with existing services

- Implemented CronJobProcessor for managing scheduled jobs and processing job queues.
- Created CronJobRepository for database interactions related to cron jobs.
- Defined CronJobType for job types, statuses, and backoff calculations.
- Added ApiloLogger for logging actions related to API interactions.
- Enhanced UpdateController to check for updates and display update logs.
- Updated FormAction to include a preview action for forms.
- Modified ApiRouter to handle new dependencies for OrderAdminService and ProductsApiController.
- Extended DictionariesApiController to manage attributes and producers.
- Enhanced ProductsApiController with variant management and image upload functionality.
- Updated ShopBasketController and ShopProductController to sort attributes and handle custom fields.
- Added configuration for cron jobs in config.php.
- Initialized apilo-sync-queue.json for managing sync tasks.
This commit is contained in:
2026-02-27 14:54:05 +01:00
parent 3ecbe628dc
commit 31fd0442b2
33 changed files with 2714 additions and 200 deletions

View File

@@ -2,6 +2,7 @@
namespace admin\Controllers;
use Domain\Integrations\IntegrationsRepository;
use admin\ViewModels\Common\PaginatedTableViewModel;
class IntegrationsController
{
@@ -12,6 +13,114 @@ class IntegrationsController
$this->repository = $repository;
}
public function logs(): string
{
$sortableColumns = ['id', 'action', 'order_id', 'message', 'date'];
$filterDefinitions = [
[
'key' => 'log_action',
'label' => 'Akcja',
'type' => 'text',
],
[
'key' => 'message',
'label' => 'Wiadomosc',
'type' => 'text',
],
[
'key' => 'order_id',
'label' => 'ID zamowienia',
'type' => 'text',
],
];
$listRequest = \admin\Support\TableListRequestFactory::fromRequest(
$filterDefinitions,
$sortableColumns,
'id'
);
$result = $this->repository->getLogs(
$listRequest['filters'],
$listRequest['sortColumn'],
$listRequest['sortDir'],
$listRequest['page'],
$listRequest['perPage']
);
$rows = [];
$lp = ($listRequest['page'] - 1) * $listRequest['perPage'] + 1;
foreach ( $result['items'] as $item ) {
$id = (int)($item['id'] ?? 0);
$context = trim( (string)($item['context'] ?? '') );
$contextHtml = '';
if ( $context !== '' ) {
$contextHtml = '<button class="btn btn-xs btn-default log-context-btn" data-id="' . $id . '">Pokaz</button>'
. '<pre class="log-context-pre" id="log-context-' . $id . '" style="display:none;max-height:300px;overflow:auto;margin-top:5px;font-size:11px;white-space:pre-wrap;">'
. htmlspecialchars( $context, ENT_QUOTES, 'UTF-8' )
. '</pre>';
}
$rows[] = [
'lp' => $lp++ . '.',
'action' => htmlspecialchars( (string)($item['action'] ?? ''), ENT_QUOTES, 'UTF-8' ),
'order_id' => $item['order_id'] ? (int)$item['order_id'] : '-',
'message' => htmlspecialchars( (string)($item['message'] ?? ''), ENT_QUOTES, 'UTF-8' ),
'context' => $contextHtml,
'date' => !empty( $item['date'] ) ? date( 'Y-m-d H:i:s', strtotime( (string)$item['date'] ) ) : '-',
];
}
$total = (int)$result['total'];
$totalPages = max( 1, (int)ceil( $total / $listRequest['perPage'] ) );
$viewModel = new PaginatedTableViewModel(
[
['key' => 'lp', 'label' => 'Lp.', 'class' => 'text-center', 'sortable' => false],
['key' => 'date', 'sort_key' => 'date', 'label' => 'Data', 'class' => 'text-center', 'sortable' => true],
['key' => 'action', 'sort_key' => 'action', 'label' => 'Akcja', 'sortable' => true],
['key' => 'order_id', 'sort_key' => 'order_id', 'label' => 'Zamowienie', 'class' => 'text-center', 'sortable' => true],
['key' => 'message', 'sort_key' => 'message', 'label' => 'Wiadomosc', 'sortable' => true],
['key' => 'context', 'label' => 'Kontekst', 'sortable' => false, 'raw' => true],
],
$rows,
$listRequest['viewFilters'],
[
'column' => $listRequest['sortColumn'],
'dir' => $listRequest['sortDir'],
],
[
'page' => $listRequest['page'],
'per_page' => $listRequest['perPage'],
'total' => $total,
'total_pages' => $totalPages,
],
array_merge( $listRequest['queryFilters'], [
'sort' => $listRequest['sortColumn'],
'dir' => $listRequest['sortDir'],
'per_page' => $listRequest['perPage'],
] ),
$listRequest['perPageOptions'],
$sortableColumns,
'/admin/integrations/logs/',
'Brak wpisow w logach.'
);
return \Shared\Tpl\Tpl::view( 'integrations/logs', [
'viewModel' => $viewModel,
] );
}
public function logs_clear(): void
{
$this->repository->clearLogs();
\Shared\Helpers\Helpers::alert( 'Logi zostaly wyczyszczone.' );
header( 'Location: /admin/integrations/logs/' );
exit;
}
public function apilo_settings(): string
{
return \Shared\Tpl\Tpl::view( 'integrations/apilo-settings', [

View File

@@ -106,6 +106,14 @@ class ProductArchiveController
'confirm_ok' => 'Przywroc',
'confirm_cancel' => 'Anuluj',
],
[
'label' => 'Usun trwale',
'url' => '/admin/product_archive/delete_permanent/product_id=' . $id,
'class' => 'btn btn-xs btn-danger',
'confirm' => 'UWAGA! Operacja nieodwracalna!' . "\n\n" . 'Produkt "' . htmlspecialchars($name, ENT_QUOTES, 'UTF-8') . '" zostanie trwale usuniety razem ze wszystkimi zdjeciami i zalacznikami z serwera.' . "\n\n" . 'Czy na pewno chcesz usunac ten produkt?',
'confirm_ok' => 'Tak, usun trwale',
'confirm_cancel' => 'Anuluj',
],
],
];
}
@@ -162,4 +170,24 @@ class ProductArchiveController
header( 'Location: /admin/product_archive/list/' );
exit;
}
public function delete_permanent(): void
{
$productId = (int) \Shared\Helpers\Helpers::get( 'product_id' );
if ( $productId <= 0 ) {
\Shared\Helpers\Helpers::alert( 'Nieprawidłowe ID produktu.' );
header( 'Location: /admin/product_archive/list/' );
exit;
}
if ( $this->productRepository->delete( $productId ) ) {
\Shared\Helpers\Helpers::set_message( 'Produkt został trwale usunięty wraz ze zdjęciami i załącznikami.' );
} else {
\Shared\Helpers\Helpers::alert( 'Podczas usuwania produktu wystąpił błąd. Proszę spróbować ponownie.' );
}
header( 'Location: /admin/product_archive/list/' );
exit;
}
}

View File

@@ -73,26 +73,45 @@ class SettingsController
*/
public function globalSearchAjax(): void
{
global $mdb;
header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-store');
$phrase = trim((string)\Shared\Helpers\Helpers::get('q'));
if ($phrase === '' || mb_strlen($phrase) < 2) {
try {
$this->executeGlobalSearch();
} catch (\Throwable $e) {
echo json_encode([
'status' => 'ok',
'status' => 'error',
'items' => [],
]);
exit;
}
exit;
}
private function executeGlobalSearch(): void
{
global $mdb;
$phrase = isset($_REQUEST['q']) ? trim((string)$_REQUEST['q']) : '';
if ($phrase === '' || mb_strlen($phrase) < 2) {
echo json_encode(['status' => 'ok', 'items' => []]);
return;
}
$phrase = mb_substr($phrase, 0, 120);
$phraseNormalized = preg_replace('/\s+/', ' ', $phrase);
$phraseNormalized = trim((string)$phraseNormalized);
$phraseNormalized = trim((string)preg_replace('/\s+/', ' ', $phrase));
$like = '%' . $phrase . '%';
$likeNormalized = '%' . $phraseNormalized . '%';
$items = [];
$defaultLang = (string)$this->languagesRepository->defaultLanguage();
$defaultLang = '1';
try {
$defaultLang = (string)$this->languagesRepository->defaultLanguage();
} catch (\Throwable $e) {
// fallback to '1'
}
// --- Produkty ---
try {
$productStmt = $mdb->query(
'SELECT '
@@ -115,7 +134,10 @@ class SettingsController
$productStmt = false;
}
$productRows = $productStmt ? $productStmt->fetchAll() : [];
$productRows = ($productStmt && method_exists($productStmt, 'fetchAll'))
? $productStmt->fetchAll(\PDO::FETCH_ASSOC)
: [];
if (is_array($productRows)) {
foreach ($productRows as $row) {
$productId = (int)($row['id'] ?? 0);
@@ -147,6 +169,7 @@ class SettingsController
}
}
// --- Zamowienia ---
try {
$orderStmt = $mdb->query(
'SELECT '
@@ -178,7 +201,10 @@ class SettingsController
$orderStmt = false;
}
$orderRows = $orderStmt ? $orderStmt->fetchAll() : [];
$orderRows = ($orderStmt && method_exists($orderStmt, 'fetchAll'))
? $orderStmt->fetchAll(\PDO::FETCH_ASSOC)
: [];
if (is_array($orderRows)) {
foreach ($orderRows as $row) {
$orderId = (int)($row['id'] ?? 0);
@@ -214,11 +240,12 @@ class SettingsController
}
}
echo json_encode([
'status' => 'ok',
'items' => array_slice($items, 0, 20),
]);
exit;
$json = json_encode(['status' => 'ok', 'items' => array_slice($items, 0, 20)]);
if ($json === false) {
echo json_encode(['status' => 'ok', 'items' => []], JSON_UNESCAPED_UNICODE);
return;
}
echo $json;
}
/**
@@ -444,8 +471,7 @@ class SettingsController
'label' => 'Htaccess cache',
'tab' => 'system',
]),
FormField::text('api_key', [
'label' => 'Klucz API (ordersPRO)',
FormField::custom('api_key', $this->renderApiKeyField($data['api_key'] ?? ''), [
'tab' => 'system',
]),
@@ -533,4 +559,23 @@ class SettingsController
return $data;
}
private function renderApiKeyField(string $value): string
{
$escaped = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
$js = "var c='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',"
. "k='';for(var i=0;i<32;i++){k+=c.charAt(Math.floor(Math.random()*c.length));}"
. "document.getElementById('api_key').value=k;";
return '<div class="form-group row">'
. '<label class="col-lg-4 control-label">Klucz API:</label>'
. '<div class="col-lg-8">'
. '<div class="input-group">'
. '<input type="text" id="api_key" class="form-control" name="api_key" value="' . $escaped . '" />'
. '<span class="input-group-addon btn btn-info" onclick="' . htmlspecialchars($js, ENT_QUOTES, 'UTF-8') . '">Generuj</span>'
. '</div>'
. '</div>'
. '</div>';
}
}

View File

@@ -69,7 +69,9 @@ class ShopOrderController
$listRequest['perPage']
);
$statusesMap = $this->service->statuses();
$statusData = $this->service->statusData();
$statusesMap = $statusData['names'];
$statusColorsMap = $statusData['colors'];
$rows = [];
$lp = ($listRequest['page'] - 1) * $listRequest['perPage'] + 1;
@@ -77,7 +79,15 @@ class ShopOrderController
$orderId = (int)($item['id'] ?? 0);
$orderNumber = (string)($item['number'] ?? '');
$statusId = (int)($item['status'] ?? 0);
$statusLabel = (string)($statusesMap[$statusId] ?? ('Status #' . $statusId));
$statusLabel = htmlspecialchars((string)($statusesMap[$statusId] ?? ('Status #' . $statusId)), ENT_QUOTES, 'UTF-8');
$statusColor = isset($statusColorsMap[$statusId]) ? $statusColorsMap[$statusId] : '';
if ($statusColor !== '') {
$textColor = $this->contrastTextColor($statusColor);
$statusHtml = '<span class="label" style="background-color:' . htmlspecialchars($statusColor, ENT_QUOTES, 'UTF-8') . ';color:' . $textColor . '">' . $statusLabel . '</span>';
} else {
$statusHtml = $statusLabel;
}
$rows[] = [
'lp' => $lp++ . '.',
@@ -86,13 +96,13 @@ class ShopOrderController
'paid' => ((int)($item['paid'] ?? 0) === 1)
? '<i class="fa fa-check text-success"></i>'
: '<i class="fa fa-times text-dark"></i>',
'status' => htmlspecialchars($statusLabel, ENT_QUOTES, 'UTF-8'),
'status' => $statusHtml,
'summary' => number_format((float)($item['summary'] ?? 0), 2, '.', ' ') . ' zł',
'client' => htmlspecialchars((string)($item['client'] ?? ''), ENT_QUOTES, 'UTF-8') . ' | zamówienia: <strong>' . (int)($item['total_orders'] ?? 0) . '</strong>',
'address' => (string)($item['address'] ?? ''),
'order_email' => (string)($item['order_email'] ?? ''),
'client_phone' => (string)($item['client_phone'] ?? ''),
'transport' => (string)($item['transport'] ?? ''),
'transport' => $this->sanitizeInlineHtml((string)($item['transport'] ?? '')),
'payment_method' => (string)($item['payment_method'] ?? ''),
'_actions' => [
[
@@ -127,7 +137,7 @@ class ShopOrderController
['key' => 'address', 'label' => 'Adres', 'sortable' => false],
['key' => 'order_email', 'sort_key' => 'order_email', 'label' => 'Email', 'sortable' => true],
['key' => 'client_phone', 'sort_key' => 'client_phone', 'label' => 'Telefon', 'sortable' => true],
['key' => 'transport', 'sort_key' => 'transport', 'label' => 'Dostawa', 'sortable' => true],
['key' => 'transport', 'sort_key' => 'transport', 'label' => 'Dostawa', 'sortable' => true, 'raw' => true],
['key' => 'payment_method', 'sort_key' => 'payment_method', 'label' => 'Płatność', 'sortable' => true],
],
$rows,
@@ -361,4 +371,26 @@ class ShopOrderController
return date('Y-m-d H:i', $ts);
}
}
private function contrastTextColor(string $hex): string
{
$hex = ltrim($hex, '#');
if (strlen($hex) === 3) {
$hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
}
if (strlen($hex) !== 6) {
return '#fff';
}
$r = hexdec(substr($hex, 0, 2));
$g = hexdec(substr($hex, 2, 2));
$b = hexdec(substr($hex, 4, 2));
$luminance = (0.299 * $r + 0.587 * $g + 0.114 * $b) / 255;
return $luminance > 0.5 ? '#000' : '#fff';
}
private function sanitizeInlineHtml(string $html): string
{
$html = strip_tags($html, '<b><strong><i><em>');
return preg_replace('/<(b|strong|i|em)\s[^>]*>/i', '<$1>', $html);
}
}

View File

@@ -182,6 +182,8 @@ class ShopPaymentMethodController
'description' => (string)($paymentMethod['description'] ?? ''),
'status' => (int)($paymentMethod['status'] ?? 0),
'apilo_payment_type_id' => $paymentMethod['apilo_payment_type_id'] ?? '',
'min_order_amount' => $paymentMethod['min_order_amount'] ?? '',
'max_order_amount' => $paymentMethod['max_order_amount'] ?? '',
];
$fields = [
@@ -203,6 +205,16 @@ class ShopPaymentMethodController
'tab' => 'settings',
'rows' => 5,
]),
FormField::number('min_order_amount', [
'label' => 'Min. kwota zamowienia (PLN)',
'tab' => 'settings',
'step' => 0.01,
]),
FormField::number('max_order_amount', [
'label' => 'Maks. kwota zamowienia (PLN)',
'tab' => 'settings',
'step' => 0.01,
]),
FormField::select('apilo_payment_type_id', [
'label' => 'Typ platnosci Apilo',
'tab' => 'settings',

View File

@@ -95,7 +95,7 @@ class ShopProductController
. '<a href="/admin/shop_product/product_edit/id=' . $id . '">' . $name . '</a> '
. '<a href="#" class="text-muted duplicate-product" product-id="' . $id . '">duplikuj</a>'
. '</div>'
. '<small class="text-muted product-categories">' . $categories . '</small>'
. '<small class="text-muted product-categories product-categories--cats" title="' . $categories . '">' . $categories . '</small>'
. '<small class="text-muted product-categories">SKU: ' . $sku . ', EAN: ' . $ean . '</small>';
$priceHtml = '<input type="text" class="product-price form-control text-right" product-id="' . $id . '" value="' . htmlspecialchars( (string) $product['price_brutto'], ENT_QUOTES, 'UTF-8' ) . '" style="width: 75px;">';
@@ -140,6 +140,7 @@ class ShopProductController
}
}
$rows[] = $row;
}
@@ -547,6 +548,11 @@ class ShopProductController
FormAction::cancel( $backUrl ),
];
if ( $productId > 0 ) {
$previewUrl = $this->repository->getProductUrl( $productId );
$actions[] = FormAction::preview( $previewUrl );
}
return new FormEditViewModel(
'product-edit',
$title,
@@ -683,7 +689,7 @@ class ShopProductController
foreach ( $products as $key => $val ) {
if ( (int) $key !== $productId ) {
$selected = ( is_array( $product['products_related'] ?? null ) && in_array( $key, $product['products_related'] ) ) ? ' selected' : '';
$html .= '<option value="' . (int) $key . '"' . $selected . '>' . $this->escapeHtml( $val ) . '</option>';
$html .= '<option value="' . (int) $key . '"' . $selected . '>' . $this->escapeHtml( (string) $val ) . '</option>';
}
}
$html .= '</select></div></div>';

View File

@@ -14,9 +14,12 @@ class UpdateController
public function main_view(): string
{
$logContent = @file_get_contents( '../libraries/update_log.txt' );
return \Shared\Tpl\Tpl::view( 'update/main-view', [
'ver' => \Shared\Helpers\Helpers::get_version(),
'new_ver' => \Shared\Helpers\Helpers::get_new_version(),
'log' => $logContent ?: '',
] );
}
@@ -46,4 +49,17 @@ class UpdateController
echo json_encode( $response );
exit;
}
public function checkUpdate(): void
{
\Shared\Helpers\Helpers::set_session( 'new-version', null );
$newVer = \Shared\Helpers\Helpers::get_new_version();
$curVer = \Shared\Helpers\Helpers::get_version();
echo json_encode( [
'has_update' => $newVer > $curVer,
'new_ver' => $newVer,
] );
exit;
}
}