PHP Style Sheet Switcher

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
JohnnyB
Dev Team Member
Dev Team Member
Posts: 731
Joined: Tue Nov 21, 2006 5:05 pm

PHP Style Sheet Switcher

Post by JohnnyB »

After trying the style switcher module (doesn't work in 1.0.2) and reading many posts to find a CMS MS based style switcher solution - search forum for "style switcher" -
I decided to use an existing php solution by Rob Ballou.

I made a User Defined Tag and uploaded the file called switcher.php.  Changed a few style sheet links to include files that I uploaded to a directory outside of CMS MS stylesheet and template management functionality and all is good.  :D

Here's how you can do this too.

First upload your "Alternate" stylesheets into a new directory inside your CMS MS directory.  For example, I created a directory called "custom" that holds my custom CSS and JavaScript files.  Next, create links to your alternate CSS links inside your template(s) .  One of these links will include a User Defined Tag.  There's more information and the User Defined Tag code provided in the included file.

Now, modify the switcher.php file as instructed in the comment section and upload to your CMS MS root directory. 

Here's the file.  Note that the a User Defined Tag must be created.  For a working style switcher, you will have:
  • the switcher.php file uploaded to your CMS MS directory
  • the setstyle tag created via the CMS Made Simple Admin Panel
  • your alternate stylesheets uploaded to your a directory of your choice
  • links to your alternate style sheets inside each of your template sections
  • links inside your pages to provide visitors the option to switch styles
copy and paste this into a file called "switcher.php"

Code: Select all

<?php /* 
###########CREDITS##############
Script created by:  By Rob Ballou. 
October 14th, 2002
(Last updated: August 06, 2003 13:24)

Original website article: 
Another PHP Styleswitcher
http://www.contrastsweb.com/switcher/v1/

Thanks extended to Rob Ballou!


###########USAGE#############
###Create a CMS Made Simple User Defined Tag called, "Setstyle."
	#Paste the following code into your User Defined Tag:
	
	//Start setstyle
	if(isset($_COOKIE['sitestyle'])){ // Use the saved style
	  print trim($_COOKIE['sitestyle']);
	}
	else { // There is not a cookie set, so use this style
	 print "default";
	}
	//end setstyle
	
	#Change line 5 above, print "normal"; to reflect the name of your "default" stylesheet.

###Example Stylesheet link in the <head></head>:
	#Place as the last entry inside the head of your web document any number of "Alternate Stylesheets." For Example,
	<link rel="alternate stylesheet" type="text/css" href="larger.css" title="larger">
	<link rel="alternate stylesheet" type="text/css" href="default.css" title="default">

	#Followed by a "Persistent Stylesheet" with a CMS Made Simple User Defined Tag
	called, "Setstyle."
	<link type="text/css" rel="stylesheet" href="custom/{setstyle}.css" media="screen">

###Next, this script has to be modified to include the names of your "Alternate Stylesheets" and your website address. Modify Line Numbers: 55, 73, and 92.

###Next, upload this file to the root of your CMS Made Simple site or your CMS Made Simple directory.

###Example of a switcher link in an html document: 
	<a href="/switcher.php?set=larger">Switch to Larger Text</a>
	<a href="/switcher.php?set=default">Switch to Default Text</a>


###########NOTES#############
For more information, visit the author's website at, http://www.contrastsweb.com/switcher/v1/
Or, visit the CMS Made Simple forum at, 


###########Start of Switcher Script############## */

// This array lists the "acceptable" styles
$accept = array('default', 'larger');
// add more styles separated by commas

// Get style from a query string (e.g. from a link),
// or from a form.
if(isset($_REQUEST['set'])){
	$style = trim(strip_tags($_REQUEST['set']));
}
else if(isset($_POST['set'])){
	$style = trim(strip_tags($_POST['set']));
}
else {
    // Unknown request
    $style = false;
}

// Check if the requested stylesheet is "acceptable"
if(($style !== false) && (in_array($style, $accept))){
	setcookie("sitestyle", $style, time()+31536000, '/', 'YOURDOMAIN.com', '0');
}
if(isset($_REQUEST['ref']) || (isset($_POST['ref']))){
    if(isset($_REQUEST['ref'])){ 
        $ref = $_REQUEST['ref'];
    }
    else {
        $ref = $_POST['ref'];
    }
    header("Location: $ref");
    exit;
}
else if(isset($_SERVER['HTTP_REFERER'])){
	// Send the user back to the refering page
	header("Location: ". $_SERVER['HTTP_REFERER']);
	exit;
}
else {
	// No HTTP referrer, send them back to the home page
	header("Location: http://YOURDOMAIN.com");
	exit;
}
?>
A few things to consider - you may have to modify your config.php and your site to create page file names that end with .php.  I haven't tested this using the default set-up (without mod rewrite) or with file names like .html.

Also, the stylesheet links have to be the last thing before the closing tag so as not to conflict with the default, persistent styles.




I look forward to hearing any successes and questions.  I'm using this on my first CMS MS site at optometricassociatesonlineDOTcom/new  (I wrote the url like this so it isn't picked up by SE's - the new site will move to the root url in the next few days...) - Take a look at the source to see the style sheet links and etc...

-John B.




[gelöscht durch Administrator]
Last edited by JohnnyB on Fri Dec 15, 2006 1:16 am, edited 1 time in total.
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo

--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12711
Joined: Thu Mar 09, 2006 5:32 am

Re: PHP Style Sheet Switcher

Post by Dr.CSS »

In firefox the "order contacts on line" is under the rest of the menu....
User avatar
Elijah Lofgren
Power Poster
Power Poster
Posts: 811
Joined: Mon Apr 24, 2006 1:01 am

Re: PHP Style Sheet Switcher

Post by Elijah Lofgren »

Cool, I've added a link to this post in the Wiki: http://wiki.cmsmadesimple.org/index.php ... _tags_here
Note: I don't have time to take on any more projects. I'm quite busy. I may be too busy to reply to emails or messages. Thanks for your understanding. :)
JohnnyB
Dev Team Member
Dev Team Member
Posts: 731
Joined: Tue Nov 21, 2006 5:05 pm

Re: PHP Style Sheet Switcher

Post by JohnnyB »

@Elijah:
Great, Thanks!

@mark:  I'll check that out Thanks!
- what version FF are you using?  I haven't tested in anything less than 2.0 since upgrading. 
I'll be testing it out on various platforms this weekend before the launch.
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo

--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12711
Joined: Thu Mar 09, 2006 5:32 am

Re: PHP Style Sheet Switcher

Post by Dr.CSS »

Firefox 2.0
JohnnyB
Dev Team Member
Dev Team Member
Posts: 731
Joined: Tue Nov 21, 2006 5:05 pm

Re: PHP Style Sheet Switcher

Post by JohnnyB »

Thanks.  ;D
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo

--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
JohnnyB
Dev Team Member
Dev Team Member
Posts: 731
Joined: Tue Nov 21, 2006 5:05 pm

Re: PHP Style Sheet Switcher

Post by JohnnyB »

Mark - I know this is a bit off topic... Are you using FF 2.0 on a mac?  It seems that I'm having the same issue on all several mac os's/browsers.

thanks
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo

--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12711
Joined: Thu Mar 09, 2006 5:32 am

Re: PHP Style Sheet Switcher

Post by Dr.CSS »

Good old XP...  ;)
baptiste
Forum Members
Forum Members
Posts: 15
Joined: Tue Dec 05, 2006 4:58 pm

Re: PHP Style Sheet Switcher

Post by baptiste »

Hi,

I've made a few adjustments to the above code so that:
  • You can apply more than one selected stylesheet (I use larger text plus an alternative background).
  • You can use the CMSMS admin panel to administrate the stylesheets (the 'custom' directory is no longer needed, avoids path probs etc).
There are just 3 places that you need to make changes to the code below :
In '1)' below, change the menu 'set' values to your values, you don't need to change 'default', this option just deletes to cookie, but the other values need to be the names of your stylesheets.
In '3)' below change the acceptable styles to match those in '1)' above.
In '3)' below change the server to be your server name.

And add stylesheets to match your options!

I've probably forgotton something really basic, but here's the stuff :

1) Create a user defined tag to write the menu to the page:
   - Extensions » User Defined Tags » Create User Defined Tag: accessibility_menu
   - Add to your template as '{accessibility_menu}'.

/***********************************************
* Writes the accessibility menu
************************************************/
echo "";
echo "Accessibility Options

";
echo "Default Text/Colour";/*ADD YOUR OPTIONS HERE*/
echo "Larger Text";
echo "Colour option 1";
echo "Colour option 2";
echo "";

2) Create a user defined tag to write the selected css links in the page:
   - Extensions » User Defined Tags » Create User Defined Tag: set_css
   - Add to the 'head' section your template as '{set_css}'.

/***********************************************
* reads a cookie to get values for the accessibility options
* these are stored in an array in the cookie as:
* sitestyle('default', 'larger', 'colour_option1') etc...
************************************************/
if(isset($_COOKIE['sitestyle'])){
    foreach($_COOKIE['sitestyle'] as $style) {
        $css_tag = "";

        echo $css_tag;
    }
}

3) Create a file called 'switcher.php' in your website document root and paste the following in to it:


/************************ End of Code *************************/



Questions welcome.


[gelöscht durch Administrator]
Last edited by baptiste on Thu Dec 21, 2006 1:20 pm, edited 1 time in total.
baptiste
Forum Members
Forum Members
Posts: 15
Joined: Tue Dec 05, 2006 4:58 pm

Re: PHP Style Sheet Switcher

Post by baptiste »

Hi,

Please detail the exact problem with safari.

I haven't got a mac to test it on, but as it's largely server-side code the only thing that I can think of is the cookie or the way the css tags are written.

If it's cookies maybe the cookie array isn't working on the mac? You'd have to store the selected styles in separate cookies maybe?

If it's the tag then CMSMS stylesheets wouldn't work at all in safari, so that's easy to check.
Last edited by baptiste on Thu Dec 21, 2006 5:12 pm, edited 1 time in total.
JohnnyB
Dev Team Member
Dev Team Member
Posts: 731
Joined: Tue Nov 21, 2006 5:05 pm

Re: PHP Style Sheet Switcher

Post by JohnnyB »

baptiste,

Good Work!  I just needed one alternate stylesheet to satisfy the needs of my latest project.  The original author created a solution for multiple styles here:  http://www.contrastsweb.com/switcher/v2/&nbsp; ...maybe this can be helpful.
Last edited by JohnnyB on Thu Dec 21, 2006 10:27 pm, edited 1 time in total.
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo

--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
User avatar
robsta
Forum Members
Forum Members
Posts: 131
Joined: Thu Oct 20, 2005 11:39 pm

Smarty switching

Post by robsta »

I use this method in smarty to change the colours and even the styling of my sites.

Essentially all I'm doing is loading another stylesheet AFTER the main CMSMS styles have loaded, thereby overriding any colours etc

It uses the heirarchy manager to get the path of the page and then I just check for the appropriate section using a strstr... the real benefit of this is that I can apply the style to an entire section on the fly. It works for sections and particular pages.

You just create a User Defined Tag, paste the following code inside and then add a {$colorstyles} tag in your template AFTER the {stylesheet}

I also use it to set the colours for the flash movies in each section (on some sites I even have the movies change out).

Most of this is attributable to the UDT pages in the manuals...

Probably more importantly the whole lot can be done without bashing stuff into the index.php file, and it doesn't make any db calls.

Code: Select all

global $smarty;
global $gCms;
$manager =& $gCms->GetHierarchyManager();

$result = "none";
$stylesheet = "none";

$thisPage = $gCms->variables['content_id'];
$currentNode = &$manager->sureGetNodeById($thisPage);

while( isset($currentNode) && $currentNode->getLevel() >= 0 )
{
    $currentContent =& $currentNode->getContent();
    $result = $currentContent->Alias();
    $currentNode =& $currentNode->getParentNode();
}

switch ($result) {

      // To customise it for your use
      // Change the string to find in $result
      // Change the $stylesheet colour to match to the stylesheets created in CMSMS as well

      case strstr($result, "commercial") :
            $stylesheet="ColourPink"; 
      break;

      case strstr($result, "residential") :
            $stylesheet="ColourGreen";
      break;

      case strstr($result, "office"):
            $stylesheet="ColourOrange"; 
      break;

      case strstr($result, "hotel") :
            $stylesheet="ColourPurple";
      break;

      default:
            $stylesheet="ColourBlue";

}

if ($stylesheet != "none") {
	$colstyles='<link rel="stylesheet" type="text/css" media="screen" href="stylesheet.php?name='.$stylesheet.'&mediatype=screen" />';
    $smarty->assign('colorstyles',$colstyles);
}
cyberman

Re: Smarty switching

Post by cyberman »

robsta wrote: Most of this is attributable to the UDT pages in the manuals...
Thanks for adding there  :).
JohnnyB
Dev Team Member
Dev Team Member
Posts: 731
Joined: Tue Nov 21, 2006 5:05 pm

Re: PHP Style Sheet Switcher

Post by JohnnyB »

Great information - thanks for adding to this post.

Curious - so you have a link to show an example of this in action?  Also, does this create a stylesheet link inside the body tag or does it stay in the header?
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo

--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
User avatar
robsta
Forum Members
Forum Members
Posts: 131
Joined: Thu Oct 20, 2005 11:39 pm

Re: PHP Style Sheet Switcher

Post by robsta »

http://www.landequity.co.nz&nbsp; uses my UDT throughout..
Post Reply

Return to “Tips and Tricks”