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

112 lines
3.3 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
require_once "config.php";
require_once "events/post_title_set_event.php";
class PostTitles extends Extension
{
2020-02-04 00:46:36 +00:00
/** @var PostTitlesTheme */
protected ?Themelet $theme;
2020-02-04 00:46:36 +00:00
public function get_priority(): int
{
return 60;
}
public function onInitExt(InitExtEvent $event)
{
global $config;
$config->set_default_bool(PostTitlesConfig::DEFAULT_TO_FILENAME, false);
$config->set_default_bool(PostTitlesConfig::SHOW_IN_WINDOW_TITLE, false);
}
2020-01-26 13:19:35 +00:00
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event)
{
2019-11-03 19:04:57 +00:00
global $database;
2019-11-03 19:04:57 +00:00
if ($this->get_version(PostTitlesConfig::VERSION) < 1) {
2020-10-25 21:34:52 +00:00
$database->execute("ALTER TABLE images ADD COLUMN title varchar(255) NULL");
2019-11-03 19:04:57 +00:00
$this->set_version(PostTitlesConfig::VERSION, 1);
}
}
public function onDisplayingImage(DisplayingImageEvent $event)
{
global $config, $page;
2019-09-29 13:30:55 +00:00
if ($config->get_bool(PostTitlesConfig::SHOW_IN_WINDOW_TITLE)) {
$page->set_title(self::get_title($event->get_image()));
}
}
public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event)
{
global $user;
$event->add_part($this->theme->get_title_set_html(self::get_title($event->image), $user->can(Permissions::EDIT_IMAGE_TITLE)), 10);
}
public function onImageInfoSet(ImageInfoSetEvent $event)
{
global $user;
if ($user->can(Permissions::EDIT_IMAGE_TITLE) && isset($_POST["post_title"])) {
$title = $_POST["post_title"];
send_event(new PostTitleSetEvent($event->image, $title));
}
}
public function onPostTitleSet(PostTitleSetEvent $event)
{
$this->set_title($event->image->id, $event->title);
}
public function onSetupBuilding(SetupBuildingEvent $event)
{
$sb = $event->panel->create_new_block("Post Titles");
$sb->start_table();
2019-09-29 13:30:55 +00:00
$sb->add_bool_option(PostTitlesConfig::DEFAULT_TO_FILENAME, "Default to filename", true);
$sb->add_bool_option(PostTitlesConfig::SHOW_IN_WINDOW_TITLE, "Show in window title", true);
$sb->end_table();
}
2020-06-02 23:05:09 +00:00
public function onBulkExport(BulkExportEvent $event)
{
$event->fields["title"] = $event->image->title;
}
public function onBulkImport(BulkImportEvent $event)
{
2023-02-03 20:03:04 +00:00
if (array_key_exists("title", $event->fields) && $event->fields['title']!=null) {
$this->set_title($event->image->id, $event->fields['title']);
2020-06-02 23:05:09 +00:00
}
}
private function set_title(int $image_id, string $title)
{
global $database;
2020-10-25 21:34:52 +00:00
$database->execute("UPDATE images SET title=:title WHERE id=:id", ['title'=>$title, 'id'=>$image_id]);
log_info("post_titles", "Title for >>{$image_id} set to: ".$title);
}
public static function get_title(Image $image): string
{
global $config;
$title = $image->title??"";
2019-09-29 13:30:55 +00:00
if (empty($title) && $config->get_bool(PostTitlesConfig::DEFAULT_TO_FILENAME)) {
$info = pathinfo($image->filename);
2019-09-29 13:30:55 +00:00
if (array_key_exists("extension", $info)) {
$title = basename($image->filename, '.' . $info['extension']);
} else {
$title = $image->filename;
}
}
return $title;
}
}