-
Notifications
You must be signed in to change notification settings - Fork 1
/
classes_Extras.php
457 lines (383 loc) · 13.7 KB
/
classes_Extras.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<?php
/*******************************************
Copyright Rich Bellamy, RMB Webs, 2008
Contact: rich@rmbwebs.com
This file is part of Amortize.
Amortize is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Amortize is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Amortize. If not, see <http://www.gnu.org/licenses/>.
*******************************************/
require_once 'class_AmortizeInterface.php';
/**
* This is an extension of AmortizeObject that provides form processing through a DOMDocument
*
*/
class AmortizeObjectDomForms extends AmortizeInterface {
protected $input_restrictions = NULL;
/**
* Returns a DOMDocument Node that can be used to display the object
* $dom is a domdocument used to create the element
* $which is an array that tells which fields to display
* $hidden will hide fields
*/
function displayFull($dom, $which=null, $hidden=null) {
$which = ($which) ? $which : getTableColumns($this->getTableDefs());
$hidden = ($hidden) ? $hidden : array();
$primary = $this->getPrimary();
$classname = get_class($this);
$returnMe = $dom->createElement('div');
$returnMe->setAttribute('id', "{$classname}_{$primary}_display_container");
$returnMe->setAttribute('class', "{$classname}_display_container display_container");
foreach ($which as $field) {
if (!isset($hidden[$field])) {
$returnMe->appendChild($this->displayField($dom, $field));
}
}
return $returnMe;
}
/**
* Returns a DOMDocument node that can be used to display the details of a specific field.
* It is called by displayFull() for every field that is not on an exclude list
*/
function displayField($dom, $field) {
$classname = get_class($this);
$primary = $this->getPrimary();
$attribs = $this->getAttribs();
$value = (isset($attribs[$field])) ? $attribs[$field] : NULL;
$returnMe = $dom->createElement('div');
$returnMe->setAttribute('id', "{$classname}_{$field}_field_container");
$returnMe->setAttribute('class', "{$classname}_field_container field_container");
$labelDisplay = $dom->createElement('span', $field);
$labelDisplay->setAttribute('id', "{$classname}_{$primary}_{$field}_label");
$labelDisplay->setAttribute('class', "{$field}_label field_label");
$valueDisplay = $dom->createElement('span', $value);
$valueDisplay->setAttribute('id', "{$classname}_{$primary}_{$field}_value");
$valueDisplay->setAttribute('class', "{$field}_value field_value");
$returnMe->appendChild($labelDisplay);
$returnMe->appendChild($valueDisplay);
return $returnMe;
}
/**
* Creates an input form from the object columns
* $which is an array that tells what columns to show
* $action sets the action for the form
* $hidden is an array that can be used to hide columns in the form (true), omit the column from the form (false)
* or pass some hidden values into the form (non-boolean)
*/
function inputForm($dom, $which = NULL, $action = NULL, $hidden=NULL) {
$classname = get_class($this);
$primary = $this->getPrimary();
$which = ($which) ? $which : getTableColumns($this->getTableDefs());
$attribs = $this->getAttribs();
$form = $dom->createElement('form');
$form->setAttribute('id', "{$classname}{$primary}");
$form->setAttribute('class', $classname);
$form->setAttribute('method', "post");
if ($action != null) {
$form->setAttribute('action', $action);
}
foreach ($which as $field) {
if (!isset($hidden[$field])) {
$form->appendChild($this->domInputField($dom, $field));
}
}
if (is_array($hidden)) {
foreach ($hidden as $key => $value) {
$attrib = $attribs[$key];
$input = $dom->createElement('input');
$input->setAttribute('type', "hidden");
$input->setAttribute('name', $key);
if ($value === true) {
$input->setAttribute('value', $attrib);
}
if ($value !== true && $value !== false) {
$input->setAttribute('value', $value);
}
if ($value === false) {
$input = null;
}
if ($input != null) {
$form->appendChild($input);
}
}
}
return $form;
}
/**
* returns a DOM node of a specific input for an object-altering form
* It is called by inputForm() for every field that is not on an exclude list.
*/
function inputField($dom, $field) {
$classname = get_class($this);
$primary = $this->getPrimary();
$attribs = $this->getAttribs();
$value = (isset($attribs[$field])) ? $attribs[$field] : NULL;
$container = $dom->createElement('div');
$container->setAttribute('id', "{$classname}_{$field}_input_container");
$container->setAttribute('class', "{$classname}_input_container");
$label = $dom->createElement('span', $field);
$label->setAttribute('id', "{$classname}_{$primary}_{$field}_label");
$label->setAttribute('class', "{$classname}_{$field}_label");
$restrictions = (isset($this->input_restrictions[$field])) ? $this->input_restrictions[$field] : "input";
if (is_array($restrictions)) {
// Dropdown box
$input = $dom->createElement('select');
if (count($restrictions) < 5) {
$input->setAttribute('size', count($restrictions));
}
foreach ($restrictions as $option => $text) {
$option = $dom->createElement('option', $text);
$option->setAttribute('value', $option);
if ($option == $value) {
$option->setAttribute('selected', "true");
}
$input->appendChild($option);
}
} else if ($restrictions == "textarea") {
//Textarea
$input = $dom->createElement('textarea', $value);
} else {
$input = $dom->createElement('input');
$input->setAttribute('value', $value);
}
$input->setAttribute('id', "{$classname}_{$primary}_{$field}_input");
$input->setAttribute('class', "{$classname}_{$field}_input");
$input->setAttribute('name', "{$field}");
$container->appendChild($label);
$container->appendChild($input);
return $container;
}
}
/**
* This is an extension of AmortizeObjectDomForms that merely provides a default table Primary key
*/
class PrimaryAmortizeObjectDomForms extends AmortizeObjectDomForms {
protected $table_defs = array("Amortize" => array('ID'=> array("bigint(20) unsigned", "NO", "PRI", "", "auto_increment") ) );
}
/**
* This is an extension of AmortizeObject that provides form processing
*
*/
class AmortizeObjectForms extends AmortizeObject {
protected $input_restrictions = NULL;
/**
* Generates fully-styleable markup for displaying this object
* $which is an array that tells which fields to display
* $hidden will hide fields
*/
function displayFull($which=null, $hidden=null) {
$which = ($which) ? $which : getTableColumns($this->getTableDefs());
$hidden = ($hidden) ? $hidden : array();
$primary = $this->getPrimary();
$classname = get_class($this);
echo
<<<startdisplay
<div id="{$classname}_{$primary}_display_container" class="{$classname}_display_container display_container">
startdisplay;
foreach ($which as $field) {
if (!isset($hidden[$field])) {
$this->displayField($field);
}
}
echo
<<<enddisplay
</div>
enddisplay;
}
/**
*
*
*/
function displayField($field) {
$classname = get_class($this);
$primary = $this->getPrimary();
$attribs = $this->getAttribs();
$value = (isset($attribs[$field])) ? $attribs[$field] : NULL;
echo
<<<display
<div id="{$classname}_{$field}_field_container" class="{$classname}_field_container field_container">
<span id="{$classname}_{$primary}_{$field}_label" class="{$field}_label field_label">{$field}</span>
<span id="{$classname}_{$primary}_{$field}_value" class="{$field}_value field_value">{$value}</span>
</div>\n
display;
}
/**
* Creates an input form from the object columns
* $which is an array that tells what columns to show
* $action sets the action for the form
* $hidden is an array that can be used to hide columns in the form (true), omit the column from the form (false)
* or pass some hidden values into the form (non-boolean)
*/
function inputForm($which = NULL, $action = NULL, $hidden=NULL) {
$actionString = ($action) ? "action=\"$action\"" : "";
$classname = get_class($this);
$primary = $this->getPrimary();
$which = ($which) ? $which : getTableColumns($this->getTableDefs());
$attribs = $this->getAttribs();
echo <<<FORMOPEN
<form id="{$classname}{$primary}" class="{$classname}" {$actionString} method="POST">\n
FORMOPEN;
foreach ($which as $field) {
if (!isset($hidden[$field])) {
$this->inputField($field);
}
}
if (is_array($hidden)) {
foreach ($hidden as $key => $value) {
if ($value === true) {
$attrib = $attribs[$key];
echo
<<<HIDDENVALUE
<input type="hidden" name="{$key}" value="{$attrib}" />\n
HIDDENVALUE;
} else if ($value !== false) {
echo
<<<HIDDENVALUE
<input type="hidden" name="{$key}" value="{$value}" />\n
HIDDENVALUE;
} else if ($value === false) {
//Do nothing, omit this field
}
}
}
echo <<<FORMCLOSE
<input type="submit" name="submit" value="Go"/>
</form>
FORMCLOSE;
}
function inputField($field) {
$classname = get_class($this);
$primary = $this->getPrimary();
$attribs = $this->getAttribs();
$value = (isset($attribs[$field])) ? $attribs[$field] : NULL;
$restrictions = (isset($this->input_restrictions[$field])) ? $this->input_restrictions[$field] : "input";
$label = (isset($this->friendly_label[$field])) ? $this->friendly_label[$field] : $field;
// FIXME Need to eventually customize this per the data type
echo <<<start
<div id="{$classname}_{$field}_input_container" class="{$classname}_input_container">
<span class="{$classname}_{$field}_label" id="{$classname}_{$primary}_{$field}_label">{$label}</span>\n
start;
if (is_array($restrictions)) {
// Dropdown box
$size = (count($restrictions) <= 4) ? " size=\"".count($restrictions)."\"" : "";
echo <<<open
<select class="{$classname}_{$field}_input" id="{$classname}_{$primary}_{$field}_input" name="{$field}"{$size}>\n
open;
foreach ($restrictions as $option => $text) {
$selected = ($option == $value) ? " selected" : "";
echo <<<option
<option value="{$option}"{$selected}>{$text}</option>\n
option;
}
echo <<<close
</select>\n
close;
} else if ($restrictions == "textarea") {
echo <<<textarea
<textarea name="{$field}" class="{$classname}_{$field}_input" id="{$classname}_{$primary}_{$field}_input">{$value}</textarea>\n
textarea;
} else {
echo <<<input
<input name="{$field}" class="{$classname}_{$field}_input" id="{$classname}_{$primary}_{$field}_input" value="{$value}">\n
input;
}
echo <<<end
</div>\n
end;
}
}
/**
* This is an extension of DbMOForms that merely provides a default table Primary key
*/
class PrimaryAmortizeObjectForms extends AmortizeObjectForms {
protected $table_defs = array("Amortize" => array('ID'=> array("bigint(20) unsigned", "NO", "PRI", "", "auto_increment") ) );
}
/**
* The User class is a generic class that allows for logging in and out of
* a custom app.
*/
class DbMExtras_User extends AmortizeObjectForms {
protected $table_defs = array(
'Users' => array(
'login' => array("varchar(10)", "NO", "PRI"),
'password' => "tinytext",
'type' => "varchar(10)"
)
);
protected $omegaMan = false;
/// Most DMB Objects do not get their IDs set by the constructor, but by the load function.
/// Users have to be an exception in the event that you build a user-driven website and can't login
function __construct($id = NULL) {
// Call the default constructor
parent::__construct($id);
// Check for proper loading
if ($this->status[$this->getPrimaryKey()] == "dirty") {
// The load didn't occur properly, check if there are any users in the DB
if (!$this->getAllPrimaries(1)) {
// I am the Omega Man!
$this->omegaMan = true;
$this->setAttribs(array('login' => $id, 'type' => "superadmin"));
}
}
}
function save($force=false) {
$a = $this->getAttribs();
if ($a['type'] != "admin" && $a['type'] != "superadmin") {
$this->setAttribs(array('type' => "regular"));
}
parent::save($force);
}
function checkPass($pass) {
$attribs = $this->getAttribs();
if (strlen($this->getPrimary()) != 0) {
// Not a blank user
if ($pass == $attribs['password']) {
// password matches what we have on record
return true;
} else {
// Password didn't match, but lets check if that's because there are no users
if ($this->omegaMan) {
// Apparently, we discovered that there were no users in the DB, so now that we have a password, let's save this user
$this->setattribs(array('password' => $pass));
$this->save();
// Indicate that this was the proper password.
return true;
}
}
} else {
// Blank user primary
return false;
}
}
function welcomeMessage($which=null) {
$pri = $this->getPrimary();
$attribs=$this->getattribs();
$sal = ($which != null) ? $attribs[$which] : $pri;
$class = get_class($this);
echo
<<<welcome
<span id="{$class}_{$pri}_welcome" class="{$class}_welcome">Welcome, {$sal}.</span>\n
welcome;
}
function logoutForm($action=null) {
$action = (!is_null($action)) ? " action=\"{$action}\"" : "";
$class = get_class($this);
echo
<<<logout
<form{$action} method="POST" class="${class}_logout">
<input type="submit" name="loginaction" value="logout" />
</form>\n
logout;
}
function loginForm($action=null) {
$this->inputForm(array("login", "password"), $action, $hidden=array('loginaction'=>"login"));
}
}
?>