4 * Plugin default_colors.
6 * Allow users to easily overrides colors of the default theme.
9 use Shaarli\Config\ConfigManager
;
10 use Shaarli\Plugin\PluginManager
;
12 const DEFAULT_COLORS_PLACEHOLDERS
= [
13 'DEFAULT_COLORS_MAIN',
14 'DEFAULT_COLORS_BACKGROUND',
15 'DEFAULT_COLORS_DARK_MAIN',
18 const DEFAULT_COLORS_CSS_FILE
= '/default_colors/default_colors.css';
21 * Display an error if the plugin is active a no color is configured.
23 * @param $conf ConfigManager instance
25 * @return array|null The errors array or null of there is none.
27 function default_colors_init($conf)
30 foreach (DEFAULT_COLORS_PLACEHOLDERS
as $placeholder) {
31 $value = trim($conf->get('plugins.'. $placeholder, ''));
32 if (strlen($value) > 0) {
33 $params[$placeholder] = $value;
38 $error = t('Default colors plugin error: '.
39 'This plugin is active and no custom color is configured.');
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);
50 * When linklist is displayed, include default_colors CSS file.
52 * @param array $data - header data.
54 * @return mixed - header data with default_colors CSS file added.
56 function hook_default_colors_render_includes($data)
58 $file = PluginManager
::$PLUGINS_PATH . '/default_colors/default_colors.css';
59 if (file_exists($file )) {
60 $data['css_files'][] = $file ;
67 * Regenerate the custom CSS file with provided settings.
69 * @param array $params Plugin configuration (CSS rules)
71 function default_colors_generate_css_file($params): void
73 $file = PluginManager
::$PLUGINS_PATH . '/default_colors/default_colors.css';
74 $template = file_get_contents(PluginManager
::$PLUGINS_PATH . '/default_colors/default_colors.css.template');
76 foreach (DEFAULT_COLORS_PLACEHOLDERS
as $rule) {
77 $content .= !empty($params[$rule])
78 ? default_colors_format_css_rule($params, $rule) .';'. PHP_EOL
82 if (! empty($content)) {
83 file_put_contents($file, sprintf($template, $content));
88 * Create a valid CSS rule from parameters settings and plugin parameter.
90 * @param array $data $_POST array
91 * @param string $parameter Plugin parameter name
93 * @return string CSS rules for the provided parameter and its matching value.
95 function default_colors_format_css_rule($data, $parameter)
97 if (empty($data[$parameter])) {
101 $key = str_replace('DEFAULT_COLORS_', '', $parameter);
102 $key = str_replace('_', '-', strtolower($key)) .'-color';
103 return ' --'. $key .': '. $data[$parameter];
108 * This function is never called, but contains translation calls for GNU gettext extraction.
110 function default_colors_translation()
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)');