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 plugin parameters are saved, we regenerate the custom CSS file with provided settings.
52 * @param array $data $_POST array
54 * @return array Updated $_POST array
56 function hook_default_colors_save_plugin_parameters($data)
58 default_colors_generate_css_file($data);
64 * When linklist is displayed, include default_colors CSS file.
66 * @param array $data - header data.
68 * @return mixed - header data with default_colors CSS file added.
70 function hook_default_colors_render_includes($data)
72 $file = PluginManager
::$PLUGINS_PATH . '/default_colors/default_colors.css';
73 if (file_exists($file)) {
74 $data['css_files'][] = $file ;
81 * Regenerate the custom CSS file with provided settings.
83 * @param array $params Plugin configuration (CSS rules)
85 function default_colors_generate_css_file($params): void
87 $file = PluginManager
::$PLUGINS_PATH . '/default_colors/default_colors.css';
88 $template = file_get_contents(PluginManager
::$PLUGINS_PATH . '/default_colors/default_colors.css.template');
90 foreach (DEFAULT_COLORS_PLACEHOLDERS
as $rule) {
91 $content .= !empty($params[$rule])
92 ? default_colors_format_css_rule($params, $rule) . ';' . PHP_EOL
96 if (! empty($content)) {
97 file_put_contents($file, sprintf($template, $content));
102 * Create a valid CSS rule from parameters settings and plugin parameter.
104 * @param array $data $_POST array
105 * @param string $parameter Plugin parameter name
107 * @return string CSS rules for the provided parameter and its matching value.
109 function default_colors_format_css_rule($data, $parameter)
111 if (empty($data[$parameter])) {
115 $key = str_replace('DEFAULT_COLORS_', '', $parameter);
116 $key = str_replace('_', '-', strtolower($key)) . '-color';
117 return ' --' . $key . ': ' . $data[$parameter];
122 * This function is never called, but contains translation calls for GNU gettext extraction.
124 function default_colors_translation()
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)');