PHP Style Sheet Switcher

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
essexboyracer
Forum Members
Forum Members
Posts: 85
Joined: Wed Jun 20, 2007 5:40 pm

Re: PHP Style Sheet Switcher

Post 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\">";
parese
New Member
New Member
Posts: 2
Joined: Sun Jun 29, 2008 12:12 pm

Re: PHP Style Sheet Switcher

Post 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
louisk
Forum Members
Forum Members
Posts: 89
Joined: Wed May 30, 2007 7:40 pm

Re: PHP Style Sheet Switcher

Post 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 :)
cyberman

Re: PHP Style Sheet Switcher

Post by cyberman »

Please try TemplateManager - it supports a stylesheet switcher too ;)

http://dev.cmsmadesimple.org/projects/template-mgr
louisk
Forum Members
Forum Members
Posts: 89
Joined: Wed May 30, 2007 7:40 pm

Re: PHP Style Sheet Switcher

Post 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
baptiste
Forum Members
Forum Members
Posts: 15
Joined: Tue Dec 05, 2006 4:58 pm

Re: PHP Style Sheet Switcher

Post 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.
louisk
Forum Members
Forum Members
Posts: 89
Joined: Wed May 30, 2007 7:40 pm

Re: PHP Style Sheet Switcher

Post 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...
cyberman

Re: PHP Style Sheet Switcher

Post by cyberman »

Maybe you should try TemplateManager module ...
baptiste
Forum Members
Forum Members
Posts: 15
Joined: Tue Dec 05, 2006 4:58 pm

Re: PHP Style Sheet Switcher

Post 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.
louisk
Forum Members
Forum Members
Posts: 89
Joined: Wed May 30, 2007 7:40 pm

Re: PHP Style Sheet Switcher

Post 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?
baptiste
Forum Members
Forum Members
Posts: 15
Joined: Tue Dec 05, 2006 4:58 pm

Re: PHP Style Sheet Switcher

Post 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.
Last edited by baptiste on Wed Jan 14, 2009 6:07 pm, edited 1 time in total.
louisk
Forum Members
Forum Members
Posts: 89
Joined: Wed May 30, 2007 7:40 pm

Re: PHP Style Sheet Switcher

Post 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 :)
baptiste
Forum Members
Forum Members
Posts: 15
Joined: Tue Dec 05, 2006 4:58 pm

Re: PHP Style Sheet Switcher

Post by baptiste »

change:

Code: Select all

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

Code: Select all

if(is_array($_COOKIE['sitestyle'])){
louisk
Forum Members
Forum Members
Posts: 89
Joined: Wed May 30, 2007 7:40 pm

Re: PHP Style Sheet Switcher

Post by louisk »

Yeah! it works!! I should have seen that one myself  :-\
THX
artisites
Forum Members
Forum Members
Posts: 12
Joined: Mon Jan 12, 2009 9:43 pm

Re: PHP Style Sheet Switcher

Post by artisites »

Excellent hack, thank you so much!
Post Reply

Return to “Tips and Tricks”