2020-01-23 00:45:41 +00:00
|
|
|
<?php declare(strict_types=1);
|
2011-03-02 10:50:22 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class Blocks extends Extension
|
|
|
|
{
|
2019-11-03 17:19:37 +00:00
|
|
|
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config, $database;
|
2019-11-03 19:49:52 +00:00
|
|
|
if ($this->get_version("ext_blocks_version") < 1) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->create_table("blocks", "
|
2012-03-19 18:40:37 +00:00
|
|
|
id SCORE_AIPK,
|
|
|
|
pages VARCHAR(128) NOT NULL,
|
|
|
|
title VARCHAR(128) NOT NULL,
|
|
|
|
area VARCHAR(16) NOT NULL,
|
|
|
|
priority INTEGER NOT NULL,
|
2014-02-23 04:02:11 +00:00
|
|
|
content TEXT NOT NULL
|
2012-03-19 18:40:37 +00:00
|
|
|
");
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->execute("CREATE INDEX blocks_pages_idx ON blocks(pages)", []);
|
2019-11-03 19:49:52 +00:00
|
|
|
$this->set_version("ext_blocks_version", 1);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-19 18:40:37 +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-08-02 19:54:48 +00:00
|
|
|
if ($user->can(Permissions::MANAGE_BLOCKS)) {
|
|
|
|
$event->add_nav_link("blocks", new Link('blocks/list'), "Blocks Editor");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onUserBlockBuilding(UserBlockBuildingEvent $event)
|
|
|
|
{
|
|
|
|
global $user;
|
2019-07-09 14:10:21 +00:00
|
|
|
if ($user->can(Permissions::MANAGE_BLOCKS)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$event->add_link("Blocks Editor", make_link("blocks/list"));
|
|
|
|
}
|
|
|
|
}
|
2012-03-19 18:40:37 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event)
|
|
|
|
{
|
2019-10-02 09:49:32 +00:00
|
|
|
global $cache, $database, $page, $user;
|
2012-03-19 18:40:37 +00:00
|
|
|
|
2019-10-02 09:49:32 +00:00
|
|
|
$blocks = $cache->get("blocks");
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($blocks === false) {
|
|
|
|
$blocks = $database->get_all("SELECT * FROM blocks");
|
2019-10-02 09:49:32 +00:00
|
|
|
$cache->set("blocks", $blocks, 600);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
foreach ($blocks as $block) {
|
|
|
|
$path = implode("/", $event->args);
|
|
|
|
if (strlen($path) < 4000 && fnmatch($block['pages'], $path)) {
|
|
|
|
$b = new Block($block['title'], $block['content'], $block['area'], $block['priority']);
|
|
|
|
$b->is_content = false;
|
|
|
|
$page->add_block($b);
|
|
|
|
}
|
|
|
|
}
|
2011-03-02 10:50:22 +00:00
|
|
|
|
2019-07-09 14:10:21 +00:00
|
|
|
if ($event->page_matches("blocks") && $user->can(Permissions::MANAGE_BLOCKS)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($event->get_arg(0) == "add") {
|
|
|
|
if ($user->check_auth_token()) {
|
|
|
|
$database->execute("
|
2012-03-19 18:40:37 +00:00
|
|
|
INSERT INTO blocks (pages, title, area, priority, content)
|
2019-11-27 11:22:46 +00:00
|
|
|
VALUES (:pages, :title, :area, :priority, :content)
|
|
|
|
", ['pages'=>$_POST['pages'], 'title'=>$_POST['title'], 'area'=>$_POST['area'], 'priority'=>(int)$_POST['priority'], 'content'=>$_POST['content']]);
|
2019-05-28 16:59:38 +00:00
|
|
|
log_info("blocks", "Added Block #".($database->get_last_insert_id('blocks_id_seq'))." (".$_POST['title'].")");
|
2019-10-02 09:49:32 +00:00
|
|
|
$cache->delete("blocks");
|
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("blocks/list"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($event->get_arg(0) == "update") {
|
|
|
|
if ($user->check_auth_token()) {
|
|
|
|
if (!empty($_POST['delete'])) {
|
|
|
|
$database->execute("
|
2012-03-19 18:40:37 +00:00
|
|
|
DELETE FROM blocks
|
2019-11-27 11:22:46 +00:00
|
|
|
WHERE id=:id
|
|
|
|
", ['id'=>$_POST['id']]);
|
2019-05-28 16:59:38 +00:00
|
|
|
log_info("blocks", "Deleted Block #".$_POST['id']);
|
|
|
|
} else {
|
|
|
|
$database->execute("
|
2019-11-27 11:22:46 +00:00
|
|
|
UPDATE blocks SET pages=:pages, title=:title, area=:area, priority=:priority, content=:content
|
|
|
|
WHERE id=:id
|
|
|
|
", ['pages'=>$_POST['pages'], 'title'=>$_POST['title'], 'area'=>$_POST['area'], 'priority'=>(int)$_POST['priority'], 'content'=>$_POST['content'], 'id'=>$_POST['id']]);
|
2019-05-28 16:59:38 +00:00
|
|
|
log_info("blocks", "Updated Block #".$_POST['id']." (".$_POST['title'].")");
|
|
|
|
}
|
2019-10-02 09:49:32 +00:00
|
|
|
$cache->delete("blocks");
|
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("blocks/list"));
|
|
|
|
}
|
|
|
|
} elseif ($event->get_arg(0) == "list") {
|
|
|
|
$this->theme->display_blocks($database->get_all("SELECT * FROM blocks ORDER BY area, priority"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-03-02 10:50:22 +00:00
|
|
|
}
|