This repository has been archived by the owner on Feb 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
table.php
253 lines (208 loc) · 4.91 KB
/
table.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php namespace Squi;
class Table extends HTML_Element {
// Array of Table_Column objects
public $columns = array();
// Row data
public $rows = array();
// Layout to use (View instance)
public $layout = 'horizontal';
// Shell row on which common row attributes
// and default values are stored (instance of Table_Row)
public $row;
// Header row (instance of Table_Row)
public $header;
// Instance of Table_Rowdata to contain column names
public $header_data;
// Footer row (instance of Table_Row)
public $footer;
// Footer data (instance of Table_Rowdata)
public $footer_data;
// Message to show when there are no rows
public $empty_message = 'No records to show';
// Built-in layouts
public static $layouts = array(
'horizontal',
'vertical',
'bootstrap',
);
public function __construct($callback = null, $rows = null)
{
// Create the shell rows
$this->row = new Table_Row;
$this->header = new Table_Row;
if (is_callable($callback))
{
call_user_func($callback, $this);
}
elseif (is_array($callback))
{
$this->columns($callback);
}
$rows && $this->rows($rows);
}
/**
* Create a new table using Schema-style callback
*/
public static function make($callback = null, $rows = null)
{
return new static($callback, $rows);
}
/**
* Specify the model from which to take column definitions
*/
public static function of($model, $method = 'configure_table')
{
$table = new static;
is_object($model) and $model = get_class($model);
call_user_func($model.'::'.$method, $table);
return $table;
}
/**
* Specify the markup layout to use
* This can be an arbitrary View or one of horizontal, vertical, or bootstrap
*/
public function layout($layout)
{
if (is_a($layout, 'View'))
{
$this->layout = $layout;
}
elseif (in_array($layout, static::$layouts))
{
$this->layout = \View::make('squi::table.'.$layout);
}
else
{
$this->layout = \View::make($layout);
}
return $this;
}
/**
* Define a column using Schema-style callback
*/
public function column($name, $callback = null)
{
$col = Table_Column::make($this);
// column(Table_Column)
if (is_a($name, 'Squi\\Table_Column'))
{
$col = $name;
$col->table = $this;
}
// column(function($col){ })
elseif (is_callable($name) && is_null($callback))
{
call_user_func($name, $col, $this);
}
// column('prop_name')
elseif (is_string($name) && is_null($callback))
{
// Shortcut that converts a single column descriptor
// into a label (captitalizing and converting underscores)
// and assuming that string is the row property/key from
// which to grab the column value.
$col->name(ucwords(str_replace('_', ' ', $name)))->value($name);
}
// column('Label', 'prop_name')
elseif (is_string($name) && is_string($callback))
{
$col->name($name)->value($callback);
}
// column('Label', function($row){ })
elseif (is_string($name) && is_callable($callback))
{
$col->name($name)->value($callback);
}
return $this->columns[] = $col;
}
/**
* Add columns using array-style declaration
*/
public function columns($columns)
{
foreach ($columns as $key => $val)
{
$args = is_int($key) ? array($val) : array($key, $val);
call_user_func_array(array($this, 'column'), $args);
}
return $this;
}
/**
* Add a row to the table
*/
public function row($row)
{
$this->rows[] = $row;
return $this;
}
/**
* Add an array of rows to the table
*/
public function rows($rows)
{
$this->rows += $rows;
return $this;
}
/**
* Add a non-standard row to the table
* (Useful for footers, button rows, etc.)
*/
public function row_raw($values)
{
$this->rows[] = new Table_Rowdata($values);
return $this;
}
/**
* Set attributes for the rows
* Can be either an assoc array,
* a closure (function($row){}) returning an assoc array,
* or an attribute and value string
*/
public function row_attr($attr, $value = null)
{
$this->row->attr($attr, $value);
return $this;
}
/**
* Set the message to display when no rows exist
*/
public function empty_message($message)
{
$this->empty_message = $message;
return $this;
}
/**
* Render the table
*/
public function render()
{
// Create the pseudo-data row to contain the column names
$this->header_data = new Table_Rowdata;
foreach ($this->columns as $col)
{
$this->header_data->set_value($col, $col->name);
}
// Create the View
if ( ! is_a($this->layout, 'View'))
{
$this->layout($this->layout);
}
// Add reference to the table
$this->layout->with('table', $this);
// Add columns
$this->layout->with('columns', $this->columns);
// Add rows
$this->layout->with('rows', $this->rows);
// Header row
$this->layout->with('header', $this->header);
// Render that mofo
return $this->layout->render();
}
/**
* Render the table to form a string value
*/
public function __toString()
{
return $this->render();
}
}