I am trying to refactor a stylesheet switcher but I am running into problems. I have created a snippet to include the logic code and I have made adjustments to the logic code to return a value from functions but I am not sure how to get what I want out of it or if it is even possible with a snippet.
The snippet code is here:
<?php
global $modx;
require_once($modx->config['base_path'] .'assets/snippets/srmSwitch/srmSwitch.inc.php');
$ulSwitch = mSwitch('ul');
$modx->setPlaceholder('ul_switch', '$ulSwitch');
?>
The include code is here:
<?php
//////////////////////////////////////////////////////////////////////////////
// minimal Switch 1.0.1 is © 2005 minimal design //
// but feel free to use and abuse it as much as you want //
// just don't sell it or say you made it - that's all //
// for info/feedback/requests/help/donations ;) go to //
// http://minimaldesign.net/ //
//////////////////////////////////////////////////////////////////////////////
// EDIT if you don't put style sheets in default location
global $modx;
$style_dir = $modx->config['base_path']. 'assets/templates/modxhost/';
// DON'T EDIT AFTER THIS LINE (unless you know what you're doing I guess ;)
$t = $_GET['t']; // in case register_globals in php.ini is Off
$script_name = eregi_replace("[^[:alnum:]+]"," ",basename($_SERVER['PHP_SELF'], ".php"));
$here = basename($_SERVER['PHP_SELF']);
$path2switcher = dirname($_SERVER['PHP_SELF']);
$site_uri = 'http://'.$_SERVER['SERVER_NAME'];
// scans $style_dir for themes and populates $styles
if ($dh03 = opendir($style_dir)) {
while (($style = readdir($dh03)) !== false) {
if($style != "." && $style != ".." && $style != ".DS_Store" && is_file("$style_dir$style")) {
$styles[] = eregi_replace(".css","",$style);
}
}
}
closedir($dh03);
// clean up and arrange $styles array
$total_styles = count($styles);
natcasesort($styles);
$styles_sorted = array_slice($styles, 0, $total_styles);
for ($i=0; $i<$total_styles; $i++) {
$styles_name[$i] = eregi_replace("[^[:alnum:]+]"," ",$styles_sorted[$i]);
}
// sets cookie to remember style between visits if $theme = 'multi'
if (!isset($_COOKIE['current_style'])) { // if cookie ain't set
setcookie('current_style', $styles[0], time()+2592000, $path2switcher);
$current_style = $styles[0];
} else if (isset($t)) { // if cookie set and switching
setcookie('current_style', $t, time()+2592000, $path2switcher);
$current_style = $t;
} else if (!isset($t)) { // if cookie set and ain't switching
$current_style = $_COOKIE[current_style];
}
// style switcher function that goes on index.php
// 3 possible options 'ul' for unordered list (default), 'ol' for ordered list, and 'select' for a drop down menu
function mSwitch($type='ul') {
global $here, $current_style, $total_styles, $styles_sorted, $styles_name;
if ($type == 'ul' || $type == 'ol') {
$output = "<$type>";
for ($i=0; $i<$total_styles; $i++) {
$output .= "<li><a href=\"$here?t=$styles_sorted[$i]\" title=\"switch to $styles_name[$i] CSS style\"".($styles_sorted[$i] == $current_style ? ' class="selected"':'').">$styles_name[$i]</a></li>";
}
$output .= "</$type>";
} else if($type == 'select') {
$output = '<form action=""><p>
<select name="select_style" onchange="window.location=(this.form.select_style[this.form.select_style.selectedIndex].value);">
<option value="">select a style</option>';
for ($i=0; $i<$total_styles; $i++) {
$output .= "<option value=\"$here?t=$styles_sorted[$i]\"".($styles_sorted[$i] == $current_style ? ' selected="selected"':'').">$styles_name[$i]</option>";
}
$output .= '</select></p></form>';
}
print $output;
}
// 'cause it's always nice to show it's Standard compliant ;)
function validation() {
echo("valid <a href=\"http://validator.w3.org/check/referer\" title=\"valid XHTML 1.0 strict\">xhtml</a> | <a href=\"http://jigsaw.w3.org/css-validator/check/referer\" title=\"valid CSS 2.0\">css</a>");
}
?>
What I’d like to achieve is the ability to call the snippet in the template header and have the selector links show in the page (via placeholder or other snippet call and have the dynamic css file name in the head either via placeholder or third snippet.
First I am no PHP programmer. I am a guy who knows some PHP (dangerous, I know).
I wonder if it makes sense to lose the function and just call the $output but as another variable name for the various outputs or using a parameter.
Any thoughts or direction would be good.
I do know that there is a cssSwitcher but it is designed for switching between print and screen. This is to use a high contrast stylesheet for users with visual impairment or screen preference for WAI (a client).
Cheers,
Jay
PS: Don’t be afraid to tell me I’m doing it all wrong