feat: Add Transport module with repository, controller, and views
- Implemented TransportRepository for managing transport data with methods for listing, finding, saving, and retrieving transport costs. - Created ShopTransportController to handle transport-related actions, including listing, editing, and saving transports. - Added views for transport management: transports list and transport edit forms. - Introduced JavaScript for responsive tabs in transport edit view. - Updated testing suite with comprehensive unit tests for TransportRepository and ShopTransportController. - Increased test coverage with new assertions and scenarios for transport functionalities.
This commit is contained in:
342
autoload/Domain/Transport/TransportRepository.php
Normal file
342
autoload/Domain/Transport/TransportRepository.php
Normal file
@@ -0,0 +1,342 @@
|
||||
<?php
|
||||
namespace Domain\Transport;
|
||||
|
||||
class TransportRepository
|
||||
{
|
||||
private const MAX_PER_PAGE = 100;
|
||||
|
||||
private $db;
|
||||
|
||||
public function __construct($db)
|
||||
{
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
public function listForAdmin(
|
||||
array $filters = [],
|
||||
string $sortColumn = 'name',
|
||||
string $sortDir = 'ASC',
|
||||
int $page = 1,
|
||||
int $perPage = 15
|
||||
): array {
|
||||
$allowedSortColumns = [
|
||||
'id' => 'st.id',
|
||||
'name' => 'st.name',
|
||||
'status' => 'st.status',
|
||||
'cost' => 'st.cost',
|
||||
'max_wp' => 'st.max_wp',
|
||||
'default' => 'st.default',
|
||||
'o' => 'st.o',
|
||||
];
|
||||
|
||||
$sortSql = $allowedSortColumns[$sortColumn] ?? 'st.name';
|
||||
$sortDir = strtoupper(trim($sortDir)) === 'DESC' ? 'DESC' : 'ASC';
|
||||
$page = max(1, $page);
|
||||
$perPage = min(self::MAX_PER_PAGE, max(1, $perPage));
|
||||
$offset = ($page - 1) * $perPage;
|
||||
|
||||
$where = ['1 = 1'];
|
||||
$params = [];
|
||||
|
||||
$name = trim((string)($filters['name'] ?? ''));
|
||||
if ($name !== '') {
|
||||
if (strlen($name) > 255) {
|
||||
$name = substr($name, 0, 255);
|
||||
}
|
||||
$where[] = 'st.name LIKE :name';
|
||||
$params[':name'] = '%' . $name . '%';
|
||||
}
|
||||
|
||||
$status = trim((string)($filters['status'] ?? ''));
|
||||
if ($status === '0' || $status === '1') {
|
||||
$where[] = 'st.status = :status';
|
||||
$params[':status'] = (int)$status;
|
||||
}
|
||||
|
||||
$whereSql = implode(' AND ', $where);
|
||||
|
||||
$sqlCount = "
|
||||
SELECT COUNT(0)
|
||||
FROM pp_shop_transports AS st
|
||||
WHERE {$whereSql}
|
||||
";
|
||||
|
||||
$stmtCount = $this->db->query($sqlCount, $params);
|
||||
$countRows = $stmtCount ? $stmtCount->fetchAll() : [];
|
||||
$total = isset($countRows[0][0]) ? (int)$countRows[0][0] : 0;
|
||||
|
||||
$sql = "
|
||||
SELECT
|
||||
st.id,
|
||||
st.name,
|
||||
st.name_visible,
|
||||
st.description,
|
||||
st.status,
|
||||
st.cost,
|
||||
st.max_wp,
|
||||
st.default,
|
||||
st.apilo_carrier_account_id,
|
||||
st.delivery_free,
|
||||
st.o
|
||||
FROM pp_shop_transports AS st
|
||||
WHERE {$whereSql}
|
||||
ORDER BY {$sortSql} {$sortDir}, st.id ASC
|
||||
LIMIT {$perPage} OFFSET {$offset}
|
||||
";
|
||||
|
||||
$stmt = $this->db->query($sql, $params);
|
||||
$items = $stmt ? $stmt->fetchAll() : [];
|
||||
|
||||
if (!is_array($items)) {
|
||||
$items = [];
|
||||
}
|
||||
|
||||
foreach ($items as &$item) {
|
||||
$item = $this->normalizeTransport($item);
|
||||
}
|
||||
unset($item);
|
||||
|
||||
return [
|
||||
'items' => $items,
|
||||
'total' => $total,
|
||||
];
|
||||
}
|
||||
|
||||
public function find(int $transportId): ?array
|
||||
{
|
||||
if ($transportId <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$transport = $this->db->get('pp_shop_transports', '*', ['id' => $transportId]);
|
||||
|
||||
if (!is_array($transport)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$transport = $this->normalizeTransport($transport);
|
||||
|
||||
$paymentMethods = $this->db->select(
|
||||
'pp_shop_transport_payment_methods',
|
||||
'id_payment_method',
|
||||
['id_transport' => $transportId]
|
||||
);
|
||||
|
||||
$transport['payment_methods'] = is_array($paymentMethods) ? $paymentMethods : [];
|
||||
|
||||
return $transport;
|
||||
}
|
||||
|
||||
public function save(array $data): ?int
|
||||
{
|
||||
$transportId = isset($data['id']) ? (int)$data['id'] : 0;
|
||||
$name = trim((string)($data['name'] ?? ''));
|
||||
$nameVisible = trim((string)($data['name_visible'] ?? ''));
|
||||
$description = trim((string)($data['description'] ?? ''));
|
||||
$status = $this->toSwitchValue($data['status'] ?? 0);
|
||||
$cost = isset($data['cost']) ? (float)$data['cost'] : 0.0;
|
||||
$maxWp = isset($data['max_wp']) && $data['max_wp'] !== '' ? (int)$data['max_wp'] : null;
|
||||
$default = $this->toSwitchValue($data['default'] ?? 0);
|
||||
$apiloCarrierAccountId = isset($data['apilo_carrier_account_id']) && $data['apilo_carrier_account_id'] !== ''
|
||||
? (int)$data['apilo_carrier_account_id']
|
||||
: null;
|
||||
$deliveryFree = $this->toSwitchValue($data['delivery_free'] ?? 0);
|
||||
$paymentMethods = $data['payment_methods'] ?? [];
|
||||
|
||||
if ($default === 1) {
|
||||
$this->db->update('pp_shop_transports', ['default' => 0]);
|
||||
}
|
||||
|
||||
$transportData = [
|
||||
'name' => $name,
|
||||
'name_visible' => $nameVisible,
|
||||
'description' => $description,
|
||||
'status' => $status,
|
||||
'default' => $default,
|
||||
'cost' => $cost,
|
||||
'max_wp' => $maxWp,
|
||||
'apilo_carrier_account_id' => $apiloCarrierAccountId,
|
||||
'delivery_free' => $deliveryFree,
|
||||
];
|
||||
|
||||
if (!$transportId) {
|
||||
$this->db->insert('pp_shop_transports', $transportData);
|
||||
$id = $this->db->id();
|
||||
|
||||
if ($id) {
|
||||
$this->savePaymentMethodLinks((int)$id, $paymentMethods);
|
||||
return (int)$id;
|
||||
}
|
||||
|
||||
return null;
|
||||
} else {
|
||||
$this->db->update('pp_shop_transports', $transportData, ['id' => $transportId]);
|
||||
$this->db->delete('pp_shop_transport_payment_methods', ['id_transport' => $transportId]);
|
||||
$this->savePaymentMethodLinks($transportId, $paymentMethods);
|
||||
return $transportId;
|
||||
}
|
||||
}
|
||||
|
||||
public function allActive(): array
|
||||
{
|
||||
$transports = $this->db->select(
|
||||
'pp_shop_transports',
|
||||
'*',
|
||||
[
|
||||
'status' => 1,
|
||||
'ORDER' => ['o' => 'ASC'],
|
||||
]
|
||||
);
|
||||
|
||||
if (!is_array($transports)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
foreach ($transports as &$transport) {
|
||||
$transport = $this->normalizeTransport($transport);
|
||||
}
|
||||
unset($transport);
|
||||
|
||||
return $transports;
|
||||
}
|
||||
|
||||
public function findActiveById(int $transportId): ?array
|
||||
{
|
||||
if ($transportId <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$transport = $this->db->get(
|
||||
'pp_shop_transports',
|
||||
'*',
|
||||
['AND' => ['id' => $transportId, 'status' => 1]]
|
||||
);
|
||||
|
||||
if (!$transport) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->normalizeTransport($transport);
|
||||
}
|
||||
|
||||
public function getApiloCarrierAccountId(int $transportId): ?int
|
||||
{
|
||||
if ($transportId <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$result = $this->db->get(
|
||||
'pp_shop_transports',
|
||||
'apilo_carrier_account_id',
|
||||
['id' => $transportId]
|
||||
);
|
||||
|
||||
return $result !== null ? (int)$result : null;
|
||||
}
|
||||
|
||||
public function getTransportCost(int $transportId): ?float
|
||||
{
|
||||
if ($transportId <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$result = $this->db->get(
|
||||
'pp_shop_transports',
|
||||
'cost',
|
||||
['AND' => ['id' => $transportId, 'status' => 1]]
|
||||
);
|
||||
|
||||
return $result !== null ? (float)$result : null;
|
||||
}
|
||||
|
||||
public function lowestTransportPrice(int $wp): ?float
|
||||
{
|
||||
$result = $this->db->get(
|
||||
'pp_shop_transports',
|
||||
'cost',
|
||||
[
|
||||
'AND' => [
|
||||
'status' => 1,
|
||||
'id' => [2, 4, 6, 8, 9],
|
||||
'max_wp[>=]' => $wp
|
||||
],
|
||||
'ORDER' => ['cost' => 'ASC']
|
||||
]
|
||||
);
|
||||
|
||||
return $result !== null ? (float)$result : null;
|
||||
}
|
||||
|
||||
public function allForAdmin(): array
|
||||
{
|
||||
$transports = $this->db->select(
|
||||
'pp_shop_transports',
|
||||
'*',
|
||||
['ORDER' => ['o' => 'ASC']]
|
||||
);
|
||||
|
||||
if (!is_array($transports)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
foreach ($transports as &$transport) {
|
||||
$transport = $this->normalizeTransport($transport);
|
||||
}
|
||||
unset($transport);
|
||||
|
||||
return $transports;
|
||||
}
|
||||
|
||||
private function savePaymentMethodLinks(int $transportId, $paymentMethods): void
|
||||
{
|
||||
if (is_array($paymentMethods)) {
|
||||
foreach ($paymentMethods as $paymentMethodId) {
|
||||
$this->db->insert('pp_shop_transport_payment_methods', [
|
||||
'id_payment_method' => (int)$paymentMethodId,
|
||||
'id_transport' => $transportId,
|
||||
]);
|
||||
}
|
||||
} elseif ($paymentMethods) {
|
||||
$this->db->insert('pp_shop_transport_payment_methods', [
|
||||
'id_payment_method' => (int)$paymentMethods,
|
||||
'id_transport' => $transportId,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
private function normalizeTransport(array $transport): array
|
||||
{
|
||||
$transport['id'] = isset($transport['id']) ? (int)$transport['id'] : 0;
|
||||
$transport['status'] = $this->toSwitchValue($transport['status'] ?? 0);
|
||||
$transport['default'] = $this->toSwitchValue($transport['default'] ?? 0);
|
||||
$transport['delivery_free'] = $this->toSwitchValue($transport['delivery_free'] ?? 0);
|
||||
$transport['cost'] = isset($transport['cost']) ? (float)$transport['cost'] : 0.0;
|
||||
$transport['max_wp'] = isset($transport['max_wp']) && $transport['max_wp'] !== null
|
||||
? (int)$transport['max_wp']
|
||||
: null;
|
||||
$transport['apilo_carrier_account_id'] = isset($transport['apilo_carrier_account_id']) && $transport['apilo_carrier_account_id'] !== null
|
||||
? (int)$transport['apilo_carrier_account_id']
|
||||
: null;
|
||||
$transport['o'] = isset($transport['o']) ? (int)$transport['o'] : 0;
|
||||
|
||||
return $transport;
|
||||
}
|
||||
|
||||
private function toSwitchValue($value): int
|
||||
{
|
||||
if (is_bool($value)) {
|
||||
return $value ? 1 : 0;
|
||||
}
|
||||
|
||||
if (is_numeric($value)) {
|
||||
return ((int)$value) === 1 ? 1 : 0;
|
||||
}
|
||||
|
||||
if (is_string($value)) {
|
||||
$normalized = strtolower(trim($value));
|
||||
return in_array($normalized, ['1', 'on', 'true', 'yes'], true) ? 1 : 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
342
autoload/admin/Controllers/ShopTransportController.php
Normal file
342
autoload/admin/Controllers/ShopTransportController.php
Normal file
@@ -0,0 +1,342 @@
|
||||
<?php
|
||||
namespace admin\Controllers;
|
||||
|
||||
use Domain\Transport\TransportRepository;
|
||||
use Domain\PaymentMethod\PaymentMethodRepository;
|
||||
use admin\ViewModels\Common\PaginatedTableViewModel;
|
||||
use admin\ViewModels\Forms\FormAction;
|
||||
use admin\ViewModels\Forms\FormEditViewModel;
|
||||
use admin\ViewModels\Forms\FormField;
|
||||
use admin\ViewModels\Forms\FormTab;
|
||||
|
||||
class ShopTransportController
|
||||
{
|
||||
private TransportRepository $transportRepository;
|
||||
private PaymentMethodRepository $paymentMethodRepository;
|
||||
|
||||
public function __construct(
|
||||
TransportRepository $transportRepository,
|
||||
PaymentMethodRepository $paymentMethodRepository
|
||||
) {
|
||||
$this->transportRepository = $transportRepository;
|
||||
$this->paymentMethodRepository = $paymentMethodRepository;
|
||||
}
|
||||
|
||||
public function list(): string
|
||||
{
|
||||
$sortableColumns = ['id', 'name', 'status', 'cost', 'max_wp', 'default', 'o'];
|
||||
$filterDefinitions = [
|
||||
[
|
||||
'key' => 'name',
|
||||
'label' => 'Nazwa',
|
||||
'type' => 'text',
|
||||
],
|
||||
[
|
||||
'key' => 'status',
|
||||
'label' => 'Aktywny',
|
||||
'type' => 'select',
|
||||
'options' => [
|
||||
'' => '- aktywny -',
|
||||
'1' => 'tak',
|
||||
'0' => 'nie',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$listRequest = \admin\Support\TableListRequestFactory::fromRequest(
|
||||
$filterDefinitions,
|
||||
$sortableColumns,
|
||||
'name'
|
||||
);
|
||||
|
||||
$sortDir = $listRequest['sortDir'];
|
||||
if (trim((string)\S::get('sort')) === '') {
|
||||
$sortDir = 'ASC';
|
||||
}
|
||||
|
||||
$result = $this->transportRepository->listForAdmin(
|
||||
$listRequest['filters'],
|
||||
$listRequest['sortColumn'],
|
||||
$sortDir,
|
||||
$listRequest['page'],
|
||||
$listRequest['perPage']
|
||||
);
|
||||
|
||||
$apiloCarrierAccounts = $this->getApiloCarrierAccounts();
|
||||
|
||||
$rows = [];
|
||||
$lp = ($listRequest['page'] - 1) * $listRequest['perPage'] + 1;
|
||||
foreach ($result['items'] as $item) {
|
||||
$id = (int)($item['id'] ?? 0);
|
||||
$name = trim((string)($item['name'] ?? ''));
|
||||
$status = (int)($item['status'] ?? 0);
|
||||
$cost = (float)($item['cost'] ?? 0.0);
|
||||
$maxWp = $item['max_wp'] ?? null;
|
||||
$default = (int)($item['default'] ?? 0);
|
||||
$apiloCarrierAccountId = $item['apilo_carrier_account_id'] ?? null;
|
||||
|
||||
$apiloLabel = '-';
|
||||
if ($apiloCarrierAccountId !== null) {
|
||||
$apiloKey = (string)$apiloCarrierAccountId;
|
||||
if (isset($apiloCarrierAccounts[$apiloKey])) {
|
||||
$apiloLabel = $apiloCarrierAccounts[$apiloKey];
|
||||
}
|
||||
}
|
||||
|
||||
$rows[] = [
|
||||
'lp' => $lp++ . '.',
|
||||
'default' => $default === 1 ? 'tak' : '<span style="color: #FF0000;">nie</span>',
|
||||
'status' => $status === 1 ? 'tak' : '<span style="color: #FF0000;">nie</span>',
|
||||
'cost' => \S::decimal($cost) . ' zł',
|
||||
'max_wp' => $maxWp !== null ? (int)$maxWp : '-',
|
||||
'name' => '<a href="/admin/shop_transport/edit/id=' . $id . '">' . htmlspecialchars($name, ENT_QUOTES, 'UTF-8') . '</a>',
|
||||
'apilo_carrier' => htmlspecialchars((string)$apiloLabel, ENT_QUOTES, 'UTF-8'),
|
||||
'_actions' => [
|
||||
[
|
||||
'label' => 'Edytuj',
|
||||
'url' => '/admin/shop_transport/edit/id=' . $id,
|
||||
'class' => 'btn btn-xs btn-primary',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
$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' => 'default', 'sort_key' => 'default', 'label' => 'Domyślna FT', 'class' => 'text-center', 'sortable' => true, 'raw' => true],
|
||||
['key' => 'status', 'sort_key' => 'status', 'label' => 'Aktywny', 'class' => 'text-center', 'sortable' => true, 'raw' => true],
|
||||
['key' => 'cost', 'sort_key' => 'cost', 'label' => 'Koszt', 'class' => 'text-center', 'sortable' => true, 'raw' => true],
|
||||
['key' => 'max_wp', 'sort_key' => 'max_wp', 'label' => 'Maks. WP', 'class' => 'text-center', 'sortable' => true, 'raw' => true],
|
||||
['key' => 'name', 'sort_key' => 'name', 'label' => 'Nazwa', 'sortable' => true, 'raw' => true],
|
||||
['key' => 'apilo_carrier', 'label' => 'Typ kuriera Apilo', 'class' => 'text-center', 'sortable' => false],
|
||||
],
|
||||
$rows,
|
||||
$listRequest['viewFilters'],
|
||||
[
|
||||
'column' => $listRequest['sortColumn'],
|
||||
'dir' => $sortDir,
|
||||
],
|
||||
[
|
||||
'page' => $listRequest['page'],
|
||||
'per_page' => $listRequest['perPage'],
|
||||
'total' => $total,
|
||||
'total_pages' => $totalPages,
|
||||
],
|
||||
array_merge($listRequest['queryFilters'], [
|
||||
'sort' => $listRequest['sortColumn'],
|
||||
'dir' => $sortDir,
|
||||
'per_page' => $listRequest['perPage'],
|
||||
]),
|
||||
$listRequest['perPageOptions'],
|
||||
$sortableColumns,
|
||||
'/admin/shop_transport/list/',
|
||||
'Brak danych w tabeli.'
|
||||
);
|
||||
|
||||
return \Tpl::view('shop-transport/transports-list', [
|
||||
'viewModel' => $viewModel,
|
||||
]);
|
||||
}
|
||||
|
||||
public function edit(): string
|
||||
{
|
||||
$transport = $this->transportRepository->find((int)\S::get('id'));
|
||||
if ($transport === null) {
|
||||
\S::alert('Rodzaj transportu nie został znaleziony.');
|
||||
header('Location: /admin/shop_transport/list/');
|
||||
exit;
|
||||
}
|
||||
|
||||
$paymentMethods = $this->paymentMethodRepository->allForAdmin();
|
||||
$apiloCarrierAccounts = $this->getApiloCarrierAccounts();
|
||||
|
||||
return \Tpl::view('shop-transport/transport-edit', [
|
||||
'form' => $this->buildFormViewModel($transport, $paymentMethods, $apiloCarrierAccounts),
|
||||
]);
|
||||
}
|
||||
|
||||
public function save(): void
|
||||
{
|
||||
$payload = $_POST;
|
||||
$transportId = isset($payload['id']) && $payload['id'] !== ''
|
||||
? (int)$payload['id']
|
||||
: (int)\S::get('id');
|
||||
|
||||
$payload['id'] = $transportId;
|
||||
|
||||
$id = $this->transportRepository->save($payload);
|
||||
if ($id !== null) {
|
||||
\S::delete_dir('../temp/');
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'id' => (int)$id,
|
||||
'message' => 'Rodzaj transportu został zapisany.',
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'errors' => ['general' => 'Podczas zapisywania rodzaju transportu wystąpił błąd.'],
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
private function buildFormViewModel(
|
||||
array $transport,
|
||||
array $paymentMethods,
|
||||
array $apiloCarrierAccounts
|
||||
): FormEditViewModel {
|
||||
$id = (int)($transport['id'] ?? 0);
|
||||
$name = (string)($transport['name'] ?? '');
|
||||
|
||||
$apiloOptions = ['' => '--- wybierz konto przewoźnika ---'];
|
||||
foreach ($apiloCarrierAccounts as $carrierId => $carrierName) {
|
||||
$apiloOptions[(string)$carrierId] = $carrierName;
|
||||
}
|
||||
|
||||
$data = [
|
||||
'id' => $id,
|
||||
'name' => $name,
|
||||
'name_visible' => (string)($transport['name_visible'] ?? ''),
|
||||
'description' => (string)($transport['description'] ?? ''),
|
||||
'cost' => (float)($transport['cost'] ?? 0.0),
|
||||
'max_wp' => $transport['max_wp'] ?? '',
|
||||
'default' => (int)($transport['default'] ?? 0),
|
||||
'status' => (int)($transport['status'] ?? 0),
|
||||
'delivery_free' => (int)($transport['delivery_free'] ?? 0),
|
||||
'apilo_carrier_account_id' => $transport['apilo_carrier_account_id'] ?? '',
|
||||
];
|
||||
|
||||
$fields = [
|
||||
FormField::hidden('id', $id),
|
||||
FormField::text('name', [
|
||||
'label' => 'Nazwa',
|
||||
'tab' => 'general',
|
||||
'readonly' => true,
|
||||
'required' => true,
|
||||
]),
|
||||
FormField::text('name_visible', [
|
||||
'label' => 'Nazwa widoczna',
|
||||
'tab' => 'general',
|
||||
]),
|
||||
FormField::text('description', [
|
||||
'label' => 'Opis',
|
||||
'tab' => 'general',
|
||||
]),
|
||||
FormField::number('cost', [
|
||||
'label' => 'Koszt (PLN)',
|
||||
'tab' => 'general',
|
||||
'step' => 0.01,
|
||||
'required' => true,
|
||||
]),
|
||||
FormField::number('max_wp', [
|
||||
'label' => 'Maks. WP',
|
||||
'tab' => 'general',
|
||||
'required' => true,
|
||||
]),
|
||||
FormField::switch('default', [
|
||||
'label' => 'Domyślna forma dostawy',
|
||||
'tab' => 'general',
|
||||
]),
|
||||
FormField::switch('status', [
|
||||
'label' => 'Aktywny',
|
||||
'tab' => 'general',
|
||||
]),
|
||||
FormField::switch('delivery_free', [
|
||||
'label' => 'Darmowa dostawa',
|
||||
'tab' => 'general',
|
||||
]),
|
||||
FormField::select('apilo_carrier_account_id', [
|
||||
'label' => 'Kurier z Apilo',
|
||||
'tab' => 'general',
|
||||
'options' => $apiloOptions,
|
||||
]),
|
||||
];
|
||||
|
||||
$transportPaymentMethods = $transport['payment_methods'] ?? [];
|
||||
|
||||
$paymentMethodsHtml = '';
|
||||
if (is_array($paymentMethods) && !empty($paymentMethods)) {
|
||||
foreach ($paymentMethods as $paymentMethod) {
|
||||
$pmId = (int)($paymentMethod['id'] ?? 0);
|
||||
$pmName = htmlspecialchars((string)($paymentMethod['name'] ?? ''), ENT_QUOTES, 'UTF-8');
|
||||
$pmStatus = (int)($paymentMethod['status'] ?? 0);
|
||||
$checked = in_array($pmId, $transportPaymentMethods) ? 'checked="checked"' : '';
|
||||
$statusClass = $pmStatus === 0 ? 'text-muted' : '';
|
||||
|
||||
$paymentMethodsHtml .= '<div class="form-group">';
|
||||
$paymentMethodsHtml .= '<div class="col-lg-12">';
|
||||
$paymentMethodsHtml .= '<div class="list">';
|
||||
$paymentMethodsHtml .= '<input type="checkbox" class="g-checkbox" name="payment_methods[]" value="' . $pmId . '" ' . $checked . ' />';
|
||||
$paymentMethodsHtml .= '<span class="bold ' . $statusClass . '">' . $pmName . '</span>';
|
||||
$paymentMethodsHtml .= '</div>';
|
||||
$paymentMethodsHtml .= '</div>';
|
||||
$paymentMethodsHtml .= '</div>';
|
||||
}
|
||||
}
|
||||
|
||||
$fields[] = FormField::custom(
|
||||
'payment_methods_section',
|
||||
$paymentMethodsHtml,
|
||||
['tab' => 'payment_methods']
|
||||
);
|
||||
|
||||
$tabs = [
|
||||
new FormTab('general', 'Ogólne', 'fa-file'),
|
||||
new FormTab('payment_methods', 'Powiązane metody płatności', 'fa-wrench'),
|
||||
];
|
||||
|
||||
$actionUrl = '/admin/shop_transport/save/id=' . $id;
|
||||
$actions = [
|
||||
FormAction::save($actionUrl, '/admin/shop_transport/list/'),
|
||||
FormAction::cancel('/admin/shop_transport/list/'),
|
||||
];
|
||||
|
||||
return new FormEditViewModel(
|
||||
'shop-transport-edit',
|
||||
'Edycja rodzaju transportu: ' . $name,
|
||||
$data,
|
||||
$fields,
|
||||
$tabs,
|
||||
$actions,
|
||||
'POST',
|
||||
$actionUrl,
|
||||
'/admin/shop_transport/list/',
|
||||
true,
|
||||
['id' => $id]
|
||||
);
|
||||
}
|
||||
|
||||
private function getApiloCarrierAccounts(): array
|
||||
{
|
||||
$rawSetting = \admin\factory\Integrations::apilo_settings('carrier-account-list');
|
||||
$raw = null;
|
||||
|
||||
if (is_array($rawSetting)) {
|
||||
$raw = $rawSetting;
|
||||
} elseif (is_string($rawSetting)) {
|
||||
$decoded = @unserialize($rawSetting);
|
||||
if (is_array($decoded)) {
|
||||
$raw = $decoded;
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_array($raw)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$list = [];
|
||||
foreach ($raw as $carrier) {
|
||||
if (is_array($carrier) && isset($carrier['id'], $carrier['name'])) {
|
||||
$list[(string)$carrier['id']] = (string)$carrier['name'];
|
||||
}
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
}
|
||||
@@ -323,6 +323,14 @@ class Site
|
||||
new \Domain\PaymentMethod\PaymentMethodRepository( $mdb )
|
||||
);
|
||||
},
|
||||
'ShopTransport' => function() {
|
||||
global $mdb;
|
||||
|
||||
return new \admin\Controllers\ShopTransportController(
|
||||
new \Domain\Transport\TransportRepository( $mdb ),
|
||||
new \Domain\PaymentMethod\PaymentMethodRepository( $mdb )
|
||||
);
|
||||
},
|
||||
'Pages' => function() {
|
||||
global $mdb;
|
||||
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
<?php
|
||||
namespace admin\controls;
|
||||
class ShopTransport
|
||||
{
|
||||
public static function transport_save()
|
||||
{
|
||||
$response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania rodzaju transportu wystąpił błąd. Proszę spróbować ponownie.' ];
|
||||
$values = json_decode( \S::get( 'values' ), true );
|
||||
|
||||
if ( $id = \admin\factory\ShopTransport::transport_save(
|
||||
$values['id'], $values['name'], $values['name_visible'], $values['description'], $values['status'], $values['cost'], $values['payment_methods'], $values['max_wp'], $values['default'], $values['apilo_carrier_account_id'], $values['delivery_free']
|
||||
) )
|
||||
$response = [ 'status' => 'ok', 'msg' => 'Rodzaj transportu został zapisany.', 'id' => $id ];
|
||||
|
||||
echo json_encode( $response );
|
||||
exit;
|
||||
}
|
||||
|
||||
public static function transport_edit()
|
||||
{
|
||||
global $mdb;
|
||||
$paymentMethodRepository = new \Domain\PaymentMethod\PaymentMethodRepository( $mdb );
|
||||
|
||||
return \Tpl::view( 'shop-transport/transport-edit', [
|
||||
'transport_details' => \admin\factory\ShopTransport::transport_details( \S::get( 'id' ) ),
|
||||
'payments_list' => $paymentMethodRepository -> allForAdmin(),
|
||||
'apilo_carrier_account_list' => unserialize( \admin\factory\Integrations::apilo_settings( 'carrier-account-list' ) ),
|
||||
] );
|
||||
}
|
||||
|
||||
public static function view_list()
|
||||
{
|
||||
return \Tpl::view( 'shop-transport/view-list', [
|
||||
'apilo_carrier_account_list' => unserialize( \admin\factory\Integrations::apilo_settings( 'carrier-account-list' ) ),
|
||||
] );
|
||||
}
|
||||
}
|
||||
@@ -4,103 +4,7 @@ class ShopTransport
|
||||
{
|
||||
public static function lowest_transport_price( $wp ) {
|
||||
global $mdb;
|
||||
return $mdb -> get( 'pp_shop_transports', 'cost', [ 'AND' => [ 'status' => 1, 'id' => [ 2, 4, 6, 8, 9 ], 'max_wp[>=]' => $wp ], 'ORDER' => [ 'cost' => 'ASC' ] ] );
|
||||
}
|
||||
|
||||
public static function transport_save( $transport_id, $name, $name_visible, $description, $status, $cost, $payment_methods, $max_wp, $default, $apilo_carrier_account_id, $delivery_free )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
if ( !$transport_id )
|
||||
{
|
||||
if ( $default == 'on' )
|
||||
$mdb -> update( 'pp_shop_transports', [ 'default' => '0' ] );
|
||||
|
||||
$mdb -> insert( 'pp_shop_transports', [
|
||||
'name' => $name,
|
||||
'name_visible' => $name_visible,
|
||||
'description' => $description,
|
||||
'status' => $status == 'on' ? 1 : 0,
|
||||
'default' => $default == 'on' ? 1 : 0,
|
||||
'cost' => $cost,
|
||||
'max_wp' => $max_wp ? $max_wp : null,
|
||||
'apilo_carrier_account_id' => $apilo_carrier_account_id ? $apilo_carrier_account_id : null,
|
||||
'delivery_free' => $delivery_free == 'on' ? 1 : 0
|
||||
] );
|
||||
|
||||
$id = $mdb -> id();
|
||||
|
||||
if ( $id )
|
||||
{
|
||||
if ( is_array( $payment_methods ) ) foreach ( $payment_methods as $payment_method )
|
||||
{
|
||||
$mdb -> insert( 'pp_shop_transport_payment_methods', [
|
||||
'id_payment_method' => (int)$payment_method,
|
||||
'id_transport' => (int)$id
|
||||
] );
|
||||
}
|
||||
else if ( $payment_methods )
|
||||
{
|
||||
$mdb -> insert( 'pp_shop_transport_payment_methods', [
|
||||
'id_payment_method' => (int)$payment_methods,
|
||||
'id_transport' => (int)$id
|
||||
] );
|
||||
}
|
||||
|
||||
\S::delete_dir( '../temp/' );
|
||||
|
||||
return $id;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( $default == 'on' )
|
||||
$mdb -> update( 'pp_shop_transports', [ 'default' => '0' ] );
|
||||
|
||||
$mdb -> update( 'pp_shop_transports', [
|
||||
'name' => $name,
|
||||
'name_visible' => $name_visible,
|
||||
'description' => $description,
|
||||
'status' => $status == 'on' ? 1 : 0,
|
||||
'default' => $default == 'on' ? 1 : 0,
|
||||
'cost' => $cost,
|
||||
'max_wp' => $max_wp ? $max_wp : null,
|
||||
'apilo_carrier_account_id' => $apilo_carrier_account_id ? $apilo_carrier_account_id : null,
|
||||
'delivery_free' => $delivery_free == 'on' ? 1 : 0
|
||||
], [
|
||||
'id' => $transport_id
|
||||
] );
|
||||
|
||||
$mdb -> delete( 'pp_shop_transport_payment_methods', [ 'id_transport' => (int)$transport_id ] );
|
||||
|
||||
if ( is_array( $payment_methods ) ) foreach ( $payment_methods as $payment_method )
|
||||
{
|
||||
$mdb -> insert( 'pp_shop_transport_payment_methods', [
|
||||
'id_payment_method' => (int)$payment_method,
|
||||
'id_transport' => (int)$transport_id
|
||||
] );
|
||||
}
|
||||
else if ( $payment_methods )
|
||||
{
|
||||
$mdb -> insert( 'pp_shop_transport_payment_methods', [
|
||||
'id_payment_method' => (int)$payment_methods,
|
||||
'id_transport' => (int)$transport_id
|
||||
] );
|
||||
}
|
||||
|
||||
\S::delete_dir( '../temp/' );
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static function transport_details( $transport_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$transport = $mdb -> get( 'pp_shop_transports', '*', [ 'id' => $transport_id ] );
|
||||
$transport['payment_methods'] = $mdb -> select( 'pp_shop_transport_payment_methods', 'id_payment_method', [ 'id_transport' => $transport_id ] );
|
||||
|
||||
return $transport;
|
||||
$repo = new \Domain\Transport\TransportRepository($mdb);
|
||||
return $repo->lowestTransportPrice($wp);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
<?php
|
||||
namespace admin\view;
|
||||
class ShopTransport
|
||||
{
|
||||
}
|
||||
@@ -2,11 +2,11 @@
|
||||
namespace front\factory;
|
||||
class ShopTransport
|
||||
{
|
||||
// get_apilo_carrier_account_id
|
||||
static public function get_apilo_carrier_account_id( $transport_method_id )
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> get( 'pp_shop_transports', 'apilo_carrier_account_id', [ 'id' => $transport_method_id ] );
|
||||
$repo = new \Domain\Transport\TransportRepository($mdb);
|
||||
return $repo->getApiloCarrierAccountId($transport_method_id);
|
||||
}
|
||||
|
||||
public static function transport_methods( $basket, $coupon )
|
||||
@@ -20,14 +20,8 @@ class ShopTransport
|
||||
|
||||
if ( !$objectData )
|
||||
{
|
||||
$results = $mdb -> query( 'SELECT '
|
||||
. 'pst.id, name, name_visible, description, cost, max_wp, pst.default, delivery_free '
|
||||
. 'FROM '
|
||||
. 'pp_shop_transports AS pst '
|
||||
. 'WHERE '
|
||||
. 'status = 1 ORDER BY o ASC' ) -> fetchAll( \PDO::FETCH_ASSOC );
|
||||
if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
|
||||
$transports_tmp[] = $row;
|
||||
$repo = new \Domain\Transport\TransportRepository($mdb);
|
||||
$transports_tmp = $repo->allActive();
|
||||
|
||||
$cacheHandler -> set( $cacheKey, $transports_tmp );
|
||||
}
|
||||
@@ -65,11 +59,8 @@ class ShopTransport
|
||||
|
||||
if ( !$cost = \Cache::fetch( 'transport_cost_' . $transport_id ) )
|
||||
{
|
||||
$cost = $mdb -> get( 'pp_shop_transports', 'cost', [
|
||||
'AND' => [
|
||||
'id' => $transport_id,
|
||||
'status' => 1
|
||||
] ] );
|
||||
$repo = new \Domain\Transport\TransportRepository($mdb);
|
||||
$cost = $repo->getTransportCost($transport_id);
|
||||
|
||||
\Cache::store( 'transport_cost_' . $transport_id, $cost );
|
||||
}
|
||||
@@ -82,11 +73,8 @@ class ShopTransport
|
||||
|
||||
if ( !$transport = \Cache::fetch( 'transport' . $transport_id ) )
|
||||
{
|
||||
$transport = $mdb -> get( 'pp_shop_transports', '*', [
|
||||
'AND' => [
|
||||
'id' => $transport_id,
|
||||
'status' => 1
|
||||
] ] );
|
||||
$repo = new \Domain\Transport\TransportRepository($mdb);
|
||||
$transport = $repo->findActiveById($transport_id);
|
||||
|
||||
\Cache::store( 'transport' . $transport_id, $transport );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user