Page 2 of 3

Re: PHP Style Sheet Switcher

Posted: Mon Sep 08, 2008 8:54 am
by essexboyracer
Update: Along the way of CMSMS upgrades, the way it calls stylesheets changed from name to css_id. To use this great UDT you will need to amend the code a bit...

1. In switcher.php, edit the following line, instead of entering stylesheet names, put in their id number, which you can discover by reading the link in stylesheets admin area:

Code: Select all

// This array lists the "acceptable" styles
$accept = array('default','large', 'xlarge', 'xxlarge'); /*ADD YOUR OPTIONS HERE*/

Code: Select all

// Change to stylesheet id numbers
$accept = array('default','45', '42', '44'); /*ADD YOUR OPTIONS HERE*/
2. Change your {accessibility_menu} to these id numbers. I left the default named as 'defualt'

3. Change the following line in {set_css}:

     

Code: Select all

$css_tag .= "name=$style\" media=\"screen\">";
TO

     

Code: Select all

 $css_tag .= "cssid=$style\" media=\"screen\">";

Re: PHP Style Sheet Switcher

Posted: Thu Sep 25, 2008 8:34 am
by parese
Thank you for the great php-stylesswitcher. There´s only one problem occuring when i use Firefox (3.x.): klicking the links to the alternate stylesheets works fantastic without mistakes. But when i klick the "default" button i get following warning:
Warning:  Invalid argument supplied for foreach() in .../lib/content.functions.php(854) : eval()'d code on line 2
This problem ain´t occur in Internet Explorer or Opera. I have Windows XP.
You hav a solution?
sorry for my bad english.

Thomas

Re: PHP Style Sheet Switcher

Posted: Thu Dec 18, 2008 6:19 pm
by louisk
I have the same problem.
When you click the link to the style sheet thats already active, the error occurs.
Does any one know how to fix this beautiful peace of script :)

Re: PHP Style Sheet Switcher

Posted: Fri Dec 19, 2008 5:54 am
by cyberman
Please try TemplateManager - it supports a stylesheet switcher too ;)

http://dev.cmsmadesimple.org/projects/template-mgr

Re: PHP Style Sheet Switcher

Posted: Wed Jan 14, 2009 12:27 pm
by louisk
When activating the stylesheet that is already active, I get this error:

Code: Select all

Warning: Invalid argument supplied for foreach() in /home/louisk/domains/klibanskydesigns.com/public_html/switcher.php on line 23

Warning: Cannot modify header information - headers already sent by (output started at /home/louisk/domains/klibanskydesigns.com/public_html/switcher.php:23) in /home/louisk/domains/klibanskydesigns.com/public_html/switcher.php on line 43
Does anyone know how to solve this issue?

Thanks

Re: PHP Style Sheet Switcher

Posted: Wed Jan 14, 2009 2:02 pm
by baptiste
Hi all,

I don't currently have access to a CMSMS installation, so this fix hasn't been tested at all.

The problem seems to be that if the default style is selected when the page is already using the default style, you get the error described by louisk and others.

So change this bit of code in switcher.php:

Code: Select all

// Check if the requested stylesheet is "acceptable"
if(($style !== false) && (in_array($style, $accept))){
    if ($style == 'default') {
        foreach($_COOKIE['sitestyle'] as $style) {
            setcookie("sitestyle[$style]", "", time()-86400, '/');
        }
    } else {
        setcookie("sitestyle[$style]", $style, time()+86400, '/');//day
    }
}
To this:

Code: Select all


// Check if the requested stylesheet is "acceptable"
if(($style !== false) && (in_array($style, $accept))){
    if ($style != 'default') {
        setcookie("sitestyle[$style]", $style, time()+86400, '/');//day
    }
}
Like I said it's untested, so please someone let me know if this fix works, if not I have another idea.

Re: PHP Style Sheet Switcher

Posted: Wed Jan 14, 2009 2:12 pm
by louisk
Hey Baptiste,
Thanks for your reply!! :)

The change doesn't fix the problem. When loading the site, the default css loads, when clicking the second css file, the site switches to that one (so far so good). However I can;t switch back the the default. The error I don't get anymore.
Hmmm...

Re: PHP Style Sheet Switcher

Posted: Wed Jan 14, 2009 2:48 pm
by cyberman
Maybe you should try TemplateManager module ...

Re: PHP Style Sheet Switcher

Posted: Wed Jan 14, 2009 2:56 pm
by baptiste
hmmm, you describe a different response to the one I get when visiting your site:

I only get this error if I select the default template if I am viewing the page with the default template already set. I can swap between with no problem.

It seems to be a problem with deleting the cookie, what browser are you using?

I use FF 2.0.0.20 and IE 7 the behaviour is the same.

Re: PHP Style Sheet Switcher

Posted: Wed Jan 14, 2009 4:00 pm
by louisk
I don't have any problem switching between them, the switcher works like a charm, except...

Your are right: I only get this error when clicking on the default, when default is already active.

I am using FF3.05 on OSX. I get the same issue in Safari and FF2.0.0.19 and IE6,7,8 on Win. What could the issue be?

Re: PHP Style Sheet Switcher

Posted: Wed Jan 14, 2009 4:20 pm
by baptiste

Code: Select all

// Check if the requested stylesheet is "acceptable"
if(($style !== false) && (in_array($style, $accept))){
    if ($style == 'default') {//loop through style cookies and delete them
    	if(is_array($_COOKIE['sitestyle']) {
	    foreach($_COOKIE['sitestyle'] as $style) {
	        setcookie("sitestyle[$style]", "", time()-86400, '/');
	    }
        }
    } else {
        setcookie("sitestyle[$style]", $style, time()+86400, '/');//day
    }
}
As before put the above code in place of :

Code: Select all

// Check if the requested stylesheet is "acceptable"
if(($style !== false) && (in_array($style, $accept))){
    if ($style == 'default') {
        foreach($_COOKIE['sitestyle'] as $style) {
            setcookie("sitestyle[$style]", "", time()-86400, '/');
        }
    } else {
        setcookie("sitestyle[$style]", $style, time()+86400, '/');//day
    }
}
The problem is that when the default template is chosen all style cookies are unset. So, if you try to select default again, it will again try to unset all style cookies by looping through them: (

Code: Select all

 foreach($_COOKIE['sitestyle'] as $style)  
), but there aren't any! They've already been deleted, so the code fails as it tries looping through a nonexistent array.

Again, I haven't tested this, please let me know if it works.

Re: PHP Style Sheet Switcher

Posted: Fri Jan 16, 2009 1:22 pm
by louisk
I tested it, and there is an error:

Parse error: syntax error, unexpected '{' in /home/louisk/domains/klibanskydesigns.com/public_html/switcher.php on line 23

It's hanging on the '{' after "if(is_array($_COOKIE['sitestyle']) {"

We're nearly there :)

Re: PHP Style Sheet Switcher

Posted: Fri Jan 16, 2009 2:21 pm
by baptiste
change:

Code: Select all

if(is_array($_COOKIE['sitestyle']) {
to:

Code: Select all

if(is_array($_COOKIE['sitestyle'])){

Re: PHP Style Sheet Switcher

Posted: Fri Jan 16, 2009 2:41 pm
by louisk
Yeah! it works!! I should have seen that one myself  :-\
THX

Re: PHP Style Sheet Switcher

Posted: Sun Feb 22, 2009 2:22 am
by artisites
Excellent hack, thank you so much!