bookmarks/lib/Controller/SettingsController.php

229 lines
4.7 KiB
PHP
Raw Normal View History

2018-06-01 21:41:56 +00:00
<?php
2020-09-21 12:25:50 +00:00
/*
* Copyright (c) 2020. The Nextcloud Bookmarks contributors.
2018-06-01 21:41:56 +00:00
*
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.
2018-06-01 21:41:56 +00:00
*/
2019-11-23 17:29:19 +00:00
namespace OCA\Bookmarks\Controller;
2018-06-01 21:41:56 +00:00
2020-09-21 12:17:46 +00:00
use Exception;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IConfig;
2020-09-21 12:17:46 +00:00
use OCP\IL10N;
use OCP\IRequest;
2018-06-01 21:41:56 +00:00
class SettingsController extends ApiController {
/** @var IConfig */
private $config;
/** @var string */
private $userId;
/**
2020-09-21 12:17:46 +00:00
* @var IL10N
*/
private $l;
2018-06-01 21:41:56 +00:00
/**
* @param string $appName
* @param IRequest $request
* @param string $userId
* @param IConfig $config
2020-09-21 12:17:46 +00:00
* @param IL10N $l
2018-06-01 21:41:56 +00:00
*/
public function __construct(
2020-09-21 12:17:46 +00:00
$appName, $request, $userId, IConfig $config, IL10N $l
2018-06-01 21:41:56 +00:00
) {
parent::__construct($appName, $request);
$this->config = $config;
$this->userId = $userId;
$this->l = $l;
2018-06-01 21:41:56 +00:00
}
private function getSetting(string $key, string $name, $default): JSONResponse {
2018-06-01 21:41:56 +00:00
try {
$userValue = $this->config->getUserValue(
2018-06-01 21:41:56 +00:00
$this->userId,
$this->appName,
$key,
$default
2018-06-01 21:41:56 +00:00
);
2020-09-21 12:17:46 +00:00
} catch (Exception $e) {
2018-06-01 21:41:56 +00:00
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
}
return new JSONResponse([$name => $userValue], Http::STATUS_OK);
}
private function setSetting(string $key, string $value): JSONResponse {
try {
$this->config->setUserValue(
$this->userId,
$this->appName,
$key,
$value
);
2020-09-21 12:17:46 +00:00
} catch (Exception $e) {
return new JSONResponse(['status' => 'error'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
return new JSONResponse(['status' => 'success'], Http::STATUS_OK);
}
/**
* get sorting option config value
*
* @return JSONResponse
*
* @NoAdminRequired
*/
2020-09-21 12:17:46 +00:00
public function getSorting(): JSONResponse {
return $this->getSetting('sorting', 'sorting', 'lastmodified');
2018-06-01 21:41:56 +00:00
}
/**
* set sorting option config value
*
* @param string $sorting
* @return JSONResponse
*
* @NoAdminRequired
*/
2020-09-21 12:17:46 +00:00
public function setSorting($sorting = ""): JSONResponse {
$legalArguments = ['title', 'added', 'clickcount', 'lastmodified', 'index'];
2018-06-01 21:41:56 +00:00
if (!in_array($sorting, $legalArguments)) {
2018-06-01 23:28:58 +00:00
return new JSONResponse(['status' => 'error'], Http::STATUS_BAD_REQUEST);
2018-06-01 21:41:56 +00:00
}
return $this->setSetting(
2022-07-11 12:58:56 +00:00
'sorting',
$sorting
);
2018-12-30 10:19:58 +00:00
}
/**
* get view mode option config value
*
* @return JSONResponse
*
* @NoAdminRequired
*/
2020-09-21 12:17:46 +00:00
public function getViewMode(): JSONResponse {
return $this->getSetting('viewMode', 'viewMode', 'grid');
2018-12-30 10:19:58 +00:00
}
/**
* set sorting option config value
*
* @param string $viewMode
2018-12-30 10:19:58 +00:00
* @return JSONResponse
*
* @NoAdminRequired
*/
2020-09-21 12:17:46 +00:00
public function setViewMode($viewMode = ""): JSONResponse {
2018-12-30 10:19:58 +00:00
$legalArguments = ['grid', 'list'];
if (!in_array($viewMode, $legalArguments)) {
return new JSONResponse(['status' => 'error'], Http::STATUS_BAD_REQUEST);
}
return $this->setSetting(
2022-07-11 12:58:56 +00:00
'viewMode',
$viewMode
);
2018-06-01 21:41:56 +00:00
}
/**
* get per-user bookmarks limit
*
* @return JSONResponse
*
* @NoAdminRequired
*/
2020-09-21 12:17:46 +00:00
public function getLimit(): JSONResponse {
$limit = (int)$this->config->getAppValue('bookmarks', 'performance.maxBookmarksperAccount', 0);
return new JSONResponse(['limit' => $limit], Http::STATUS_OK);
}
/**
* get user-defined archive path
*
* @return JSONResponse
*
* @NoAdminRequired
*/
2020-09-21 12:17:46 +00:00
public function getArchivePath(): JSONResponse {
return $this->getSetting(
'archive.filePath',
'archivePath',
$this->l->t('Bookmarks')
);
}
/**
* set user-defined archive path
*
2020-09-21 12:17:46 +00:00
* @param string $archivePath
* @return JSONResponse
*
* @NoAdminRequired
*/
2020-09-21 13:14:54 +00:00
public function setArchivePath(string $archivePath): JSONResponse {
return $this->setSetting('archive.filePath', $archivePath);
}
/**
* get user-defined archive path
*
* @return JSONResponse
*
* @NoAdminRequired
*/
public function getBackupEnabled(): JSONResponse {
return $this->getSetting(
'backup.enabled',
'backupEnabled',
(string) false
);
}
/**
* set user-defined backup path
*
* @param string $backupEnabled
* @return JSONResponse
*
* @NoAdminRequired
*/
public function setBackupEnabled(bool $backupEnabled): JSONResponse {
return $this->setSetting('backup.enabled', (string) $backupEnabled);
}
/**
* get user-defined archive path
*
* @return JSONResponse
*
* @NoAdminRequired
*/
public function getBackupPath(): JSONResponse {
return $this->getSetting(
'backup.filePath',
'backupPath',
$this->l->t('Bookmarks Backups')
);
}
/**
* set user-defined backup path
*
* @param string $backupPath
* @return JSONResponse
*
* @NoAdminRequired
*/
public function setBackupPath(string $backupPath): JSONResponse {
return $this->setSetting('backup.filePath', $backupPath);
}
2018-06-01 21:41:56 +00:00
}