2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2014-04-29 05:33:03 +00:00
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2014-04-29 05:33:03 +00:00
|
|
|
* Class Block
|
|
|
|
*
|
|
|
|
* A basic chunk of a page.
|
2007-12-06 11:01:18 +00:00
|
|
|
*/
|
2019-05-28 16:59:38 +00:00
|
|
|
class Block
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The block's title.
|
|
|
|
*/
|
2021-03-14 23:43:50 +00:00
|
|
|
public ?string $header;
|
2012-03-12 04:39:04 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* The content of the block.
|
|
|
|
*/
|
2021-03-14 23:43:50 +00:00
|
|
|
public ?string $body;
|
2012-03-12 04:39:04 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* Where the block should be placed. The default theme supports
|
|
|
|
* "main" and "left", other themes can add their own areas.
|
|
|
|
*/
|
2021-03-14 23:43:50 +00:00
|
|
|
public string $section;
|
2012-03-12 04:39:04 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* How far down the section the block should appear, higher
|
|
|
|
* numbers appear lower. The scale is 0-100 by convention,
|
2020-01-26 23:12:48 +00:00
|
|
|
* though any number will work.
|
2019-05-28 16:59:38 +00:00
|
|
|
*/
|
2021-03-14 23:43:50 +00:00
|
|
|
public int $position;
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* A unique ID for the block.
|
|
|
|
*/
|
2021-03-14 23:43:50 +00:00
|
|
|
public string $id;
|
2012-03-12 04:39:04 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* Should this block count as content for the sake of
|
|
|
|
* the 404 handler
|
|
|
|
*/
|
2021-03-14 23:43:50 +00:00
|
|
|
public bool $is_content = true;
|
2017-08-24 09:17:24 +00:00
|
|
|
|
2022-10-28 00:45:35 +00:00
|
|
|
public function __construct(string $header=null, string|\MicroHTML\HTMLElement $body=null, string $section="main", int $position=50, string $id=null)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$this->header = $header;
|
2022-10-28 00:45:35 +00:00
|
|
|
$this->body = (string)$body;
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->section = $section;
|
|
|
|
$this->position = $position;
|
2017-08-24 09:17:39 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if (is_null($id)) {
|
2020-01-26 19:44:36 +00:00
|
|
|
$id = (empty($header) ? md5($body ?? '') : $header) . $section;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2021-03-14 23:43:50 +00:00
|
|
|
$str_id = preg_replace('/[^\w-]/', '', str_replace(' ', '_', $id));
|
|
|
|
assert(is_string($str_id));
|
|
|
|
$this->id = $str_id;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2012-03-12 04:39:04 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* Get the HTML for this block.
|
|
|
|
*/
|
|
|
|
public function get_html(bool $hidable=false): string
|
|
|
|
{
|
|
|
|
$h = $this->header;
|
|
|
|
$b = $this->body;
|
|
|
|
$i = $this->id;
|
|
|
|
$html = "<section id='$i'>";
|
|
|
|
$h_toggler = $hidable ? " shm-toggler" : "";
|
|
|
|
if (!empty($h)) {
|
|
|
|
$html .= "<h3 data-toggle-sel='#$i' class='$h_toggler'>$h</h3>";
|
|
|
|
}
|
|
|
|
if (!empty($b)) {
|
|
|
|
$html .= "<div class='blockbody'>$b</div>";
|
|
|
|
}
|
|
|
|
$html .= "</section>\n";
|
|
|
|
return $html;
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2007-12-04 22:24:58 +00:00
|
|
|
|
2007-12-06 11:01:18 +00:00
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2014-04-29 05:33:03 +00:00
|
|
|
* Class NavBlock
|
|
|
|
*
|
2009-07-19 07:38:13 +00:00
|
|
|
* A generic navigation block with a link to the main page.
|
|
|
|
*
|
|
|
|
* Used because "new NavBlock()" is easier than "new Block('Navigation', ..."
|
2014-04-29 05:33:03 +00:00
|
|
|
*
|
2007-12-06 11:01:18 +00:00
|
|
|
*/
|
2019-05-28 16:59:38 +00:00
|
|
|
class NavBlock extends Block
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct("Navigation", "<a href='".make_link()."'>Index</a>", "left", 0);
|
|
|
|
}
|
2007-12-04 22:24:58 +00:00
|
|
|
}
|