forked from iRail/iRail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPage.php
170 lines (149 loc) · 5.44 KB
/
Page.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* This is the start of all pages. It uses the template design pattern to
* create the page: it will need a template chosen by the user.
*
* @author pieterc
*/
include_once("api/DataStructs/Request.php");
abstract class Page {
//CONFIG PART
protected $AVAILABLE_TEMPLATES = array("iRail", "iPhone", "jQueryMobile");
protected $AVAILABLE_LANGUAGES = array("EN", "NL", "FR", "DE");
private $globals = array(
"iRail" => "iRail"
);
//DON'T TOUCH
private $template = "iRail";
private $lang = "EN";
//The page content is stored here
protected $loops = array(); // array("loop1" => array(1 => array("stuff" => "cool stuff")))
private $content = "";
private $pageName;
//This is the array that needs to be filled
protected $page;
private $detectLanguageAndTemplate = false;
public function buildPage($pageName) {
if ($this->detectLanguageAndTemplate) {
$this->detectLanguageAndTemplate();
}
$this->pageName = $pageName;
$this->loadTemplate();
$this->generateLoops();
$this->loadContent();
$this->loadGlobalVariables();
$this->loadI18n();
$this->printPage();
}
public function setDetectLanguageAndTemplate($bool) {
$this->detectLanguageAndTemplate = $bool;
}
private function detectLanguageAndTemplate() {
if (isset($_COOKIE["language"])) {
$this->setLanguage($_COOKIE["language"]);
}else if(in_array(strtoupper(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2)), $this->AVAILABLE_LANGUAGES)){
$this->setLanguage(strtoupper($_SERVER['HTTP_ACCEPT_LANGUAGE']));
}else{
$this->setLanguage("EN");
}
if (isset($_GET["lang"])) {
$this->setLanguage($_GET["lang"]);
setcookie("language", $_GET["lang"], time() + 60 * 60 * 24 * 360);
}
if (isset($_COOKIE["output"])) {
$this->setTemplate($_COOKIE["output"]);
}
if (isset($_GET["output"])) {
$this->setTemplate($_GET["output"]);
setcookie("output", $_GET["output"], time() + 60 * 60 * 24 * 360);
}
}
private function setGlobals() {
$this->globals["GoogleAnalytics"] = file_get_contents("includes/googleAnalytics.php");
//hack
if ($this->template == "jQueryMobile") {
$this->globals["footer"] = '<a href="news.php?output=jQueryMobile">{i18n_news}</a>
<a href="feedback.php?output=jQueryMobile">{i18n_feedback}</a>
<a href="about.php?output=jQueryMobile">the iRail team</a>
<a href="http://project.iRail.be" target="_blank">Help us out</a>';
} else {
$this->globals["footer"] = file_get_contents("includes/footer.php");
}
}
public function setTemplate($template) {
if (in_array($template, $this->AVAILABLE_TEMPLATES)) {
$this->template = $template;
}
}
public function setLanguage($lang) {
if (in_array($lang, $this->AVAILABLE_LANGUAGES)) {
$this->lang = $lang;
}
}
private function loadTemplate() {
$tplPath = "templates/" . $this->template . "/" . $this->pageName;
if (file_exists($tplPath)) {
$this->content = file_get_contents($tplPath);
} else {
throw new Exception("Template doesn't exist: " . $tplPath);
}
}
/**
* This algorithm will generate looped content
*/
private function generateLoops(){
foreach($this->loops as $loopname => $loopsArray){
//get the right piece of template:
$pattern = "/{loop_".$loopname."}(.*?){\/loop_". $loopname."}/ism";
preg_match($pattern, $this -> content, $matches);
$template = $matches[1];
$loopcontent = "";
foreach($loopsArray as $loopMap){
$tempstring = $template;
foreach ($loopMap as $tag => $value) {
$tempstring = str_ireplace("{" . $tag . "}", $value, $tempstring);
}
$loopcontent .= $tempstring;
}
//now replace the loopcontent in the template
$this->content = preg_replace($pattern, $loopcontent, $this->content);
}
}
private function loadGlobalVariables() {
$this->setGlobals();
$this->substituteTagsInContent($this->globals);
}
private function loadContent() {
$this->substituteTagsInContent($this->page);
}
private function substituteTagsInContent($tagMap) {
foreach ($tagMap as $tag => $value) {
$this->content = str_ireplace("{" . $tag . "}", $value, $this->content);
}
}
private function loadI18n() {
if ($this->lang == "EN") {
include_once("i18n/EN.php");
} else if ($this->lang == "NL") {
include_once("i18n/NL.php");
} else if ($this->lang == "FR") {
include_once("i18n/FR.php");
} else if ($this->lang == "DE") {
include_once("i18n/DE.php");
}
foreach ($i18n as $tag => $value) {
$this->content = str_ireplace("{i18n_" . $tag . "}", $value, $this->content);
}
}
private function printPage() {
echo $this->content;
}
protected function newRequestInstance(){
$this->detectLanguageAndTemplate();
return new Request("xml", $this->lang);
}
public function getLang() {
return $this->lang;
}
}
?>