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/source_history/theme.php

150 lines
4.7 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
2020-04-25 20:36:28 +00:00
use function MicroHTML\LI;
use function MicroHTML\A;
use function MicroHTML\INPUT;
use function MicroHTML\LABEL;
use function MicroHTML\rawHTML;
2020-04-12 11:42:37 +00:00
/**
* @phpstan-type HistoryEntry array{image_id:int,id:int,source:string,date_set:string,user_id:string,user_ip:string,name:string}
*/
2019-09-29 16:37:03 +00:00
class SourceHistoryTheme extends Themelet
{
/** @var string[] */
private array $messages = [];
/**
* @param HistoryEntry[] $history
*/
public function display_history_page(Page $page, int $image_id, array $history): void
{
2020-04-12 11:42:37 +00:00
$history_html = $this->history_list($history, true);
2013-05-18 12:52:11 +00:00
2020-10-26 15:23:49 +00:00
$page->set_title('Post '.$image_id.' Source History');
$page->set_heading('Source History: '.$image_id);
$page->add_block(new NavBlock());
$page->add_block(new Block("Source History", $history_html, "main", 10));
}
2013-05-18 12:52:11 +00:00
/**
* @param HistoryEntry[] $history
*/
public function display_global_page(Page $page, array $history, int $page_number): void
{
2020-04-12 11:42:37 +00:00
$history_html = $this->history_list($history, false);
2013-05-18 12:52:11 +00:00
$page->set_title("Global Source History");
$page->set_heading("Global Source History");
$page->add_block(new Block("Source History", $history_html, "main", 10));
$h_prev = ($page_number <= 1) ? "Prev" :
2023-11-11 21:49:12 +00:00
'<a href="'.make_link('source_history/all/'.($page_number - 1)).'">Prev</a>';
$h_index = "<a href='".make_link()."'>Index</a>";
2023-11-11 21:49:12 +00:00
$h_next = '<a href="'.make_link('source_history/all/'.($page_number + 1)).'">Next</a>';
$nav = $h_prev.' | '.$h_index.' | '.$h_next;
$page->add_block(new Block("Navigation", $nav, "left"));
}
/**
* Add a section to the admin page.
*/
public function display_admin_block(string $validation_msg = ''): void
{
global $page;
2020-04-12 11:42:37 +00:00
if (!empty($validation_msg)) {
$validation_msg = '<br><b>'. $validation_msg .'</b>';
}
2020-04-12 11:42:37 +00:00
$html = '
2019-06-09 07:32:01 +00:00
Revert source changes by a specific IP address or username, optionally limited to recent changes.
2013-05-18 12:52:11 +00:00
'.$validation_msg.'
<br><br>'.make_form(make_link("source_history/bulk_revert"))."
2013-05-18 12:52:11 +00:00
<table class='form'>
<tr><th>Username</th> <td><input type='text' name='revert_name' size='15'></td></tr>
<tr><th>IP&nbsp;Address</th> <td><input type='text' name='revert_ip' size='15'></td></tr>
2019-06-09 07:32:01 +00:00
<tr><th>Since</th> <td><input type='date' name='revert_date' size='15'></td></tr>
2013-05-18 12:52:11 +00:00
<tr><td colspan='2'><input type='submit' value='Revert'></td></tr>
</table>
</form>
";
$page->add_block(new Block("Mass Source Revert", $html));
}
2020-04-12 11:42:37 +00:00
/*
* Show a standard page for results to be put into
*/
public function display_revert_ip_results(): void
{
global $page;
$html = implode("\n", $this->messages);
$page->add_block(new Block("Bulk Revert Results", $html));
}
public function add_status(string $title, string $body): void
{
$this->messages[] = '<p><b>'. $title .'</b><br>'. $body .'</p>';
}
2020-04-12 11:42:37 +00:00
/**
* @param HistoryEntry[] $history
*/
2020-04-12 11:42:37 +00:00
protected function history_list(array $history, bool $select_2nd): string
{
$history_list = "";
foreach ($history as $n => $fields) {
$history_list .= $this->history_entry($fields, $select_2nd && $n == 1);
}
return "
<div style='text-align: left'>
" . make_form(make_link("source_history/revert")) . "
<ul style='list-style-type:none;'>
$history_list
</ul>
<input type='submit' value='Revert To'>
</form>
</div>
";
}
/**
* @param HistoryEntry $fields
*/
2020-04-12 11:42:37 +00:00
protected function history_entry(array $fields, bool $selected): string
{
global $user;
$image_id = $fields['image_id'];
$current_id = $fields['id'];
2020-04-12 11:45:19 +00:00
$current_source = $fields['source'];
2020-04-12 11:42:37 +00:00
$name = $fields['name'];
$date_set = rawHTML(autodate($fields['date_set']));
$ip = $user->can(Permissions::VIEW_IP) ?
rawHTML(" " . show_ip($fields['user_ip'], "Sourcing >>$image_id as '$current_source'"))
2020-04-12 11:42:37 +00:00
: null;
2023-11-11 21:49:12 +00:00
$setter = A(["href" => make_link("user/" . url_escape($name))], $name);
2020-04-12 11:42:37 +00:00
return (string)LI(
2023-11-11 21:49:12 +00:00
INPUT(["type" => "radio", "name" => "revert", "id" => "$current_id", "value" => "$current_id", "checked" => $selected]),
A(["href" => make_link("post/view/$image_id")], $image_id),
2020-04-12 11:42:37 +00:00
": ",
LABEL(
2023-11-11 21:49:12 +00:00
["for" => "$current_id"],
2020-04-12 11:42:37 +00:00
$current_source,
" - ",
$setter,
$ip,
" - ",
$date_set
)
);
}
2013-05-18 12:52:11 +00:00
}