refactor newsletter module and disable prepare/user templates

This commit is contained in:
2026-02-12 23:26:28 +01:00
parent 76287923e8
commit 0ac74b6cf4
26 changed files with 1182 additions and 602 deletions

View File

@@ -0,0 +1,59 @@
<?php
namespace Domain\Newsletter;
class NewsletterPreviewRenderer
{
public function render(array $articles, array $settings, ?array $template, string $dates = ''): string
{
$out = '<div style="border-bottom: 1px solid #ccc;">';
$out .= !empty($settings['newsletter_header'])
? (string)$settings['newsletter_header']
: '<p style="text-align: center;">--- brak zdefiniowanego naglowka ---</p>';
$out .= '</div>';
$out .= '<div style="border-bottom: 1px solid #ccc; padding: 10px 0 0 0;">';
if (is_array($template) && !empty($template)) {
$out .= '<div style="padding: 10px; background: #F1F1F1; margin-bottom: 10px">';
$out .= (string)($template['text'] ?? '');
$out .= '</div>';
}
if (is_array($articles) && !empty($articles)) {
foreach ($articles as $article) {
$articleId = (int)($article['id'] ?? 0);
$title = (string)($article['language']['title'] ?? '');
$seoLink = trim((string)($article['language']['seo_link'] ?? ''));
$url = $seoLink !== '' ? $seoLink : ('a-' . $articleId . '-' . \S::seo($title));
$entry = !empty($article['language']['entry'])
? (string)$article['language']['entry']
: (string)($article['language']['text'] ?? '');
$out .= '<div style="padding: 10px; background: #F1F1F1; margin-bottom: 10px">';
$out .= '<a href="http://' . htmlspecialchars((string)($_SERVER['SERVER_NAME'] ?? ''), ENT_QUOTES, 'UTF-8') . '/'
. htmlspecialchars($url, ENT_QUOTES, 'UTF-8')
. '" title="' . htmlspecialchars($title, ENT_QUOTES, 'UTF-8')
. '" style="margin-bottom: 10px; display: block; font-size: 14px; color: #5b7fb1; font-weight: 600;">'
. htmlspecialchars($title, ENT_QUOTES, 'UTF-8')
. '</a>';
$out .= '<div>' . $entry . '</div>';
$out .= '<div style="clear: both;"></div>';
$out .= '</div>';
}
} elseif (trim($dates) !== '') {
$out .= '<div style="padding: 10px; background: #F1F1F1; margin-bottom: 10px; text-align: center;">';
$out .= '--- brak artykulow w danym okresie ---';
$out .= '</div>';
}
$out .= '</div>';
$out .= '<div style="border-bottom: 1px solid #ccc; padding: 10px 0 0 0;">';
$out .= !empty($settings['newsletter_footer'])
? (string)$settings['newsletter_footer']
: '<p style="text-align: center;">--- brak zdefiniowanej stopki ---</p>';
$out .= '</div>';
return $out;
}
}

View File

@@ -0,0 +1,292 @@
<?php
namespace Domain\Newsletter;
use Domain\Settings\SettingsRepository;
class NewsletterRepository
{
private const MAX_PER_PAGE = 100;
private $db;
private SettingsRepository $settingsRepository;
public function __construct($db, ?SettingsRepository $settingsRepository = null)
{
$this->db = $db;
$this->settingsRepository = $settingsRepository ?? new SettingsRepository($db);
}
public function getSettings(): array
{
return $this->settingsRepository->getSettings();
}
public function saveSettings(array $values): bool
{
$this->settingsRepository->updateSetting('newsletter_footer', (string)($values['newsletter_footer'] ?? ''));
$this->settingsRepository->updateSetting('newsletter_header', (string)($values['newsletter_header'] ?? ''));
return true;
}
public function queueSend(string $dates = '', int $templateId = 0): bool
{
$subscribers = $this->db->select('pp_newsletter', 'email', ['status' => 1]);
if (!is_array($subscribers) || empty($subscribers)) {
return true;
}
$cleanDates = trim($dates);
$templateId = $templateId > 0 ? $templateId : 0;
foreach ($subscribers as $subscriber) {
$email = is_array($subscriber) ? (string)($subscriber['email'] ?? '') : (string)$subscriber;
if ($email === '') {
continue;
}
$this->db->insert('pp_newsletter_send', [
'email' => $email,
'dates' => $cleanDates,
'id_template' => $templateId > 0 ? $templateId : null,
]);
}
return true;
}
public function templateByName(string $templateName): string
{
return (string)$this->db->get('pp_newsletter_templates', 'text', ['name' => $templateName]);
}
public function templateDetails(int $templateId): ?array
{
if ($templateId <= 0) {
return null;
}
$row = $this->db->get('pp_newsletter_templates', '*', ['id' => $templateId]);
if (!is_array($row)) {
return null;
}
return $row;
}
public function isAdminTemplate(int $templateId): bool
{
$isAdmin = $this->db->get('pp_newsletter_templates', 'is_admin', ['id' => $templateId]);
return (int)$isAdmin === 1;
}
public function deleteTemplate(int $templateId): bool
{
if ($templateId <= 0 || $this->isAdminTemplate($templateId)) {
return false;
}
return (bool)$this->db->delete('pp_newsletter_templates', ['id' => $templateId]);
}
public function saveTemplate(int $templateId, string $name, string $text): ?int
{
$templateId = max(0, $templateId);
$name = trim($name);
if ($templateId <= 0) {
if ($name === '') {
return null;
}
$ok = $this->db->insert('pp_newsletter_templates', [
'name' => $name,
'text' => $text,
'is_admin' => 0,
]);
if (!$ok) {
return null;
}
\S::delete_dir('../temp/');
return (int)$this->db->id();
}
$current = $this->templateDetails($templateId);
if (!is_array($current)) {
return null;
}
if ((int)($current['is_admin'] ?? 0) === 1) {
$name = (string)($current['name'] ?? '');
}
$this->db->update('pp_newsletter_templates', [
'name' => $name,
'text' => $text,
], [
'id' => $templateId,
]);
\S::delete_dir('../temp/');
return $templateId;
}
public function listTemplatesSimple(bool $adminTemplates = false): array
{
$rows = $this->db->select('pp_newsletter_templates', '*', [
'is_admin' => $adminTemplates ? 1 : 0,
'ORDER' => ['name' => 'ASC'],
]);
return is_array($rows) ? $rows : [];
}
public function deleteSubscriber(int $subscriberId): bool
{
if ($subscriberId <= 0) {
return false;
}
return (bool)$this->db->delete('pp_newsletter', ['id' => $subscriberId]);
}
/**
* @return array{items: array<int, array<string, mixed>>, total: int}
*/
public function listSubscribersForAdmin(
array $filters,
string $sortColumn = 'email',
string $sortDir = 'ASC',
int $page = 1,
int $perPage = 15
): array {
$allowedSortColumns = [
'id' => 'pn.id',
'email' => 'pn.email',
'status' => 'pn.status',
];
$sortSql = $allowedSortColumns[$sortColumn] ?? 'pn.email';
$sortDir = strtoupper(trim($sortDir)) === 'DESC' ? 'DESC' : 'ASC';
$page = max(1, $page);
$perPage = min(self::MAX_PER_PAGE, max(1, $perPage));
$offset = ($page - 1) * $perPage;
$where = ['1 = 1'];
$params = [];
$email = trim((string)($filters['email'] ?? ''));
if ($email !== '') {
if (strlen($email) > 255) {
$email = substr($email, 0, 255);
}
$where[] = 'pn.email LIKE :email';
$params[':email'] = '%' . $email . '%';
}
$status = trim((string)($filters['status'] ?? ''));
if ($status === '0' || $status === '1') {
$where[] = 'pn.status = :status';
$params[':status'] = (int)$status;
}
$whereSql = implode(' AND ', $where);
$sqlCount = "
SELECT COUNT(0)
FROM pp_newsletter AS pn
WHERE {$whereSql}
";
$stmtCount = $this->db->query($sqlCount, $params);
$countRows = $stmtCount ? $stmtCount->fetchAll() : [];
$total = isset($countRows[0][0]) ? (int)$countRows[0][0] : 0;
$sql = "
SELECT
pn.id,
pn.email,
pn.status
FROM pp_newsletter AS pn
WHERE {$whereSql}
ORDER BY {$sortSql} {$sortDir}, pn.id ASC
LIMIT {$perPage} OFFSET {$offset}
";
$stmt = $this->db->query($sql, $params);
$items = $stmt ? $stmt->fetchAll() : [];
return [
'items' => is_array($items) ? $items : [],
'total' => $total,
];
}
/**
* @return array{items: array<int, array<string, mixed>>, total: int}
*/
public function listTemplatesForAdmin(
bool $adminTemplates,
array $filters,
string $sortColumn = 'name',
string $sortDir = 'ASC',
int $page = 1,
int $perPage = 15
): array {
$allowedSortColumns = [
'id' => 'pnt.id',
'name' => 'pnt.name',
];
$sortSql = $allowedSortColumns[$sortColumn] ?? 'pnt.name';
$sortDir = strtoupper(trim($sortDir)) === 'DESC' ? 'DESC' : 'ASC';
$page = max(1, $page);
$perPage = min(self::MAX_PER_PAGE, max(1, $perPage));
$offset = ($page - 1) * $perPage;
$where = ['pnt.is_admin = :is_admin'];
$params = [':is_admin' => $adminTemplates ? 1 : 0];
$name = trim((string)($filters['name'] ?? ''));
if ($name !== '') {
if (strlen($name) > 255) {
$name = substr($name, 0, 255);
}
$where[] = 'pnt.name LIKE :name';
$params[':name'] = '%' . $name . '%';
}
$whereSql = implode(' AND ', $where);
$sqlCount = "
SELECT COUNT(0)
FROM pp_newsletter_templates AS pnt
WHERE {$whereSql}
";
$stmtCount = $this->db->query($sqlCount, $params);
$countRows = $stmtCount ? $stmtCount->fetchAll() : [];
$total = isset($countRows[0][0]) ? (int)$countRows[0][0] : 0;
$sql = "
SELECT
pnt.id,
pnt.name,
pnt.is_admin
FROM pp_newsletter_templates AS pnt
WHERE {$whereSql}
ORDER BY {$sortSql} {$sortDir}, pnt.id ASC
LIMIT {$perPage} OFFSET {$offset}
";
$stmt = $this->db->query($sql, $params);
$items = $stmt ? $stmt->fetchAll() : [];
return [
'items' => is_array($items) ? $items : [],
'total' => $total,
];
}
}