2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-07-09 14:23:46 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2019-07-09 14:23:46 +00:00
|
|
|
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
|
|
|
|
2019-07-13 22:12:03 +00:00
|
|
|
public function get_priority(): int
|
|
|
|
{
|
|
|
|
return 60;
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onInitExt(InitExtEvent $event): void
|
2019-07-09 14:23:46 +00:00
|
|
|
{
|
2019-10-02 10:23:57 +00:00
|
|
|
global $config;
|
2019-07-09 14:23:46 +00:00
|
|
|
|
|
|
|
$config->set_default_bool(PostTitlesConfig::DEFAULT_TO_FILENAME, false);
|
2019-07-13 22:12:03 +00:00
|
|
|
$config->set_default_bool(PostTitlesConfig::SHOW_IN_WINDOW_TITLE, false);
|
2024-01-15 18:01:48 +00:00
|
|
|
Image::$prop_types["title"] = ImagePropType::STRING;
|
2019-07-09 14:23:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event): void
|
2019-07-09 14:23:46 +00:00
|
|
|
{
|
2019-11-03 19:04:57 +00:00
|
|
|
global $database;
|
2019-07-09 14:23:46 +00:00
|
|
|
|
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);
|
2019-07-09 14:23:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onDisplayingImage(DisplayingImageEvent $event): void
|
2019-07-13 22:12:03 +00:00
|
|
|
{
|
2020-05-28 13:30:18 +00:00
|
|
|
global $config, $page;
|
2019-07-13 22:12:03 +00:00
|
|
|
|
2019-09-29 13:30:55 +00:00
|
|
|
if ($config->get_bool(PostTitlesConfig::SHOW_IN_WINDOW_TITLE)) {
|
2020-05-28 13:30:18 +00:00
|
|
|
$page->set_title(self::get_title($event->get_image()));
|
2019-07-13 22:12:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event): void
|
2019-07-09 14:23:46 +00:00
|
|
|
{
|
|
|
|
global $user;
|
|
|
|
|
|
|
|
$event->add_part($this->theme->get_title_set_html(self::get_title($event->image), $user->can(Permissions::EDIT_IMAGE_TITLE)), 10);
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onImageInfoSet(ImageInfoSetEvent $event): void
|
2019-07-09 14:23:46 +00:00
|
|
|
{
|
|
|
|
global $user;
|
2024-02-20 21:28:14 +00:00
|
|
|
$title = $event->get_param('title');
|
|
|
|
if ($user->can(Permissions::EDIT_IMAGE_TITLE) && !is_null($title)) {
|
|
|
|
send_event(new PostTitleSetEvent($event->image, $title));
|
2019-07-09 14:23:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPostTitleSet(PostTitleSetEvent $event): void
|
2019-07-09 14:23:46 +00:00
|
|
|
{
|
|
|
|
$this->set_title($event->image->id, $event->title);
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event): void
|
2019-07-09 14:23:46 +00:00
|
|
|
{
|
2020-10-26 15:13:28 +00:00
|
|
|
$sb = $event->panel->create_new_block("Post Titles");
|
2019-07-09 14:23:46 +00:00
|
|
|
$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);
|
2019-07-09 14:23:46 +00:00
|
|
|
$sb->end_table();
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onBulkExport(BulkExportEvent $event): void
|
2020-06-02 23:05:09 +00:00
|
|
|
{
|
2024-01-15 17:12:36 +00:00
|
|
|
$event->fields["title"] = $event->image['title'];
|
2020-06-02 23:05:09 +00:00
|
|
|
}
|
2024-01-15 17:12:36 +00:00
|
|
|
|
2024-01-15 11:52:35 +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
|
|
|
}
|
|
|
|
}
|
2019-07-13 22:12:03 +00:00
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
private function set_title(int $image_id, string $title): void
|
2019-07-09 14:23:46 +00:00
|
|
|
{
|
|
|
|
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]);
|
2020-10-09 12:47:42 +00:00
|
|
|
log_info("post_titles", "Title for >>{$image_id} set to: ".$title);
|
2019-07-09 14:23:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function get_title(Image $image): string
|
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
|
2024-01-15 17:12:36 +00:00
|
|
|
$title = $image['title'] ?? "";
|
2019-09-29 13:30:55 +00:00
|
|
|
if (empty($title) && $config->get_bool(PostTitlesConfig::DEFAULT_TO_FILENAME)) {
|
2019-07-09 14:23:46 +00:00
|
|
|
$info = pathinfo($image->filename);
|
2019-09-29 13:30:55 +00:00
|
|
|
if (array_key_exists("extension", $info)) {
|
2019-07-13 22:12:03 +00:00
|
|
|
$title = basename($image->filename, '.' . $info['extension']);
|
|
|
|
} else {
|
|
|
|
$title = $image->filename;
|
|
|
|
}
|
2019-07-09 14:23:46 +00:00
|
|
|
}
|
|
|
|
return $title;
|
|
|
|
}
|
|
|
|
}
|