2020-10-26 15:46:05 +00:00
|
|
|
<?php
|
|
|
|
namespace OCA\Radio\Controller;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use OCP\AppFramework\ApiController;
|
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\Util;
|
|
|
|
|
|
|
|
class SettingsController extends ApiController {
|
|
|
|
|
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $userId;
|
|
|
|
/**
|
|
|
|
* @var IL10N
|
|
|
|
*/
|
|
|
|
private $l;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $appName
|
|
|
|
* @param IRequest $request
|
|
|
|
* @param string $userId
|
|
|
|
* @param IConfig $config
|
|
|
|
* @param IL10N $l
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
$appName, $request, $userId, IConfig $config, IL10N $l
|
|
|
|
) {
|
|
|
|
parent::__construct($appName, $request);
|
|
|
|
$this->config = $config;
|
|
|
|
$this->userId = $userId;
|
|
|
|
$this->l = $l;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getSetting(string $key, string $name, $default): JSONResponse {
|
|
|
|
try {
|
|
|
|
$userValue = $this->config->getUserValue(
|
|
|
|
$this->userId,
|
|
|
|
$this->appName,
|
|
|
|
$key,
|
|
|
|
$default
|
|
|
|
);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Util::writeLog('radio', $e->getMessage(), Util::ERROR);
|
|
|
|
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new JSONResponse([$name => $userValue], Http::STATUS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setSetting($key, $value): JSONResponse {
|
|
|
|
try {
|
|
|
|
$this->config->setUserValue(
|
|
|
|
$this->userId,
|
|
|
|
$this->appName,
|
|
|
|
$key,
|
|
|
|
$value
|
|
|
|
);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return new JSONResponse(['status' => 'error'], Http::STATUS_INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new JSONResponse(['status' => 'success'], Http::STATUS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-27 12:43:30 +00:00
|
|
|
* set menu state
|
2020-10-26 15:46:05 +00:00
|
|
|
*
|
2020-10-27 12:43:30 +00:00
|
|
|
* @param string $menuState
|
2020-10-26 15:46:05 +00:00
|
|
|
* @return JSONResponse
|
|
|
|
*
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
2020-10-27 12:43:30 +00:00
|
|
|
public function setMenuState($menuState = ""): JSONResponse {
|
|
|
|
$legalArguments = ['TOP', 'RECENT', 'NEW', 'FAVORITES', 'CATEGORIES'];
|
|
|
|
if (!in_array($menuState, $legalArguments)) {
|
2020-10-26 15:46:05 +00:00
|
|
|
return new JSONResponse(['status' => 'error'], Http::STATUS_BAD_REQUEST);
|
|
|
|
}
|
|
|
|
return $this->setSetting(
|
2020-10-27 12:43:30 +00:00
|
|
|
'menuState',
|
|
|
|
$menuState
|
2020-10-26 15:46:05 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-27 12:43:30 +00:00
|
|
|
* get menu state
|
2020-10-26 15:46:05 +00:00
|
|
|
*
|
|
|
|
* @return JSONResponse
|
|
|
|
*
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
2020-10-27 12:43:30 +00:00
|
|
|
public function getMenuState(): JSONResponse {
|
|
|
|
return $this->getSetting('menuState', 'menuState', 'TOP');
|
2020-10-26 15:46:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|