Files
cmsPRO/autoload/Domain/Authors/AuthorsRepository.php
Jacek Pyziak ffe661b4d2 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>
2026-04-04 18:21:32 +02:00

157 lines
3.8 KiB
PHP

<?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;
}
}