build_selector and make_form_microhtml as SHM_ functions

This commit is contained in:
Luana 2023-07-04 19:20:43 -03:00 committed by Shish
parent 463aec6df7
commit 8202367eaa
4 changed files with 84 additions and 108 deletions

View file

@ -191,43 +191,4 @@ class BaseThemelet
' >>'
);
}
/**
* Generates a <select> HTML template and sets up the given options.
*
* @param string $name The name attribute of <select>.
* @param array $options An array of pairs of parameters for <option> tags. First one is value, second one is text. Example: ('optionA', 'Choose Option A').
* @param bool $required Wether the <select> element is required.
* @param bool $multiple Wether the <select> element is multiple-choice.
* @param bool $empty_option Whether the first option should be an empty one.
* @param array $selected_options The values of options that should be pre-selected.
*/
protected function build_selector(string $name, array $options, bool $required=false, bool $multiple=false, bool $empty_option=false, array $selected_options=[]): HTMLElement
{
$attrs = [];
if ($required) {
$attrs["required"] = "";
}
if ($multiple) {
$name = $name . "[]";
$attrs["multiple"] = "";
}
$attrs["name"] = $name;
$s = SELECT($attrs);
if ($empty_option) {
$s->appendChild(OPTION());
}
foreach ($options as $value => $op) {
if (in_array($value, $selected_options)) {
$s->appendChild(OPTION(["value"=>$value, "selected"=>""], $op));
} else {
$s->appendChild(OPTION(["value"=>$value], $op));
}
}
return $s;
}
}

View file

@ -11,8 +11,10 @@ use function MicroHTML\rawHTML;
use function MicroHTML\FORM;
use function MicroHTML\INPUT;
use function MicroHTML\DIV;
use function MicroHTML\OPTION;
use function MicroHTML\PRE;
use function MicroHTML\P;
use function MicroHTML\SELECT;
use function MicroHTML\TABLE;
use function MicroHTML\THEAD;
use function MicroHTML\TFOOT;
@ -754,30 +756,6 @@ function make_form(string $target, string $method="POST", bool $multipart=false,
return '<form action="'.$target.'" method="'.$method.'" '.$extra.'>'.$extra_inputs;
}
// Temporary? This should eventually become make_form.
function make_form_microhtml(string $target, string $method="POST", bool $multipart=false, string $form_id="", string $onsubmit=""): HTMLElement
{
global $user;
if ($method == "GET") {
$extra_inputs = INPUT(["type"=>"hidden", "name"=>"q", "value"=>$target]);
$target = make_link($target);
} else {
$extra_inputs = $user->get_auth_microhtml();
}
$args = ["action"=>$target, "method"=>$method];
if ($multipart) {
$args["enctype"] = "multipart/form-data";
}
if ($onsubmit) {
$args["onsubmit"] = $onsubmit;
}
return FORM($args, $extra_inputs);
}
function SHM_FORM(string $target, string $method="POST", bool $multipart=false, string $form_id="", string $onsubmit=""): HTMLElement
{
global $user;
@ -799,7 +777,7 @@ function SHM_FORM(string $target, string $method="POST", bool $multipart=false,
return FORM(
$attrs,
INPUT(["type"=>"hidden", "name"=>"q", "value"=>$target]),
$method == "GET" ? "" : rawHTML($user->get_auth_html())
$method == "GET" ? "" : $user->get_auth_microhtml()
);
}
@ -843,6 +821,53 @@ function SHM_USER_FORM(User $duser, string $target, string $title, $body, $foot)
);
}
/**
* Generates a <select> element and sets up the given options.
*
* @param string $name The name attribute of <select>.
* @param array $options An array of pairs of parameters for <option> tags. First one is value, second one is text. Example: ('optionA', 'Choose Option A').
* @param array $selected_options The values of options that should be pre-selected.
* @param bool $required Wether the <select> element is required.
* @param bool $multiple Wether the <select> element is multiple-choice.
* @param bool $empty_option Whether the first option should be an empty one.
*/
function SHM_SELECT(string $name, array $options, array $selected_options=[], bool $required=false, bool $multiple=false, bool $empty_option=false): HTMLElement
{
$attrs = [];
if ($required) {
$attrs["required"] = "";
}
if ($multiple) {
if (!str_ends_with($name, "[]")) {
$name = $name . "[]";
}
$attrs["multiple"] = "";
}
$attrs["name"] = $name;
$_options = [];
if ($empty_option) {
$_options[] = OPTION();
}
foreach ($options as $value => $text) {
$_options[] = SHM_OPTION((string)$value, (string)$text, in_array($value, $selected_options));
}
return SELECT($attrs, ...$_options);
}
function SHM_OPTION(string $value, string $text, bool $selected=false): HTMLElement
{
if ($selected) {
return OPTION(["value"=>$value, "selected"=>""], $text);
}
return OPTION(["value"=>$value], $text);
}
const BYTE_DENOMINATIONS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
function human_filesize(int $bytes, $decimals = 2): string
{

View file

@ -44,13 +44,12 @@ class PoolsTheme extends Themelet
public function get_adder_html(Image $image, array $pools): HTMLElement
{
$form = make_form_microhtml(make_link("pool/add_post"));
$form->appendChild($this->build_selector("pool_id", $pools));
$form->appendChild(INPUT(["type"=>"hidden", "name"=>"image_id", "value"=>$image->id]));
$form->appendChild(INPUT(["type"=>"submit", "value"=>"Add Post to Pool"]));
return $form;
return SHM_SIMPLE_FORM(
"pool/add_post",
SHM_SELECT("pool_id", $pools),
INPUT(["type"=>"hidden", "name"=>"image_id", "value"=>$image->id]),
SHM_SUBMIT("Add Post to Pool")
);
}
/**
@ -392,7 +391,7 @@ class PoolsTheme extends Themelet
public function get_bulk_pool_selector(array $options): HTMLElement
{
return $this->build_selector("bulk_pool_select", $options, required: true, empty_option: true);
return SHM_SELECT("bulk_pool_select", $options, required: true, empty_option: true);
}
public function get_bulk_pool_input(array $search_terms): HTMLElement

View file

@ -13,7 +13,7 @@ class RatingsTheme extends Themelet
{
public function get_selection_rater_html(string $name = "rating", array $ratings = [], array $selected_options = []): HTMLElement
{
return $this->build_selector($name, !empty($ratings) ? $ratings : Ratings::get_ratings_dict(), required: true, selected_options: $selected_options);
return SHM_SELECT($name, !empty($ratings) ? $ratings : Ratings::get_ratings_dict(), required: true, selected_options: $selected_options);
}
public function get_rater_html(int $image_id, string $rating, bool $can_rate): HTMLElement
@ -40,51 +40,42 @@ class RatingsTheme extends Themelet
{
global $page;
$html = make_form_microhtml(make_link("admin/update_ratings"));
$html->appendChild(TABLE(
$table = TABLE(
["class"=>"form"],
TR(TH("Change"), TD($this->get_selection_rater_html("rating_old", $current_ratings))),
TR(TH("To"), TD($this->get_selection_rater_html("rating_new"))),
TR(TD(["colspan"=>"2"], INPUT(["type"=>"submit", "value"=>"Update"])))
));
TR(TD(["colspan"=>"2"], SHM_SUBMIT("Update")))
);
$page->add_block(new Block("Update Ratings", $html));
$page->add_block(new Block("Update Ratings", SHM_SIMPLE_FORM("admin/update_ratings", $table)));
}
public function get_help_html(array $ratings): HTMLElement
{
$output = emptyHTML(
P("Search for posts with one or more possible ratings."),
DIV(
["class"=>"command_example"],
PRE("rating:" . $ratings[0]->search_term),
P("Returns posts with the " . $ratings[0]->name . " rating.")
),
P("Ratings can be abbreviated to a single letter as well."),
DIV(
["class"=>"command_example"],
PRE("rating:" . $ratings[0]->code),
P("Returns posts with the " . $ratings[0]->name . " rating.")
),
P("If abbreviations are used, multiple ratings can be searched for."),
DIV(
["class"=>"command_example"],
PRE("rating:" . $ratings[0]->code . $ratings[1]->code),
P("Returns posts with the " . $ratings[0]->name . " or " . $ratings[1]->name . " rating.")
),
P("Available ratings:")
);
$table = TABLE(TR(TH("Name"), TH("Search Term"), TH("Abbreviation")));
$rating_rows = [TR(TH("Name"), TH("Search Term"), TH("Abbreviation"))];
foreach ($ratings as $rating) {
$table->appendChild(TR(TD($rating->name), TD($rating->search_term), TD($rating->code)));
$rating_rows[] = TR(TD($rating->name), TD($rating->search_term), TD($rating->code));
}
$output->appendChild($table);
return $output;
return emptyHTML(
P("Search for posts with one or more possible ratings."),
SHM_COMMAND_EXAMPLE(
"rating:" . $ratings[0]->search_term,
"Returns posts with the " . $ratings[0]->name . " rating."
),
P("Ratings can be abbreviated to a single letter as well."),
SHM_COMMAND_EXAMPLE(
"rating:" . $ratings[0]->code,
"Returns posts with the " . $ratings[0]->name . " rating."
),
P("If abbreviations are used, multiple ratings can be searched for."),
SHM_COMMAND_EXAMPLE(
"rating:" . $ratings[0]->code . $ratings[1]->code,
"Returns posts with the " . $ratings[0]->name . " or " . $ratings[1]->name . " rating."
),
P("Available ratings:"),
TABLE(...$rating_rows)
);
}
// This wasn't being used at all
@ -101,7 +92,7 @@ class RatingsTheme extends Themelet
<tbody>
<tr><td>This controls the default rating search results will be filtered by, and nothing else. To override in your search results, add rating:* to your search.</td></tr>
<tr><td>
".$this->build_selector("ratings", selected_options: $selected_ratings, multiple: true, options: $available_ratings)."
".SHM_SELECT("ratings", selected_options: $selected_ratings, multiple: true, options: $available_ratings)."
</td></tr>
</tbody>
<tfoot>