diff --git a/core/permissions.php b/core/permissions.php index a78af388..4378a9c0 100644 --- a/core/permissions.php +++ b/core/permissions.php @@ -29,6 +29,7 @@ abstract class Permissions public const EDIT_IMAGE_SOURCE = "edit_image_source"; public const EDIT_IMAGE_OWNER = "edit_image_owner"; public const EDIT_IMAGE_LOCK = "edit_image_lock"; + public const EDIT_IMAGE_TITLE = "edit_image_title"; public const BULK_EDIT_IMAGE_TAG = "bulk_edit_image_tag"; public const BULK_EDIT_IMAGE_SOURCE = "bulk_edit_image_source"; public const DELETE_IMAGE = "delete_image"; diff --git a/core/userclass.php b/core/userclass.php index d2d06c0a..a66ad6f7 100644 --- a/core/userclass.php +++ b/core/userclass.php @@ -99,6 +99,7 @@ new UserClass("base", null, [ Permissions::EDIT_IMAGE_SOURCE => false, Permissions::EDIT_IMAGE_OWNER => false, Permissions::EDIT_IMAGE_LOCK => false, + Permissions::EDIT_IMAGE_TITLE => false, Permissions::BULK_EDIT_IMAGE_TAG => false, Permissions::BULK_EDIT_IMAGE_SOURCE => false, Permissions::DELETE_IMAGE => false, @@ -146,6 +147,7 @@ new UserClass("user", "base", [ Permissions::CREATE_COMMENT => true, Permissions::EDIT_IMAGE_TAG => true, Permissions::EDIT_IMAGE_SOURCE => true, + Permissions::EDIT_IMAGE_TITLE => true, Permissions::CREATE_IMAGE_REPORT => true, Permissions::EDIT_IMAGE_RATING => true, @@ -175,6 +177,7 @@ new UserClass("admin", "base", [ Permissions::EDIT_IMAGE_TAG => true, Permissions::EDIT_IMAGE_SOURCE => true, Permissions::EDIT_IMAGE_OWNER => true, + Permissions::EDIT_IMAGE_TITLE => true, Permissions::BULK_EDIT_IMAGE_TAG => true, Permissions::BULK_EDIT_IMAGE_SOURCE => true, Permissions::MASS_TAG_EDIT => true, diff --git a/ext/post_titles/config.php b/ext/post_titles/config.php new file mode 100644 index 00000000..47156d0d --- /dev/null +++ b/ext/post_titles/config.php @@ -0,0 +1,8 @@ + + * License: MIT + * Description: Add titles to media posts + */ + +require_once "config.php"; +require_once "events/post_title_set_event.php"; + +class PostTitles extends Extension +{ + public function onInitExt(InitExtEvent $event) + { + global $config, $database; + + $config->set_default_bool(PostTitlesConfig::DEFAULT_TO_FILENAME, false); + + if ($config->get_int(PostTitlesConfig::VERSION) < 1) { + $this->install(); + } + } + + private function install() + { + global $config, $database; + + if ($config->get_int(PostTitlesConfig::VERSION) < 1) { + $database->Execute("ALTER TABLE images ADD COLUMN title varchar(255) NULL"); + $config->set_int(PostTitlesConfig::VERSION, 1); + } + } + + 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 = new SetupBlock("Post Titles"); + $sb->start_table(); + $sb->add_bool_option(PostTitlesConfig::DEFAULT_TO_FILENAME,"Default to filename", true); + $sb->end_table(); + + $event->panel->add_block($sb); + } + + private function set_title(int $image_id, string $title) + { + global $database; + $database->Execute("UPDATE images SET title=? WHERE id=?", [$title, $image_id]); + log_info("post_titles", "Title for Image #{$image_id} set to: ".$title); + } + + public static function get_title(Image $image): string + { + global $config; + + $title = $image->title??""; + if(empty($title) && $config->get_bool(PostTitlesConfig::DEFAULT_TO_FILENAME)) { + $info = pathinfo($image->filename); + $title = basename($image->filename,'.'.$info['extension']); + } + return $title; + } +} diff --git a/ext/post_titles/theme.php b/ext/post_titles/theme.php new file mode 100644 index 00000000..53a6c1b5 --- /dev/null +++ b/ext/post_titles/theme.php @@ -0,0 +1,25 @@ + + Title + + ".($can_set ? " + ".html_escape($title)." + + + + " : html_escape(" + $title + "))." + + + "; + return $html; + } +}