-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.php
More file actions
130 lines (107 loc) · 3.46 KB
/
controller.php
File metadata and controls
130 lines (107 loc) · 3.46 KB
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
<?php
namespace CB;
abstract class Controller
{
public function forward($Module, $Action = 'index')
{
$class = "CB_App_".ucfirst($Module);
if (!class_exists($class))
throw new \Exception("Module $Module not found");
# new CB_App_Home()
$controller = new $class();
$controller->Module = ucfirst($Module);
$controller->Section = $Action;
# Call Methods
$controller->init(); # Initializes Database connection, session
$controller->dispatchAction($Action);
$controller->finish();
global $CB;
# Compress HTML
# ob_start();
require_once CB_DIR_UI."{$controller->Skin}/html/{$controller->Layout}.html"; # master layout template file
/*
$html = ob_get_clean();
$htmls = explode("\n", $html);
for ($i = 0; $i < count($htmls); $i++)
{
$htmls[$i] = trim($htmls[$i]);
}
$html = implode("", $htmls);
echo $html;
*/
exit(0);
}
public function finish()
{
if (session_id() != '') session_write_close();
}
}
class FrontController extends Controller
{
public static function createInstance()
{
$instance = new self();
return $instance;
}
public function dispatch()
{
$Module = isset($_GET['module']) ? $_GET['module'] : "home";
$Action = isset($_GET['action']) ? $_GET['action'] : "index";
$this->forward($Module, $Action);
}
}
abstract class ActionController extends Controller
{
protected $Module;
# Mainly $Action values
protected $Section;
# view data
protected $data = [];
public function setTemplateVariable($key, $value)
{
$this->data[$key] = $value;
}
public function __set($name, $value)
{
switch ($name)
{
case 'Module':
$this->Module = $value;
break;
default:
throw new \Exception("No Setter for $name");
break;
}
}
public function __get($name)
{
}
public function dispatchAction($Action)
{
$actionMethod = "act_$Action";
if (!method_exists($this, $actionMethod))
die("Method $actionMethod not found");
$this->Section = $Action;
$this->$actionMethod(); # Can change Section value in action method
$this->displayView();
}
public function displayView()
{
/*
* __CLASS__ will return ActionController - not CB_App_<Module>
*/
global $CB;
$CB['skin'] = $this->Skin;
$CB['html'] = $this->html; # Object of HTML_Document
$CB['layout'] = $this->Layout;
$CB['template_file'] = "{$this->Module}.{$this->Section}.html";
$CB['data'] = $this->data;
$CB['Module'] = $this->Module;
$CB['Section'] = $this->Section;
/*
Actual display called in index.html file
require_once "$skin/html/layout.html";
*/
}
}
?>