web-base/Core/Elements/Head.class.php

107 lines
3.4 KiB
PHP
Raw Normal View History

2020-02-09 23:02:19 +01:00
<?php
2022-11-18 18:06:46 +01:00
namespace Core\Elements;
2020-02-09 23:02:19 +01:00
2020-04-03 15:56:04 +02:00
abstract class Head extends View {
protected array $sources;
protected string $title;
protected array $metas;
protected array $rawFields;
protected array $keywords;
protected string $description;
protected string $baseUrl;
2020-02-09 23:02:19 +01:00
function __construct($document) {
parent::__construct($document);
$this->sources = array();
2020-06-22 19:09:02 +02:00
$this->searchable = false;
2020-02-09 23:02:19 +01:00
$this->metas = $this->initMetas();
$this->rawFields = $this->initRawFields();
$this->title = $this->initTitle();
$this->initSources();
$this->init();
}
protected abstract function initSources();
2021-04-02 21:58:06 +02:00
protected abstract function initMetas(): array;
protected abstract function initRawFields(): array;
protected abstract function initTitle(): string;
2020-02-09 23:02:19 +01:00
2024-04-23 14:05:29 +02:00
protected function init(): void {
2020-02-09 23:02:19 +01:00
$this->keywords = array();
$this->description = "";
$this->baseUrl = "";
}
public function setBaseUrl($baseUrl) { $this->baseUrl = $baseUrl; }
public function setDescription($description) { $this->description = $description; }
public function setKeywords($keywords) { $this->keywords = $keywords; }
public function setTitle($title) { $this->title = $title; }
2021-04-02 22:44:44 +02:00
public function getSources(): array { return $this->sources; }
2020-02-09 23:02:19 +01:00
public function addScript($type, $url, $js = '') { $this->sources[] = new Script($type, $url, $js); }
public function addRawField($rawField) { $this->rawFields[] = $rawField; }
public function addMeta($aMeta) { $this->metas[] = $aMeta; }
public function addLink($rel, $href, $type = "") { $this->sources[] = new Link($rel, $href, $type); }
2021-04-02 22:44:44 +02:00
public function addKeywords($keywords) { $this->keywords = array_merge($this->keywords, $keywords); }
public function getTitle(): string { return $this->title; }
2020-02-09 23:02:19 +01:00
2020-04-04 01:15:59 +02:00
public function addCSS($href, $type = Link::MIME_TEXT_CSS) { $this->sources[] = new Link(Link::STYLESHEET, $href, $type); }
2020-02-09 23:02:19 +01:00
public function addStyle($style) { $this->sources[] = new Style($style); }
public function addJS($url) { $this->sources[] = new Script(Script::MIME_TEXT_JAVASCRIPT, $url, ""); }
public function addJSCode($code) { $this->sources[] = new Script(Script::MIME_TEXT_JAVASCRIPT, "", $code); }
2024-04-23 14:05:29 +02:00
public function loadFontawesome(): void {
2020-02-09 23:02:19 +01:00
$this->addCSS(Link::FONTAWESOME);
}
2024-04-23 14:05:29 +02:00
public function loadJQuery(): void {
2020-02-09 23:02:19 +01:00
$this->addJS(Script::JQUERY);
}
2024-04-23 14:05:29 +02:00
public function loadBootstrap(): void {
2020-02-09 23:02:19 +01:00
$this->addCSS(Link::BOOTSTRAP);
$this->addJS(Script::BOOTSTRAP);
}
2021-04-02 22:47:11 +02:00
public function getCode(): string {
$content = [];
// meta tags
foreach($this->metas as $meta) {
$content[] = html_tag_short("meta", $meta);
2020-02-09 23:02:19 +01:00
}
// description
2020-02-09 23:02:19 +01:00
if(!empty($this->description)) {
$content[] = html_tag_short("meta", ["name" => "description", "content" => $this->description]);
2020-02-09 23:02:19 +01:00
}
// keywords
2020-02-09 23:02:19 +01:00
if(!empty($this->keywords)) {
$keywords = implode(", ", $this->keywords);
$content[] = html_tag_short("meta", ["name" => "keywords", "content" => $keywords]);
2020-02-09 23:02:19 +01:00
}
// base tag
2020-02-09 23:02:19 +01:00
if(!empty($this->baseUrl)) {
$content[] = html_tag_short("base", ["href" => $this->baseUrl]);
2020-02-09 23:02:19 +01:00
}
// title
$content[] = html_tag("title", [], $this->title);
2020-02-09 23:02:19 +01:00
// src tags
2020-02-09 23:02:19 +01:00
foreach($this->sources as $src) {
$content[] = $src->getCode();
2020-02-09 23:02:19 +01:00
}
//
foreach ($this->rawFields as $raw) {
$content[] = $raw;
2020-02-09 23:02:19 +01:00
}
return html_tag("head", [], $content, false);
2020-02-09 23:02:19 +01:00
}
}