]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/Themes/Index.php
Refactor deprecated App::internalRedirect() to DI::baseUrl()->redirect()
[friendica.git] / src / Module / Admin / Themes / Index.php
1 <?php
2
3 namespace Friendica\Module\Admin\Themes;
4
5 use Friendica\Core\Config;
6 use Friendica\Core\L10n;
7 use Friendica\Core\Renderer;
8 use Friendica\Core\Theme;
9 use Friendica\DI;
10 use Friendica\Module\BaseAdminModule;
11 use Friendica\Util\Strings;
12
13 class Index extends BaseAdminModule
14 {
15         public static function content(array $parameters = [])
16         {
17                 parent::content($parameters);
18
19                 $a = DI::app();
20
21                 $allowed_themes = Theme::getAllowedList();
22
23                 // reload active themes
24                 if (!empty($_GET['action'])) {
25                         parent::checkFormSecurityTokenRedirectOnError($a->getBaseURL() . '/admin/themes', 'admin_themes', 't');
26
27                         switch ($_GET['action']) {
28                                 case 'reload':
29                                         $allowed_themes = array_unique($allowed_themes);
30                                         foreach ($allowed_themes as $theme) {
31                                                 Theme::uninstall($theme);
32                                                 Theme::install($theme);
33                                         }
34                                         Theme::setAllowedList($allowed_themes);
35
36                                         info('Themes reloaded');
37                                         break;
38
39                                 case 'toggle' :
40                                         $theme = $_GET['addon'] ?? '';
41                                         if ($theme) {
42                                                 $theme = Strings::sanitizeFilePathItem($theme);
43                                                 if (!is_dir("view/theme/$theme")) {
44                                                         notice(L10n::t('Item not found.'));
45                                                         return '';
46                                                 }
47
48                                                 if (in_array($theme, Theme::getAllowedList())) {
49                                                         Theme::uninstall($theme);
50                                                         info(L10n::t('Theme %s disabled.', $theme));
51                                                 } elseif (Theme::install($theme)) {
52                                                         info(L10n::t('Theme %s successfully enabled.', $theme));
53                                                 } else {
54                                                         info(L10n::t('Theme %s failed to install.', $theme));
55                                                 }
56                                         }
57
58                                         break;
59
60                         }
61
62                         DI::baseUrl()->redirect('admin/themes');
63                 }
64
65                 $themes = [];
66                 $files = glob('view/theme/*');
67                 if (is_array($files)) {
68                         foreach ($files as $file) {
69                                 $theme = basename($file);
70
71                                 // Is there a style file?
72                                 $theme_files = glob('view/theme/' . $theme . '/style.*');
73
74                                 // If not then quit
75                                 if (count($theme_files) == 0) {
76                                         continue;
77                                 }
78
79                                 $is_experimental = intval(file_exists($file . '/experimental'));
80                                 $is_supported = 1 - (intval(file_exists($file . '/unsupported')));
81                                 $is_allowed = intval(in_array($theme, $allowed_themes));
82
83                                 if ($is_allowed || $is_supported || Config::get('system', 'show_unsupported_themes')) {
84                                         $themes[] = ['name' => $theme, 'experimental' => $is_experimental, 'supported' => $is_supported, 'allowed' => $is_allowed];
85                                 }
86                         }
87                 }
88
89                 $addons = [];
90                 foreach ($themes as $theme) {
91                         $addons[] = [$theme['name'], (($theme['allowed']) ? 'on' : 'off'), Theme::getInfo($theme['name'])];
92                 }
93
94                 $t = Renderer::getMarkupTemplate('admin/addons/index.tpl');
95                 return Renderer::replaceMacros($t, [
96                         '$title'               => L10n::t('Administration'),
97                         '$page'                => L10n::t('Themes'),
98                         '$submit'              => L10n::t('Save Settings'),
99                         '$reload'              => L10n::t('Reload active themes'),
100                         '$baseurl'             => $a->getBaseURL(true),
101                         '$function'            => 'themes',
102                         '$addons'              => $addons,
103                         '$pcount'              => count($themes),
104                         '$noplugshint'         => L10n::t('No themes found on the system. They should be placed in %1$s', '<code>/view/themes</code>'),
105                         '$experimental'        => L10n::t('[Experimental]'),
106                         '$unsupported'         => L10n::t('[Unsupported]'),
107                         '$form_security_token' => parent::getFormSecurityToken('admin_themes'),
108                 ]);
109         }
110 }