]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - plugins/default_colors/default_colors.php
Apply PHP Code Beautifier on source code for linter automatic fixes
[github/shaarli/Shaarli.git] / plugins / default_colors / default_colors.php
CommitLineData
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 9use Shaarli\Config\ConfigManager;
a5a0c039
A
10use Shaarli\Plugin\PluginManager;
11
12const DEFAULT_COLORS_PLACEHOLDERS = [
13 'DEFAULT_COLORS_MAIN',
14 'DEFAULT_COLORS_BACKGROUND',
15 'DEFAULT_COLORS_DARK_MAIN',
16];
17
972daa45
A
18const 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 */
27function 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
49/**
972daa45 50 * When linklist is displayed, include default_colors CSS file.
b5507350 51 *
972daa45 52 * @param array $data - header data.
b5507350 53 *
972daa45 54 * @return mixed - header data with default_colors CSS file added.
a5a0c039 55 */
972daa45 56function hook_default_colors_render_includes($data)
a5a0c039
A
57{
58 $file = PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css';
53054b2b 59 if (file_exists($file)) {
972daa45 60 $data['css_files'][] = $file ;
b5507350
A
61 }
62
a5a0c039
A
63 return $data;
64}
65
66/**
972daa45 67 * Regenerate the custom CSS file with provided settings.
a5a0c039 68 *
972daa45 69 * @param array $params Plugin configuration (CSS rules)
a5a0c039 70 */
972daa45 71function default_colors_generate_css_file($params): void
a5a0c039
A
72{
73 $file = PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css';
972daa45
A
74 $template = file_get_contents(PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css.template');
75 $content = '';
76 foreach (DEFAULT_COLORS_PLACEHOLDERS as $rule) {
77 $content .= !empty($params[$rule])
53054b2b 78 ? default_colors_format_css_rule($params, $rule) . ';' . PHP_EOL
972daa45 79 : '';
a5a0c039
A
80 }
81
972daa45
A
82 if (! empty($content)) {
83 file_put_contents($file, sprintf($template, $content));
84 }
a5a0c039
A
85}
86
b5507350
A
87/**
88 * Create a valid CSS rule from parameters settings and plugin parameter.
89 *
90 * @param array $data $_POST array
91 * @param string $parameter Plugin parameter name
92 *
93 * @return string CSS rules for the provided parameter and its matching value.
94 */
95function default_colors_format_css_rule($data, $parameter)
96{
97 if (empty($data[$parameter])) {
98 return '';
99 }
100
101 $key = str_replace('DEFAULT_COLORS_', '', $parameter);
53054b2b
A
102 $key = str_replace('_', '-', strtolower($key)) . '-color';
103 return ' --' . $key . ': ' . $data[$parameter];
b5507350
A
104}
105
106
a5a0c039
A
107/**
108 * This function is never called, but contains translation calls for GNU gettext extraction.
109 */
110function default_colors_translation()
111{
112 // meta
113 t('Override default theme colors. Use any CSS valid color.');
114 t('Main color (navbar green)');
115 t('Background color (light grey)');
116 t('Dark main color (e.g. visited links)');
117}