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

113 lines
4.6 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
2011-03-02 10:50:22 +00:00
namespace Shimmie2;
class Blocks extends Extension
{
2020-02-04 00:46:36 +00:00
/** @var BlocksTheme */
2023-06-27 14:56:49 +00:00
protected Themelet $theme;
2020-02-04 00:46:36 +00:00
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event)
{
2020-01-26 13:19:35 +00:00
global $database;
2019-11-03 19:49:52 +00:00
if ($this->get_version("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,
2023-06-26 04:46:43 +00:00
content TEXT NOT NULL,
userclass TEXT
");
$database->execute("CREATE INDEX blocks_pages_idx ON blocks(pages)", []);
2023-06-26 04:46:43 +00:00
$this->set_version("ext_blocks_version", 2);
}
if ($this->get_version("ext_blocks_version") < 2) {
$database->execute("ALTER TABLE blocks ADD COLUMN userclass TEXT");
2023-06-26 04:46:43 +00:00
$this->set_version("ext_blocks_version", 2);
}
}
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event)
{
global $user;
2019-09-29 13:30:55 +00:00
if ($event->parent==="system") {
if ($user->can(Permissions::MANAGE_BLOCKS)) {
$event->add_nav_link("blocks", new Link('blocks/list'), "Blocks Editor");
}
}
}
public function onUserBlockBuilding(UserBlockBuildingEvent $event)
{
global $user;
2019-07-09 14:10:21 +00:00
if ($user->can(Permissions::MANAGE_BLOCKS)) {
$event->add_link("Blocks Editor", make_link("blocks/list"));
}
}
public function onPageRequest(PageRequestEvent $event)
{
global $cache, $database, $page, $user;
$blocks = $cache->get("blocks");
if (is_null($blocks)) {
$blocks = $database->get_all("SELECT * FROM blocks");
$cache->set("blocks", $blocks, 600);
}
foreach ($blocks as $block) {
$path = implode("/", $event->args);
if (strlen($path) < 4000 && fnmatch($block['pages'], $path)) {
2020-01-30 10:26:36 +00:00
$b = new Block($block['title'], $block['content'], $block['area'], (int)$block['priority']);
$b->is_content = false;
2023-06-26 04:46:43 +00:00
# Split by comma, trimming whitespaces, and not allowing empty elements.
$userclasses = preg_split('/\s*,+\s*/', strtolower($block['userclass'] ?? ""), 0, PREG_SPLIT_NO_EMPTY);
2023-06-26 04:46:43 +00:00
if (empty($userclasses) || in_array(strtolower($user->class->name), $userclasses)) {
$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)) {
if ($event->get_arg(0) == "add") {
if ($user->check_auth_token()) {
$database->execute("
2023-06-26 04:46:43 +00:00
INSERT INTO blocks (pages, title, area, priority, content, userclass)
VALUES (:pages, :title, :area, :priority, :content, :userclass)
", ['pages'=>$_POST['pages'], 'title'=>$_POST['title'], 'area'=>$_POST['area'], 'priority'=>(int)$_POST['priority'], 'content'=>$_POST['content'], 'userclass'=>$_POST['userclass']]);
log_info("blocks", "Added Block #".($database->get_last_insert_id('blocks_id_seq'))." (".$_POST['title'].")");
$cache->delete("blocks");
2019-06-19 01:58:28 +00:00
$page->set_mode(PageMode::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
2019-11-27 11:22:46 +00:00
WHERE id=:id
", ['id'=>$_POST['id']]);
log_info("blocks", "Deleted Block #".$_POST['id']);
} else {
$database->execute("
2023-06-26 04:46:43 +00:00
UPDATE blocks SET pages=:pages, title=:title, area=:area, priority=:priority, content=:content, userclass=:userclass
2019-11-27 11:22:46 +00:00
WHERE id=:id
2023-06-26 04:46:43 +00:00
", ['pages'=>$_POST['pages'], 'title'=>$_POST['title'], 'area'=>$_POST['area'], 'priority'=>(int)$_POST['priority'], 'content'=>$_POST['content'], 'userclass'=>$_POST['userclass'], 'id'=>$_POST['id']]);
log_info("blocks", "Updated Block #".$_POST['id']." (".$_POST['title'].")");
}
$cache->delete("blocks");
2019-06-19 01:58:28 +00:00
$page->set_mode(PageMode::REDIRECT);
$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
}