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\ThemeDoesNotExistException; |
33
|
|
|
use OCA\CMSPico\Exceptions\WriteAccessException; |
34
|
|
|
use OCA\CMSPico\Model\TemplateFile; |
35
|
|
|
use OCA\CMSPico\Model\Website; |
36
|
|
|
use OCP\Files\Folder; |
37
|
|
|
use OCP\IL10N; |
38
|
|
|
|
39
|
|
|
class ThemesService { |
40
|
|
|
|
41
|
|
|
const THEMES = ['default']; |
42
|
|
|
const THEMES_DIR = __DIR__ . '/../../Pico/themes/'; |
43
|
|
|
|
44
|
|
|
/** @var IL10N */ |
45
|
|
|
private $l10n; |
46
|
|
|
|
47
|
|
|
/** @var ConfigService */ |
48
|
|
|
private $configService; |
49
|
|
|
|
50
|
|
|
/** @var FileService */ |
51
|
|
|
private $fileService; |
52
|
|
|
|
53
|
|
|
/** @var MiscService */ |
54
|
|
|
private $miscService; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* ThemesService constructor. |
58
|
|
|
* |
59
|
|
|
* @param IL10N $l10n |
60
|
|
|
* @param ConfigService $configService |
61
|
|
|
* @param FileService $fileService |
62
|
|
|
* @param MiscService $miscService |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
function __construct( |
|
|
|
|
65
|
|
|
IL10N $l10n, ConfigService $configService, FileService $fileService, MiscService $miscService |
66
|
|
|
) { |
67
|
|
|
$this->l10n = $l10n; |
68
|
|
|
$this->configService = $configService; |
69
|
|
|
$this->fileService = $fileService; |
70
|
|
|
$this->miscService = $miscService; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* getThemesList(); |
76
|
|
|
* |
77
|
|
|
* returns all available themes. |
78
|
|
|
* |
79
|
|
|
* @param bool $customOnly |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
public function getThemesList($customOnly = false) { |
|
|
|
|
84
|
|
|
$themes = []; |
85
|
|
|
if ($customOnly !== true) { |
86
|
|
|
$themes = self::THEMES; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$customs = json_decode($this->configService->getAppValue(ConfigService::CUSTOM_THEMES), true); |
90
|
|
|
if ($customs !== null) { |
91
|
|
|
$themes = array_merge($themes, $customs); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $themes; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Check if a theme exist. |
100
|
|
|
* |
101
|
|
|
* @param $theme |
102
|
|
|
* |
103
|
|
|
* @throws ThemeDoesNotExistException |
104
|
|
|
*/ |
105
|
|
|
public function hasToBeAValidTheme($theme) { |
106
|
|
|
$themes = $this->getThemesList(); |
107
|
|
|
if (!in_array($theme, $themes)) { |
108
|
|
|
throw new ThemeDoesNotExistException($this->l10n->t('Theme does not exist')); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* returns theme from the Pico/themes/ dir that are not available yet to users. |
115
|
|
|
* |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
View Code Duplication |
public function getNewThemesList() { |
|
|
|
|
119
|
|
|
|
120
|
|
|
$newThemes = []; |
121
|
|
|
$currThemes = $this->getThemesList(); |
122
|
|
|
$allThemes = $this->fileService->getDirectoriesFromAppDataFolder(PicoService::DIR_THEMES); |
123
|
|
|
|
124
|
|
|
foreach ($allThemes as $theme) { |
125
|
|
|
if (!in_array($theme, $currThemes)) { |
126
|
|
|
$newThemes[] = $theme; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $newThemes; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
} |
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.