MicroHTML on pools ext

This commit is contained in:
Luana 2023-07-04 21:23:11 -03:00 committed by Shish
parent 8202367eaa
commit 0978ef99e3
3 changed files with 157 additions and 150 deletions

View file

@ -756,7 +756,7 @@ function make_form(string $target, string $method="POST", bool $multipart=false,
return '<form action="'.$target.'" method="'.$method.'" '.$extra.'>'.$extra_inputs;
}
function SHM_FORM(string $target, string $method="POST", bool $multipart=false, string $form_id="", string $onsubmit=""): HTMLElement
function SHM_FORM(string $target, string $method="POST", bool $multipart=false, string $form_id="", string $onsubmit="", string $name=""): HTMLElement
{
global $user;
@ -774,6 +774,9 @@ function SHM_FORM(string $target, string $method="POST", bool $multipart=false,
if ($onsubmit) {
$attrs["onsubmit"] = $onsubmit;
}
if ($name) {
$attrs["name"] = $name;
}
return FORM(
$attrs,
INPUT(["type"=>"hidden", "name"=>"q", "value"=>$target]),
@ -788,9 +791,11 @@ function SHM_SIMPLE_FORM($target, ...$children): HTMLElement
return $form;
}
function SHM_SUBMIT(string $text): HTMLElement
function SHM_SUBMIT(string $text, array $args=[]): HTMLElement
{
return INPUT(["type"=>"submit", "value"=>$text]);
$args["type"] = "submit";
$args["value"] = $text;
return INPUT($args);
}
function SHM_COMMAND_EXAMPLE(string $ex, string $desc): HTMLElement
@ -830,11 +835,10 @@ function SHM_USER_FORM(User $duser, string $target, string $title, $body, $foot)
* @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 $attrs Additional attributes dict for <select>. Example: ["id"=>"some_id", "class"=>"some_class"].
*/
function SHM_SELECT(string $name, array $options, array $selected_options=[], bool $required=false, bool $multiple=false, bool $empty_option=false): HTMLElement
function SHM_SELECT(string $name, array $options, array $selected_options=[], bool $required=false, bool $multiple=false, bool $empty_option=false, array $attrs=[]): HTMLElement
{
$attrs = [];
if ($required) {
$attrs["required"] = "";
}

View file

@ -481,10 +481,7 @@ class Pools extends Extension
public function onHelpPageBuilding(HelpPageBuildingEvent $event)
{
if ($event->key===HelpPages::SEARCH) {
$block = new Block();
$block->header = "Pools";
$block->body = $this->theme->get_help_html();
$event->add_block($block);
$event->add_block(new Block("Pools", $this->theme->get_help_html()));
}
}

View file

@ -6,7 +6,9 @@ namespace Shimmie2;
use MicroHTML\HTMLElement;
use function MicroHTML\INPUT;
use function MicroHTML\emptyHTML;
use function MicroHTML\rawHTML;
use function MicroHTML\{A,BR,INPUT,P,SCRIPT,TABLE,THEAD,TBODY,TR,TH,TD,TEXTAREA,SPAN};
class PoolsTheme extends Themelet
{
@ -57,45 +59,34 @@ class PoolsTheme extends Themelet
*/
public function list_pools(Page $page, array $pools, int $pageNumber, int $totalPages)
{
$html = '
<table id="poolsList" class="zebra">
<thead><tr>
<th>Name</th>
<th>Creator</th>
<th>Posts</th>
<th>Public</th>
</tr></thead><tbody>';
// Build up the list of pools.
$pool_rows = [];
foreach ($pools as $pool) {
$pool_link = '<a href="' . make_link("pool/view/" . $pool->id) . '">' . html_escape($pool->title) . "</a>";
$user_link = '<a href="' . make_link("user/" . url_escape($pool->user_name)) . '">' . html_escape($pool->user_name) . "</a>";
$public = ($pool->public ? "Yes" : "No");
$pool_link = A(["href"=>make_link("pool/view/" . $pool->id)], $pool->title);
$user_link = A(["href"=>make_link("user/" . url_escape($pool->user_name))], $pool->user_name);
$html .= "<tr>" .
"<td class='left'>" . $pool_link . "</td>" .
"<td>" . $user_link . "</td>" .
"<td>" . $pool->posts . "</td>" .
"<td>" . $public . "</td>" .
"</tr>";
$pool_rows[] = TR(
TD(["class"=>"left"], $pool_link),
TD($user_link),
TD($pool->posts),
TD($pool->public ? "Yes" : "No")
);
}
$html .= "</tbody></table>";
$table = TABLE(
["id"=>"poolsList", "class"=>"zebra"],
THEAD(TR(TH("Name"), TH("Creator"), TH("Posts"), TH("Public"))),
TBODY(...$pool_rows)
);
$order_html = '<select id="order_pool">';
$order_selected = $page->get_cookie('ui-order-pool');
$order_arr = ['created' => 'Recently created', 'updated' => 'Last updated', 'name' => 'Name', 'count' => 'Post Count'];
foreach ($order_arr as $value => $text) {
$selected = ($value == $order_selected ? "selected" : "");
$order_html .= "<option value=\"{$value}\" {$selected}>{$text}</option>\n";
}
$order_html .= '</select>';
$order_selected = $page->get_cookie('ui-order-pool');
$order_sel = SHM_SELECT("order_pool", $order_arr, selected_options: [$order_selected], attrs: ["id"=>"order_pool"]);
$this->display_top(null, "Pools");
$page->add_block(new Block("Order By", $order_html, "left", 15));
$page->add_block(new Block("Pools", $html, "main", 10));
$page->add_block(new Block("Order By", $order_sel, "left", 15));
$page->add_block(new Block("Pools", $table, position: 10));
$this->display_paginator($page, "pool/list", null, $pageNumber, $totalPages);
}
@ -105,19 +96,15 @@ class PoolsTheme extends Themelet
*/
public function new_pool_composer(Page $page)
{
$create_html = "
" . make_form(make_link("pool/create")) . "
<table>
<tr><td>Title:</td><td><input type='text' name='title'></td></tr>
<tr><td>Public?</td><td><input name='public' type='checkbox' value='Y' checked='checked'/></td></tr>
<tr><td>Description:</td><td><textarea name='description'></textarea></td></tr>
<tr><td colspan='2'><input type='submit' value='Create' /></td></tr>
</table>
</form>
";
$form = SHM_SIMPLE_FORM("pool/create", TABLE(
TR(TD("Title:"), TD(INPUT(["type"=>"text", "name"=>"title"]))),
TR(TD("Public?:"), TD(INPUT(["type"=>"checkbox", "name"=>"public", "value"=>"Y", "checked"=>"checked"]))),
TR(TD("Description:"), TD(TEXTAREA(["name"=>"description"]))),
TR(TD(["colspan"=>"2"], SHM_SUBMIT("Create")))
));
$this->display_top(null, "Create Pool");
$page->add_block(new Block("Create Pool", $create_html, "main", 20));
$page->add_block(new Block("Create Pool", $form, position: 20));
}
private function display_top(?Pool $pool, string $heading, bool $check_all = false)
@ -127,14 +114,16 @@ class PoolsTheme extends Themelet
$page->set_title($heading);
$page->set_heading($heading);
$poolnav_html = '
<a href="' . make_link("pool/list") . '">Pool Index</a>
<br><a href="' . make_link("pool/new") . '">Create Pool</a>
<br><a href="' . make_link("pool/updated") . '">Pool Changes</a>
';
$poolnav = emptyHTML(
A(["href"=>make_link("pool/list")], "Pool Index"),
BR(),
A(["href"=>make_link("pool/new")], "Create Pool"),
BR(),
A(["href"=>make_link("pool/updated")], "Pool Changes")
);
$page->add_block(new NavBlock());
$page->add_block(new Block("Pool Navigation", $poolnav_html, "left", 10));
$page->add_block(new Block("Pool Navigation", $poolnav, "left", 10));
if (!is_null($pool)) {
if ($pool->public || $user->can(Permissions::POOLS_ADMIN)) {// IF THE POOL IS PUBLIC OR IS ADMIN SHOW EDIT PANEL
@ -156,10 +145,9 @@ class PoolsTheme extends Themelet
$this->display_top($pool, "Pool: " . html_escape($pool->title));
$pool_images = '';
$pool_images = emptyHTML();
foreach ($images as $image) {
$thumb_html = $this->build_thumb_html($image);
$pool_images .= "\n" . $thumb_html . "\n";
$pool_images->appendChild($this->build_thumb_html($image));
}
$page->add_block(new Block("Viewing Posts", $pool_images, "main", 30));
@ -174,63 +162,77 @@ class PoolsTheme extends Themelet
{
global $user;
$editor = "\n" . make_form(make_link('pool/import')) . '
<input type="text" name="pool_tag" id="edit_pool_tag" placeholder="Please enter a tag"/>
<input type="submit" name="edit" id="edit_pool_import_btn" value="Import"/>
<input type="hidden" name="pool_id" value="' . $pool->id . '">
</form>
// This could become a SHM_INPUT function that also accepts 'type' and other attributes.
$_hidden=function (string $name, $value) {
return INPUT(["type"=>"hidden", "name"=>$name, "value"=>$value]);
};
' . make_form(make_link('pool/edit')) . '
<input type="submit" name="edit" id="edit_pool_btn" value="Edit Pool"/>
<input type="hidden" name="edit_pool" value="yes">
<input type="hidden" name="pool_id" value="' . $pool->id . '">
</form>
$_input_id = $_hidden("pool_id", $pool->id);
' . make_form(make_link('pool/order')) . '
<input type="submit" name="edit" id="edit_pool_order_btn" value="Order Pool"/>
<input type="hidden" name="order_view" value="yes">
<input type="hidden" name="pool_id" value="' . $pool->id . '">
</form>
' . make_form(make_link('pool/reverse')) . '
<input type="submit" name="edit" id="reverse_pool_order_btn" value="Reverse Order"/>
<input type="hidden" name="reverse_view" value="yes">
<input type="hidden" name="pool_id" value="' . $pool->id . '">
</form>
' . make_form(make_link('post/list/pool_id%3A' . $pool->id . '/1')) . '
<input type="submit" name="edit" id="postlist_pool_btn" value="Post/List View"/>
</form>
';
$editor = emptyHTML(
SHM_SIMPLE_FORM(
"pool/import",
INPUT(["type"=>"text", "name"=>"pool_tag", "id"=>"edit_pool_tag", "placeholder"=>"Please enter a tag"]),
$_input_id,
SHM_SUBMIT("Import", ["name"=>"edit", "id"=>"edit_pool_import_btn"])
),
SHM_SIMPLE_FORM(
"pool/edit",
$_hidden("edit_pool", "yes"),
$_input_id,
SHM_SUBMIT("Edit Pool", ["name"=>"edit", "id"=>"edit_pool_btn"]),
),
SHM_SIMPLE_FORM(
"pool/order",
$_hidden("order_view", "yes"),
$_input_id,
SHM_SUBMIT("Order Pool", ["name"=>"edit", "id"=>"edit_pool_order_btn"])
),
SHM_SIMPLE_FORM(
"pool/reverse",
$_hidden("reverse_view", "yes"),
$_input_id,
SHM_SUBMIT("Reverse Order", ["name"=>"edit", "id"=>"reverse_pool_order_btn"])
),
SHM_SIMPLE_FORM(
"pool/list/pool_id%3A" . $pool->id . "/1",
SHM_SUBMIT("Post/List View", ["name"=>"edit", "id"=>"postlist_pool_btn"])
)
);
if ($user->id == $pool->user_id || $user->can(Permissions::POOLS_ADMIN)) {
$editor .= "
<script type='text/javascript'>
<!--
function confirm_action() {
return confirm('Are you sure that you want to delete this pool?');
}
//-->
</script>
" . make_form(make_link("pool/nuke")) . "
<input type='submit' name='delete' id='delete_pool_btn' value='Delete Pool' onclick='return confirm_action()' />
<input type='hidden' name='pool_id' value='" . $pool->id . "'>
</form>
";
$editor->appendChild(
SCRIPT(
["type"=>"text/javascript"],
rawHTML("<!--
function confirm_action() {
return confirm('Are you sure that you want to delete this pool?');
}
//-->")
),
SHM_SIMPLE_FORM(
"pool/nuke",
$_input_id,
SHM_SUBMIT("Delete Pool", ["name"=>"delete", "id"=>"delete_pool_btn", "onclick"=>"return confirm_action()"])
)
);
}
if ($check_all) {
$editor .= "
<script type='text/javascript'>
<!--
function setAll(value) {
$('[name=\"check[]\"]').attr('checked', value);
}
//-->
</script>
<br><input type='button' name='CheckAll' value='Check All' onClick='setAll(true)'>
<input type='button' name='UnCheckAll' value='Uncheck All' onClick='setAll(false)'>
";
$editor->appendChild(
SCRIPT(
["type"=>"text/javascript"],
rawHTML("<!--
function setAll(value) {
$('[name=\"check[]\"]').attr('checked', value);
}
//-->")
),
INPUT(["type"=>"button", "name"=>"CheckAll", "value"=>"Check All", "onclick"=>"setAll(true)"]),
INPUT(["type"=>"button", "name"=>"UnCheckAll", "value"=>"Uncheck All", "onclick"=>"setAll(false)"])
);
}
$page->add_block(new Block("Manage Pool", $editor, "left", 15));
}
@ -240,30 +242,33 @@ class PoolsTheme extends Themelet
public function pool_result(Page $page, array $images, Pool $pool)
{
$this->display_top($pool, "Importing Posts", true);
$pool_images = "
<script type='text/javascript'>
function confirm_action() {
return confirm('Are you sure you want to add selected posts to this pool?');
}
</script>
";
$pool_images .= "<form action='" . make_link("pool/add_posts") . "' method='POST' name='checks'>";
$import = emptyHTML(
SCRIPT(
["type"=>"text/javascript"],
rawHTML("
function confirm_action() {
return confirm('Are you sure you want to add selected posts to this pool?');
}")
)
);
$form = SHM_FORM("pool/add_posts", name: "checks");
foreach ($images as $image) {
$thumb_html = $this->build_thumb_html($image);
$pool_images .= '<span class="thumb">' . $thumb_html . '<br>' .
'<input name="check[]" type="checkbox" value="' . $image->id . '" />' .
'</span>';
$form->appendChild(
SPAN(["class"=>"thumb"], $this->build_thumb_html($image), BR(), INPUT(["type"=>"checkbox", "name"=>"check[]", "value"=>$image->id])),
);
}
$pool_images .= "<br>" .
"<input type='submit' name='edit' id='edit_pool_add_btn' value='Add Selected' onclick='return confirm_action()'/>" .
"<input type='hidden' name='pool_id' value='" . $pool->id . "'>" .
"</form>";
$form->appendChild(
BR(),
SHM_SUBMIT("Add Selected", ["name"=>"edit", "id"=>"edit_pool_add_btn", "onclick"=>"return confirm_action()"]),
INPUT(["type"=>"hidden", "name"=>"pool_id", "value"=>$pool->id])
);
$page->add_block(new Block("Import", $pool_images, "main", 30));
$import->appendChild($form);
$page->add_block(new Block("Import", $import, "main", 30));
}
@ -407,29 +412,30 @@ class PoolsTheme extends Themelet
);
}
public function get_help_html(): string
public function get_help_html(): HTMLElement
{
return '<p>Search for posts that are in a pool.</p>
<div class="command_example">
<pre>pool=1</pre>
<p>Returns posts in pool #1.</p>
</div>
<div class="command_example">
<pre>pool=any</pre>
<p>Returns posts in any pool.</p>
</div>
<div class="command_example">
<pre>pool=none</pre>
<p>Returns posts not in any pool.</p>
</div>
<div class="command_example">
<pre>pool_by_name=swimming</pre>
<p>Returns posts in the "swimming" pool.</p>
</div>
<div class="command_example">
<pre>pool_by_name=swimming_pool</pre>
<p>Returns posts in the "swimming pool" pool. Note that the underscore becomes a space</p>
</div>
';
return emptyHTML(
P("Search for posts that are in a pool."),
SHM_COMMAND_EXAMPLE(
"pool=1",
"Returns posts in pool #1."
),
SHM_COMMAND_EXAMPLE(
"pool=any",
"Returns posts in any pool."
),
SHM_COMMAND_EXAMPLE(
"pool=none",
"Returns posts not in any pool."
),
SHM_COMMAND_EXAMPLE(
"pool_by_name=swimming",
"Returns posts in the \"swimming\" pool."
),
SHM_COMMAND_EXAMPLE(
"pool_by_name=swimming_pool",
"Returns posts in the \"swimming pool\" pool. Note that the underscore becomes a space."
)
);
}
}