|
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\Service; |
|
28
|
|
|
|
|
29
|
|
|
use DirectoryIterator; |
|
30
|
|
|
use Exception; |
|
31
|
|
|
use OCA\CMSPico\Exceptions\TemplateDoesNotExistException; |
|
32
|
|
|
use OCA\CMSPico\Exceptions\WriteAccessException; |
|
33
|
|
|
use OCA\CMSPico\Model\TemplateFile; |
|
34
|
|
|
use OCA\CMSPico\Model\Website; |
|
35
|
|
|
use OCP\Files\Folder; |
|
36
|
|
|
use OCP\IL10N; |
|
37
|
|
|
|
|
38
|
|
|
class TemplatesService { |
|
39
|
|
|
|
|
40
|
|
|
const TEMPLATE_DEFAULT = 'sample_pico'; |
|
41
|
|
|
const TEMPLATES = ['sample_pico', 'other template']; |
|
42
|
|
|
const TEMPLATE_DIR = __DIR__ . '/../../templates/'; |
|
43
|
|
|
|
|
44
|
|
|
/** @var IL10N */ |
|
45
|
|
|
private $l10n; |
|
46
|
|
|
|
|
47
|
|
|
/** @var ConfigService */ |
|
48
|
|
|
private $configService; |
|
49
|
|
|
|
|
50
|
|
|
/** @var MiscService */ |
|
51
|
|
|
private $miscService; |
|
52
|
|
|
|
|
53
|
|
|
/** @var Folder */ |
|
54
|
|
|
private $websiteFolder; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* TemplatesService constructor. |
|
58
|
|
|
* |
|
59
|
|
|
* @param IL10N $l10n |
|
60
|
|
|
* @param ConfigService $configService |
|
61
|
|
|
* @param MiscService $miscService |
|
62
|
|
|
*/ |
|
63
|
|
|
function __construct(IL10N $l10n, ConfigService $configService, MiscService $miscService) { |
|
64
|
|
|
$this->l10n = $l10n; |
|
65
|
|
|
$this->configService = $configService; |
|
66
|
|
|
$this->miscService = $miscService; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param $template |
|
72
|
|
|
* |
|
73
|
|
|
* @throws TemplateDoesNotExistException |
|
74
|
|
|
*/ |
|
75
|
|
|
public function templateHasToExist($template) { |
|
76
|
|
|
if (key_exists($template, self::TEMPLATES)) { |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
throw new TemplateDoesNotExistException($this->l10n->t('Template does not exist')); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getTemplatesList() { |
|
88
|
|
|
$templates = self::TEMPLATES; |
|
89
|
|
|
$customs = json_decode($this->configService->getAppValue('templates'), true); |
|
90
|
|
|
|
|
91
|
|
|
if ($customs !== null) { |
|
92
|
|
|
return $templates = array_merge($templates, $customs); |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $templates; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return array |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getNewTemplatesList() { |
|
103
|
|
|
|
|
104
|
|
|
$newTemplates = []; |
|
105
|
|
|
$currTemplates = $this->getTemplatesList(); |
|
106
|
|
|
$allTemplates = $this->getDirectoriesFromTemplatesDir(); |
|
107
|
|
|
foreach ($allTemplates as $template) { |
|
108
|
|
|
if (!in_array($template, $currTemplates)) { |
|
109
|
|
|
$newTemplates[] = $template; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $newTemplates; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
private function getDirectoriesFromTemplatesDir() { |
|
118
|
|
|
|
|
119
|
|
|
$allTemplates = []; |
|
120
|
|
|
foreach (new DirectoryIterator(self::TEMPLATE_DIR) as $file) { |
|
121
|
|
|
|
|
122
|
|
|
if (!$file->isDir() || substr($file->getFilename(), 0, 1) === '.') { |
|
123
|
|
|
continue; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$allTemplates[] = $file->getFilename(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $allTemplates; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param Website $website |
|
135
|
|
|
*/ |
|
136
|
|
|
public function installTemplates(Website $website) { |
|
137
|
|
|
|
|
138
|
|
|
$files = $this->getSourceFiles(self::TEMPLATE_DIR . $website->getTemplateSource() . '/'); |
|
139
|
|
|
|
|
140
|
|
|
$this->initWebsiteFolder($website); |
|
141
|
|
|
$data = $this->generateData($website); |
|
142
|
|
|
foreach ($files as $file) { |
|
143
|
|
|
$file->applyData($data); |
|
144
|
|
|
$this->generateFile($file, $website); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param $base |
|
151
|
|
|
* @param string $dir |
|
152
|
|
|
* |
|
153
|
|
|
* @return TemplateFile[] |
|
154
|
|
|
*/ |
|
155
|
|
|
private function getSourceFiles($base, $dir = '') { |
|
156
|
|
|
|
|
157
|
|
|
MiscService::endSlash($base); |
|
158
|
|
|
MiscService::endSlash($dir); |
|
159
|
|
|
|
|
160
|
|
|
$files = []; |
|
161
|
|
|
foreach (new DirectoryIterator($base . $dir) as $file) { |
|
162
|
|
|
|
|
163
|
|
|
if (substr($file->getFilename(), 0, 1) === '.') { |
|
164
|
|
|
continue; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
if ($file->isDir()) { |
|
168
|
|
|
$files = array_merge($files, $this->getSourceFiles($base, $dir . $file->getFilename())); |
|
169
|
|
|
continue; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
$files[] = new TemplateFile($base, $dir . $file->getFilename()); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return $files; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param TemplateFile $file |
|
181
|
|
|
* @param Website $website |
|
182
|
|
|
* |
|
183
|
|
|
* @throws WriteAccessException |
|
184
|
|
|
*/ |
|
185
|
|
|
private function generateFile(TemplateFile $file, Website $website) { |
|
186
|
|
|
try { |
|
187
|
|
|
$this->initFolder(pathinfo($website->getPath() . $file->getFileName(), PATHINFO_DIRNAME)); |
|
188
|
|
|
|
|
189
|
|
|
$new = $this->websiteFolder->newFile($website->getPath() . $file->getFileName()); |
|
190
|
|
|
$new->putContent($file->getContent()); |
|
191
|
|
|
} catch (Exception $e) { |
|
192
|
|
|
throw new WriteAccessException( |
|
193
|
|
|
$this->l10n->t('Cannot generate template file in this folder') |
|
194
|
|
|
); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @param Website $website |
|
202
|
|
|
*/ |
|
203
|
|
|
private function initWebsiteFolder(Website $website) { |
|
204
|
|
|
$this->websiteFolder = \OC::$server->getUserFolder($website->getUserId()); |
|
205
|
|
|
$this->initFolder($website->getPath()); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @param Website $website |
|
211
|
|
|
* |
|
212
|
|
|
* @return array |
|
213
|
|
|
*/ |
|
214
|
|
|
private function generateData(Website $website) { |
|
215
|
|
|
return [ |
|
216
|
|
|
'site_title' => $website->getName(), |
|
217
|
|
|
'base_url' => \OC::$WEBROOT . $website->getSite() |
|
218
|
|
|
]; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
private function initFolder($path) { |
|
222
|
|
|
|
|
223
|
|
|
if (!$this->websiteFolder->nodeExists($path)) { |
|
224
|
|
|
$this->websiteFolder->newFolder($path); |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
|
|
229
|
|
|
} |