2014-11-25 21:43:48 +00:00
|
|
|
<?php
|
2020-09-21 12:25:50 +00:00
|
|
|
/*
|
2024-02-02 18:27: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.
|
|
|
|
*/
|
2014-11-25 21:43:48 +00:00
|
|
|
|
2019-11-23 17:29:19 +00:00
|
|
|
namespace OCA\Bookmarks\Controller;
|
2014-11-25 21:43:48 +00:00
|
|
|
|
2019-12-28 16:41:31 +00:00
|
|
|
use OCA\Bookmarks\Db;
|
|
|
|
use OCP\AppFramework\ApiController;
|
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2014-11-25 21:43:48 +00:00
|
|
|
|
|
|
|
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) {
|
2014-11-25 21:43:48 +00:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
$this->userId = $userId;
|
2019-10-26 12:10:49 +00:00
|
|
|
$this->tagMapper = $tagMapper;
|
2014-11-25 21:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-06 09:32:18 +00:00
|
|
|
* @param string $old_name
|
|
|
|
* @return JSONResponse
|
|
|
|
*
|
2014-11-25 21:43:48 +00:00
|
|
|
* @NoAdminRequired
|
2017-02-10 17:23:43 +00:00
|
|
|
* @NoCSRFRequired
|
2023-12-10 17:08:03 +00:00
|
|
|
*
|
2014-11-25 21:43:48 +00:00
|
|
|
*/
|
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);
|
2014-11-25 21:43:48 +00:00
|
|
|
}
|
|
|
|
|
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']);
|
2014-11-25 21:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-06 09:32:18 +00:00
|
|
|
* @param string $old_name
|
|
|
|
* @param string $new_name
|
2018-01-21 21:25:01 +00:00
|
|
|
* @param string $name
|
2016-08-06 09:32:18 +00:00
|
|
|
* @return JSONResponse
|
|
|
|
*
|
2014-11-25 21:43:48 +00:00
|
|
|
* @NoAdminRequired
|
2017-02-10 17:23:43 +00:00
|
|
|
* @NoCSRFRequired
|
2023-12-10 17:08:03 +00:00
|
|
|
*
|
2014-11-25 21:43:48 +00:00
|
|
|
*/
|
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;
|
|
|
|
}
|
2014-11-25 21:43:48 +00:00
|
|
|
|
2018-11-03 14:43:49 +00:00
|
|
|
if ($old_name === "" || $new_name === "") {
|
2021-02-01 13:16:51 +00:00
|
|
|
return new JSONResponse(['status' => 'error', 'data' => ['Must provide old_name and a new name']], Http::STATUS_BAD_REQUEST);
|
2014-11-25 21:43:48 +00:00
|
|
|
}
|
|
|
|
|
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']);
|
2014-11-25 21:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
2014-11-25 21:43:48 +00:00
|
|
|
* @NoAdminRequired
|
2017-02-10 17:23:43 +00:00
|
|
|
* @NoCSRFRequired
|
2023-12-10 17:08:03 +00:00
|
|
|
*
|
2014-11-25 21:43:48 +00:00
|
|
|
*/
|
2020-09-21 12:17:46 +00:00
|
|
|
public function fullTags($count = false): JSONResponse {
|
2014-11-25 21:43:48 +00:00
|
|
|
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);
|
2019-12-28 16:41:31 +00:00
|
|
|
} else {
|
2019-10-26 12:10:49 +00:00
|
|
|
$tags = $this->tagMapper->findAll($this->userId);
|
2014-11-25 21:43:48 +00:00
|
|
|
}
|
|
|
|
return new JSONResponse($tags);
|
|
|
|
}
|
|
|
|
}
|