Template   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Test Coverage

Coverage 25.77%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 96
c 2
b 0
f 0
dl 0
loc 197
ccs 25
cts 97
cp 0.2577
rs 9.76
wmc 33

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getEngine() 0 27 4
A getSeparator() 0 7 2
A __construct() 0 3 1
A render() 0 16 1
C buildNavigation() 0 44 12
A renderNavigation() 0 22 4
B registerFunctions() 0 45 9
1
<?php
2
3
namespace Todaymade\Daux\Format\HTML;
4
5
use League\Plates\Engine;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Todaymade\Daux\Config as GlobalConfig;
8
use Todaymade\Daux\Daux;
9
use Todaymade\Daux\Tree\Content;
10
use Todaymade\Daux\Tree\Directory;
11
12
class Template
13
{
14
    protected $engine;
15
16
    protected $config;
17
18
    /**
19
     * @param string $base
20
     * @param string $theme
21
     */
22 3
    public function __construct(GlobalConfig $config)
23
    {
24 3
        $this->config = $config;
25 3
    }
26
27 2
    public function getEngine(GlobalConfig $config)
28
    {
29 2
        if ($this->engine) {
30
            return $this->engine;
31
        }
32
33 2
        $base = $config->getTemplates();
34 2
        $theme = $config->getTheme()->getTemplates();
35
36
        // Use internal templates if no templates
37
        // dir exists in the working directory
38 2
        if (!is_dir($base)) {
39 2
            $base = __DIR__ . '/../../../templates';
40
        }
41
42
        // Create new Plates instance
43 2
        $this->engine = new Engine($base);
44 2
        if (!is_dir($theme)) {
45 2
            $theme = $base;
46
        }
47 2
        $this->engine->addFolder('theme', $theme, true);
48
49 2
        Daux::writeln("Starting Template engine with basedir '$base' and theme folder '$theme'.", OutputInterface::VERBOSITY_VERBOSE);
50
51 2
        $this->registerFunctions($this->engine);
52
53 2
        return $this->engine;
54
    }
55
56
    /**
57
     * @param string $name
58
     *
59
     * @return string
60
     */
61
    public function render($name, array $data = [])
62
    {
63
        $engine = $this->getEngine($data['config']);
64
65
        $engine->addData([
66
            'base_url' => $data['config']->getBaseUrl(),
67
            'base_page' => $data['config']->getBasePage(),
68
            'page' => $data['page'],
69
            'params' => $data['config'], // legacy name for config
70
            'config' => $data['config'],
71
            'tree' => $data['config']['tree'],
72
        ]);
73
74
        Daux::writeln("Rendering template '$name'", OutputInterface::VERBOSITY_VERBOSE);
75
76
        return $engine->render($name, $data);
77
    }
78
79 2
    protected function registerFunctions($engine)
80
    {
81
        $engine->registerFunction('get_navigation', function ($tree, $path, $current_url, $base_page, $mode) {
82
            $nav = $this->buildNavigation($tree, $path, $current_url, $base_page, $mode);
83
84
            return $this->renderNavigation($nav);
85 2
        });
86
87
        $engine->registerFunction('translate', function ($key) {
88 2
            $language = $this->config->getLanguage();
89
90 2
            if (isset($this->engine->getData('page')['page'])) {
91
                $page = $this->engine->getData('page');
92
                if (!empty($page['page']['language'])) {
93
                    $language = $page['page']['language'];
94
                }
95
            }
96
97 2
            if ($this->config->hasTranslationKey($language, $key)) {
98 2
                return $this->config->getTranslationKey($language, $key);
99
            }
100
101
            if ($this->config->hasTranslationKey('en', $key)) {
102
                return $this->config->getTranslationKey('en', $key);
103
            }
104
105
            return "Unknown key $key";
106 2
        });
107
108
        $engine->registerFunction('get_breadcrumb_title', function ($page, $base_page) {
109
            $title = '';
110
            $breadcrumb_trail = $page['breadcrumb_trail'];
111
            $separator = $this->getSeparator($page['breadcrumb_separator']);
112
            foreach ($breadcrumb_trail as $value) {
113
                $title .= '<a href="' . $base_page . $value['url'] . '">' . $value['title'] . '</a>' . $separator;
114
            }
115
            if ($page['filename'] === 'index' || $page['filename'] === '_index') {
116
                if ($page['title'] != '') {
117
                    $title = substr($title, 0, -1 * strlen($separator));
118
                }
119
            } else {
120
                $title .= '<a href="' . $base_page . $page['request'] . '">' . $page['title'] . '</a>';
121
            }
122
123
            return $title;
124 2
        });
125 2
    }
126
127
    private function renderNavigation($entries)
128
    {
129
        $nav = '';
130
        foreach ($entries as $entry) {
131
            if (array_key_exists('children', $entry)) {
132
                $icon = '<i class="Nav__arrow">&nbsp;</i>';
133
134
                if (array_key_exists('href', $entry)) {
135
                    $link = '<a href="' . $entry['href'] . '" class="Nav__item__link">' . $icon . $entry['title'] . '</a>';
136
                } else {
137
                    $link = '<a href="#" class="Nav__item__link Nav__item__link--nopage">' . $icon . $entry['title'] . '</a>';
138
                }
139
140
                $link .= $this->renderNavigation($entry['children']);
141
            } else {
142
                $link = '<a href="' . $entry['href'] . '">' . $entry['title'] . '</a>';
143
            }
144
145
            $nav .= "<li class='Nav__item $entry[class]'>$link</li>";
146
        }
147
148
        return "<ul class='Nav'>$nav</ul>";
149
    }
150
151
    private function buildNavigation(Directory $tree, $path, $current_url, $base_page, $mode)
152
    {
153
        $nav = [];
154
        foreach ($tree->getEntries() as $node) {
155
            $url = $node->getUri();
156
            if ($node instanceof Content) {
157
                if ($node->isIndex()) {
158
                    continue;
159
                }
160
161
                $link = ($path === '') ? $url : $path . '/' . $url;
162
163
                $nav[] = [
164
                    'title' => $node->getTitle(),
165
                    'href' => $base_page . $link,
166
                    'class' => $node->isHotPath() ? 'Nav__item--active' : '',
167
                ];
168
            } elseif ($node instanceof Directory) {
169
                if (!$node->hasContent()) {
170
                    continue;
171
                }
172
173
                $folder = [
174
                    'title' => $node->getTitle(),
175
                    'class' => $node->isHotPath() ? 'Nav__item--open' : '',
176
                ];
177
178
                if ($index = $node->getIndexPage()) {
179
                    $folder['href'] = $base_page . $index->getUrl();
180
                }
181
182
                //Child pages
183
                $new_path = ($path === '') ? $url : $path . '/' . $url;
184
                $folder['children'] = $this->buildNavigation($node, $new_path, $current_url, $base_page, $mode);
185
186
                if (!empty($folder['children'])) {
187
                    $folder['class'] .= ' has-children';
188
                }
189
190
                $nav[] = $folder;
191
            }
192
        }
193
194
        return $nav;
195
    }
196
197
    /**
198
     * @param string $separator
199
     *
200
     * @return string
201
     */
202
    private function getSeparator($separator)
203
    {
204
        switch ($separator) {
205
            case 'Chevrons':
206
                return ' <svg class="Page__header--separator" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 477.175 477.175"><path d="M360.73 229.075l-225.1-225.1c-5.3-5.3-13.8-5.3-19.1 0s-5.3 13.8 0 19.1l215.5 215.5-215.5 215.5c-5.3 5.3-5.3 13.8 0 19.1 2.6 2.6 6.1 4 9.5 4 3.4 0 6.9-1.3 9.5-4l225.1-225.1c5.3-5.2 5.3-13.8.1-19z"/></svg> ';
207
            default:
208
                return $separator;
209
        }
210
    }
211
}
212