- Introduced new `SettingsRepository` and `CacheRepository` classes in the `autoload\Domain` namespace. - Updated `SettingsController` in the `admin\Controllers` namespace to enhance settings management. - Added new templates for settings in `admin\templates\settings` and `admin\templates\site`. - Improved overall structure and organization of settings-related files.
110 lines
3.0 KiB
PHP
110 lines
3.0 KiB
PHP
<?php
|
|
namespace Domain\Banner;
|
|
|
|
/**
|
|
* Repository odpowiedzialny za dostęp do danych banerów
|
|
*/
|
|
class BannerRepository
|
|
{
|
|
private $db;
|
|
|
|
public function __construct($db)
|
|
{
|
|
$this->db = $db;
|
|
}
|
|
|
|
/**
|
|
* Pobiera baner po ID wraz z tłumaczeniami
|
|
*
|
|
* @param int $bannerId ID banera
|
|
* @return array|null Dane banera lub null
|
|
*/
|
|
public function find(int $bannerId): ?array
|
|
{
|
|
$banner = $this->db->get('pp_banners', '*', ['id' => $bannerId]);
|
|
|
|
if (!$banner) {
|
|
return null;
|
|
}
|
|
|
|
$results = $this->db->select('pp_banners_langs', '*', ['id_banner' => $bannerId]);
|
|
if (is_array($results)) {
|
|
foreach ($results as $row) {
|
|
$banner['languages'][$row['id_lang']] = $row;
|
|
}
|
|
}
|
|
|
|
return $banner;
|
|
}
|
|
|
|
/**
|
|
* Usuwa baner
|
|
*
|
|
* @param int $bannerId ID banera
|
|
* @return bool Czy usunięto
|
|
*/
|
|
public function delete(int $bannerId): bool
|
|
{
|
|
$result = $this->db->delete('pp_banners', ['id' => $bannerId]);
|
|
return $result !== false;
|
|
}
|
|
|
|
/**
|
|
* Zapisuje baner (insert lub update)
|
|
*
|
|
* @param array $data Dane banera
|
|
* @return int|false ID banera lub false
|
|
*/
|
|
public function save(array $data)
|
|
{
|
|
$bannerId = $data['id'] ?? null;
|
|
|
|
$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,
|
|
];
|
|
|
|
if (!$bannerId) {
|
|
$this->db->insert('pp_banners', $bannerData);
|
|
$bannerId = $this->db->id();
|
|
if (!$bannerId) {
|
|
return false;
|
|
}
|
|
} else {
|
|
$this->db->update('pp_banners', $bannerData, ['id' => (int)$bannerId]);
|
|
}
|
|
|
|
$this->saveTranslations($bannerId, $data['src'], $data['url'], $data['html'], $data['text']);
|
|
|
|
return (int)$bannerId;
|
|
}
|
|
|
|
/**
|
|
* Zapisuje tłumaczenia banera
|
|
*/
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|