2015-05-18 14:26:29 +02:00
|
|
|
<?php namespace Todaymade\Daux\Format\HTML;
|
2015-04-23 10:24:50 +02:00
|
|
|
|
|
|
|
use League\Plates\Engine;
|
2015-05-18 14:26:29 +02:00
|
|
|
use Todaymade\Daux\Daux;
|
2015-07-18 14:37:18 +02:00
|
|
|
use Todaymade\Daux\Tree\Content;
|
|
|
|
use Todaymade\Daux\Tree\Directory;
|
2015-04-23 10:24:50 +02:00
|
|
|
|
|
|
|
class Template
|
|
|
|
{
|
|
|
|
protected $engine;
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* @param string $base
|
|
|
|
* @param string $theme
|
|
|
|
*/
|
2015-04-23 10:24:50 +02:00
|
|
|
public function __construct($base, $theme)
|
|
|
|
{
|
2016-01-25 16:10:13 +01:00
|
|
|
// Use templates from the phar archive if the templates dir doesn't exist.
|
|
|
|
if (!is_dir($base)) {
|
|
|
|
$base = 'phar://daux.phar/templates';
|
|
|
|
}
|
|
|
|
|
2015-04-23 10:24:50 +02:00
|
|
|
// Create new Plates instance
|
|
|
|
$this->engine = new Engine($base);
|
|
|
|
if (!is_dir($theme)) {
|
|
|
|
$theme = $base;
|
|
|
|
}
|
|
|
|
$this->engine->addFolder('theme', $theme, true);
|
|
|
|
|
|
|
|
$this->registerFunctions();
|
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param array $data
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-04-23 10:24:50 +02:00
|
|
|
public function render($name, array $data = array())
|
|
|
|
{
|
|
|
|
$this->engine->addData([
|
|
|
|
'base_url' => $data['params']['base_url'],
|
|
|
|
'base_page' => $data['params']['base_page'],
|
|
|
|
'page' => $data['page'],
|
|
|
|
'params' => $data['params'],
|
|
|
|
'tree' => $data['params']['tree'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $this->engine->render($name, $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function registerFunctions()
|
|
|
|
{
|
|
|
|
$this->engine->registerFunction('get_navigation', function($tree, $path, $current_url, $base_page, $mode) {
|
2015-04-23 20:42:44 +02:00
|
|
|
$nav = $this->buildNavigation($tree, $path, $current_url, $base_page, $mode);
|
|
|
|
return $this->renderNavigation($nav);
|
2015-04-23 10:24:50 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$this->engine->registerFunction('get_breadcrumb_title', function($page, $base_page) {
|
|
|
|
$title = '';
|
|
|
|
$breadcrumb_trail = $page['breadcrumb_trail'];
|
|
|
|
$separator = $this->getSeparator($page['breadcrumb_separator']);
|
|
|
|
foreach ($breadcrumb_trail as $key => $value) {
|
|
|
|
$title .= '<a href="' . $base_page . $value . '">' . $key . '</a>' . $separator;
|
|
|
|
}
|
|
|
|
if ($page['filename'] === 'index' || $page['filename'] === '_index') {
|
|
|
|
if ($page['title'] != '') {
|
|
|
|
$title = substr($title, 0, -1 * strlen($separator));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$title .= '<a href="' . $base_page . $page['request'] . '">' . $page['title'] . '</a>';
|
|
|
|
}
|
|
|
|
return $title;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-04-23 20:42:44 +02:00
|
|
|
private function renderNavigation($entries)
|
|
|
|
{
|
|
|
|
$nav = "";
|
|
|
|
foreach ($entries as $entry) {
|
|
|
|
if (array_key_exists('children', $entry)) {
|
2016-04-02 13:39:04 +02:00
|
|
|
|
|
|
|
$icon = '<i class="arrow"> </i>';
|
|
|
|
|
2015-04-23 20:42:44 +02:00
|
|
|
if (array_key_exists('href', $entry)) {
|
2016-04-02 13:39:04 +02:00
|
|
|
$link = '<a href="' . $entry['href'] . '" class="folder">' . $icon . $entry['title'] . '</a>';
|
2015-04-23 20:42:44 +02:00
|
|
|
} else {
|
2016-04-02 13:39:04 +02:00
|
|
|
$link = '<a href="#" class="aj-nav folder">' . $icon . $entry['title'] . '</a>';
|
2015-04-23 20:42:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$link .= $this->renderNavigation($entry['children']);
|
|
|
|
} else {
|
|
|
|
$link = '<a href="' . $entry['href'] . '">' . $entry['title'] . '</a>';
|
|
|
|
}
|
|
|
|
|
|
|
|
$nav .= "<li class='$entry[class]'>$link</li>";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "<ul class='nav nav-list'>$nav</ul>";
|
|
|
|
}
|
|
|
|
|
2015-07-18 14:37:18 +02:00
|
|
|
private function buildNavigation(Directory $tree, $path, $current_url, $base_page, $mode)
|
2015-04-23 10:24:50 +02:00
|
|
|
{
|
2015-04-23 20:42:44 +02:00
|
|
|
$nav = [];
|
2015-07-18 14:37:18 +02:00
|
|
|
foreach ($tree->getEntries() as $node) {
|
2015-04-23 10:24:50 +02:00
|
|
|
$url = $node->getUri();
|
2015-07-18 14:37:18 +02:00
|
|
|
if ($node instanceof Content) {
|
2015-11-06 22:44:34 +01:00
|
|
|
if ($node->isIndex()) {
|
2015-04-23 10:24:50 +02:00
|
|
|
continue;
|
|
|
|
}
|
2015-04-23 20:42:44 +02:00
|
|
|
|
2015-04-23 10:24:50 +02:00
|
|
|
$link = ($path === '') ? $url : $path . '/' . $url;
|
2015-04-23 20:42:44 +02:00
|
|
|
|
|
|
|
$nav[] = [
|
|
|
|
'title' => $node->getTitle(),
|
|
|
|
'href' => $base_page . $link,
|
2016-04-02 13:39:04 +02:00
|
|
|
'class' => $current_url === $link ? 'active' : '',
|
2015-04-23 20:42:44 +02:00
|
|
|
];
|
2015-07-18 20:52:14 +02:00
|
|
|
} elseif ($node instanceof Directory) {
|
2015-07-29 08:30:41 +02:00
|
|
|
if (!$node->hasContent()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-04-23 10:24:50 +02:00
|
|
|
$link = ($path === '') ? $url : $path . '/' . $url;
|
2015-04-23 20:42:44 +02:00
|
|
|
|
|
|
|
$folder = [
|
|
|
|
'title' => $node->getTitle(),
|
2016-04-02 13:39:04 +02:00
|
|
|
'class' => strpos($current_url, $link) === 0 ? 'open' : '',
|
2015-04-23 20:42:44 +02:00
|
|
|
];
|
|
|
|
|
2015-04-27 11:57:44 +02:00
|
|
|
if ($mode === Daux::STATIC_MODE) {
|
2015-04-23 10:24:50 +02:00
|
|
|
$link .= "/index.html";
|
|
|
|
}
|
2015-04-23 20:42:44 +02:00
|
|
|
|
2015-04-23 10:24:50 +02:00
|
|
|
if ($node->getIndexPage()) {
|
2015-04-23 20:42:44 +02:00
|
|
|
$folder['href'] = $base_page . $link;
|
2015-04-23 10:24:50 +02:00
|
|
|
}
|
2015-04-23 20:42:44 +02:00
|
|
|
|
|
|
|
//Child pages
|
2015-04-23 10:24:50 +02:00
|
|
|
$new_path = ($path === '') ? $url : $path . '/' . $url;
|
2015-04-23 20:42:44 +02:00
|
|
|
$folder['children'] = $this->buildNavigation($node, $new_path, $current_url, $base_page, $mode);
|
|
|
|
|
2016-04-02 13:39:04 +02:00
|
|
|
if (!empty($folder['children'])) {
|
|
|
|
$folder['class'] .= ' has-children';
|
|
|
|
}
|
|
|
|
|
2015-04-23 20:42:44 +02:00
|
|
|
$nav[] = $folder;
|
2015-04-23 10:24:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $nav;
|
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* @param string $separator
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-04-23 10:24:50 +02:00
|
|
|
private function getSeparator($separator)
|
|
|
|
{
|
|
|
|
switch ($separator) {
|
|
|
|
case 'Chevrons':
|
|
|
|
return ' <i class="glyphicon glyphicon-chevron-right"></i> ';
|
|
|
|
default:
|
|
|
|
return $separator;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|