Make SHM_POST_INFO more logical

Rather than having a boolean for view or edit + optional editor, use the
existence of the editor to know if a field is editable
This commit is contained in:
Shish 2023-12-19 11:36:35 +00:00 committed by Shish
parent 60da9efba9
commit 9e7f3cb397
10 changed files with 50 additions and 63 deletions

View file

@ -15,6 +15,7 @@ use function MicroHTML\OPTION;
use function MicroHTML\PRE; use function MicroHTML\PRE;
use function MicroHTML\P; use function MicroHTML\P;
use function MicroHTML\SELECT; use function MicroHTML\SELECT;
use function MicroHTML\SPAN;
use function MicroHTML\TABLE; use function MicroHTML\TABLE;
use function MicroHTML\THEAD; use function MicroHTML\THEAD;
use function MicroHTML\TFOOT; use function MicroHTML\TFOOT;
@ -154,17 +155,20 @@ function SHM_OPTION(string $value, string $text, bool $selected = false): HTMLEl
function SHM_POST_INFO( function SHM_POST_INFO(
HTMLElement|string $title, HTMLElement|string $title,
bool $can_edit, HTMLElement|string|null $view = null,
HTMLElement|string $view, HTMLElement|string|null $edit = null,
HTMLElement|string $edit = "",
): HTMLElement { ): HTMLElement {
return TR( if(!is_null($view) && !is_null($edit)) {
TH(["width" => "50px"], $title), $show = emptyHTML(
$can_edit ? SPAN(["class" => "view"], $view),
emptyHTML( SPAN(["class" => "edit"], $edit),
TD(["class" => "view"], $view), );
TD(["class" => "edit"], $edit), } elseif(!is_null($edit)) {
) : $show = $edit;
TD($view) } elseif(!is_null($view)) {
); $show = $view;
} else {
$show = "???";
}
return TR(TH(["width" => "50px"], $title), TD($show));
} }

View file

@ -15,7 +15,6 @@ class ArtistsTheme extends Themelet
{ {
return SHM_POST_INFO( return SHM_POST_INFO(
"Author", "Author",
true,
$author, $author,
INPUT(["type" => "text", "name" => "tag_edit__author", "value" => $author]) INPUT(["type" => "text", "name" => "tag_edit__author", "value" => $author])
); );

View file

@ -70,7 +70,7 @@ class ImageViewCounter extends Extension
["image_id" => $event->image->id] ["image_id" => $event->image->id]
); );
$event->add_part(SHM_POST_INFO("Views", false, $view_count, ""), 38); $event->add_part(SHM_POST_INFO("Views", $view_count), 38);
} }
} }

View file

@ -14,9 +14,8 @@ class PostTitlesTheme extends Themelet
{ {
return SHM_POST_INFO( return SHM_POST_INFO(
"Title", "Title",
$can_set,
$title, $title,
INPUT(["type" => "text", "name" => "post_title", "value" => $title]) $can_set ? INPUT(["type" => "text", "name" => "post_title", "value" => $title]) : null
); );
} }
} }

View file

@ -20,9 +20,8 @@ class RatingsTheme extends Themelet
{ {
return SHM_POST_INFO( return SHM_POST_INFO(
"Rating", "Rating",
$can_rate,
A(["href" => search_link(["rating=$rating"])], Ratings::rating_to_human($rating)), A(["href" => search_link(["rating=$rating"])], Ratings::rating_to_human($rating)),
$this->get_selection_rater_html("rating", selected_options: [$rating]) $can_rate ? $this->get_selection_rater_html("rating", selected_options: [$rating]) : null
); );
} }

View file

@ -39,9 +39,8 @@ class RelationshipsTheme extends Themelet
return SHM_POST_INFO( return SHM_POST_INFO(
"Parent", "Parent",
!$user->is_anonymous(),
strval($image->parent_id) ?: "None", strval($image->parent_id) ?: "None",
INPUT(["type" => "number", "name" => "tag_edit__parent", "value" => $image->parent_id]) !$user->is_anonymous() ? INPUT(["type" => "number", "name" => "tag_edit__parent", "value" => $image->parent_id]) : null
); );
} }

View file

@ -41,7 +41,6 @@ class Rule34 extends Extension
$event->add_part( $event->add_part(
SHM_POST_INFO( SHM_POST_INFO(
"Links", "Links",
false,
emptyHTML( emptyHTML(
A(["href" => $url0], "File Only"), A(["href" => $url0], "File Only"),
" (", " (",

View file

@ -56,16 +56,15 @@ class TagEditTheme extends Themelet
return SHM_POST_INFO( return SHM_POST_INFO(
"Tags", "Tags",
$user->can(Permissions::EDIT_IMAGE_TAG),
joinHTML(", ", $tag_links), joinHTML(", ", $tag_links),
INPUT([ $user->can(Permissions::EDIT_IMAGE_TAG) ? INPUT([
"class" => "autocomplete_tags", "class" => "autocomplete_tags",
"type" => "text", "type" => "text",
"name" => "tag_edit__tags", "name" => "tag_edit__tags",
"value" => $image->get_tag_list(), "value" => $image->get_tag_list(),
"id" => "tag_editor", "id" => "tag_editor",
"autocomplete" => "off" "autocomplete" => "off"
]) ]) : null
); );
} }
@ -77,9 +76,8 @@ class TagEditTheme extends Themelet
$ip = $user->can(Permissions::VIEW_IP) ? rawHTML(" (" . show_ip($image->owner_ip, "Post posted {$image->posted}") . ")") : ""; $ip = $user->can(Permissions::VIEW_IP) ? rawHTML(" (" . show_ip($image->owner_ip, "Post posted {$image->posted}") . ")") : "";
$info = SHM_POST_INFO( $info = SHM_POST_INFO(
"Uploader", "Uploader",
$user->can(Permissions::EDIT_IMAGE_OWNER),
emptyHTML(A(["class" => "username", "href" => make_link("user/$owner")], $owner), $ip, ", ", $date), emptyHTML(A(["class" => "username", "href" => make_link("user/$owner")], $owner), $ip, ", ", $date),
INPUT(["type" => "text", "name" => "tag_edit__owner", "value" => $owner]) $user->can(Permissions::EDIT_IMAGE_OWNER) ? INPUT(["type" => "text", "name" => "tag_edit__owner", "value" => $owner]) : null
); );
// SHM_POST_INFO returns a TR, let's sneakily append // SHM_POST_INFO returns a TR, let's sneakily append
// a TD with the avatar in it // a TD with the avatar in it
@ -97,12 +95,11 @@ class TagEditTheme extends Themelet
global $user; global $user;
return SHM_POST_INFO( return SHM_POST_INFO(
"Source", "Source",
$user->can(Permissions::EDIT_IMAGE_SOURCE),
DIV( DIV(
["style" => "overflow: hidden; white-space: nowrap; max-width: 350px; text-overflow: ellipsis;"], ["style" => "overflow: hidden; white-space: nowrap; max-width: 350px; text-overflow: ellipsis;"],
$this->format_source($image->get_source()) $this->format_source($image->get_source())
), ),
INPUT(["type" => "text", "name" => "tag_edit__source", "value" => $image->get_source()]) $user->can(Permissions::EDIT_IMAGE_SOURCE) ? INPUT(["type" => "text", "name" => "tag_edit__source", "value" => $image->get_source()]) : null
); );
} }
@ -127,9 +124,8 @@ class TagEditTheme extends Themelet
global $user; global $user;
return SHM_POST_INFO( return SHM_POST_INFO(
"Locked", "Locked",
$user->can(Permissions::EDIT_IMAGE_LOCK),
$image->is_locked() ? "Yes (Only admins may edit these details)" : "No", $image->is_locked() ? "Yes (Only admins may edit these details)" : "No",
INPUT(["type" => "checkbox", "name" => "tag_edit__locked", "checked" => $image->is_locked()]) $user->can(Permissions::EDIT_IMAGE_LOCK) ? INPUT(["type" => "checkbox", "name" => "tag_edit__locked", "checked" => $image->is_locked()]) : null
); );
} }
} }

View file

@ -123,7 +123,7 @@ class ViewImage extends Extension
global $config; global $config;
$image_info = $config->get_string(ImageConfig::INFO); $image_info = $config->get_string(ImageConfig::INFO);
if ($image_info) { if ($image_info) {
$event->add_part(SHM_POST_INFO("Info", false, $event->image->get_info()), 85); $event->add_part(SHM_POST_INFO("Info", $event->image->get_info()), 85);
} }
} }
} }

View file

@ -27,36 +27,31 @@ class CustomTagEditTheme extends TagEditTheme
public function get_tag_editor_html(Image $image): HTMLElement public function get_tag_editor_html(Image $image): HTMLElement
{ {
$h_tags = html_escape($image->get_tag_list()); global $user;
return rawHTML(" return SHM_POST_INFO(
<tr> "Tags",
<th width='50px'><a href='".make_link("tag_history/{$image->id}")."'>Tags</a></th> INPUT([
<td> "type" => "text",
<input type='text' name='tag_edit__tags' value='$h_tags'> "name" => "tag_edit__tags",
</td> "value" => $image->get_tag_list(),
</tr> "autocomplete" => "off"
"); ])
);
} }
public function get_source_editor_html(Image $image): HTMLElement public function get_source_editor_html(Image $image): HTMLElement
{ {
global $user; global $user;
$h_source = html_escape($image->get_source()); return SHM_POST_INFO(
$f_source = $this->format_source($image->get_source()); A(["href" => make_link("source_history/{$image->id}")], rawHTML("Source&nbsp;Link")),
$style = "overflow: hidden; white-space: nowrap; max-width: 350px; text-overflow: ellipsis;"; emptyHTML(
return rawHTML(" DIV(
<tr> ["style" => "overflow: hidden; white-space: nowrap; max-width: 350px; text-overflow: ellipsis;"],
<th><a href='".make_link("source_history/{$image->id}")."'>Source&nbsp;Link</a></th> $this->format_source($image->get_source())
<td> ),
".($user->can("edit_image_source") ? " $user->can(Permissions::EDIT_IMAGE_SOURCE) ? INPUT(["type" => "text", "name" => "tag_edit__source", "value" => $image->get_source()]) : null
<div class='view' style='$style'>$f_source</div> )
<input class='edit' type='text' name='tag_edit__source' value='$h_source'> );
" : "
<div style='$style'>$f_source</div>
")."
</td>
</tr>
");
} }
public function get_user_editor_html(Image $image): HTMLElement public function get_user_editor_html(Image $image): HTMLElement
@ -67,13 +62,12 @@ class CustomTagEditTheme extends TagEditTheme
$ip = $user->can(Permissions::VIEW_IP) ? rawHTML(" (" . show_ip($image->owner_ip, "Post posted {$image->posted}") . ")") : ""; $ip = $user->can(Permissions::VIEW_IP) ? rawHTML(" (" . show_ip($image->owner_ip, "Post posted {$image->posted}") . ")") : "";
$info = SHM_POST_INFO( $info = SHM_POST_INFO(
"Uploader", "Uploader",
$user->can(Permissions::EDIT_IMAGE_OWNER),
emptyHTML( emptyHTML(
A(["class" => "username", "href" => make_link("user/$owner")], $owner), A(["class" => "username", "href" => make_link("user/$owner")], $owner),
$ip, $ip,
", ", ", ",
$date, $date,
INPUT(["type" => "text", "name" => "tag_edit__owner", "value" => $owner]) $user->can(Permissions::EDIT_IMAGE_OWNER) ? INPUT(["type" => "text", "name" => "tag_edit__owner", "value" => $owner]) : null
), ),
); );
// SHM_POST_INFO returns a TR, let's sneakily append // SHM_POST_INFO returns a TR, let's sneakily append
@ -92,11 +86,9 @@ class CustomTagEditTheme extends TagEditTheme
global $user; global $user;
return SHM_POST_INFO( return SHM_POST_INFO(
"Locked", "Locked",
$user->can(Permissions::EDIT_IMAGE_LOCK), $user->can(Permissions::EDIT_IMAGE_LOCK) ?
emptyHTML( INPUT(["type" => "checkbox", "name" => "tag_edit__locked", "checked" => $image->is_locked()]) :
INPUT(["type" => "checkbox", "name" => "tag_edit__locked", "checked" => $image->is_locked()]), emptyHTML($image->is_locked() ? "Yes (Only admins may edit these details)" : "No")
$image->is_locked() ? "Yes (Only admins may edit these details)" : "No",
),
); );
} }
} }