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',
19 * Display an error if the plugin is active a no color is configured.
21 * @param $conf ConfigManager instance
23 * @return array|null The errors array or null of there is none.
25 function default_colors_init($conf)
28 foreach (DEFAULT_COLORS_PLACEHOLDERS
as $placeholder) {
29 $params .= trim($conf->get('plugins.'. $placeholder, ''));
33 $error = t('Default colors plugin error: '.
34 'This plugin is active and no custom color is configured.');
40 * When plugin parameters are saved, we regenerate the custom CSS file with provided settings.
42 * @param array $data $_POST array
44 * @return array Updated $_POST array
46 function hook_default_colors_save_plugin_parameters($data)
48 $file = PluginManager
::$PLUGINS_PATH . '/default_colors/default_colors.css';
49 $template = file_get_contents(PluginManager
::$PLUGINS_PATH . '/default_colors/default_colors.css.template');
51 foreach (DEFAULT_COLORS_PLACEHOLDERS
as $rule) {
52 $content .= ! empty($data[$rule])
53 ? default_colors_format_css_rule($data, $rule) .';'. PHP_EOL
57 if (! empty($content)) {
58 file_put_contents($file, sprintf($template, $content));
65 * When linklist is displayed, include default_colors CSS file.
67 * @param array $data - header data.
69 * @return mixed - header data with default_colors CSS file added.
71 function hook_default_colors_render_includes($data)
73 $file = PluginManager
::$PLUGINS_PATH . '/default_colors/default_colors.css';
74 if (file_exists($file )) {
75 $data['css_files'][] = $file ;
82 * Create a valid CSS rule from parameters settings and plugin parameter.
84 * @param array $data $_POST array
85 * @param string $parameter Plugin parameter name
87 * @return string CSS rules for the provided parameter and its matching value.
89 function default_colors_format_css_rule($data, $parameter)
91 if (empty($data[$parameter])) {
95 $key = str_replace('DEFAULT_COLORS_', '', $parameter);
96 $key = str_replace('_', '-', strtolower($key)) .'-color';
97 return ' --'. $key .': '. $data[$parameter];
102 * This function is never called, but contains translation calls for GNU gettext extraction.
104 function default_colors_translation()
107 t('Override default theme colors. Use any CSS valid color.');
108 t('Main color (navbar green)');
109 t('Background color (light grey)');
110 t('Dark main color (e.g. visited links)');