SHM_POST_INFO element for doing info box elements in a standard way

This commit is contained in:
Shish 2023-08-17 18:09:38 +01:00 committed by Shish
parent 849b760b0b
commit 5879184895
12 changed files with 149 additions and 164 deletions

View file

@ -151,3 +151,20 @@ function SHM_OPTION(string $value, string $text, bool $selected=false): HTMLElem
return OPTION(["value"=>$value], $text);
}
function SHM_POST_INFO(
HTMLElement|string $title,
bool $can_edit,
HTMLElement|string $view,
HTMLElement|string $edit = "",
): HTMLElement {
return TR(
TH(["width"=>"50px"], $title),
$can_edit ?
emptyHTML(
TD(["class"=>"view"], $view),
TD(["class"=>"edit"], $edit),
) :
TD($view)
);
}

View file

@ -7,17 +7,18 @@ namespace Shimmie2;
use MicroHTML\HTMLElement;
use function MicroHTML\emptyHTML;
use function MicroHTML\{INPUT,P,SPAN,TD,TH,TR};
use function MicroHTML\{INPUT,P};
class ArtistsTheme extends Themelet
{
public function get_author_editor_html(string $author): string
public function get_author_editor_html(string $author): HTMLElement
{
$h_author = html_escape($author);
return (string)TR(TH("Author", TD(
SPAN(["class"=>"view"], $h_author),
INPUT(["class"=>"edit", "type"=>"text", "name"=>"tag_edit__author", "value"=>$h_author])
)));
return SHM_POST_INFO(
"Author",
true,
$author,
INPUT(["type"=>"text", "name"=>"tag_edit__author", "value"=>$author])
);
}
public function sidebar_options(string $mode, ?int $artistID=null, $is_admin=false): void

View file

@ -4,6 +4,8 @@ declare(strict_types=1);
namespace Shimmie2;
use function MicroHTML\{TD,TH,TR};
class ImageViewCounter extends Extension
{
/** @var ImageViewCounterTheme */
@ -63,15 +65,12 @@ class ImageViewCounter extends Extension
global $user, $database;
if ($user->can(Permissions::SEE_IMAGE_VIEW_COUNTS)) {
$view_count = (int)$database->get_one(
$view_count = (string)$database->get_one(
"SELECT COUNT(*) FROM image_views WHERE image_id =:image_id",
["image_id" => $event->image->id]
);
$event->add_part(
"<tr><th>Views:</th><td>$view_count</td></tr>",
38
);
$event->add_part(SHM_POST_INFO("Views", false, $view_count, ""), 38);
}
}

View file

@ -4,23 +4,19 @@ declare(strict_types=1);
namespace Shimmie2;
use MicroHTML\HTMLElement;
use function MicroHTML\{INPUT};
class PostTitlesTheme extends Themelet
{
public function get_title_set_html(string $title, bool $can_set): string
public function get_title_set_html(string $title, bool $can_set): HTMLElement
{
$html = "
<tr>
<th>Title</th>
<td>
".($can_set ? "
<span class='view'>".html_escape($title)."</span>
<input class='edit' type='text' name='post_title' value='".html_escape($title)."' />
" : html_escape("
$title
"))."
</td>
</tr>
";
return $html;
return SHM_POST_INFO(
"Title",
$can_set,
$title,
INPUT(["type" => "text", "name" => "post_title", "value" => $title])
);
}
}

View file

@ -203,7 +203,7 @@ class Ratings extends Extension
{
global $user;
$event->add_part(
(string)$this->theme->get_rater_html(
$this->theme->get_rater_html(
$event->image->id,
$event->image->rating,
$user->can(Permissions::EDIT_IMAGE_RATING)

View file

@ -18,22 +18,12 @@ class RatingsTheme extends Themelet
public function get_rater_html(int $image_id, string $rating, bool $can_rate): HTMLElement
{
$human_rating = Ratings::rating_to_human($rating);
$html = TR(TH("Rating"));
if ($can_rate) {
$selector = $this->get_selection_rater_html(selected_options: [$rating]);
$html->appendChild(TD(
SPAN(["class"=>"view"], $human_rating),
SPAN(["class"=>"edit"], $selector)
));
} else {
$html->appendChild(TD($human_rating));
}
return $html;
return SHM_POST_INFO(
"Rating",
$can_rate,
Ratings::rating_to_human($rating),
$this->get_selection_rater_html("rating", selected_options: [$rating])
);
}
public function display_form(array $current_ratings)

View file

@ -4,6 +4,10 @@ declare(strict_types=1);
namespace Shimmie2;
use MicroHTML\HTMLElement;
use function MicroHTML\{TR, TH, TD, emptyHTML, DIV, INPUT};
class RelationshipsTheme extends Themelet
{
public function relationship_info(Image $image)
@ -29,26 +33,16 @@ class RelationshipsTheme extends Themelet
}
}
public function get_parent_editor_html(Image $image): string
public function get_parent_editor_html(Image $image): HTMLElement
{
global $user;
$h_parent_id = $image->parent_id;
$s_parent_id = $h_parent_id ?: "None";
$html = "<tr>\n".
" <th>Parent</th>\n".
" <td>\n".
(
!$user->is_anonymous() ?
" <span class='view' style='overflow: hidden; white-space: nowrap;'>{$s_parent_id}</span>\n".
" <input class='edit' type='number' name='tag_edit__parent' type='number' value='{$h_parent_id}'>\n"
:
$s_parent_id
).
" <td>\n".
"</tr>\n";
return $html;
return SHM_POST_INFO(
"Parent",
!$user->is_anonymous(),
$image->parent_id ?: "None",
INPUT(["type"=>"number", "name"=>"tag_edit__parent", "value"=>$image->parent_id])
);
}

View file

@ -4,12 +4,10 @@ declare(strict_types=1);
namespace Shimmie2;
use function MicroHTML\TR;
use function MicroHTML\TH;
use function MicroHTML\TD;
use function MicroHTML\A;
use function MicroHTML\{emptyHTML, A};
if ( // kill these glitched requests immediately
if (
// kill these glitched requests immediately
!empty($_SERVER["REQUEST_URI"])
&& str_contains(@$_SERVER["REQUEST_URI"], "/http")
&& str_contains(@$_SERVER["REQUEST_URI"], "paheal.net")
@ -40,16 +38,19 @@ class Rule34 extends Extension
$image_link = $config->get_string(ImageConfig::ILINK);
$url0 = $event->image->parse_link_template($image_link, 0);
$url1 = $event->image->parse_link_template($image_link, 1);
$html = (string)TR(
TH("Links"),
TD(
A(["href"=>$url0], "File Only"),
" (",
A(["href"=>$url1], "Backup Server"),
")"
)
$event->add_part(
SHM_POST_INFO(
"Links",
false,
emptyHTML(
A(["href" => $url0], "File Only"),
" (",
A(["href" => $url1], "Backup Server"),
")"
)
),
90
);
$event->add_part($html, 90);
}
public function onAdminBuilding(AdminBuildingEvent $event)
@ -66,7 +67,7 @@ class Rule34 extends Extension
{
global $database, $user, $config;
if ($user->can(Permissions::CHANGE_SETTING) && $config->get_bool('r34_comic_integration')) {
$current_state = bool_escape($database->get_one("SELECT comic_admin FROM users WHERE id=:id", ['id'=>$event->display_user->id]));
$current_state = bool_escape($database->get_one("SELECT comic_admin FROM users WHERE id=:id", ['id' => $event->display_user->id]));
$this->theme->show_comic_changer($event->display_user, $current_state);
}
}
@ -128,7 +129,7 @@ class Rule34 extends Extension
]);
$database->execute(
'UPDATE users SET comic_admin=:is_admin WHERE id=:id',
['is_admin'=>$input['is_admin'] ? 't' : 'f', 'id'=>$input['user_id']]
['is_admin' => $input['is_admin'] ? 't' : 'f', 'id' => $input['user_id']]
);
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(referer_or(make_link()));

View file

@ -4,6 +4,10 @@ declare(strict_types=1);
namespace Shimmie2;
use MicroHTML\HTMLElement;
use function MicroHTML\{TR, TH, TD, emptyHTML, rawHTML, joinHTML, DIV, INPUT, A};
class TagEditTheme extends Themelet
{
/*
@ -14,7 +18,7 @@ class TagEditTheme extends Themelet
{
global $page;
$html = "
".make_form(make_link("tag_edit/replace"))."
" . make_form(make_link("tag_edit/replace")) . "
<table class='form'>
<tr><th>Search</th><td><input type='text' name='search' class='autocomplete_tags' autocomplete='off'></tr>
<tr><th>Replace</th><td><input type='text' name='replace' class='autocomplete_tags' autocomplete='off'></td></tr>
@ -37,115 +41,96 @@ class TagEditTheme extends Themelet
return $html;
}
public function get_tag_editor_html(Image $image): string
public function get_tag_editor_html(Image $image): HTMLElement
{
global $user;
$tag_links = [];
foreach ($image->get_tag_array() as $tag) {
$h_tag = html_escape($tag);
$u_tag = url_escape($tag);
$h_link = make_link("post/list/$u_tag/1");
$tag_links[] = "<a href='$h_link'>$h_tag</a>";
$tag_links[] = A([
"href" => make_link("post/list/$u_tag/1"),
"class" => "tag",
"title" => "View all posts tagged $tag"
], $tag);
}
$h_tag_links = Tag::implode($tag_links);
$h_tags = html_escape($image->get_tag_list());
return "
<tr>
<th width='50px'>Tags</th>
<td>
".($user->can(Permissions::EDIT_IMAGE_TAG) ? "
<span class='view'>$h_tag_links</span>
<div class='edit'>
<input class='autocomplete_tags' type='text' name='tag_edit__tags' value='$h_tags' id='tag_editor' autocomplete='off'>
</div>
" : "
$h_tag_links
")."
</td>
</tr>
";
return SHM_POST_INFO(
"Tags",
$user->can(Permissions::EDIT_IMAGE_TAG),
joinHTML(", ", $tag_links),
INPUT([
"class" => "autocomplete_tags",
"type" => "text",
"name" => "tag_edit__tags",
"value" => $image->get_tag_list(),
"id"=>"tag_editor",
"autocomplete" => "off"
])
);
}
public function get_user_editor_html(Image $image): string
public function get_user_editor_html(Image $image): HTMLElement
{
global $user;
$h_owner = html_escape($image->get_owner()->name);
$h_av = $image->get_owner()->get_avatar_html();
$h_date = autodate($image->posted);
$h_ip = $user->can(Permissions::VIEW_IP) ? " (".show_ip($image->owner_ip, "Post posted {$image->posted}").")" : "";
return "
<tr>
<th>Uploader</th>
<td>
".($user->can(Permissions::EDIT_IMAGE_OWNER) ? "
<span class='view'><a class='username' href='".make_link("user/$h_owner")."'>$h_owner</a>$h_ip, $h_date</span>
<input class='edit' type='text' name='tag_edit__owner' value='$h_owner'>
" : "
<a class='username' href='".make_link("user/$h_owner")."'>$h_owner</a>$h_ip, $h_date
")."
</td>
<td width='80px' rowspan='4'>$h_av</td>
</tr>
";
$owner = $image->get_owner()->name;
$date = rawHTML(autodate($image->posted));
$ip = $user->can(Permissions::VIEW_IP) ? rawHTML(" (" . show_ip($image->owner_ip, "Post posted {$image->posted}") . ")") : "";
$info = SHM_POST_INFO(
"Uploader",
$user->can(Permissions::EDIT_IMAGE_OWNER),
emptyHTML(A(["class" => "username", "href" => make_link("user/$owner")], $owner), $ip, ", ", $date),
INPUT(["type" => "text", "name" => "tag_edit__owner", "value" => $owner])
);
// SHM_POST_INFO returns a TR, let's sneakily append
// a TD with the avatar in it
$info->appendChild(
TD(
["width" => "80px", "rowspan" => "4"],
rawHTML($image->get_owner()->get_avatar_html())
)
);
return $info;
}
public function get_source_editor_html(Image $image): string
public function get_source_editor_html(Image $image): HTMLElement
{
global $user;
$h_source = html_escape($image->get_source());
$f_source = $this->format_source($image->get_source());
$style = "overflow: hidden; white-space: nowrap; max-width: 350px; text-overflow: ellipsis;";
return "
<tr>
<th>Source</th>
<td>
".($user->can(Permissions::EDIT_IMAGE_SOURCE) ? "
<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>
";
return SHM_POST_INFO(
"Source",
$user->can(Permissions::EDIT_IMAGE_SOURCE),
DIV(
["style" => "overflow: hidden; white-space: nowrap; max-width: 350px; text-overflow: ellipsis;"],
$this->format_source($image->get_source())
),
INPUT(["type" => "text", "name" => "tag_edit__source", "value" => $image->get_source()])
);
}
protected function format_source(string $source=null): string
protected function format_source(string $source = null): HTMLElement
{
if (!empty($source)) {
if (!str_starts_with($source, "http://") && !str_starts_with($source, "https://")) {
$source = "http://" . $source;
}
$proto_domain = explode("://", $source);
$h_source = html_escape($proto_domain[1]);
$u_source = html_escape($source);
$h_source = $proto_domain[1];
if (str_ends_with($h_source, "/")) {
$h_source = substr($h_source, 0, -1);
}
return "<a href='$u_source'>$h_source</a>";
return A(["href"=>$source], $h_source);
}
return "Unknown";
return rawHTML("Unknown");
}
public function get_lock_editor_html(Image $image): string
public function get_lock_editor_html(Image $image): HTMLElement
{
global $user;
$b_locked = $image->is_locked() ? "Yes (Only admins may edit these details)" : "No";
$h_locked = $image->is_locked() ? " checked" : "";
return "
<tr>
<th>Locked</th>
<td>
".($user->can(Permissions::EDIT_IMAGE_LOCK) ? "
<span class='view'>$b_locked</span>
<input class='edit' type='checkbox' name='tag_edit__locked'$h_locked>
" : "
$b_locked
")."
</td>
</tr>
";
return SHM_POST_INFO(
"Locked",
$user->can(Permissions::EDIT_IMAGE_LOCK),
$image->is_locked() ? "Yes (Only admins may edit these details)" : "No",
INPUT(["type" => "checkbox", "name" => "tag_edit__locked", "checked" => $image->is_locked()])
);
}
}

View file

@ -4,6 +4,10 @@ declare(strict_types=1);
namespace Shimmie2;
use MicroHTML\HTMLElement;
use function MicroHTML\rawHTML;
/* Todo:
* usepref(todo2: port userpref)
* theme junk
@ -53,7 +57,7 @@ class TagEditCloud extends Extension
$sb->add_text_option("tageditcloud_ignoretags");
}
private function build_tag_map(Image $image): ?string
private function build_tag_map(Image $image): ?HTMLElement
{
global $database, $config;
@ -204,7 +208,7 @@ class TagEditCloud extends Extension
$html .= "</div><br>[<span onclick='tageditcloud_toggle_extra(this);' style='color: #0000EF; font-weight:bold;'>show {$rem} more tags</span>]";
}
return "<div id='tageditcloud' class='tageditcloud'>{$html}</div>"; // FIXME: stupidasallhell
return rawHTML("<div id='tageditcloud' class='tageditcloud'>{$html}</div>"); // FIXME: stupidasallhell
}
private function can_tag(Image $image): bool

View file

@ -4,6 +4,8 @@ declare(strict_types=1);
namespace Shimmie2;
use MicroHTML\HTMLElement;
class ImageInfoBoxBuildingEvent extends Event
{
public array $parts = [];
@ -17,7 +19,7 @@ class ImageInfoBoxBuildingEvent extends Event
$this->user = $user;
}
public function add_part(string $html, int $position=50)
public function add_part(HTMLElement $html, int $position=50)
{
while (isset($this->parts[$position])) {
$position++;

View file

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