Files
pomysloweprezenty.pl/autoload/api/Controllers/DictionariesApiController.php
Jacek Pyziak 31fd0442b2 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.
2026-02-27 14:54:05 +01:00

209 lines
5.7 KiB
PHP

<?php
namespace api\Controllers;
use api\ApiRouter;
use Domain\Attribute\AttributeRepository;
use Domain\Producer\ProducerRepository;
use Domain\ShopStatus\ShopStatusRepository;
use Domain\Transport\TransportRepository;
use Domain\PaymentMethod\PaymentMethodRepository;
class DictionariesApiController
{
private $statusRepo;
private $transportRepo;
private $paymentRepo;
private $attrRepo;
private $producerRepo;
public function __construct(
ShopStatusRepository $statusRepo,
TransportRepository $transportRepo,
PaymentMethodRepository $paymentRepo,
AttributeRepository $attrRepo,
ProducerRepository $producerRepo
) {
$this->statusRepo = $statusRepo;
$this->transportRepo = $transportRepo;
$this->paymentRepo = $paymentRepo;
$this->attrRepo = $attrRepo;
$this->producerRepo = $producerRepo;
}
public function statuses(): void
{
if (!ApiRouter::requireMethod('GET')) {
return;
}
$statuses = $this->statusRepo->allStatuses();
$result = [];
foreach ($statuses as $id => $name) {
$result[] = [
'id' => (int)$id,
'name' => (string)$name,
];
}
ApiRouter::sendSuccess($result);
}
public function transports(): void
{
if (!ApiRouter::requireMethod('GET')) {
return;
}
$transports = $this->transportRepo->allActive();
$result = [];
foreach ($transports as $transport) {
$result[] = [
'id' => (int)($transport['id'] ?? 0),
'name' => (string)($transport['name_visible'] ?? $transport['name'] ?? ''),
'cost' => (float)($transport['cost'] ?? 0),
];
}
ApiRouter::sendSuccess($result);
}
public function payment_methods(): void
{
if (!ApiRouter::requireMethod('GET')) {
return;
}
$methods = $this->paymentRepo->allActive();
$result = [];
foreach ($methods as $method) {
$result[] = [
'id' => (int)($method['id'] ?? 0),
'name' => (string)($method['name'] ?? ''),
];
}
ApiRouter::sendSuccess($result);
}
public function attributes(): void
{
if (!ApiRouter::requireMethod('GET')) {
return;
}
$attributes = $this->attrRepo->listForApi();
ApiRouter::sendSuccess($attributes);
}
public function ensure_attribute(): void
{
if (!ApiRouter::requireMethod('POST')) {
return;
}
$body = ApiRouter::getJsonBody();
if (!is_array($body)) {
ApiRouter::sendError('BAD_REQUEST', 'Missing or invalid JSON body', 400);
return;
}
$name = trim((string) ($body['name'] ?? ''));
if ($name === '') {
ApiRouter::sendError('BAD_REQUEST', 'Missing name', 400);
return;
}
$type = (int) ($body['type'] ?? 0);
$lang = trim((string) ($body['lang'] ?? 'pl'));
if ($lang === '') {
$lang = 'pl';
}
$result = $this->attrRepo->ensureAttributeForApi($name, $type, $lang);
if (!is_array($result) || (int) ($result['id'] ?? 0) <= 0) {
ApiRouter::sendError('INTERNAL_ERROR', 'Failed to ensure attribute', 500);
return;
}
ApiRouter::sendSuccess([
'id' => (int) ($result['id'] ?? 0),
'created' => !empty($result['created']),
]);
}
public function ensure_attribute_value(): void
{
if (!ApiRouter::requireMethod('POST')) {
return;
}
$body = ApiRouter::getJsonBody();
if (!is_array($body)) {
ApiRouter::sendError('BAD_REQUEST', 'Missing or invalid JSON body', 400);
return;
}
$attributeId = (int) ($body['attribute_id'] ?? 0);
if ($attributeId <= 0) {
ApiRouter::sendError('BAD_REQUEST', 'Missing or invalid attribute_id', 400);
return;
}
$name = trim((string) ($body['name'] ?? ''));
if ($name === '') {
ApiRouter::sendError('BAD_REQUEST', 'Missing name', 400);
return;
}
$lang = trim((string) ($body['lang'] ?? 'pl'));
if ($lang === '') {
$lang = 'pl';
}
$result = $this->attrRepo->ensureAttributeValueForApi($attributeId, $name, $lang);
if (!is_array($result) || (int) ($result['id'] ?? 0) <= 0) {
ApiRouter::sendError('INTERNAL_ERROR', 'Failed to ensure attribute value', 500);
return;
}
ApiRouter::sendSuccess([
'id' => (int) ($result['id'] ?? 0),
'created' => !empty($result['created']),
]);
}
public function ensure_producer(): void
{
if (!ApiRouter::requireMethod('POST')) {
return;
}
$body = ApiRouter::getJsonBody();
if (!is_array($body)) {
ApiRouter::sendError('BAD_REQUEST', 'Missing or invalid JSON body', 400);
return;
}
$name = trim((string) ($body['name'] ?? ''));
if ($name === '') {
ApiRouter::sendError('BAD_REQUEST', 'Missing name', 400);
return;
}
$result = $this->producerRepo->ensureProducerForApi($name);
if ((int) ($result['id'] ?? 0) <= 0) {
ApiRouter::sendError('INTERNAL_ERROR', 'Failed to ensure producer', 500);
return;
}
ApiRouter::sendSuccess([
'id' => (int) ($result['id'] ?? 0),
'created' => !empty($result['created']),
]);
}
}