-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.alternatestyles.php
95 lines (95 loc) · 3.58 KB
/
class.alternatestyles.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
<?php
class AlternateStyles {
var $styleSheet = array(); // @Array: collection of All Style Sheets
var $altStyles = array(); // @Array: collection of Alternate Style Sheets
var $prefStyleSheet = ''; // @String: The name (title) of the Preferred Style Sheet
var $styleSheets = ''; // @String: All the style sheets output in their respective html formats
// @constructor
function AlternateStyles() {
$this->prefStyleSheet = $this->cleanTitle($_GET['css']);
if ( isset($_GET['cssJaxy']) && $_GET['cssJaxy'] == true ) {
$this->setStyleCookie($this->prefStyleSheet);
die();
}
}
// @public
function add($path,$media='',$title='',$alternate=false, $condcom='') {
// first grab all global styles
if ( !$title ) {
$mediaRef = ($media != '' ? 'media="'.$media.'" ' : '');
$styleLink = '<link type="text/css" href="'.$path.'" rel="stylesheet" '.$mediaRef.'/>';
if($condcom) $styleLink = '<!--[if '.$condcom.']>'.$styleLink.'<![endif]-->';
// add it to our style sheet array
array_push($this->styleSheet,$styleLink);
}
// otherwise we're adding the 'preferred' & 'alternates'
else {
$this->determinePreferred($path,$title,$media,$alternate,$condcom);
}
// now grab our preferred
$this->getPreferredStyles();
}
// @private
function getPreferredStyles() {
$this->styleSheets = '';
$totalStyleSheets = count($this->styleSheet);
for ( $i = 0; $i < $totalStyleSheets;$i++ ) {
$this->styleSheets .= $this->styleSheet[$i]."\n\t";
}
}
// @private
function determinePreferred($path,$title,$media='',$alternate=false,$condcom='') {
// still need that media thing no matter what
$mediaRef = ($media != '' ? 'media="'.$media.'" ' : '');
// if $_GET['css'] was set
if ( $this->prefStyleSheet ) {
$this->setStylecookie($this->prefStyleSheet);
if ( $this->prefStyleSheet == $title ) {
$styleLink = '<link type="text/css" href="'.$path.'" rel="stylesheet" '.$mediaRef.'title="'.$title.'" />';
if($condcom) $styleLink = '<!--[if '.$condcom.']>'.$styleLink.'<![endif]-->';
}
else {
$styleLink = '<link type="text/css" href="'.$path.'" rel=" alternate stylesheet" '.$mediaRef.'title="'.$title.'" />';
if($condcom) $styleLink = '<!--[if '.$condcom.']>'.$styleLink.'<![endif]-->';
}
}
// or we could have set a style sheet from before
elseif ( $_COOKIE['PrefStyles'] ) {
// odd bug with prototype, php, and cookies....don't ask
$cookieCheck = $this->fixOurCookie($_COOKIE['PrefStyles']);
if ( $cookieCheck == $title ) {
$styleLink = '<link type="text/css" href="'.$path.'" rel="stylesheet" '.$mediaRef.'title="'.$title.'" />';
if($condcom) $styleLink = '<!--[if '.$condcom.']>'.$styleLink.'<![endif]-->';
}
else {
$styleLink = '<link type="text/css" href="'.$path.'" rel=" alternate stylesheet" '.$mediaRef.'title="'.$title.'" />';
if($condcom) $styleLink = '<!--[if '.$condcom.']>'.$styleLink.'<![endif]-->';
}
}
// probably just our first time here
else {
$styleLink = '<link type="text/css" href="'.$path.'" rel="'.($alternate ? 'alternate ' : '' ).'stylesheet" '.$mediaRef.'title="'.$title.'" />';
if($condcom) $styleLink = '<!--[if '.$condcom.']>'.$styleLink.'<![endif]-->';
}
array_push($this->styleSheet,$styleLink);
}
// @private
function setStyleCookie($value) {
setcookie("PrefStyles", $value, time()+(3600*24*365)); /* expires in 1 year */
}
// @private
function cleanTitle($str) {
return str_replace('_',' ',$str);
}
// @private
function fixOurCookie($str) {
$c = explode('?',$str);
return $c[0];
}
// @public
function drop() {
// watchout! magic may occur
echo $this->styleSheets;
}
}
?>