From 3a114fdba9e84e99738d9b4c220169d8f335ab22 Mon Sep 17 00:00:00 2001 From: mrilyew <99399973+mrilyew@users.noreply.github.com> Date: Fri, 10 Jan 2025 19:13:23 +0300 Subject: [PATCH] add tags --- VKAPI/Handlers/Docs.php | 26 +++++++++---- Web/Models/Entities/Document.php | 15 ++++++- Web/Models/Repositories/Documents.php | 34 ++++++++++++++++ Web/Presenters/DocumentsPresenter.php | 5 ++- Web/Presenters/templates/Documents/List.xml | 39 ++++++++++++------- .../templates/Documents/components/doc.xml | 4 +- .../templates/Documents/components/image.xml | 2 +- Web/Presenters/templates/Group/View.xml | 2 +- Web/static/css/main.css | 6 ++- Web/static/js/al_docs.js | 7 +++- locales/en.strings | 1 + locales/ru.strings | 1 + 12 files changed, 109 insertions(+), 33 deletions(-) diff --git a/VKAPI/Handlers/Docs.php b/VKAPI/Handlers/Docs.php index c7548b2a..ae05bddf 100644 --- a/VKAPI/Handlers/Docs.php +++ b/VKAPI/Handlers/Docs.php @@ -30,7 +30,7 @@ final class Docs extends VKAPIRequestHandler { $this->requireUser(); $this->willExecuteWriteAction(); - $doc = (new Documents)->getDocumentById($owner_id, $doc_id); + $doc = (new Documents)->getDocumentByIdUnsafe($owner_id, $doc_id); if(!$doc || $doc->isDeleted()) $this->fail(1150, "Invalid document id"); @@ -55,7 +55,7 @@ final class Docs extends VKAPIRequestHandler $this->requireUser(); $this->willExecuteWriteAction(); - $doc = (new Documents)->getDocumentById($owner_id, $doc_id); + $doc = (new Documents)->getDocumentByIdUnsafe($owner_id, $doc_id); if(!$doc || $doc->isDeleted()) $this->fail(1150, "Invalid document id"); if(!$doc->canBeModifiedBy($this->getUser())) @@ -67,8 +67,8 @@ final class Docs extends VKAPIRequestHandler if($title) $doc->setName($title); - if($tags) - $doc->setTags($tags); + + $doc->setTags($tags); if(in_array($folder_id, [0, 3])) $doc->setFolder_id($folder_id); if(in_array($owner_hidden, [0, 1])) @@ -118,13 +118,10 @@ final class Docs extends VKAPIRequestHandler foreach($item_ids as $id) { $splitted_id = explode("_", $id); - $doc = (new Documents)->getDocumentById((int)$splitted_id[0], (int)$splitted_id[1]); + $doc = (new Documents)->getDocumentById((int)$splitted_id[0], (int)$splitted_id[1], $splitted_id[2]); if(!$doc || $doc->isDeleted()) continue; - if(!$doc->checkAccessKey($splitted_id[2])) - continue; - $response[] = $doc->toVkApiStruct($this->getUser(), $return_tags === 1); } @@ -147,6 +144,19 @@ final class Docs extends VKAPIRequestHandler ]; } + function getTags(?int $owner_id, ?int $type = 0) + { + $this->requireUser(); + if(!$owner_id) + $owner_id = $this->getUser()->getId(); + + if($owner_id > 0 && $owner_id != $this->getUser()->getId()) + $this->fail(15, "Access denied"); + + $tags = (new Documents)->getTags($owner_id, $type); + return $tags; + } + function search(string $q = "", int $search_own = -1, int $order = -1, int $count = 30, int $offset = 0, int $return_tags = 0, int $type = 0, ?string $tags = NULL): object { $this->requireUser(); diff --git a/Web/Models/Entities/Document.php b/Web/Models/Entities/Document.php index 15b558b1..030ffe76 100644 --- a/Web/Models/Entities/Document.php +++ b/Web/Models/Entities/Document.php @@ -155,6 +155,11 @@ class Document extends Media { return false; } + + function isPrivate(): bool + { + return $this->getFolder() == Document::VKAPI_FOLDER_PRIVATE; + } function isImage(): bool { @@ -210,11 +215,17 @@ class Document extends Media function setTags(?string $tags): bool { - if(!$tags) { - return false; + if(is_null($tags)) { + $this->stateChanges("tags", NULL); + return true; } $parsed = explode(",", $tags); + if(sizeof($parsed) < 1 || $parsed[0] == "") { + $this->stateChanges("tags", NULL); + return true; + } + $result = ""; foreach($parsed as $tag) { $result .= trim($tag) . ($tag != end($parsed) ? "," : ''); diff --git a/Web/Models/Repositories/Documents.php b/Web/Models/Repositories/Documents.php index b6c51bf0..da388cc0 100644 --- a/Web/Models/Repositories/Documents.php +++ b/Web/Models/Repositories/Documents.php @@ -45,6 +45,19 @@ class Documents return $n_doc; } + function getDocumentByIdUnsafe(int $virtual_id, int $real_id): ?Document + { + $doc = $this->documents->where(['virtual_id' => $virtual_id, 'id' => $real_id]); + + $doc = $doc->fetch(); + if(is_null($doc)) + return NULL; + + $n_doc = new Document($doc); + + return $n_doc; + } + function getDocumentsByOwner(int $owner, int $order = 0, int $type = -1): EntityStream { $search = $this->documents->where([ @@ -90,6 +103,27 @@ class Documents return $response; } + function getTags(int $owner_id, ?int $type = 0): array + { + $query = "SELECT `tags` FROM `documents` WHERE `owner` = ? AND `deleted` = 0 AND `unlisted` = 0 "; + if($type > 0 && $type < 9) { + $query .= "AND `type` = $type"; + } + + $query .= " AND `tags` IS NOT NULL ORDER BY `id`"; + $result = DatabaseConnection::i()->getConnection()->query($query, $owner_id); + $tags = []; + foreach($result as $res) { + $tags[] = $res->tags; + } + $imploded_tags = implode(",", $tags); + $exploded_tags = array_values(array_unique(explode(",", $imploded_tags))); + if($exploded_tags[0] == "") + return []; + + return array_slice($exploded_tags, 0, 50); + } + function find(string $query, array $params = [], array $order = ['type' => 'id', 'invert' => false]): Util\EntityStream { $result = $this->documents->where("name LIKE ?", "%$query%")->where([ diff --git a/Web/Presenters/DocumentsPresenter.php b/Web/Presenters/DocumentsPresenter.php index 6fd827d7..93cfcf62 100644 --- a/Web/Presenters/DocumentsPresenter.php +++ b/Web/Presenters/DocumentsPresenter.php @@ -36,6 +36,7 @@ final class DocumentsPresenter extends OpenVKPresenter $docs = (new Documents)->getDocumentsByOwner($owner_id, (int)$order, (int)$tab); $this->template->tabs = (new Documents)->getTypes($owner_id); + $this->template->tags = (new Documents)->getTags($owner_id, (int)$tab); $this->template->current_tab = $tab; $this->template->order = $order; $this->template->count = $docs->size(); @@ -134,10 +135,10 @@ final class DocumentsPresenter extends OpenVKPresenter $this->assertUserLoggedIn(); $access_key = $this->queryParam("key"); - $doc = (new Documents)->getDocumentById((int)$virtual_id, (int)$real_id); + $doc = (new Documents)->getDocumentById((int)$virtual_id, (int)$real_id, $access_key); if(!$doc || $doc->isDeleted()) $this->notFound(); - + if(!$doc->checkAccessKey($access_key)) $this->notFound(); diff --git a/Web/Presenters/templates/Documents/List.xml b/Web/Presenters/templates/Documents/List.xml index 9094bf19..ad9a9948 100644 --- a/Web/Presenters/templates/Documents/List.xml +++ b/Web/Presenters/templates/Documents/List.xml @@ -35,7 +35,7 @@
{$tab["name"]} - {$tab["count"]} + {$tab["count"]}
@@ -44,24 +44,35 @@
{tr($locale_string, $count)}.
-
-
- {if $count > 0} - {foreach $docs as $doc} - {if $is_gallery} - {include "components/image.xml", doc => $doc, scroll_context => true, club => isset($group) ? $group : NULL} - {else} - {include "components/doc.xml", doc => $doc, scroll_context => true, club => isset($group) ? $group : NULL} - {/if} - {/foreach} - {else} - {include "../components/error.xml", description => tr("there_is_no_documents_alright")} - {/if} +
+
+ {if $count > 0} + {foreach $docs as $doc} + {if $is_gallery} + {include "components/image.xml", doc => $doc, scroll_context => true, club => isset($group) ? $group : NULL} + {else} + {include "components/doc.xml", doc => $doc, scroll_context => true, club => isset($group) ? $group : NULL} + {/if} + {/foreach} + {else} + {include "../components/error.xml", description => tr("there_is_no_documents_alright")} + {/if} +
+ +
+
+ {_documents_all} + {foreach $tags as $tag} + {$tag} + {/foreach} +
+
{include "../components/paginator.xml", conf => $paginatorConf}
diff --git a/Web/Presenters/templates/Documents/components/doc.xml b/Web/Presenters/templates/Documents/components/doc.xml index c805661f..48baf653 100644 --- a/Web/Presenters/templates/Documents/components/doc.xml +++ b/Web/Presenters/templates/Documents/components/doc.xml @@ -4,7 +4,7 @@ {var $modifiable = $doc->canBeModifiedBy($thisUser)}
- + {if $preview} document_preview {else} @@ -14,7 +14,7 @@ {/if}
- {$doc->getName()} + {$doc->getName()}