X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=plugins%2Fdefault_colors%2Fdefault_colors.php;h=e1fd5cfbdb19daf547519af2d7f41482c88f7b96;hb=46d3f8162bb7950d3c7fec78ffcc2bdaf349dced;hp=b898814be11690b32cbfe124a61fb43f7b678b59;hpb=a5a0c0399bcfea518330c4bad186da77f89ace6e;p=github%2Fshaarli%2FShaarli.git diff --git a/plugins/default_colors/default_colors.php b/plugins/default_colors/default_colors.php index b898814b..e1fd5cfb 100644 --- a/plugins/default_colors/default_colors.php +++ b/plugins/default_colors/default_colors.php @@ -6,6 +6,7 @@ * Allow users to easily overrides colors of the default theme. */ +use Shaarli\Config\ConfigManager; use Shaarli\Plugin\PluginManager; const DEFAULT_COLORS_PLACEHOLDERS = [ @@ -14,29 +15,43 @@ const DEFAULT_COLORS_PLACEHOLDERS = [ 'DEFAULT_COLORS_DARK_MAIN', ]; +const DEFAULT_COLORS_CSS_FILE = '/default_colors/default_colors.css'; + /** - * When plugin parameters are saved + * Display an error if the plugin is active a no color is configured. + * + * @param $conf ConfigManager instance + * + * @return array|null The errors array or null of there is none. */ -function hook_default_colors_save_plugin_parameters($data) +function default_colors_init($conf) { - $file = PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css'; - $template = file_get_contents(PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css.template'); - $content = ''; - foreach (DEFAULT_COLORS_PLACEHOLDERS as $rule) { - $content .= ! empty($data[$rule]) - ? default_colors_format_css_rule($data, $rule) .';'. PHP_EOL - : ''; + $params = []; + foreach (DEFAULT_COLORS_PLACEHOLDERS as $placeholder) { + $value = trim($conf->get('plugins.'. $placeholder, '')); + if (strlen($value) > 0) { + $params[$placeholder] = $value; + } + } + + if (empty($params)) { + $error = t('Default colors plugin error: '. + 'This plugin is active and no custom color is configured.'); + return [$error]; + } + + // Colors are defined but the custom CSS file does not exist -> generate it + if (!file_exists(PluginManager::$PLUGINS_PATH . DEFAULT_COLORS_CSS_FILE)) { + default_colors_generate_css_file($params); } - file_put_contents($file, sprintf($template, $content)); - return $data; } /** - * When linklist is displayed, include isso CSS file. + * When linklist is displayed, include default_colors CSS file. * * @param array $data - header data. * - * @return mixed - header data with isso CSS file added. + * @return mixed - header data with default_colors CSS file added. */ function hook_default_colors_render_includes($data) { @@ -49,20 +64,54 @@ function hook_default_colors_render_includes($data) } /** - * This function is never called, but contains translation calls for GNU gettext extraction. + * Regenerate the custom CSS file with provided settings. + * + * @param array $params Plugin configuration (CSS rules) */ -function default_colors_translation() +function default_colors_generate_css_file($params): void { - // meta - t('Override default theme colors. Use any CSS valid color.'); - t('Main color (navbar green)'); - t('Background color (light grey)'); - t('Dark main color (e.g. visited links)'); + $file = PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css'; + $template = file_get_contents(PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css.template'); + $content = ''; + foreach (DEFAULT_COLORS_PLACEHOLDERS as $rule) { + $content .= !empty($params[$rule]) + ? default_colors_format_css_rule($params, $rule) .';'. PHP_EOL + : ''; + } + + if (! empty($content)) { + file_put_contents($file, sprintf($template, $content)); + } } +/** + * Create a valid CSS rule from parameters settings and plugin parameter. + * + * @param array $data $_POST array + * @param string $parameter Plugin parameter name + * + * @return string CSS rules for the provided parameter and its matching value. + */ function default_colors_format_css_rule($data, $parameter) { + if (empty($data[$parameter])) { + return ''; + } + $key = str_replace('DEFAULT_COLORS_', '', $parameter); $key = str_replace('_', '-', strtolower($key)) .'-color'; return ' --'. $key .': '. $data[$parameter]; } + + +/** + * This function is never called, but contains translation calls for GNU gettext extraction. + */ +function default_colors_translation() +{ + // meta + t('Override default theme colors. Use any CSS valid color.'); + t('Main color (navbar green)'); + t('Background color (light grey)'); + t('Dark main color (e.g. visited links)'); +}