This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/contrib/blotter/main.php

134 lines
4.3 KiB
PHP
Raw Normal View History

2010-02-16 00:37:00 +00:00
<?php
/*
* Name: Blotter
* Author: Zach Hall <zach@sosguy.net> [http://seemslegit.com/]
* License: GPLv2
* Description: Displays brief updates about whatever you want on every page.
* Colors and positioning can be configured to match your site's design.
*
* Development TODO at http://github.com/zshall/shimmie2/issues
*/
class Blotter extends SimpleExtension {
public function onInitExt(Event $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.
*/
2010-02-18 14:12:52 +00:00
if($version < 1) {
/**
* Installer
*/
2010-02-16 00:37:00 +00:00
global $database, $config;
2010-02-18 14:12:52 +00:00
$database->create_table("blotter", "
id SCORE_AIPK,
entry_date SCORE_DATETIME DEFAULT SCORE_NOW,
entry_text TEXT NOT NULL,
important SCORE_BOOL NOT NULL DEFAULT SCORE_BOOL_N
");
2010-02-16 00:37:00 +00:00
// Insert sample data:
$database->execute("INSERT INTO blotter (id, entry_date, entry_text, important) VALUES (?, now(), ?, ?)",
2010-02-18 14:12:52 +00:00
array(NULL, "Installed the blotter extension!", "Y"));
2010-02-16 00:37:00 +00:00
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");
}
public function onSetupBuilding(Event $event) {
global $config;
$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", array("Top of page" => "subheading", "In navigation bar" => "left"), "<br>Position: ");
$event->panel->add_block($sb);
}
public function onUserBlockBuilding(Event $event) {
global $user;
if($user->is_admin()) {
$event->add_link("Blotter Editor", make_link("blotter/editor"));
}
}
public function onPageRequest(Event $event) {
if($event->page_matches("blotter")) {
switch($event->get_arg(0)) {
case "editor":
2010-02-18 14:12:52 +00:00
/**
* Displays the blotter editor.
*/
2010-02-16 00:37:00 +00:00
global $database, $user;
2010-02-18 14:14:33 +00:00
if(!$user->is_admin()) {
$this->theme->display_permission_denied($page);
} else {
$entries = $database->get_all("SELECT * FROM blotter ORDER BY id DESC");
$this->theme->display_editor($entries);
}
break;
2010-02-16 00:37:00 +00:00
case "add":
/**
* Adds an entry
*/
global $page, $database, $user;
2010-02-18 14:14:33 +00:00
if(!$user->is_admin()) {
$this->theme->display_permission_denied($page);
} 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:
2010-02-18 14:20:32 +00:00
$database->execute("INSERT INTO blotter (entry_date, entry_text, important) VALUES (now(), ?, ?)",
array($entry_text, $important));
2010-02-18 14:14:33 +00:00
log_info("blotter", "Added Message: $entry_text");
$page->set_mode("redirect");
$page->set_redirect(make_link("blotter/editor"));
}
break;
2010-02-16 00:37:00 +00:00
case "remove":
/**
* Removes an entry
*/
global $page, $database, $user;
2010-02-18 14:14:33 +00:00
if(!$user->is_admin()) {
$this->theme->display_permission_denied($page);
} else {
$id = int_escape($_POST['id']);
if(!isset($id)) { die("No ID!"); }
2010-02-18 14:20:32 +00:00
$database->Execute("DELETE FROM blotter WHERE id=?", array($id));
2010-02-18 14:14:33 +00:00
log_info("blotter", "Removed Entry #$id");
$page->set_mode("redirect");
$page->set_redirect(make_link("blotter/editor"));
}
break;
2010-02-16 00:37:00 +00:00
case "":
/**
* Displays all blotter entries
*/
global $database, $user;
2010-02-18 14:14:33 +00:00
$entries = $database->get_all("SELECT * FROM blotter ORDER BY id DESC");
$this->theme->display_blotter_page($entries);
break;
2010-02-16 00:37:00 +00:00
}
}
2010-02-18 14:12:52 +00:00
/**
* Finally, display the blotter on whatever page we're viewing.
*/
$this->display_blotter();
2010-02-16 00:37:00 +00:00
}
2010-02-18 14:12:52 +00:00
2010-02-16 00:37:00 +00:00
private function display_blotter() {
global $database, $config;
$limit = $config->get_int("blotter_recent", 5);
2010-02-18 14:20:32 +00:00
$entries = $database->get_all("SELECT * FROM blotter ORDER BY id DESC LIMIT ?,?", array(0, $limit));
2010-02-16 00:37:00 +00:00
$this->theme->display_blotter($entries);
}
2010-02-18 14:12:52 +00:00
}
?>