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

219 lines
6.6 KiB
PHP
Raw Normal View History

2020-02-09 23:02:19 +01:00
<?php
2020-04-04 01:15:59 +02:00
namespace 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 bool $searchable;
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->searchable = false;
$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 isSearchable(): bool { return $this->searchable; }
public function getSiteName(): string {
// what a chain lol
return $this->getDocument()->getUser()->getConfiguration()->getSettings()->getSiteName();
}
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) {
error_log($e->getMessage());
}
return "";
}
2020-02-09 23:02:19 +01:00
private function loadLanguageModules() {
$lang = $this->document->getUser()->getLanguage();
foreach($this->langModules as $langModule) {
$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 Meta Data + Head (title, scripts, includes, ...)
if($this->loadView) {
$this->loadView();
}
return '';
}
// UI Functions
2021-04-03 13:22:23 +02:00
private function createList($items, $tag, $classes = ""): string {
$class = ($classes ? " class=\"$classes\"" : "");
if(count($items) === 0) {
return "<$tag$class></$tag>";
} else {
return "<$tag$class><li>" . implode("</li><li>", $items) . "</li></$tag>";
}
2020-02-09 23:02:19 +01:00
}
2021-04-03 13:22:23 +02:00
public function createOrderedList($items=array(), $classes = ""): string {
return $this->createList($items, "ol", $classes);
2020-02-09 23:02:19 +01:00
}
2021-04-03 13:22:23 +02:00
public function createUnorderedList($items=array(), $classes = ""): string {
return $this->createList($items, "ul", $classes);
2020-02-09 23:02:19 +01:00
}
2021-04-02 21:58:06 +02:00
protected function createLink($link, $title=null, $classes=""): string {
2020-02-09 23:02:19 +01:00
if(is_null($title)) $title=$link;
2021-01-07 15:54:19 +01:00
if(!empty($classes)) $classes = " class=\"$classes\"";
return "<a href=\"$link\"$classes>$title</a>";
2020-02-09 23:02:19 +01:00
}
2021-04-02 21:58:06 +02:00
protected function createExternalLink($link, $title=null): string {
2020-02-09 23:02:19 +01:00
if(is_null($title)) $title=$link;
return "<a href=\"$link\" target=\"_blank\" rel=\"noopener noreferrer\" class=\"external\">$title</a>";
2020-02-09 23:02:19 +01:00
}
2021-04-02 21:58:06 +02:00
protected function createIcon($icon, $type = "fas", $classes = ""): string {
2020-04-03 22:10:21 +02:00
$iconClass = "$type fa-$icon";
2020-02-09 23:02:19 +01:00
2020-06-25 16:54:58 +02:00
if($icon === "spinner" || $icon === "circle-notch")
2020-04-03 22:10:21 +02:00
$iconClass .= " fa-spin";
2020-02-09 23:02:19 +01:00
2020-04-03 22:10:21 +02:00
if($classes)
$iconClass .= " $classes";
2020-02-09 23:02:19 +01:00
2020-06-20 18:35:43 +02:00
return "<i class=\"$iconClass\" ></i>";
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);
}
2021-04-02 21:58:06 +02:00
protected function createStatusText($type, $text, $id="", $hidden=false, $classes=""): string {
2020-02-09 23:02:19 +01:00
if(strlen($id) > 0) $id = " id=\"$id\"";
2021-01-07 15:54:19 +01:00
if($hidden) $classes .= " hidden";
if(strlen($classes) > 0) $classes = " $classes";
return "<div class=\"alert alert-$type$hidden$classes\" role=\"alert\"$id>$text</div>";
2020-02-09 23:02:19 +01:00
}
2020-04-04 01:15:59 +02:00
2021-04-02 21:58:06 +02:00
protected function createBadge($type, $text): string {
2020-04-04 01:15:59 +02:00
$text = htmlspecialchars($text);
return "<span class=\"badge badge-$type\">$text</span>";
}
2021-01-07 15:54:19 +01:00
2021-04-02 21:58:06 +02:00
protected function createJumbotron(string $content, bool $fluid=false, $class=""): string {
2021-01-07 15:54:19 +01:00
$jumbotronClass = "jumbotron" . ($fluid ? "-fluid" : "");
if (!empty($class)) $jumbotronClass .= " $class";
return "
<div class=\"row\">
<div class=\"col-12\">
<div class=\"$jumbotronClass\">
$content
</div>
</div>
</div>";
}
2021-04-02 21:58:06 +02:00
public function createSimpleParagraph(string $content, string $class=""): string {
2021-01-07 15:54:19 +01:00
if($class) $class = " class=\"$class\"";
return "<p$class>$content</p>";
}
2021-04-02 21:58:06 +02:00
public function createParagraph($title, $id, $content): string {
2021-01-07 15:54:19 +01:00
$id = replaceCssSelector($id);
$iconId = urlencode("$id-icon");
return "
<div class=\"row mt-4\">
<div class=\"col-12\">
<h2 id=\"$id\" data-target=\"$iconId\" class=\"inlineLink\">$title</h2>
<hr/>
$content
</div>
</div>";
}
2021-04-02 21:58:06 +02:00
protected function createBootstrapTable($data, string $classes=""): string {
2021-01-07 15:54:19 +01:00
$classes = empty($classes) ? "" : " $classes";
$code = "<div class=\"container$classes\">";
foreach($data as $row) {
$code .= "<div class=\"row mt-2 mb-2\">";
$columnCount = count($row);
if($columnCount > 0) {
$remainingSize = 12;
$columnSize = 12 / $columnCount;
foreach($row as $col) {
$size = ($columnSize <= $remainingSize ? $columnSize : $remainingSize);
$content = $col;
$class = "";
$code .= "<div";
if(is_array($col)) {
$content = "";
foreach($col as $key => $val) {
if(strcmp($key, "content") === 0) {
$content = $val;
} else if(strcmp($key, "class") === 0) {
$class = " " . $col["class"];
} else if(strcmp($key, "cols") === 0 && is_numeric($val)) {
$size = intval($val);
} else {
$code .= " $key=\"$val\"";
}
}
if(isset($col["class"])) $class = " " . $col["class"];
}
if($size <= 6) $class .= " col-md-" . intval($size * 2);
$code .= " class=\"col-lg-$size$class\">$content</div>";
$remainingSize -= $size;
}
}
$code .= "</div>";
}
$code .= "</div>";
return $code;
}
}