]>
Commit | Line | Data |
---|---|---|
a5a0c039 A |
1 | <?php |
2 | ||
3 | /** | |
4 | * Plugin default_colors. | |
5 | * | |
6 | * Allow users to easily overrides colors of the default theme. | |
7 | */ | |
8 | ||
b5507350 | 9 | use Shaarli\Config\ConfigManager; |
a5a0c039 A |
10 | use Shaarli\Plugin\PluginManager; |
11 | ||
12 | const DEFAULT_COLORS_PLACEHOLDERS = [ | |
13 | 'DEFAULT_COLORS_MAIN', | |
14 | 'DEFAULT_COLORS_BACKGROUND', | |
15 | 'DEFAULT_COLORS_DARK_MAIN', | |
16 | ]; | |
17 | ||
972daa45 A |
18 | const DEFAULT_COLORS_CSS_FILE = '/default_colors/default_colors.css'; |
19 | ||
a5a0c039 | 20 | /** |
b5507350 A |
21 | * Display an error if the plugin is active a no color is configured. |
22 | * | |
23 | * @param $conf ConfigManager instance | |
24 | * | |
25 | * @return array|null The errors array or null of there is none. | |
26 | */ | |
27 | function default_colors_init($conf) | |
28 | { | |
972daa45 | 29 | $params = []; |
b5507350 | 30 | foreach (DEFAULT_COLORS_PLACEHOLDERS as $placeholder) { |
53054b2b | 31 | $value = trim($conf->get('plugins.' . $placeholder, '')); |
972daa45 A |
32 | if (strlen($value) > 0) { |
33 | $params[$placeholder] = $value; | |
34 | } | |
b5507350 A |
35 | } |
36 | ||
37 | if (empty($params)) { | |
53054b2b | 38 | $error = t('Default colors plugin error: ' . |
b5507350 | 39 | 'This plugin is active and no custom color is configured.'); |
972daa45 A |
40 | return [$error]; |
41 | } | |
42 | ||
43 | // Colors are defined but the custom CSS file does not exist -> generate it | |
44 | if (!file_exists(PluginManager::$PLUGINS_PATH . DEFAULT_COLORS_CSS_FILE)) { | |
45 | default_colors_generate_css_file($params); | |
b5507350 A |
46 | } |
47 | } | |
48 | ||
035a002e A |
49 | /** |
50 | * When plugin parameters are saved, we regenerate the custom CSS file with provided settings. | |
51 | * | |
52 | * @param array $data $_POST array | |
53 | * | |
54 | * @return array Updated $_POST array | |
55 | */ | |
56 | function hook_default_colors_save_plugin_parameters($data) | |
57 | { | |
58 | default_colors_generate_css_file($data); | |
59 | ||
60 | return $data; | |
61 | } | |
62 | ||
b5507350 | 63 | /** |
972daa45 | 64 | * When linklist is displayed, include default_colors CSS file. |
b5507350 | 65 | * |
972daa45 | 66 | * @param array $data - header data. |
b5507350 | 67 | * |
972daa45 | 68 | * @return mixed - header data with default_colors CSS file added. |
a5a0c039 | 69 | */ |
972daa45 | 70 | function hook_default_colors_render_includes($data) |
a5a0c039 A |
71 | { |
72 | $file = PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css'; | |
53054b2b | 73 | if (file_exists($file)) { |
972daa45 | 74 | $data['css_files'][] = $file ; |
b5507350 A |
75 | } |
76 | ||
a5a0c039 A |
77 | return $data; |
78 | } | |
79 | ||
80 | /** | |
972daa45 | 81 | * Regenerate the custom CSS file with provided settings. |
a5a0c039 | 82 | * |
972daa45 | 83 | * @param array $params Plugin configuration (CSS rules) |
a5a0c039 | 84 | */ |
972daa45 | 85 | function default_colors_generate_css_file($params): void |
a5a0c039 A |
86 | { |
87 | $file = PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css'; | |
972daa45 A |
88 | $template = file_get_contents(PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css.template'); |
89 | $content = ''; | |
90 | foreach (DEFAULT_COLORS_PLACEHOLDERS as $rule) { | |
91 | $content .= !empty($params[$rule]) | |
53054b2b | 92 | ? default_colors_format_css_rule($params, $rule) . ';' . PHP_EOL |
972daa45 | 93 | : ''; |
a5a0c039 A |
94 | } |
95 | ||
972daa45 A |
96 | if (! empty($content)) { |
97 | file_put_contents($file, sprintf($template, $content)); | |
98 | } | |
a5a0c039 A |
99 | } |
100 | ||
b5507350 A |
101 | /** |
102 | * Create a valid CSS rule from parameters settings and plugin parameter. | |
103 | * | |
104 | * @param array $data $_POST array | |
105 | * @param string $parameter Plugin parameter name | |
106 | * | |
107 | * @return string CSS rules for the provided parameter and its matching value. | |
108 | */ | |
109 | function default_colors_format_css_rule($data, $parameter) | |
110 | { | |
111 | if (empty($data[$parameter])) { | |
112 | return ''; | |
113 | } | |
114 | ||
115 | $key = str_replace('DEFAULT_COLORS_', '', $parameter); | |
53054b2b A |
116 | $key = str_replace('_', '-', strtolower($key)) . '-color'; |
117 | return ' --' . $key . ': ' . $data[$parameter]; | |
b5507350 A |
118 | } |
119 | ||
120 | ||
a5a0c039 A |
121 | /** |
122 | * This function is never called, but contains translation calls for GNU gettext extraction. | |
123 | */ | |
124 | function default_colors_translation() | |
125 | { | |
126 | // meta | |
127 | t('Override default theme colors. Use any CSS valid color.'); | |
128 | t('Main color (navbar green)'); | |
129 | t('Background color (light grey)'); | |
130 | t('Dark main color (e.g. visited links)'); | |
131 | } |