Passed
Push — master ( d122a2...93ca9b )
by Maxence
02:32
created

SettingsController::addTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
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 OCA\CMSPico\AppInfo\Application;
30
use OCA\CMSPico\Service\ConfigService;
31
use OCA\CMSPico\Service\MiscService;
32
use OCA\CMSPico\Service\TemplatesService;
33
use OCA\CMSPico\Service\WebsitesService;
34
use OCP\AppFramework\Controller;
35
use OCP\AppFramework\Http;
36
use OCP\AppFramework\Http\DataResponse;
37
use OCP\IRequest;
38
39
class SettingsController extends Controller {
40
41
	/** @var string */
42
	private $userId;
43
44
	/** @var ConfigService */
45
	private $configService;
46
47
	/** @var TemplatesService */
48
	private $templatesService;
49
50
	/** @var WebsitesService */
51
	private $websitesService;
52
53
	/** @var MiscService */
54
	private $miscService;
55
56
57
	/**
58
	 * NavigationController constructor.
59
	 *
60
	 * @param IRequest $request
61
	 * @param string $userId
62
	 * @param ConfigService $configService
63
	 * @param TemplatesService $templatesService
64
	 * @param WebsitesService $websitesService
65
	 * @param MiscService $miscService
66
	 */
67 View Code Duplication
	function __construct(
68
		IRequest $request, $userId, ConfigService $configService, TemplatesService $templatesService,
69
		WebsitesService $websitesService,
70
		MiscService $miscService
71
	) {
72
		parent::__construct(Application::APP_NAME, $request);
73
		$this->userId = $userId;
74
		$this->configService = $configService;
75
		$this->templatesService = $templatesService;
76
		$this->websitesService = $websitesService;
77
		$this->miscService = $miscService;
78
	}
79
80
81
	/**
82
	 * @NoAdminRequired
83
	 *
84
	 * @param array $data
85
	 *
86
	 * @return DataResponse
87
	 */
88
	public function createPersonalWebsite($data) {
89
90
		try {
91
			$this->websitesService->createWebsite(
92
				$data['name'], $this->userId, $data['website'], $data['path'], $data['template']
93
			);
94
95
			return $this->miscService->success(
96
				[
97
					'name'     => $data['name'],
98
					'websites' => $this->websitesService->getWebsitesFromUser($this->userId)
99
				]
100
			);
101
		} catch (\Exception $e) {
102
			return $this->miscService->fail(['name' => $data['name'], 'error' => $e->getMessage()]);
103
		}
104
	}
105
106
107
	/**
108
	 * @NoAdminRequired
109
	 *
110
	 * @param int $siteId
111
	 * @param string $option
112
	 * @param string $value
113
	 *
114
	 * @return DataResponse
115
	 */
116
	public function editPersonalWebsiteOption($siteId, $option, $value) {
117
118
		try {
119
			$website = $this->websitesService->getWebsiteFromId($siteId);
120
121
			$website->hasToBeOwnedBy($this->userId);
122
			$website->setOption((string)$option, (string)$value);
123
124
			$this->websitesService->updateWebsite($website);
125
126
			return $this->miscService->success(
127
				['websites' => $this->websitesService->getWebsitesFromUser($this->userId)]
128
			);
129
		} catch (\Exception $e) {
130
			return $this->miscService->fail(['error' => $e->getMessage()]);
131
		}
132
	}
133
134
135
	/**
136
	 * @NoAdminRequired
137
	 *
138
	 * @return DataResponse
139
	 */
140
	public function getPersonalWebsites() {
141
		try {
142
			$websites = $this->websitesService->getWebsitesFromUser($this->userId);
143
144
			return $this->miscService->success(['websites' => $websites]);
145
		} catch (\Exception $e) {
146
			return $this->miscService->fail(['error' => $e->getMessage()]);
147
		}
148
	}
149
150
151
	/**
152
	 * @return DataResponse
153
	 */
154
	public function getSettingsAdmin() {
155
		$data = [
156
			'templates_new' => $this->templatesService->getNewTemplatesList()
157
		];
158
159
		return new DataResponse($data, Http::STATUS_OK);
160
	}
161
162
163
	/**
164
	 * @param $data
165
	 *
166
	 * @return DataResponse
167
	 */
168
	public function setSettingsAdmin($data) {
169
		$this->configService->setAppValue(
170
			ConfigService::APP_TEST, $data[ConfigService::APP_TEST]
171
		);
172
173
		return $this->getSettingsAdmin();
174
	}
175
176
177
	/**
178
	 * @param $data
179
	 *
180
	 * @return DataResponse
181
	 */
182
	public function addTemplate($data) {
183
184
		return $this->getSettingsAdmin();
185
	}
186
187
}