ver. 0.280: Articles frontend migration, class.Article removal, Settings facade cleanup
- Add 8 frontend methods to ArticleRepository (with Redis cache) - Create front\Views\Articles (rendering + utility methods) - Rewire front\view\Site::show() and front\controls\Site::route() to repo + Views - Update 5 article templates to use \front\Views\Articles:: - Convert front\factory\Articles and front\view\Articles to facades - Remove class.Article (entity + static methods migrated to repo + Views) - Remove front\factory\Settings facade (already migrated) - Fix: eliminate global $lang from articleNoindex(), inline page sort query - Tests: 450 OK, 1431 assertions (+13 new) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -858,7 +858,342 @@ class ArticleRepository
|
||||
|
||||
if ( is_array( $rows ) ) {
|
||||
foreach ( $rows as $row ) {
|
||||
$articles[] = \front\factory\Articles::article_details( $row['id'], 'pl' );
|
||||
$articles[] = $this->articleDetailsFrontend( $row['id'], 'pl' );
|
||||
}
|
||||
}
|
||||
|
||||
return $articles;
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// FRONTEND METHODS (z Redis cache)
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* Pobiera szczegoly artykulu dla frontendu (z copy_from fallback + Redis cache).
|
||||
*/
|
||||
public function articleDetailsFrontend(int $articleId, string $langId): ?array
|
||||
{
|
||||
$cacheHandler = new \CacheHandler();
|
||||
$cacheKey = "ArticleRepository::articleDetailsFrontend:{$articleId}:{$langId}";
|
||||
|
||||
$objectData = $cacheHandler->get($cacheKey);
|
||||
|
||||
if ($objectData) {
|
||||
return unserialize($objectData);
|
||||
}
|
||||
|
||||
$article = $this->db->get('pp_articles', '*', ['id' => $articleId]);
|
||||
|
||||
if (!$article) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$results = $this->db->select('pp_articles_langs', '*', [
|
||||
'AND' => ['article_id' => $articleId, 'lang_id' => $langId]
|
||||
]);
|
||||
|
||||
if (is_array($results)) {
|
||||
foreach ($results as $row) {
|
||||
if ($row['copy_from']) {
|
||||
$results2 = $this->db->select('pp_articles_langs', '*', [
|
||||
'AND' => ['article_id' => $articleId, 'lang_id' => $row['copy_from']]
|
||||
]);
|
||||
if (is_array($results2)) {
|
||||
foreach ($results2 as $row2) {
|
||||
$article['language'] = $row2;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$article['language'] = $row;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$article['images'] = $this->db->select('pp_articles_images', '*', [
|
||||
'article_id' => $articleId,
|
||||
'ORDER' => ['o' => 'ASC', 'id' => 'DESC']
|
||||
]);
|
||||
|
||||
$article['files'] = $this->db->select('pp_articles_files', '*', [
|
||||
'article_id' => $articleId
|
||||
]);
|
||||
|
||||
$article['pages'] = $this->db->select('pp_articles_pages', 'page_id', [
|
||||
'article_id' => $articleId
|
||||
]);
|
||||
|
||||
$cacheHandler->set($cacheKey, $article);
|
||||
|
||||
return $article;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pobiera ID artykulow ze strony z sortowaniem i paginacja (z Redis cache).
|
||||
*/
|
||||
public function articlesIds(int $pageId, string $langId, int $limit, int $sortType, int $from): ?array
|
||||
{
|
||||
$output = null;
|
||||
|
||||
switch ($sortType) {
|
||||
case 0: $order = 'date_add ASC'; break;
|
||||
case 1: $order = 'date_add DESC'; break;
|
||||
case 2: $order = 'date_modify ASC'; break;
|
||||
case 3: $order = 'date_modify DESC'; break;
|
||||
case 4: $order = 'o ASC'; break;
|
||||
case 5: $order = 'title ASC'; break;
|
||||
case 6: $order = 'title DESC'; break;
|
||||
default: $order = 'id ASC'; break;
|
||||
}
|
||||
|
||||
$cacheHandler = new \CacheHandler();
|
||||
$cacheKey = "ArticleRepository::articlesIds:{$pageId}:{$langId}:{$limit}:{$sortType}:{$from}:{$order}";
|
||||
|
||||
$objectData = $cacheHandler->get($cacheKey);
|
||||
|
||||
if ($objectData) {
|
||||
return unserialize($objectData);
|
||||
}
|
||||
|
||||
$results = $this->db->query(
|
||||
'SELECT * FROM ( '
|
||||
. 'SELECT '
|
||||
. 'a.id, date_modify, date_add, o, '
|
||||
. '( CASE '
|
||||
. 'WHEN copy_from IS NULL THEN title '
|
||||
. 'WHEN copy_from IS NOT NULL THEN ( '
|
||||
. 'SELECT title FROM pp_articles_langs '
|
||||
. 'WHERE lang_id = al.copy_from AND article_id = a.id '
|
||||
. ') '
|
||||
. 'END ) AS title '
|
||||
. 'FROM '
|
||||
. 'pp_articles_pages AS ap '
|
||||
. 'INNER JOIN pp_articles AS a ON a.id = ap.article_id '
|
||||
. 'INNER JOIN pp_articles_langs AS al ON al.article_id = ap.article_id '
|
||||
. 'WHERE '
|
||||
. 'status = 1 AND page_id = ' . (int)$pageId . ' AND lang_id = \'' . $langId . '\' '
|
||||
. ') AS q1 '
|
||||
. 'WHERE q1.title IS NOT NULL '
|
||||
. 'ORDER BY q1.' . $order . ' '
|
||||
. 'LIMIT ' . (int)$from . ',' . (int)$limit
|
||||
)->fetchAll();
|
||||
|
||||
if (is_array($results) && !empty($results)) {
|
||||
foreach ($results as $row) {
|
||||
$output[] = $row['id'];
|
||||
}
|
||||
}
|
||||
|
||||
$cacheHandler->set($cacheKey, $output);
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zlicza artykuly na stronie (z Redis cache).
|
||||
*/
|
||||
public function pageArticlesCount(int $pageId, string $langId): int
|
||||
{
|
||||
$cacheHandler = new \CacheHandler();
|
||||
$cacheKey = "ArticleRepository::pageArticlesCount:{$pageId}:{$langId}";
|
||||
|
||||
$objectData = $cacheHandler->get($cacheKey);
|
||||
|
||||
if ($objectData) {
|
||||
return (int)unserialize($objectData);
|
||||
}
|
||||
|
||||
$results = $this->db->query(
|
||||
'SELECT COUNT(0) FROM ( '
|
||||
. 'SELECT '
|
||||
. 'a.id, '
|
||||
. '( CASE '
|
||||
. 'WHEN copy_from IS NULL THEN title '
|
||||
. 'WHEN copy_from IS NOT NULL THEN ( '
|
||||
. 'SELECT title FROM pp_articles_langs '
|
||||
. 'WHERE lang_id = al.copy_from AND article_id = a.id '
|
||||
. ') '
|
||||
. 'END ) AS title '
|
||||
. 'FROM '
|
||||
. 'pp_articles_pages AS ap '
|
||||
. 'INNER JOIN pp_articles AS a ON a.id = ap.article_id '
|
||||
. 'INNER JOIN pp_articles_langs AS al ON al.article_id = ap.article_id '
|
||||
. 'WHERE '
|
||||
. 'status = 1 AND page_id = ' . (int)$pageId . ' AND lang_id = \'' . $langId . '\' '
|
||||
. ') AS q1 '
|
||||
. 'WHERE q1.title IS NOT NULL'
|
||||
)->fetchAll();
|
||||
|
||||
$count = isset($results[0][0]) ? (int)$results[0][0] : 0;
|
||||
|
||||
$cacheHandler->set($cacheKey, $count);
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pobiera paginowane artykuly ze strony.
|
||||
*
|
||||
* @return array{articles: ?array, ls: int}
|
||||
*/
|
||||
public function pageArticles(array $page, string $langId, int $bs): array
|
||||
{
|
||||
$count = $this->pageArticlesCount((int)$page['id'], $langId);
|
||||
$articlesLimit = (int)($page['articles_limit'] ?: 10);
|
||||
$ls = (int)ceil($count / $articlesLimit);
|
||||
|
||||
if ($bs < 1) {
|
||||
$bs = 1;
|
||||
} elseif ($bs > $ls) {
|
||||
$bs = $ls;
|
||||
}
|
||||
|
||||
$from = $articlesLimit * ($bs - 1);
|
||||
|
||||
if ($from < 0) {
|
||||
$from = 0;
|
||||
}
|
||||
|
||||
return [
|
||||
'articles' => $this->articlesIds((int)$page['id'], $langId, $articlesLimit, (int)($page['sort_type'] ?? 0), $from),
|
||||
'ls' => $ls,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Pobiera artykuly-aktualnosci ze strony (z sortowaniem wg page_sort).
|
||||
*/
|
||||
public function news(int $pageId, int $limit, string $langId): ?array
|
||||
{
|
||||
$sort = (int)$this->db->get('pp_pages', 'sort_type', ['id' => $pageId]);
|
||||
|
||||
$articlesIds = $this->articlesIds($pageId, $langId, $limit, $sort, 0);
|
||||
|
||||
$articles = null;
|
||||
if (is_array($articlesIds) && !empty($articlesIds)) {
|
||||
foreach ($articlesIds as $articleId) {
|
||||
$articles[] = $this->articleDetailsFrontend($articleId, $langId);
|
||||
}
|
||||
}
|
||||
|
||||
return $articles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sprawdza czy artykul ma flage noindex (z Redis cache).
|
||||
*/
|
||||
public function articleNoindex(int $articleId, string $langId): bool
|
||||
{
|
||||
$cacheHandler = new \CacheHandler();
|
||||
$cacheKey = "ArticleRepository::articleNoindex:{$articleId}:{$langId}";
|
||||
|
||||
$objectData = $cacheHandler->get($cacheKey);
|
||||
|
||||
if ($objectData) {
|
||||
return (bool)unserialize($objectData);
|
||||
}
|
||||
|
||||
$noindex = $this->db->get('pp_articles_langs', 'noindex', [
|
||||
'AND' => ['article_id' => $articleId, 'lang_id' => $langId]
|
||||
]);
|
||||
|
||||
$cacheHandler->set($cacheKey, $noindex);
|
||||
|
||||
return (bool)$noindex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pobiera najpopularniejsze artykuly ze strony (wg views DESC, z Redis cache).
|
||||
*/
|
||||
public function topArticles(int $pageId, int $limit, string $langId): ?array
|
||||
{
|
||||
$cacheHandler = new \CacheHandler();
|
||||
$cacheKey = "ArticleRepository::topArticles:{$pageId}:{$limit}:{$langId}";
|
||||
|
||||
$objectData = $cacheHandler->get($cacheKey);
|
||||
|
||||
if (!$objectData) {
|
||||
$articlesData = $this->db->query(
|
||||
'SELECT * FROM ( '
|
||||
. 'SELECT '
|
||||
. 'a.id, date_add, views, '
|
||||
. '( CASE '
|
||||
. 'WHEN copy_from IS NULL THEN title '
|
||||
. 'WHEN copy_from IS NOT NULL THEN ( '
|
||||
. 'SELECT title FROM pp_articles_langs '
|
||||
. 'WHERE lang_id = al.copy_from AND article_id = a.id '
|
||||
. ') '
|
||||
. 'END ) AS title '
|
||||
. 'FROM '
|
||||
. 'pp_articles_pages AS ap '
|
||||
. 'INNER JOIN pp_articles AS a ON a.id = ap.article_id '
|
||||
. 'INNER JOIN pp_articles_langs AS al ON al.article_id = ap.article_id '
|
||||
. 'WHERE '
|
||||
. 'status = 1 AND page_id = ' . (int)$pageId . ' AND lang_id = \'' . $langId . '\' '
|
||||
. ') AS q1 '
|
||||
. 'WHERE q1.title IS NOT NULL '
|
||||
. 'ORDER BY q1.views DESC '
|
||||
. 'LIMIT 0, ' . (int)$limit
|
||||
)->fetchAll(\PDO::FETCH_ASSOC);
|
||||
|
||||
$cacheHandler->set($cacheKey, $articlesData);
|
||||
} else {
|
||||
$articlesData = unserialize($objectData);
|
||||
}
|
||||
|
||||
$articles = null;
|
||||
if (\S::is_array_fix($articlesData)) {
|
||||
foreach ($articlesData as $row) {
|
||||
$articles[] = $this->articleDetailsFrontend((int)$row['id'], $langId);
|
||||
}
|
||||
}
|
||||
|
||||
return $articles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pobiera najnowsze artykuly ze strony (wg date_add DESC, z Redis cache).
|
||||
*/
|
||||
public function newsListArticles(int $pageId, int $limit, string $langId): ?array
|
||||
{
|
||||
$cacheHandler = new \CacheHandler();
|
||||
$cacheKey = "ArticleRepository::newsListArticles:{$pageId}:{$limit}:{$langId}";
|
||||
|
||||
$objectData = $cacheHandler->get($cacheKey);
|
||||
|
||||
if (!$objectData) {
|
||||
$articlesData = $this->db->query(
|
||||
'SELECT * FROM ( '
|
||||
. 'SELECT '
|
||||
. 'a.id, date_add, '
|
||||
. '( CASE '
|
||||
. 'WHEN copy_from IS NULL THEN title '
|
||||
. 'WHEN copy_from IS NOT NULL THEN ( '
|
||||
. 'SELECT title FROM pp_articles_langs '
|
||||
. 'WHERE lang_id = al.copy_from AND article_id = a.id '
|
||||
. ') '
|
||||
. 'END ) AS title '
|
||||
. 'FROM '
|
||||
. 'pp_articles_pages AS ap '
|
||||
. 'INNER JOIN pp_articles AS a ON a.id = ap.article_id '
|
||||
. 'INNER JOIN pp_articles_langs AS al ON al.article_id = ap.article_id '
|
||||
. 'WHERE '
|
||||
. 'status = 1 AND page_id = ' . (int)$pageId . ' AND lang_id = \'' . $langId . '\' '
|
||||
. ') AS q1 '
|
||||
. 'WHERE q1.title IS NOT NULL '
|
||||
. 'ORDER BY q1.date_add DESC '
|
||||
. 'LIMIT 0, ' . (int)$limit
|
||||
)->fetchAll(\PDO::FETCH_ASSOC);
|
||||
|
||||
$cacheHandler->set($cacheKey, $articlesData);
|
||||
} else {
|
||||
$articlesData = unserialize($objectData);
|
||||
}
|
||||
|
||||
$articles = null;
|
||||
if (\S::is_array_fix($articlesData)) {
|
||||
foreach ($articlesData as $row) {
|
||||
$articles[] = $this->articleDetailsFrontend((int)$row['id'], $langId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1524,7 +1524,7 @@ class ProductRepository
|
||||
{
|
||||
global $lang_id;
|
||||
|
||||
$settings = \front\factory\Settings::settings_details( true );
|
||||
$settings = ( new \Domain\Settings\SettingsRepository( $this->db ) )->allSettings( true );
|
||||
|
||||
$domainPrefix = 'https';
|
||||
$url = preg_replace( '#^(http(s)?://)?w{3}\.#', '$1', $_SERVER['SERVER_NAME'] );
|
||||
|
||||
Reference in New Issue
Block a user