|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2009-2020 Michael Steenbeek |
| 4 | + * |
| 5 | + * Cyndaron is licensed under the MIT License. See the LICENSE file for more details. |
| 6 | + */ |
| 7 | +/** @noinspection PhpIncludeInspection */ |
| 8 | + |
| 9 | +namespace Cyndaron\View; |
| 10 | + |
| 11 | +use Cyndaron\Category\ModelWithCategory; |
| 12 | +use Cyndaron\CyndaronInfo; |
| 13 | +use Cyndaron\Menu\MenuItem; |
| 14 | +use Cyndaron\Util\Setting; |
| 15 | +use Cyndaron\View\Template\ViewHelpers; |
| 16 | +use Cyndaron\User\User; |
| 17 | +use Cyndaron\DBAL\Model; |
| 18 | + |
| 19 | +use function Safe\sprintf; |
| 20 | +use function Safe\substr; |
| 21 | +use function assert; |
| 22 | +use function dirname; |
| 23 | +use function str_replace; |
| 24 | +use function basename; |
| 25 | +use function strrchr; |
| 26 | +use function file_exists; |
| 27 | +use function ob_start; |
| 28 | +use function ob_get_clean; |
| 29 | +use function count; |
| 30 | +use function array_merge; |
| 31 | + |
| 32 | +class Page |
| 33 | +{ |
| 34 | + public const DEFAULT_SCRIPTS = [ |
| 35 | + '/vendor/components/jquery/jquery.min.js', |
| 36 | + '/vendor/twbs/bootstrap/dist/js/bootstrap.min.js', |
| 37 | + '/js/cyndaron.js', |
| 38 | + ]; |
| 39 | + |
| 40 | + protected string $extraMeta = ''; |
| 41 | + protected string $title = ''; |
| 42 | + protected array $extraScripts = []; |
| 43 | + protected array $extraCss = []; |
| 44 | + protected string $websiteName = ''; |
| 45 | + protected string $body = ''; |
| 46 | + |
| 47 | + protected ?Model $model = null; |
| 48 | + |
| 49 | + protected string $template = ''; |
| 50 | + protected array $templateVars = []; |
| 51 | + |
| 52 | + public function __construct(string $title, string $body = '') |
| 53 | + { |
| 54 | + $this->title = $title; |
| 55 | + $this->body = $body; |
| 56 | + |
| 57 | + $this->updateTemplate(); |
| 58 | + } |
| 59 | + |
| 60 | + protected function updateTemplate(): void |
| 61 | + { |
| 62 | + if (empty($this->template)) |
| 63 | + { |
| 64 | + $rc = new \ReflectionClass(static::class); |
| 65 | + $filename = $rc->getFileName(); |
| 66 | + assert($filename !== false); |
| 67 | + $dir = dirname($filename) . '/templates'; |
| 68 | + |
| 69 | + $file = str_replace('.php', '.blade.php', basename($filename)); |
| 70 | + $testPath = "$dir/$file"; |
| 71 | + |
| 72 | + if (file_exists($testPath)) |
| 73 | + { |
| 74 | + $this->template = $testPath; |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + $this->template = 'Index'; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + protected function renderSkeleton(): void |
| 84 | + { |
| 85 | + $this->websiteName = Setting::get('siteName'); |
| 86 | + $this->templateVars['isAdmin'] = User::isAdmin(); |
| 87 | + $this->templateVars['websiteName'] = $this->websiteName; |
| 88 | + $this->templateVars['title'] = $this->title; |
| 89 | + $this->templateVars['referrer'] = $_SESSION['referrer'] ?? ''; |
| 90 | + $this->templateVars['previewImage'] = ''; |
| 91 | + if ($this->model instanceof ModelWithCategory) |
| 92 | + { |
| 93 | + $this->templateVars['previewImage'] = $this->model->getPreviewImage(); |
| 94 | + } |
| 95 | + |
| 96 | + $this->templateVars['version'] = CyndaronInfo::ENGINE_VERSION; |
| 97 | + $favicon = Setting::get('favicon'); |
| 98 | + $this->templateVars['favicon'] = $favicon; |
| 99 | + if ($favicon !== '') |
| 100 | + { |
| 101 | + $dotPosition = strrchr($favicon, '.'); |
| 102 | + $extension = $dotPosition !== false ? substr($dotPosition, 1) : ''; |
| 103 | + /** @todo Replace with actual mime type check */ |
| 104 | + $this->templateVars['faviconType'] = "image/$extension"; |
| 105 | + } |
| 106 | + |
| 107 | + foreach (['backgroundColor', 'menuColor', 'menuBackground', 'articleColor', 'accentColor'] as $setting) |
| 108 | + { |
| 109 | + $this->templateVars[$setting] = Setting::get($setting); |
| 110 | + } |
| 111 | + |
| 112 | + $this->templateVars['menu'] = $this->renderMenu(); |
| 113 | + |
| 114 | + $jumboContents = Setting::get('jumboContents'); |
| 115 | + $this->templateVars['showJumbo'] = $this->isFrontPage() && Setting::get('frontPageIsJumbo') && $jumboContents; |
| 116 | + $this->templateVars['jumboContents'] = ViewHelpers::parseText($jumboContents); |
| 117 | + |
| 118 | + $this->templateVars['pageCaptionClasses'] = ''; |
| 119 | + if ($this->isFrontPage()) |
| 120 | + { |
| 121 | + $this->templateVars['pageCaptionClasses'] = 'voorpagina'; |
| 122 | + } |
| 123 | + |
| 124 | + $this->templateVars['pageCaption'] = $this->generateBreadcrumbs(); |
| 125 | + |
| 126 | + $this->templateVars['scripts'] = array_merge(self::DEFAULT_SCRIPTS, $this->extraScripts); |
| 127 | + $this->templateVars['extraCss'] = $this->extraCss; |
| 128 | + |
| 129 | + static $includes = [ |
| 130 | + 'extraHead' => 'extra-head', |
| 131 | + 'extraBodyStart' => 'extra-body-start', |
| 132 | + 'extraBodyEnd' => 'extra-body-end' |
| 133 | + ]; |
| 134 | + |
| 135 | + foreach ($includes as $varName => $filename) |
| 136 | + { |
| 137 | + $this->templateVars[$varName] = ''; |
| 138 | + if (file_exists(__DIR__ . "/../$filename.php")) |
| 139 | + { |
| 140 | + ob_start(); |
| 141 | + include __DIR__ . "/../$filename.php"; |
| 142 | + $this->templateVars[$varName] = ViewHelpers::parseText(ob_get_clean() ?: ''); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + public function setExtraMeta(string $extraMeta): void |
| 148 | + { |
| 149 | + $this->extraMeta = $extraMeta; |
| 150 | + } |
| 151 | + |
| 152 | + public function isFrontPage(): bool |
| 153 | + { |
| 154 | + return $_SERVER['REQUEST_URI'] === '/'; |
| 155 | + } |
| 156 | + |
| 157 | + protected function renderMenu(): string |
| 158 | + { |
| 159 | + $logo = Setting::get('logo'); |
| 160 | + $vars = [ |
| 161 | + 'isLoggedIn' => User::isLoggedIn(), |
| 162 | + 'isAdmin' => User::isAdmin(), |
| 163 | + 'inverseClass' => (Setting::get('menuTheme') === 'dark') ? 'navbar-dark' : 'navbar-light', |
| 164 | + 'navbar' => $logo !== '' ? sprintf('<img alt="" src="%s"> ', $logo) : $this->websiteName, |
| 165 | + ]; |
| 166 | + |
| 167 | + $vars['menuItems'] = $this->getMenu(); |
| 168 | + $vars['configMenuItems'] = [ |
| 169 | + ['link' => '/system', 'title' => 'Systeembeheer', 'icon' => 'cog'], |
| 170 | + ['link' => '/pagemanager', 'title' => 'Pagina-overzicht', 'icon' => 'th-list'], |
| 171 | + ['link' => '/menu-editor', 'title' => 'Menu bewerken', 'icon' => 'menu-hamburger'], |
| 172 | + ['link' => '/user/manager', 'title' => 'Gebruikersbeheer', 'icon' => 'user'], |
| 173 | + ]; |
| 174 | + $userMenuItems = [ |
| 175 | + ['link' => '', 'title' => $_SESSION['username'] ?? ''], |
| 176 | + ]; |
| 177 | + foreach (User::getUserMenuFiltered() as $extraItem) |
| 178 | + { |
| 179 | + $userMenuItems[] = ['link' => $extraItem['link'], 'title' => $extraItem['label'], 'icon' => $extraItem['icon'] ?? '']; |
| 180 | + } |
| 181 | + $userMenuItems[] = ['link' => '/user/changePassword', 'title' => 'Wachtwoord wijzigen', 'icon' => 'lock']; |
| 182 | + $userMenuItems[] = ['link' => '/user/logout', 'title' => 'Uitloggen', 'icon' => 'log-out']; |
| 183 | + |
| 184 | + $vars['userMenuItems'] = $userMenuItems; |
| 185 | + |
| 186 | + $vars['notifications'] = User::getNotifications(); |
| 187 | + |
| 188 | + $template = new \Cyndaron\View\Template\Template(); |
| 189 | + return $template->render('Menu', $vars); |
| 190 | + } |
| 191 | + |
| 192 | + public function render(array $vars = []): string |
| 193 | + { |
| 194 | + $this->addTemplateVars($vars); |
| 195 | + |
| 196 | + $this->templateVars['contents'] = $this->body; |
| 197 | + |
| 198 | + $this->renderSkeleton(); |
| 199 | + |
| 200 | + $template = new \Cyndaron\View\Template\Template(); |
| 201 | + return $template->render($this->template, $this->templateVars); |
| 202 | + } |
| 203 | + |
| 204 | + public function addScript(string $filename): void |
| 205 | + { |
| 206 | + $this->extraScripts[] = $filename; |
| 207 | + } |
| 208 | + |
| 209 | + public function addCss(string $filename): void |
| 210 | + { |
| 211 | + $this->extraCss[] = $filename; |
| 212 | + } |
| 213 | + |
| 214 | + public function getMenu(): array |
| 215 | + { |
| 216 | + if (!User::hasSufficientReadLevel()) |
| 217 | + { |
| 218 | + return []; |
| 219 | + } |
| 220 | + return MenuItem::fetchAll([], [], 'ORDER BY priority, id'); |
| 221 | + } |
| 222 | + |
| 223 | + protected function generateBreadcrumbs(): string |
| 224 | + { |
| 225 | + $title = ''; |
| 226 | + $titleParts = [$this->title]; |
| 227 | + if ($this->model instanceof ModelWithCategory) |
| 228 | + { |
| 229 | + $titleParts = []; |
| 230 | + if ($this->model->showBreadcrumbs) |
| 231 | + { |
| 232 | + $category = $this->model->getFirstCategory(); |
| 233 | + if ($category !== null) |
| 234 | + { |
| 235 | + $titleParts[] = $category->name; |
| 236 | + } |
| 237 | + } |
| 238 | + $titleParts[] = $this->model->name; |
| 239 | + } |
| 240 | + |
| 241 | + $count = count($titleParts); |
| 242 | + if ($count === 1) |
| 243 | + { |
| 244 | + $title = $titleParts[0]; |
| 245 | + } |
| 246 | + else |
| 247 | + { |
| 248 | + for ($i = 0; $i < $count; $i++) |
| 249 | + { |
| 250 | + $class = ($i === 0) ? 'breadcrumb-main-item' : 'breadcrumb-item'; |
| 251 | + $title .= sprintf('<span class="%s">%s</span>', $class, $titleParts[$i]); |
| 252 | + if ($i !== $count - 1) |
| 253 | + { |
| 254 | + $title .= '<span class="breadcrumb-separator"> // </span>'; |
| 255 | + } |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + return $title; |
| 260 | + } |
| 261 | + |
| 262 | + /** |
| 263 | + * @param string $varName |
| 264 | + * @param mixed $var |
| 265 | + */ |
| 266 | + public function addTemplateVar(string $varName, $var): void |
| 267 | + { |
| 268 | + $this->templateVars[$varName] = $var; |
| 269 | + } |
| 270 | + |
| 271 | + public function addTemplateVars(array $vars): void |
| 272 | + { |
| 273 | + $this->templateVars = array_merge($this->templateVars, $vars); |
| 274 | + } |
| 275 | +} |
0 commit comments