-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathAdminSettingsController.php
More file actions
136 lines (124 loc) · 4.53 KB
/
AdminSettingsController.php
File metadata and controls
136 lines (124 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
declare(strict_types=1);
/**
* This file is part of the Unsplash App
* and licensed under the AGPL.
*/
namespace OCA\Unsplash\Controller;
use OCA\Unsplash\Services\SettingsService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
/**
* Class AdminSettingsController
*
* @package OCA\Unsplash\Controller
*/
class AdminSettingsController extends Controller
{
/**
* AdminSettingsController constructor.
*
* @param $appName
* @param IRequest $request
* @param SettingsService $settings
*/
public function __construct(
$appName,
IRequest $request,
private SettingsService $settings,
)
{
parent::__construct($appName, $request);
}
/**
* Update the app default settings
*
* @param string $key
* @param $value
*
* @return JSONResponse
*/
public function set(string $key, bool|string|int|array|null $value): JSONResponse
{
// TODO: Refactor all the boolean handling (will need to be fixed in SettingsService at same time) and streamline all the code below
if (isset($value) && is_string($value) && strtolower($value) === 'true') {
$value = true;
}
if (isset($value) && is_string($value) && strtolower($value) === 'false') {
$value = false;
}
if ($key === 'style/login') { // TODO: $value should be sanity checked too
if ($value) {
$this->settings->setServerStyleLoginEnabled(1);
} else {
$this->settings->setServerStyleLoginEnabled(0);
}
} else if ($key === 'style/dashboard') { // TODO: $value should be sanity checked too
if ($value) {
$this->settings->setServerStyleDashboardEnabled(1);
} else {
$this->settings->setServerStyleDashboardEnabled(0);
}
} else if ($key === 'provider/provider') {
$this->settings->setImageProviderSanitized((string)$value);
return $this->generateProviderResponse($value);
} else if ($key === 'provider/customization') {
$this->settings->setImageProviderCustomization((string)$value);
} else if ($key === 'style/tint') {
if ($value) {
$this->settings->setTint(1);
} else {
$this->settings->setTint(0);
}
} else if ($key === 'style/strength/color') {
$this->settings->setColorStrength(filter_var($value, FILTER_SANITIZE_NUMBER_INT));
} else if ($key === 'style/strength/blur') {
$this->settings->setBlurStrength(filter_var($value, FILTER_SANITIZE_NUMBER_INT));
} else if ($key === 'style/login/highvisibility') {
if ($value) {
$this->settings->setHighVisibilityLogin(1);
} else {
$this->settings->setHighVisibilityLogin(0);
}
} else if ($key === 'provider/token') {
$this->settings->setCurrentProviderToken($value);
} else if ($key === 'delete/cache') {
$this->settings->updateCachedBackground();
return $this->generateProviderResponse("success");
} else {
return new JSONResponse(['status' => 'error'], Http::STATUS_BAD_REQUEST);
}
return new JSONResponse(['status' => $value]);
}
/**
* Get the customizationstring for the requested providername
*
* @param string $providername
*
* @return JSONResponse
*/
public function getCustomization(string $providername): JSONResponse
{
$validProviders = $this->settings->getAllImageProvider();
if (!in_array($providername, $validProviders, true)) {
return new JSONResponse(['status' => 'error', 'message' => 'Unknown provider'], Http::STATUS_BAD_REQUEST);
}
$provider = $this->settings->getImageProvider($providername);
return new JSONResponse(['status' => 'ok', 'customization' => $provider->getCustomSearchterms()]);
}
private function generateProviderResponse(string $value): JSONResponse
{
$cached = $this->settings->isCached();
$provider = $this->settings->getSelectedImageProvider();
$name = $provider->getName();
$url = $provider->getCachedImageURL();
return new JSONResponse([
'status' => $value,
'isCached' => $cached,
'provider' => $name,
'imageURL' => $url
]);
}
}