ver. 0.271 - ShopAttribute refactor + update package
This commit is contained in:
451
autoload/admin/Controllers/ShopAttributeController.php
Normal file
451
autoload/admin/Controllers/ShopAttributeController.php
Normal file
@@ -0,0 +1,451 @@
|
||||
<?php
|
||||
namespace admin\Controllers;
|
||||
|
||||
use Domain\Attribute\AttributeRepository;
|
||||
use Domain\Languages\LanguagesRepository;
|
||||
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 ShopAttributeController
|
||||
{
|
||||
private AttributeRepository $repository;
|
||||
private LanguagesRepository $languagesRepository;
|
||||
|
||||
public function __construct(
|
||||
AttributeRepository $repository,
|
||||
LanguagesRepository $languagesRepository
|
||||
) {
|
||||
$this->repository = $repository;
|
||||
$this->languagesRepository = $languagesRepository;
|
||||
}
|
||||
|
||||
public function list(): string
|
||||
{
|
||||
$sortableColumns = ['id', 'o', 'name', 'type', 'status', 'values_count'];
|
||||
$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,
|
||||
'o'
|
||||
);
|
||||
|
||||
$sortDir = $listRequest['sortDir'];
|
||||
if (trim((string)\S::get('sort')) === '') {
|
||||
$sortDir = 'ASC';
|
||||
}
|
||||
|
||||
$result = $this->repository->listForAdmin(
|
||||
$listRequest['filters'],
|
||||
$listRequest['sortColumn'],
|
||||
$sortDir,
|
||||
$listRequest['page'],
|
||||
$listRequest['perPage']
|
||||
);
|
||||
|
||||
$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);
|
||||
$type = (int)($item['type'] ?? 0);
|
||||
$order = (int)($item['o'] ?? 0);
|
||||
$valuesCount = (int)($item['values_count'] ?? 0);
|
||||
|
||||
$typeLabel = '-';
|
||||
if ($type === 0) {
|
||||
$typeLabel = 'tekst';
|
||||
} elseif ($type === 1) {
|
||||
$typeLabel = 'kolor';
|
||||
} elseif ($type === 2) {
|
||||
$typeLabel = 'wzor';
|
||||
}
|
||||
|
||||
$rows[] = [
|
||||
'lp' => $lp++ . '.',
|
||||
'o' => $order,
|
||||
'name' => '<a href="/admin/shop_attribute/edit/id=' . $id . '">' . htmlspecialchars($name, ENT_QUOTES, 'UTF-8') . '</a>',
|
||||
'type' => htmlspecialchars($typeLabel, ENT_QUOTES, 'UTF-8'),
|
||||
'status' => $status === 1 ? 'tak' : '<span style="color: #FF0000;">nie</span>',
|
||||
'values' => '<a href="/admin/shop_attribute/values/id=' . $id . '">edytuj wartosci</a>',
|
||||
'values_count' => $valuesCount,
|
||||
'_actions' => [
|
||||
[
|
||||
'label' => 'Edytuj',
|
||||
'url' => '/admin/shop_attribute/edit/id=' . $id,
|
||||
'class' => 'btn btn-xs btn-primary',
|
||||
],
|
||||
[
|
||||
'label' => 'Usun',
|
||||
'url' => '/admin/shop_attribute/delete/id=' . $id,
|
||||
'class' => 'btn btn-xs btn-danger',
|
||||
'confirm' => 'Na pewno chcesz usunac wybrana ceche?',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
$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' => 'o', 'sort_key' => 'o', 'label' => 'Kolejnosc', 'class' => 'text-center', 'sortable' => true],
|
||||
['key' => 'name', 'sort_key' => 'name', 'label' => 'Nazwa', 'sortable' => true, 'raw' => true],
|
||||
['key' => 'type', 'sort_key' => 'type', 'label' => 'Typ', 'class' => 'text-center', 'sortable' => true],
|
||||
['key' => 'status', 'sort_key' => 'status', 'label' => 'Aktywny', 'class' => 'text-center', 'sortable' => true, 'raw' => true],
|
||||
['key' => 'values_count', 'sort_key' => 'values_count', 'label' => 'Ilosc wartosci', 'class' => 'text-center', 'sortable' => true],
|
||||
['key' => 'values', 'label' => 'Wartosci', 'class' => 'text-center', 'sortable' => false, 'raw' => true],
|
||||
],
|
||||
$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_attribute/list/',
|
||||
'Brak danych w tabeli.',
|
||||
'/admin/shop_attribute/edit/',
|
||||
'Dodaj ceche'
|
||||
);
|
||||
|
||||
return \Tpl::view('shop-attribute/attributes-list', [
|
||||
'viewModel' => $viewModel,
|
||||
]);
|
||||
}
|
||||
|
||||
public function edit(): string
|
||||
{
|
||||
$attribute = $this->repository->findAttribute((int)\S::get('id'));
|
||||
$languages = $this->languagesRepository->languagesList();
|
||||
|
||||
return \Tpl::view('shop-attribute/attribute-edit', [
|
||||
'form' => $this->buildFormViewModel($attribute, $languages),
|
||||
]);
|
||||
}
|
||||
|
||||
public function save(): void
|
||||
{
|
||||
$response = [
|
||||
'status' => 'error',
|
||||
'msg' => 'Podczas zapisywania atrybutu wystapil blad. Prosze sprobowac ponownie.',
|
||||
];
|
||||
|
||||
$legacyValues = \S::get('values');
|
||||
if ($legacyValues) {
|
||||
$values = json_decode((string)$legacyValues, true);
|
||||
if (is_array($values)) {
|
||||
$id = $this->repository->saveAttribute($values);
|
||||
if (!empty($id)) {
|
||||
$response = [
|
||||
'status' => 'ok',
|
||||
'msg' => 'Atrybut zostal zapisany.',
|
||||
'id' => (int)$id,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
echo json_encode($response);
|
||||
exit;
|
||||
}
|
||||
|
||||
$payload = $_POST;
|
||||
if (empty($payload['id'])) {
|
||||
$routeId = (int)\S::get('id');
|
||||
if ($routeId > 0) {
|
||||
$payload['id'] = $routeId;
|
||||
}
|
||||
}
|
||||
|
||||
$id = $this->repository->saveAttribute($payload);
|
||||
if (!empty($id)) {
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'id' => (int)$id,
|
||||
'message' => 'Atrybut zostal zapisany.',
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'errors' => ['general' => 'Podczas zapisywania atrybutu wystapil blad.'],
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
public function delete(): void
|
||||
{
|
||||
if ($this->repository->deleteAttribute((int)\S::get('id'))) {
|
||||
\S::alert('Atrybut zostal usuniety.');
|
||||
}
|
||||
|
||||
header('Location: /admin/shop_attribute/list/');
|
||||
exit;
|
||||
}
|
||||
|
||||
public function values(): string
|
||||
{
|
||||
$attributeId = (int)\S::get('id');
|
||||
if ($attributeId <= 0) {
|
||||
\S::alert('Nieprawidlowy identyfikator cechy.');
|
||||
header('Location: /admin/shop_attribute/list/');
|
||||
exit;
|
||||
}
|
||||
|
||||
$attribute = $this->repository->findAttribute($attributeId);
|
||||
if ((int)($attribute['id'] ?? 0) <= 0) {
|
||||
\S::alert('Wybrana cecha nie zostala znaleziona.');
|
||||
header('Location: /admin/shop_attribute/list/');
|
||||
exit;
|
||||
}
|
||||
|
||||
$languages = $this->languagesRepository->languagesList();
|
||||
|
||||
return \Tpl::view('shop-attribute/values-edit', [
|
||||
'attribute' => $attribute,
|
||||
'values' => $this->repository->findValues($attributeId),
|
||||
'languages' => $languages,
|
||||
'defaultLanguageId' => $this->languagesRepository->defaultLanguageId(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function values_save(): void
|
||||
{
|
||||
$response = [
|
||||
'status' => 'error',
|
||||
'msg' => 'Podczas zapisywania wartosci atrybutu wystapil blad. Prosze sprobowac ponownie.',
|
||||
];
|
||||
|
||||
$attributeId = (int)\S::get('attribute_id');
|
||||
if ($attributeId <= 0) {
|
||||
$attributeId = (int)\S::get('id');
|
||||
}
|
||||
|
||||
$payloadRaw = \S::get('payload');
|
||||
$payload = json_decode((string)$payloadRaw, true);
|
||||
if (is_array($payload) && is_array($payload['rows'] ?? null) && $attributeId > 0) {
|
||||
$validationErrors = $this->validateValuesRows(
|
||||
$payload['rows'],
|
||||
$this->languagesRepository->defaultLanguageId()
|
||||
);
|
||||
|
||||
if (!empty($validationErrors)) {
|
||||
echo json_encode([
|
||||
'status' => 'error',
|
||||
'msg' => $validationErrors[0],
|
||||
'errors' => $validationErrors,
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
$saved = $this->repository->saveValues($attributeId, ['rows' => $payload['rows']]);
|
||||
if ($saved) {
|
||||
$response = [
|
||||
'status' => 'ok',
|
||||
'msg' => 'Wartosci atrybutu zostaly zapisane.',
|
||||
'id' => (int)$attributeId,
|
||||
];
|
||||
}
|
||||
|
||||
echo json_encode($response);
|
||||
exit;
|
||||
}
|
||||
|
||||
$valuesRaw = \S::get('values');
|
||||
$values = json_decode((string)$valuesRaw, true);
|
||||
if (is_array($values) && $attributeId > 0) {
|
||||
$savedId = $this->repository->saveLegacyValues(
|
||||
$attributeId,
|
||||
is_array($values['name'] ?? null) ? $values['name'] : [],
|
||||
is_array($values['value'] ?? null) ? $values['value'] : [],
|
||||
is_array($values['ids'] ?? null) ? $values['ids'] : [],
|
||||
$values['default_value'] ?? '',
|
||||
is_array($values['impact_on_the_price'] ?? null) ? $values['impact_on_the_price'] : []
|
||||
);
|
||||
|
||||
if (!empty($savedId)) {
|
||||
$response = [
|
||||
'status' => 'ok',
|
||||
'msg' => 'Wartosci atrybutu zostaly zapisane.',
|
||||
'id' => (int)$savedId,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
echo json_encode($response);
|
||||
exit;
|
||||
}
|
||||
|
||||
public function value_row_tpl(): void
|
||||
{
|
||||
$rowKey = trim((string)\S::get('row_key'));
|
||||
if ($rowKey === '') {
|
||||
$rowKey = 'new-' . time();
|
||||
}
|
||||
|
||||
$html = \Tpl::view('shop-attribute/_partials/value-row', [
|
||||
'rowKey' => $rowKey,
|
||||
'value' => ['id' => 0, 'is_default' => 0, 'impact_on_the_price' => null, 'languages' => []],
|
||||
'languages' => $this->languagesRepository->languagesList(),
|
||||
'defaultLanguageId' => $this->languagesRepository->defaultLanguageId(),
|
||||
]);
|
||||
|
||||
echo $html;
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array<string, mixed>> $rows
|
||||
* @return array<int, string>
|
||||
*/
|
||||
private function validateValuesRows(array $rows, string $defaultLanguageId): array
|
||||
{
|
||||
$errors = [];
|
||||
if (empty($rows)) {
|
||||
return ['Dodaj co najmniej jedna wartosc cechy.'];
|
||||
}
|
||||
|
||||
$defaultCount = 0;
|
||||
foreach ($rows as $index => $row) {
|
||||
$rowNumber = $index + 1;
|
||||
if (!is_array($row)) {
|
||||
$errors[] = 'Nieprawidlowe dane wiersza nr ' . $rowNumber . '.';
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!empty($row['is_default'])) {
|
||||
++$defaultCount;
|
||||
}
|
||||
|
||||
$translations = is_array($row['translations'] ?? null) ? $row['translations'] : [];
|
||||
$defaultLangData = is_array($translations[$defaultLanguageId] ?? null)
|
||||
? $translations[$defaultLanguageId]
|
||||
: [];
|
||||
$defaultName = trim((string)($defaultLangData['name'] ?? ''));
|
||||
if ($defaultName === '') {
|
||||
$errors[] = 'Wiersz nr ' . $rowNumber . ': nazwa w jezyku domyslnym jest wymagana.';
|
||||
}
|
||||
|
||||
$impact = trim((string)($row['impact_on_the_price'] ?? ''));
|
||||
if ($impact !== '' && !preg_match('/^-?[0-9]+([.,][0-9]{1,4})?$/', $impact)) {
|
||||
$errors[] = 'Wiersz nr ' . $rowNumber . ': nieprawidlowy format "wplyw na cene".';
|
||||
}
|
||||
}
|
||||
|
||||
if ($defaultCount !== 1) {
|
||||
$errors[] = 'Wybierz dokladnie jedna wartosc domyslna.';
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
private function buildFormViewModel(array $attribute, array $languages): FormEditViewModel
|
||||
{
|
||||
$id = (int)($attribute['id'] ?? 0);
|
||||
$isNew = $id <= 0;
|
||||
|
||||
$data = [
|
||||
'id' => $id,
|
||||
'status' => (int)($attribute['status'] ?? 1),
|
||||
'type' => (int)($attribute['type'] ?? 0),
|
||||
'o' => (int)($attribute['o'] ?? 0),
|
||||
'languages' => [],
|
||||
];
|
||||
|
||||
if (is_array($attribute['languages'] ?? null)) {
|
||||
foreach ($attribute['languages'] as $langId => $translation) {
|
||||
$data['languages'][(string)$langId] = [
|
||||
'name' => (string)($translation['name'] ?? ''),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$fields = [
|
||||
FormField::hidden('id', $id),
|
||||
FormField::langSection('attribute_content', 'content', [
|
||||
FormField::text('name', [
|
||||
'label' => 'Tytul',
|
||||
]),
|
||||
]),
|
||||
FormField::switch('status', [
|
||||
'label' => 'Aktywny',
|
||||
'tab' => 'settings',
|
||||
'value' => true,
|
||||
]),
|
||||
FormField::select('type', [
|
||||
'label' => 'Typ',
|
||||
'tab' => 'settings',
|
||||
'options' => [
|
||||
0 => 'tekst',
|
||||
1 => 'kolor',
|
||||
2 => 'wzor',
|
||||
],
|
||||
]),
|
||||
FormField::number('o', [
|
||||
'label' => 'Kolejnosc',
|
||||
'tab' => 'settings',
|
||||
]),
|
||||
];
|
||||
|
||||
$tabs = [
|
||||
new FormTab('content', 'Tresc', 'fa-file'),
|
||||
new FormTab('settings', 'Ustawienia', 'fa-wrench'),
|
||||
];
|
||||
|
||||
$actionUrl = '/admin/shop_attribute/save/' . ($isNew ? '' : ('id=' . $id));
|
||||
$actions = [
|
||||
FormAction::save($actionUrl, '/admin/shop_attribute/list/'),
|
||||
FormAction::cancel('/admin/shop_attribute/list/'),
|
||||
];
|
||||
|
||||
return new FormEditViewModel(
|
||||
'shop-attribute-edit',
|
||||
$isNew ? 'Nowa cecha' : 'Edycja cechy',
|
||||
$data,
|
||||
$fields,
|
||||
$tabs,
|
||||
$actions,
|
||||
'POST',
|
||||
$actionUrl,
|
||||
'/admin/shop_attribute/list/',
|
||||
true,
|
||||
['id' => $id],
|
||||
$languages
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -316,6 +316,14 @@ class Site
|
||||
new \Domain\Coupon\CouponRepository( $mdb )
|
||||
);
|
||||
},
|
||||
'ShopAttribute' => function() {
|
||||
global $mdb;
|
||||
|
||||
return new \admin\Controllers\ShopAttributeController(
|
||||
new \Domain\Attribute\AttributeRepository( $mdb ),
|
||||
new \Domain\Languages\LanguagesRepository( $mdb )
|
||||
);
|
||||
},
|
||||
'ShopPaymentMethod' => function() {
|
||||
global $mdb;
|
||||
|
||||
@@ -406,6 +414,12 @@ class Site
|
||||
{
|
||||
return $controller->$action();
|
||||
}
|
||||
|
||||
if ( $moduleName === 'ShopAttribute' )
|
||||
{
|
||||
\S::alert( 'Nieprawidłowy adres url.' );
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
<?php
|
||||
namespace admin\controls;
|
||||
class ShopAttribute
|
||||
{
|
||||
static public function attribute_value_tpl()
|
||||
{
|
||||
$html = \Tpl::view( 'shop-attribute/_partials/value', [
|
||||
'i' => \S::get( 'i' ),
|
||||
'value' => \S::get( 'value' ),
|
||||
'languages' => \S::get( 'languages' ),
|
||||
'attribute' => \S::get( 'attribute' ),
|
||||
] );
|
||||
echo $html;
|
||||
exit;
|
||||
}
|
||||
|
||||
static public function values_save()
|
||||
{
|
||||
$response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania wartości atrybutu wystąpił błąd. Proszę spróbować ponownie.' ];
|
||||
$values = json_decode( \S::get( 'values' ), true );
|
||||
|
||||
if ( $id = \admin\factory\ShopAttribute::values_save( (int) \S::get( 'attribute_id' ), $values['name'], $values['value'], $values['ids'], $values['default_value'], $values['impact_on_the_price'] ) )
|
||||
$response = [ 'status' => 'ok', 'msg' => 'Wartości atrybutu zostały zapisane.', 'id' => $id ];
|
||||
|
||||
echo json_encode( $response );
|
||||
exit;
|
||||
}
|
||||
|
||||
public static function values_edit()
|
||||
{
|
||||
return \Tpl::view( 'shop-attribute/values-edit', [
|
||||
'attribute' => \admin\factory\ShopAttribute::attribute_details( (int) \S::get( 'attribute-id' ) ),
|
||||
'values' => \admin\factory\ShopAttribute::get_attribute_values( (int) \S::get( 'attribute-id' ) ),
|
||||
'languages' => ( new \Domain\Languages\LanguagesRepository( $GLOBALS['mdb'] ) )->languagesList()
|
||||
] );
|
||||
}
|
||||
|
||||
public static function delete_attribute()
|
||||
{
|
||||
if ( \admin\factory\ShopAttribute::delete_attribute( (int) \S::get( 'id' ) ) )
|
||||
\S::alert( 'Atrybut został usunięty.' );
|
||||
|
||||
header( 'Location: /admin/shop_attribute/view_list/' );
|
||||
exit;
|
||||
}
|
||||
|
||||
public static function attribute_save()
|
||||
{
|
||||
$response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania atrybutu wystąpił błąd. Proszę spróbować ponownie.' ];
|
||||
$values = json_decode( \S::get( 'values' ), true );
|
||||
|
||||
if ( $id = \admin\factory\ShopAttribute::attribute_save( (int) $values['id'], $values['name'], $values['status'] == 'on' ? 1 : 0, (int) $values['type'], (int) $values['o'] ) )
|
||||
$response = [ 'status' => 'ok', 'msg' => 'Atrybut został zapisany.', 'id' => $id ];
|
||||
|
||||
echo json_encode( $response );
|
||||
exit;
|
||||
}
|
||||
|
||||
static public function attribute_edit()
|
||||
{
|
||||
return \Tpl::view( 'shop-attribute/attribute-edit', [
|
||||
'attribute' => \admin\factory\ShopAttribute::attribute_details( (int) \S::get( 'id' ) ),
|
||||
'languages' => ( new \Domain\Languages\LanguagesRepository( $GLOBALS['mdb'] ) )->languagesList()
|
||||
] );
|
||||
}
|
||||
|
||||
public static function view_list()
|
||||
{
|
||||
return \Tpl::view( 'shop-attribute/attributes-list' );
|
||||
}
|
||||
}
|
||||
@@ -373,9 +373,11 @@ class ShopProduct
|
||||
//wyświetlenie kombinacji produktu
|
||||
static public function product_combination()
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
return \Tpl::view( 'shop-product/product-combination', [
|
||||
'product' => \admin\factory\ShopProduct::product_details( (int) \S::get( 'product_id' ) ),
|
||||
'attributes' => \admin\factory\ShopAttribute::get_attributes_list(),
|
||||
'attributes' => ( new \Domain\Attribute\AttributeRepository( $mdb ) ) -> getAttributesListForCombinations(),
|
||||
'default_language' => \front\factory\Languages::default_language(),
|
||||
'product_permutations' => \admin\factory\ShopProduct::get_product_permutations( (int) \S::get( 'product_id' ) )
|
||||
] );
|
||||
|
||||
@@ -1,263 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace admin\factory;
|
||||
|
||||
class ShopAttribute
|
||||
{
|
||||
static public function get_attributes_list()
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$rows = $mdb -> select( 'pp_shop_attributes', '*', [ 'ORDER' => [ 'o' => 'ASC' ] ] );
|
||||
if ( \S::is_array_fix( $rows ) ) foreach ( $rows as $row )
|
||||
{
|
||||
$attribute = \admin\factory\ShopAttribute::attribute_details( $row['id'] );
|
||||
$attribute['values'] = \admin\factory\ShopAttribute::get_attribute_values( $row['id'] );
|
||||
$attributes[] = $attribute;
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
// pobierz nazwę wartości atrybutu
|
||||
static public function get_attribute_value_by_id( int $value_id )
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> get( 'pp_shop_attributes_values_langs', 'name', [ 'AND' => [ 'value_id' => $value_id, 'lang_id' => \front\factory\Languages::default_language() ] ] );
|
||||
}
|
||||
|
||||
//pobierz nazwę atrybutu
|
||||
static public function get_attribute_name_by_id( int $attribute_id )
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> get( 'pp_shop_attributes_langs', 'name', [ 'AND' => [ 'attribute_id' => $attribute_id, 'lang_id' => \front\factory\Languages::default_language() ] ] );
|
||||
}
|
||||
|
||||
static public function values_save( int $attribute_id, $names, $values, $ids, $default_value = '', $impact_on_the_price )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$main_language = \front\factory\Languages::default_language();
|
||||
|
||||
if ( \S::is_array_fix( $ids[$main_language] ) )
|
||||
$ids_delete = implode( ',', $ids[$main_language] );
|
||||
else
|
||||
{
|
||||
if ( $ids[$main_language] )
|
||||
$ids_delete = $ids[$main_language];
|
||||
}
|
||||
|
||||
if ( $ids_delete )
|
||||
$mdb -> query( 'DELETE FROM pp_shop_attributes_values WHERE id NOT IN (' . $ids_delete . ') AND attribute_id = ' . $attribute_id );
|
||||
|
||||
for ( $i = 0; $i < count( $names[$main_language] ); ++$i )
|
||||
{
|
||||
if ( $ids[$main_language][$i] )
|
||||
{
|
||||
$mdb -> update( 'pp_shop_attributes_values', [
|
||||
'impact_on_the_price' => $impact_on_the_price[$i] ? \S::normalize_decimal( $impact_on_the_price[$i] ) : null,
|
||||
], [
|
||||
'id' => $ids[$main_language][$i],
|
||||
] );
|
||||
|
||||
\admin\factory\ShopProduct::update_product_price_by_attribute_value_impact( $ids[$main_language][$i], $impact_on_the_price[$i] );
|
||||
|
||||
$langs = ( new \Domain\Languages\LanguagesRepository( $mdb ) )->languagesList();
|
||||
|
||||
foreach ( $langs as $lang )
|
||||
{
|
||||
if ( $names[$lang['id']][$i] and $mdb -> count( 'pp_shop_attributes_values_langs', [ 'AND' => [ 'value_id' => $ids[$main_language][$i], 'lang_id' => $lang['id'] ] ] ) )
|
||||
{
|
||||
$mdb -> update( 'pp_shop_attributes_values_langs', [
|
||||
'name' => $names[$lang['id']][$i],
|
||||
'value' => $values[$lang['id']][$i] ? $values[$lang['id']][$i] : null,
|
||||
], [
|
||||
'AND' => [
|
||||
'value_id' => $ids[$main_language][$i],
|
||||
'lang_id' => $lang['id'],
|
||||
],
|
||||
] );
|
||||
}
|
||||
elseif ( $names[$lang['id']][$i] and !$mdb -> count( 'pp_shop_attributes_values_langs', [ 'AND' => [ 'value_id' => $ids[$main_language][$i], 'lang_id' => $lang['id'] ] ] ) )
|
||||
{
|
||||
$mdb -> insert('pp_shop_attributes_values_langs', [
|
||||
'value_id' => $ids[$main_language][$i],
|
||||
'lang_id' => $lang['id'],
|
||||
'name' => $names[$lang['id']][$i],
|
||||
'value' => $values[$lang['id']][$i] ? $values[$lang['id']][$i] : null,
|
||||
] );
|
||||
}
|
||||
}
|
||||
|
||||
if ( $default_value == $i )
|
||||
$default_value_id = $ids[$main_language][$i];
|
||||
}
|
||||
else
|
||||
{
|
||||
$mdb -> insert( 'pp_shop_attributes_values', [ 'attribute_id' => $attribute_id ] );
|
||||
$value_id = $mdb -> id();
|
||||
|
||||
if ( $value_id )
|
||||
{
|
||||
$mdb -> update( 'pp_shop_attributes_values', [
|
||||
'impact_on_the_price' => $impact_on_the_price[$i] ? \S::normalize_decimal( $impact_on_the_price[$i] ) : null,
|
||||
], [
|
||||
'id' => $value_id,
|
||||
] );
|
||||
|
||||
if ( $impact_on_the_price[$i] )
|
||||
\admin\factory\ShopProduct::update_product_price_by_attribute_value_impact( $value_id, \S::normalize_decimal( $impact_on_the_price[$i] ) );
|
||||
|
||||
$langs = ( new \Domain\Languages\LanguagesRepository( $mdb ) )->languagesList();
|
||||
if ( \S::is_array_fix( $langs ) ) foreach ( $langs as $lang )
|
||||
{
|
||||
if ( $names[$lang['id']][$i] )
|
||||
{
|
||||
$mdb -> insert('pp_shop_attributes_values_langs', [
|
||||
'value_id' => $value_id,
|
||||
'lang_id' => $lang['id'],
|
||||
'name' => $names[$lang['id']][$i],
|
||||
'value' => $values[$lang['id']][$i] ? $values[$lang['id']][$i] : null,
|
||||
] );
|
||||
}
|
||||
}
|
||||
|
||||
if ( $default_value == $i )
|
||||
$default_value_id = $value_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( $default_value_id )
|
||||
{
|
||||
$mdb -> update( 'pp_shop_attributes_values', [ 'is_default' => 0 ], [ 'attribute_id' => $attribute_id ] );
|
||||
$mdb -> update( 'pp_shop_attributes_values', [ 'is_default' => 1 ], [ 'id' => $default_value_id ] );
|
||||
}
|
||||
|
||||
\S::delete_cache();
|
||||
|
||||
return $attribute_id;
|
||||
}
|
||||
|
||||
static public function get_attribute_values( int $attribute_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$results = $mdb -> select( 'pp_shop_attributes_values', [ 'id', 'is_default', 'impact_on_the_price' ], [ 'attribute_id' => $attribute_id ] );
|
||||
if ( \S::is_array_fix( $results ) ) foreach ( $results as $row )
|
||||
{
|
||||
$results2 = $mdb -> select( 'pp_shop_attributes_values_langs', [ 'lang_id', 'name', 'value' ], [ 'value_id' => $row['id'] ] );
|
||||
if ( \S::is_array_fix( $results2 ) ) foreach ( $results2 as $row2 )
|
||||
$row['languages'][$row2['lang_id']] = $row2;
|
||||
|
||||
$values[] = $row;
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
static public function delete_attribute( int $attribute_id )
|
||||
{
|
||||
global $mdb, $user;
|
||||
|
||||
if ( $mdb -> delete( 'pp_shop_attributes', [ 'id' => $attribute_id ] ) )
|
||||
{
|
||||
\Log::save_log( 'Atrybut został usunięty | ID: ' . $attribute_id, $user['id'] );
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static public function attribute_save( int $attribute_id, $name, int $status, int $type, int $o )
|
||||
{
|
||||
global $mdb, $user;
|
||||
|
||||
if ( !$attribute_id )
|
||||
{
|
||||
$mdb -> insert( 'pp_shop_attributes', [
|
||||
'status' => $status,
|
||||
'type' => $type,
|
||||
'o' => $o
|
||||
] );
|
||||
|
||||
$id = $mdb -> id();
|
||||
|
||||
if ( !$id )
|
||||
return false;
|
||||
|
||||
\Log::save_log( 'Dodano nowy atrybut | ID: ' . $id, $user['id'] );
|
||||
|
||||
foreach ( $name as $key => $val )
|
||||
{
|
||||
$mdb -> insert( 'pp_shop_attributes_langs', [
|
||||
'attribute_id' => (int)$id,
|
||||
'lang_id' => $key,
|
||||
'name' => $name[$key],
|
||||
] );
|
||||
}
|
||||
|
||||
\S::delete_dir( '../temp/' );
|
||||
|
||||
return $id;
|
||||
}
|
||||
else
|
||||
{
|
||||
$mdb -> update( 'pp_shop_attributes', [
|
||||
'status' => $status,
|
||||
'type' => $type,
|
||||
'o' => $o
|
||||
], [
|
||||
'id' => $attribute_id,
|
||||
] );
|
||||
|
||||
\Log::save_log( 'Zaktualizowano atrybut | ID: ' . $attribute_id, $user['id'] );
|
||||
|
||||
foreach ( $name as $key => $val )
|
||||
{
|
||||
if ( $translation_id = $mdb -> get( 'pp_shop_attributes_langs', 'id', [ 'AND' => [ 'attribute_id' => $attribute_id, 'lang_id' => $key ] ] ) )
|
||||
$mdb -> update( 'pp_shop_attributes_langs', [
|
||||
'lang_id' => $key,
|
||||
'name' => $name[$key],
|
||||
], [
|
||||
'id' => $translation_id
|
||||
] );
|
||||
else
|
||||
$mdb -> insert( 'pp_shop_attributes_langs', [
|
||||
'attribute_id' => (int)$attribute_id,
|
||||
'lang_id' => $key,
|
||||
'name' => $name[$key],
|
||||
] );
|
||||
}
|
||||
|
||||
\S::delete_dir( '../temp/' );
|
||||
|
||||
return $attribute_id;
|
||||
}
|
||||
}
|
||||
|
||||
static public function value_details( int $value_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$value = $mdb -> get( 'pp_shop_attributes_values', '*', [ 'id' => (int) $value_id ] );
|
||||
|
||||
$results = $mdb -> select( 'pp_shop_attributes_values_langs', [ 'lang_id', 'name', 'value' ], [ 'value_id' => (int) $value_id ] );
|
||||
if ( \S::is_array_fix( $results ) ) foreach ( $results as $row)
|
||||
$value['languages'][$row['lang_id']] = $row;
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
static public function attribute_details( int $attribute_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$attribute = $mdb -> get( 'pp_shop_attributes', '*', [ 'id' => (int) $attribute_id ] );
|
||||
|
||||
$results = $mdb -> select( 'pp_shop_attributes_langs', [ 'lang_id', 'name' ], [ 'attribute_id' => (int) $attribute_id ] );
|
||||
if ( \S::is_array_fix( $results ) ) foreach ( $results as $row)
|
||||
$attribute['languages'][$row['lang_id']] = $row;
|
||||
|
||||
return $attribute;
|
||||
}
|
||||
}
|
||||
@@ -162,6 +162,7 @@ class ShopProduct
|
||||
global $mdb;
|
||||
|
||||
$vat = $mdb -> get( 'pp_shop_products', 'vat', [ 'id' => $product_id ] );
|
||||
$attributeRepository = new \Domain\Attribute\AttributeRepository( $mdb );
|
||||
|
||||
$permutations = \shop\Product::array_cartesian( $attributes );
|
||||
if ( \S::is_array_fix( $permutations ) ) foreach ( $permutations as $permutation )
|
||||
@@ -179,7 +180,7 @@ class ShopProduct
|
||||
$permutation_hash .= $key . '-' . $val;
|
||||
|
||||
// sprawdzenie czy atrybut ma wpływ na cenę
|
||||
$value_details = \admin\factory\ShopAttribute::value_details( $val );
|
||||
$value_details = $attributeRepository -> valueDetails( (int)$val );
|
||||
$impact_on_the_price = $value_details[ 'impact_on_the_price' ];
|
||||
|
||||
if ( $impact_on_the_price > 0 )
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
<?php
|
||||
namespace admin\view;
|
||||
class ShopAttribute
|
||||
{
|
||||
public static function values_edit( $attribute, $values, $languages )
|
||||
{
|
||||
$tpl = new \Tpl;
|
||||
$tpl -> attribute = $attribute;
|
||||
$tpl -> values = $values;
|
||||
$tpl -> languages = $languages;
|
||||
return $tpl -> render( 'shop-attribute/values-edit' );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user