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

66 lines
1.6 KiB
PHP
Raw Normal View History

2020-02-09 23:02:19 +01:00
<?php
namespace Elements;
2020-04-03 15:56:04 +02:00
use Objects\User;
2020-02-09 23:02:19 +01:00
abstract class Document {
2020-04-03 15:56:04 +02:00
protected Head $head;
protected Body $body;
protected User $user;
protected bool $databaseRequired;
2020-06-19 16:51:41 +02:00
private ?string $activeView;
2020-02-09 23:02:19 +01:00
2020-06-19 16:51:41 +02:00
public function __construct(User $user, $headClass, $bodyClass, ?string $view = NULL) {
2021-04-02 22:48:14 +02:00
$this->user = $user;
2020-02-09 23:02:19 +01:00
$this->head = new $headClass($this);
$this->body = new $bodyClass($this);
2020-04-02 21:39:02 +02:00
$this->databaseRequired = true;
2020-06-19 16:51:41 +02:00
$this->activeView = $view;
2020-02-09 23:02:19 +01:00
}
2021-04-02 22:48:14 +02:00
public function getHead(): Head { return $this->head; }
public function getBody(): Body { return $this->body; }
public function getSQL(): ?\Driver\SQL\SQL { return $this->user->getSQL(); }
public function getUser(): User { return $this->user; }
2020-02-09 23:02:19 +01:00
2020-06-20 15:49:53 +02:00
public function getView() : ?View {
$view = parseClass($this->activeView);
$file = getClassPath($view);
if(!file_exists($file) || !is_subclass_of($view, View::class)) {
2020-06-20 15:49:53 +02:00
return null;
}
2020-02-09 23:02:19 +01:00
return new $view($this);
}
public function getRequestedView(): string {
return $this->activeView;
2020-02-09 23:02:19 +01:00
}
2021-04-02 22:47:11 +02:00
function getCode(): string {
2020-02-09 23:02:19 +01:00
2020-04-02 21:39:02 +02:00
if ($this->databaseRequired) {
2020-02-09 23:02:19 +01:00
$sql = $this->user->getSQL();
if (is_null($sql)) {
die("Database is not configured yet.");
2020-02-10 12:32:53 +01:00
} else if(!$sql->isConnected()) {
2020-02-09 23:02:19 +01:00
die("Database is not connected: " . $sql->getLastError());
}
}
$body = $this->body->getCode();
$head = $this->head->getCode();
2020-06-20 15:49:53 +02:00
$lang = $this->user->getLanguage()->getShortCode();
2020-02-09 23:02:19 +01:00
$html = "<!DOCTYPE html>";
2020-06-20 15:49:53 +02:00
$html .= "<html lang=\"$lang\">";
2020-02-09 23:02:19 +01:00
$html .= $head;
$html .= $body;
$html .= "</html>";
return $html;
}
2020-04-03 15:56:04 +02:00
}