2010-02-16 00:37:00 +00:00
|
|
|
<?php
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class Blotter extends Extension
|
|
|
|
{
|
|
|
|
public function onInitExt(InitExtEvent $event)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* I love re-using this installer don't I...
|
|
|
|
*/
|
|
|
|
global $config;
|
|
|
|
$version = $config->get_int("blotter_version", 0);
|
|
|
|
/**
|
|
|
|
* If this version is less than "1", it's time to install.
|
|
|
|
*
|
|
|
|
* REMINDER: If I change the database tables, I must change up version by 1.
|
|
|
|
*/
|
|
|
|
if ($version < 1) {
|
|
|
|
/**
|
|
|
|
* Installer
|
|
|
|
*/
|
|
|
|
global $database, $config;
|
|
|
|
$database->create_table("blotter", "
|
2010-02-18 14:12:52 +00:00
|
|
|
id SCORE_AIPK,
|
|
|
|
entry_date SCORE_DATETIME DEFAULT SCORE_NOW,
|
|
|
|
entry_text TEXT NOT NULL,
|
|
|
|
important SCORE_BOOL NOT NULL DEFAULT SCORE_BOOL_N
|
|
|
|
");
|
2019-05-28 16:59:38 +00:00
|
|
|
// Insert sample data:
|
|
|
|
$database->execute(
|
|
|
|
"INSERT INTO blotter (entry_date, entry_text, important) VALUES (now(), ?, ?)",
|
|
|
|
["Installed the blotter extension!", "Y"]
|
|
|
|
);
|
|
|
|
log_info("blotter", "Installed tables for blotter extension.");
|
|
|
|
$config->set_int("blotter_version", 1);
|
|
|
|
}
|
|
|
|
// Set default config:
|
|
|
|
$config->set_default_int("blotter_recent", 5);
|
|
|
|
$config->set_default_string("blotter_color", "FF0000");
|
|
|
|
$config->set_default_string("blotter_position", "subheading");
|
|
|
|
}
|
2012-08-15 18:46:36 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event)
|
|
|
|
{
|
|
|
|
$sb = new SetupBlock("Blotter");
|
|
|
|
$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: ");
|
|
|
|
$event->panel->add_block($sb);
|
|
|
|
}
|
2012-08-15 18:46:36 +00:00
|
|
|
|
2019-08-02 19:54:48 +00:00
|
|
|
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event)
|
|
|
|
{
|
|
|
|
global $user;
|
2019-09-29 13:30:55 +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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onUserBlockBuilding(UserBlockBuildingEvent $event)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event)
|
|
|
|
{
|
|
|
|
global $page, $database, $user;
|
|
|
|
if ($event->page_matches("blotter")) {
|
|
|
|
switch ($event->get_arg(0)) {
|
|
|
|
case "editor":
|
|
|
|
/**
|
|
|
|
* Displays the blotter editor.
|
|
|
|
*/
|
2019-09-29 18:00:51 +00:00
|
|
|
if (!$user->can(Permissions::BLOTTER_ADMIN)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->theme->display_permission_denied();
|
|
|
|
} else {
|
|
|
|
$entries = $database->get_all("SELECT * FROM blotter ORDER BY id DESC");
|
|
|
|
$this->theme->display_editor($entries);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "add":
|
|
|
|
/**
|
|
|
|
* Adds an entry
|
|
|
|
*/
|
2019-09-29 18:00:51 +00:00
|
|
|
if (!$user->can(Permissions::BLOTTER_ADMIN) || !$user->check_auth_token()) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->theme->display_permission_denied();
|
|
|
|
} else {
|
|
|
|
$entry_text = $_POST['entry_text'];
|
|
|
|
if ($entry_text == "") {
|
|
|
|
die("No entry message!");
|
|
|
|
}
|
|
|
|
if (isset($_POST['important'])) {
|
|
|
|
$important = 'Y';
|
|
|
|
} else {
|
|
|
|
$important = 'N';
|
|
|
|
}
|
|
|
|
// Now insert into db:
|
|
|
|
$database->execute(
|
|
|
|
"INSERT INTO blotter (entry_date, entry_text, important) VALUES (now(), ?, ?)",
|
|
|
|
[$entry_text, $important]
|
|
|
|
);
|
|
|
|
log_info("blotter", "Added Message: $entry_text");
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->set_redirect(make_link("blotter/editor"));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "remove":
|
|
|
|
/**
|
|
|
|
* Removes an entry
|
|
|
|
*/
|
2019-09-29 18:00:51 +00:00
|
|
|
if (!$user->can(Permissions::BLOTTER_ADMIN) || !$user->check_auth_token()) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->theme->display_permission_denied();
|
|
|
|
} else {
|
|
|
|
$id = int_escape($_POST['id']);
|
|
|
|
if (!isset($id)) {
|
|
|
|
die("No ID!");
|
|
|
|
}
|
|
|
|
$database->Execute("DELETE FROM blotter WHERE id=:id", ["id"=>$id]);
|
|
|
|
log_info("blotter", "Removed Entry #$id");
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->set_redirect(make_link("blotter/editor"));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "list":
|
|
|
|
/**
|
|
|
|
* Displays all blotter entries
|
|
|
|
*/
|
|
|
|
$entries = $database->get_all("SELECT * FROM blotter ORDER BY id DESC");
|
|
|
|
$this->theme->display_blotter_page($entries);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Finally, display the blotter on whatever page we're viewing.
|
|
|
|
*/
|
|
|
|
$this->display_blotter();
|
|
|
|
}
|
2010-02-18 14:12:52 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
private function display_blotter()
|
|
|
|
{
|
|
|
|
global $database, $config;
|
|
|
|
$limit = $config->get_int("blotter_recent", 5);
|
|
|
|
$sql = 'SELECT * FROM blotter ORDER BY id DESC LIMIT '.intval($limit);
|
|
|
|
$entries = $database->get_all($sql);
|
|
|
|
$this->theme->display_blotter($entries);
|
|
|
|
}
|
2010-02-18 14:12:52 +00:00
|
|
|
}
|