New post titles extension, resolves #19

This commit is contained in:
Matthew Barbour 2019-07-09 09:23:46 -05:00 committed by matthew
parent 45df025e7d
commit c3f2d2e1bd
6 changed files with 123 additions and 0 deletions

View file

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

View file

@ -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,

View file

@ -0,0 +1,8 @@
<?php
abstract class PostTitlesConfig
{
public const VERSION = "ext_post_titles_version";
public const DEFAULT_TO_FILENAME = "post_titles_default_to_filename";
}

View file

@ -0,0 +1 @@
<?php

85
ext/post_titles/main.php Normal file
View file

@ -0,0 +1,85 @@
<?php
/**
* Name: Post Titles
* Author: Matthew Barbour <matthew@darkholme.net>
* 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;
}
}

25
ext/post_titles/theme.php Normal file
View file

@ -0,0 +1,25 @@
<?php
class PostTitlesTheme extends Themelet
{
public function get_title_set_html(string $title, bool $can_set): string
{
$html = "
<tr>
<th>Title</th>
<td>
".($can_set ? "
<span class='view'>".html_escape($title)."</span>
<span class='edit'>
<input class='edit' type='text' name='post_title' value='".html_escape($title)."' />
</span>
" : html_escape("
$title
"))."
</td>
</tr>
";
return $html;
}
}