Skip to content

Commit

Permalink
Autocomplete and search for alias_editor and auto_tagger
Browse files Browse the repository at this point in the history
  • Loading branch information
myname committed Nov 8, 2023
1 parent e498eb8 commit c74b3f6
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 16 deletions.
55 changes: 55 additions & 0 deletions ext/alias_editor/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,26 @@
use MicroCRUD\TextColumn;
use MicroCRUD\Table;

use MicroHTML\HTMLElement;

use function MicroHTML\emptyHTML;
use function MicroHTML\TABLE as html_TABLE;
use function MicroHTML\THEAD;
use function MicroHTML\TBODY;
use function MicroHTML\TFOOT;
use function MicroHTML\TR;
use function MicroHTML\TH;
use function MicroHTML\TD;
use function MicroHTML\INPUT;
use function MicroHTML\FORM;
use function MicroHTML\DIV;
use function MicroHTML\A;
use function MicroHTML\B;
use function MicroHTML\BR;

class AliasTable extends Table
{
public $search_url = null;
public function __construct(\FFSPHP\PDO $db)
{
parent::__construct($db);
Expand All @@ -26,6 +44,33 @@ public function __construct(\FFSPHP\PDO $db)
$this->order_by = ["oldtag"];
$this->table_attrs = ["class" => "zebra"];
}

public function thead(): HTMLElement
{
$thead = THEAD(["id"=>"read"]);

$tr = TR();
foreach ($this->columns as $col) {
if ($col->sortable) {
$sort_name = (@$this->inputs["r__sort"] == $col->name) ? "-{$col->name}" : $col->name;
$sort = "?" . $this->modify_url(["r__sort"=>$sort_name]);
$tr->appendChild(TH(A(["href"=>$sort], $col->title)));
} else {
$tr->appendChild(TH($col->title));
}
}
$thead->appendChild($tr);

if ($this->create_url) {
$tr = TR();
foreach ($this->columns as $col) {
$tr->appendChild(TD($col->read_input($this->inputs)));
}
}
$thead->appendChild(FORM(["method"=>"POST", 'action'=>$this->search_url], $tr));

return $thead;
}
}

class AddAliasEvent extends Event
Expand Down Expand Up @@ -91,7 +136,17 @@ public function onPageRequest(PageRequestEvent $event)
$t->token = $user->get_auth_token();
$t->inputs = $_GET;
$t->size = $config->get_int('alias_items_per_page', 30);
$input = validate_input(["r_oldtag"=>"string,optional", "r_newtag"=>"string,optional"]);
$tag_inputs = array();
if (isset($_GET["r_oldtag"])) {
$tag_inputs["r_oldtag"] = $_GET["r_oldtag"];
}
if (isset($_GET["r_newtag"])) {
$tag_inputs["r_newtag"] = $_GET["r_newtag"];
}
$t->inputs = array_merge($t->inputs, $input, $tag_inputs);
if ($user->can(Permissions::MANAGE_ALIAS_LIST)) {
$t->search_url = make_link("alias/list");
$t->create_url = make_link("alias/add");
$t->delete_url = make_link("alias/remove");
}
Expand Down
8 changes: 6 additions & 2 deletions ext/alias_editor/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function display_aliases(HTMLElement $table, HTMLElement $paginator): voi
global $page, $user;

$html = emptyHTML($table, BR(), $paginator, BR(), SHM_A("alias/export/aliases.csv", "Download as CSV", args: ["download"=>"aliases.csv"]));

$bulk_form = SHM_FORM("alias/import", multipart: true);
$bulk_form->appendChild(
INPUT(["type"=>"file", "name"=>"alias_file"]),
Expand All @@ -30,7 +30,11 @@ public function display_aliases(HTMLElement $table, HTMLElement $paginator): voi
$page->set_title("Alias List");
$page->set_heading("Alias List");
$page->add_block(new NavBlock());
$page->add_block(new Block("Aliases", $html));
$block = new Block("Aliases", $html);
$block->body = str_replace(array("name='c_oldtag'", "name='c_newtag'"),
array("name='c_oldtag' class='autocomplete_tags'", "name='c_newtag' class='autocomplete_tags'"),
$block->body);
$page->add_block($block);

if ($user->can(Permissions::MANAGE_ALIAS_LIST)) {
$page->add_block(new Block("Bulk Upload", $bulk_html, "main", 51));
Expand Down
113 changes: 100 additions & 13 deletions ext/auto_tagger/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,26 @@
use MicroCRUD\TextColumn;
use MicroCRUD\Table;

use MicroHTML\HTMLElement;

use function MicroHTML\emptyHTML;
use function MicroHTML\TABLE as html_TABLE;
use function MicroHTML\THEAD;
use function MicroHTML\TBODY;
use function MicroHTML\TFOOT;
use function MicroHTML\TR;
use function MicroHTML\TH;
use function MicroHTML\TD;
use function MicroHTML\INPUT;
use function MicroHTML\FORM;
use function MicroHTML\DIV;
use function MicroHTML\A;
use function MicroHTML\B;
use function MicroHTML\BR;

class AutoTaggerTable extends Table
{
public $search_url = null;
public function __construct(\FFSPHP\PDO $db)
{
parent::__construct($db);
Expand All @@ -28,6 +46,33 @@ public function __construct(\FFSPHP\PDO $db)
$this->order_by = ["tag"];
$this->table_attrs = ["class" => "zebra"];
}

public function thead(): HTMLElement
{
$thead = THEAD(["id"=>"read"]);

$tr = TR();
foreach ($this->columns as $col) {
if ($col->sortable) {
$sort_name = (@$this->inputs["r__sort"] == $col->name) ? "-{$col->name}" : $col->name;
$sort = "?" . $this->modify_url(["r__sort"=>$sort_name]);
$tr->appendChild(TH(A(["href"=>$sort], $col->title)));
} else {
$tr->appendChild(TH($col->title));
}
}
$thead->appendChild($tr);

if ($this->create_url) {
$tr = TR();
foreach ($this->columns as $col) {
$tr->appendChild(TD($col->read_input($this->inputs)));
}
}
$thead->appendChild(FORM(["method"=>"POST", 'action'=>$this->search_url], $tr));

return $thead;
}
}

class AddAutoTagEvent extends Event
Expand Down Expand Up @@ -96,8 +141,18 @@ public function onPageRequest(PageRequestEvent $event)
$t = new AutoTaggerTable($database->raw_db());
$t->token = $user->get_auth_token();
$t->inputs = $_GET;
$input = validate_input(["r_tag"=>"string,optional", "r_additional_tags"=>"string,optional"]);
$tag_inputs = array();
if (isset($_GET["r_tag"])) {
$tag_inputs["r_tag"] = $_GET["r_tag"];
}
if (isset($_GET["r_additional_tags"])) {
$tag_inputs["r_additional_tags"] = $_GET["r_additional_tags"];
}
$t->inputs = array_merge($t->inputs, $input, $tag_inputs);
$t->size = $config->get_int(AutoTaggerConfig::ITEMS_PER_PAGE, 30);
if ($user->can(Permissions::MANAGE_AUTO_TAG)) {
$t->search_url = make_link("auto_tag/list");
$t->create_url = make_link("auto_tag/add");
$t->delete_url = make_link("auto_tag/remove");
}
Expand Down Expand Up @@ -212,23 +267,23 @@ private function add_auto_tag_csv(string $csv): int
private function add_auto_tag(string $tag, string $additional_tags)
{
global $database;
$existing_tags = $database->get_one("SELECT additional_tags FROM auto_tag WHERE LOWER(tag)=LOWER(:tag)", ["tag"=>$tag]);
if (!is_null($existing_tags)) {
// Auto Tags already exist, so we will append new tags to the existing one
$tag = Tag::sanitize($tag);
$existing_tags = $database->get_one("SELECT additional_tags FROM auto_tag WHERE LOWER(tag)=LOWER(:tag)", ["tag"=>$tag]);
if (!is_null($existing_tags)) {
// Auto Tags already exist, so we will append new tags to the existing one
$tag = Tag::sanitize($tag);
$additional_tags = Tag::explode($additional_tags);
$existing_tags = Tag::explode($existing_tags);
foreach ($additional_tags as $t) {
if (!in_array(strtolower($t), $existing_tags)) {
$existing_tags[] = strtolower($t);
}
}

$database->execute(
$existing_tags = Tag::explode($existing_tags);
foreach ($additional_tags as $t) {
if (!in_array(strtolower($t), $existing_tags)) {
array_push($existing_tags, strtolower($t));
}
}
$database->execute(
"UPDATE auto_tag set additional_tags=:existing_tags where tag=:tag",
["tag"=>$tag, "existing_tags"=>Tag::implode($existing_tags)]
);
log_info(
log_info(
AutoTaggerInfo::KEY,
"Updated auto-tag for {$tag} -> {".implode(" ", $additional_tags)."}"
);
Expand All @@ -245,6 +300,38 @@ private function add_auto_tag(string $tag, string $additional_tags)
AutoTaggerInfo::KEY,
"Added auto-tag for {$tag} -> {".implode(" ", $additional_tags)."}"
);
}
// Now we apply it to existing items
$this->apply_new_auto_tag($tag);
}

private function update_auto_tag(string $tag, string $additional_tags): bool
{
global $database;
$result = $database->get_row("SELECT * FROM auto_tag WHERE LOWER(tag)=LOWER(:tag)", ["tag"=>$tag]);

if ($result===null) {
throw new AutoTaggerException("Auto-tag not set for $tag, can't update");
} else {
$additional_tags = Tag::explode($additional_tags);
$current_additional_tags = Tag::explode($result["additional_tags"]);

if (!Tag::compare($additional_tags, $current_additional_tags)) {
$database->execute(
"UPDATE auto_tag SET additional_tags = :additional_tags WHERE LOWER(tag)=LOWER(:tag)",
["tag"=>$tag, "additional_tags"=>Tag::implode($additional_tags)]
);

log_info(
AutoTaggerInfo::KEY,
"Updated auto-tag for {$tag} -> {".implode(" ", $additional_tags)."}",
"Updated Auto-Tag"
);

// Now we apply it to existing items
$this->apply_new_auto_tag($tag);
return true;
}
}
// Now we apply it to existing items
$this->apply_new_auto_tag($tag);

Check failure on line 337 in ext/auto_tagger/main.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Method Shimmie2\AutoTagger::update_auto_tag() should return bool but return statement is missing.
Expand Down
6 changes: 5 additions & 1 deletion ext/auto_tagger/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public function display_auto_tagtable($table, $paginator): void
$page->set_title("Auto-Tag List");
$page->set_heading("Auto-Tag List");
$page->add_block(new NavBlock());
$page->add_block(new Block("Auto-Tag", $html));
$block = new Block("Auto-Tag", $html);
$block->body = str_replace(array("name='c_tag'", "name='c_additional_tags'"),
array("name='c_tag' class='autocomplete_tags'", "name='c_additional_tags' class='autocomplete_tags'"),
$block->body);
$page->add_block($block);
if ($can_manage) {
$page->add_block(new Block("Bulk Upload", $bulk_html, "main", 51));
}
Expand Down

0 comments on commit c74b3f6

Please sign in to comment.