Improved tag management

This commit is contained in:
Sergio Brighenti 2020-04-08 18:36:10 +02:00
parent f42064d9ee
commit fd11209e08
5 changed files with 16 additions and 9 deletions

View file

@ -279,6 +279,7 @@ class MediaController extends Controller
throw new HttpNotFoundException($request);
} finally {
$this->database->query('DELETE FROM `uploads` WHERE `id` = ?', $id);
$this->database->query('DELETE FROM `tags` WHERE `tags`.`id` NOT IN (SELECT `uploads_tags`.`tag_id` FROM `uploads_tags`)');
}
}
@ -326,7 +327,7 @@ class MediaController extends Controller
set_time_limit(0);
$mime = $storage->getMimetype($media->storage_path);
if (param($request, 'width') !== null && explode('/', $mime)[0] === 'image') {
if ((param($request, 'width') || param($request, 'height')) !== null && explode('/', $mime)[0] === 'image') {
return $this->makeThumbnail($storage, $media, param($request, 'width'), param($request, 'height'), $disposition);
} else {
$stream = new Stream($storage->readStream($media->storage_path));

View file

@ -55,13 +55,15 @@ class TagController extends Controller
$result = make(TagQuery::class)->removeTag(param($request, 'tagId'), param($request, 'mediaId'));
if (!$result) {
if ($result === null) {
throw new HttpNotFoundException($request);
}
$this->logger->info("Tag removed ".param($request, 'tagId').', from media '.param($request, 'mediaId'));
return $response;
return json($response, [
'deleted' => $result,
]);
}
/**

View file

@ -39,7 +39,7 @@ class TagQuery
return $this->db->query('SELECT * FROM `tags` ORDER BY `name`')->fetchAll();
}
return $this->db->query('SELECT `tags`.* FROM `tags` INNER JOIN `uploads_tags` ON `tags`.`id` = `uploads_tags`.`tag_id` INNER JOIN `uploads` ON `uploads`.`id` = `uploads_tags`.`upload_id` WHERE `uploads`.`user_id` = ? ORDER BY `tags`.`name`', $this->userId)->fetchAll();
return $this->db->query('SELECT DISTINCT `tags`.* FROM `tags` INNER JOIN `uploads_tags` ON `tags`.`id` = `uploads_tags`.`tag_id` INNER JOIN `uploads` ON `uploads`.`id` = `uploads_tags`.`upload_id` WHERE `uploads`.`user_id` = ? ORDER BY `tags`.`name`', $this->userId)->fetchAll();
}
/**
@ -95,11 +95,12 @@ class TagQuery
if ($this->db->query('SELECT COUNT(*) AS `count` FROM `uploads_tags` WHERE `tag_id` = ?', $tag->id)->fetch()->count == 0) {
$this->db->query('DELETE FROM `tags` WHERE `id` = ? ', $tag->id);
return true;
}
return true;
return false;
}
return false;
return null;
}
}

View file

@ -12,12 +12,12 @@
<button class="btn btn-outline-dark dropdown-toggle" type="button" data-toggle="dropdown">
<i class="fas fa-tags"></i>
</button>
<div class="dropdown-menu" style="max-height: 250px; overflow: auto">
<div class="dropdown-menu" id="dropdown-tag-list" style="max-height: 250px; overflow: auto">
{% if tags is empty %}
<h6 class="dropdown-header">{{ lang('no_tags') }}</h6>
{% else %}
{% for tag in tags %}
<a class="dropdown-item {{ request.queryParams['tag'] == tag.id ? 'active' }}" href="{{ queryParams({'tag': tag.id}) }}">{{ tag.name }}</a>
<a class="dropdown-item {{ request.queryParams['tag'] == tag.id ? 'active' }}" href="{{ queryParams({'tag': tag.id}) }}" data-id="{{ tag.id }}">{{ tag.name }}</a>
{% endfor %}
{% endif %}
</div>

View file

@ -220,8 +220,11 @@ var app = {
$.post(window.AppConfig.base_url + '/tag/remove', {
'tagId': $tag.data('id'),
'mediaId': $tag.data('media')
}, function () {
}, function (data) {
$tag.remove();
if (data.deleted) {
$('#dropdown-tag-list > a[data-id="' + $tag.data('id') + '"]').remove();
}
});
}
};