Skip to content

Commit

Permalink
SHM_POST_INFO element for doing info box elements in a standard way
Browse files Browse the repository at this point in the history
  • Loading branch information
shish committed Aug 17, 2023
1 parent b910569 commit ce97503
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 164 deletions.
17 changes: 17 additions & 0 deletions core/microhtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
15 changes: 8 additions & 7 deletions ext/artists/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
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
Expand Down
9 changes: 4 additions & 5 deletions ext/image_view_counter/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Shimmie2;

use function MicroHTML\{TD,TH,TR};

class ImageViewCounter extends Extension
{
/** @var ImageViewCounterTheme */
Expand Down Expand Up @@ -63,15 +65,12 @@ public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event)
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);
}
}

Expand Down
26 changes: 11 additions & 15 deletions ext/post_titles/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,19 @@

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])
);
}
}
2 changes: 1 addition & 1 deletion ext/rating/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event)
{
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)
Expand Down
22 changes: 6 additions & 16 deletions ext/rating/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,12 @@ public function get_selection_rater_html(string $name = "rating", array $ratings

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)
Expand Down
28 changes: 11 additions & 17 deletions ext/relationships/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace Shimmie2;

use MicroHTML\HTMLElement;

use function MicroHTML\{TR, TH, TD, emptyHTML, DIV, INPUT};

class RelationshipsTheme extends Themelet
{
public function relationship_info(Image $image)
Expand All @@ -29,26 +33,16 @@ public function relationship_info(Image $image)
}
}

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


Expand Down
33 changes: 17 additions & 16 deletions ext/rule34/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

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")
Expand Down Expand Up @@ -40,16 +38,19 @@ public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event)
$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)
Expand All @@ -66,7 +67,7 @@ public function onUserPageBuilding(UserPageBuildingEvent $event)
{
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);
}
}
Expand Down Expand Up @@ -128,7 +129,7 @@ public function onPageRequest(PageRequestEvent $event)
]);
$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()));
Expand Down
Loading

0 comments on commit ce97503

Please sign in to comment.