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

119 lines
3.3 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 $reference;
protected string $title;
protected array $langModules;
2020-02-09 23:02:19 +01:00
2020-04-03 15:56:04 +02:00
public function __construct(Document $document, $loadView = true) {
2020-02-09 23:02:19 +01:00
$this->document = $document;
$this->searchable = false;
$this->reference = "";
$this->title = "Untitled View";
$this->langModules = array();
$this->loadView = $loadView;
}
public function getTitle() { return $this->title; }
public function getDocument() { return $this->document; }
public function isSearchable() { return $this->searchable; }
public function getReference() { return $this->reference; }
private function loadLanguageModules() {
$lang = $this->document->getUser()->getLanguage();
foreach($this->langModules as $langModule) {
$lang->loadModule($langModule);
}
}
// Virtual Methods
public function loadView() { }
public function getCode() {
// Load translations
$this->loadLanguageModules();
// Load Meta Data + Head (title, scripts, includes, ...)
if($this->loadView) {
$this->loadView();
}
return '';
}
// UI Functions
private function createList($items, $tag) {
if(count($items) === 0)
return "<$tag></$tag>";
else
return "<$tag><li>" . implode("</li><li>", $items) . "</li></$tag>";
}
public function createOrderedList($items=array()) {
return $this->createList($items, "ol");
}
public function createUnorderedList($items=array()) {
return $this->createList($items, "ul");
}
protected function createLink($link, $title=null) {
if(is_null($title)) $title=$link;
return "<a href=\"$link\">$title</a>";
}
protected function createExternalLink($link, $title=null) {
if(is_null($title)) $title=$link;
return "<a href=\"$link\" target=\"_blank\" class=\"external\">$title</a>";
}
2020-04-03 22:10:21 +02:00
protected function createIcon($icon, $type = "fas", $classes = "") {
$iconClass = "$type fa-$icon";
2020-02-09 23:02:19 +01:00
if($icon === "spinner")
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 15:49:53 +02:00
return "<i class=\"$iconClass\" />";
2020-02-09 23:02:19 +01:00
}
protected function createErrorText($text, $id="", $hidden=false) {
return $this->createStatusText("danger", $text, $id, $hidden);
}
protected function createWarningText($text, $id="", $hidden=false) {
return $this->createStatusText("warning", $text, $id, $hidden);
}
protected function createSuccessText($text, $id="", $hidden=false) {
return $this->createStatusText("success", $text, $id, $hidden);
}
protected function createSecondaryText($text, $id="", $hidden=false) {
return $this->createStatusText("secondary", $text, $id, $hidden);
}
protected function createInfoText($text, $id="", $hidden=false) {
return $this->createStatusText("info", $text, $id, $hidden);
}
protected function createStatusText($type, $text, $id="", $hidden=false) {
if(strlen($id) > 0) $id = " id=\"$id\"";
$hidden = ($hidden?" hidden" : "");
return "<div class=\"alert alert-$type$hidden\" role=\"alert\"$id>$text</div>";
}
2020-04-04 01:15:59 +02:00
protected function createBadge($type, $text) {
$text = htmlspecialchars($text);
return "<span class=\"badge badge-$type\">$text</span>";
}
2020-04-03 15:56:04 +02:00
}