web-base/Core/Elements/View.class.php

147 lines
4.3 KiB
PHP
Raw Normal View History

2020-02-09 23:02:19 +01:00
<?php
2022-11-18 18:06:46 +01:00
namespace Core\Elements;
2020-04-03 15:56:04 +02:00
2020-04-04 01:15:59 +02:00
abstract class View extends StaticView {
2020-02-09 23:02:19 +01:00
2020-04-03 15:56:04 +02:00
private Document $document;
private bool $loadView;
protected string $title;
protected array $langModules;
2020-02-09 23:02:19 +01:00
2021-04-02 21:58:06 +02:00
public function __construct(Document $document, bool $loadView = true) {
2020-02-09 23:02:19 +01:00
$this->document = $document;
$this->title = "Untitled View";
$this->langModules = array();
$this->loadView = $loadView;
}
2021-04-02 21:58:06 +02:00
public function getTitle(): string { return $this->title; }
public function getDocument(): Document { return $this->document; }
public function getSiteName(): string {
2022-06-20 19:52:31 +02:00
return $this->getDocument()->getSettings()->getSiteName();
2021-04-02 21:58:06 +02:00
}
2020-02-09 23:02:19 +01:00
2020-06-22 19:09:02 +02:00
protected function load(string $viewClass) : string {
try {
$reflectionClass = new \ReflectionClass($viewClass);
if ($reflectionClass->isSubclassOf(View::class) && $reflectionClass->isInstantiable()) {
$view = $reflectionClass->newInstanceArgs(array($this->getDocument()));
$view->loadView();
return $view;
}
} catch(\ReflectionException $e) {
$this->document->getLogger()->error("Error loading view: '$viewClass': " . $e->getMessage());
2020-06-22 19:09:02 +02:00
}
return "";
}
2020-02-09 23:02:19 +01:00
private function loadLanguageModules() {
2022-06-20 19:52:31 +02:00
$lang = $this->document->getContext()->getLanguage();
foreach ($this->langModules as $langModule) {
2020-02-09 23:02:19 +01:00
$lang->loadModule($langModule);
}
}
// Virtual Methods
public function loadView() { }
2021-04-02 21:58:06 +02:00
public function getCode(): string {
2020-02-09 23:02:19 +01:00
// Load translations
$this->loadLanguageModules();
// Load metadata + head (title, scripts, includes, ...)
2020-02-09 23:02:19 +01:00
if($this->loadView) {
$this->loadView();
}
return '';
}
// UI Functions
private function createList(array $items, string $tag, array $classes = []): string {
2021-04-03 13:22:23 +02:00
$attributes = [];
if (!empty($classes)) {
$attributes["class"] = implode(" ", $classes);
2021-04-03 13:22:23 +02:00
}
2022-08-20 22:04:09 +02:00
$content = array_map(function ($item) { return html_tag("li", [], $item, false); }, $items);
return html_tag_ex($tag, $attributes, $content, false);
2020-02-09 23:02:19 +01:00
}
public function createOrderedList(array $items=[], array $classes=[]): string {
2021-04-03 13:22:23 +02:00
return $this->createList($items, "ol", $classes);
2020-02-09 23:02:19 +01:00
}
public function createUnorderedList(array $items=[], array $classes=[]): string {
2021-04-03 13:22:23 +02:00
return $this->createList($items, "ul", $classes);
2020-02-09 23:02:19 +01:00
}
protected function createLink(string $link, $title=null, array $classes=[], bool $escapeTitle=true): string {
$attrs = ["href" => $link];
if (!empty($classes)) {
$attrs["class"] = implode(" ", $classes);
}
2020-02-09 23:02:19 +01:00
return html_tag("a", $attrs, $title ?? $link, $escapeTitle);
2020-02-09 23:02:19 +01:00
}
protected function createExternalLink(string $link, $title=null, bool $escapeTitle=true): string {
$attrs = ["href" => $link, "target" => "_blank", "rel" => "noopener noreferrer", "class" => "external"];
return html_tag("a", $attrs, $title ?? $link, $escapeTitle);
}
2020-02-09 23:02:19 +01:00
protected function createIcon($icon, $type="fas", $classes = []): string {
$classes = array_merge($classes, [$type, "fa-$icon"]);
if ($icon === "spinner" || $icon === "circle-notch") {
$classes[] = "fa-spin";
}
2020-02-09 23:02:19 +01:00
return html_tag("i", ["class" => implode(" ", $classes)]);
2020-02-09 23:02:19 +01:00
}
2021-04-02 21:58:06 +02:00
protected function createErrorText($text, $id="", $hidden=false): string {
2020-02-09 23:02:19 +01:00
return $this->createStatusText("danger", $text, $id, $hidden);
}
2021-04-02 21:58:06 +02:00
protected function createWarningText($text, $id="", $hidden=false): string {
2020-02-09 23:02:19 +01:00
return $this->createStatusText("warning", $text, $id, $hidden);
}
2021-04-02 21:58:06 +02:00
protected function createSuccessText($text, $id="", $hidden=false): string {
2020-02-09 23:02:19 +01:00
return $this->createStatusText("success", $text, $id, $hidden);
}
2021-04-02 21:58:06 +02:00
protected function createSecondaryText($text, $id="", $hidden=false): string {
2020-02-09 23:02:19 +01:00
return $this->createStatusText("secondary", $text, $id, $hidden);
}
2021-04-02 21:58:06 +02:00
protected function createInfoText($text, $id="", $hidden=false): string {
2020-02-09 23:02:19 +01:00
return $this->createStatusText("info", $text, $id, $hidden);
}
protected function createStatusText(string $type, $text, string $id="", bool $hidden=false, array $classes=[]): string {
$classes[] = "alert";
$classes[] = "alert-$type";
if ($hidden) {
$classes[] = "hidden";
}
$attributes = [
"class" => implode(" ", $classes),
"role" => "alert"
];
if (!empty($id)) {
$attributes["id"] = $id;
2021-01-07 15:54:19 +01:00
}
return html_tag("div", $attributes, $text, false);
2021-01-07 15:54:19 +01:00
}
}