-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.php
183 lines (160 loc) · 4.65 KB
/
controller.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
<?php
/**
* Controllers extend this base controller class.
* The core will use it to load the view, and it also
* offers some functionality to your controllers.
*/
class controller
{
/**
* The core
* @var core
*/
public $core = null;
/**
* The view this view will load later on.
* @var view
*/
public $view = null;
public function controller($core)
{
$this->core = $core;
$this->init();
// Only do the following if we've been extended. (ie; not if someone used "new controller()"
if(get_class($this) != 'controller' && get_class($this) != 'app')
{
if(class_exists('appView')) {
$this->view = new appView($this);
}
else
{
$this->view = new view($this);
}
$this->view->init();
$app = (isset($core->uri[0]) ? $core->uri[0] : 'index');
$page = 'index';
if(isset($core->uri[1])) $page = $core->uri[1];
$action = $page.'View';
if(!method_exists($this, $action)) $action='defaultView';
$this->core->pageDetails['view'] = '../app/'.$app.'/'.$app.($page=='index' ? '' : '-'.$page).'View.php';
$this->$action();
}
}
protected function defaultView()
{
// Override me with your own default view (optional).
$page = (isset($this->core->uri[1]) ? $this->core->uri[1] : 'index');
$page .= 'View()';
$this->core->error("You need [ <b>$page</b> ] or [ <b>defaultView()</b> ] in this controller.");
}
public function init()
{
// Override me with your own constructor (optional).
}
/**
* Change which layout this page will display in.
*/
public function setLayout($layout)
{
$this->core->pageDetails['layout'] = $layout;
}
/**
* Set the page's title
*/
public function setTitle($title)
{
$this->core->pageDetails['title'] = $title;
}
/**
* Add another .js file to the head
*/
public function addJS($script, $version='')
{
if($version) $version = "?v=$version";
$this->core->pageDetails['scripts'] .= "<script type='text/javascript' charset='ISO-8859-1' src='/js/{$script}.js{$version}'></script>\n";
}
/**
* Add inline JS to the head
*/
public function addInlineJS($script)
{
$this->core->pageDetails['scripts'] .= '<script type="text/javascript" charset="ISO-8859-1">'.$script.'</script>'."\n";
}
/**
* Add another .css file to the head
*/
public function addCSS($style)
{
$this->core->pageDetails['styles'] .= '<link rel="stylesheet" type="text/css" href="/css/'.$style.'.css" />'."\n";
}
/**
* Returns a newly instantiated table.
* @return table
*/
public function newTable($table)
{
$file = '../app/_tables/'.$table.'Table.php';
if(!file_exists($file)) $this->core->error("Error including table. [<b> $file </b>] doesn't exist.");
require_once($file);
$table = $table.'Table';
if(!class_exists($table)) $this->core->error("Error opening table. [<b> $file </b>] needs [<b> class $table extends table</b>].");
return new $table($this);
}
/**
* Redirect to another page.
* Defaults to the current page (good for clearing POST)
*/
public function redirect($url=false)
{
// echo "Redirecting";
// check nothings been printed yet
if(ob_get_contents()) $this->core->error("Shouldn't redirect. There's output. [$url]");
// Generate new URL
if(!$url) $url = $_SERVER['REQUEST_URI'];
$slash = ($url[0]=='/' ? '' : '/');
$url = 'http://'.$_SERVER['HTTP_HOST'].$slash.$url;
// send them to new url
header("location:$url");
exit;
}
/**
* Redirect back to the previous page (good for clearing POST)
*/
public function redirectBack()
{
// check nothings been printed yet
if(ob_get_contents()) $this->core->error("Shouldn't redirect. There's output.");
$url = $_SERVER['HTTP_REFERER'];
header("location:$url");
exit;
}
/**
* Sets a note to be retrieved by a view's $this->getNotes() function.
*/
public function setNote($note='OK')
{
if(!session_id()) $this->core->error('You must use session_start(), or have "use_session" enabled in config.ini to use setNote().');
$existingNote = isset_val($_SESSION['note']);
if($existingNote) $note = $existingNote.'<br>'.$note;
$_SESSION['note'] = $note;
}
/**
* Sets an error to be retrieved by a view's $this->getNotes() function.
*/
public function setError($error='Error')
{
if(!session_id()) $this->core->error('You must use session_start(), or have "use_session" enabled in config.ini to use setError().');
$existingError = isset_val($_SESSION['error']);
if($existingError) $error = $existingError.'<br>'.$error;
$_SESSION['error'] = $error;
}
/**
* Pass me the path to the view. eg. /index/indexView.php
*
* @param unknown_type $view
*/
public function setView($view='')
{
$this->core->pageDetails['view'] = "../app$view";
}
}