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