bookmarks/lib/Controller/TagsController.php

88 lines
2.1 KiB
PHP
Raw Normal View History

<?php
2020-09-21 12:25:50 +00:00
/*
* Copyright (c) 2020-2024. The Nextcloud Bookmarks contributors.
2020-09-21 12:25:50 +00:00
*
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
*/
2019-11-23 17:29:19 +00:00
namespace OCA\Bookmarks\Controller;
use OCA\Bookmarks\Db;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
class TagsController extends ApiController {
private $userId;
2019-10-26 12:10:49 +00:00
/**
* @var Db\TagMapper
*/
private $tagMapper;
2016-10-15 22:41:47 +00:00
2019-11-23 17:29:19 +00:00
public function __construct($appName, $request, $userId, DB\TagMapper $tagMapper) {
parent::__construct($appName, $request);
$this->userId = $userId;
2019-10-26 12:10:49 +00:00
$this->tagMapper = $tagMapper;
}
/**
2016-08-06 09:32:18 +00:00
* @param string $old_name
* @return JSONResponse
*
* @NoAdminRequired
* @NoCSRFRequired
*
*/
2020-09-21 12:17:46 +00:00
public function deleteTag($old_name = ""): JSONResponse {
2018-11-03 14:43:49 +00:00
if ($old_name === "") {
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
}
2019-10-26 12:10:49 +00:00
$this->tagMapper->deleteTag($this->userId, $old_name);
2018-11-03 14:43:49 +00:00
return new JSONResponse(['status' => 'success']);
}
/**
2016-08-06 09:32:18 +00:00
* @param string $old_name
* @param string $new_name
* @param string $name
2016-08-06 09:32:18 +00:00
* @return JSONResponse
*
* @NoAdminRequired
* @NoCSRFRequired
*
*/
2020-09-21 12:17:46 +00:00
public function renameTag($old_name = "", $new_name = "", $name = ''): JSONResponse {
2018-11-03 14:43:49 +00:00
if ($new_name === '') {
$new_name = $name;
}
2018-11-03 14:43:49 +00:00
if ($old_name === "" || $new_name === "") {
return new JSONResponse(['status' => 'error', 'data' => ['Must provide old_name and a new name']], Http::STATUS_BAD_REQUEST);
}
2019-10-26 12:10:49 +00:00
$this->tagMapper->renameTag($this->userId, $old_name, $new_name);
2018-11-03 14:43:49 +00:00
return new JSONResponse(['status' => 'success']);
}
/**
2018-01-28 13:49:36 +00:00
* @param bool $count whether to add the count of bookmarks per tag
2019-10-26 12:10:49 +00:00
* @return JSONResponse
* @NoAdminRequired
* @NoCSRFRequired
*
*/
2020-09-21 12:17:46 +00:00
public function fullTags($count = false): JSONResponse {
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
2018-06-22 22:03:18 +00:00
2019-10-26 12:10:49 +00:00
if ($count === true) {
$tags = $this->tagMapper->findAllWithCount($this->userId);
} else {
2019-10-26 12:10:49 +00:00
$tags = $this->tagMapper->findAll($this->userId);
}
return new JSONResponse($tags);
}
}