This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/pools/theme.php

456 lines
16 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
2012-02-08 04:15:23 +00:00
namespace Shimmie2;
2023-06-29 05:50:32 +00:00
use MicroHTML\HTMLElement;
2023-07-05 00:23:11 +00:00
use function MicroHTML\emptyHTML;
use function MicroHTML\rawHTML;
2023-07-05 19:04:26 +00:00
use function MicroHTML\{A,BR,DIV,INPUT,P,SCRIPT,SPAN,TABLE,TBODY,TD,TEXTAREA,TH,THEAD,TR};
2023-06-29 05:50:32 +00:00
/**
* @phpstan-type PoolHistory array{id:int,pool_id:int,title:string,user_name:string,action:string,images:string,count:int,date:string}
*/
class PoolsTheme extends Themelet
{
/**
* Adds a block to the panel with information on the pool(s) the image is in.
* $navIDs = Multidimensional array containing pool id, info & nav IDs.
*
2024-01-20 20:48:47 +00:00
* @param array<int,array{info:Pool,nav:array{prev:?int,next:?int}|null}> $navIDs
*/
public function pool_info(array $navIDs): void
{
global $page;
2023-07-05 19:04:26 +00:00
//TODO: Use a 3 column table?
$linksPools = emptyHTML();
2020-03-27 00:15:15 +00:00
foreach ($navIDs as $poolID => $poolInfo) {
2023-07-05 19:22:42 +00:00
$div = DIV(SHM_A("pool/view/" . $poolID, $poolInfo["info"]->title));
2024-01-20 20:48:47 +00:00
if(!empty($poolInfo["nav"])) {
if (!empty($poolInfo["nav"]["prev"])) {
$div->appendChild(SHM_A("post/view/" . $poolInfo["nav"]["prev"], "Prev", class: "pools_prev_img"));
}
if (!empty($poolInfo["nav"]["next"])) {
$div->appendChild(SHM_A("post/view/" . $poolInfo["nav"]["next"], "Next", class: "pools_next_img"));
}
}
2023-07-05 19:04:26 +00:00
$linksPools->appendChild($div);
}
2023-07-05 19:04:26 +00:00
if (!empty($navIDs)) {
$page->add_block(new Block("Pools", $linksPools, "left"));
}
}
/**
* HERE WE SHOWS THE LIST OF POOLS.
*
* @param Pool[] $pools
*/
public function list_pools(Page $page, array $pools, string $search, int $pageNumber, int $totalPages): void
{
// Build up the list of pools.
2023-07-05 00:23:11 +00:00
$pool_rows = [];
foreach ($pools as $pool) {
2023-07-05 19:22:42 +00:00
$pool_link = SHM_A("pool/view/" . $pool->id, $pool->title);
$user_link = SHM_A("user/" . url_escape($pool->user_name), $pool->user_name);
2023-07-05 00:23:11 +00:00
$pool_rows[] = TR(
2023-11-11 21:49:12 +00:00
TD(["class" => "left"], $pool_link),
2023-07-05 00:23:11 +00:00
TD($user_link),
TD($pool->posts),
TD($pool->public ? "Yes" : "No")
);
}
2009-12-26 00:31:02 +00:00
2023-07-05 00:23:11 +00:00
$table = TABLE(
2023-11-11 21:49:12 +00:00
["id" => "poolsList", "class" => "zebra"],
2023-07-05 00:23:11 +00:00
THEAD(TR(TH("Name"), TH("Creator"), TH("Posts"), TH("Public"))),
TBODY(...$pool_rows)
);
2009-12-24 07:36:09 +00:00
$order_arr = ['created' => 'Recently created', 'updated' => 'Last updated', 'name' => 'Name', 'count' => 'Post Count'];
2023-07-05 00:23:11 +00:00
$order_selected = $page->get_cookie('ui-order-pool');
2023-11-11 21:49:12 +00:00
$order_sel = SHM_SELECT("order_pool", $order_arr, selected_options: [$order_selected], attrs: ["id" => "order_pool"]);
$this->display_top(null, "Pools");
2023-07-05 00:23:11 +00:00
$page->add_block(new Block("Order By", $order_sel, "left", 15));
2023-07-05 00:23:11 +00:00
$page->add_block(new Block("Pools", $table, position: 10));
2023-11-07 21:01:03 +00:00
if ($search != "" and !str_starts_with($search, '/')) {
$search = '/'.$search;
}
2023-11-06 05:50:33 +00:00
$this->display_paginator($page, "pool/list".$search, null, $pageNumber, $totalPages);
}
2009-12-24 07:36:09 +00:00
/*
* HERE WE DISPLAY THE NEW POOL COMPOSER
*/
public function new_pool_composer(Page $page): void
{
2023-07-05 00:23:11 +00:00
$form = SHM_SIMPLE_FORM("pool/create", TABLE(
2023-11-11 21:49:12 +00:00
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")))
2023-07-05 00:23:11 +00:00
));
2009-12-24 07:36:09 +00:00
$this->display_top(null, "Create Pool");
2023-07-05 00:23:11 +00:00
$page->add_block(new Block("Create Pool", $form, position: 20));
}
2009-12-26 00:31:02 +00:00
private function display_top(?Pool $pool, string $heading, bool $check_all = false): void
{
global $page, $user;
2009-12-26 00:31:02 +00:00
$page->set_title($heading);
$page->set_heading($heading);
2023-07-05 00:23:11 +00:00
$poolnav = emptyHTML(
2023-07-05 19:22:42 +00:00
SHM_A("pool/list", "Pool Index"),
2023-07-05 00:23:11 +00:00
BR(),
2023-07-05 19:22:42 +00:00
SHM_A("pool/new", "Create Pool"),
2023-07-05 00:23:11 +00:00
BR(),
2023-07-05 19:22:42 +00:00
SHM_A("pool/updated", "Pool Changes")
2023-07-05 00:23:11 +00:00
);
2023-11-07 21:01:03 +00:00
$search = "<form action='".make_link('pool/list')."' method='POST'>
2023-11-06 05:50:33 +00:00
<input name='search' type='text' style='width:75%'>
<input type='submit' value='Go' style='width:20%'>
</form>";
$page->add_block(new NavBlock());
2023-07-05 00:23:11 +00:00
$page->add_block(new Block("Pool Navigation", $poolnav, "left", 10));
2023-11-07 21:01:03 +00:00
$page->add_block(new Block("Search", $search, "left", 10));
2020-03-27 00:15:15 +00:00
if (!is_null($pool)) {
if ($pool->public || $user->can(Permissions::POOLS_ADMIN)) {// IF THE POOL IS PUBLIC OR IS ADMIN SHOW EDIT PANEL
if (!$user->is_anonymous()) {// IF THE USER IS REGISTERED AND LOGGED IN SHOW EDIT PANEL
$this->sidebar_options($page, $pool, $check_all);
}
}
2023-02-04 20:50:26 +00:00
$tfe = send_event(new TextFormattingEvent($pool->description));
2020-03-27 00:15:15 +00:00
$page->add_block(new Block(html_escape($pool->title), $tfe->formatted, "main", 10));
}
}
/**
* HERE WE DISPLAY THE POOL WITH TITLE DESCRIPTION AND IMAGES WITH PAGINATION.
*
* @param Image[] $images
*/
public function view_pool(Pool $pool, array $images, int $pageNumber, int $totalPages): void
{
global $page;
2020-03-27 00:15:15 +00:00
$this->display_top($pool, "Pool: " . html_escape($pool->title));
2023-07-05 00:23:11 +00:00
$pool_images = emptyHTML();
foreach ($images as $image) {
2023-07-05 00:23:11 +00:00
$pool_images->appendChild($this->build_thumb_html($image));
}
$page->add_block(new Block("Viewing Posts", $pool_images, "main", 30));
2020-03-27 00:15:15 +00:00
$this->display_paginator($page, "pool/view/" . $pool->id, null, $pageNumber, $totalPages);
}
/**
* HERE WE DISPLAY THE POOL OPTIONS ON SIDEBAR BUT WE HIDE REMOVE OPTION IF THE USER IS NOT THE OWNER OR ADMIN.
*/
public function sidebar_options(Page $page, Pool $pool, bool $check_all): void
{
global $user;
2023-07-05 00:23:11 +00:00
// This could become a SHM_INPUT function that also accepts 'type' and other attributes.
2023-11-11 21:49:12 +00:00
$_hidden = function (string $name, $value) {
return INPUT(["type" => "hidden", "name" => $name, "value" => $value]);
2023-07-05 00:23:11 +00:00
};
$_input_id = $_hidden("pool_id", $pool->id);
$editor = emptyHTML(
SHM_SIMPLE_FORM(
"pool/import",
2023-11-11 21:49:12 +00:00
INPUT(["type" => "text", "name" => "pool_tag", "id" => "edit_pool_tag", "placeholder" => "Please enter a tag"]),
2023-07-05 00:23:11 +00:00
$_input_id,
2023-11-11 21:49:12 +00:00
SHM_SUBMIT("Import", ["name" => "edit", "id" => "edit_pool_import_btn"])
2023-07-05 00:23:11 +00:00
),
SHM_SIMPLE_FORM(
"pool/edit",
$_hidden("edit_pool", "yes"),
$_input_id,
2023-11-11 21:49:12 +00:00
SHM_SUBMIT("Edit Pool", ["name" => "edit", "id" => "edit_pool_btn"]),
2023-07-05 00:23:11 +00:00
),
SHM_SIMPLE_FORM(
"pool/order",
$_hidden("order_view", "yes"),
$_input_id,
2023-11-11 21:49:12 +00:00
SHM_SUBMIT("Order Pool", ["name" => "edit", "id" => "edit_pool_order_btn"])
2023-07-05 00:23:11 +00:00
),
SHM_SIMPLE_FORM(
"pool/reverse",
$_hidden("reverse_view", "yes"),
$_input_id,
2023-11-11 21:49:12 +00:00
SHM_SUBMIT("Reverse Order", ["name" => "edit", "id" => "reverse_pool_order_btn"])
2023-07-05 00:23:11 +00:00
),
SHM_SIMPLE_FORM(
2023-11-06 12:47:59 +00:00
"post/list/pool_id=" . $pool->id . "/1",
2023-11-11 21:49:12 +00:00
SHM_SUBMIT("Post/List View", ["name" => "edit", "id" => $pool->id])
2023-07-05 00:23:11 +00:00
)
);
2009-12-26 00:31:02 +00:00
2020-03-27 00:15:15 +00:00
if ($user->id == $pool->user_id || $user->can(Permissions::POOLS_ADMIN)) {
2023-07-05 00:23:11 +00:00
$editor->appendChild(
SCRIPT(
2023-11-11 21:49:12 +00:00
["type" => "text/javascript"],
2023-07-05 00:23:11 +00:00
rawHTML("<!--
function confirm_action() {
return confirm('Are you sure that you want to delete this pool?');
}
//-->")
),
SHM_SIMPLE_FORM(
"pool/nuke",
$_input_id,
2023-11-11 21:49:12 +00:00
SHM_SUBMIT("Delete Pool", ["name" => "delete", "id" => "delete_pool_btn", "onclick" => "return confirm_action()"])
2023-07-05 00:23:11 +00:00
)
);
}
2009-12-30 07:07:10 +00:00
if ($check_all) {
2023-07-05 00:23:11 +00:00
$editor->appendChild(
SCRIPT(
2023-11-11 21:49:12 +00:00
["type" => "text/javascript"],
2023-07-05 00:23:11 +00:00
rawHTML("<!--
function setAll(value) {
$('[name=\"check[]\"]').attr('checked', value);
}
//-->")
),
2023-11-11 21:49:12 +00:00
INPUT(["type" => "button", "name" => "CheckAll", "value" => "Check All", "onclick" => "setAll(true)"]),
INPUT(["type" => "button", "name" => "UnCheckAll", "value" => "Uncheck All", "onclick" => "setAll(false)"])
2023-07-05 00:23:11 +00:00
);
}
2023-07-05 00:23:11 +00:00
$page->add_block(new Block("Manage Pool", $editor, "left", 15));
}
/**
* HERE WE DISPLAY THE RESULT OF THE SEARCH ON IMPORT.
*
* @param Image[] $images
*/
public function pool_result(Page $page, array $images, Pool $pool): void
{
$this->display_top($pool, "Importing Posts", true);
2009-12-26 00:31:02 +00:00
2023-07-05 00:23:11 +00:00
$import = emptyHTML(
SCRIPT(
2023-11-11 21:49:12 +00:00
["type" => "text/javascript"],
2023-07-05 00:23:11 +00:00
rawHTML("
function confirm_action() {
return confirm('Are you sure you want to add selected posts to this pool?');
}")
)
);
2023-07-05 00:23:11 +00:00
$form = SHM_FORM("pool/add_posts", name: "checks");
foreach ($images as $image) {
2023-07-05 00:23:11 +00:00
$form->appendChild(
2023-11-11 21:49:12 +00:00
SPAN(["class" => "thumb"], $this->build_thumb_html($image), BR(), INPUT(["type" => "checkbox", "name" => "check[]", "value" => $image->id])),
2023-07-05 00:23:11 +00:00
);
}
2023-07-05 00:23:11 +00:00
$form->appendChild(
BR(),
2023-11-11 21:49:12 +00:00
SHM_SUBMIT("Add Selected", ["name" => "edit", "id" => "edit_pool_add_btn", "onclick" => "return confirm_action()"]),
INPUT(["type" => "hidden", "name" => "pool_id", "value" => $pool->id])
2023-07-05 00:23:11 +00:00
);
2023-07-05 00:23:11 +00:00
$import->appendChild($form);
$page->add_block(new Block("Import", $import, "main", 30));
}
/**
* HERE WE DISPLAY THE POOL ORDERER.
* WE LIST ALL IMAGES ON POOL WITHOUT PAGINATION AND WITH A TEXT INPUT TO SET A NUMBER AND CHANGE THE ORDER
*
* @param Image[] $images
*/
public function edit_order(Page $page, Pool $pool, array $images): void
{
2020-03-27 00:15:15 +00:00
$this->display_top($pool, "Sorting Pool");
2023-07-05 19:04:26 +00:00
$form = SHM_FORM("pool/order", name: "checks");
2023-11-11 21:49:12 +00:00
foreach ($images as $i => $image) {
2023-07-05 19:04:26 +00:00
$form->appendChild(SPAN(
2023-11-11 21:49:12 +00:00
["class" => "thumb"],
2023-07-05 19:04:26 +00:00
$this->build_thumb_html($image),
2024-02-10 00:46:35 +00:00
INPUT(["type" => "number", "name" => "order_{$image->id}", "value" => $image['image_order'], "style" => "max-width: 50px;"]),
2023-07-05 19:04:26 +00:00
));
}
2023-07-05 19:04:26 +00:00
$form->appendChild(
2023-11-11 21:49:12 +00:00
INPUT(["type" => "hidden", "name" => "pool_id", "value" => $pool->id]),
SHM_SUBMIT("Order", ["name" => "edit", "id" => "edit_pool_order"])
2023-07-05 19:04:26 +00:00
);
2023-07-05 19:04:26 +00:00
$page->add_block(new Block("Sorting Posts", $form, position: 30));
}
/**
* HERE WE DISPLAY THE POOL EDITOR.
*
* WE LIST ALL IMAGES ON POOL WITHOUT PAGINATION AND WITH
* A CHECKBOX TO SELECT WHICH IMAGE WE WANT TO REMOVE
*
* @param Image[] $images
*/
public function edit_pool(Page $page, Pool $pool, array $images): void
{
2023-11-11 21:49:12 +00:00
$_input_id = INPUT(["type" => "hidden", "name" => "pool_id", "value" => $pool->id]);
2009-12-26 00:31:02 +00:00
2023-07-05 19:04:26 +00:00
$desc_form = SHM_SIMPLE_FORM(
"pool/edit/description",
2023-11-11 21:49:12 +00:00
TEXTAREA(["name" => "description"], $pool->description),
2023-07-05 19:04:26 +00:00
BR(),
$_input_id,
SHM_SUBMIT("Change Description")
);
2009-12-24 07:36:09 +00:00
2023-07-05 19:04:26 +00:00
$images_form = SHM_FORM("pool/remove_posts", name: "checks");
foreach ($images as $image) {
$images_form->appendChild(SPAN(
2023-11-11 21:49:12 +00:00
["class" => "thumb"],
2023-07-05 19:04:26 +00:00
$this->build_thumb_html($image),
2023-11-11 21:49:12 +00:00
INPUT(["type" => "checkbox", "name" => "check[]", "value" => $image->id])
2023-07-05 19:04:26 +00:00
));
}
2009-12-24 07:36:09 +00:00
2023-07-05 19:04:26 +00:00
$images_form->appendChild(
BR(),
$_input_id,
2023-11-11 21:49:12 +00:00
SHM_SUBMIT("Remove Selected", ["name" => "edit", "id" => "edit_pool_remove_sel"])
2023-07-05 19:04:26 +00:00
);
2009-12-24 07:36:09 +00:00
2020-03-27 00:15:15 +00:00
$pool->description = ""; //This is a rough fix to avoid showing the description twice.
$this->display_top($pool, "Editing Pool", true);
2023-07-05 19:04:26 +00:00
$page->add_block(new Block("Editing Description", $desc_form, position: 28));
$page->add_block(new Block("Editing Posts", $images_form, position: 30));
}
2009-12-26 00:31:02 +00:00
/**
* HERE WE DISPLAY THE HISTORY LIST.
*
* @param PoolHistory[] $histories
*/
public function show_history(array $histories, int $pageNumber, int $totalPages): void
{
global $page;
2009-12-26 00:31:02 +00:00
2023-07-05 19:04:26 +00:00
$table = TABLE(
2023-11-11 21:49:12 +00:00
["id" => "poolsList", "class" => "zebra"],
2023-07-05 19:04:26 +00:00
THEAD(TR(TH("Pool"), TH("Post Count"), TH("Changes"), TH("Updater"), TH("Date"), TH("Action")))
);
$body = [];
foreach ($histories as $history) {
2023-07-05 19:22:42 +00:00
$pool_link = SHM_A("pool/view/" . $history["pool_id"], $history["title"]);
$user_link = SHM_A("user/" . url_escape($history["user_name"]), $history["user_name"]);
$revert_link = SHM_A(("pool/revert/" . $history["id"]), "Revert");
if ($history['action'] == 1) {
$prefix = "+";
} elseif ($history['action'] == 0) {
$prefix = "-";
2019-05-28 19:27:23 +00:00
} else {
throw new \RuntimeException("history['action'] not in {0, 1}");
2019-06-14 12:16:58 +00:00
}
2023-07-05 19:04:26 +00:00
$images = trim($history["images"]);
$images = explode(" ", $images);
2023-07-05 19:04:26 +00:00
$image_links = emptyHTML();
foreach ($images as $image) {
2023-07-05 19:22:42 +00:00
$image_links->appendChild(" ", SHM_A("post/view/" . $image, $prefix . $image));
}
2023-07-05 19:04:26 +00:00
$body[] = TR(
2023-11-11 21:49:12 +00:00
TD(["class" => "left"], $pool_link),
2023-07-05 19:04:26 +00:00
TD($history["count"]),
TD($image_links),
TD($user_link),
TD($history["date"]),
TD($revert_link)
);
}
2023-07-05 19:04:26 +00:00
$table->appendChild(TBODY(...$body));
$this->display_top(null, "Recent Changes");
2023-07-05 19:04:26 +00:00
$page->add_block(new Block("Recent Changes", $table, position: 10));
$this->display_paginator($page, "pool/updated", null, $pageNumber, $totalPages);
}
/**
* @param array<int,string> $options
*/
2023-06-29 05:50:32 +00:00
public function get_bulk_pool_selector(array $options): HTMLElement
{
return SHM_SELECT("bulk_pool_select", $options, required: true, empty_option: true);
}
/**
* @param string[] $search_terms
*/
2023-06-29 05:50:32 +00:00
public function get_bulk_pool_input(array $search_terms): HTMLElement
{
2023-06-29 05:50:32 +00:00
return INPUT(
[
2023-11-11 21:49:12 +00:00
"type" => "text",
"name" => "bulk_pool_new",
"placeholder" => "New Pool",
"required" => "",
"value" => Tag::implode($search_terms)
2023-06-29 05:50:32 +00:00
]
);
}
2023-07-05 00:23:11 +00:00
public function get_help_html(): HTMLElement
{
2023-07-05 00:23:11 +00:00
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."
)
);
}
2009-12-24 07:36:09 +00:00
}