Skip to content

Commit

Permalink
add tags
Browse files Browse the repository at this point in the history
  • Loading branch information
mrilyew committed Jan 10, 2025
1 parent cebb4d4 commit 3a114fd
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 33 deletions.
26 changes: 18 additions & 8 deletions VKAPI/Handlers/Docs.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function delete(int $owner_id, int $doc_id): int
{
$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");

Expand All @@ -55,7 +55,7 @@ function edit(int $owner_id, int $doc_id, ?string $title = "", ?string $tags = "
$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()))
Expand All @@ -67,8 +67,8 @@ function edit(int $owner_id, int $doc_id, ?string $title = "", ?string $tags = "

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]))
Expand Down Expand Up @@ -118,13 +118,10 @@ function getById(string $docs, int $return_tags = 0): array

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);
}

Expand All @@ -147,6 +144,19 @@ function getTypes(?int $owner_id)
];
}

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();
Expand Down
15 changes: 13 additions & 2 deletions Web/Models/Entities/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ function isAnonymous(): bool
{
return false;
}

function isPrivate(): bool
{
return $this->getFolder() == Document::VKAPI_FOLDER_PRIVATE;
}

function isImage(): bool
{
Expand Down Expand Up @@ -210,11 +215,17 @@ function copy(User $user): Document

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) ? "," : '');
Expand Down
34 changes: 34 additions & 0 deletions Web/Models/Repositories/Documents.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ function getDocumentById(int $virtual_id, int $real_id, string $access_key = NUL
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([
Expand Down Expand Up @@ -90,6 +103,27 @@ function getTypes(int $owner_id): array
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([
Expand Down
5 changes: 3 additions & 2 deletions Web/Presenters/DocumentsPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function renderList(?int $owner_id = NULL): void

$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();
Expand Down Expand Up @@ -134,10 +135,10 @@ function renderPage(int $virtual_id, int $real_id): void
$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();

Expand Down
39 changes: 25 additions & 14 deletions Web/Presenters/templates/Documents/List.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<div n:foreach="$tabs as $tab" class="mb_tab" n:attr="id => $tab['type'] == $current_tab ? active">
<a href="?tab={$tab['type']}">
{$tab["name"]}
<span class="special_counter">{$tab["count"]}</span>
<span n:if="$tab['count'] > 1" class="special_counter">{$tab["count"]}</span>
</a>
</div>
</div>
Expand All @@ -44,24 +44,35 @@
<div class="summaryBar display_flex_row display_flex_space_between">
<div class="summary">{tr($locale_string, $count)}.</div>

<select name="docs_sort">
<select n:if="$count > 3" name="docs_sort">
<option n:attr="selected => $order == 0" value="0">{_documents_sort_add}</option>
<option n:attr="selected => $order == 1" value="1">{_documents_sort_alphabet}</option>
<option n:attr="selected => $order == 2" value="2">{_documents_sort_size}</option>
</select>
</div>
<div class="container_white scroll_container">
{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}
<div n:attr="id => !$is_gallery && sizeof($tags) > 0 ? search_page">
<div n:class="container_white, scroll_container, !$is_gallery && sizeof($tags) > 0 ? page_wrap_content_main">
{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}
</div>

<div n:if="!$is_gallery && sizeof($tags) > 0" class='page_wrap_content_options verticalGrayTabsWrapper'>
<div class="page_wrap_content_options_list verticalGrayTabs with_padding">
<a id="used">{_documents_all}</a>
{foreach $tags as $tag}
<a href="/search?section=docs&tags={urlencode($tag)}">{$tag}</a>
{/foreach}
</div>
</div>
</div>
{include "../components/paginator.xml", conf => $paginatorConf}
</div>
Expand Down
4 changes: 2 additions & 2 deletions Web/Presenters/templates/Documents/components/doc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{var $modifiable = $doc->canBeModifiedBy($thisUser)}

<div n:class="docMainItem, docListViewItem, $scroll_context ? scroll_node" data-id="{$doc->getPrettiestId()}">
<a class="viewerOpener" href="/doc{$doc->getPrettyId()}">
<a class="viewerOpener" href="/doc{$doc->getPrettyId()}?key={$doc->getAccessKey()}">
{if $preview}
<img class="doc_icon" alt="document_preview" src="{$preview->getURLBySizeId('tiny')}">
{else}
Expand All @@ -14,7 +14,7 @@
{/if}
</a>
<div class="doc_content noOverflow">
<a class="viewerOpener" href="/doc{$doc->getPrettyId()}"><b class="noOverflow doc_name">{$doc->getName()}</b></a>
<a class="viewerOpener" href="/doc{$doc->getPrettyId()}?key={$doc->getAccessKey()}"><b class="noOverflow doc_name">{$doc->getName()}</b></a>

<div class="doc_content_info">
<span>{$doc->getPublicationTime()}</span>,
Expand Down
2 changes: 1 addition & 1 deletion Web/Presenters/templates/Documents/components/image.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{var $copied = !isset($club) ? $doc->isCopiedBy($thisUser) : $doc->isCopiedBy($club)}
{var $modifiable = $doc->canBeModifiedBy($thisUser)}

<a href="/doc{$doc->getPrettyId()}" n:class="docMainItem, viewerOpener, docGalleryItem, $scroll_context ? scroll_node" data-id="{$doc->getPrettiestId()}">
<a href="/doc{$doc->getPrettyId()}?key={$doc->getAccessKey()}" n:class="docMainItem, viewerOpener, docGalleryItem, $scroll_context ? scroll_node" data-id="{$doc->getPrettiestId()}">
<img loading="lazy" src="{$preview->getURLBySizeId('medium')}" alt="gallery photo">

<div class="doc_top_panel doc_shown_by_hover">
Expand Down
2 changes: 1 addition & 1 deletion Web/Presenters/templates/Group/View.xml
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@
</div>
<div>
<div class="content_subtitle">
{tr("documents", $topicsCount)}
{tr("documents", $docsCount)}
<div style="float: right;">
<a href="/docs{$club->getRealId()}">{_all_title}</a>
</div>
Expand Down
6 changes: 5 additions & 1 deletion Web/static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -4176,10 +4176,14 @@ hr {
background-position-y: -21px;
}

.docListViewItem .doc_volume #report_icon, .docGalleryItem .doc_top_panel #report_icon {
.docListViewItem .doc_volume #report_icon {
background-position-x: -40px;
}

.docGalleryItem .doc_top_panel #report_icon {
background-position-x: -24px;
}

.docListViewItem .doc_volume #edit_icon {
background-position-x: -20px;
}
Expand Down
7 changes: 5 additions & 2 deletions Web/static/js/al_docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ u(document).on("drop", "#_document_upload_frame", (e) => {

u(document).on('click', '.docMainItem #edit_icon', async (e) => {
e.preventDefault()
if(u("#ajloader").hasClass("shown")) {
return
}

const target = u(e.target).closest("#edit_icon")
const item = target.closest('.docMainItem')
Expand All @@ -121,10 +124,10 @@ u(document).on('click', '.docMainItem #edit_icon', async (e) => {
title: tr("document_editing_in_general"),
body: `
<p><b>${tr("info_name")}</b></p>
<input type="text" name="doc_name" value="${doc.title}" placeholder="...">
<input maxlength="128" type="text" name="doc_name" value="${doc.title}" placeholder="...">
<label>
<input maxlength="255" value="0" type="radio" name="doc_access" ${doc.folder_id != 3 ? "checked" : ''}>
<input value="0" type="radio" name="doc_access" ${doc.folder_id != 3 ? "checked" : ''}>
${tr("private_document")}
</label>
<br>
Expand Down
1 change: 1 addition & 0 deletions locales/en.strings
Original file line number Diff line number Diff line change
Expand Up @@ -2331,6 +2331,7 @@
"remove" = "Remove";

"document" = "Document";
"documents_all" = "All documents";
"document_type_0" = "All";
"document_type_1" = "Text";
"document_type_2" = "Archives";
Expand Down
1 change: 1 addition & 0 deletions locales/ru.strings
Original file line number Diff line number Diff line change
Expand Up @@ -2226,6 +2226,7 @@
"remove" = "Удалить";

"document" = "Документ";
"documents_all" = "Все документы";
"document_type_0" = "Все";
"document_type_1" = "Текстовые";
"document_type_2" = "Архивы";
Expand Down

0 comments on commit 3a114fd

Please sign in to comment.