1.5.2: html functions, DB Row Iterator, and more
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace Elements;
|
||||
|
||||
use Configuration\Settings;
|
||||
use Driver\Logger\Logger;
|
||||
use Driver\SQL\SQL;
|
||||
use Objects\Router\Router;
|
||||
use Objects\User;
|
||||
@@ -10,6 +11,7 @@ use Objects\User;
|
||||
abstract class Document {
|
||||
|
||||
protected Router $router;
|
||||
private Logger $logger;
|
||||
protected bool $databaseRequired;
|
||||
private bool $cspEnabled;
|
||||
private ?string $cspNonce;
|
||||
@@ -23,6 +25,11 @@ abstract class Document {
|
||||
$this->databaseRequired = true;
|
||||
$this->cspWhitelist = [];
|
||||
$this->domain = $this->getSettings()->getBaseUrl();
|
||||
$this->logger = new Logger("Document", $this->getSQL());
|
||||
}
|
||||
|
||||
public function getLogger(): Logger {
|
||||
return $this->logger;
|
||||
}
|
||||
|
||||
public function getUser(): User {
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Elements\Body;
|
||||
|
||||
class EmptyBody extends Body {
|
||||
|
||||
}
|
||||
@@ -69,40 +69,42 @@ abstract class Head extends View {
|
||||
}
|
||||
|
||||
public function getCode(): string {
|
||||
$header = "<head>";
|
||||
$content = [];
|
||||
|
||||
foreach($this->metas as $aMeta) {
|
||||
$header .= '<meta';
|
||||
foreach($aMeta as $key => $val) {
|
||||
$header .= " $key=\"$val\"";
|
||||
}
|
||||
$header .= ' />';
|
||||
// meta tags
|
||||
foreach($this->metas as $meta) {
|
||||
$content[] = html_tag_short("meta", $meta);
|
||||
}
|
||||
|
||||
// description
|
||||
if(!empty($this->description)) {
|
||||
$header .= "<meta name=\"description\" content=\"$this->description\" />";
|
||||
$content[] = html_tag_short("meta", ["name" => "description", "content" => $this->description]);
|
||||
}
|
||||
|
||||
// keywords
|
||||
if(!empty($this->keywords)) {
|
||||
$keywords = implode(", ", $this->keywords);
|
||||
$header .= "<meta name=\"keywords\" content=\"$keywords\" />";
|
||||
$content[] = html_tag_short("meta", ["name" => "keywords", "content" => $keywords]);
|
||||
}
|
||||
|
||||
// base tag
|
||||
if(!empty($this->baseUrl)) {
|
||||
$header .= "<base href=\"$this->baseUrl\">";
|
||||
$content[] = html_tag_short("base", ["href" => $this->baseUrl]);
|
||||
}
|
||||
|
||||
$header .= "<title>$this->title</title>";
|
||||
// title
|
||||
$content[] = html_tag("title", [], $this->title);
|
||||
|
||||
// src tags
|
||||
foreach($this->sources as $src) {
|
||||
$header .= $src->getCode();
|
||||
$content[] = $src->getCode();
|
||||
}
|
||||
|
||||
foreach($this->rawFields as $raw) {
|
||||
$header .= $raw;
|
||||
//
|
||||
foreach ($this->rawFields as $raw) {
|
||||
$content[] = $raw;
|
||||
}
|
||||
|
||||
$header .= "</head>";
|
||||
return $header;
|
||||
return html_tag("head", [], $content, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ class HtmlDocument extends Document {
|
||||
|
||||
$view = parseClass($this->activeView);
|
||||
$file = getClassPath($view);
|
||||
if(!file_exists($file) || !is_subclass_of($view, View::class)) {
|
||||
if (!file_exists($file) || !is_subclass_of($view, View::class)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -67,12 +67,10 @@ class HtmlDocument extends Document {
|
||||
$head = $this->head->getCode();
|
||||
$lang = $this->getUser()->getLanguage()->getShortCode();
|
||||
|
||||
$html = "<!DOCTYPE html>";
|
||||
$html .= "<html lang=\"$lang\">";
|
||||
$html .= $head;
|
||||
$html .= $body;
|
||||
$html .= "</html>";
|
||||
return $html;
|
||||
$code = "<!DOCTYPE html>";
|
||||
$code .= html_tag("html", ["lang" => $lang], $head . $body, false);
|
||||
|
||||
return $code;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -34,8 +34,7 @@ class Link extends StaticView {
|
||||
$attributes["nonce"] = $this->nonce;
|
||||
}
|
||||
|
||||
$attributes = html_attributes($attributes);
|
||||
return "<link $attributes/>";
|
||||
return html_tag_short("link", $attributes);
|
||||
}
|
||||
|
||||
public function setNonce(string $nonce) {
|
||||
|
||||
@@ -35,8 +35,8 @@ class Script extends StaticView {
|
||||
$attributes["nonce"] = $this->nonce;
|
||||
}
|
||||
|
||||
$attributes = html_attributes($attributes);
|
||||
return "<script $attributes>$this->content</script>";
|
||||
// TODO: do we need to escape the content here?
|
||||
return html_tag("script", $attributes, $this->content, false);
|
||||
}
|
||||
|
||||
public function setNonce(string $nonce) {
|
||||
|
||||
@@ -10,7 +10,7 @@ abstract class SimpleBody extends Body {
|
||||
|
||||
public function getCode(): string {
|
||||
$content = $this->getContent();
|
||||
return parent::getCode() . "<body>$content</body>";
|
||||
return html_tag("body", [], $content, false);
|
||||
}
|
||||
|
||||
protected abstract function getContent(): string;
|
||||
|
||||
@@ -11,6 +11,7 @@ class Style extends StaticView {
|
||||
}
|
||||
|
||||
function getCode(): string {
|
||||
return "<style>$this->style</style>";
|
||||
// TODO: do we need to escape the content here?
|
||||
return html_tag("style", [], $this->style, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ abstract class View extends StaticView {
|
||||
return $view;
|
||||
}
|
||||
} catch(\ReflectionException $e) {
|
||||
error_log($e->getMessage());
|
||||
$this->document->getLogger()->error("Error loading view: '$viewClass': " . $e->getMessage());
|
||||
}
|
||||
|
||||
return "";
|
||||
@@ -44,7 +44,7 @@ abstract class View extends StaticView {
|
||||
|
||||
private function loadLanguageModules() {
|
||||
$lang = $this->document->getUser()->getLanguage();
|
||||
foreach($this->langModules as $langModule) {
|
||||
foreach ($this->langModules as $langModule) {
|
||||
$lang->loadModule($langModule);
|
||||
}
|
||||
}
|
||||
@@ -57,7 +57,7 @@ abstract class View extends StaticView {
|
||||
// Load translations
|
||||
$this->loadLanguageModules();
|
||||
|
||||
// Load Meta Data + Head (title, scripts, includes, ...)
|
||||
// Load metadata + head (title, scripts, includes, ...)
|
||||
if($this->loadView) {
|
||||
$this->loadView();
|
||||
}
|
||||
@@ -66,46 +66,46 @@ abstract class View extends StaticView {
|
||||
}
|
||||
|
||||
// UI Functions
|
||||
private function createList($items, $tag, $classes = ""): string {
|
||||
private function createList(array $items, string $tag, array $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>";
|
||||
$attributes = [];
|
||||
if (!empty($classes)) {
|
||||
$attributes["class"] = implode(" ", $classes);
|
||||
}
|
||||
|
||||
$content = array_map(function ($item) { html_tag("li", [], $item, false); }, $items);
|
||||
return html_tag_ex($tag, $attributes, $content, false);
|
||||
}
|
||||
|
||||
public function createOrderedList($items=array(), $classes = ""): string {
|
||||
public function createOrderedList(array $items=[], array $classes=[]): string {
|
||||
return $this->createList($items, "ol", $classes);
|
||||
}
|
||||
|
||||
public function createUnorderedList($items=array(), $classes = ""): string {
|
||||
public function createUnorderedList(array $items=[], array $classes=[]): string {
|
||||
return $this->createList($items, "ul", $classes);
|
||||
}
|
||||
|
||||
protected function createLink($link, $title=null, $classes=""): string {
|
||||
if(is_null($title)) $title=$link;
|
||||
if(!empty($classes)) $classes = " class=\"$classes\"";
|
||||
return "<a href=\"$link\"$classes>$title</a>";
|
||||
protected function createLink(string $link, $title=null, array $classes=[], bool $escapeTitle=true): string {
|
||||
$attrs = ["href" => $link];
|
||||
if (!empty($classes)) {
|
||||
$attrs["class"] = implode(" ", $classes);
|
||||
}
|
||||
|
||||
return html_tag("a", $attrs, $title ?? $link, $escapeTitle);
|
||||
}
|
||||
|
||||
protected function createExternalLink($link, $title=null): string {
|
||||
if(is_null($title)) $title=$link;
|
||||
return "<a href=\"$link\" target=\"_blank\" rel=\"noopener noreferrer\" class=\"external\">$title</a>";
|
||||
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);
|
||||
}
|
||||
|
||||
protected function createIcon($icon, $type = "fas", $classes = ""): string {
|
||||
$iconClass = "$type fa-$icon";
|
||||
protected function createIcon($icon, $type="fas", $classes = []): string {
|
||||
$classes = array_merge($classes, [$type, "fa-$icon"]);
|
||||
if ($icon === "spinner" || $icon === "circle-notch") {
|
||||
$classes[] = "fa-spin";
|
||||
}
|
||||
|
||||
if($icon === "spinner" || $icon === "circle-notch")
|
||||
$iconClass .= " fa-spin";
|
||||
|
||||
if($classes)
|
||||
$iconClass .= " $classes";
|
||||
|
||||
return "<i class=\"$iconClass\" ></i>";
|
||||
return html_tag("i", ["class" => implode(" ", $classes)]);
|
||||
}
|
||||
|
||||
protected function createErrorText($text, $id="", $hidden=false): string {
|
||||
@@ -128,87 +128,23 @@ abstract class View extends StaticView {
|
||||
return $this->createStatusText("info", $text, $id, $hidden);
|
||||
}
|
||||
|
||||
protected function createStatusText($type, $text, $id="", $hidden=false, $classes=""): string {
|
||||
if(strlen($id) > 0) $id = " id=\"$id\"";
|
||||
if($hidden) $classes .= " hidden";
|
||||
if(strlen($classes) > 0) $classes = " $classes";
|
||||
return "<div class=\"alert alert-$type$hidden$classes\" role=\"alert\"$id>$text</div>";
|
||||
}
|
||||
protected function createStatusText(string $type, $text, string $id="", bool $hidden=false, array $classes=[]): string {
|
||||
$classes[] = "alert";
|
||||
$classes[] = "alert-$type";
|
||||
|
||||
protected function createBadge($type, $text): string {
|
||||
$text = htmlspecialchars($text);
|
||||
return "<span class=\"badge badge-$type\">$text</span>";
|
||||
}
|
||||
|
||||
protected function createJumbotron(string $content, bool $fluid=false, $class=""): string {
|
||||
$jumbotronClass = "jumbotron" . ($fluid ? " jumbotron-fluid" : "");
|
||||
if (!empty($class)) $jumbotronClass .= " $class";
|
||||
|
||||
return
|
||||
"<div class=\"$jumbotronClass\">
|
||||
$content
|
||||
</div>";
|
||||
}
|
||||
|
||||
public function createSimpleParagraph(string $content, string $class=""): string {
|
||||
if($class) $class = " class=\"$class\"";
|
||||
return "<p$class>$content</p>";
|
||||
}
|
||||
|
||||
public function createParagraph($title, $id, $content): string {
|
||||
$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>";
|
||||
}
|
||||
|
||||
protected function createBootstrapTable($data, string $classes=""): string {
|
||||
$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>";
|
||||
if ($hidden) {
|
||||
$classes[] = "hidden";
|
||||
}
|
||||
|
||||
$code .= "</div>";
|
||||
return $code;
|
||||
$attributes = [
|
||||
"class" => implode(" ", $classes),
|
||||
"role" => "alert"
|
||||
];
|
||||
|
||||
if (!empty($id)) {
|
||||
$attributes["id"] = $id;
|
||||
}
|
||||
|
||||
return html_tag("div", $attributes, $text, false);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user