feat(domain): Domain\Authors + Domain\Newsletter repositories z wrapper delegation

Phase 4 complete:
- AuthorsRepository: simpleList, authorDetails, authorSave, authorDelete, authorByLang
- NewsletterRepository: 14 methods — subscriber lifecycle, templates, sending
- 4 legacy factories converted to thin wrappers
- Globals ($settings, $lang) passed as explicit params to repo methods

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-04 18:21:32 +02:00
parent 73ff0ca5b6
commit ffe661b4d2
11 changed files with 833 additions and 273 deletions

View File

@@ -0,0 +1,156 @@
<?php
namespace Domain\Authors;
class AuthorsRepository
{
private $db;
public function __construct($db)
{
$this->db = $db;
}
/**
* Prosta lista autorow
* @return array|bool
*/
public function simpleList()
{
return $this->db->select('pp_authors', '*', ['ORDER' => ['author' => 'ASC']]);
}
/**
* Szczegoly autora z jezykami
* @param int $authorId
* @return array|bool
*/
public function authorDetails($authorId)
{
$author = $this->db->get('pp_authors', '*', ['id' => (int)$authorId]);
$results = $this->db->select('pp_authors_langs', '*', ['id_author' => (int)$authorId]);
if (is_array($results)) foreach ($results as $row)
$author['languages'][$row['id_lang']] = $row;
return $author;
}
/**
* Zapis autora (insert lub update)
* @param int $authorId
* @param string $author
* @param string $image
* @param string|array $description
* @return int|bool
*/
public function authorSave($authorId, $author, $image, $description)
{
if (!$authorId)
{
$this->db->insert('pp_authors', [
'author' => $author,
'image' => $image
]);
$id = $this->db->id();
if ($id)
{
$i = 0;
$results = $this->db->select('pp_langs', ['id'], ['status' => 1, 'ORDER' => ['o' => 'ASC']]);
if (is_array($results) and count($results) > 1) foreach ($results as $row)
{
$this->db->insert('pp_authors_langs', [
'id_author' => (int)$id,
'id_lang' => $row['id'],
'description' => $description[$i]
]);
$i++;
}
else if (is_array($results) and count($results) == 1) foreach ($results as $row)
{
$this->db->insert('pp_authors_langs', [
'id_author' => (int)$id,
'id_lang' => $row['id'],
'description' => $description
]);
}
\S::delete_cache();
return $id;
}
}
else
{
$this->db->update('pp_authors', [
'author' => $author,
'image' => $image
], [
'id' => (int)$authorId
]);
$this->db->delete('pp_authors_langs', ['id_author' => (int)$authorId]);
$i = 0;
$results = $this->db->select('pp_langs', ['id'], ['status' => 1, 'ORDER' => ['o' => 'ASC']]);
if (is_array($results) and count($results) > 1) foreach ($results as $row)
{
$this->db->insert('pp_authors_langs', [
'id_author' => (int)$authorId,
'id_lang' => $row['id'],
'description' => $description[$i]
]);
$i++;
}
else if (is_array($results) and count($results) == 1) foreach ($results as $row)
{
$this->db->insert('pp_authors_langs', [
'id_author' => (int)$authorId,
'id_lang' => $row['id'],
'description' => $description
]);
}
\S::delete_cache();
return $authorId;
}
return false;
}
/**
* Usuniecie autora
* @param int $authorId
* @return object|bool
*/
public function authorDelete($authorId)
{
$result = $this->db->delete('pp_authors', ['id' => (int)$authorId]);
\S::delete_cache();
return $result;
}
/**
* Szczegoly autora z cache (front)
* @param int $authorId
* @return array|bool
*/
public function authorByLang($authorId)
{
if (!$author = \Shared\Cache\CacheHandler::fetch("get_single_author:$authorId"))
{
$author = $this->db->get('pp_authors', '*', ['id' => (int)$authorId]);
$results = $this->db->select('pp_authors_langs', '*', ['id_author' => (int)$authorId]);
if (is_array($results)) foreach ($results as $row)
$author['languages'][$row['id_lang']] = $row;
\Shared\Cache\CacheHandler::store("get_single_author:$authorId", $author);
}
return $author;
}
}

View File

@@ -0,0 +1,281 @@
<?php
namespace Domain\Newsletter;
class NewsletterRepository
{
private $db;
public function __construct($db)
{
$this->db = $db;
}
/**
* Import emaili do newslettera
* @param string $emails
* @return bool
*/
public function emailsImport($emails)
{
$emails = explode(PHP_EOL, $emails);
if (is_array($emails)) foreach ($emails as $email)
{
if (trim($email) and !$this->db->count('pp_newsletter', ['email' => trim($email)]))
$this->db->insert('pp_newsletter', [
'email' => trim($email),
'hash' => md5($email . time()),
'status' => 1
]);
}
return true;
}
/**
* Sprawdza czy szablon jest adminski
* @param int $templateId
* @return string|bool
*/
public function isAdminTemplate($templateId)
{
return $this->db->get('pp_newsletter_templates', 'is_admin', ['id' => (int)$templateId]);
}
/**
* Usuniecie szablonu newslettera
* @param int $templateId
* @return object|bool
*/
public function templateDelete($templateId)
{
return $this->db->delete('pp_newsletter_templates', ['id' => (int)$templateId]);
}
/**
* Wysylka newslettera - kolejkowanie
* @param string $dates
* @param int $template
* @param string $onlyOnce
* @return bool
*/
public function send($dates, $template, $onlyOnce)
{
$results = $this->db->select('pp_newsletter', 'email', ['status' => 1]);
if (is_array($results) and !empty($results)) foreach ($results as $row)
{
if ($template and $onlyOnce)
{
if (!$this->db->count('pp_newsletter_send', ['AND' => ['id_template' => $template, 'email' => $row]]))
$this->db->insert('pp_newsletter_send', [
'email' => $row,
'dates' => $dates,
'id_template' => $template ? $template : null,
'only_once' => ($onlyOnce == 'on' and $template) ? 1 : 0
]);
}
else
$this->db->insert('pp_newsletter_send', [
'email' => $row,
'dates' => $dates,
'id_template' => $template ? $template : null,
'only_once' => ($onlyOnce == 'on' and $template) ? 1 : 0
]);
}
return true;
}
/**
* Szczegoly szablonu email
* @param int $templateId
* @return array|bool
*/
public function templateDetails($templateId)
{
$result = $this->db->get('pp_newsletter_templates', '*', ['id' => (int)$templateId]);
return $result;
}
/**
* Zapis szablonu (insert lub update)
* @param int $id
* @param string $name
* @param string $text
* @return int|bool
*/
public function templateSave($id, $name, $text)
{
if (!$id)
{
if ($this->db->insert('pp_newsletter_templates', [
'name' => $name,
'text' => $text
]))
{
\S::delete_cache();
return $this->db->id();
}
}
else
{
$this->db->update('pp_newsletter_templates', [
'name' => $name,
'text' => $text
], [
'id' => (int)$id
]);
\S::delete_cache();
return $id;
}
}
/**
* Lista szablonow (nie-adminskich)
* @return array|bool
*/
public function templatesList()
{
return $this->db->select('pp_newsletter_templates', '*', ['is_admin' => 0, 'ORDER' => ['name' => 'ASC']]);
}
/**
* Wypisanie z newslettera po hashu
* @param string $hash
* @return object|bool
*/
public function unsubscribe($hash)
{
return $this->db->update('pp_newsletter', ['status' => 0], ['hash' => $hash]);
}
/**
* Potwierdzenie zapisu po hashu
* @param string $hash
* @return bool
*/
public function confirm($hash)
{
if (!$id = $this->db->get('pp_newsletter', 'id', ['AND' => ['hash' => $hash, 'status' => 0]]))
return false;
else
$this->db->update('pp_newsletter', ['status' => 1], ['id' => $id]);
return true;
}
/**
* Wysylka zakolejkowanych newsletterow (cron/front)
* @param int $limit
* @param array $settings
* @param array $lang
* @return bool
*/
public function newsletterSend($limit, $settings, $lang)
{
$results = $this->db->query('SELECT * FROM pp_newsletter_send WHERE mailed = 0 ORDER BY id ASC LIMIT ' . (int)$limit)->fetchAll();
if (is_array($results) and !empty($results))
{
foreach ($results as $row)
{
$dates = explode(' - ', $row['dates']);
$text = \admin\view\Newsletter::preview(
\admin\factory\Articles::articles_by_date_add($dates[0], $dates[1]),
\admin\factory\Settings::settings_details(),
\admin\factory\Newsletter::email_template_detalis($row['id_template'])
);
if ($settings['ssl']) $base = 'https'; else $base = 'http';
$link = $base . "://" . $_SERVER['SERVER_NAME'] . '/newsletter/unsubscribe/hash=' . $this->getHash($row['email']);
$text = str_replace('[WYPISZ_SIE]', $link, $text);
$regex = "-(<img[^>]+src\s*=\s*['\"])(((?!'|\"|http(|s)://).)*)(['\"][^>]*>)-i";
$text = preg_replace($regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text);
$regex = "-(<a[^>]+href\s*=\s*['\"])(((?!'|\"|http(|s)://).)*)(['\"][^>]*>)-i";
$text = preg_replace($regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text);
\S::send_email($row['email'], 'Newsletter ze strony: ' . $_SERVER['SERVER_NAME'], $text);
if ($row['only_once'])
$this->db->update('pp_newsletter_send', ['mailed' => 1], ['id' => $row['id']]);
else
$this->db->delete('pp_newsletter_send', ['id' => $row['id']]);
}
return true;
}
return false;
}
/**
* Pobranie hasha dla emaila
* @param string $email
* @return string|bool
*/
public function getHash($email)
{
return $this->db->get('pp_newsletter', 'hash', ['email' => $email]);
}
/**
* Zapis do newslettera z wysylka potwierdzenia
* @param string $email
* @param array $settings
* @param array $lang
* @return bool
*/
public function signin($email, $settings, $lang)
{
if (!\S::email_check($email))
return false;
if (!$this->db->get('pp_newsletter', 'id', ['email' => $email]))
{
$hash = md5(time() . $email);
$text = $settings['newsletter_header'];
$text .= $this->getTemplate('#potwierdzenie-zapisu-do-newslettera');
$text .= $settings['newsletter_footer_1'];
$settings['ssl'] ? $base = 'https' : $base = 'http';
$regex = "-(<img[^>]+src\s*=\s*['\"])(((?!'|\"|http://).)*)(['\"][^>]*>)-i";
$text = preg_replace($regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text);
$regex = "-(<a[^>]+href\s*=\s*['\"])(((?!'|\"|http://).)*)(['\"][^>]*>)-i";
$text = preg_replace($regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text);
$link = '/newsletter/confirm/hash=' . $hash;
$text = str_replace('[LINK]', $link, $text);
$send = \S::send_email($email, $lang['potwierdz-zapisanie-sie-do-newslettera'], $text);
$this->db->insert('pp_newsletter', ['email' => $email, 'hash' => $hash, 'status' => 0]);
return true;
}
return false;
}
/**
* Pobranie szablonu po nazwie
* @param string $templateName
* @return string|bool
*/
public function getTemplate($templateName)
{
return $this->db->get('pp_newsletter_templates', 'text', ['name' => $templateName]);
}
/**
* Wypisanie z newslettera po emailu
* @param string $email
* @return object|bool
*/
public function signout($email)
{
if ($this->db->get('pp_newsletter', 'id', ['email' => $email]))
return $this->db->delete('pp_newsletter', ['email' => $email]);
return false;
}
}

View File

@@ -1,4 +1,4 @@
<?
<?php
namespace admin\factory;
class Authors
{
@@ -6,112 +6,31 @@ class Authors
static public function get_simple_list()
{
global $mdb;
return $mdb -> select( 'pp_authors', '*', [ 'ORDER' => [ 'author' => 'ASC' ] ] );
$repo = new \Domain\Authors\AuthorsRepository($mdb);
return $repo->simpleList();
}
// usunięcie autora
static public function delete_author( $id_author )
{
global $mdb;
$result = $mdb -> delete( 'pp_authors', [ 'id' => (int)$id_author ] );
\S::delete_cache();
return $result;
$repo = new \Domain\Authors\AuthorsRepository($mdb);
return $repo->authorDelete($id_author);
}
// zapis autora
static public function save_author( $id_author, $author, $image, $description )
{
global $mdb;
if ( !$id_author )
{
$mdb -> insert( 'pp_authors', [
'author' => $author,
'image' => $image
] );
$id = $mdb -> id();
if ( $id )
{
$i = 0;
$results = $mdb -> select( 'pp_langs', [ 'id' ], [ 'status' => 1, 'ORDER' => [ 'o' => 'ASC' ] ] );
if ( is_array( $results ) and count( $results ) > 1 ) foreach ( $results as $row )
{
$mdb -> insert( 'pp_authors_langs', [
'id_author' => (int)$id,
'id_lang' => $row['id'],
'description' => $description[ $i ]
] );
$i++;
}
else if ( is_array( $results ) and count( $results ) == 1 ) foreach ( $results as $row )
{
$mdb -> insert( 'pp_authors_langs', [
'id_author' => (int)$id,
'id_lang' => $row['id'],
'description' => $description
] );
}
\S::delete_cache();
return $id;
}
}
else
{
$mdb -> update( 'pp_authors', [
'author' => $author,
'image' => $image
], [
'id' => (int)$id_author
] );
$mdb -> delete( 'pp_authors_langs', [ 'id_author' => (int)$id_author ] );
$i = 0;
$results = $mdb -> select( 'pp_langs', [ 'id' ], [ 'status' => 1, 'ORDER' => [ 'o' => 'ASC' ] ] );
if ( is_array( $results ) and count( $results ) > 1 ) foreach ( $results as $row )
{
$mdb -> insert( 'pp_authors_langs', [
'id_author' => (int)$id_author,
'id_lang' => $row['id'],
'description' => $description[ $i ]
] );
$i++;
}
else if ( is_array( $results ) and count( $results ) == 1 ) foreach ( $results as $row )
{
$mdb -> insert( 'pp_authors_langs', [
'id_author' => (int)$id_author,
'id_lang' => $row['id'],
'description' => $description
] );
}
\S::delete_cache();
return $id_author;
}
return false;
$repo = new \Domain\Authors\AuthorsRepository($mdb);
return $repo->authorSave($id_author, $author, $image, $description);
}
// szczególy autora
static public function get_single_author( $id_author )
{
global $mdb;
$author = $mdb -> get( 'pp_authors', '*', [ 'id' => (int)$id_author ] );
$results = $mdb -> select( 'pp_authors_langs', '*', [ 'id_author' => (int)$id_author ] );
if ( is_array( $results ) ) foreach ( $results as $row )
$author['languages'][$row['id_lang']] = $row;
return $author;
$repo = new \Domain\Authors\AuthorsRepository($mdb);
return $repo->authorDetails($id_author);
}
}

View File

@@ -6,100 +6,49 @@ class Newsletter
public static function emails_import( $emails )
{
global $mdb;
$emails = explode( PHP_EOL, $emails );
if ( is_array( $emails ) ) foreach ( $emails as $email )
{
if ( trim( $email ) and !$mdb -> count( 'pp_newsletter', [ 'email' => trim( $email ) ] ) )
$mdb -> insert( 'pp_newsletter', [
'email' => trim( $email ),
'hash' => md5( $email . time() ),
'status' => 1
] );
}
return true;
$repo = new \Domain\Newsletter\NewsletterRepository($mdb);
return $repo->emailsImport($emails);
}
public static function is_admin_template( $template_id )
{
global $mdb;
return $mdb -> get( 'pp_newsletter_templates', 'is_admin', [ 'id' => (int)$template_id ] );
$repo = new \Domain\Newsletter\NewsletterRepository($mdb);
return $repo->isAdminTemplate($template_id);
}
public static function newsletter_template_delete( $template_id )
{
global $mdb;
return $mdb -> delete( 'pp_newsletter_templates', [ 'id' => (int)$template_id ] );
$repo = new \Domain\Newsletter\NewsletterRepository($mdb);
return $repo->templateDelete($template_id);
}
public static function send( $dates, $template, $only_once )
{
global $mdb;
$results = $mdb -> select( 'pp_newsletter', 'email', [ 'status' => 1 ] );
if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
{
if ( $template and $only_once )
{
if ( !$mdb -> count( 'pp_newsletter_send', [ 'AND' => [ 'id_template' => $template, 'email' => $row ] ] ) )
$mdb -> insert( 'pp_newsletter_send', [
'email' => $row,
'dates' => $dates,
'id_template' => $template ? $template : null,
'only_once' => ( $only_once == 'on' and $template ) ? 1 : 0
] );
}
else
$mdb -> insert( 'pp_newsletter_send', [
'email' => $row,
'dates' => $dates,
'id_template' => $template ? $template : null,
'only_once' => ( $only_once == 'on' and $template ) ? 1 : 0
] );
}
return true;
$repo = new \Domain\Newsletter\NewsletterRepository($mdb);
return $repo->send($dates, $template, $only_once);
}
public static function email_template_detalis ($id_template)
{
global $mdb;
$result = $mdb -> get ('pp_newsletter_templates', '*', [ 'id' => (int)$id_template ] );
return $result;
$repo = new \Domain\Newsletter\NewsletterRepository($mdb);
return $repo->templateDetails($id_template);
}
public static function template_save($id, $name, $text)
{
global $mdb;
if ( !$id )
{
if ( $mdb -> insert( 'pp_newsletter_templates', [
'name' => $name,
'text' => $text
] ) )
{
\S::delete_cache();
return $mdb -> id();
}
}
else
{
$mdb -> update( 'pp_newsletter_templates', [
'name' => $name,
'text' => $text
$repo = new \Domain\Newsletter\NewsletterRepository($mdb);
return $repo->templateSave($id, $name, $text);
}
], [
'id' => (int)$id
] );
\S::delete_cache();
return $id;
}
}
public static function templates_list()
{
global $mdb;
return $mdb -> select( 'pp_newsletter_templates', '*', [ 'is_admin' => 0, 'ORDER' => [ 'name' => 'ASC' ] ] );
$repo = new \Domain\Newsletter\NewsletterRepository($mdb);
return $repo->templatesList();
}
}
}

View File

@@ -1,4 +1,4 @@
<?
<?php
namespace front\factory;
class Authors
{
@@ -6,17 +6,7 @@ class Authors
static public function get_single_author( $id_author )
{
global $mdb;
if ( !$author = \Cache::fetch( "get_single_author:$id_author" ) )
{
$author = $mdb -> get( 'pp_authors', '*', [ 'id' => (int)$id_author ] );
$results = $mdb -> select( 'pp_authors_langs', '*', [ 'id_author' => (int)$id_author ] );
if ( is_array( $results ) ) foreach ( $results as $row )
$author['languages'][$row['id_lang']] = $row;
\Cache::store( "get_single_author:$id_author", $author );
}
return $author;
$repo = new \Domain\Authors\AuthorsRepository($mdb);
return $repo->authorByLang($id_author);
}
}

View File

@@ -6,113 +6,49 @@ class Newsletter
public static function newsletter_unsubscribe( $hash )
{
global $mdb;
return $mdb -> update( 'pp_newsletter', [ 'status' => 0 ], [ 'hash' => $hash ] );
$repo = new \Domain\Newsletter\NewsletterRepository($mdb);
return $repo->unsubscribe($hash);
}
public static function newsletter_confirm( $hash )
{
global $mdb;
if ( !$id = $mdb -> get( 'pp_newsletter', 'id', [ 'AND' => [ 'hash' => $hash, 'status' => 0 ] ] ) )
return false;
else
$mdb -> update( 'pp_newsletter', [ 'status' => 1 ], [ 'id' => $id ] );
return true;
$repo = new \Domain\Newsletter\NewsletterRepository($mdb);
return $repo->confirm($hash);
}
public static function newsletter_send( $limit = 5 )
{
global $mdb, $settings, $lang;
$results = $mdb -> query( 'SELECT * FROM pp_newsletter_send WHERE mailed = 0 ORDER BY id ASC LIMIT ' . $limit ) -> fetchAll();
if ( is_array( $results ) and !empty( $results ) )
{
foreach ( $results as $row )
{
$dates = explode( ' - ', $row['dates'] );
$text = \admin\view\Newsletter::preview(
\admin\factory\Articles::articles_by_date_add( $dates[0], $dates[1] ),
\admin\factory\Settings::settings_details(),
\admin\factory\Newsletter::email_template_detalis($row['id_template'])
);
if ( $settings['ssl'] ) $base = 'https'; else $base = 'http';
$link = $base . "://" . $_SERVER['SERVER_NAME'] . '/newsletter/unsubscribe/hash=' . \front\factory\Newsletter::get_hash( $row['email'] );
$text = str_replace( '[WYPISZ_SIE]', $link, $text );
$regex = "-(<img[^>]+src\s*=\s*['\"])(((?!'|\"|http(|s)://).)*)(['\"][^>]*>)-i";
$text = preg_replace( $regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text );
$regex = "-(<a[^>]+href\s*=\s*['\"])(((?!'|\"|http(|s)://).)*)(['\"][^>]*>)-i";
$text = preg_replace( $regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text );
\S::send_email( $row['email'], 'Newsletter ze strony: ' . $_SERVER['SERVER_NAME'], $text );
if ( $row['only_once'] )
$mdb -> update( 'pp_newsletter_send', [ 'mailed' => 1 ], [ 'id' => $row['id'] ] );
else
$mdb -> delete( 'pp_newsletter_send', [ 'id' => $row['id'] ] );
}
return true;
}
return false;
$repo = new \Domain\Newsletter\NewsletterRepository($mdb);
return $repo->newsletterSend($limit, $settings, $lang);
}
public static function get_hash( $email )
{
global $mdb;
return $mdb -> get( 'pp_newsletter', 'hash', [ 'email' => $email ] );
$repo = new \Domain\Newsletter\NewsletterRepository($mdb);
return $repo->getHash($email);
}
public static function newsletter_signin( $email )
{
global $mdb, $lang, $settings;
if ( !\S::email_check( $email ) )
return false;
if ( !$mdb -> get( 'pp_newsletter', 'id', [ 'email' => $email ] ) )
{
$hash = md5( time() . $email );
$text = $settings['newsletter_header'];
$text .= \front\factory\Newsletter::get_template( '#potwierdzenie-zapisu-do-newslettera' );
$text .= $settings['newsletter_footer_1'];
$settings['ssl'] ? $base = 'https' : $base = 'http';
$regex = "-(<img[^>]+src\s*=\s*['\"])(((?!'|\"|http://).)*)(['\"][^>]*>)-i";
$text = preg_replace( $regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text );
$regex = "-(<a[^>]+href\s*=\s*['\"])(((?!'|\"|http://).)*)(['\"][^>]*>)-i";
$text = preg_replace( $regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text );
$link = '/newsletter/confirm/hash=' . $hash;
$text = str_replace( '[LINK]', $link, $text );
$send = \S::send_email( $email, $lang['potwierdz-zapisanie-sie-do-newslettera'], $text );
$mdb -> insert( 'pp_newsletter', [ 'email' => $email, 'hash' => $hash, 'status' => 0 ] );
return true;
}
return false;
$repo = new \Domain\Newsletter\NewsletterRepository($mdb);
return $repo->signin($email, $settings, $lang);
}
public static function get_template( $template_name )
{
global $mdb;
return $mdb -> get( 'pp_newsletter_templates', 'text', [ 'name' => $template_name ] );
$repo = new \Domain\Newsletter\NewsletterRepository($mdb);
return $repo->getTemplate($template_name);
}
public static function newsletter_signout( $email )
{
global $mdb;
if ( $mdb -> get( 'pp_newsletter', 'id', [ 'email' => $email ] ) )
return $mdb -> delete( 'pp_newsletter', [ 'email' => $email ] );
return false;
$repo = new \Domain\Newsletter\NewsletterRepository($mdb);
return $repo->signout($email);
}
}