2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2009-11-24 14:07:18 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2020-01-29 00:49:21 +00:00
|
|
|
class CreateTipEvent extends Event
|
|
|
|
{
|
2021-03-14 23:43:50 +00:00
|
|
|
public bool $enable;
|
|
|
|
public string $image;
|
|
|
|
public string $text;
|
|
|
|
|
2020-01-29 00:49:21 +00:00
|
|
|
public function __construct(bool $enable, string $image, string $text)
|
|
|
|
{
|
2020-01-28 21:19:59 +00:00
|
|
|
parent::__construct();
|
|
|
|
$this->enable = $enable;
|
|
|
|
$this->image = $image;
|
|
|
|
$this->text = $text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 00:49:21 +00:00
|
|
|
class DeleteTipEvent extends Event
|
|
|
|
{
|
2021-03-14 23:43:50 +00:00
|
|
|
public int $tip_id;
|
2020-01-29 00:49:21 +00:00
|
|
|
public function __construct(int $tip_id)
|
|
|
|
{
|
2020-01-28 21:19:59 +00:00
|
|
|
parent::__construct();
|
|
|
|
$this->tip_id = $tip_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class Tips extends Extension
|
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
/** @var TipsTheme */
|
2023-06-27 14:56:49 +00:00
|
|
|
protected Themelet $theme;
|
2020-01-26 13:19:35 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
global $database;
|
2009-11-24 14:07:18 +00:00
|
|
|
|
2019-11-03 19:49:52 +00:00
|
|
|
if ($this->get_version("ext_tips_version") < 1) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->create_table("tips", "
|
2020-10-26 18:22:47 +00:00
|
|
|
id SCORE_AIPK,
|
2020-10-26 22:37:25 +00:00
|
|
|
enable BOOLEAN NOT NULL DEFAULT FALSE,
|
2020-10-26 18:22:47 +00:00
|
|
|
image TEXT NOT NULL,
|
|
|
|
text TEXT NOT NULL,
|
|
|
|
");
|
2009-11-24 14:07:18 +00:00
|
|
|
|
2020-10-26 18:22:47 +00:00
|
|
|
$this->set_version("ext_tips_version", 2);
|
|
|
|
}
|
|
|
|
if ($this->get_version("ext_tips_version") < 2) {
|
|
|
|
$database->standardise_boolean("tips", "enable");
|
|
|
|
$this->set_version("ext_tips_version", 2);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $page, $user;
|
|
|
|
|
|
|
|
$this->getTip();
|
|
|
|
|
2024-02-10 23:03:14 +00:00
|
|
|
if ($event->page_matches("tips/list", permission: Permissions::TIPS_ADMIN)) {
|
|
|
|
$this->manageTips();
|
|
|
|
$this->getAll();
|
|
|
|
}
|
|
|
|
if ($event->page_matches("tips/save", method: "POST", permission: Permissions::TIPS_ADMIN)) {
|
|
|
|
send_event(new CreateTipEvent(
|
|
|
|
$event->get_POST("enable") == "on",
|
|
|
|
$event->req_POST("image"),
|
|
|
|
$event->req_POST("text")
|
|
|
|
));
|
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
|
|
|
$page->set_redirect(make_link("tips/list"));
|
|
|
|
}
|
2024-02-11 11:34:09 +00:00
|
|
|
if ($event->page_matches("tips/status/{tipID}", permission: Permissions::TIPS_ADMIN)) {
|
2024-02-10 23:03:14 +00:00
|
|
|
// FIXME: HTTP GET CSRF
|
2024-02-11 11:34:09 +00:00
|
|
|
$tipID = $event->get_iarg('tipID');
|
2024-02-10 23:03:14 +00:00
|
|
|
$this->setStatus($tipID);
|
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
|
|
|
$page->set_redirect(make_link("tips/list"));
|
|
|
|
}
|
2024-02-11 11:34:09 +00:00
|
|
|
if ($event->page_matches("tips/delete/{tipID}", permission: Permissions::TIPS_ADMIN)) {
|
2024-02-10 23:03:14 +00:00
|
|
|
// FIXME: HTTP GET CSRF
|
2024-02-11 11:34:09 +00:00
|
|
|
$tipID = $event->get_iarg('tipID');
|
2024-02-10 23:03:14 +00:00
|
|
|
send_event(new DeleteTipEvent($tipID));
|
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
|
|
|
$page->set_redirect(make_link("tips/list"));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event): void
|
2019-08-02 19:54:48 +00:00
|
|
|
{
|
|
|
|
global $user;
|
2023-11-11 21:49:12 +00:00
|
|
|
if ($event->parent === "system") {
|
2019-09-29 18:00:51 +00:00
|
|
|
if ($user->can(Permissions::TIPS_ADMIN)) {
|
2019-08-02 19:54:48 +00:00
|
|
|
$event->add_nav_link("tips", new Link('tips/list'), "Tips Editor");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onUserBlockBuilding(UserBlockBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $user;
|
2019-09-29 18:00:51 +00:00
|
|
|
if ($user->can(Permissions::TIPS_ADMIN)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$event->add_link("Tips Editor", make_link("tips/list"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
private function manageTips(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$data_href = get_base_href();
|
|
|
|
$url = $data_href."/ext/tips/images/";
|
|
|
|
|
2024-01-20 20:48:47 +00:00
|
|
|
$dirPath = dir_ex('./ext/tips/images');
|
2019-05-28 16:59:38 +00:00
|
|
|
$images = [];
|
|
|
|
while (($file = $dirPath->read()) !== false) {
|
|
|
|
if ($file[0] != ".") {
|
|
|
|
$images[] = trim($file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$dirPath->close();
|
|
|
|
sort($images);
|
|
|
|
|
|
|
|
$this->theme->manageTips($url, $images);
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onCreateTip(CreateTipEvent $event): void
|
2020-01-29 00:49:21 +00:00
|
|
|
{
|
2019-05-28 16:59:38 +00:00
|
|
|
global $database;
|
|
|
|
$database->execute(
|
|
|
|
"
|
2009-11-24 14:07:18 +00:00
|
|
|
INSERT INTO tips (enable, image, text)
|
2019-11-27 11:22:46 +00:00
|
|
|
VALUES (:enable, :image, :text)",
|
2023-11-11 21:49:12 +00:00
|
|
|
["enable" => $event->enable, "image" => $event->image, "text" => $event->text]
|
2019-05-28 16:59:38 +00:00
|
|
|
);
|
|
|
|
}
|
2009-11-24 14:07:18 +00:00
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
private function getTip(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $database;
|
2009-11-24 14:07:18 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$data_href = get_base_href();
|
|
|
|
$url = $data_href."/ext/tips/images/";
|
2009-11-24 14:07:18 +00:00
|
|
|
|
2020-10-26 18:30:42 +00:00
|
|
|
$tip = $database->get_row("
|
|
|
|
SELECT *
|
|
|
|
FROM tips
|
|
|
|
WHERE enable = :true
|
|
|
|
ORDER BY RAND()
|
|
|
|
LIMIT 1
|
2023-11-11 21:49:12 +00:00
|
|
|
", ["true" => true]);
|
2009-11-24 14:07:18 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($tip) {
|
|
|
|
$this->theme->showTip($url, $tip);
|
|
|
|
}
|
|
|
|
}
|
2009-11-24 14:07:18 +00:00
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
private function getAll(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $database;
|
2009-11-24 14:07:18 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$data_href = get_base_href();
|
|
|
|
$url = $data_href."/ext/tips/images/";
|
2009-11-24 14:07:18 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$tips = $database->get_all("SELECT * FROM tips ORDER BY id ASC");
|
2009-11-24 14:07:18 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->theme->showAll($url, $tips);
|
|
|
|
}
|
2009-11-24 14:07:18 +00:00
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
private function setStatus(int $tipID): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $database;
|
2009-11-24 14:07:18 +00:00
|
|
|
|
2023-11-11 21:49:12 +00:00
|
|
|
$tip = $database->get_row("SELECT * FROM tips WHERE id = :id ", ["id" => $tipID]);
|
2009-11-24 14:07:18 +00:00
|
|
|
|
2020-10-26 18:22:47 +00:00
|
|
|
$enable = bool_escape($tip['enable']);
|
2009-11-24 14:07:18 +00:00
|
|
|
|
2023-11-11 21:49:12 +00:00
|
|
|
$database->execute("UPDATE tips SET enable = :enable WHERE id = :id", ["enable" => $enable, "id" => $tipID]);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2009-11-24 14:07:18 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onDeleteTip(DeleteTipEvent $event): void
|
2020-01-29 00:49:21 +00:00
|
|
|
{
|
2019-05-28 16:59:38 +00:00
|
|
|
global $database;
|
2023-11-11 21:49:12 +00:00
|
|
|
$database->execute("DELETE FROM tips WHERE id = :id", ["id" => $event->tip_id]);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2009-11-24 14:07:18 +00:00
|
|
|
}
|