aboutsummaryrefslogtreecommitdiffstats
path: root/view/theme/redbasic/php/config.php
blob: a9a766ad5e3dd054ef607f8f535bd4ae8259040b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php

namespace Zotlabs\Theme;

use ScssPhp\ScssPhp\Compiler;
use ScssPhp\ScssPhp\OutputStyle;

class RedbasicConfig {

	function get_schemas() {
		$files = glob('view/theme/redbasic/schema/*.php');

		$scheme_choices = [];

		if($files) {

			if(in_array('view/theme/redbasic/schema/default.php', $files)) {
				$scheme_choices['---'] = t('Default');
				$scheme_choices['focus'] = t('Focus (Hubzilla default)');
			}
			else {
				$scheme_choices['---'] = t('Focus (Hubzilla default)');
			}

			foreach($files as $file) {
				$f = basename($file, ".php");
				if($f != 'default') {
					$scheme_name = $f;
					$scheme_choices[$f] = $scheme_name;
				}
			}
		}

		return $scheme_choices;
	}

	function get() {
		if(! local_channel()) {
			return;
		}

		$arr['primary_color'] = get_pconfig(local_channel(),'redbasic', 'primary_color');
		$arr['success_color'] = get_pconfig(local_channel(),'redbasic', 'success_color');
		$arr['info_color'] = get_pconfig(local_channel(),'redbasic', 'info_color');
		$arr['warning_color'] = get_pconfig(local_channel(),'redbasic', 'warning_color');
		$arr['danger_color'] = get_pconfig(local_channel(),'redbasic', 'danger_color');
		$arr['dark_mode'] = get_pconfig(local_channel(),'redbasic', 'dark_mode');
		$arr['navbar_dark_mode'] = get_pconfig(local_channel(),'redbasic', 'navbar_dark_mode');
		$arr['narrow_navbar'] = get_pconfig(local_channel(),'redbasic', 'narrow_navbar' );
		$arr['nav_bg'] = get_pconfig(local_channel(),'redbasic', 'nav_bg' );
		$arr['nav_bg_dark'] = get_pconfig(local_channel(),'redbasic', 'nav_bg_dark' );
		$arr['bgcolor'] = get_pconfig(local_channel(),'redbasic', 'background_color' );
		$arr['bgcolor_dark'] = get_pconfig(local_channel(),'redbasic', 'background_color_dark' );
		$arr['background_image'] = get_pconfig(local_channel(),'redbasic', 'background_image' );
		$arr['background_image_dark'] = get_pconfig(local_channel(),'redbasic', 'background_image_dark' );
		$arr['font_size'] = get_pconfig(local_channel(),'redbasic', 'font_size' );
		$arr['radius'] = get_pconfig(local_channel(),'redbasic', 'radius' );
		$arr['converse_width']=get_pconfig(local_channel(),"redbasic","converse_width");
		$arr['top_photo']=get_pconfig(local_channel(),"redbasic","top_photo");
		$arr['reply_photo']=get_pconfig(local_channel(),"redbasic","reply_photo");
		$arr['advanced_theming'] = get_pconfig(local_channel(), 'redbasic', 'advanced_theming');
		return $this->form($arr);
	}

	function post() {
		if(!local_channel()) {
			return;
		}

		if (isset($_POST['redbasic-settings-submit'])) {
			if (isset($_POST['redbasic_primary_color']) || isset($_POST['redbasic_radius'])) {

				$primary_color = '';
				$success_color = '';
				$info_color = '';
				$warning_color = '';
				$danger_color = '';
				$radius = floatval($_POST['redbasic_radius']);

				if (preg_match('/^#([A-Fa-f0-9]{3}){1,2}$/', $_POST['redbasic_primary_color'])) {
					$primary_color = $_POST['redbasic_primary_color'];
				}
				if (preg_match('/^#([A-Fa-f0-9]{3}){1,2}$/', $_POST['redbasic_success_color'])) {
					$success_color = $_POST['redbasic_success_color'];
				}
				if (preg_match('/^#([A-Fa-f0-9]{3}){1,2}$/', $_POST['redbasic_info_color'])) {
					$info_color = $_POST['redbasic_info_color'];
				}
				if (preg_match('/^#([A-Fa-f0-9]{3}){1,2}$/', $_POST['redbasic_warning_color'])) {
					$warning_color = $_POST['redbasic_warning_color'];
				}
				if (preg_match('/^#([A-Fa-f0-9]{3}){1,2}$/', $_POST['redbasic_danger_color'])) {
					$danger_color = $_POST['redbasic_danger_color'];
				}

				if ($primary_color || $success_color || $info_color || $warning_color || $danger_color || $radius) {

					try {
						$cache_dir = 'store/[data]/[scss]/';
						if(!is_dir($cache_dir)) {
							os_mkdir($cache_dir, STORAGE_DEFAULT_PERMISSIONS, true);
						}

						$options = [
							'cacheDir' => $cache_dir,
							'prefix' => 'redbasic_',
							'forceRefresh' => false
						];

						$compiler = new Compiler($options);
						$compiler->setImportPaths('vendor/twbs/bootstrap/scss');
						$compiler->setOutputStyle(OutputStyle::COMPRESSED);

						// Set Variables
						if ($primary_color) {
							$variables['$primary'] = $primary_color;
						}
						if ($success_color) {
							$variables['$success'] = $success_color;
						}
						if ($info_color) {
							$variables['$info'] = $info_color;
						}
						if ($warning_color) {
							$variables['$warning'] = $warning_color;
						}
						if ($danger_color) {
							$variables['$danger'] = $danger_color;
						}
						if ($radius) {
							$variables['$border-radius'] = $radius . 'rem';
							$variables['$border-radius-sm'] = $radius/1.5 . 'rem';
							$variables['$border-radius-lg'] = $radius*1.333 . 'rem';
							$variables['$border-radius-xl'] = $radius*2.666 . 'rem';
							$variables['$border-radius-xxl'] = $radius*5.333 . 'rem';
						}

						// Replace Bootstrap Variables with Customizer Variables
						$compiler->replaceVariables($variables);

						$bs = $compiler->compileString('@import "bootstrap.scss";');

						set_pconfig(local_channel(), 'redbasic', 'bootstrap', $bs->getCss());
					} catch (\Exception $e) {
						logger('scssphp: Unable to compile content');
					}
				}
				else {
					set_pconfig(local_channel(), 'redbasic', 'bootstrap', '');
				}
			}

			set_pconfig(local_channel(), 'redbasic', 'primary_color', $_POST['redbasic_primary_color']);
			set_pconfig(local_channel(), 'redbasic', 'success_color', $_POST['redbasic_success_color']);
			set_pconfig(local_channel(), 'redbasic', 'info_color', $_POST['redbasic_info_color']);
			set_pconfig(local_channel(), 'redbasic', 'warning_color', $_POST['redbasic_warning_color']);
			set_pconfig(local_channel(), 'redbasic', 'danger_color', $_POST['redbasic_danger_color']);

			set_pconfig(local_channel(), 'redbasic', 'narrow_navbar', $_POST['redbasic_narrow_navbar']);
			set_pconfig(local_channel(), 'redbasic', 'navbar_dark_mode', $_POST['redbasic_navbar_dark_mode']);
			set_pconfig(local_channel(), 'redbasic', 'dark_mode', $_POST['redbasic_dark_mode']);
			set_pconfig(local_channel(), 'redbasic', 'nav_bg', $_POST['redbasic_nav_bg']);
			set_pconfig(local_channel(), 'redbasic', 'nav_bg_dark', $_POST['redbasic_nav_bg_dark']);
			set_pconfig(local_channel(), 'redbasic', 'background_color', $_POST['redbasic_background_color']);
			set_pconfig(local_channel(), 'redbasic', 'background_color_dark', $_POST['redbasic_background_color_dark']);
			set_pconfig(local_channel(), 'redbasic', 'background_image', $_POST['redbasic_background_image']);
			set_pconfig(local_channel(), 'redbasic', 'background_image_dark', $_POST['redbasic_background_image_dark']);
			set_pconfig(local_channel(), 'redbasic', 'font_size', $_POST['redbasic_font_size']);
			set_pconfig(local_channel(), 'redbasic', 'radius', $_POST['redbasic_radius']);
			set_pconfig(local_channel(), 'redbasic', 'converse_width', $_POST['redbasic_converse_width']);
			set_pconfig(local_channel(), 'redbasic', 'top_photo', $_POST['redbasic_top_photo']);
			set_pconfig(local_channel(), 'redbasic', 'reply_photo', $_POST['redbasic_reply_photo']);
			set_pconfig(local_channel(), 'redbasic', 'advanced_theming', $_POST['redbasic_advanced_theming']);

			// This is used to refresh the cache
			set_pconfig(local_channel(), 'system', 'style_update', time());
		}
	}

	function form($arr) {

		$expert = false;
		if(get_pconfig(local_channel(), 'redbasic', 'advanced_theming')) {
			$expert = true;
		}

	  	$o = replace_macros(get_markup_template('theme_settings.tpl'), array(
			'$submit' => t('Submit'),
			'$baseurl' => z_root(),
			'$theme' => \App::$channel['channel_theme'],
			'$expert' => $expert,
			'$title' => t("Theme settings"),
			'$dark' => t('Dark style'),
			'$light' => t('Light style'),
			'$common' => t('Common settings'),
			'$primary_color' => array('redbasic_primary_color', t('Primary theme color'), $arr['primary_color']),
			'$success_color' => array('redbasic_success_color', t('Success theme color'), $arr['success_color']),
			'$info_color' => array('redbasic_info_color', t('Info theme color'), $arr['info_color']),
			'$warning_color' => array('redbasic_warning_color', t('Warning theme color'), $arr['warning_color']),
			'$danger_color' => array('redbasic_danger_color', t('Danger theme color'), $arr['danger_color']),
			'$dark_mode' => array('redbasic_dark_mode',t('Default to dark mode'),$arr['dark_mode'], '', array(t('No'),t('Yes'))),
			'$navbar_dark_mode' => array('redbasic_navbar_dark_mode',t('Always use light icons for navbar'),$arr['navbar_dark_mode'], t('Enable this option if you use a dark navbar color in light mode'), array(t('No'),t('Yes'))),
			'$narrow_navbar' => array('redbasic_narrow_navbar',t('Narrow navbar'),$arr['narrow_navbar'], '', array(t('No'),t('Yes'))),
			'$nav_bg' => array('redbasic_nav_bg', t('Navigation bar background color'), $arr['nav_bg']),
			'$nav_bg_dark' => array('redbasic_nav_bg_dark', t('Dark navigation bar background color'), $arr['nav_bg_dark']),
			'$bgcolor' => array('redbasic_background_color', t('Set the background color'), $arr['bgcolor']),
			'$bgcolor_dark' => array('redbasic_background_color_dark', t('Set the dark background color'), $arr['bgcolor_dark']),
			'$background_image' => array('redbasic_background_image', t('Set the background image'), $arr['background_image']),
			'$background_image_dark' => array('redbasic_background_image_dark', t('Set the dark background image'), $arr['background_image_dark']),
			'$font_size' => array('redbasic_font_size', t('Set font-size for the entire application'), $arr['font_size'], t('Examples: 1rem, 100%, 16px')),
			'$radius' => array('redbasic_radius', t('Set radius of corners in rem'), $arr['radius']),
			'$converse_width' => array('redbasic_converse_width',t('Set maximum width of content region in rem'),$arr['converse_width'], t('Leave empty for default width')),
			'$top_photo' => array('redbasic_top_photo', t('Set size of conversation author photo'), $arr['top_photo']),
			'$reply_photo' => array('redbasic_reply_photo', t('Set size of followup author photos'), $arr['reply_photo']),
			'$advanced_theming' => ['redbasic_advanced_theming', t('Show advanced settings'), $arr['advanced_theming'], '', [t('No'), t('Yes')]]
			));

		return $o;
	}

}

function redbasic_theme_admin_enable() {
	// This function is called once when the theme is being enabled by the admin
	// It can be used to register hooks etc.
}

function redbasic_theme_admin_disable() {
	// This function is called once when the theme is being disabled by the admin
	// It can be used to unregister hooks etc.
}