Frontend, Bugfixes

This commit is contained in:
2020-04-04 01:15:59 +02:00
parent efe3ada470
commit 8ce74edc38
30 changed files with 501 additions and 132 deletions

View File

@@ -1,19 +1,24 @@
<?php
namespace Views;
namespace Views\Admin;
// Source: https://adminlte.io/themes/v3/
use Documents\Document404\Body404;
use Elements\Body;
use Elements\Script;
use Elements\View;
use Views\View404;
class AdminDashboard extends Body {
class AdminDashboardBody extends Body {
private array $errorMessages;
private array $notifications;
public function __construct($document) {
parent::__construct($document);
$this->errorMessages = array();
$this->notifications = array();
}
private function getNotifications() : array {
@@ -49,8 +54,7 @@ class AdminDashboard extends Body {
$iconMail = $this->createIcon("envelope", "fas");
// Notifications
$notifications = $this->getNotifications();
$numNotifications = count($notifications);
$numNotifications = count($this->notifications);
if ($numNotifications === 0) {
$notificationText = L("No new notifications");
} else if($numNotifications === 1) {
@@ -98,7 +102,7 @@ class AdminDashboard extends Body {
// Notifications
$i = 0;
foreach($notifications as $notification) {
foreach($this->notifications as $notification) {
$title = $notification["title"];
$notificationId = $notification["uid"];
@@ -150,12 +154,17 @@ class AdminDashboard extends Body {
),
);
$notificationCount = count($this->notifications);
if ($notificationCount > 0) {
$menuEntries["dashboard"]["badge"] = array("type" => "warning", "value" => $notificationCount);
}
$currentView = $_GET["view"] ?? "dashboard";
$html =
"<aside class=\"main-sidebar sidebar-dark-primary elevation-4\">
<!-- Brand Logo -->
<a href=\"index3.html\" class=\"brand-link\">
<a href=\"/admin\" class=\"brand-link\">
<img src=\"/img/web_base_logo.png\" alt=\"WebBase Logo\" class=\"brand-image img-circle elevation-3\"
style=\"opacity: .8\">
<span class=\"brand-text font-weight-light\">WebBase</span>
@@ -172,17 +181,23 @@ class AdminDashboard extends Body {
$name = L($menuEntry["name"]);
$icon = $this->createIcon($menuEntry["icon"], "fas", "nav-icon");
$active = ($currentView === $view) ? " active" : "";
$badge = $menuEntry["badge"] ?? "";
if($badge) {
$badgeType = $badge["type"];
$badgeValue = $badge["value"];
$badge = "<span class=\"badge badge-$badgeType right\">$badgeValue</span>";
}
$html .=
"<li class=\"nav-item\">
"<li class=\"nav-item\">
<a href=\"?view=$view\" class=\"nav-link$active\">
$icon
<p>$name </p>
$icon<p>$name$badge</p>
</a>
</li>";
}
$html .=
"</ul>
"</ul>
</nav>
</div>
</aside>";
@@ -190,32 +205,64 @@ class AdminDashboard extends Body {
return $html;
}
private function getView() {
$views = array(
"dashboard" => Dashboard::class,
"users" => UserOverview::class,
"404" => View404::class,
);
$currentView = $_GET["view"] ?? "dashboard";
if (!isset($views[$currentView])) {
$currentView = "404";
}
$view = new $views[$currentView]($this->getDocument());
assert($view instanceof View);
$code = $view->getCode();
if ($view instanceof AdminView) {
$this->errorMessages = array_merge($this->errorMessages, $view->getErrorMessages());
}
return $code;
}
public function loadView() {
parent::loadView();
$head = $this->getDocument()->getHead();
$head->addJS(Script::BOOTSTRAP);
$head->loadAdminlte();
$this->notifications = $this->getNotifications();
}
private function getContent() {
$this->getUsers();
$view = $this->getView();
$html = "<div class=\"content-wrapper p-2\">";
foreach($this->errorMessages as $errorMessage) {
$html .= $this->createErrorText($errorMessage);
}
$html .= $view;
$html .= "</div>";
return $html;
}
public function getCode() {
$head = $this->getDocument()->getHead();
$head->addJS(Script::BOOTSTRAP);
$head->loadAdminlte();
$html = parent::getCode();
$header = $this->getHeader();
$sidebar = $this->getSidebar();
$content = $this->getContent();
$html =
$html .=
"<!-- LICENSE: /docs/LICENSE_ADMINLTE -->
<body class=\"hold-transition sidebar-mini layout-fixed\">
<div class=\"wrapper\">

View File

@@ -0,0 +1,46 @@
<?php
namespace Views\Admin;
use Elements\Document;
use Elements\View;
class AdminView extends View {
protected array $errorMessages;
public function __construct(Document $document) {
parent::__construct($document);
$this->errorMessages = array();
}
public function getErrorMessages() {
return $this->errorMessages;
}
public function getCode() {
$html = parent::getCode();
$home = L("Home");
$html .=
"<div class=\"content-header\">
<div class=\"container-fluid\">
<div class=\"row mb-2\">
<div class=\"col-sm-6\">
<h1 class=\"m-0 text-dark\">$this->title</h1>
</div>
<div class=\"col-sm-6\">
<ol class=\"breadcrumb float-sm-right\">
<li class=\"breadcrumb-item\"><a href=\"/\">$home</a></li>
<li class=\"breadcrumb-item active\">$this->title</li>
</ol>
</div>
</div>
</div><!-- /.container-fluid -->
</div>";
return $html;
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace Views\Admin;
use Elements\Document;
class Dashboard extends AdminView {
public function __construct(Document $document) {
parent::__construct($document);
}
public function loadView() {
parent::loadView();
$this->title = L("Dashboard");
}
public function getCode() {
$html = parent::getCode();
return $html;
}
}

View File

@@ -0,0 +1,164 @@
<?php
namespace Views\Admin;
use DateTime;
use Elements\Document;
class UserOverview extends AdminView {
private array $users;
private int $page;
private int $pageCount;
public function __construct(Document $document) {
parent::__construct($document);
$this->users = array();
$this->pageCount = 0;
$this->page = 1;
}
public function loadView() {
parent::loadView();
$this->title = L("User Control");
$this->requestUsers();
}
private function requestUsers() {
if(isset($_GET["page"]) && is_numeric($_GET["page"])) {
$this->page = intval($_GET["page"]);
} else {
$this->page = 1;
}
$req = new \Api\User\Fetch($this->getDocument()->getUser());
if (!$req->execute(array("page" => $this->page))) {
$this->errorMessages[] = $req->getLastError();
} else {
$result = $req->getResult();
$this->users = $result["users"];
$this->pageCount = $result["pages"];
}
}
private function getGroups($groups) {
$badges = [];
foreach($groups as $groupId => $group) {
$badgeClass = "secondary";
if ($groupId === USER_GROUP_ADMIN) {
$badgeClass = "danger";
}
$badges[] = $this->createBadge($badgeClass, $group);
}
return implode("&nbsp;", $badges);
}
private function getPagination() {
$userPageNavigation = L("User page navigation");
$previousDisabled = ($this->page == 1 ? " disabled" : "");
$nextDisabled = ($this->page >= $this->pageCount ? " disabled" : "");
$html =
"<nav aria-label=\"$userPageNavigation\" id=\"userPageNavigation\">
<ul class=\"pagination p-2 m-0 justify-content-end\">
<li class=\"page-item$previousDisabled\"><a class=\"page-link\" href=\"#\">Previous</a></li>";
for($i = 1; $i <= $this->pageCount; $i++) {
$active = $i === $this->page ? " active" : "";
$html .=
"<li class=\"page-item$active\"><a class=\"page-link\" href=\"#\">$i</a></li>";
}
$html .=
"<li class=\"page-item$nextDisabled\"><a class=\"page-link\" href=\"#\">Next</a></li>
</ul>
</nav>";
return $html;
}
private function getUserRows() {
$dateFormat = L("Y/m/d");
$userRows = array();
foreach($this->users as $uid => $user) {
$name = $user["name"];
$email = $user["email"] ?? "";
$registeredAt = (new DateTime($user["created_at"]))->format($dateFormat);
$groups = $this->getGroups($user["groups"]);
$userRows[] =
"<tr data-id=\"$uid\">
<td>$name</td>
<td>$email</td>
<td>$groups</td>
<td>$registeredAt</td>
</tr>";
}
return implode("", $userRows);
}
public function getCode() {
$html = parent::getCode();
// Icons
$iconRefresh = $this->createIcon("sync");
// Locale
$users = L("Users");
$name = L("Name");
$email = L("Email");
$groups = L("Groups");
$registeredAt = L("Registered At");
// Content
$pagination = $this->getPagination();
$userRows = $this->getUserRows();
$html .=
"<div class=\"content\">
<div class=\"container-fluid\">
<div class=\"row\">
<div class=\"col-lg-12\">
<div class=\"card\">
<div class=\"card-header border-0\">
<h3 class=\"card-title\">$users</h3>
<div class=\"card-tools\">
<a href=\"#\" class=\"btn btn-tool btn-sm\" id=\"userTableRefresh\">
$iconRefresh
</a>
</div>
</div>
<div class=\"card-body table-responsive p-0\">
<table class=\"table table-striped table-valign-middle\" id=\"userTable\">
<thead>
<tr>
<th>$name</th>
<th>$email</th>
<th>$groups</th>
<th>$registeredAt</th>
</tr>
</thead>
<tbody>
$userRows
</tbody>
</table>
$pagination
</div>
</div>
</div>
</div>
</div>
</div>";
return $html;
}
}

View File

@@ -3,52 +3,58 @@
namespace Views;
use Api\GetLanguages;
use Elements\View;
class LanguageFlags extends \View {
class LanguageFlags extends View {
private array $languageFlags;
public function __construct($document) {
parent::__construct($document);
$this->languageFlags = array();
}
public function getCode() {
public function loadView() {
parent::loadView();
$requestUri = $_SERVER["REQUEST_URI"];
$queryString = $_SERVER['QUERY_STRING'];
$flags = array();
$request = new GetLanguages($this->getDocument()->getUser());
$params = explode("&", $queryString);
$query = array();
foreach($params as $param) {
$aParam = explode("=", $param);
$key = $aParam[0];
if($key == "s" && startsWith($requestUri, "/s/"))
continue;
$val = (isset($aParam[1]) ? $aParam[1] : "");
if(!empty($key)) {
$query[$key] = $val;
}
}
$url = parse_url($requestUri, PHP_URL_PATH) . "?";
if($request->execute()) {
$requestUri = $_SERVER["REQUEST_URI"];
$queryString = $_SERVER['QUERY_STRING'];
$params = explode("&", $queryString);
$query = array();
foreach($params as $param) {
$aParam = explode("=", $param);
$key = $aParam[0];
if($key == "s" && startsWith($requestUri, "/s/"))
continue;
$val = (isset($aParam[1]) ? $aParam[1] : "");
if(!empty($key)) {
$query[$key] = $val;
}
}
$url = parse_url($requestUri, PHP_URL_PATH) . "?";
foreach($request->getResult()['languages'] as $lang) {
$langCode = $lang['code'];
$langName = $lang['name'];
$query['lang'] = $langCode;
$queryString = http_build_query($query);
$flags[] = $this->createLink(
$this->languageFlags[] = $this->createLink(
"$url$queryString",
"<img class=\"p-1\" src=\"/img/icons/lang/$langCode.gif\" alt=\"$langName\" title=\"$langName\">"
);
}
} else {
$flags[] = $this->createErrorText($request->getLastError());
}
}
return implode('', $flags);
public function getCode() {
return implode('', $this->languageFlags);
}
}

View File

@@ -10,9 +10,13 @@ class LoginBody extends Body {
parent::__construct($document);
}
public function getCode() {
public function loadView() {
parent::loadView();
$this->getDocument()->getHead()->loadBootstrap();
}
public function getCode() {
$html = parent::getCode();
$username = L("Username");
$password = L("Password");
@@ -25,7 +29,7 @@ class LoginBody extends Body {
$domain = $_SERVER['HTTP_HOST'];
$protocol = getProtocol();
$html = "<body>";
$html .= "<body>";
$accountCreated = "";
if(isset($_GET["accountCreated"])) {

View File

@@ -0,0 +1,13 @@
<?php
namespace Views;
use Elements\View;
class View404 extends View {
public function getCode() {
return parent::getCode() . "<b>Not found</b>";
}
};