Release 0.249: banner edit fixes and thumbnail popup
This commit is contained in:
@@ -29,7 +29,10 @@ class BannerRepository
|
||||
return null;
|
||||
}
|
||||
|
||||
$results = $this->db->select('pp_banners_langs', '*', ['id_banner' => $bannerId]);
|
||||
$results = $this->db->select('pp_banners_langs', '*', [
|
||||
'id_banner' => $bannerId,
|
||||
'ORDER' => ['id' => 'ASC'],
|
||||
]);
|
||||
if (is_array($results)) {
|
||||
foreach ($results as $row) {
|
||||
$banner['languages'][$row['id_lang']] = $row;
|
||||
@@ -54,19 +57,30 @@ class BannerRepository
|
||||
/**
|
||||
* Zapisuje baner (insert lub update)
|
||||
*
|
||||
* @param array $data Dane banera
|
||||
* @param array $data Dane banera (obsługuje format z FormRequestHandler lub stary format)
|
||||
* @return int|false ID banera lub false
|
||||
*/
|
||||
public function save(array $data)
|
||||
{
|
||||
$bannerId = $data['id'] ?? null;
|
||||
|
||||
// Obsługa obu formatów: nowy (int) i stary ('on'/string)
|
||||
$status = $data['status'] ?? 0;
|
||||
if ($status === 'on') {
|
||||
$status = 1;
|
||||
}
|
||||
|
||||
$homePage = $data['home_page'] ?? 0;
|
||||
if ($homePage === 'on') {
|
||||
$homePage = 1;
|
||||
}
|
||||
|
||||
$bannerData = [
|
||||
'name' => $data['name'],
|
||||
'status' => $data['status'] == 'on' ? 1 : 0,
|
||||
'date_start' => $data['date_start'] != '' ? $data['date_start'] : null,
|
||||
'date_end' => $data['date_end'] != '' ? $data['date_end'] : null,
|
||||
'home_page' => $data['home_page'] == 'on' ? 1 : 0,
|
||||
'status' => (int)$status,
|
||||
'date_start' => !empty($data['date_start']) ? $data['date_start'] : null,
|
||||
'date_end' => !empty($data['date_end']) ? $data['date_end'] : null,
|
||||
'home_page' => (int)$homePage,
|
||||
];
|
||||
|
||||
if (!$bannerId) {
|
||||
@@ -79,7 +93,14 @@ class BannerRepository
|
||||
$this->db->update('pp_banners', $bannerData, ['id' => (int)$bannerId]);
|
||||
}
|
||||
|
||||
$this->saveTranslations($bannerId, $data['src'], $data['url'], $data['html'], $data['text']);
|
||||
// Obsługa danych językowych - nowy format (translations) lub stary (src/url/html/text)
|
||||
if (isset($data['translations']) && is_array($data['translations'])) {
|
||||
// Nowy format z FormRequestHandler
|
||||
$this->saveTranslationsFromArray($bannerId, $data['translations']);
|
||||
} elseif (isset($data['src']) && is_array($data['src'])) {
|
||||
// Stary format (backward compatibility)
|
||||
$this->saveTranslations($bannerId, $data['src'], $data['url'], $data['html'], $data['text']);
|
||||
}
|
||||
|
||||
return (int)$bannerId;
|
||||
}
|
||||
@@ -159,35 +180,134 @@ class BannerRepository
|
||||
|
||||
$stmt = $this->db->query($sql, $params);
|
||||
$items = $stmt ? $stmt->fetchAll() : [];
|
||||
$items = is_array($items) ? $items : [];
|
||||
|
||||
if (!empty($items)) {
|
||||
$bannerIds = array_map('intval', array_column($items, 'id'));
|
||||
$thumbByBannerId = $this->fetchThumbnailsByBannerIds($bannerIds);
|
||||
|
||||
foreach ($items as &$item) {
|
||||
$item['thumbnail_src'] = $thumbByBannerId[(int)($item['id'] ?? 0)] ?? '';
|
||||
}
|
||||
unset($item);
|
||||
}
|
||||
|
||||
return [
|
||||
'items' => is_array($items) ? $items : [],
|
||||
'items' => $items,
|
||||
'total' => $total,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Zapisuje tłumaczenia banera
|
||||
* Pobiera pierwsza dostepna sciezke obrazka (src) dla kazdego banera.
|
||||
*
|
||||
* @param array<int, int> $bannerIds
|
||||
* @return array<int, string> [id_banner => src]
|
||||
*/
|
||||
private function fetchThumbnailsByBannerIds(array $bannerIds): array
|
||||
{
|
||||
$bannerIds = array_values(array_unique(array_filter($bannerIds, static function ($id): bool {
|
||||
return (int)$id > 0;
|
||||
})));
|
||||
|
||||
if (empty($bannerIds)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$in = [];
|
||||
$params = [];
|
||||
foreach ($bannerIds as $index => $bannerId) {
|
||||
$placeholder = ':id' . $index;
|
||||
$in[] = $placeholder;
|
||||
$params[$placeholder] = (int)$bannerId;
|
||||
}
|
||||
|
||||
$sql = '
|
||||
SELECT id_banner, src
|
||||
FROM pp_banners_langs
|
||||
WHERE id_banner IN (' . implode(', ', $in) . ')
|
||||
AND src IS NOT NULL
|
||||
AND src <> \'\'
|
||||
ORDER BY id_lang ASC, id ASC
|
||||
';
|
||||
|
||||
$stmt = $this->db->query($sql, $params);
|
||||
$rows = $stmt ? $stmt->fetchAll() : [];
|
||||
if (!is_array($rows)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$thumbByBannerId = [];
|
||||
foreach ($rows as $row) {
|
||||
$bannerId = (int)($row['id_banner'] ?? 0);
|
||||
if ($bannerId <= 0 || isset($thumbByBannerId[$bannerId])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$src = trim((string)($row['src'] ?? ''));
|
||||
if ($src !== '') {
|
||||
$thumbByBannerId[$bannerId] = $src;
|
||||
}
|
||||
}
|
||||
|
||||
return $thumbByBannerId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zapisuje tłumaczenia banera (stary format - zachowano dla kompatybilności)
|
||||
*/
|
||||
private function saveTranslations(int $bannerId, array $src, array $url, array $html, array $text): void
|
||||
{
|
||||
foreach ($src as $langId => $val) {
|
||||
$translationData = [
|
||||
'id_banner' => $bannerId,
|
||||
'id_lang' => $langId,
|
||||
'src' => $src[$langId],
|
||||
'url' => $url[$langId],
|
||||
'html' => $html[$langId],
|
||||
'text' => $text[$langId],
|
||||
];
|
||||
|
||||
$existingId = $this->db->get('pp_banners_langs', 'id', ['AND' => ['banner_id' => $bannerId, 'lang_id' => $langId]]);
|
||||
|
||||
if ($existingId) {
|
||||
$this->db->update('pp_banners_langs', $translationData, ['id' => $existingId]);
|
||||
} else {
|
||||
$this->db->insert('pp_banners_langs', $translationData);
|
||||
}
|
||||
$this->upsertTranslation($bannerId, $langId, [
|
||||
'src' => $src[$langId] ?? '',
|
||||
'url' => $url[$langId] ?? '',
|
||||
'html' => $html[$langId] ?? '',
|
||||
'text' => $text[$langId] ?? '',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Zapisuje tłumaczenia banera z nowego formatu (z FormRequestHandler)
|
||||
* Format: [lang_id => [field => value]]
|
||||
*/
|
||||
private function saveTranslationsFromArray(int $bannerId, array $translations): void
|
||||
{
|
||||
foreach ($translations as $langId => $fields) {
|
||||
$this->upsertTranslation($bannerId, $langId, [
|
||||
'src' => $fields['src'] ?? '',
|
||||
'url' => $fields['url'] ?? '',
|
||||
'html' => $fields['html'] ?? '',
|
||||
'text' => $fields['text'] ?? '',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Upsert tlumaczenia banera.
|
||||
* Aktualizuje wszystkie rekordy dla pary id_banner + id_lang,
|
||||
* co usuwa problem z historycznymi duplikatami.
|
||||
*/
|
||||
private function upsertTranslation(int $bannerId, $langId, array $fields): void
|
||||
{
|
||||
$where = ['AND' => ['id_banner' => $bannerId, 'id_lang' => $langId]];
|
||||
$translationData = [
|
||||
'id_banner' => $bannerId,
|
||||
'id_lang' => $langId,
|
||||
'src' => $fields['src'] ?? '',
|
||||
'url' => $fields['url'] ?? '',
|
||||
'html' => $fields['html'] ?? '',
|
||||
'text' => $fields['text'] ?? '',
|
||||
];
|
||||
|
||||
$hasExisting = (int)$this->db->count('pp_banners_langs', $where) > 0;
|
||||
|
||||
if ($hasExisting) {
|
||||
$this->db->update('pp_banners_langs', $translationData, $where);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->db->insert('pp_banners_langs', $translationData);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user