PHP 8.4+ Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
CodeHydrater/src/classes/html_document.php

427 lines
11 KiB

<?php
declare(strict_types=1);
/**
* @author Robert Strutts
* @copyright Copyright (c) 2022, Robert Strutts.
* @license MIT
*/
namespace CodeHydrater;
/**
* HTML Document
*/
class html_document {
private $title = '';
private $author = '';
private $description = '';
private $keywords = '';
private $robots = '';
private $head = '';
private $header = '';
private $body = '';
private $footer = '';
private $js_onready = '';
private $styles = '';
private $scripts = '';
private $main_styles = '';
private $main_scripts = '';
private $active_crumb = '';
private $breadcrumb = array();
public function __construct() {
$this->title = bootstrap\configure::get('html', 'title') ?? '';
$this->author = bootstrap\configure::get('html', 'author') ?? '';
$this->keywords = bootstrap\configure::get('html', 'keywords') ?? '';
$this->description = bootstrap\configure::get('html', 'description') ?? '';
$this->robots = bootstrap\configure::get('html', 'robots');
$css = bootstrap\configure::get('html', 'css');
if (bootstrap\common::get_count($css) > 0) {
foreach($css as $file=>$path) {
if (is_array($file)) continue;
if (is_array($path)) {
if (isset($path['path'])) {
$path_type = $path['path'];
unset($path['path']);
} else {
$path_type = "project";
}
$this->add_css($file, $path_type, $path);
} else {
$this->add_css($file, $path);
}
}
}
$js = bootstrap\configure::get('html', 'javascript');
if (bootstrap\common::get_count($js) >0) {
foreach($js as $file=>$path) {
if (is_array($file)) continue;
if (is_array($path)) {
if (isset($path['path'])) {
$path_type = $path['path'];
unset($path['path']);
} else {
$path_type = "project";
}
$this->add_js($file, $path_type, $path);
} else {
$this->add_js($file, $path);
}
}
}
}
public function clear_css(): void {
$this->styles = '';
}
public function clear_js(): void {
$this->scripts = '';
}
/**
* Set both Title and Header for HTML
* @param string $title
*/
public function set_title_and_header(string $title): void {
$this->title = $title;
$this->header = $title;
}
/**
* Set Author for HTML
* @param string $title
*/
public function set_author(string $author): void {
$this->author = $author;
}
/**
* Set Title for HTML
* @param string $title
*/
public function set_title(string $title): void {
$this->title = $title;
}
/**
* Set Header for HTML
* @param string $header
*/
public function set_header(string $header): void {
$this->header = $header;
}
public function set_head(string $head): void {
$this->head = $head;
}
/**
* Set Footer for HTML
* @param string $footer
*/
public function set_footer(string $footer): void {
// $this->add_css('footer.css', 'project');
$this->footer = $footer;
}
/**
* Set Description for HTML
* @param string $description
*/
public function set_description(string $description): void {
$this->description = $description;
}
/**
* Set Keywords for HTML
* @param string $keywords
*/
public function set_keywords(string $keywords): void {
$this->keywords = $keywords;
}
/**
* Set Robots for HTML
* @param string $robot
*/
public function set_robots(string $robot): void {
$this->robots = $robot;
}
public function set_body(string $body): void {
$this->body = $body;
}
/**
* Set Active BreadCrumb in HTML
* @param string $active
*/
public function set_active_crumb(string $active): void {
$this->active_crumb = $active;
}
/**
* Set BreadCrumbs using array (HyperLink => Name of Crumb)
* @param array $crumbs Array(href => name)
*/
public function set_breadcrumbs(array $crumbs): void {
$this->breadcrumb = $crumbs;
}
public function set_assets_from_array(array $files, string $which, string $scope = 'project'): void {
foreach($files as $file => $a) {
switch($which) {
case 'main_css':
$this->add_main_css($file, $scope, $a);
break;
case 'css':
$this->add_css($file, $scope, $a);
break;
case 'main_js':
$this->add_main_js($file, $scope, $a);
break;
case 'js':
$this->add_js($file, $scope, $a);
break;
}
}
}
private function missing_file(string $file, string $scope, string $kind) {
$failed = strtoupper($kind) . " filename of {$file} - {$scope} Asset Failed to Load!";
$this->add_to_javascript("console.log(\"%c {$failed}\", \"color: red\")");
$comment = "<!-- {$failed} -->";
if ($kind === "css") {
$this->styles .= $comment;
} else if ($kind === "main_css") {
$this->main_styles .= $comment;
} else if ($kind === "js") {
$this->scripts .= $comment;
} else if ($kind === "main_js") {
$this->main_scripts .= $comment;
}
}
/**
* Add CSS stylesheet to HTML under main CSS
* @param string $file
* @param string $scope (project, framework, cdn)
* @return bool was successful
*/
public function add_css(string $file, string $scope = 'project', array $a = array()): bool {
$css = assets::wrap_asset($file, $scope);
if ($css === false) {
$this->missing_file($file, $scope, "css");
return false;
}
$this->styles .= assets::wrap_css($file, $scope, $a);
return true;
}
/**
* Add JS JavaScript to HTML under main JS
* @param string $file
* @param string $scope (project, framework, cdn)
* @return bool was successful
*/
public function add_js(string $file, string $scope = 'project', array $a = array()): bool {
$js = assets::wrap_asset($file, $scope);
if ($js === false) {
$this->js_log($file . " - {$scope} Asset Failed to Load!");
return false;
}
$this->scripts .= assets::wrap_js($file, $scope, $a);
return true;
}
/**
* Add CSS stylesheet to HTML towards top of HTML for CSS
* @param string $file
* @param string $scope (project, framework, cdn)
* @return bool was successful
*/
public function add_main_css(string $file, string $scope = 'project', array $a = array()): bool {
$css = assets::wrap_asset($file, $scope);
if ($css === false) {
$this->js_log($file . " - {$scope} Asset Failed to Load!");
return false;
}
$this->main_styles .= assets::wrap_css($file, $scope, $a);
return true;
}
/**
* Add JavaScript to HTML towards top of HTML for JS
* @param string $file
* @param string $scope (project, framework, cdn)
* @return bool was successful
*/
public function add_main_js(string $file, string $scope = 'project', array $a = array()): bool {
$js = assets::wrap_asset($file, $scope);
if ($js === false) {
$this->js_log($file . " - {$scope} Asset Failed to Load!");
return false;
}
$this->main_scripts .= assets::wrap_js($file, $scope, $a);
return true;
}
/**
* Adds JavaScript code to called after JQuery is ready.
* @param string $code
*/
//public function add_js_onready(string $code): void {
// $this->js_onready .= \px_inline_js(\px_jquery_load($code));
//}
/**
* Log to JavaScript Console under Chrome Browser
* @param string $log
*/
public function js_log(string $log): void {
$this->add_to_javascript("console.log('{$log}');");
}
/**
* Place JavaScript in HTML
* @param string $js
*/
public function add_to_javascript(string $js): void {
if (! empty($js)) {
$this->scripts .= assets::inline_js($js);
}
}
/**
* Use CSS/JS for Database SSP
public function datatables_code(): void {
$this->add_css('datatables/datatables.min.css', 'cl');
$this->add_js('datatables/datatables_no_jquery.min.js', 'cl');
}
*
*/
/**
* Used by Template file to render HTML Author
* @return string HTML Author
*/
public function get_author(): string {
return $this->author;
}
/**
* Used by Template file to render HTML TITLE
* @return string HTML TITLE
*/
public function get_title(): string {
return $this->title;
}
/**
* Used by Template file to render HTML Header
* @return string HTML Header
*/
public function get_header(): string {
return $this->header;
}
public function get_body(): string {
return $this->body;
}
/**
* Used by Template file to render HTML Footer
* @return string HTML Footer
*/
public function get_footer(): string {
return $this->footer;
}
/**
* Used by Template file to render HTML Meta data for Description
* @return string HTML Meta Description
*/
public function get_description(): string {
return $this->description;
}
/**
* Used by Template file to render HTML Meta data for Keywords
* @return string HTML Meta Keywords
*/
public function get_keywords(): string {
return $this->keywords;
}
/**
* Used by Template file to render HTML Meta data for Robots
* @return string HTML Meta Robots
*/
public function get_robots(): string {
return $this->robots;
}
/**
* Used by Template file to render HTML CSS
* @return string HTML CSS
*/
public function get_styles(): string {
return $this->styles;
}
/**
* Used by Template file to render HTML JavaScripts
* @return string HTML JS
*/
public function get_scripts(): string {
return $this->scripts;
}
/**
* Used by Template file to render HTML main CSS @Top
* @return string HTML CSS
*/
public function get_main_styles(): string {
return $this->main_styles;
}
/**
* Used by Template file to render HTML main JS @Top
* @return string HTML JavaScript
*/
public function get_main_scripts(): string {
return $this->main_scripts;
}
/**
* Used by Template file to render HTML JS after main JS
* @return string HTML JS
*/
public function get_js_onready(): string {
return $this->js_onready;
}
/**
* Used by Template file to render HTML Active BreadCrumb
* @return string HTML Active BreadCrumb
*/
public function get_active_crumb(): string {
return $this->active_crumb;
}
/**
* Used by Template file to render HTML BreadCrumbs
* @return string HTML BreadCrumbs
*/
public function get_breadcrumbs(): array {
return $this->breadcrumb;
}
public function get_head(): string {
return $this->head;
}
}