daux.io/libs/Format/HTML/ContentPage.php

127 regels
3.9 KiB
PHP

<?php namespace Todaymade\Daux\Format\HTML;
use Todaymade\Daux\Tree\Root;
class ContentPage extends \Todaymade\Daux\Format\Base\ContentPage
{
2020-04-22 22:24:52 +02:00
/**
* @var Template
*/
public $templateRenderer;
/**
* @var string
*/
private $language;
/**
* @var bool
*/
private $homepage;
private function isHomepage(): bool
2015-04-23 00:32:30 +02:00
{
// If the current page isn't the index, no chance it is the landing page
if ($this->file->getParent()->getIndexPage() != $this->file) {
return false;
}
// If the direct parent is root, this is the homepage
2016-07-27 21:32:51 +02:00
return $this->file->getParent() instanceof Root;
}
2020-04-22 21:55:53 +02:00
private function isLanding(): bool
{
return $this->config->getHTML()->hasLandingPage() && $this->homepage;
}
private function initialize()
{
$this->homepage = $this->isHomepage();
$this->language = '';
if ($this->config->isMultilanguage() && count($this->file->getParents())) {
2015-07-18 21:23:02 +02:00
$language_dir = $this->file->getParents()[0];
$this->language = $language_dir->getName();
}
}
2015-07-18 21:23:02 +02:00
/**
* @param \Todaymade\Daux\Tree\Directory[] $parents
* @param bool $multilanguage
2020-04-22 22:24:52 +02:00
*
2015-07-18 21:23:02 +02:00
* @return array
*/
2015-04-27 11:57:44 +02:00
private function getBreadcrumbTrail($parents, $multilanguage)
2015-04-23 00:32:30 +02:00
{
if ($multilanguage && !empty($parents)) {
$parents = array_splice($parents, 1);
}
2016-07-27 21:32:51 +02:00
$breadcrumb_trail = [];
if (!empty($parents)) {
foreach ($parents as $node) {
2015-07-21 09:51:55 +02:00
$page = $node->getIndexPage() ?: $node->getFirstPage();
$breadcrumb_trail[] = ['title' => $node->getTitle(), 'url' => $page ? $page->getUrl() : ''];
}
}
2016-07-27 21:32:51 +02:00
return $breadcrumb_trail;
}
protected function generatePage()
2015-04-23 00:32:30 +02:00
{
$this->initialize();
$config = $this->config;
2015-04-23 00:32:30 +02:00
$entry_page = [];
if ($this->homepage) {
if ($config->isMultilanguage()) {
foreach ($config->getLanguages() as $key => $name) {
$entry_page[$name] = $config->getBasePage() . $config->getEntryPage()[$key]->getUrl();
2015-04-23 00:32:30 +02:00
}
} else {
$entry_page['__VIEW_DOCUMENTATION__'] = $config->getBasePage() . $config->getEntryPage()->getUrl();
2015-04-23 00:32:30 +02:00
}
}
2015-04-27 11:57:44 +02:00
$page = [
'entry_page' => $entry_page,
'homepage' => $this->homepage,
'title' => $this->file->getTitle(),
'filename' => $this->file->getName(),
'language' => $this->language,
'path' => $this->file->getPath(),
'relative_path' => $this->file->getRelativePath(),
2015-04-27 11:57:44 +02:00
'modified_time' => filemtime($this->file->getPath()),
'markdown' => $this->content,
'request' => $config->getRequest(),
'content' => $this->getPureContent(),
'breadcrumbs' => $config->getHTML()->hasBreadcrumbs(),
'prev' => $this->file->getPrevious(),
'next' => $this->file->getNext(),
2020-04-22 22:24:52 +02:00
'attributes' => $this->file->getAttribute(),
2015-04-27 11:57:44 +02:00
];
if ($page['breadcrumbs']) {
$page['breadcrumb_trail'] = $this->getBreadcrumbTrail($this->file->getParents(), $config->isMultilanguage());
$page['breadcrumb_separator'] = $this->config->getHTML()->getBreadcrumbsSeparator();
if ($this->homepage) {
$page['breadcrumb_trail'] = [['title' => $this->file->getTitle(), 'url' => '']];
}
}
2015-04-23 00:32:30 +02:00
$context = ['page' => $page, 'config' => $config];
2015-08-02 15:42:23 +02:00
2020-04-22 22:24:52 +02:00
$template = 'theme::content';
if ($this->isLanding()) {
2020-04-22 22:24:52 +02:00
$template = 'theme::home';
}
if (array_key_exists('template', $page['attributes'])) {
2020-04-22 22:24:52 +02:00
$template = 'theme::' . $page['attributes']['template'];
}
return $this->templateRenderer->render($template, $context);
}
}