2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class Blotter extends Extension
|
|
|
|
{
|
2020-02-04 00:46:36 +00:00
|
|
|
/** @var BlotterTheme */
|
2023-06-27 14:56:49 +00:00
|
|
|
protected Themelet $theme;
|
2020-02-04 00:46:36 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onInitExt(InitExtEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-11-03 17:19:37 +00:00
|
|
|
global $config;
|
|
|
|
$config->set_default_int("blotter_recent", 5);
|
|
|
|
$config->set_default_string("blotter_color", "FF0000");
|
|
|
|
$config->set_default_string("blotter_position", "subheading");
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event): void
|
2019-11-03 17:19:37 +00:00
|
|
|
{
|
2020-10-29 00:57:58 +00:00
|
|
|
global $database;
|
2020-10-26 18:10:34 +00:00
|
|
|
|
2020-10-29 00:57:58 +00:00
|
|
|
if ($this->get_version("blotter_version") < 1) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->create_table("blotter", "
|
2020-10-26 18:10:34 +00:00
|
|
|
id SCORE_AIPK,
|
|
|
|
entry_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
entry_text TEXT NOT NULL,
|
2020-10-26 22:37:25 +00:00
|
|
|
important BOOLEAN NOT NULL DEFAULT FALSE
|
2020-10-26 18:10:34 +00:00
|
|
|
");
|
2019-05-28 16:59:38 +00:00
|
|
|
// Insert sample data:
|
|
|
|
$database->execute(
|
2019-11-27 11:22:46 +00:00
|
|
|
"INSERT INTO blotter (entry_date, entry_text, important) VALUES (now(), :text, :important)",
|
2023-11-11 21:49:12 +00:00
|
|
|
["text" => "Installed the blotter extension!", "important" => true]
|
2019-05-28 16:59:38 +00:00
|
|
|
);
|
|
|
|
log_info("blotter", "Installed tables for blotter extension.");
|
2020-10-29 00:57:58 +00:00
|
|
|
$this->set_version("blotter_version", 2);
|
2020-10-26 18:10:34 +00:00
|
|
|
}
|
2020-10-29 00:57:58 +00:00
|
|
|
if ($this->get_version("blotter_version") < 2) {
|
2020-10-26 18:10:34 +00:00
|
|
|
$database->standardise_boolean("blotter", "important");
|
2020-10-29 00:57:58 +00:00
|
|
|
$this->set_version("blotter_version", 2);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-15 18:46:36 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-10-26 15:13:28 +00:00
|
|
|
$sb = $event->panel->create_new_block("Blotter");
|
2019-05-28 16:59:38 +00:00
|
|
|
$sb->add_int_option("blotter_recent", "<br />Number of recent entries to display: ");
|
|
|
|
$sb->add_text_option("blotter_color", "<br />Color of important updates: (ABCDEF format) ");
|
|
|
|
$sb->add_choice_option("blotter_position", ["Top of page" => "subheading", "In navigation bar" => "left"], "<br>Position: ");
|
|
|
|
}
|
2012-08-15 18:46:36 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event): void
|
2019-08-02 19:54:48 +00:00
|
|
|
{
|
|
|
|
global $user;
|
2023-11-11 21:49:12 +00:00
|
|
|
if ($event->parent === "system") {
|
2019-09-29 18:00:51 +00:00
|
|
|
if ($user->can(Permissions::BLOTTER_ADMIN)) {
|
2019-08-02 19:54:48 +00:00
|
|
|
$event->add_nav_link("blotter", new Link('blotter/editor'), "Blotter Editor");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onUserBlockBuilding(UserBlockBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $user;
|
2019-09-29 18:00:51 +00:00
|
|
|
if ($user->can(Permissions::BLOTTER_ADMIN)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$event->add_link("Blotter Editor", make_link("blotter/editor"));
|
|
|
|
}
|
|
|
|
}
|
2012-08-15 18:46:36 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $page, $database, $user;
|
2024-02-10 23:03:14 +00:00
|
|
|
if ($event->page_matches("blotter/editor", method: "GET", permission: Permissions::BLOTTER_ADMIN)) {
|
|
|
|
$entries = $database->get_all("SELECT * FROM blotter ORDER BY id DESC");
|
|
|
|
$this->theme->display_editor($entries);
|
|
|
|
}
|
|
|
|
if ($event->page_matches("blotter/add", method: "POST", permission: Permissions::BLOTTER_ADMIN)) {
|
|
|
|
$entry_text = $event->req_POST('entry_text');
|
|
|
|
$important = !is_null($event->get_POST('important'));
|
|
|
|
// Now insert into db:
|
|
|
|
$database->execute(
|
|
|
|
"INSERT INTO blotter (entry_date, entry_text, important) VALUES (now(), :text, :important)",
|
|
|
|
["text" => $entry_text, "important" => $important]
|
|
|
|
);
|
|
|
|
log_info("blotter", "Added Message: $entry_text");
|
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
|
|
|
$page->set_redirect(make_link("blotter/editor"));
|
|
|
|
}
|
|
|
|
if ($event->page_matches("blotter/remove", method: "POST", permission: Permissions::BLOTTER_ADMIN)) {
|
|
|
|
$id = int_escape($event->req_POST('id'));
|
|
|
|
$database->execute("DELETE FROM blotter WHERE id=:id", ["id" => $id]);
|
|
|
|
log_info("blotter", "Removed Entry #$id");
|
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
|
|
|
$page->set_redirect(make_link("blotter/editor"));
|
|
|
|
}
|
|
|
|
if ($event->page_matches("blotter/list", method: "GET")) {
|
|
|
|
$entries = $database->get_all("SELECT * FROM blotter ORDER BY id DESC");
|
|
|
|
$this->theme->display_blotter_page($entries);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Finally, display the blotter on whatever page we're viewing.
|
|
|
|
*/
|
|
|
|
$this->display_blotter();
|
|
|
|
}
|
2010-02-18 14:12:52 +00:00
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
private function display_blotter(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $database, $config;
|
2024-02-11 11:42:49 +00:00
|
|
|
$entries = $database->get_all(
|
|
|
|
'SELECT * FROM blotter ORDER BY id DESC LIMIT :limit',
|
|
|
|
["limit" => $config->get_int("blotter_recent", 5)]
|
|
|
|
);
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->theme->display_blotter($entries);
|
|
|
|
}
|
2010-02-18 14:12:52 +00:00
|
|
|
}
|