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

104 lines
4.7 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): void
{
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): void
{
global $user;
2023-11-11 21:49:12 +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): void
{
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): void
{
global $cache, $database, $page, $user;
2023-12-14 17:06:54 +00:00
$blocks = cache_get_or_set("blocks", fn () => $database->get_all("SELECT * FROM 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
2024-02-11 11:34:09 +00:00
if ($event->page_matches("blocks/add", method: "POST", permission: Permissions::MANAGE_BLOCKS)) {
$database->execute("
INSERT INTO blocks (pages, title, area, priority, content, userclass)
VALUES (:pages, :title, :area, :priority, :content, :userclass)
", ['pages' => $event->req_POST('pages'), 'title' => $event->req_POST('title'), 'area' => $event->req_POST('area'), 'priority' => (int)$event->req_POST('priority'), 'content' => $event->req_POST('content'), 'userclass' => $event->req_POST('userclass')]);
2024-02-11 11:34:09 +00:00
log_info("blocks", "Added Block #".($database->get_last_insert_id('blocks_id_seq'))." (".$event->req_POST('title').")");
$cache->delete("blocks");
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("blocks/list"));
}
if ($event->page_matches("blocks/update", method: "POST", permission: Permissions::MANAGE_BLOCKS)) {
2024-04-18 14:40:52 +00:00
if (!is_null($event->get_POST('delete'))) {
2024-02-11 11:34:09 +00:00
$database->execute("
DELETE FROM blocks
WHERE id=:id
", ['id' => $event->req_POST('id')]);
2024-02-11 11:34:09 +00:00
log_info("blocks", "Deleted Block #".$event->req_POST('id'));
} else {
$database->execute("
UPDATE blocks SET pages=:pages, title=:title, area=:area, priority=:priority, content=:content, userclass=:userclass
WHERE id=:id
", ['pages' => $event->req_POST('pages'), 'title' => $event->req_POST('title'), 'area' => $event->req_POST('area'), 'priority' => (int)$event->req_POST('priority'), 'content' => $event->req_POST('content'), 'userclass' => $event->req_POST('userclass'), 'id' => $event->req_POST('id')]);
2024-02-11 11:34:09 +00:00
log_info("blocks", "Updated Block #".$event->req_POST('id')." (".$event->req_POST('title').")");
}
2024-02-11 11:34:09 +00:00
$cache->delete("blocks");
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("blocks/list"));
}
if ($event->page_matches("blocks/list", permission: Permissions::MANAGE_BLOCKS)) {
$this->theme->display_blocks($database->get_all("SELECT * FROM blocks ORDER BY area, priority"));
}
}
2011-03-02 10:50:22 +00:00
}