2024-04-20 23:27:47 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services\BundleRenderers\Renderers;
|
|
|
|
|
2024-04-23 21:04:20 +02:00
|
|
|
use App\Classes\AttachmentsManager;
|
2024-04-20 23:27:47 +02:00
|
|
|
use App\Classes\Bundle;
|
2024-04-24 13:55:08 +02:00
|
|
|
use App\Classes\Link;
|
2024-04-20 23:27:47 +02:00
|
|
|
use App\Services\BundleRenderers\Contracts\RendersBundle;
|
2024-04-23 21:04:20 +02:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use Exception;
|
2024-04-20 23:27:47 +02:00
|
|
|
|
|
|
|
abstract class BaseRenderer implements RendersBundle
|
|
|
|
{
|
2024-04-23 21:04:20 +02:00
|
|
|
protected array $viewData = [];
|
|
|
|
|
2024-04-26 16:09:33 +02:00
|
|
|
protected ?array $subBundles;
|
|
|
|
|
2024-04-20 23:27:47 +02:00
|
|
|
public function __construct(protected Bundle $bundle)
|
|
|
|
{
|
|
|
|
$bundle->load();
|
2024-04-23 21:04:20 +02:00
|
|
|
|
|
|
|
$this->shareCommonDataWithView();
|
|
|
|
}
|
|
|
|
|
2024-04-26 16:09:33 +02:00
|
|
|
public function getSubBundles()
|
|
|
|
{
|
|
|
|
if (!isset($this->subBundles)) {
|
|
|
|
$this->subBundles = $this->collectSubBundles();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->subBundles;
|
|
|
|
}
|
|
|
|
|
2024-04-25 15:44:29 +02:00
|
|
|
/**
|
|
|
|
* Renders a complete HTML view of the bundle
|
|
|
|
*/
|
|
|
|
public function render()
|
|
|
|
{
|
2024-04-26 16:09:33 +02:00
|
|
|
$result = [];
|
|
|
|
$totalPages = count(array_chunk($this->getSubBundles(), config('pagination.itemsPerPage')));
|
|
|
|
|
|
|
|
if ($totalPages > 1) {
|
|
|
|
for ($currentPage = 1; $currentPage <= $totalPages; $currentPage++) {
|
|
|
|
$this->prepareRender($currentPage);
|
|
|
|
|
|
|
|
if ($currentPage === 1) {
|
|
|
|
$result[$this->bundle->getPath()] = (string) view('article', $this->viewData);
|
|
|
|
}
|
|
|
|
|
|
|
|
$page = sprintf('%spage/%s/', $this->bundle->getPath(), $currentPage);
|
|
|
|
|
|
|
|
$result[$page] = (string) view('article', $this->viewData);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->prepareRender(1);
|
|
|
|
|
|
|
|
$result[$this->bundle->getPath()] = (string) view('article', $this->viewData);
|
|
|
|
}
|
2024-04-25 15:44:29 +02:00
|
|
|
|
2024-04-26 16:09:33 +02:00
|
|
|
return $result;
|
2024-04-25 15:44:29 +02:00
|
|
|
}
|
|
|
|
|
2024-04-23 21:04:20 +02:00
|
|
|
/**
|
|
|
|
* Renders a card HTML view of the bundle, suitable to display in lists
|
|
|
|
*/
|
|
|
|
public function renderCard()
|
|
|
|
{
|
2024-04-23 23:55:48 +02:00
|
|
|
$this->prepareRenderCard();
|
|
|
|
|
|
|
|
return view('article-card', $this->viewData);
|
2024-04-20 23:27:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an instance of the creator, using specified data as input
|
|
|
|
*/
|
|
|
|
public static function make(Bundle $bundle): RendersBundle
|
|
|
|
{
|
|
|
|
return new static($bundle);
|
|
|
|
}
|
2024-04-23 21:04:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a complete HTML view of the bundle
|
|
|
|
*/
|
2024-04-26 16:09:33 +02:00
|
|
|
protected function prepareRender(int $currentPage = 1)
|
2024-04-23 21:04:20 +02:00
|
|
|
{
|
|
|
|
$this->bundle->markdown()->lint();
|
|
|
|
|
|
|
|
$coverRef = $this->bundle->metadata()->get('cover');
|
|
|
|
$cover = null;
|
|
|
|
|
|
|
|
if (!empty($coverRef)) {
|
|
|
|
$cover = $this->bundle->attachments(AttachmentsManager::Images)->getComponentByRef($coverRef, 'article');
|
|
|
|
}
|
|
|
|
|
|
|
|
$date = $this->bundle->metadata()->get('date');
|
|
|
|
|
|
|
|
if (!empty($date)) {
|
|
|
|
data_set($this->viewData, 'date', Carbon::parse($date));
|
|
|
|
}
|
|
|
|
|
|
|
|
data_set($this->viewData, 'cover', $cover ? $cover->render() : null);
|
|
|
|
data_set($this->viewData, 'body', $this->bundle->markdown()->render());
|
|
|
|
|
2024-04-26 16:09:33 +02:00
|
|
|
$this->handlePagination($currentPage);
|
2024-04-23 21:04:20 +02:00
|
|
|
}
|
|
|
|
|
2024-04-23 23:55:48 +02:00
|
|
|
/**
|
|
|
|
* Renders a HTML card view of the bundle
|
|
|
|
*/
|
|
|
|
protected function prepareRenderCard()
|
|
|
|
{
|
|
|
|
$coverRef = $this->bundle->metadata()->get('cover');
|
|
|
|
$cover = null;
|
|
|
|
|
|
|
|
if (!empty($coverRef)) {
|
|
|
|
$cover = $this->bundle->attachments(AttachmentsManager::Images)->getComponentByRef($coverRef, 'listitem', ['nolink' => true]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$date = $this->bundle->metadata()->get('date');
|
|
|
|
|
|
|
|
if (!empty($date)) {
|
|
|
|
data_set($this->viewData, 'date', Carbon::parse($date));
|
|
|
|
}
|
|
|
|
|
|
|
|
data_set($this->viewData, 'cover', $cover ? $cover->render() : null);
|
|
|
|
data_set($this->viewData, 'section', $this->bundle->getSection());
|
|
|
|
}
|
|
|
|
|
2024-04-23 21:04:20 +02:00
|
|
|
/**
|
|
|
|
* Pre-fill view with data used by all renderers
|
|
|
|
*/
|
|
|
|
protected function shareCommonDataWithView(): void
|
|
|
|
{
|
|
|
|
data_set($this->viewData, 'siteTitle', $this->bundle->getSiteTitle());
|
|
|
|
data_set($this->viewData, 'articleTitle', $this->bundle->getArticleTitle());
|
|
|
|
data_set($this->viewData, 'bundle', $this->bundle);
|
2024-04-24 13:55:08 +02:00
|
|
|
|
|
|
|
if (!empty($this->bundle->metadata('metadata')->get('links.Page originale'))) {
|
|
|
|
$url = $this->bundle->metadata('metadata')->get('links.Page originale')[0];
|
|
|
|
$link = (new Link($url, null, $this->bundle))->toArray();
|
|
|
|
|
|
|
|
$link['url'] = $url;
|
|
|
|
|
|
|
|
data_set($this->viewData, 'mainLink', $link);
|
|
|
|
}
|
2024-04-23 21:04:20 +02:00
|
|
|
}
|
|
|
|
|
2024-04-26 16:09:33 +02:00
|
|
|
protected function handlePagination(int $currentPage = 1)
|
2024-04-23 21:04:20 +02:00
|
|
|
{
|
|
|
|
$itemsPerPage = config('pagination.itemsPerPage');
|
2024-04-26 16:09:33 +02:00
|
|
|
$subBundles = $this->getSubBundles();
|
2024-04-23 21:04:20 +02:00
|
|
|
|
|
|
|
if (!$this->bundle->exists() && empty($subBundles)) {
|
|
|
|
throw new Exception(sprintf(
|
|
|
|
'Page %s does not exist in bundle %s',
|
|
|
|
$this->bundle->getCurrentPage(),
|
|
|
|
$this->bundle->getPath()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
$totalPages = 1;
|
|
|
|
|
|
|
|
if (!empty($subBundles)) {
|
|
|
|
$chunks = array_chunk($subBundles, $itemsPerPage);
|
|
|
|
|
|
|
|
if (!array_key_exists($currentPage - 1, $chunks) && $currentPage > 1) {
|
|
|
|
throw new Exception(sprintf(
|
|
|
|
'Page %s does not exist in bundle %s',
|
|
|
|
$currentPage - 1,
|
|
|
|
$this->bundle->getPath()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
$currentChunk = $chunks[$currentPage - 1];
|
|
|
|
$totalPages = count($chunks);
|
|
|
|
} else {
|
|
|
|
$currentChunk = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$bundles = [];
|
|
|
|
|
|
|
|
if (!empty($currentChunk)) {
|
|
|
|
$bundles = array_map(fn (string $path) => new Bundle($path, $this->bundle->getDisk()), $currentChunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'root' => $this->bundle->getPath(),
|
|
|
|
'itemsPerPage' => $itemsPerPage,
|
|
|
|
'currentPage' => $currentPage,
|
|
|
|
'totalPages' => $totalPages,
|
|
|
|
'items' => $bundles,
|
|
|
|
];
|
|
|
|
|
|
|
|
data_set($this->viewData, 'pagination', $data);
|
2024-04-25 15:44:29 +02:00
|
|
|
|
|
|
|
$this->handleFilters($subBundles);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function handleFilters($subBundles)
|
|
|
|
{
|
2024-04-23 21:04:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function collectSubBundles()
|
|
|
|
{
|
|
|
|
$subBundles = Bundle::findBundles($this->bundle->getDisk(), $this->bundle->getPath(), true);
|
|
|
|
|
|
|
|
$subBundles = collect($subBundles)
|
|
|
|
->filter(fn (Bundle $bundle) => !empty($bundle->metadata()->get('date')) && $bundle->getPath() !== $this->bundle->getPath())
|
|
|
|
->sort(function (Bundle $bundleA, Bundle $bundleB) {
|
|
|
|
return Carbon::parse($bundleA->metadata()->get('date'))->lt(Carbon::parse($bundleB->metadata()->get('date')));
|
|
|
|
})
|
|
|
|
->map(fn (Bundle $bundle) => $bundle->getPath())
|
|
|
|
->toArray();
|
|
|
|
|
|
|
|
return $subBundles;
|
|
|
|
}
|
2024-04-20 23:27:47 +02:00
|
|
|
}
|