standard HTML select code

This commit is contained in:
Luana 2023-06-25 20:05:38 -03:00 committed by Shish
parent 2b26908624
commit 8db0086d7a
5 changed files with 62 additions and 42 deletions

View file

@ -191,4 +191,32 @@ class BaseThemelet
' >>'
);
}
/**
* Generates a <select> HTML template and sets up the given options.
*
* @param string $name The name attribute of <select>.
* @param array $options A pair of parameters for <option> tags. First one is value, second one is text. Example: ('optionA', 'Choose Option A').
* @param string $attributes Flags for <select>. Example: 'multiple required' becomes <select multiple required>
* @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, string $attributes, bool $empty_option=false, array $selected_options=[]): string
{
$output = "<select name='" . $name . "' " . $attributes . ">";
if ($empty_option) {
$output .= "<option></option>";
}
foreach ($options as $value => $op) {
if (isset($selected_options) && in_array($value, $selected_options)) {
$output .= "<option value='" . $value . "' selected>" . $op . "</option>";
} else {
$output .= "<option value='" . $value . "' >" . $op . "</option>";
}
}
return $output . "</select>";
}
}

View file

@ -553,12 +553,9 @@ class Pools extends Extension
{
global $database;
$pools = array_map(
[Pool::class, "makePool"],
$database->get_all("SELECT * FROM pools ORDER BY title ")
);
$options = $database->get_pairs("SELECT id,title FROM pools ORDER BY title");
$event->add_action("bulk_pool_add_existing", "Add To (P)ool", "p", "", $this->theme->get_bulk_pool_selector($pools));
$event->add_action("bulk_pool_add_existing", "Add To (P)ool", "p", "", $this->theme->get_bulk_pool_selector($options));
$event->add_action("bulk_pool_add_new", "Create Pool", "", "", $this->theme->get_bulk_pool_input($event->search_terms));
}

View file

@ -391,13 +391,9 @@ class PoolsTheme extends Themelet
$this->display_paginator($page, "pool/updated", null, $pageNumber, $totalPages);
}
public function get_bulk_pool_selector(array $pools): string
public function get_bulk_pool_selector(array $options): string
{
$output = "<select name='bulk_pool_select' required='required'><option></option>";
foreach ($pools as $pool) {
$output .= "<option value='" . $pool->id . "' >" . $pool->title . "</option>";
}
return $output . "</select>";
return $this->build_selector("bulk_pool_select", $options, "required", true);
}
public function get_bulk_pool_input(array $search_terms): string

View file

@ -313,7 +313,7 @@ class Ratings extends Extension
}
}
$this->theme->display_form($original_values, self::get_sorted_ratings());
$this->theme->display_form($original_values);
}
public function onAdminAction(AdminActionEvent $event)
@ -415,6 +415,21 @@ class Ratings extends Extension
return $ratings;
}
public static function get_ratings_dict(array $ratings=null): array
{
if (!isset($ratings)) {
$ratings = self::get_sorted_ratings();
}
return array_combine(
array_map(function ($o) {
return $o->code;
}, $ratings),
array_map(function ($o) {
return $o->name;
}, $ratings)
);
}
public static function get_user_class_privs(User $user): array
{
global $config;

View file

@ -27,42 +27,24 @@ class RatingsTheme extends Themelet
return $html;
}
public function display_form(array $current_ratings, array $available_ratings)
public function display_form(array $current_ratings)
{
global $page;
$html = make_form(make_link("admin/update_ratings"))."<table class='form'><tr>
<th>Change</th><td><select name='rating_old' required='required'><option></option>";
foreach ($current_ratings as $key=>$value) {
$html .= "<option value='$key'>$value</option>";
}
$html .= "</select></td></tr>
<tr><th>To</th><td><select name='rating_new' required='required'><option></option>";
foreach ($available_ratings as $value) {
$html .= "<option value='$value->code'>$value->name</option>";
}
$html .= "</select></td></tr>
<tr><td colspan='2'><input type='submit' value='Update'></td></tr></table>
$html = make_form(make_link("admin/update_ratings"))."<table class='form'>";
$html .= "<tr><th>Change</th><td>".$this->build_selector("rating_old", $current_ratings, "required", true)."</td></tr>";
$html .= "<tr><th>To</th><td>".$this->build_selector("rating_new", Ratings::get_ratings_dict(), "required", true)."</td></tr>";
$html .= "<tr><td colspan='2'><input type='submit' value='Update'></td></tr></table>
</form>\n";
$page->add_block(new Block("Update Ratings", $html));
}
public function get_selection_rater_html(array $selected_options, bool $multiple = false, array $available_options = null): string
public function get_selection_rater_html(array $selected_options, bool $multiple = false): string
{
$output = "<select name='rating".($multiple ? "[]' multiple='multiple'" : "' ")." >";
$options = Ratings::get_sorted_ratings();
foreach ($options as $option) {
if ($available_options!=null && !in_array($option->code, $available_options)) {
continue;
}
$output .= "<option value='".$option->code."' ".
(in_array($option->code, $selected_options) ? "selected='selected'" : "")
.">".$option->name."</option>";
}
return $output."</select>";
return $this->build_selector($multiple ? "rating[]" : "rating", Ratings::get_ratings_dict(), ($multiple ? "multiple" : ""), false, $selected_options);
}
public function get_help_html(array $ratings): string
@ -93,7 +75,9 @@ class RatingsTheme extends Themelet
return $output;
}
public function get_user_options(User $user, array $selected_ratings, array $available_ratings): string
// This wasn't being used at all
/* public function get_user_options(User $user, array $selected_ratings, array $available_ratings): string
{
$html = "
<p>".make_form(make_link("user_admin/default_ratings"))."
@ -115,5 +99,5 @@ class RatingsTheme extends Themelet
</form>
";
return $html;
}
} */
}