Passed
Push — master ( 9ca045...4164c5 )
by Maxence
02:09
created

SettingsController::setSettingsAdmin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
/**
3
 * CMS Pico - Integration of Pico within your files to create websites.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\CMSPico\Controller;
28
29
use Exception;
30
use OCA\CMSPico\AppInfo\Application;
31
use OCA\CMSPico\Service\ConfigService;
32
use OCA\CMSPico\Service\MiscService;
33
use OCA\CMSPico\Service\TemplatesService;
34
use OCA\CMSPico\Service\WebsitesService;
35
use OCP\AppFramework\Controller;
36
use OCP\AppFramework\Http;
37
use OCP\AppFramework\Http\DataResponse;
38
use OCP\IRequest;
39
40
class SettingsController extends Controller {
41
42
	/** @var string */
43
	private $userId;
44
45
	/** @var ConfigService */
46
	private $configService;
47
48
	/** @var TemplatesService */
49
	private $templatesService;
50
51
	/** @var WebsitesService */
52
	private $websitesService;
53
54
	/** @var MiscService */
55
	private $miscService;
56
57
58
	/**
59
	 * NavigationController constructor.
60
	 *
61
	 * @param IRequest $request
62
	 * @param string $userId
63
	 * @param ConfigService $configService
64
	 * @param TemplatesService $templatesService
65
	 * @param WebsitesService $websitesService
66
	 * @param MiscService $miscService
67
	 */
68
	function __construct(
69
		IRequest $request, $userId, ConfigService $configService, TemplatesService $templatesService,
70
		WebsitesService $websitesService,
71
		MiscService $miscService
72
	) {
73
		parent::__construct(Application::APP_NAME, $request);
74
		$this->userId = $userId;
75
		$this->configService = $configService;
76
		$this->templatesService = $templatesService;
77
		$this->websitesService = $websitesService;
78
		$this->miscService = $miscService;
79
	}
80
81
82
	/**
83
	 * @NoAdminRequired
84
	 *
85
	 * @param array $data
86
	 *
87
	 * @return DataResponse
88
	 */
89
	public function createPersonalWebsite($data) {
90
91
		try {
92
			$this->websitesService->createWebsite(
93
				$data['name'], $this->userId, $data['website'], $data['path'], $data['template']
94
			);
95
96
			return $this->miscService->success(
97
				[
98
					'name'     => $data['name'],
99
					'websites' => $this->websitesService->getWebsitesFromUser($this->userId)
100
				]
101
			);
102
		} catch (Exception $e) {
103
			return $this->miscService->fail(['name' => $data['name'], 'error' => $e->getMessage()]);
104
		}
105
	}
106
107
108
	/**
109
	 * @NoAdminRequired
110
	 *
111
	 * @param $data
112
	 *
113
	 * @return DataResponse
114
	 */
115
	public function removePersonalWebsite($data) {
116
117
		try {
118
			$this->websitesService->deleteWebsite($data['id'], $this->userId);
119
120
			return $this->miscService->success(
121
				[
122
					'name' => $data['name'],
123
					'websites' => $this->websitesService->getWebsitesFromUser($this->userId)
124
				]
125
			);
126
		} catch (Exception $e) {
127
			return $this->miscService->fail(['name' => $data['name'], 'error' => $e->getMessage()]);
128
		}
129
	}
130
131
132
	/**
133
	 * @NoAdminRequired
134
	 *
135
	 * @param int $siteId
136
	 * @param string $option
137
	 * @param string $value
138
	 *
139
	 * @return DataResponse
140
	 */
141
	public function editPersonalWebsiteOption($siteId, $option, $value) {
142
143
		try {
144
			$website = $this->websitesService->getWebsiteFromId($siteId);
145
146
			$website->hasToBeOwnedBy($this->userId);
147
			$website->setOption((string)$option, (string)$value);
148
149
			$this->websitesService->updateWebsite($website);
0 ignored issues
show
Bug introduced by
$website of type OCA\CMSPico\Model\Website is incompatible with the type string expected by parameter $website of OCA\CMSPico\Service\Webs...ervice::updateWebsite(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

149
			$this->websitesService->updateWebsite(/** @scrutinizer ignore-type */ $website);
Loading history...
150
151
			return $this->miscService->success(
152
				['websites' => $this->websitesService->getWebsitesFromUser($this->userId)]
153
			);
154
		} catch (Exception $e) {
155
			return $this->miscService->fail(['error' => $e->getMessage()]);
156
		}
157
	}
158
159
160
	/**
161
	 * @NoAdminRequired
162
	 *
163
	 * @return DataResponse
164
	 */
165
	public function getPersonalWebsites() {
166
		try {
167
			$websites = $this->websitesService->getWebsitesFromUser($this->userId);
168
169
			return $this->miscService->success(['websites' => $websites]);
170
		} catch (Exception $e) {
171
			return $this->miscService->fail(['error' => $e->getMessage()]);
172
		}
173
	}
174
175
176
	/**
177
	 * @return DataResponse
178
	 */
179
	public function getSettingsAdmin() {
180
		$data = [
181
			'templates'     => $this->templatesService->getTemplatesList(true),
182
			'templates_new' => $this->templatesService->getNewTemplatesList()
183
		];
184
185
		return new DataResponse($data, Http::STATUS_OK);
186
	}
187
188
189
	/**
190
	 * @return DataResponse
191
	 */
192
	public function setSettingsAdmin() {
193
		return $this->getSettingsAdmin();
194
	}
195
196
197
	/**
198
	 * @param string $template
199
	 *
200
	 * @return DataResponse
201
	 */
202
	public function addCustomTemplate($template) {
203
204
		$custom = $this->templatesService->getTemplatesList(true);
205
		array_push($custom, $template);
206
		$this->configService->setAppValue(ConfigService::CUSTOM_TEMPLATES, json_encode($custom));
207
208
		return $this->getSettingsAdmin();
209
	}
210
211
	/**
212
	 * @param string $template
213
	 *
214
	 * @return DataResponse
215
	 */
216
	public function removeCustomTemplate($template) {
217
218
		$custom = $this->templatesService->getTemplatesList(true);
219
220
		$k = array_search($template, $custom);
221
		if ($k !== false) {
222
			unset($custom[$k]);
223
		}
224
225
		$this->configService->setAppValue(ConfigService::CUSTOM_TEMPLATES, json_encode($custom));
226
227
		return $this->getSettingsAdmin();
228
	}
229
230
}