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/ext/blocks/main.php

89 lines
2.9 KiB
PHP
Raw Normal View History

2011-03-02 10:50:22 +00:00
<?php
/*
* Name: Generic Blocks
* Author: Shish <webmaster@shishnet.org>
2012-02-10 04:04:37 +00:00
* Link: http://code.shishnet.org/shimmie2/
2011-03-02 10:50:22 +00:00
* License: GPLv2
2012-03-19 18:43:20 +00:00
* Description: Add HTML to some space (News, Ads, etc)
2011-03-02 10:50:22 +00:00
*/
class Blocks extends Extension {
public function onInitExt(InitExtEvent $event) {
global $config, $database;
if($config->get_int("ext_blocks_version") < 1) {
$database->create_table("blocks", "
id SCORE_AIPK,
pages VARCHAR(128) NOT NULL,
title VARCHAR(128) NOT NULL,
area VARCHAR(16) NOT NULL,
priority INTEGER NOT NULL,
content TEXT NOT NULL
");
$database->execute("CREATE INDEX blocks_pages_idx ON blocks(pages)", array());
$config->set_int("ext_blocks_version", 1);
}
}
public function onUserBlockBuilding(UserBlockBuildingEvent $event) {
global $user;
if($user->can("manage_blocks")) {
$event->add_link("Blocks Editor", make_link("blocks/list"));
}
}
public function onPageRequest(PageRequestEvent $event) {
global $config, $database, $page, $user;
2012-06-23 22:21:04 +00:00
$blocks = $database->cache->get("blocks");
if($blocks === false) {
$blocks = $database->get_all("SELECT * FROM blocks");
$database->cache->set("blocks", $blocks, 600);
2012-06-23 22:21:04 +00:00
}
2011-03-02 10:50:22 +00:00
foreach($blocks as $block) {
if(fnmatch($block['pages'], implode("/", $event->args))) {
$page->add_block(new Block($block['title'], $block['content'], $block['area'], $block['priority']));
}
}
2011-03-02 10:50:22 +00:00
if($event->page_matches("blocks") && $user->can("manage_blocks")) {
if($event->get_arg(0) == "add") {
if($user->check_auth_token()) {
$database->execute("
INSERT INTO blocks (pages, title, area, priority, content)
VALUES (?, ?, ?, ?, ?)
", array($_POST['pages'], $_POST['title'], $_POST['area'], (int)$_POST['priority'], $_POST['content']));
2012-03-19 18:57:33 +00:00
log_info("blocks", "Added Block #".($database->get_last_insert_id('blocks_id_seq'))." (".$_POST['title'].")");
2012-06-23 22:21:04 +00:00
$database->cache->delete("blocks");
$page->set_mode("redirect");
$page->set_redirect(make_link("blocks/list"));
}
}
if($event->get_arg(0) == "update") {
if($user->check_auth_token()) {
if(!empty($_POST['delete'])) {
$database->execute("
DELETE FROM blocks
WHERE id=?
", array($_POST['id']));
2012-03-19 18:57:33 +00:00
log_info("blocks", "Deleted Block #".$_POST['id']);
2011-03-02 10:50:22 +00:00
}
else {
$database->execute("
UPDATE blocks SET pages=?, title=?, area=?, priority=?, content=?
WHERE id=?
", array($_POST['pages'], $_POST['title'], $_POST['area'], (int)$_POST['priority'], $_POST['content'], $_POST['id']));
2012-03-19 18:57:33 +00:00
log_info("blocks", "Updated Block #".$_POST['id']." (".$_POST['title'].")");
2011-03-02 10:50:22 +00:00
}
2012-06-23 22:21:04 +00:00
$database->cache->delete("blocks");
$page->set_mode("redirect");
$page->set_redirect(make_link("blocks/list"));
2011-03-02 10:50:22 +00:00
}
}
else if($event->get_arg(0) == "list") {
2012-03-19 19:09:52 +00:00
$this->theme->display_blocks($database->get_all("SELECT * FROM blocks ORDER BY area, priority"));
2011-03-02 10:50:22 +00:00
}
}
}
}
?>