From dd6c3b232149046c768fad400bff93207cf9fcdf Mon Sep 17 00:00:00 2001 From: Matthew Barbour Date: Sat, 13 Jul 2019 17:12:03 -0500 Subject: [PATCH] Added window title option to post title extension --- ext/post_titles/config.php | 1 + .../events/post_title_set_event.php | 12 +++ ext/post_titles/main.php | 24 +++++- ext/view/events/displaying_image_event.php | 23 +++++ .../image_admin_block_building_event.php | 25 ++++++ .../events/image_info_box_building_event.php | 25 ++++++ ext/view/events/image_info_set_event.php | 12 +++ ext/view/main.php | 83 +++---------------- ext/view/theme.php | 2 - themes/danbooru/view.theme.php | 1 - themes/danbooru2/view.theme.php | 1 - themes/futaba/view.theme.php | 1 - themes/lite/view.theme.php | 1 - themes/material/view.theme.php | 1 - 14 files changed, 131 insertions(+), 81 deletions(-) create mode 100644 ext/view/events/displaying_image_event.php create mode 100644 ext/view/events/image_admin_block_building_event.php create mode 100644 ext/view/events/image_info_box_building_event.php create mode 100644 ext/view/events/image_info_set_event.php diff --git a/ext/post_titles/config.php b/ext/post_titles/config.php index 47156d0d..5499bfad 100644 --- a/ext/post_titles/config.php +++ b/ext/post_titles/config.php @@ -5,4 +5,5 @@ abstract class PostTitlesConfig { public const VERSION = "ext_post_titles_version"; public const DEFAULT_TO_FILENAME = "post_titles_default_to_filename"; + public const SHOW_IN_WINDOW_TITLE = "post_titles_show_in_window_title"; } \ No newline at end of file diff --git a/ext/post_titles/events/post_title_set_event.php b/ext/post_titles/events/post_title_set_event.php index b3d9bbc7..57f942f7 100644 --- a/ext/post_titles/events/post_title_set_event.php +++ b/ext/post_titles/events/post_title_set_event.php @@ -1 +1,13 @@ image = $image; + $this->title = $title; + } +} \ No newline at end of file diff --git a/ext/post_titles/main.php b/ext/post_titles/main.php index 654d581d..50f550c3 100644 --- a/ext/post_titles/main.php +++ b/ext/post_titles/main.php @@ -11,11 +11,17 @@ require_once "events/post_title_set_event.php"; class PostTitles extends Extension { + public function get_priority(): int + { + return 60; + } + public function onInitExt(InitExtEvent $event) { global $config, $database; $config->set_default_bool(PostTitlesConfig::DEFAULT_TO_FILENAME, false); + $config->set_default_bool(PostTitlesConfig::SHOW_IN_WINDOW_TITLE, false); if ($config->get_int(PostTitlesConfig::VERSION) < 1) { $this->install(); @@ -32,6 +38,15 @@ class PostTitles extends Extension } } + public function onDisplayingImage(DisplayingImageEvent $event) + { + global $config; + + if($config->get_bool(PostTitlesConfig::SHOW_IN_WINDOW_TITLE)) { + $event->set_title(self::get_title($event->get_image())); + } + } + public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event) { global $user; @@ -59,11 +74,14 @@ class PostTitles extends Extension $sb = new SetupBlock("Post Titles"); $sb->start_table(); $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(); $event->panel->add_block($sb); } + + private function set_title(int $image_id, string $title) { global $database; @@ -78,7 +96,11 @@ class PostTitles extends Extension $title = $image->title??""; if(empty($title) && $config->get_bool(PostTitlesConfig::DEFAULT_TO_FILENAME)) { $info = pathinfo($image->filename); - $title = basename($image->filename,'.'.$info['extension']); + if(array_key_exists("extension",$info)) { + $title = basename($image->filename, '.' . $info['extension']); + } else { + $title = $image->filename; + } } return $title; } diff --git a/ext/view/events/displaying_image_event.php b/ext/view/events/displaying_image_event.php new file mode 100644 index 00000000..3ac3090f --- /dev/null +++ b/ext/view/events/displaying_image_event.php @@ -0,0 +1,23 @@ +image = $image; + } + + public function get_image(): Image + { + return $this->image; + } + + public function set_title(String $title) { + $this->title = $title; + } +} \ No newline at end of file diff --git a/ext/view/events/image_admin_block_building_event.php b/ext/view/events/image_admin_block_building_event.php new file mode 100644 index 00000000..0211065a --- /dev/null +++ b/ext/view/events/image_admin_block_building_event.php @@ -0,0 +1,25 @@ +image = $image; + $this->user = $user; + } + + public function add_part(string $html, int $position=50) + { + while (isset($this->parts[$position])) { + $position++; + } + $this->parts[$position] = $html; + } +} \ No newline at end of file diff --git a/ext/view/events/image_info_box_building_event.php b/ext/view/events/image_info_box_building_event.php new file mode 100644 index 00000000..cb626349 --- /dev/null +++ b/ext/view/events/image_info_box_building_event.php @@ -0,0 +1,25 @@ +image = $image; + $this->user = $user; + } + + public function add_part(string $html, int $position=50) + { + while (isset($this->parts[$position])) { + $position++; + } + $this->parts[$position] = $html; + } +} \ No newline at end of file diff --git a/ext/view/events/image_info_set_event.php b/ext/view/events/image_info_set_event.php new file mode 100644 index 00000000..e6d77d59 --- /dev/null +++ b/ext/view/events/image_info_set_event.php @@ -0,0 +1,12 @@ +image = $image; + } +} \ No newline at end of file diff --git a/ext/view/main.php b/ext/view/main.php index bb2ee8c6..11c9f74e 100644 --- a/ext/view/main.php +++ b/ext/view/main.php @@ -14,80 +14,12 @@ * wish to appear on the "view" page should listen for this, * which only appears when an image actually exists. */ -class DisplayingImageEvent extends Event -{ - /** @var Image */ - public $image; - public function __construct(Image $image) - { - $this->image = $image; - } +require_once "events/displaying_image_event.php"; +require_once "events/image_info_box_building_event.php"; +require_once "events/image_info_set_event.php"; +require_once "events/image_admin_block_building_event.php"; - public function get_image(): Image - { - return $this->image; - } -} - -class ImageInfoBoxBuildingEvent extends Event -{ - /** @var array */ - public $parts = []; - /** @var Image */ - public $image; - /** @var User */ - public $user; - - public function __construct(Image $image, User $user) - { - $this->image = $image; - $this->user = $user; - } - - public function add_part(string $html, int $position=50) - { - while (isset($this->parts[$position])) { - $position++; - } - $this->parts[$position] = $html; - } -} - -class ImageInfoSetEvent extends Event -{ - /** @var Image */ - public $image; - - public function __construct(Image $image) - { - $this->image = $image; - } -} - -class ImageAdminBlockBuildingEvent extends Event -{ - /** @var string[] */ - public $parts = []; - /** @var ?Image */ - public $image = null; - /** @var ?User */ - public $user = null; - - public function __construct(Image $image, User $user) - { - $this->image = $image; - $this->user = $user; - } - - public function add_part(string $html, int $position=50) - { - while (isset($this->parts[$position])) { - $position++; - } - $this->parts[$position] = $html; - } -} class ViewImage extends Extension { @@ -140,7 +72,9 @@ class ViewImage extends Extension $image = Image::by_id($image_id); if (!is_null($image)) { - send_event(new DisplayingImageEvent($image)); + $die = new DisplayingImageEvent($image); + send_event($die); + $page->set_title(html_escape($die->title)); $iabbe = new ImageAdminBlockBuildingEvent($image, $user); send_event($iabbe); ksort($iabbe->parts); @@ -169,6 +103,9 @@ class ViewImage extends Extension send_event($iibbe); ksort($iibbe->parts); $this->theme->display_meta_headers($event->get_image()); + + $event->title = "Image {$event->get_image()->id}: ".$event->get_image()->get_tag_list(); + $this->theme->display_page($event->get_image(), $iibbe->parts); } } diff --git a/ext/view/theme.php b/ext/view/theme.php index de1694ac..ba085159 100644 --- a/ext/view/theme.php +++ b/ext/view/theme.php @@ -20,8 +20,6 @@ class ViewImageTheme extends Themelet public function display_page(Image $image, $editor_parts) { global $page; - - $page->set_title("Image {$image->id}: ".html_escape($image->get_tag_list())); $page->set_heading(html_escape($image->get_tag_list())); $page->add_block(new Block("Navigation", $this->build_navigation($image), "left", 0)); $page->add_block(new Block(null, $this->build_info($image, $editor_parts), "main", 20)); diff --git a/themes/danbooru/view.theme.php b/themes/danbooru/view.theme.php index 3b448c2e..51adbe1b 100644 --- a/themes/danbooru/view.theme.php +++ b/themes/danbooru/view.theme.php @@ -5,7 +5,6 @@ class CustomViewImageTheme extends ViewImageTheme public function display_page(Image $image, $editor_parts) { global $page; - $page->set_title("Image {$image->id}: ".html_escape($image->get_tag_list())); $page->set_heading(html_escape($image->get_tag_list())); $page->add_block(new Block("Navigation", $this->build_navigation($image), "left", 0)); $page->add_block(new Block("Statistics", $this->build_stats($image), "left", 15)); diff --git a/themes/danbooru2/view.theme.php b/themes/danbooru2/view.theme.php index 3e588307..7f3439b5 100644 --- a/themes/danbooru2/view.theme.php +++ b/themes/danbooru2/view.theme.php @@ -5,7 +5,6 @@ class CustomViewImageTheme extends ViewImageTheme public function display_page(Image $image, $editor_parts) { global $page; - $page->set_title("Image {$image->id}: ".html_escape($image->get_tag_list())); $page->set_heading(html_escape($image->get_tag_list())); $page->add_block(new Block("Search", $this->build_navigation($image), "left", 0)); $page->add_block(new Block("Information", $this->build_information($image), "left", 15)); diff --git a/themes/futaba/view.theme.php b/themes/futaba/view.theme.php index feb06743..4bbba64a 100644 --- a/themes/futaba/view.theme.php +++ b/themes/futaba/view.theme.php @@ -5,7 +5,6 @@ class CustomViewImageTheme extends ViewImageTheme public function display_page(Image $image, $editor_parts) { global $page; - $page->set_title("Image {$image->id}: ".html_escape($image->get_tag_list())); $page->set_heading(html_escape($image->get_tag_list())); $page->add_block(new Block(null, $this->build_info($image, $editor_parts), "main", 10)); } diff --git a/themes/lite/view.theme.php b/themes/lite/view.theme.php index aea9b028..f9ce414f 100644 --- a/themes/lite/view.theme.php +++ b/themes/lite/view.theme.php @@ -5,7 +5,6 @@ class CustomViewImageTheme extends ViewImageTheme public function display_page(Image $image, $editor_parts) { global $page; - $page->set_title("Image {$image->id}: ".html_escape($image->get_tag_list())); $page->set_heading(html_escape($image->get_tag_list())); $page->add_block(new Block("Navigation", $this->build_navigation($image), "left", 0)); $page->add_block(new Block("Statistics", $this->build_stats($image), "left", 15)); diff --git a/themes/material/view.theme.php b/themes/material/view.theme.php index 71edf7c6..601faf94 100644 --- a/themes/material/view.theme.php +++ b/themes/material/view.theme.php @@ -8,7 +8,6 @@ class CustomViewImageTheme extends ViewImageTheme public function display_page(Image $image, $editor_parts) { global $page; - $page->set_title("Image {$image->id}: ".html_escape($image->get_tag_list())); $page->set_heading(html_escape($image->get_tag_list())); $page->add_block(new Block(null, $this->build_pin($image), "subtoolbar", 0)); $page->add_block(new Block(null, $this->build_info($image, $editor_parts), "left", 20));