ver. 0.271 - ShopAttribute refactor + update package
This commit is contained in:
853
autoload/Domain/Attribute/AttributeRepository.php
Normal file
853
autoload/Domain/Attribute/AttributeRepository.php
Normal file
@@ -0,0 +1,853 @@
|
||||
<?php
|
||||
namespace Domain\Attribute;
|
||||
|
||||
class AttributeRepository
|
||||
{
|
||||
private const MAX_PER_PAGE = 100;
|
||||
|
||||
private $db;
|
||||
private ?string $defaultLangId = null;
|
||||
|
||||
public function __construct($db)
|
||||
{
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{items: array<int, array<string, mixed>>, total: int}
|
||||
*/
|
||||
public function listForAdmin(
|
||||
array $filters,
|
||||
string $sortColumn = 'o',
|
||||
string $sortDir = 'ASC',
|
||||
int $page = 1,
|
||||
int $perPage = 15
|
||||
): array {
|
||||
$allowedSortColumns = [
|
||||
'id' => 'sa.id',
|
||||
'o' => 'sa.o',
|
||||
'name' => 'name_for_sort',
|
||||
'type' => 'sa.type',
|
||||
'status' => 'sa.status',
|
||||
'values_count' => 'values_count',
|
||||
];
|
||||
|
||||
$sortSql = $allowedSortColumns[$sortColumn] ?? 'sa.o';
|
||||
$sortDir = strtoupper(trim($sortDir)) === 'DESC' ? 'DESC' : 'ASC';
|
||||
$page = max(1, $page);
|
||||
$perPage = min(self::MAX_PER_PAGE, max(1, $perPage));
|
||||
$offset = ($page - 1) * $perPage;
|
||||
|
||||
$whereData = $this->buildAdminWhere($filters);
|
||||
$whereSql = $whereData['sql'];
|
||||
$params = $whereData['params'];
|
||||
$params[':default_lang_id'] = $this->defaultLanguageId();
|
||||
|
||||
$sqlCount = "
|
||||
SELECT COUNT(0)
|
||||
FROM pp_shop_attributes AS sa
|
||||
WHERE {$whereSql}
|
||||
";
|
||||
$stmtCount = $this->db->query($sqlCount, $params);
|
||||
$countRows = $stmtCount ? $stmtCount->fetchAll() : [];
|
||||
$total = isset($countRows[0][0]) ? (int)$countRows[0][0] : 0;
|
||||
|
||||
$sql = "
|
||||
SELECT
|
||||
sa.id,
|
||||
sa.status,
|
||||
sa.type,
|
||||
sa.o,
|
||||
(
|
||||
SELECT sal.name
|
||||
FROM pp_shop_attributes_langs AS sal
|
||||
WHERE sal.attribute_id = sa.id
|
||||
AND sal.lang_id = :default_lang_id
|
||||
LIMIT 1
|
||||
) AS name_default,
|
||||
(
|
||||
SELECT sal2.name
|
||||
FROM pp_shop_attributes_langs AS sal2
|
||||
INNER JOIN pp_langs AS pl2 ON pl2.id = sal2.lang_id
|
||||
WHERE sal2.attribute_id = sa.id
|
||||
AND sal2.name <> ''
|
||||
ORDER BY pl2.o ASC
|
||||
LIMIT 1
|
||||
) AS name_any,
|
||||
(
|
||||
SELECT COUNT(0)
|
||||
FROM pp_shop_attributes_values AS sav
|
||||
WHERE sav.attribute_id = sa.id
|
||||
) AS values_count,
|
||||
COALESCE(
|
||||
(
|
||||
SELECT sal3.name
|
||||
FROM pp_shop_attributes_langs AS sal3
|
||||
WHERE sal3.attribute_id = sa.id
|
||||
AND sal3.lang_id = :default_lang_id
|
||||
LIMIT 1
|
||||
),
|
||||
(
|
||||
SELECT sal4.name
|
||||
FROM pp_shop_attributes_langs AS sal4
|
||||
INNER JOIN pp_langs AS pl4 ON pl4.id = sal4.lang_id
|
||||
WHERE sal4.attribute_id = sa.id
|
||||
AND sal4.name <> ''
|
||||
ORDER BY pl4.o ASC
|
||||
LIMIT 1
|
||||
),
|
||||
''
|
||||
) AS name_for_sort
|
||||
FROM pp_shop_attributes AS sa
|
||||
WHERE {$whereSql}
|
||||
ORDER BY {$sortSql} {$sortDir}, sa.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) {
|
||||
$nameDefault = trim((string)($item['name_default'] ?? ''));
|
||||
$nameAny = trim((string)($item['name_any'] ?? ''));
|
||||
|
||||
$item['id'] = (int)($item['id'] ?? 0);
|
||||
$item['status'] = $this->toSwitchValue($item['status'] ?? 0);
|
||||
$item['type'] = (int)($item['type'] ?? 0);
|
||||
$item['o'] = (int)($item['o'] ?? 0);
|
||||
$item['name'] = $nameDefault !== '' ? $nameDefault : $nameAny;
|
||||
$item['values_count'] = (int)($item['values_count'] ?? 0);
|
||||
|
||||
unset($item['name_default'], $item['name_any'], $item['name_for_sort']);
|
||||
}
|
||||
unset($item);
|
||||
|
||||
return [
|
||||
'items' => $items,
|
||||
'total' => $total,
|
||||
];
|
||||
}
|
||||
|
||||
public function findAttribute(int $attributeId): array
|
||||
{
|
||||
if ($attributeId <= 0) {
|
||||
return $this->defaultAttribute();
|
||||
}
|
||||
|
||||
$attribute = $this->db->get('pp_shop_attributes', '*', ['id' => $attributeId]);
|
||||
if (!is_array($attribute)) {
|
||||
return $this->defaultAttribute();
|
||||
}
|
||||
|
||||
$attribute['id'] = (int)($attribute['id'] ?? 0);
|
||||
$attribute['status'] = $this->toSwitchValue($attribute['status'] ?? 0);
|
||||
$attribute['type'] = (int)($attribute['type'] ?? 0);
|
||||
$attribute['o'] = (int)($attribute['o'] ?? 0);
|
||||
$attribute['languages'] = [];
|
||||
|
||||
$translations = $this->db->select(
|
||||
'pp_shop_attributes_langs',
|
||||
['lang_id', 'name'],
|
||||
['attribute_id' => $attribute['id']]
|
||||
);
|
||||
if (is_array($translations)) {
|
||||
foreach ($translations as $translation) {
|
||||
$langId = (string)($translation['lang_id'] ?? '');
|
||||
if ($langId !== '') {
|
||||
$attribute['languages'][$langId] = $translation;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $attribute;
|
||||
}
|
||||
|
||||
public function saveAttribute(array $data): ?int
|
||||
{
|
||||
$attributeId = (int)($data['id'] ?? 0);
|
||||
$row = [
|
||||
'status' => $this->toSwitchValue($data['status'] ?? 0),
|
||||
'type' => $this->toTypeValue($data['type'] ?? 0),
|
||||
'o' => max(0, (int)($data['o'] ?? 0)),
|
||||
];
|
||||
|
||||
if ($attributeId <= 0) {
|
||||
$this->db->insert('pp_shop_attributes', $row);
|
||||
$attributeId = (int)$this->db->id();
|
||||
if ($attributeId <= 0) {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
$this->db->update('pp_shop_attributes', $row, ['id' => $attributeId]);
|
||||
}
|
||||
|
||||
$names = [];
|
||||
if (isset($data['name']) && is_array($data['name'])) {
|
||||
$names = $data['name'];
|
||||
}
|
||||
$this->saveAttributeTranslations($attributeId, $names);
|
||||
|
||||
$this->clearTempAndCache();
|
||||
return $attributeId;
|
||||
}
|
||||
|
||||
public function deleteAttribute(int $attributeId): bool
|
||||
{
|
||||
if ($attributeId <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$deleted = (bool)$this->db->delete('pp_shop_attributes', ['id' => $attributeId]);
|
||||
if ($deleted) {
|
||||
$this->clearTempAndCache();
|
||||
}
|
||||
|
||||
return $deleted;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
public function findValues(int $attributeId): array
|
||||
{
|
||||
if ($attributeId <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$rows = $this->db->select(
|
||||
'pp_shop_attributes_values',
|
||||
['id', 'is_default', 'impact_on_the_price'],
|
||||
[
|
||||
'attribute_id' => $attributeId,
|
||||
'ORDER' => ['id' => 'ASC'],
|
||||
]
|
||||
);
|
||||
if (!is_array($rows)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$values = [];
|
||||
foreach ($rows as $row) {
|
||||
$valueId = (int)($row['id'] ?? 0);
|
||||
if ($valueId <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$value = [
|
||||
'id' => $valueId,
|
||||
'is_default' => $this->toSwitchValue($row['is_default'] ?? 0),
|
||||
'impact_on_the_price' => $this->toNullableNumeric($row['impact_on_the_price'] ?? null),
|
||||
'languages' => [],
|
||||
];
|
||||
|
||||
$translations = $this->db->select(
|
||||
'pp_shop_attributes_values_langs',
|
||||
['lang_id', 'name', 'value'],
|
||||
['value_id' => $valueId]
|
||||
);
|
||||
if (is_array($translations)) {
|
||||
foreach ($translations as $translation) {
|
||||
$langId = (string)($translation['lang_id'] ?? '');
|
||||
if ($langId !== '') {
|
||||
$value['languages'][$langId] = $translation;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$values[] = $value;
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $payload
|
||||
*/
|
||||
public function saveValues(int $attributeId, array $payload): bool
|
||||
{
|
||||
if ($attributeId <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$rowsRaw = [];
|
||||
if (isset($payload['rows']) && is_array($payload['rows'])) {
|
||||
$rowsRaw = $payload['rows'];
|
||||
} elseif (array_key_exists(0, $payload)) {
|
||||
$rowsRaw = $payload;
|
||||
}
|
||||
|
||||
$rows = $this->normalizeValueRows($rowsRaw);
|
||||
|
||||
$currentIds = $this->db->select(
|
||||
'pp_shop_attributes_values',
|
||||
'id',
|
||||
['attribute_id' => $attributeId]
|
||||
);
|
||||
if (!is_array($currentIds)) {
|
||||
$currentIds = [];
|
||||
}
|
||||
$currentIds = array_values(array_unique(array_map('intval', $currentIds)));
|
||||
|
||||
$incomingIds = [];
|
||||
foreach ($rows as $row) {
|
||||
$rowId = (int)($row['id'] ?? 0);
|
||||
if ($rowId > 0) {
|
||||
$incomingIds[$rowId] = $rowId;
|
||||
}
|
||||
}
|
||||
$incomingIds = array_values($incomingIds);
|
||||
|
||||
$deleteIds = array_diff($currentIds, $incomingIds);
|
||||
foreach ($deleteIds as $deleteId) {
|
||||
$this->db->delete('pp_shop_attributes_values_langs', ['value_id' => (int)$deleteId]);
|
||||
$this->db->delete('pp_shop_attributes_values', ['id' => (int)$deleteId]);
|
||||
}
|
||||
|
||||
$defaultValueId = 0;
|
||||
foreach ($rows as $row) {
|
||||
$rowId = (int)($row['id'] ?? 0);
|
||||
if ($rowId > 0 && !$this->valueBelongsToAttribute($rowId, $attributeId)) {
|
||||
$rowId = 0;
|
||||
}
|
||||
|
||||
$impactOnPrice = $this->toNullableNumeric($row['impact_on_the_price'] ?? null);
|
||||
|
||||
if ($rowId <= 0) {
|
||||
$this->db->insert('pp_shop_attributes_values', [
|
||||
'attribute_id' => $attributeId,
|
||||
'impact_on_the_price' => $impactOnPrice,
|
||||
'is_default' => 0,
|
||||
]);
|
||||
$rowId = (int)$this->db->id();
|
||||
if ($rowId <= 0) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
$this->db->update('pp_shop_attributes_values', [
|
||||
'impact_on_the_price' => $impactOnPrice,
|
||||
], [
|
||||
'id' => $rowId,
|
||||
]);
|
||||
}
|
||||
|
||||
$translations = is_array($row['translations'] ?? null) ? $row['translations'] : [];
|
||||
$this->saveValueTranslations($rowId, $translations);
|
||||
$this->refreshCombinationPricesForValue($rowId, $impactOnPrice);
|
||||
|
||||
if (!empty($row['is_default'])) {
|
||||
$defaultValueId = $rowId;
|
||||
}
|
||||
}
|
||||
|
||||
$this->db->update(
|
||||
'pp_shop_attributes_values',
|
||||
['is_default' => 0],
|
||||
['attribute_id' => $attributeId]
|
||||
);
|
||||
if ($defaultValueId > 0) {
|
||||
$this->db->update(
|
||||
'pp_shop_attributes_values',
|
||||
['is_default' => 1],
|
||||
['id' => $defaultValueId]
|
||||
);
|
||||
}
|
||||
|
||||
$this->clearTempAndCache();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Legacy compatibility for old payload shape.
|
||||
*
|
||||
* @param array<string, array<int, string>> $names
|
||||
* @param array<string, array<int, string>> $values
|
||||
* @param array<string, array<int, string|int>> $ids
|
||||
* @param mixed $defaultValue
|
||||
* @param array<int, string|float|int|null> $impactOnThePrice
|
||||
*/
|
||||
public function saveLegacyValues(
|
||||
int $attributeId,
|
||||
array $names,
|
||||
array $values,
|
||||
array $ids,
|
||||
$defaultValue = '',
|
||||
array $impactOnThePrice = []
|
||||
): ?int {
|
||||
if ($attributeId <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$mainLanguage = $this->defaultLanguageId();
|
||||
$mainLanguageNames = $names[$mainLanguage] ?? [];
|
||||
if (!is_array($mainLanguageNames)) {
|
||||
$mainLanguageNames = [];
|
||||
}
|
||||
|
||||
$rows = [];
|
||||
$count = count($mainLanguageNames);
|
||||
for ($i = 0; $i < $count; ++$i) {
|
||||
$rowId = 0;
|
||||
if (isset($ids[$mainLanguage]) && is_array($ids[$mainLanguage])) {
|
||||
$rowId = (int)($ids[$mainLanguage][$i] ?? 0);
|
||||
}
|
||||
|
||||
$translations = [];
|
||||
foreach ($names as $langId => $langNames) {
|
||||
if (!is_string($langId) || !is_array($langNames)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$translations[$langId] = [
|
||||
'name' => (string)($langNames[$i] ?? ''),
|
||||
'value' => isset($values[$langId]) && is_array($values[$langId])
|
||||
? (string)($values[$langId][$i] ?? '')
|
||||
: '',
|
||||
];
|
||||
}
|
||||
|
||||
$rows[] = [
|
||||
'id' => $rowId,
|
||||
'impact_on_the_price' => $impactOnThePrice[$i] ?? null,
|
||||
'is_default' => ((string)$defaultValue === (string)$i),
|
||||
'translations' => $translations,
|
||||
];
|
||||
}
|
||||
|
||||
$saved = $this->saveValues($attributeId, ['rows' => $rows]);
|
||||
return $saved ? $attributeId : null;
|
||||
}
|
||||
|
||||
public function valueDetails(int $valueId): array
|
||||
{
|
||||
if ($valueId <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$value = $this->db->get('pp_shop_attributes_values', '*', ['id' => $valueId]);
|
||||
if (!is_array($value)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$value['id'] = (int)($value['id'] ?? 0);
|
||||
$value['attribute_id'] = (int)($value['attribute_id'] ?? 0);
|
||||
$value['is_default'] = $this->toSwitchValue($value['is_default'] ?? 0);
|
||||
$value['impact_on_the_price'] = $this->toNullableNumeric($value['impact_on_the_price'] ?? null);
|
||||
$value['languages'] = [];
|
||||
|
||||
$translations = $this->db->select(
|
||||
'pp_shop_attributes_values_langs',
|
||||
['lang_id', 'name', 'value'],
|
||||
['value_id' => $valueId]
|
||||
);
|
||||
if (is_array($translations)) {
|
||||
foreach ($translations as $translation) {
|
||||
$langId = (string)($translation['lang_id'] ?? '');
|
||||
if ($langId !== '') {
|
||||
$value['languages'][$langId] = $translation;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function getAttributeNameById(int $attributeId, ?string $langId = null): string
|
||||
{
|
||||
if ($attributeId <= 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$languageId = $langId !== null && trim($langId) !== '' ? trim($langId) : $this->defaultLanguageId();
|
||||
return (string)$this->db->get(
|
||||
'pp_shop_attributes_langs',
|
||||
'name',
|
||||
[
|
||||
'AND' => [
|
||||
'attribute_id' => $attributeId,
|
||||
'lang_id' => $languageId,
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function getAttributeValueById(int $valueId, ?string $langId = null): string
|
||||
{
|
||||
if ($valueId <= 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$languageId = $langId !== null && trim($langId) !== '' ? trim($langId) : $this->defaultLanguageId();
|
||||
return (string)$this->db->get(
|
||||
'pp_shop_attributes_values_langs',
|
||||
'name',
|
||||
[
|
||||
'AND' => [
|
||||
'value_id' => $valueId,
|
||||
'lang_id' => $languageId,
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
public function getAttributesListForCombinations(): array
|
||||
{
|
||||
$rows = $this->db->select('pp_shop_attributes', '*', ['ORDER' => ['o' => 'ASC']]);
|
||||
if (!is_array($rows)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$attributes = [];
|
||||
foreach ($rows as $row) {
|
||||
$attributeId = (int)($row['id'] ?? 0);
|
||||
if ($attributeId <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$attribute = $this->findAttribute($attributeId);
|
||||
$attribute['values'] = $this->findValues($attributeId);
|
||||
$attributes[] = $attribute;
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{sql: string, params: array<string, mixed>}
|
||||
*/
|
||||
private function buildAdminWhere(array $filters): array
|
||||
{
|
||||
$where = ['1 = 1'];
|
||||
$params = [];
|
||||
|
||||
$name = trim((string)($filters['name'] ?? ''));
|
||||
if ($name !== '') {
|
||||
if (strlen($name) > 255) {
|
||||
$name = substr($name, 0, 255);
|
||||
}
|
||||
$where[] = 'EXISTS (
|
||||
SELECT 1
|
||||
FROM pp_shop_attributes_langs AS sal_filter
|
||||
WHERE sal_filter.attribute_id = sa.id
|
||||
AND sal_filter.name LIKE :name
|
||||
)';
|
||||
$params[':name'] = '%' . $name . '%';
|
||||
}
|
||||
|
||||
$status = trim((string)($filters['status'] ?? ''));
|
||||
if ($status === '0' || $status === '1') {
|
||||
$where[] = 'sa.status = :status';
|
||||
$params[':status'] = (int)$status;
|
||||
}
|
||||
|
||||
return [
|
||||
'sql' => implode(' AND ', $where),
|
||||
'params' => $params,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, string> $names
|
||||
*/
|
||||
private function saveAttributeTranslations(int $attributeId, array $names): void
|
||||
{
|
||||
foreach ($names as $langId => $name) {
|
||||
if (!is_string($langId) || trim($langId) === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$translationName = trim((string)$name);
|
||||
$where = [
|
||||
'AND' => [
|
||||
'attribute_id' => $attributeId,
|
||||
'lang_id' => $langId,
|
||||
],
|
||||
];
|
||||
|
||||
$translationId = $this->db->get('pp_shop_attributes_langs', 'id', $where);
|
||||
if ($translationId) {
|
||||
$this->db->update('pp_shop_attributes_langs', [
|
||||
'name' => $translationName,
|
||||
], [
|
||||
'id' => (int)$translationId,
|
||||
]);
|
||||
} else {
|
||||
$this->db->insert('pp_shop_attributes_langs', [
|
||||
'attribute_id' => $attributeId,
|
||||
'lang_id' => $langId,
|
||||
'name' => $translationName,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $translations
|
||||
*/
|
||||
private function saveValueTranslations(int $valueId, array $translations): void
|
||||
{
|
||||
foreach ($translations as $langId => $translationData) {
|
||||
if (!is_string($langId) || trim($langId) === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$name = '';
|
||||
$value = null;
|
||||
if (is_array($translationData)) {
|
||||
$name = trim((string)($translationData['name'] ?? ''));
|
||||
$rawValue = trim((string)($translationData['value'] ?? ''));
|
||||
$value = $rawValue !== '' ? $rawValue : null;
|
||||
} else {
|
||||
$name = trim((string)$translationData);
|
||||
}
|
||||
|
||||
$where = [
|
||||
'AND' => [
|
||||
'value_id' => $valueId,
|
||||
'lang_id' => $langId,
|
||||
],
|
||||
];
|
||||
|
||||
$translationId = $this->db->get('pp_shop_attributes_values_langs', 'id', $where);
|
||||
if ($name === '') {
|
||||
if ($translationId) {
|
||||
$this->db->delete('pp_shop_attributes_values_langs', ['id' => (int)$translationId]);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($translationId) {
|
||||
$this->db->update('pp_shop_attributes_values_langs', [
|
||||
'name' => $name,
|
||||
'value' => $value,
|
||||
], [
|
||||
'id' => (int)$translationId,
|
||||
]);
|
||||
} else {
|
||||
$this->db->insert('pp_shop_attributes_values_langs', [
|
||||
'value_id' => $valueId,
|
||||
'lang_id' => $langId,
|
||||
'name' => $name,
|
||||
'value' => $value,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function valueBelongsToAttribute(int $valueId, int $attributeId): bool
|
||||
{
|
||||
if ($valueId <= 0 || $attributeId <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool)$this->db->count('pp_shop_attributes_values', [
|
||||
'AND' => [
|
||||
'id' => $valueId,
|
||||
'attribute_id' => $attributeId,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array<string, mixed>> $rows
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
private function normalizeValueRows(array $rows): array
|
||||
{
|
||||
$normalizedRows = [];
|
||||
foreach ($rows as $row) {
|
||||
if (!is_array($row)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$translations = [];
|
||||
if (isset($row['translations']) && is_array($row['translations'])) {
|
||||
$translations = $row['translations'];
|
||||
}
|
||||
|
||||
$normalizedRows[] = [
|
||||
'id' => (int)($row['id'] ?? 0),
|
||||
'impact_on_the_price' => $row['impact_on_the_price'] ?? null,
|
||||
'is_default' => !empty($row['is_default']),
|
||||
'translations' => $translations,
|
||||
];
|
||||
}
|
||||
|
||||
return $normalizedRows;
|
||||
}
|
||||
|
||||
private function refreshCombinationPricesForValue(int $valueId, ?string $impactOnThePrice): void
|
||||
{
|
||||
if ($valueId <= 0 || $impactOnThePrice === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$impact = (float)$impactOnThePrice;
|
||||
|
||||
$products = $this->db->select('pp_shop_products_attributes', ['product_id'], ['value_id' => $valueId]);
|
||||
if (!is_array($products)) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($products as $row) {
|
||||
$productId = (int)($row['product_id'] ?? 0);
|
||||
if ($productId <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$parentId = (int)$this->db->get('pp_shop_products', 'parent_id', ['id' => $productId]);
|
||||
if ($parentId <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$parentProduct = $this->db->get('pp_shop_products', '*', ['id' => $parentId]);
|
||||
if (!is_array($parentProduct)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$parentPriceBrutto = (float)($parentProduct['price_brutto'] ?? 0);
|
||||
$parentVat = (float)($parentProduct['vat'] ?? 0);
|
||||
$parentPriceBruttoPromo = $parentProduct['price_brutto_promo'];
|
||||
$parentPriceNettoPromo = $parentProduct['price_netto_promo'];
|
||||
|
||||
if ($impact > 0) {
|
||||
$priceBrutto = $parentPriceBrutto + $impact;
|
||||
$priceNetto = $this->normalizeDecimal($priceBrutto / (1 + ($parentVat / 100)), 2);
|
||||
|
||||
if ($parentPriceNettoPromo !== null && $parentPriceBruttoPromo !== null) {
|
||||
$priceBruttoPromo = (float)$parentPriceBruttoPromo + $impact;
|
||||
$priceNettoPromo = $this->normalizeDecimal($priceBruttoPromo / (1 + ($parentVat / 100)), 2);
|
||||
} else {
|
||||
$priceBruttoPromo = null;
|
||||
$priceNettoPromo = null;
|
||||
}
|
||||
|
||||
$this->db->update('pp_shop_products', [
|
||||
'price_netto' => $priceNetto,
|
||||
'price_brutto' => $priceBrutto,
|
||||
'price_netto_promo' => $priceNettoPromo,
|
||||
'price_brutto_promo' => $priceBruttoPromo,
|
||||
'date_modify' => date('Y-m-d H:i:s'),
|
||||
], [
|
||||
'id' => $productId,
|
||||
]);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (abs($impact) < 0.000001) {
|
||||
$this->db->update('pp_shop_products', [
|
||||
'price_netto' => null,
|
||||
'price_brutto' => null,
|
||||
'price_netto_promo' => null,
|
||||
'price_brutto_promo' => null,
|
||||
'quantity' => null,
|
||||
'stock_0_buy' => null,
|
||||
'date_modify' => date('Y-m-d H:i:s'),
|
||||
], [
|
||||
'id' => $productId,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
private function toTypeValue($value): int
|
||||
{
|
||||
$type = (int)$value;
|
||||
if ($type < 0 || $type > 2) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $type;
|
||||
}
|
||||
|
||||
private function toNullableNumeric($value): ?string
|
||||
{
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$stringValue = trim((string)$value);
|
||||
if ($stringValue === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return str_replace(',', '.', $stringValue);
|
||||
}
|
||||
|
||||
private function defaultAttribute(): array
|
||||
{
|
||||
return [
|
||||
'id' => 0,
|
||||
'status' => 1,
|
||||
'type' => 0,
|
||||
'o' => $this->nextOrder(),
|
||||
'languages' => [],
|
||||
];
|
||||
}
|
||||
|
||||
private function nextOrder(): int
|
||||
{
|
||||
$maxOrder = $this->db->max('pp_shop_attributes', 'o');
|
||||
return max(0, (int)$maxOrder + 1);
|
||||
}
|
||||
|
||||
private function defaultLanguageId(): string
|
||||
{
|
||||
if ($this->defaultLangId !== null) {
|
||||
return $this->defaultLangId;
|
||||
}
|
||||
|
||||
$rows = $this->db->select('pp_langs', ['id', 'start', 'o'], [
|
||||
'status' => 1,
|
||||
'ORDER' => ['start' => 'DESC', 'o' => 'ASC'],
|
||||
]);
|
||||
|
||||
if (is_array($rows) && !empty($rows)) {
|
||||
$this->defaultLangId = (string)($rows[0]['id'] ?? '');
|
||||
} else {
|
||||
$this->defaultLangId = '';
|
||||
}
|
||||
|
||||
return $this->defaultLangId;
|
||||
}
|
||||
|
||||
private function clearTempAndCache(): void
|
||||
{
|
||||
if (class_exists('\S')) {
|
||||
if (method_exists('\S', 'delete_dir')) {
|
||||
\S::delete_dir('../temp/');
|
||||
}
|
||||
if (method_exists('\S', 'delete_cache')) {
|
||||
\S::delete_cache();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function normalizeDecimal(float $value, int $precision = 2): float
|
||||
{
|
||||
return round($value, $precision);
|
||||
}
|
||||
}
|
||||
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