]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - 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
1 <?php
2
3 /**
4 * Plugin default_colors.
5 *
6 * Allow users to easily overrides colors of the default theme.
7 */
8
9 use Shaarli\Config\ConfigManager;
10 use Shaarli\Plugin\PluginManager;
11
12 const DEFAULT_COLORS_PLACEHOLDERS = [
13 'DEFAULT_COLORS_MAIN',
14 'DEFAULT_COLORS_BACKGROUND',
15 'DEFAULT_COLORS_DARK_MAIN',
16 ];
17
18 const DEFAULT_COLORS_CSS_FILE = '/default_colors/default_colors.css';
19
20 /**
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 */
27 function default_colors_init($conf)
28 {
29 $params = [];
30 foreach (DEFAULT_COLORS_PLACEHOLDERS as $placeholder) {
31 $value = trim($conf->get('plugins.' . $placeholder, ''));
32 if (strlen($value) > 0) {
33 $params[$placeholder] = $value;
34 }
35 }
36
37 if (empty($params)) {
38 $error = t('Default colors plugin error: ' .
39 'This plugin is active and no custom color is configured.');
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);
46 }
47 }
48
49 /**
50 * When linklist is displayed, include default_colors CSS file.
51 *
52 * @param array $data - header data.
53 *
54 * @return mixed - header data with default_colors CSS file added.
55 */
56 function hook_default_colors_render_includes($data)
57 {
58 $file = PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css';
59 if (file_exists($file)) {
60 $data['css_files'][] = $file ;
61 }
62
63 return $data;
64 }
65
66 /**
67 * Regenerate the custom CSS file with provided settings.
68 *
69 * @param array $params Plugin configuration (CSS rules)
70 */
71 function default_colors_generate_css_file($params): void
72 {
73 $file = PluginManager::$PLUGINS_PATH . '/default_colors/default_colors.css';
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])
78 ? default_colors_format_css_rule($params, $rule) . ';' . PHP_EOL
79 : '';
80 }
81
82 if (! empty($content)) {
83 file_put_contents($file, sprintf($template, $content));
84 }
85 }
86
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 */
95 function default_colors_format_css_rule($data, $parameter)
96 {
97 if (empty($data[$parameter])) {
98 return '';
99 }
100
101 $key = str_replace('DEFAULT_COLORS_', '', $parameter);
102 $key = str_replace('_', '-', strtolower($key)) . '-color';
103 return ' --' . $key . ': ' . $data[$parameter];
104 }
105
106
107 /**
108 * This function is never called, but contains translation calls for GNU gettext extraction.
109 */
110 function 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 }