Passed
Push — master ( ed87ca...31e7e0 )
by Maxence
02:20
created

SettingsController::addCustomTheme()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 7
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
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\ThemesService;
35
use OCA\CMSPico\Service\WebsitesService;
36
use OCP\AppFramework\Controller;
37
use OCP\AppFramework\Http;
38
use OCP\AppFramework\Http\DataResponse;
39
use OCP\AppFramework\Http\TemplateResponse;
40
use OCP\IRequest;
41
42
class SettingsController extends Controller {
43
44
	/** @var string */
45
	private $userId;
46
47
	/** @var ConfigService */
48
	private $configService;
49
50
	/** @var TemplatesService */
51
	private $templatesService;
52
53
	/** @var ThemesService */
54
	private $themesService;
55
56
	/** @var WebsitesService */
57
	private $websitesService;
58
59
	/** @var MiscService */
60
	private $miscService;
61
62
63
	/**
64
	 * SettingsController constructor.
65
	 *
66
	 * @param IRequest $request
67
	 * @param string $userId
68
	 * @param ConfigService $configService
69
	 * @param TemplatesService $templatesService
70
	 * @param ThemesService $themesService
71
	 * @param WebsitesService $websitesService
72
	 * @param MiscService $miscService
73
	 */
74
	function __construct(
75
		IRequest $request, $userId, ConfigService $configService, TemplatesService $templatesService,
76
		ThemesService $themesService, WebsitesService $websitesService,
77
		MiscService $miscService
78
	) {
79
		parent::__construct(Application::APP_NAME, $request);
80
		$this->userId = $userId;
81
		$this->configService = $configService;
82
		$this->templatesService = $templatesService;
83
		$this->themesService = $themesService;
84
		$this->websitesService = $websitesService;
85
		$this->miscService = $miscService;
86
	}
87
88
89
	/**
90
	 * @NoAdminRequired
91
	 *
92
	 * @param array $data
93
	 *
94
	 * @return DataResponse
95
	 */
96
	public function createPersonalWebsite($data) {
97
98
		try {
99
			$this->websitesService->createWebsite(
100
				$data['name'], $this->userId, $data['website'], $data['path'], $data['template']
101
			);
102
103
			return $this->miscService->success(
104
				[
105
					'name'     => $data['name'],
106
					'websites' => $this->websitesService->getWebsitesFromUser($this->userId)
107
				]
108
			);
109
		} catch (Exception $e) {
110
			return $this->miscService->fail(['name' => $data['name'], 'error' => $e->getMessage()]);
111
		}
112
	}
113
114
115
	/**
116
	 * @NoAdminRequired
117
	 *
118
	 * @param $data
119
	 *
120
	 * @return DataResponse
121
	 */
122
	public function removePersonalWebsite($data) {
123
124
		try {
125
			$this->websitesService->deleteWebsite($data['id'], $this->userId);
126
127
			return $this->miscService->success(
128
				[
129
					'name'     => $data['name'],
130
					'websites' => $this->websitesService->getWebsitesFromUser($this->userId)
131
				]
132
			);
133
		} catch (Exception $e) {
134
			return $this->miscService->fail(['name' => $data['name'], 'error' => $e->getMessage()]);
135
		}
136
	}
137
138
139
	/**
140
	 * @NoAdminRequired
141
	 *
142
	 * @param int $siteId
143
	 * @param string $theme
144
	 *
145
	 * @return DataResponse
146
	 */
147 View Code Duplication
	public function updateWebsiteTheme($siteId, $theme) {
148
149
		try {
150
			$website = $this->websitesService->getWebsiteFromId($siteId);
151
152
			$website->hasToBeOwnedBy($this->userId);
153
			$website->setTheme((string)$theme);
154
155
			$this->themesService->hasToBeAValidTheme($theme);
156
			$this->websitesService->updateWebsite($website);
157
158
			return $this->miscService->success(
159
				['websites' => $this->websitesService->getWebsitesFromUser($this->userId)]
160
			);
161
		} catch (Exception $e) {
162
			return $this->miscService->fail(['error' => $e->getMessage()]);
163
		}
164
	}
165
166
167
	/**
168
	 * @NoAdminRequired
169
	 *
170
	 * @param int $siteId
171
	 * @param string $option
172
	 * @param string $value
173
	 *
174
	 * @return DataResponse
175
	 */
176 View Code Duplication
	public function editPersonalWebsiteOption($siteId, $option, $value) {
177
178
		try {
179
			$website = $this->websitesService->getWebsiteFromId($siteId);
180
181
			$website->hasToBeOwnedBy($this->userId);
182
			$website->setOption((string)$option, (string)$value);
183
184
			$this->websitesService->updateWebsite($website);
185
186
			return $this->miscService->success(
187
				['websites' => $this->websitesService->getWebsitesFromUser($this->userId)]
188
			);
189
		} catch (Exception $e) {
190
			return $this->miscService->fail(['error' => $e->getMessage()]);
191
		}
192
	}
193
194
195
	/**
196
	 * @NoAdminRequired
197
	 *
198
	 * @return DataResponse
199
	 */
200
	public function getPersonalWebsites() {
201
		try {
202
			$websites = $this->websitesService->getWebsitesFromUser($this->userId);
203
204
			return $this->miscService->success(
205
				[
206
					'themes'   => $this->themesService->getThemesList(),
207
					'websites' => $websites
208
				]
209
			);
210
		} catch (Exception $e) {
211
			return $this->miscService->fail(['error' => $e->getMessage()]);
212
		}
213
	}
214
215
216
	/**
217
	 * @return DataResponse
218
	 */
219
	public function getSettingsAdmin() {
220
		$data = [
221
			'templates'     => $this->templatesService->getTemplatesList(true),
222
			'templates_new' => $this->templatesService->getNewTemplatesList(),
223
			'themes'        => $this->themesService->getThemesList(true),
224
			'themes_new'    => $this->themesService->getNewThemesList()
225
		];
226
227
		return new DataResponse($data, Http::STATUS_OK);
228
	}
229
230
231
	/**
232
	 * @return DataResponse
233
	 */
234
	public function setSettingsAdmin() {
235
		return $this->getSettingsAdmin();
236
	}
237
238
239
	/**
240
	 * @param string $template
241
	 *
242
	 * @return DataResponse
243
	 */
244 View Code Duplication
	public function addCustomTemplate($template) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
245
246
		$custom = $this->templatesService->getTemplatesList(true);
247
		array_push($custom, $template);
248
		$this->configService->setAppValue(ConfigService::CUSTOM_TEMPLATES, json_encode($custom));
249
250
		return $this->getSettingsAdmin();
251
	}
252
253
	/**
254
	 * @param string $template
255
	 *
256
	 * @return DataResponse
257
	 */
258 View Code Duplication
	public function removeCustomTemplate($template) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
259
260
		$custom = $this->templatesService->getTemplatesList(true);
261
262
		$k = array_search($template, $custom);
263
		if ($k !== false) {
264
			unset($custom[$k]);
265
		}
266
267
		$this->configService->setAppValue(ConfigService::CUSTOM_TEMPLATES, json_encode($custom));
268
269
		return $this->getSettingsAdmin();
270
	}
271
272
273
	/**
274
	 * @param string $theme
275
	 *
276
	 * @return DataResponse
277
	 */
278 View Code Duplication
	public function addCustomTheme($theme) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
279
280
		$custom = $this->themesService->getThemesList(true);
281
		array_push($custom, $theme);
282
		$this->configService->setAppValue(ConfigService::CUSTOM_THEMES, json_encode($custom));
283
284
		return $this->getSettingsAdmin();
285
	}
286
287
	/**
288
	 * @param string $theme
289
	 *
290
	 * @return DataResponse
291
	 */
292 View Code Duplication
	public function removeCustomTheme($theme) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
293
294
		$custom = $this->themesService->getThemesList(true);
295
296
		$k = array_search($theme, $custom);
297
		if ($k !== false) {
298
			unset($custom[$k]);
299
		}
300
301
		$this->configService->setAppValue(ConfigService::CUSTOM_THEMES, json_encode($custom));
302
303
		return $this->getSettingsAdmin();
304
	}
305
306
	public function nc12personal() {
307
		$data = [
308
			'templates' => $this->templatesService->getTemplatesList()
309
		];
310
311
		return new TemplateResponse(Application::APP_NAME, 'settings.personal', $data, '');
312
	}
313
}