2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2014-02-03 13:54:09 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2019-07-05 15:15:38 +00:00
|
|
|
class ImageRelationshipSetEvent extends Event
|
|
|
|
{
|
2021-03-14 23:43:50 +00:00
|
|
|
public int $child_id;
|
|
|
|
public int $parent_id;
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2019-09-29 13:30:55 +00:00
|
|
|
public function __construct(int $child_id, int $parent_id)
|
2019-07-05 15:15:38 +00:00
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
parent::__construct();
|
2019-07-05 15:15:38 +00:00
|
|
|
$this->child_id = $child_id;
|
|
|
|
$this->parent_id = $parent_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class Relationships extends Extension
|
|
|
|
{
|
2020-02-04 00:46:36 +00:00
|
|
|
/** @var RelationshipsTheme */
|
2023-06-27 14:56:49 +00:00
|
|
|
protected Themelet $theme;
|
2020-02-04 00:46:36 +00:00
|
|
|
|
2019-07-05 15:15:38 +00:00
|
|
|
public const NAME = "Relationships";
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onInitExt(InitExtEvent $event): void
|
2020-01-29 20:22:50 +00:00
|
|
|
{
|
2024-01-15 17:32:56 +00:00
|
|
|
Image::$prop_types["parent_id"] = ImagePropType::INT;
|
|
|
|
Image::$prop_types["has_children"] = ImagePropType::BOOL;
|
2020-01-29 20:22:50 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
global $database;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2019-11-03 19:49:52 +00:00
|
|
|
if ($this->get_version("ext_relationships_version") < 1) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->execute("ALTER TABLE images ADD parent_id INT");
|
2020-10-26 22:37:25 +00:00
|
|
|
$database->execute("ALTER TABLE images ADD has_children BOOLEAN DEFAULT FALSE NOT NULL");
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->execute("CREATE INDEX images__parent_id ON images(parent_id)");
|
2020-10-26 19:17:11 +00:00
|
|
|
$database->execute("CREATE INDEX images__has_children ON images(has_children)");
|
|
|
|
$this->set_version("ext_relationships_version", 3);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-11-03 19:49:52 +00:00
|
|
|
if ($this->get_version("ext_relationships_version") < 2) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->execute("CREATE INDEX images__has_children ON images(has_children)");
|
2019-11-03 19:49:52 +00:00
|
|
|
$this->set_version("ext_relationships_version", 2);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2020-10-26 18:55:03 +00:00
|
|
|
if ($this->get_version("ext_relationships_version") < 3) {
|
|
|
|
$database->standardise_boolean("images", "has_children");
|
|
|
|
$this->set_version("ext_relationships_version", 3);
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onImageInfoSet(ImageInfoSetEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-02-08 00:24:13 +00:00
|
|
|
global $user;
|
|
|
|
if ($user->can(Permissions::EDIT_IMAGE_RELATIONSHIPS)) {
|
|
|
|
if (isset($_POST['tag_edit__tags']) ? !preg_match('/parent[=|:]/', $_POST["tag_edit__tags"]) : true) { //Ignore tag_edit__parent if tags contain parent metatag
|
|
|
|
if (isset($_POST["tag_edit__parent"]) ? ctype_digit($_POST["tag_edit__parent"]) : false) {
|
|
|
|
send_event(new ImageRelationshipSetEvent($event->image->id, (int) $_POST["tag_edit__parent"]));
|
|
|
|
} else {
|
|
|
|
$this->remove_parent($event->image->id);
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onDisplayingImage(DisplayingImageEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$this->theme->relationship_info($event->image);
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onSearchTermParse(SearchTermParseEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-01-26 16:38:26 +00:00
|
|
|
if (is_null($event->term)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-01-26 13:19:35 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$matches = [];
|
|
|
|
if (preg_match("/^parent[=|:]([0-9]+|any|none)$/", $event->term, $matches)) {
|
|
|
|
$parentID = $matches[1];
|
|
|
|
|
|
|
|
if (preg_match("/^(any|none)$/", $parentID)) {
|
|
|
|
$not = ($parentID == "any" ? "NOT" : "");
|
|
|
|
$event->add_querylet(new Querylet("images.parent_id IS $not NULL"));
|
|
|
|
} else {
|
2023-11-11 21:49:12 +00:00
|
|
|
$event->add_querylet(new Querylet("images.parent_id = :pid", ["pid" => $parentID]));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
} elseif (preg_match("/^child[=|:](any|none)$/", $event->term, $matches)) {
|
|
|
|
$not = ($matches[1] == "any" ? "=" : "!=");
|
2023-11-11 21:49:12 +00:00
|
|
|
$event->add_querylet(new Querylet("images.has_children $not :true", ["true" => true]));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onHelpPageBuilding(HelpPageBuildingEvent $event): void
|
2019-08-02 20:05:49 +00:00
|
|
|
{
|
2023-11-11 21:49:12 +00:00
|
|
|
if ($event->key === HelpPages::SEARCH) {
|
2019-08-02 20:05:49 +00:00
|
|
|
$block = new Block();
|
|
|
|
$block->header = "Relationships";
|
|
|
|
$block->body = $this->theme->get_help_html();
|
|
|
|
$event->add_block($block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onTagTermCheck(TagTermCheckEvent $event): void
|
2020-01-29 20:22:50 +00:00
|
|
|
{
|
|
|
|
if (preg_match("/^(parent|child)[=|:](.*)$/i", $event->term)) {
|
|
|
|
$event->metatag = true;
|
|
|
|
}
|
|
|
|
}
|
2019-08-02 20:05:49 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onTagTermParse(TagTermParseEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$matches = [];
|
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
if (preg_match("/^parent[=|:]([0-9]+|none)$/", $event->term, $matches)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$parentID = $matches[1];
|
|
|
|
if ($parentID == "none" || $parentID == "0") {
|
2020-01-29 20:22:50 +00:00
|
|
|
$this->remove_parent($event->image_id);
|
2019-05-28 16:59:38 +00:00
|
|
|
} else {
|
2020-01-29 20:22:50 +00:00
|
|
|
send_event(new ImageRelationshipSetEvent($event->image_id, (int)$parentID));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2020-01-29 20:22:50 +00:00
|
|
|
} elseif (preg_match("/^child[=|:]([0-9]+)$/", $event->term, $matches)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$childID = $matches[1];
|
2020-01-29 20:22:50 +00:00
|
|
|
send_event(new ImageRelationshipSetEvent((int)$childID, $event->image_id));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$event->add_part($this->theme->get_parent_editor_html($event->image), 45);
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onImageDeletion(ImageDeletionEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $database;
|
|
|
|
|
2024-01-15 17:12:36 +00:00
|
|
|
if (bool_escape($event->image['has_children'])) {
|
2023-11-11 21:49:12 +00:00
|
|
|
$database->execute("UPDATE images SET parent_id = NULL WHERE parent_id = :iid", ["iid" => $event->image->id]);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 17:12:36 +00:00
|
|
|
if ($event->image['parent_id'] !== null) {
|
|
|
|
$this->set_has_children($event->image['parent_id']);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onImageRelationshipSet(ImageRelationshipSetEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $database;
|
|
|
|
|
2023-11-11 21:49:12 +00:00
|
|
|
$old_parent = $database->get_one("SELECT parent_id FROM images WHERE id = :cid", ["cid" => $event->child_id]);
|
2020-01-29 20:22:50 +00:00
|
|
|
if (!is_null($old_parent)) {
|
|
|
|
$old_parent = (int)$old_parent;
|
|
|
|
}
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
if ($old_parent == $event->parent_id) {
|
|
|
|
return; // no change
|
|
|
|
}
|
|
|
|
if (!Image::by_id($event->parent_id) || !Image::by_id($event->child_id)) {
|
|
|
|
return; // one of the images doesn't exist
|
|
|
|
}
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-01-29 20:22:50 +00:00
|
|
|
$database->execute("UPDATE images SET parent_id = :pid WHERE id = :cid", ["pid" => $event->parent_id, "cid" => $event->child_id]);
|
2023-11-11 21:49:12 +00:00
|
|
|
$database->execute("UPDATE images SET has_children = :true WHERE id = :pid", ["pid" => $event->parent_id, "true" => true]);
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2023-11-11 21:49:12 +00:00
|
|
|
if ($old_parent != null) {
|
2020-01-29 20:22:50 +00:00
|
|
|
$this->set_has_children($old_parent);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 15:15:38 +00:00
|
|
|
public static function get_children(Image $image, int $omit = null): array
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $database;
|
2023-11-11 21:49:12 +00:00
|
|
|
$results = $database->get_all_iterable("SELECT * FROM images WHERE parent_id = :pid ", ["pid" => $image->id]);
|
2019-07-05 15:15:38 +00:00
|
|
|
$output = [];
|
|
|
|
foreach ($results as $result) {
|
2023-11-11 21:49:12 +00:00
|
|
|
if ($result["id"] == $omit) {
|
2019-07-05 15:15:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$output[] = new Image($result);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-07-05 15:15:38 +00:00
|
|
|
return $output;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function remove_parent(int $imageID)
|
|
|
|
{
|
|
|
|
global $database;
|
2023-11-11 21:49:12 +00:00
|
|
|
$parentID = $database->get_one("SELECT parent_id FROM images WHERE id = :iid", ["iid" => $imageID]);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
if ($parentID) {
|
2023-11-11 21:49:12 +00:00
|
|
|
$database->execute("UPDATE images SET parent_id = NULL WHERE id = :iid", ["iid" => $imageID]);
|
2020-01-29 20:22:50 +00:00
|
|
|
$this->set_has_children((int)$parentID);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-05 15:15:38 +00:00
|
|
|
|
|
|
|
private function set_has_children(int $parent_id)
|
|
|
|
{
|
|
|
|
global $database;
|
|
|
|
|
|
|
|
// Doesn't work on pgsql
|
2020-01-29 20:22:50 +00:00
|
|
|
// $database->execute("
|
|
|
|
// UPDATE images
|
|
|
|
// SET has_children = (SELECT * FROM (SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END FROM images WHERE parent_id = :pid) AS sub)
|
|
|
|
// WHERE id = :pid
|
|
|
|
// ", ["pid"=>$parentID]);
|
2019-07-05 15:15:38 +00:00
|
|
|
|
2020-01-30 09:01:19 +00:00
|
|
|
$children = $database->get_one(
|
|
|
|
"SELECT COUNT(*) FROM images WHERE parent_id=:pid",
|
2023-11-11 21:49:12 +00:00
|
|
|
["pid" => $parent_id]
|
2020-01-30 09:01:19 +00:00
|
|
|
);
|
2019-07-05 15:15:38 +00:00
|
|
|
$database->execute(
|
2020-10-26 18:55:03 +00:00
|
|
|
"UPDATE images SET has_children = :has_children WHERE id = :pid",
|
2023-11-11 21:49:12 +00:00
|
|
|
["has_children" => $children > 0, "pid" => $parent_id]
|
2019-09-29 13:30:55 +00:00
|
|
|
);
|
2019-07-05 15:15:38 +00:00
|
|
|
}
|
2014-02-03 13:54:09 +00:00
|
|
|
}
|