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

435 lines
15 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
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.
*/
public function pool_info(array $navIDs)
{
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));
2023-07-05 19:04:26 +00:00
if (!empty($poolInfo["nav"])) {
if (!empty($poolInfo["nav"]["prev"])) {
2023-07-05 19:22:42 +00:00
$div->appendChild(SHM_A("post/view/" . $poolInfo["nav"]["prev"], "Prev", class: "pools_prev_img"));
}
2023-07-05 19:04:26 +00:00
if (!empty($poolInfo["nav"]["next"])) {
2023-07-05 19:22:42 +00:00
$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"));
}
}
2023-06-29 17:58:41 +00:00
public function get_adder_html(Image $image, array $pools): HTMLElement
{
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")
);
}
/**
* HERE WE SHOWS THE LIST OF POOLS.
*/
public function list_pools(Page $page, array $pools, int $pageNumber, int $totalPages)
{
// 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(
TD(["class"=>"left"], $pool_link),
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(
["id"=>"poolsList", "class"=>"zebra"],
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');
$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));
$this->display_paginator($page, "pool/list", 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)
{
2023-07-05 00:23:11 +00:00
$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")))
));
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
2020-03-27 00:15:15 +00:00
private function display_top(?Pool $pool, string $heading, bool $check_all = false)
{
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
);
$page->add_block(new NavBlock());
2023-07-05 00:23:11 +00:00
$page->add_block(new Block("Pool Navigation", $poolnav, "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.
*/
2020-03-27 00:15:15 +00:00
public function view_pool(Pool $pool, array $images, int $pageNumber, int $totalPages)
{
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.
*/
2020-03-27 00:15:15 +00:00
public function sidebar_options(Page $page, Pool $pool, bool $check_all)
{
global $user;
2023-07-05 00:23:11 +00:00
// 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]);
};
$_input_id = $_hidden("pool_id", $pool->id);
$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"])
)
);
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(
["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()"])
)
);
}
2009-12-30 07:07:10 +00:00
if ($check_all) {
2023-07-05 00:23:11 +00:00
$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)"])
);
}
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.
*/
2020-03-27 00:15:15 +00:00
public function pool_result(Page $page, array $images, Pool $pool)
{
$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(
["type"=>"text/javascript"],
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(
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
$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])
);
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
*/
2020-03-27 00:15:15 +00:00
public function edit_order(Page $page, Pool $pool, array $images)
{
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");
foreach ($images as $i=>$image) {
$form->appendChild(SPAN(
["class"=>"thumb"],
$this->build_thumb_html($image),
INPUT(["type"=>"number", "name"=>"imgs[$i][]", "value"=>$image->image_order, "style"=>"max-width: 50px;"]),
INPUT(["type"=>"hidden", "name"=>"imgs[$i][]", "value"=>$image->id])
));
}
2023-07-05 19:04:26 +00:00
$form->appendChild(
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
$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
*/
2020-03-27 00:15:15 +00:00
public function edit_pool(Page $page, Pool $pool, array $images)
{
2023-07-05 19:04:26 +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",
TEXTAREA(["name"=>"description"], $pool->description),
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(
["class"=>"thumb"],
$this->build_thumb_html($image),
INPUT(["type"=>"checkbox", "name"=>"check[]", "value"=>$image->id])
));
}
2009-12-24 07:36:09 +00:00
2023-07-05 19:04:26 +00:00
$images_form->appendChild(
BR(),
$_input_id,
SHM_SUBMIT("Remove Selected", ["name"=>"edit", "id"=>"edit_pool_remove_sel"])
);
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.
*/
public function show_history(array $histories, int $pageNumber, int $totalPages)
{
global $page;
2009-12-26 00:31:02 +00:00
2023-07-05 19:04:26 +00:00
$table = TABLE(
["id"=>"poolsList", "class"=>"zebra"],
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(
TD(["class"=>"left"], $pool_link),
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);
}
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);
}
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(
[
"type"=>"text",
"name"=>"bulk_pool_new",
"placeholder"=>"New Pool",
"required"=>"",
"value"=>implode(" ", $search_terms)
]
);
}
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
}