-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscaffold.php
222 lines (159 loc) · 4.19 KB
/
scaffold.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/**
*
* Scaffold Class
*
* @package Scaffold
* @author Eugeniy Kalinin
* @copyright Copyright (c) 2009, Eugeniy Kalinin
* @license http://pandabytes.info/license
*
*/
require_once 'view.php';
require_once 'pagination.php';
require_once 'db.php';
class Scaffold
{
protected static $config = array();
protected $table;
protected $page = 1;
protected $sort = '';
protected $primary;
protected $id = null;
protected $action = 'list';
public static function LoadConfig($config)
{
if (is_array($config) AND ! empty($config))
{
self::$config = $config;
return true;
}
return false;
}
public static function Config()
{
$args = func_get_args();
$count = count($args);
$config = self::$config;
while (true)
{
$current = current($args);
if (isset($config[$current]))
{
// Base-case
if ($count <= 1) return $config[$current];
// Move to the next array level
elseif (is_array($config[$current]))
{
array_shift($args);
$config = $config[$current];
$count--;
}
else return null;
}
else return null;
}
}
public function __construct($config = array())
{
self::LoadConfig($config);
$this->table = new Scaffold_Db();
if (self::Config('auto_build'))
echo $this->Build();
}
public function Build()
{
if (isset($_GET['action']))
$this->action = $_GET['action'];
if (isset($_GET['sort']))
$this->sort = $_GET['sort'];
if (isset($_GET['page']) && is_numeric($_GET['page']))
$this->page = (int) $_GET['page'];
if (isset($_REQUEST['id']) && is_numeric($_REQUEST['id']))
$this->id = (int) $_REQUEST['id'];
switch ($this->action)
{
case 'add':
case 'edit':
return $this->GetForm();
case 'save':
return $this->Save();
default:
return $this->GetList();
}
}
public static function GetError($message, $exception=null)
{
echo "<div class=\"scaffold-error\">{$message}</div>";
echo '<pre>'; print_r($exception->getMessage()); echo '</pre>';
return false;
}
public static function Url($attr = array(), $appendGetAttrs = false)
{
if ($appendGetAttrs)
foreach (explode('&', $_SERVER['QUERY_STRING']) as $arg)
{
$parts = explode('=', $arg, 2);
if ( ! isset($attr[$parts[0]]) && isset($parts[1]))
$attr[$parts[0]] = urldecode($parts[1]);
}
return '?'.http_build_query($attr, '', '&');
}
public function GetList()
{
$view = new Scaffold_View('list.php');
$view->primary = $this->table->Primary();
$view->title = $this->table->Label();
$view->count = $this->table->Count();
// Set-up pagination
$pagination = new Scaffold_Pagination($view->count, $this->page);
$view->pagination = $pagination->Render();
// Fetch rows
$this->table->Limit($pagination->GetLimit(), $pagination->GetOffset());
$view->rows = $this->table->Order($this->sort)->FetchAll();
$view->fields = $this->table->Fields();
$view->parents = $this->table->FetchParentsList();
return $view->Render();
}
public function GetForm()
{
$view = new Scaffold_View('form.php');
$view->fields = $this->table->Fields();
$view->primary = $this->table->Primary();
$view->title = $this->table->Label();
$data = $this->table->FetchOne($this->id);
// Set default values for the output
if ( ! is_array($data))
foreach ($view->fields as $key => $value)
$data[$key] = $value['default'];
$view->data = $data;
return $view->Render();
}
protected function Save()
{
$status = $this->table->Save($_POST);
if ($status) echo 'Saved';
else echo 'Error!';
/*
$this->view->fields = $this->table->GetFields();
$this->view->primary = $this->primary;
// Do validation and filtering here, if needed
foreach ($this->view->fields as $key => $value)
if (isset($_POST[$key]))
$data[$key] = $_POST[$key];
if (isset($this->id))
{
$where = $this->table->getAdapter()->quoteInto("{$this->primary} = ?", $this->id);
if ( ! $this->table->update($data, $where))
$this->view->error = "Could not save!";
}
else
{
$id = $this->table->insert($data);
if ($id) $this->id = $id;
else $this->view->error = "Could not save!";
}
return $this->GetForm();
*/
}
}