-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcolumn.php
More file actions
198 lines (179 loc) · 5.77 KB
/
Copy pathcolumn.php
File metadata and controls
198 lines (179 loc) · 5.77 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
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
<?php
include_once(join(DIRECTORY_SEPARATOR,
array(dirname(__FILE__), 'date.php')));
class Column {
private $name;
private $datatype;
private $allow_null;
private $primary_key;
/**
* Column constructor
*
* @param string $name The name of the column
* @param string $datatype The datatype of the column
* @param boolean $allow_null Whether NULL is an allowable value
* @param boolean $primary_key Whether the column is a primary key
* @return Column
*/
function __construct($name, $datatype, $allow_null, $primary_key) {
$this->name = $name;
$this->datatype = $datatype;
$this->allow_null = (boolean) $allow_null;
$this->primary_key = (boolean) $primary_key;
}
/**
* Return the name of the column
*
* @return string
*/
public function name() {
return $this->name;
}
/**
* Return the datatype of the column
*
* @return string
*/
public function datatype() {
return $this->datatype;
}
/**
* Return whether this column allows NULL values in the database
*
* @return boolean
*/
public function allow_null() {
return $this->allow_null;
}
/**
* Returns whether this column is part of the primary key of the table
*
* @return boolean
*/
public function primary_key() {
return $this->primary_key;
}
/**
* Convert value to string for HTML
*
* Converts a value to a String for displaying on an HTML page, based
* on its datatype.
*
* @param string $value The value to convert
* @return string
*/
public function stringify($value) {
switch ($this->datatype) {
case 'boolean':
if ($value === null) {
return '?';
}
return $value ? 'Yes' : 'No';
case 'date':
return $value ? $value->to_s() : '';
default:
return htmlspecialchars((string) $value);
}
}
/**
* Convert value to string for a form
*
* Converts a value to a String, which is a fragment of an HTML form
* suitable for filling in the current value.
*
* @param string $value The value to convert
* @param string $comparison The value to compare for a checkbox
* @return string
*/
public function formify($value, $comparison = null) {
if ($value === null) {
return '';
}
if ($comparison !== null) {
if ($value == $comparison) {
return ' checked';
}
else {
return '';
}
}
switch ($this->datatype) {
case 'boolean':
return $value ? ' checked' : '';
case 'date':
return ' value="' . $value->to_s() . '"';
default:
return ' value="' . ((string) $value) . '"';
}
}
/**
* Prepare a string for the database
*
* Converts a value to a string suitable for insertion into q query to
* the database (eg, INSERT or UPDATE queries).
*
* @param string $value The value to prepare
* @return string
*/
public function prep_for_database($value) {
if ($value === null) {
return null;
}
switch ($this->datatype) {
case 'boolean':
return $value ? 't' : 'f';
case 'date':
return $value->to_s();
default:
return (string) $value;
}
}
/**
* Process value from database or form
*
* Processes a value coming either from the database or an HTML form
* for inclusion in an object, depending on the datatype of the
* column.
*
* @param string $value The value to process
* @return mixed
*/
public function process_value($value) {
if (($value === '') || ($value === null)) {
return null;
}
switch ($this->datatype) {
case 'boolean':
if ($value === 'null') {
return null;
}
else if ($value === 'f' || $value === 0 ||
$value === false || $value === 'false' ||
!$value) {
return false;
}
else {
return true;
}
case 'double precision':
case 'numeric':
return (double) $value;
case 'integer':
return (integer) $value;
case 'date':
if (is_object($value) &&
((string) get_class($value)) == 'Date') {
return $value;
}
return Date::parse($value);
default:
if ($value == '') {
return null;
}
else {
return $value;
}
}
}
}
?>