Added window title option to post title extension

This commit is contained in:
Matthew Barbour 2019-07-13 17:12:03 -05:00 committed by matthew
parent c3f2d2e1bd
commit dd6c3b2321
14 changed files with 131 additions and 81 deletions

View file

@ -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";
}

View file

@ -1 +1,13 @@
<?php
class PostTitleSetEvent extends Event
{
public $image;
public $title;
public function __construct(Image $image, String $title)
{
$this->image = $image;
$this->title = $title;
}
}

View file

@ -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;
}

View file

@ -0,0 +1,23 @@
<?php
class DisplayingImageEvent extends Event
{
/** @var Image */
public $image;
public $title;
public function __construct(Image $image)
{
$this->image = $image;
}
public function get_image(): Image
{
return $this->image;
}
public function set_title(String $title) {
$this->title = $title;
}
}

View file

@ -0,0 +1,25 @@
<?php
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;
}
}

View file

@ -0,0 +1,25 @@
<?php
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;
}
}

View file

@ -0,0 +1,12 @@
<?php
class ImageInfoSetEvent extends Event
{
/** @var Image */
public $image;
public function __construct(Image $image)
{
$this->image = $image;
}
}

View file

@ -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);
}
}

View file

@ -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));

View file

@ -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));

View file

@ -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));

View file

@ -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));
}

View file

@ -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));

View file

@ -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));