Skip to content

Commit

Permalink
Started refactoring outdated "Model" code.
Browse files Browse the repository at this point in the history
  • Loading branch information
parpalak committed May 5, 2024
1 parent 3aab7ba commit 7316968
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 138 deletions.
8 changes: 4 additions & 4 deletions _admin/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/


use S2\Cms\Model\Model;
use S2\Cms\Model\ArticleProvider;
use S2\Cms\Pdo\DbLayer;

if (!defined('S2_ROOT'))
Expand All @@ -23,7 +23,7 @@ function s2_check_url_status ($parent_id, $url)

($hook = s2_hook('fn_check_url_status_start')) ? eval($hook) : null;

if ($parent_id != Model::ROOT_ID)
if ($parent_id != ArticleProvider::ROOT_ID)
{
if ($url == '')
$url_status = 'empty';
Expand Down Expand Up @@ -405,8 +405,8 @@ function s2_output_article_form ($id)
<?php ($hook = s2_hook('fn_output_article_form_after_checkboxes')) ? eval($hook) : null; ?>
<hr />
<?php ($hook = s2_hook('fn_output_article_form_pre_url')) ? eval($hook) : null; ?>
<label id="url_input_label"<?php if ($url_error) echo ' class="error" title="'.$url_error.'"'; elseif ($page['parent_id'] == Model::ROOT_ID) echo ' title="'.$lang_admin['URL on mainpage'].'"'; ?> title_unique="<?php echo $lang_admin['URL not unique']; ?>" title_empty="<?php echo $lang_admin['URL empty']; ?>"><?php echo $lang_admin['URL part']; ?><br />
<input type="text" name="page[url]" size="15" maxlength="255" value="<?php echo $page['url']; ?>" <?php echo $page['parent_id'] == Model::ROOT_ID ? 'disabled="disabled" ' : ''; ?>/></label>
<label id="url_input_label"<?php if ($url_error) echo ' class="error" title="'.$url_error.'"'; elseif ($page['parent_id'] == ArticleProvider::ROOT_ID) echo ' title="'.$lang_admin['URL on mainpage'].'"'; ?> title_unique="<?php echo $lang_admin['URL not unique']; ?>" title_empty="<?php echo $lang_admin['URL empty']; ?>"><?php echo $lang_admin['URL part']; ?><br />
<input type="text" name="page[url]" size="15" maxlength="255" value="<?php echo $page['url']; ?>" <?php echo $page['parent_id'] == ArticleProvider::ROOT_ID ? 'disabled="disabled" ' : ''; ?>/></label>
<?php ($hook = s2_hook('fn_output_article_form_pre_published')) ? eval($hook) : null; ?>
<label for="publiched_checkbox"<?php if ($page['published']) echo ' class="ok"'; ?>><input type="checkbox" id="publiched_checkbox" name="flags[published]" value="1"<?php if ($page['published']) echo ' checked="checked"' ?> />
<?php echo $lang_admin['Published']; ?></label>
Expand Down
4 changes: 2 additions & 2 deletions _admin/site_lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/


use S2\Cms\Model\Model;
use S2\Cms\Model\ArticleProvider;
use S2\Cms\Pdo\DbLayer;

if (!defined('S2_ROOT'))
Expand Down Expand Up @@ -218,7 +218,7 @@ function s2_preload_editor ()
if (!S2_USE_HIERARCHY)
$request_array = array($request_array[1]);

$id = Model::ROOT_ID;
$id = ArticleProvider::ROOT_ID;
$max = count($request_array);

// Walking through page parents
Expand Down
4 changes: 2 additions & 2 deletions _admin/tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/


use S2\Cms\Model\Model;
use S2\Cms\Model\ArticleProvider;
use S2\Cms\Pdo\DbLayer;

if (!defined('S2_ROOT'))
Expand Down Expand Up @@ -221,7 +221,7 @@ function s2_delete_branch ($id)
else
die('Item not found!');

if ($parent_id == Model::ROOT_ID)
if ($parent_id == ArticleProvider::ROOT_ID)
die('Can\'t delete root item!');

if (!$s2_user['edit_site'])
Expand Down
13 changes: 9 additions & 4 deletions _extensions/s2_blog/Controller/Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@
* @package s2_blog
*/

namespace s2_extensions\s2_blog\Controller;
declare(strict_types=1);

namespace s2_extensions\s2_blog\Controller;

use S2\Cms\Model\UrlBuilder;
use S2\Cms\Pdo\DbLayer;
use S2\Cms\Template\Viewer;

class Sitemap extends \S2\Cms\Controller\Sitemap
{
public function __construct(DbLayer $dbLayer, Viewer $viewer, private readonly string $blogUrl)
{
parent::__construct($dbLayer, $viewer);
public function __construct(
protected DbLayer $dbLayer,
protected UrlBuilder $urlBuilder,
protected Viewer $viewer,
private readonly string $blogUrl
) {
}

/**
Expand Down
24 changes: 14 additions & 10 deletions _extensions/s2_blog/Controller/TagPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

use Lang;
use S2\Cms\Framework\Exception\NotFoundException;
use S2\Cms\Model\ArticleProvider;
use S2\Cms\Model\UrlBuilder;
use S2\Cms\Pdo\DbLayer;
use S2\Cms\Template\HtmlTemplate;
use S2\Cms\Template\HtmlTemplateProvider;
Expand All @@ -24,13 +26,15 @@
class TagPageController extends BlogController
{
public function __construct(
DbLayer $dbLayer,
HtmlTemplateProvider $templateProvider,
Viewer $viewer,
string $tagsUrl,
string $blogUrl,
string $blogTitle,
private readonly bool $useHierarchy
DbLayer $dbLayer,
private readonly ArticleProvider $articleProvider,
private readonly UrlBuilder $urlBuilder,
HtmlTemplateProvider $templateProvider,
Viewer $viewer,
string $tagsUrl,
string $blogUrl,
string $blogTitle,
private readonly bool $useHierarchy
) {
parent::__construct($dbLayer, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle);
}
Expand Down Expand Up @@ -85,7 +89,7 @@ public function body(Request $request, HtmlTemplate $template): ?Response
throw new NotFoundException();
}

$template->addBreadCrumb(\S2\Cms\Model\Model::main_page_title(), s2_link('/'));
$template->addBreadCrumb(\S2\Cms\Model\Model::main_page_title(), $this->urlBuilder->link('/'));
if ($this->blogUrl !== '') {
$template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogPath);
}
Expand Down Expand Up @@ -137,10 +141,10 @@ private function articles_by_tag(int $tag_id): array
$parentIds[] = $row['parent_id'];
$title[] = $row['title'];
}
$urls = \S2\Cms\Model\Model::get_group_url($parentIds, $urls);
$urls = $this->articleProvider->getFullUrlsForArticles($parentIds, $urls);

foreach ($urls as $k => $v) {
$urls[$k] = '<a href="' . s2_link($v) . '">' . $title[$k] . '</a>';
$urls[$k] = '<a href="' . $this->urlBuilder->link($v) . '">' . $title[$k] . '</a>';
}

return $urls;
Expand Down
5 changes: 5 additions & 0 deletions _extensions/s2_blog/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use S2\Cms\Config\DynamicConfigProvider;
use S2\Cms\Framework\Container;
use S2\Cms\Framework\ExtensionInterface;
use S2\Cms\Model\ArticleProvider;
use S2\Cms\Model\UrlBuilder;
use S2\Cms\Pdo\DbLayer;
use S2\Cms\Recommendation\RecommendationProvider;
use S2\Cms\Template\HtmlTemplateProvider;
Expand Down Expand Up @@ -119,6 +121,8 @@ public function buildContainer(Container $container): void
$provider = $container->get(DynamicConfigProvider::class);
return new TagPageController(
$container->get(DbLayer::class),
$container->get(ArticleProvider::class),
$container->get(UrlBuilder::class),
$container->get(HtmlTemplateProvider::class),
$container->get(Viewer::class),
$provider->get('S2_TAGS_URL'),
Expand Down Expand Up @@ -156,6 +160,7 @@ public function buildContainer(Container $container): void
$provider = $container->get(DynamicConfigProvider::class);
return new Sitemap(
$container->get(DbLayer::class),
$container->get(UrlBuilder::class),
$container->get('strict_viewer'),
$provider->get('S2_BLOG_URL'),
);
Expand Down
9 changes: 9 additions & 0 deletions _include/src/CmsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ public function buildContainer(Container $container): void
$provider = $container->get(DynamicConfigProvider::class);
return new CommentProvider(
$container->get(DbLayer::class),
$container->get(ArticleProvider::class),
$container->get(UrlBuilder::class),
$provider->get('S2_SHOW_COMMENTS') === '1',
);
Expand All @@ -301,6 +302,8 @@ public function buildContainer(Container $container): void
$container->set(PageFavorite::class, function (Container $container) {
return new PageFavorite(
$container->get(DbLayer::class),
$container->get(ArticleProvider::class),
$container->get(UrlBuilder::class),
$container->get(HtmlTemplateProvider::class),
$container->get(Viewer::class),
);
Expand All @@ -320,6 +323,8 @@ public function buildContainer(Container $container): void
$provider = $container->get(DynamicConfigProvider::class);
return new PageTag(
$container->get(DbLayer::class),
$container->get(ArticleProvider::class),
$container->get(UrlBuilder::class),
$container->get(HtmlTemplateProvider::class),
$container->get(Viewer::class),
$provider->get('S2_TAGS_URL'),
Expand All @@ -331,6 +336,8 @@ public function buildContainer(Container $container): void
$provider = $container->get(DynamicConfigProvider::class);
return new PageCommon(
$container->get(DbLayer::class),
$container->get(ArticleProvider::class),
$container->get(UrlBuilder::class),
$container->get(HtmlTemplateProvider::class),
$container->get(RecommendationProvider::class),
$container->get(Viewer::class),
Expand All @@ -357,6 +364,8 @@ public function buildContainer(Container $container): void
$container->set(Sitemap::class, function (Container $container) {
return new Sitemap(
$container->get(DbLayer::class),
$container->get(ArticleProvider::class),
$container->get(UrlBuilder::class),
$container->get('strict_viewer'),
);
});
Expand Down
38 changes: 21 additions & 17 deletions _include/src/Controller/PageCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use S2\Cms\Framework\ControllerInterface;
use S2\Cms\Framework\Exception\NotFoundException;
use S2\Cms\Model\ArticleProvider;
use S2\Cms\Model\UrlBuilder;
use S2\Cms\Pdo\DbLayer;
use S2\Cms\Recommendation\RecommendationProvider;
use S2\Cms\Template\HtmlTemplateProvider;
Expand All @@ -27,6 +29,8 @@
{
public function __construct(
private DbLayer $dbLayer,
private ArticleProvider $articleProvider,
private UrlBuilder $urlBuilder,
private HtmlTemplateProvider $htmlTemplateProvider,
private RecommendationProvider $recommendationProvider,
private Viewer $viewer,
Expand All @@ -46,15 +50,15 @@ public function handle(Request $request): Response

// Correcting trailing slash and the rest of URL
if (!$this->useHierarchy && \count($request_array) > 2) {
return new RedirectResponse(s2_link('/' . $request_array[1]), Response::HTTP_MOVED_PERMANENTLY);
return new RedirectResponse($this->urlBuilder->link('/' . $request_array[1]), Response::HTTP_MOVED_PERMANENTLY);
}

$was_end_slash = str_ends_with($request_uri, '/');

$bread_crumbs = [];

$parent_path = '';
$parent_id = \S2\Cms\Model\Model::ROOT_ID;
$parent_id = ArticleProvider::ROOT_ID;
$parent_num = \count($request_array) - 1 - (int)$was_end_slash;

$template_id = '';
Expand Down Expand Up @@ -103,7 +107,7 @@ public function handle(Request $request): Response
}

$bread_crumbs[] = [
'link' => s2_link($parent_path),
'link' => $this->urlBuilder->link($parent_path),
'title' => $cur_node['title']
];
}
Expand Down Expand Up @@ -154,7 +158,7 @@ public function handle(Request $request): Response
if ($template_id === '') {
if ($this->useHierarchy) {
$bread_crumbs[] = [
'link' => s2_link($parent_path),
'link' => $this->urlBuilder->link($parent_path),
'title' => $page['title'],
];

Expand All @@ -167,7 +171,7 @@ public function handle(Request $request): Response
}

if ($this->useHierarchy && $parent_num && $page['children_exist'] != $was_end_slash) {
return new RedirectResponse(s2_link($current_path . (!$was_end_slash ? '/' : '')), Response::HTTP_MOVED_PERMANENTLY);
return new RedirectResponse($this->urlBuilder->link($current_path . (!$was_end_slash ? '/' : '')), Response::HTTP_MOVED_PERMANENTLY);
}

$articleId = (int)$page['id'];
Expand Down Expand Up @@ -197,13 +201,13 @@ public function handle(Request $request): Response
foreach ($bread_crumbs as $crumb) {
$template->addBreadCrumb($crumb['title'], $crumb['link'] ?? null);
}
$template->setLink('top', s2_link('/'));
$template->setLink('top', $this->urlBuilder->link('/'));

if (\count($bread_crumbs) > 1) {
$template->setLink('up', s2_link($parent_path));
$template->setLink('up', $this->urlBuilder->link($parent_path));
$template->putInPlaceholder(
'section_link',
'<a href="' . s2_link($parent_path) . '">' . $bread_crumbs[\count($bread_crumbs) - 2]['title'] . '</a>'
'<a href="' . $this->urlBuilder->link($parent_path) . '">' . $bread_crumbs[\count($bread_crumbs) - 2]['title'] . '</a>'
);
}
}
Expand Down Expand Up @@ -246,7 +250,7 @@ public function handle(Request $request): Response
$item = [
'id' => $row['id'],
'title' => $row['title'],
'link' => s2_link($current_path . '/' . urlencode($row['url']) . '/'),
'link' => $this->urlBuilder->link($current_path . '/' . urlencode($row['url']) . '/'),
'date' => s2_date($row['create_time']),
'excerpt' => $row['excerpt'],
'favorite' => $row['favorite'],
Expand All @@ -258,7 +262,7 @@ public function handle(Request $request): Response
$item = array(
'id' => $row['id'],
'title' => $row['title'],
'link' => s2_link($current_path . '/' . urlencode($row['url'])),
'link' => $this->urlBuilder->link($current_path . '/' . urlencode($row['url'])),
'date' => s2_date($row['create_time']),
'excerpt' => $row['excerpt'],
'favorite' => $row['favorite'],
Expand Down Expand Up @@ -310,7 +314,7 @@ public function handle(Request $request): Response
$total_pages = ceil(1.0 * \count($subarticles) / $this->maxItems);

$link_nav = [];
$paging = s2_paging($page_num + 1, $total_pages, s2_link(str_replace('%', '%%', $current_path . '/'), ['p=%d']), $link_nav) . "\n";
$paging = s2_paging($page_num + 1, $total_pages, $this->urlBuilder->link(str_replace('%', '%%', $current_path . '/'), ['p=%d']), $link_nav) . "\n";
foreach ($link_nav as $rel => $href) {
$template->setLink($rel, $href);
}
Expand Down Expand Up @@ -367,7 +371,7 @@ public function handle(Request $request): Response
$curr_item = -1;
while ($row = $this->dbLayer->fetchAssoc($result)) {
// A neighbour
$url = s2_link($parent_path . urlencode($row['url']));
$url = $this->urlBuilder->link($parent_path . urlencode($row['url']));

$menu_articles[] = [
'title' => $row['title'],
Expand All @@ -385,7 +389,7 @@ public function handle(Request $request): Response

if (\count($bread_crumbs) > 1) {
$template->putInPlaceholder('menu_siblings', $this->viewer->render('menu_block', [
'title' => sprintf(\Lang::get('More in this section'), '<a href="' . s2_link($parent_path) . '">' . $bread_crumbs[\count($bread_crumbs) - 2]['title'] . '</a>'),
'title' => sprintf(\Lang::get('More in this section'), '<a href="' . $this->urlBuilder->link($parent_path) . '">' . $bread_crumbs[\count($bread_crumbs) - 2]['title'] . '</a>'),
'menu' => $menu_articles,
'class' => 'menu_siblings',
]));
Expand All @@ -402,7 +406,7 @@ public function handle(Request $request): Response
$template->putInPlaceholder('back_forward', [
'up' => \count($bread_crumbs) <= 1 ? null : [
'title' => $bread_crumbs[\count($bread_crumbs) - 2]['title'],
'link' => s2_link($parent_path),
'link' => $this->urlBuilder->link($parent_path),
],
'back' => empty($menu_articles[$curr_item - 1]) ? null : [
'title' => $menu_articles[$curr_item - 1]['title'],
Expand Down Expand Up @@ -529,7 +533,7 @@ private function tagged_articles(int $articleId): string
}

if ($hasArticlesInList) {
$urls = \S2\Cms\Model\Model::get_group_url($parent_ids, $urls);
$urls = $this->articleProvider->getFullUrlsForArticles($parent_ids, $urls);
}

// Sorting all obtained article links into groups by each tag
Expand All @@ -553,7 +557,7 @@ private function tagged_articles(int $articleId): string
$output = [];
foreach ($art_by_tags as $tag_id => $articles) {
$output[] = $this->viewer->render('menu_block', array(
'title' => sprintf(\Lang::get('With this tag'), '<a href="' . s2_link('/' . $this->tagsUrl . '/' . urlencode($tag_urls[$tag_id]) . '/') . '">' . $tag_names[$tag_id] . '</a>'),
'title' => sprintf(\Lang::get('With this tag'), '<a href="' . $this->urlBuilder->link('/' . $this->tagsUrl . '/' . urlencode($tag_urls[$tag_id]) . '/') . '">' . $tag_names[$tag_id] . '</a>'),
'menu' => $articles,
'class' => 'article_tags',
));
Expand Down Expand Up @@ -582,7 +586,7 @@ private function get_tags(int $articleId): string
while ($row = $this->dbLayer->fetchAssoc($result)) {
$tags[] = array(
'title' => $row['name'],
'link' => s2_link('/' . $this->tagsUrl . '/' . urlencode($row['url']) . '/'),
'link' => $this->urlBuilder->link('/' . $this->tagsUrl . '/' . urlencode($row['url']) . '/'),
);
}

Expand Down
Loading

0 comments on commit 7316968

Please sign in to comment.