-
Notifications
You must be signed in to change notification settings - Fork 1
/
include-style-switcher.php
353 lines (299 loc) · 12.5 KB
/
include-style-switcher.php
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
/*
PHP script by Mike Challis, www.642weather.com/weather/
Carterlake/AJAX/PHP Template - Color Theme Switcher Plugin PHP Script
Script available at: http://www.642weather.com/weather/scripts.php
Contact Mike: http://www.642weather.com/weather/contact_us.php
Live Demo: http://www.642weather.com/weather/template/
Download: http://www.642weather.com/weather/scripts/css-theme-switcher.zip
Version: 1.17 19-Dec-2008
- adjustment to cookie path setting
Version 1.18 - 10-Feb-2011 - fix duplicate id= in wide/narrow selection HTML
Version 1.19 - 05-Feb-2013 = fix HTML5 validation issues
see changelog.txt for all change history
Support comes from MCHALLIS in this topic at the Weather-Watch.com forum:
http://www.weather-watch.com/smf/index.php/topic,30183.0.html
You are free to use and modify the code
This php code provided "as is", and Long Beach Weather (Michael Challis)
disclaims any and all warranties, whether express or implied, including
(without limitation) any implied warranties of merchantability or
fitness for a particular purpose.
*/
# session_start() must be processed before any other output
# or you might get a warning "headers already sent".
if( !isset( $_SESSION ) ) {
session_start();
}
#################
# begin settings
# ##############
# note the style names in this array are intentionally missing
# the '-wide' and '-narrow' part of the file name, because it is automatically
# replaced by the program based on the user's setting for wide or narrow
# ie: a css file on the server has weather-screen-blue-wide.css
# but the name of it in this array is weather-screen-blue.css
# You can make new styles and add them, just add a new file to the server
# and add a new entry in the array below using the file name conventions noted above
# you can add or remove styles from this array
$CSSstyles = array(
'weather-screen-black.css' => 'Black',
'weather-screen-blue.css' => 'Blue',
'weather-screen-dark.css' => 'Dark',
'weather-screen-fall.css' => 'Fall',
'weather-screen-green.css' => 'Green',
'weather-screen-icetea.css' => 'Ice Tea',
'weather-screen-mocha.css' => 'Mocha',
'weather-screen-orange.css' => 'Orange',
'weather-screen-pastel.css' => 'Pastel',
'weather-screen-purple.css' => 'Purple',
'weather-screen-red.css' => 'Red',
'weather-screen-salmon.css' => 'Salmon',
'weather-screen-silver.css' => 'Silver',
'weather-screen-spring.css' => 'Spring',
'weather-screen-taupe.css' => 'Taupe',
'weather-screen-teal.css' => 'Teal',
);
############
# Use javascript 'OnChange' to post form
############
# if set to true, the style select will not need a "set" button
# if set to false, the style select show a "set" button
# There is a known issue with IE6 and the onchange feature,
# the ajax updates sometimes interfere causing the onchange not to change when changing a selection
# recommended setting: false
$use_onchange_submit = false; # set to true or false
############
# Use cookies to remember settings between site visits
############
# cookies are used to remember the users style settings on each visit to the web site
# you can disable use of cookies, but then the settings would have to be set by
# the user each visit to the web site
# recommended setting: true
$use_cookies = true; # set to true or false
# sets allowable user style select options: (can be overriden with $SITE['CSSsettings_mode'])
$settings_mode = 1; # user can select style and widescreen (show style select and screen width select)
//$settings_mode = 2; # user can select styles only (hide screen width select)
//$settings_mode = 3; # user can select screen width only (hide style select)
#################
# end settings
# ##############
include_once("Settings.php");
include_once("common.php");
global $SITE, $LANGLOOKUP;
if (isset($SITE['CSSsettings_mode']) && $SITE['CSSsettings_mode'] > 0 && $SITE['CSSsettings_mode'] < 4) {
$settings_mode = $SITE['CSSsettings_mode'];
}
function print_css_style_menu ($menubar = 0, $output = 'echo') {
global $SITE, $LANGLOOKUP;
$string = '';
/* ###
# this function makes the form input html so a user can select a style
# it can be called in menubar.php or on index.php
# when using on on menubar.php, you can have it print narrow on a few lines this:
#<div style="margin-left: 5px">
#<?php print_css_style_menu(1); ?>
#</div>
# when using on on index.php, you can have it print horizontal on one line like this:
#<?php print_css_style_menu(0); ?>
*/ ###
global $CSSstyles, $use_onchange_submit, $settings_mode;
$CSSstyle = '';
# was there a style selected from the form input
if (isset($_POST['CSSstyle']) && preg_match("/^[a-z0-9-]{1,50}.css$/i", $_POST['CSSstyle'])) {
$CSSstyle = $_POST['CSSstyle'];
} else if (isset($_SESSION['CSSstyle'])) {
$CSSstyle = $_SESSION['CSSstyle'];
}
# was use widescreen template selected from the form input?
$CSSwscreenOnChecked = '';
$CSSwscreenOffChecked = '';
if (isset($_SESSION['CSSwidescreen']) && $_SESSION['CSSwidescreen'] == 1) {
$CSSwscreenOnChecked = ' checked="checked"';
} else if (isset($_SESSION['CSSwidescreen']) && $_SESSION['CSSwidescreen'] == 0) {
$CSSwscreenOffChecked = ' checked="checked"';
} else {
$CSSwscreenOffChecked = ' checked="checked"';
}
$string .= '<p class="sideBarTitle" style="margin-left: -5px;">'. langtransstr('Style Options') .'</p>';
$string .= '
<form method="post" name="style_select" action="#">
<p>';
if ($settings_mode != 3) { // style settings allowed
$string .= '<label for="CSSstyle">'.langtransstr('Style').':</label>';
if ($menubar) $string .= '<br />';
if($use_onchange_submit == false) {
$string .= '
<select id="CSSstyle" name="CSSstyle">';
}
else {
$string .= '
<select id="CSSstyle" name="CSSstyle" onchange="this.form.submit();">';
}
$CSSstyleSelected = '';
foreach ($CSSstyles as $k => $v) {
if ($CSSstyle == "$k") $CSSstyleSelected = ' selected="selected"';
$string .= '<option value="'.$k.'"'.$CSSstyleSelected.'>'.langtransstr($v).'</option>'."\n";
$CSSstyleSelected = '';
}
$string .= '</select>';
if ($menubar) {
$string .= '<br />';
}else{
$string .= ' ';
if($use_onchange_submit == false) {
$string .= '<input type="submit" name="' . langtransstr('Set') .'" value="' . langtransstr('Set') .'" />';
}
}
if (!$menubar) {
$string .= ' ';
}
}
if ($settings_mode != 2) { // screen width settings allowed
$string .= langtransstr('Widescreen') . ':';
if ($menubar) {
$string .= '<br />';
}else{
$string .= ' ';
}
if($use_onchange_submit == true) {
$string .= '<label for="CSSwidescreenOn">'.langtransstr('On').'</label> <input type="radio" id="CSSwidescreenOn" name="CSSwidescreen" value="1" '.$CSSwscreenOnChecked.' onchange="this.form.submit();" />
| <label for="CSSwidescreenOff">'.langtransstr('Off').'</label> <input type="radio" id="CSSwidescreenOff" name="CSSwidescreen" value="0" '.$CSSwscreenOffChecked.' onchange="this.form.submit();" />';
}
else {
$string .= '<label for="CSSwidescreenOn">'.langtransstr('On').'</label> <input type="radio" id="CSSwidescreenOn" name="CSSwidescreen" value="1" '.$CSSwscreenOnChecked.' />
| <label for="CSSwidescreenOff">'.langtransstr('Off').'</label> <input type="radio" id="CSSwidescreenOff" name="CSSwidescreen" value="0" '.$CSSwscreenOffChecked.' />';
}
}
if ($menubar) {
$string .= '<br />';
}else{
$string .= ' ';
}
if($use_onchange_submit == false) {
$string .= '<input type="submit" name="' . langtransstr('Set') .'" value="' . langtransstr('Set') .'" />';
}
$string .= '</p>
</form>';
# can be printed right where you call it or loaded into a string for later print
# $menubottom .= print_css_style_menu(1,'string');
# print_css_style_menu(1);
if ($output == 'echo') {
echo $string;
} else {
return $string;
}
# uncomment only for debugging
// echo '<p><b>COOKIE INFO:</b><br>';
// print_r($_COOKIE).'</p>';
// echo '<hr>';
// echo '<p><b>SESSION INFO:</b><br>';
// print_r($_SESSION).'</p>';
// echo '<p>Style choice ' .$SITE['CSSscreen'] .'</p>';
}
function validate_style_choice() {
# this function sets the working style based on user's input or not
# input can come from post, cookie, or session
global $CSSstyles, $SITE, $use_cookies, $settings_mode;
$wide_style = 0;
$narrow_style = 0;
$CSSstyle = '';
// incase site admin sets up $SITE['CSSscreenDefault'] wrong
$SITE['CSSscreenDefault'] = str_replace ('-narrow','',$SITE['CSSscreenDefault']);
$SITE['CSSscreenDefault'] = str_replace ('-wide','',$SITE['CSSscreenDefault']);
if ($settings_mode == 2) { // user can select styles only (screen width will be $SITE['CSSscreenDefault'])
if ($SITE['CSSwideOrNarrowDefault'] == 'wide') {
$_SESSION['CSSwidescreen'] = 1;
$wide_style = 1;
}else {
$_SESSION['CSSwidescreen'] = 0;
$narrow_style = 1;
}
}
# is there a CSSstyle selection from the post, cookie, or session?
if (isset($_POST['CSSstyle']) && preg_match("/^[a-z0-9-]{1,50}.css$/i", $_POST['CSSstyle'])) {
$CSSstyle = $_POST['CSSstyle'];
}
else if ($use_cookies == true && isset($_COOKIE['CSSstyle'])) {
$_SESSION['CSSstyle'] = $_COOKIE['CSSstyle'];
$CSSstyle = $_COOKIE['CSSstyle'];
}
else if (isset($_SESSION['CSSstyle'])) {
$CSSstyle = $_SESSION['CSSstyle'];
}
if ($settings_mode == 3) { // widescreen on off only (user cannot select styles)
$_SESSION['CSSstyle'] = $SITE['CSSscreenDefault'];
$CSSstyle = $SITE['CSSscreenDefault'];
}
# was use CSSwidescreen template selected from the form post?
if (isset($_POST['CSSwidescreen']) && $_POST['CSSwidescreen'] == 1) {
if ($use_cookies == true) set_style_cookie ('CSSwidescreen', '1');
$_SESSION['CSSwidescreen'] = 1;
$wide_style = 1;
}
# was use CSSwidescreen template unselected from the form post?
else if (isset($_POST['CSSwidescreen']) && $_POST['CSSwidescreen'] == 0) {
if ($use_cookies == true) set_style_cookie ('CSSwidescreen', '0');
$_SESSION['CSSwidescreen'] = 0;
$narrow_style = 1;
}
// fixed bug (default widescreen not being set)
if (isset($_SESSION['CSSwidescreen']) && $_SESSION['CSSwidescreen'] == 1) {
$wide_style = 1;
}
# was use CSSwidescreen template selected from a stored cookie?
if ($wide_style == 0 && $narrow_style == 0 && $use_cookies == true && isset($_COOKIE['CSSwidescreen']) && $_COOKIE['CSSwidescreen'] == 1) {
if ($use_cookies == true) set_style_cookie ('CSSwidescreen', '1');
$_SESSION['CSSwidescreen'] = 1;
$wide_style = 1;
}
# was use CSSwidescreen template unselected from a stored cookie?
if ($wide_style == 0 && $narrow_style == 0 && $use_cookies == true && isset($_COOKIE['CSSwidescreen']) && $_COOKIE['CSSwidescreen'] == 0) {
if ($use_cookies == true) set_style_cookie ('CSSwidescreen', '0');
$_SESSION['CSSwidescreen'] = 0;
}
# is the seletcted template or one in session one we really have?
if ($CSSstyle != '') {
foreach ($CSSstyles as $k => $v) {
if ($CSSstyle == "$k") {
$_SESSION['CSSstyle'] = $k;
if ($use_cookies == true) {
set_style_cookie ('CSSstyle', $k);
set_style_cookie ('CSSwidescreen' , $wide_style);
}
if (isset($_SESSION['CSSwidescreen']) && $_SESSION['CSSwidescreen'] == 1) {
$CSSstyle = str_replace ('.css','-wide.css',$k);
} else {
$CSSstyle = str_replace ('.css','-narrow.css',$k);
}
return $CSSstyle;
}
}
}else{
# nothing was matched, show default, no set cookie
$_SESSION['CSSstyle'] = $SITE['CSSscreenDefault'];
if ($SITE['CSSwideOrNarrowDefault'] == 'wide') {
$_SESSION['CSSwidescreen'] = 1;
$CSSstyle = str_replace ('.css','-wide.css',$SITE['CSSscreenDefault']);
} else {
$_SESSION['CSSwidescreen'] = 0;
$CSSstyle = str_replace ('.css','-narrow.css',$SITE['CSSscreenDefault']);
}
return $CSSstyle;
}
}
function set_style_cookie ($name, $val) {
global $SITE;
# this function sets a cookie
# set expiration date 6 months ahead
$expire = strtotime("+6 month");
if ( isset($SITE['cookie_path']) ) {
$path = $SITE['cookie_path']; // this can be set in Settings.php
} else {
$scripturlparts = explode('/', $_SERVER['PHP_SELF']);
$scriptfilename = $scripturlparts[count($scripturlparts)-1];
$scripturl = preg_replace("/$scriptfilename$/i", '', $_SERVER['PHP_SELF']);
$path = $scripturl;
}
setcookie("$name",$val,$expire,$path);
}
?>