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:
2026-02-16 15:52:03 +01:00
parent 3b32ea0b9b
commit 723cb1a5eb
35 changed files with 1070 additions and 642 deletions

View File

@@ -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);
}
}

View File

@@ -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'] );

View File

@@ -1,217 +0,0 @@
<?php
class Article implements \ArrayAccess
{
public function __construct( $article_id, $lang_id )
{
global $mdb;
$result = $mdb -> get( 'pp_articles', '*', [ 'id' => (int)$article_id ] );
if ( \S::is_array_fix( $result ) ) foreach ( $result as $key => $val )
$this -> $key = $val;
$results = $mdb -> select( 'pp_articles_langs', '*', [ 'AND' => [ 'article_id' => (int)$article_id, 'lang_id' => $lang_id ] ] );
if ( is_array( $results ) ) foreach ( $results as $row )
{
if ( $row['copy_from'] )
{
$results2 = $mdb -> select( 'pp_articles_langs', '*', [ 'AND' => [ 'article_id' => (int)$article_id, 'lang_id' => $row['copy_from'] ] ] );
if ( is_array( $results2 ) ) foreach ( $results2 as $row2 )
$this -> language = $row2;
}
else
$this -> language = $row;
preg_match_all( \front\view\Site::container_pattern, $this -> language['entry'], $container_list );
if ( is_array( $container_list[0] ) ) foreach( $container_list[0] as $container_list_tmp )
{
$container_list_tmp = explode( ':', $container_list_tmp );
$this -> language['entry'] = str_replace( '[KONTENER:' . $container_list_tmp[1] . ']', \front\view\Scontainers::scontainer( $container_list_tmp[1] ), $this -> language['entry'] );
}
preg_match_all( \front\view\Site::container_pattern, $this -> language['text'], $container_list );
if ( is_array( $container_list[0] ) ) foreach( $container_list[0] as $container_list_tmp )
{
$container_list_tmp = explode( ':', $container_list_tmp );
$this -> language['text'] = str_replace( '[KONTENER:' . $container_list_tmp[1] . ']', \front\view\Scontainers::scontainer( $container_list_tmp[1] ), $this -> language['text'] );
}
}
$this -> images = $mdb -> select( 'pp_articles_images', '*', [ 'article_id' => (int)$article_id, 'ORDER' => [ 'o' => 'ASC', 'id' => 'ASC' ] ] );
$this -> files = $mdb -> select( 'pp_articles_files', '*', [ 'article_id' => (int)$article_id ] );
$this -> pages = $mdb -> select( 'pp_articles_pages', 'page_id', [ 'article_id' => (int)$article_id ] );
if ( is_array( $results ) ) foreach ( $results as $row )
{
if ( !$row['language_id'] )
$params[ $row['name'] ] = $row['value'];
else
$params[ $row['name'] ][$row['language_id']] = $row['value'];
}
$this -> params = $params;
}
static public function get_from_cache( $article_id, $lang_id )
{
$cacheHandler = new \CacheHandler();
$cacheKey = "\Article::get_from_cache:$article_id:$lang_id";
$objectData = $cacheHandler -> get( $cacheKey );
if ( !$objectData )
{
$article = new \Article( $article_id, $lang_id );
$cacheHandler -> set( $cacheKey, $article );
}
else
{
return unserialize( $objectData );
}
return $article;
}
public function updateView()
{
global $mdb;
$mdb -> update( 'pp_articles', [ 'views[+]' => 1 ], [ 'id' => $this -> id ] );
}
static public function newsList( $articles )
{
return \Tpl::view( 'articles/news-list', [
'articles' => $articles
] );
}
// pobierz najczęściej wyświtlane artykuły
static public function getTopNews( $page_id, $limit = 6, $lang_id )
{
global $mdb;
$cacheHandler = new \CacheHandler();
$cacheKey = "\Article::getTopNews:$page_id:$limit:$lang_id";
$objectData = $cacheHandler -> get( $cacheKey );
if ( !$objectData )
{
$articles_id = $mdb -> 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)$page_id . ' AND lang_id = \'' . $lang_id . '\' '
. ') AS q1 '
. 'WHERE '
. 'q1.title IS NOT NULL '
. 'ORDER BY '
. 'q1.views DESC '
. 'LIMIT '
. '0, ' . (int)$limit ) -> fetchAll( \PDO::FETCH_ASSOC );
$cacheHandler -> set( $cacheKey, $articles_id );
}
else
{
$articles_id = unserialize( $objectData );
}
if ( \S::is_array_fix( $articles_id ) ) foreach ( $articles_id as $article_tmp )
{
$article = \Article::get_from_cache( $article_tmp['id'], $lang_id );
$articles[] = $article;
}
return $articles;
}
static public function getNews( $page_id, $limit = 6, $lang_id )
{
global $mdb;
if ( !$articles_id = \Cache::fetch( "getNews:$page_id:$limit:$lang_id" ) )
{
$articles_id = $mdb -> 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)$page_id . ' AND lang_id = \'' . $lang_id . '\' '
. ') AS q1 '
. 'WHERE '
. 'q1.title IS NOT NULL '
. 'ORDER BY '
. 'q1.date_add DESC '
. 'LIMIT '
. '0, ' . (int)$limit ) -> fetchAll( \PDO::FETCH_ASSOC );
\Cache::store( "getNews:$page_id:$limit:$lang_id", $articles_id );
}
if ( \S::is_array_fix( $articles_id ) ) foreach ( $articles_id as $article_tmp )
{
$article = \Article::get_from_cache( $article_tmp['id'], $lang_id );
$articles[] = $article;
}
return $articles;
}
public function __get( $variable )
{
if ( array_key_exists( $variable, $this -> data ) )
return $this -> $variable;
}
public function __set( $variable, $value )
{
$this -> $variable = $value;
}
public function offsetExists( $offset )
{
return isset( $this -> $offset );
}
public function offsetGet( $offset )
{
return $this -> $offset;
}
public function offsetSet( $offset, $value )
{
$this -> $offset = $value;
}
public function offsetUnset( $offset )
{
unset( $this -> $offset );
}
}

View File

@@ -573,7 +573,7 @@ class S
{
global $mdb;
$settings = \front\factory\Settings::settings_details( true );
$settings = ( new \Domain\Settings\SettingsRepository( $mdb ) )->allSettings( true );
$url = preg_replace( '#^(http(s)?://)?w{3}\.#', '$1', $_SERVER['SERVER_NAME'] );

View File

@@ -0,0 +1,241 @@
<?php
namespace front\Views;
class Articles
{
/**
* Renderuje pelny widok pojedynczego artykulu.
*/
public static function fullArticle( $article )
{
$tpl = new \Tpl;
$tpl->article = $article;
return $tpl->render( 'articles/article' );
}
/**
* Renderuje liste artykulow w trybie miniaturek z pagerem.
*/
public static function miniatureArticlesList( $articles, $ls, $bs, $page )
{
$out = '';
if ( is_array( $articles ) ) foreach ( $articles as $article )
{
$tpl = new \Tpl;
$tpl->article = $article;
$out .= $tpl->render( 'articles/article-miniature' );
}
if ( $ls > 1 )
{
$tpl = new \Tpl;
$tpl->ls = $ls;
$tpl->bs = $bs ? $bs : 1;
$tpl->page = $page;
$out .= $tpl->render( 'site/pager' );
}
return $out;
}
/**
* Renderuje liste artykulow w trybie wprowadzen z pagerem.
*/
public static function entryArticlesList( $articles, $ls, $bs, $page )
{
$tpl = new \Tpl;
$tpl->page_id = $page['id'];
$tpl->articles = $articles;
$out = $tpl->render( 'articles/articles-entries' );
if ( $ls > 1 )
{
$tpl = new \Tpl;
$tpl->ls = $ls;
$tpl->bs = $bs ? $bs : 1;
$tpl->page = $page;
$out .= $tpl->render( 'site/pager' );
}
return $out;
}
/**
* Renderuje liste pelnych artykulow z pagerem.
*/
public static function fullArticlesList( $articles, $ls, $bs, $page )
{
$out = '';
if ( is_array( $articles ) ) foreach ( $articles as $article )
{
$tpl = new \Tpl;
$tpl->article = $article;
$out .= $tpl->render( 'articles/article-full' );
}
if ( $ls > 1 )
{
$tpl = new \Tpl;
$tpl->ls = $ls;
$tpl->bs = $bs ? $bs : 1;
$tpl->page = $page;
$out .= $tpl->render( 'site/pager' );
}
return $out;
}
/**
* Renderuje box z aktualnosciami.
*/
public static function news( $page_id, $articles )
{
$tpl = new \Tpl;
$tpl->page_id = $page_id;
$tpl->articles = $articles;
return $tpl->render( 'articles/news' );
}
/**
* Renderuje prosta liste artykulow (news-list).
*/
public static function newsList( $articles )
{
return \Tpl::view( 'articles/news-list', [
'articles' => $articles
] );
}
// =========================================================================
// UTILITY (czyste transformacje HTML, brak DB)
// =========================================================================
/**
* Generuje spis tresci z naglowkow HTML.
*/
public static function generateTableOfContents( $content )
{
$result = '';
$currentLevel = [];
preg_match_all( '/<(h[1-6])([^>]*)>(.*?)<\/\1>/', $content, $matches, PREG_SET_ORDER );
$firstLevel = true;
foreach ( $matches as $match )
{
$level = intval( substr( $match[1], 1 ) );
while ( $level < count( $currentLevel ) )
{
$result .= '</li></ol>';
array_pop( $currentLevel );
}
if ( $level > count( $currentLevel ) )
{
while ( $level > count( $currentLevel ) )
{
if ( count( $currentLevel ) > 0 || $firstLevel )
{
$result .= '<ol>';
$firstLevel = false;
}
array_push( $currentLevel, 0 );
}
$result .= '<li>';
}
else
{
$result .= '</li><li>';
}
$currentLevel[ count( $currentLevel ) - 1 ]++;
preg_match( '/\sid="([^"]*)"/', $match[2], $idMatches );
$id = isset( $idMatches[1] ) ? $idMatches[1] : '';
$result .= sprintf(
'<a href="#%s">%s</a>',
urlencode( strtolower( $id ) ),
$match[3]
);
}
while ( !empty( $currentLevel ) )
{
$result .= '</li></ol>';
array_pop( $currentLevel );
}
if ( substr( $result, 0, 8 ) === '<ol><ol>' )
return substr( $result, 4, -5 );
else
return $result;
}
/**
* Callback dla processHeaders.
*/
public static function processHeaders( $matches )
{
$level = $matches[1];
$attrs = $matches[2];
$content = $matches[3];
$id_attr = 'id=';
$id_attr_pos = strpos( $attrs, $id_attr );
if ( $id_attr_pos === false )
{
$id = \S::seo( $content );
$attrs .= sprintf( ' id="%s"', $id );
}
return sprintf( '<h%d%s>%s</h%d>', $level, $attrs, $content, $level );
}
/**
* Dodaje atrybuty id do naglowkow HTML.
*/
public static function generateHeadersIds( $text )
{
$pattern = '/<h([1-6])(.*?)>(.*?)<\/h\1>/si';
$text = preg_replace_callback( $pattern, array( __CLASS__, 'processHeaders' ), $text );
return $text;
}
/**
* Pobiera glowne zdjecie artykulu (z entry, text lub galerii).
*/
public static function getImage( $article )
{
if ( $main_img = $article['language']['main_image'] )
return $main_img;
$dom = new \DOMDocument();
@$dom->loadHTML( mb_convert_encoding( $article['language']['entry'], 'HTML-ENTITIES', 'UTF-8' ) );
$images = $dom->getElementsByTagName( 'img' );
foreach ( $images as $img )
{
$src = $img->getAttribute( 'src' );
if ( file_exists( substr( $src, 1, strlen( $src ) ) ) )
return $src;
}
$dom = new \DOMDocument();
@$dom->loadHTML( mb_convert_encoding( $article['language']['text'], 'HTML-ENTITIES', 'UTF-8' ) );
$images = $dom->getElementsByTagName( 'img' );
foreach ( $images as $img )
{
$src = $img->getAttribute( 'src' );
if ( file_exists( substr( $src, 1, strlen( $src ) ) ) )
return $src;
}
if ( $article['images'] )
return $article['images'][0]['src'];
return false;
}
}

View File

@@ -35,8 +35,10 @@ class Site
{
global $page, $lang_id, $settings;
$articleRepo = new \Domain\Article\ArticleRepository( $GLOBALS['mdb'] );
if ( \S::get( 'article' ) )
return \front\view\Articles::full_article( \S::get( 'article' ), $lang_id );
return \front\Views\Articles::fullArticle( $articleRepo->articleDetailsFrontend( (int)\S::get( 'article' ), $lang_id ) );
// wyświetlenie pojedynczego produktu
if ( $product )
@@ -86,26 +88,35 @@ class Site
if ( $page['id'] )
{
$bs = (int)\S::get( 'bs' );
$pageArticlesResult = $articleRepo->pageArticles( $page, $lang_id, $bs ?: 1 );
$articlesForPage = [];
if ( is_array( $pageArticlesResult['articles'] ) ) {
foreach ( $pageArticlesResult['articles'] as $aid ) {
$articlesForPage[] = $articleRepo->articleDetailsFrontend( (int)$aid, $lang_id );
}
}
switch ( $page['page_type'] )
{
/* pełne artykuły */
case 0:
return \front\view\Articles::full_articles_list( $page, $lang_id, \S::get( 'bs' ) );
return \front\Views\Articles::fullArticlesList( $articlesForPage, $pageArticlesResult['ls'], $bs ?: 1, $page );
break;
/* wprowadzenia */
case 1:
return \front\view\Articles::entry_articles_list( $page, $lang_id, \S::get( 'bs' ) );
return \front\Views\Articles::entryArticlesList( $articlesForPage, $pageArticlesResult['ls'], $bs ?: 1, $page );
break;
/* miniaturki */
case 2:
return \front\view\Articles::miniature_articles_list( $page, $lang_id, \S::get( 'bs' ) );
return \front\Views\Articles::miniatureArticlesList( $articlesForPage, $pageArticlesResult['ls'], $bs ?: 1, $page );
break;
/* strona kontaktu */
case 4:
$out = \front\view\Articles::full_articles_list( $page, $lang_id, \S::get( 'bs' ) );
$out = \front\Views\Articles::fullArticlesList( $articlesForPage, $pageArticlesResult['ls'], $bs ?: 1, $page );
$out .= \front\view\Site::contact();
return $out;
break;

View File

@@ -1,317 +1,66 @@
<?php
namespace front\factory;
/**
* FASADA — deleguje do \Domain\Article\ArticleRepository i \front\Views\Articles
* Zachowana dla wstecznej kompatybilności. Nowy kod powinien używać bezpośrednio repo + Views.
*/
class Articles
{
static public function generateTableOfContents($content) {
$result = '';
$currentLevel = [];
preg_match_all('/<(h[1-6])([^>]*)>(.*?)<\/\1>/', $content, $matches, PREG_SET_ORDER);
$firstLevel = true;
foreach ($matches as $match) {
$level = intval(substr($match[1], 1));
while ($level < count($currentLevel)) {
$result .= '</li></ol>';
array_pop($currentLevel);
}
if ($level > count($currentLevel)) {
while ($level > count($currentLevel)) {
if (count($currentLevel) > 0 || $firstLevel) {
$result .= '<ol>';
$firstLevel = false;
}
array_push($currentLevel, 0);
}
$result .= '<li>';
} else {
$result .= '</li><li>';
}
$currentLevel[count($currentLevel) - 1]++;
preg_match('/\sid="([^"]*)"/', $match[2], $idMatches);
$id = isset($idMatches[1]) ? $idMatches[1] : '';
$result .= sprintf(
'<a href="#%s">%s</a>',
urlencode(strtolower($id)),
$match[3]
);
}
while (!empty($currentLevel)) {
$result .= '</li></ol>';
array_pop($currentLevel);
}
if (substr($result, 0, 8) === '<ol><ol>') {
return substr($result, 4, -5);
} else {
return $result;
}
static public function generateTableOfContents( $content )
{
return \front\Views\Articles::generateTableOfContents( $content );
}
// funkcja wywoływana dla każdego dopasowania do wyrażenia regularnego
static public function processHeaders( $matches )
{
$level = $matches[1];
$attrs = $matches[2];
$content = $matches[3];
$id_attr = 'id=';
$id_attr_pos = strpos($attrs, $id_attr);
if ($id_attr_pos === false) { // jeśli nie ma atrybutu id
$id = \S::seo( $content );
$attrs .= sprintf(' id="%s"', $id);
}
$html = sprintf( '<h%d%s>%s</h%d>', $level, $attrs, $content, $level );
return $html;
return \front\Views\Articles::processHeaders( $matches );
}
static public function generateHeadersIds( $text )
{
$pattern = '/<h([1-6])(.*?)>(.*?)<\/h\1>/si';
$text = preg_replace_callback( $pattern, array(__CLASS__, 'processHeaders'), $text );
return $text;
}
public static function news( $page_id, $limit = 6, $lang_id )
{
$sort = \front\factory\Pages::page_sort( $page_id );
$articles_id = \front\factory\Articles::artciles_id( (int)$page_id, $lang_id, $limit, $sort, 0 );
if ( is_array( $articles_id ) and !empty( $articles_id ) ) foreach ( $articles_id as $article_id )
$articles[] = \front\factory\Articles::article_details( $article_id, $lang_id );
return $articles;
return \front\Views\Articles::generateHeadersIds( $text );
}
public static function get_image( $article )
{
if ( $main_img = $article['language']['main_image'] )
return $main_img;
$dom = new \DOMDocument();
$dom -> loadHTML( mb_convert_encoding( $article['language']['entry'], 'HTML-ENTITIES', "UTF-8" ) );
$images = $dom -> getElementsByTagName( 'img' );
foreach ( $images as $img )
{
$src = $img -> getAttribute( 'src' );
if ( file_exists( substr( $src, 1, strlen( $src ) ) ) )
return $src;
}
$dom = new \DOMDocument();
$dom -> loadHTML( mb_convert_encoding( $article['language']['text'], 'HTML-ENTITIES', "UTF-8" ) );
$images = $dom -> getElementsByTagName( 'img' );
foreach ( $images as $img )
{
$src = $img -> getAttribute( 'src' );
if ( file_exists( substr( $src, 1, strlen( $src ) ) ) )
return $src;
}
if ( $article['images'] )
return $article['images'][0]['src'];
return false;
return \front\Views\Articles::getImage( $article );
}
public static function article_noindex( $article_id )
{
global $mdb, $lang;
$cacheHandler = new \CacheHandler();
$cacheKey = "\front\factory\Articles::article_noindex:$article_id";
$objectData = $cacheHandler -> get( $cacheKey );
if ( !$objectData )
{
$noindex = $mdb -> get( 'pp_articles_langs', 'noindex', [ 'AND' => [ 'article_id' => (int)$article_id, 'lang_id' => $lang[0] ] ] );
$cacheHandler -> set( $cacheKey, $noindex );
}
else
{
return unserialize( $objectData );
}
return $noindex;
}
public static function page_articles( $page, $lang_id, $bs )
{
$count = \front\factory\Articles::page_articles_count( $page['id'], $lang_id );
$ls = ceil( $count / $page['articles_limit'] );
if ( $bs < 1 )
$bs = 1;
else if ( $bs > $ls )
$bs = $ls;
$from = $page['articles_limit'] * ( $bs - 1 );
if ( $from < 0 )
$from = 0;
$results['articles'] = \front\factory\Articles::artciles_id( (int)$page['id'], $lang_id, (int)$page['articles_limit'], $page['sort_type'], $from );
$results['ls'] = $ls;
return $results;
global $lang;
$repo = new \Domain\Article\ArticleRepository( $GLOBALS['mdb'] );
return $repo->articleNoindex( (int)$article_id, $lang[0] );
}
public static function article_details( $article_id, $lang_id )
{
global $mdb;
$cacheHandler = new \CacheHandler();
$cacheKey = "\front\factory\Articles::article_details:$article_id:$lang_id";
$objectData = $cacheHandler->get($cacheKey);
if ( !$objectData )
{
$article = $mdb -> get( 'pp_articles', '*', [ 'id' => (int)$article_id ] );
$results = $mdb -> select( 'pp_articles_langs', '*', [ 'AND' => [ 'article_id' => (int)$article_id, 'lang_id' => $lang_id ] ] );
if ( is_array( $results ) ) foreach ( $results as $row )
{
if ( $row['copy_from'] )
{
$results2 = $mdb -> select( 'pp_articles_langs', '*', [ 'AND' => [ 'article_id' => (int)$article_id, 'lang_id' => $row['copy_from'] ] ] );
if ( is_array( $results2 ) ) foreach ( $results2 as $row2 )
$article['language'] = $row2;
}
else
$article['language'] = $row;
}
$article['images'] = $mdb -> select( 'pp_articles_images', '*', [ 'article_id' => (int)$article_id, 'ORDER' => [ 'o' => 'ASC', 'id' => 'DESC' ] ] );
$article['files'] = $mdb -> select( 'pp_articles_files', '*', [ 'article_id' => (int)$article_id ] );
$article['pages'] = $mdb -> select( 'pp_articles_pages', 'page_id', [ 'article_id' => (int)$article_id ] );
$cacheHandler -> set( $cacheKey, $article );
}
else
{
return unserialize( $objectData );
}
return $article;
$repo = new \Domain\Article\ArticleRepository( $GLOBALS['mdb'] );
return $repo->articleDetailsFrontend( (int)$article_id, $lang_id );
}
public static function artciles_id( $page_id, $lang_id, $articles_limit, $sort_type, $from )
{
global $mdb;
switch ( $sort_type )
{
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 = "\front\factory\Artiles::artciles_id:$page_id:$lang_id:$articles_limit:$sort_type:$from:$order";
$objectData = $cacheHandler->get($cacheKey);
if ( !$objectData )
{
$results = $mdb -> 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)$page_id . ' AND lang_id = \'' . $lang_id . '\' '
. ') AS q1 '
. 'WHERE '
. 'q1.title IS NOT NULL '
. 'ORDER BY '
. 'q1.' . $order . ' '
. 'LIMIT '
. (int)$from . ',' . (int)$articles_limit ) -> fetchAll();
if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
$output[] = $row['id'];
$cacheHandler -> set( $cacheKey, $output );
}
else
{
return unserialize($objectData);
}
return $output;
$repo = new \Domain\Article\ArticleRepository( $GLOBALS['mdb'] );
return $repo->articlesIds( (int)$page_id, $lang_id, (int)$articles_limit, (int)$sort_type, (int)$from );
}
public static function page_articles_count( $page_id, $lang_id )
{
global $mdb;
$repo = new \Domain\Article\ArticleRepository( $GLOBALS['mdb'] );
return $repo->pageArticlesCount( (int)$page_id, $lang_id );
}
$cacheHandler = new \CacheHandler();
$cacheKey = "\front\factory\Articles::page_articles_count:$page_id:$lang_id";
public static function page_articles( $page, $lang_id, $bs )
{
$repo = new \Domain\Article\ArticleRepository( $GLOBALS['mdb'] );
return $repo->pageArticles( $page, $lang_id, (int)$bs );
}
$objectData = $cacheHandler -> get( $cacheKey );
if ( !$objectData )
{
$results = $mdb -> 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)$page_id . ' AND lang_id = \'' . $lang_id . '\' '
. ') AS q1 '
. 'WHERE '
. 'q1.title IS NOT NULL' ) -> fetchAll();
$articles_count = $results[0][0];
$cacheHandler -> set( $cacheKey, $articles_count );
}
else
{
return unserialize( $objectData );
}
return $articles_count;
public static function news( $page_id, $limit = 6, $lang_id )
{
$repo = new \Domain\Article\ArticleRepository( $GLOBALS['mdb'] );
return $repo->news( (int)$page_id, (int)$limit, $lang_id );
}
}

View File

@@ -1,22 +0,0 @@
<?php
namespace front\factory;
/**
* Fasada delegujaca do Domain\Settings\SettingsRepository.
*/
class Settings
{
public static function settings_details($admin = false)
{
global $mdb;
$repo = new \Domain\Settings\SettingsRepository($mdb);
return $repo->allSettings($admin);
}
public static function get_single_settings_value($param)
{
global $mdb;
$repo = new \Domain\Settings\SettingsRepository($mdb);
return $repo->getSingleValue($param);
}
}

View File

@@ -1,90 +1,57 @@
<?php
namespace front\view;
/**
* FASADA — deleguje do \Domain\Article\ArticleRepository i \front\Views\Articles
* Zachowana dla wstecznej kompatybilności. Nowy kod powinien używać bezpośrednio repo + Views.
*/
class Articles
{
public static function news( $page_id, $articles )
{
$tpl = new \Tpl;
$tpl -> page_id = $page_id;
$tpl -> articles = $articles;
return $tpl -> render( 'articles/news' );
return \front\Views\Articles::news( $page_id, $articles );
}
public static function full_article( $article_id, $lang_id )
{
$tpl = new \Tpl;
$tpl -> article = \front\factory\Articles::article_details( $article_id, $lang_id );
return $tpl -> render( 'articles/article' );
$repo = new \Domain\Article\ArticleRepository( $GLOBALS['mdb'] );
$article = $repo->articleDetailsFrontend( (int)$article_id, $lang_id );
return \front\Views\Articles::fullArticle( $article );
}
public static function miniature_articles_list( $page, $lang_id, $bs = 1 )
{
$results = \front\factory\Articles::page_articles( $page, $lang_id, $bs );
if ( is_array( $results['articles'] ) ) foreach ( $results['articles'] as $article )
{
$tpl = new \Tpl;
$tpl -> article = \front\factory\Articles::article_details( $article, $lang_id );
$out .= $tpl -> render( 'articles/article-miniature' );
}
if ( $results['ls'] > 1 )
{
$tpl = new \Tpl;
$tpl -> ls = $results['ls'];
$tpl -> bs = $bs ? $bs : 1;
$tpl -> page = $page;
$out .= $tpl -> render( 'site/pager' );
}
return $out;
$repo = new \Domain\Article\ArticleRepository( $GLOBALS['mdb'] );
$results = $repo->pageArticles( $page, $lang_id, (int)$bs );
$articles = [];
if ( is_array( $results['articles'] ) ) foreach ( $results['articles'] as $article_id )
$articles[] = $repo->articleDetailsFrontend( (int)$article_id, $lang_id );
return \front\Views\Articles::miniatureArticlesList( $articles, $results['ls'], $bs ?: 1, $page );
}
public static function entry_articles_list( $page, $lang_id, $bs = 1 )
{
$results = \front\factory\Articles::page_articles( $page, $lang_id, $bs );
if ( is_array( $results['articles'] ) ) foreach ( $results['articles'] as $article )
$articles[] = \front\factory\Articles::article_details( $article, $lang_id );
$tpl = new \Tpl;
$tpl -> page_id = $page['id'];
$tpl -> articles = $articles;
$out .= $tpl -> render( 'articles/articles-entries' );
if ( $results['ls'] > 1 )
{
$tpl = new \Tpl;
$tpl -> ls = $results['ls'];
$tpl -> bs = $bs ? $bs : 1;
$tpl -> page = $page;
$out .= $tpl -> render( 'site/pager' );
}
return $out;
$repo = new \Domain\Article\ArticleRepository( $GLOBALS['mdb'] );
$results = $repo->pageArticles( $page, $lang_id, (int)$bs );
$articles = [];
if ( is_array( $results['articles'] ) ) foreach ( $results['articles'] as $article_id )
$articles[] = $repo->articleDetailsFrontend( (int)$article_id, $lang_id );
return \front\Views\Articles::entryArticlesList( $articles, $results['ls'], $bs ?: 1, $page );
}
public static function full_articles_list( $page, $lang_id, $bs = 1 )
{
$results = \front\factory\Articles::page_articles( $page, $lang_id, $bs );
if ( is_array( $results['articles'] ) ) foreach ( $results['articles'] as $article )
{
$tpl = new \Tpl;
$tpl -> article = \front\factory\Articles::article_details( $article, $lang_id );
$out .= $tpl -> render( 'articles/article-full' );
}
if ( $results['ls'] > 1 )
{
$tpl = new \Tpl;
$tpl -> ls = $results['ls'];
$tpl -> bs = $bs ? $bs : 1;
$tpl -> page = $page;
$out .= $tpl -> render( 'site/pager' );
}
return $out;
$repo = new \Domain\Article\ArticleRepository( $GLOBALS['mdb'] );
$results = $repo->pageArticles( $page, $lang_id, (int)$bs );
$articles = [];
if ( is_array( $results['articles'] ) ) foreach ( $results['articles'] as $article_id )
$articles[] = $repo->articleDetailsFrontend( (int)$article_id, $lang_id );
return \front\Views\Articles::fullArticlesList( $articles, $results['ls'], $bs ?: 1, $page );
}
}
}

View File

@@ -22,6 +22,8 @@ class Site
{
global $page, $settings, $settings, $lang, $lang_id;
$articleRepo = new \Domain\Article\ArticleRepository( $GLOBALS['mdb'] );
if ( (int) \S::get( 'layout_id' ) )
$layout = new \cms\Layout( (int) \S::get( 'layout_id' ) );
@@ -92,9 +94,9 @@ class Site
$news_list_tmp[2] != '' ? $news_limit = $news_list_tmp[2] : $news_limit = $settings['news_limit'];
$news_list_tmp[2] != '' ? $pattern = '[AKTUALNOSCI:' . $news_list_tmp[1] . ':' . $news_list_tmp[2] . ']' : $pattern = '[AKTUALNOSCI:' . $news_list_tmp[1] . ']';
$html = str_replace( $pattern, \front\view\Articles::news(
$html = str_replace( $pattern, \front\Views\Articles::news(
$news_list_tmp[1],
\front\factory\Articles::news( $news_list_tmp[1], $news_limit, $lang_id )
$articleRepo->news( (int)$news_list_tmp[1], (int)$news_limit, $lang_id )
), $html );
}
@@ -175,7 +177,7 @@ class Site
//
if ( \S::get( 'article' ) )
{
$article = \front\factory\Articles::article_details( \S::get( 'article' ), $lang_id );
$article = $articleRepo->articleDetailsFrontend( (int)\S::get( 'article' ), $lang_id );
if ( $article['language']['meta_title'] )
$page['language']['title'] = $article['language']['meta_title'];
@@ -340,7 +342,7 @@ class Site
/* atrybut noindex */
if ( \S::get( 'article' ) )
{
\front\factory\Articles::article_noindex( \S::get( 'article' ) ) ? $noindex = 'noindex' : $noindex = 'all';
$articleRepo->articleNoindex( (int)\S::get( 'article' ), $lang_id ) ? $noindex = 'noindex' : $noindex = 'all';
$html = str_replace( '[META_INDEX]', '<meta name="robots" content="' . $noindex . '">', $html );
}
else
@@ -390,29 +392,27 @@ class Site
}
// prosta lista aktualności z wybranej podstrony
preg_match_all( self::news_list_pattern, $html, $news_list );
if ( is_array( $news_list[0] ) ) foreach( $news_list[0] as $news_list_tmp )
preg_match_all( self::news_list_pattern, $html, $news_list_matches );
if ( is_array( $news_list_matches[0] ) ) foreach( $news_list_matches[0] as $news_list_tmp )
{
$news_list_tmp = explode( ':', $news_list_tmp );
$news_list_tmp[2] != '' ? $news_limit = $news_list_tmp[2] : $news_limit = $settings['news_limit'];
$news_list_tmp[2] != '' ? $pattern = '[AKTUALNOSCI_LISTA:' . $news_list_tmp[1] . ':' . $news_list_tmp[2] . ']' : $pattern = '[AKTUALNOSCI_LISTA:' . $news_list_tmp[1] . ']';
$news_list = \Article::getNews( $news_list_tmp[1], $news_limit, $lang_id );
$view_news_list = \Article::newsList( $news_list );
$html = str_replace( $pattern, $view_news_list, $html );
$newsArticles = $articleRepo->newsListArticles( (int)$news_list_tmp[1], (int)$news_limit, $lang_id );
$html = str_replace( $pattern, \front\Views\Articles::newsList( $newsArticles ), $html );
}
// prosta lista z najpopularniejszymi artykułami
preg_match_all( self::top_news_pattern, $html, $news_list );
if ( is_array( $news_list[0] ) ) foreach( $news_list[0] as $news_list_tmp )
preg_match_all( self::top_news_pattern, $html, $top_news_matches );
if ( is_array( $top_news_matches[0] ) ) foreach( $top_news_matches[0] as $news_list_tmp )
{
$news_list_tmp = explode( ':', $news_list_tmp );
$news_list_tmp[2] != '' ? $news_limit = $news_list_tmp[2] : $news_limit = $settings['news_limit'];
$news_list_tmp[2] != '' ? $pattern = '[NAJPOULARNIEJSZE_ARTYKULY:' . $news_list_tmp[1] . ':' . $news_list_tmp[2] . ']' : $pattern = '[NAJPOULARNIEJSZE_ARTYKULY:' . $news_list_tmp[1] . ']';
$news_list = \Article::getTopNews( $news_list_tmp[1], $news_limit, $lang_id );
$view_news_list = \Article::newsList( $news_list );
$html = str_replace( $pattern, $view_news_list, $html );
$topArticles = $articleRepo->topArticles( (int)$news_list_tmp[1], (int)$news_limit, $lang_id );
$html = str_replace( $pattern, \front\Views\Articles::newsList( $topArticles ), $html );
}
$html = str_replace( '[ALERT]', \front\view\Site::alert(), $html );

View File

@@ -197,7 +197,7 @@ class Order implements \ArrayAccess
$order_statuses = self::order_statuses();
$firm_name = \front\factory\Settings::get_single_settings_value( 'firm_name' );
$firm_name = ( new \Domain\Settings\SettingsRepository( $mdb ) )->getSingleValue( 'firm_name' );
switch( $this -> status ):
case 0: