/
sgBaseController.class.php
184 lines (167 loc) · 5.03 KB
/
sgBaseController.class.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* Base Controller - all other controllers should extend this class
*/
class sgBaseController
{
public $matchedRoute;
public $matches;
public $base;
function __construct($matches = array())
{
$this->matches = $matches;
$this->matchedRoute = sgContext::getCurrentRoute();
$this->base = sgContext::getRelativeBaseUrl();
$this->title = $this->guessTitle();
$this->site_name = sgConfiguration::get('settings.site_name');
$this->scripts = array();
$this->styles = array();
$this->js_settings = sgConfiguration::get('settings.js_settings');
}
public function GET()
{
return $this->render();
}
public function getTemplateVars()
{
$templateVars = get_object_vars($this);
$templateVars['context'] = sgContext::getInstance();
$templateVars['configuration'] = sgConfiguration::getInstance();
$templateVars['request'] = array(
'uri' => $_SERVER['REQUEST_URI'],
'method' => $_SERVER['REQUEST_METHOD'],
'ajax' => sgContext::isAjaxRequest(),
'vars' => array('GET' => $_GET, 'POST' => $_POST),
);
$templateVars['js_settings'] = json_encode($this->js_settings);
//str_replace() needed for incorrect slash escaping in php 5.2
$js_settings = str_replace('\/', '/', json_encode($this->js_settings));
$templateVars['js_settings'] =
<<<END
<script type="text/javascript" charset="utf-8">
<!--//--><![CDATA[//><!--
superglue = new Object();
superglue.settings = $js_settings;
//--><!]]>
</script>
END;
return $templateVars;
}
/*
TODO figure out better way to handle titles
*/
public function guessTitle()
{
$title = '';
if (isset($this->matchedRoute['title'])) {
$title = $this->matchedRoute['title'];
}
else if (isset($this->matchedRoute['dynamic_title'])) {
$titlevars = $this->matches;
array_shift($titlevars);
$title = ucwords(vsprintf($this->matchedRoute['dynamic_title'], $titlevars));
}
else if (isset($route['name'])) {
$title = ucwords(str_replace(array('_', '-'), ' ', $this->matchedRoute['name']));
}
return $title;
}
public function throwError($error)
{
if (strpos($error->getMessage(), 'Unable to find template') === 0)
{
return $this->throwErrorCode(404, $error);
}
return $this->throwErrorCode(500, $error);
}
public function throwErrorCode($httpErrorCode, $error = NULL, $header = '')
{
$headers = array(
'404' => 'HTTP/1.0 404 Not Found',
'500' => 'HTTP/1.0 500 Internal Server Error',
);
/*
TODO figure out why I have this here
*/
if (!empty($header))
{
header($header);
}
if (!isset($headers[(string)$httpErrorCode]))
{
header($error);
}
else
{
header($headers[(string)$httpErrorCode]);
}
if (sgConfiguration::get('settings.debug') && is_object($error))
{
exit('<pre>' . $error->getMessage() . "\n" . $error->getTraceAsString() . '</pre>');
}
try
{
$this->loadTemplate($httpErrorCode);
return sgView::getInstance()->render($this->getTemplateVars());
}
catch (Exception $error)
{
// if the error is in the overridden 500 template, just display the core 500 template
if ($httpErrorCode == 500)
{
sgView::getInstance()->getTwig()->getLoader()->setPaths(end(sgView::getInstance()->getTwig()->getLoader()->getPaths()));
}
$this->throwErrorCode(500);
}
}
/*
TODO add real file checks for templates, as oppossed to try catch block on loadTemplate()
*/
public function render($template = NULL)
{
$plugins = sgConfiguration::getInstance()->getPlugins();
foreach ($plugins as $plugin)
{
if (isset($plugin->configuration))
{
sgToolkit::executeMethod($plugin->configuration, 'preRender');
}
}
sgToolkit::executeMethod(sgConfiguration::getInstance(), 'preRender');
sgToolkit::executeMethod($this, 'preRender');
try
{
if ($template)
{
$this->loadTemplate($template);
}
else if (isset($this->matchedRoute['template']))
{
$this->loadTemplate($this->matchedRoute['template']);
}
else
{
$this->loadTemplate($this->matchedRoute['name']);
}
}
catch(Exception $e)
{
return $this->throwError($e);
}
// update route cache with appropriate template
if (sgConfiguration::get('settings.cache_routes'))
{
$method = strtoupper($_SERVER['REQUEST_METHOD']);
$path = sgContext::getCurrentPath();
sgGlue::$cachedRoutes["$method $path"]['template'] = str_replace('.html', '', sgView::getInstance()->getView()->getName());
}
return sgView::getInstance()->render($this->getTemplateVars());
}
public function loadTemplate($name)
{
//set view so that exception can be thrown without setting template_name
sgView::getInstance()->loadTemplate($name . '.html');
$this->template_structure = explode('/', $name);
$this->template_name = end($this->template_structure);
}
}