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

113 lines
3.4 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 */
2023-06-27 14:56:49 +00:00
protected Themelet $theme;
2020-02-04 00:46:36 +00:00
public function get_priority(): int
{
return 60;
}
public function onInitExt(InitExtEvent $event): void
{
global $config;
$config->set_default_bool(PostTitlesConfig::DEFAULT_TO_FILENAME, false);
$config->set_default_bool(PostTitlesConfig::SHOW_IN_WINDOW_TITLE, false);
Image::$prop_types["title"] = ImagePropType::STRING;
}
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event): void
{
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): void
{
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): void
{
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): void
{
global $user;
$title = $event->get_param('title');
if ($user->can(Permissions::EDIT_IMAGE_TITLE) && !is_null($title)) {
send_event(new PostTitleSetEvent($event->image, $title));
}
}
public function onPostTitleSet(PostTitleSetEvent $event): void
{
$this->set_title($event->image->id, $event->title);
}
public function onSetupBuilding(SetupBuildingEvent $event): void
{
$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();
}
public function onBulkExport(BulkExportEvent $event): void
2020-06-02 23:05:09 +00:00
{
$event->fields["title"] = $event->image['title'];
2020-06-02 23:05:09 +00:00
}
public function onBulkImport(BulkImportEvent $event): void
2020-06-02 23:05:09 +00:00
{
2023-11-11 21:49:12 +00:00
if (array_key_exists("title", $event->fields) && $event->fields['title'] != null) {
2023-02-03 20:03:04 +00:00
$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): void
{
global $database;
2023-11-11 21:49:12 +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;
}
}