-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyhtmldom.php
488 lines (306 loc) · 13.5 KB
/
myhtmldom.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<?php
class MyHtmlDom {
protected $dom; //DOM document
protected $head, $body;
function __construct() {
$impl = new DOMImplementation();
$dtd = $impl->createDocumentType('HTML', '', '');
$this->dom = $impl->createDocument('', '', $dtd);
$this->html = $this->dom->appendChild($this->dom->createElement('html'));
$this->head = $this->html->appendChild($this->dom->createElement('head'));
$this->head->appendChild(new DOMText("\n"));
$this->CSS = $this->head->appendChild($this->dom->createElement('style'));
$this->body = $this->html->appendChild($this->dom->createElement('body'));
$this->body->appendChild(new DOMText("\n"));
$this->body->appendChild($this->dom->createElement('p', 'asd'));
}
function display() {
$this->dom->preserveWhiteSpace = false;
$this->dom->formatOutput = true;
return $this->dom->saveHTML();
}
function replace_or_add_form($newF) {
$oldF = $this->dom->getElementsByTagName("form")[0];
if ($oldF) {
$this->dom->body->replaceChild($newF, $oldF);
return;
}
$this->body->appendChild($newF);
}
function get_dom() {
return $this->dom;
}
function get_head() {
return $this->head;
}
function get_body() {
return $this->body;
}
function add_CSS($style_text) { //I assume it's in head (?)
$this->CSS->appendChild(new DOMText("\n"));
$this->CSS->appendChild(new DOMText($style_text));
$this->CSS->appendChild(new DOMText("\n"));
}
function add_script_src($src) {
$script_el = $this->head->appendChild($this->dom->createElement('script'));
$script_el->setAttribute("src", $src);
}
function add_css_link($href) {
$script_el = $this->head->appendChild($this->dom->createElement('link'));
$script_el->setAttribute("rel", "stylesheet");
$script_el->setAttribute("href", $href);
}
function add_page_title($title) {
$title_el = $this->head->appendChild($this->dom->createElement('title'));
$title_el->appendChild(new DOMText($title));
}
} //class
class MyFormDom {
private $dom; //corresponding
private $form;
private $id;
private $submit_callables; //asc array
private $input_tags; //array
private $init_callables; //array, too complicated otherwise
private $helper_callables; //asc array
private $get_string; //same as in the maindom case (not in constructor - set in function)
private $dom_head; //1.1
function __construct($dom, $id, $action, $method = "post") {
$this->id = $id;
$this->dom = $dom;
$this->dom_head = $this->dom->getElementsByTagName("head")[0]; //beleave it's only one
$this->form = $this->dom->createElement("form");
$this->form->setAttribute("id", $this->id);
$this->form->setIdAttribute("id", true); //important
$this->form->setAttribute("method", $method);
$this->form->setAttribute("action", $action);
$this->add_newline();
$this->submit_callables = array();
$this->init_callables = array();
$this->helper_callables = array();
$this->input_tags = array("input", "select", "textarea");
}
//no display - it is in dom class
function get_form() {
return $this->form;
}
function get_dom() {
return $this->dom;
}
//1.1
function add_form_page_title($title) {
$title_el = $this->dom_head->appendChild($this->dom->createElement('title'));
$title_el->appendChild(new DOMText($title));
}
function set_get_string($get_string) {
$this->get_string = $get_string;
}
function get_get_string() { //supid a little...
return $this->get_string;
}
//\1.1
function add_br() {
$this->form->appendChild($this->dom->createElement('br'));
$this->add_newline();
}
function add_newline() {
$this->form->appendChild(new DOMText("\n"));
}
function add_text($text) {
$this->form->appendChild(new DOMText($text));
$this->add_newline();
}
function add_text_element($tag, $text) { //p, h123, etc.
$text_element = $this->form->appendChild($this->dom->createElement($tag, $text));
$this->add_newline();
return $text_element;
}
function add_id_to_element($element, $id) {
if ($this->dom->getElementById($id)) return;
$element->setAttribute("id", $id);
$element->setIdAttribute("id", true); //important
}
function add_entity_ref($ename) {
$this->form->appendChild(new DOMEntityReference($ename));
}
function add_sametype_element($type, $id) {
$sametype_element = $this->form->appendChild($this->dom->createElement($type));
$sametype_element->setAttribute("type", $type);
$sametype_element->setAttribute("id", $id);
$sametype_element->setIdAttribute("id", true); //important
$sametype_element->setAttribute("id", $id);
$sametype_element->setAttribute("name", $id); //for getting from POST
$this->add_newline();
return $sametype_element;
}
function add_input_element($type, $id) {
$input_element = $this->form->appendChild($this->dom->createElement("input"));
$input_element->setAttribute("type", $type);
$input_element->setAttribute("id", $id);
$input_element->setIdAttribute("id", true); //important
$input_element->setAttribute("name", $id); //for getting from POST
$this->add_newline();
return $input_element;
}
function set_attr_novalue($id, $name) { //better v.
$element = $this->dom->getElementByID($id);
$attr = $this->dom->createAttribute($name);
$element->setAttributeNode($attr);
}
function set_inner_text($element, $text) { //in practice - add
$element->appendChild(new DOMText($text));
}
function add_inner_text_by_id($id, $text) {
$element = $this->dom->getElementById($id);
$element->appendChild(new DOMText($text));
}
function replace_inner_text($id, $text) {
$element = $this->dom->getElementById($id);
if ($element->childNodes->length) {
$toremove = $element->childNodes[0];
$element->removeChild($toremove);
}
$element->appendChild(new DOMText($text));
}
function el_replace_inner_text($element, $text) {
if ($element->childNodes->length) {
$toremove = $element->childNodes[0];
$element->removeChild($toremove);
}
$element->appendChild(new DOMText($text));
}
function append_child_to_parent($parent_element_id, $child_element) { //this when we've got only the form and parent id
$parent_element = $this->dom->getElementById($parent_element_id);
$parent_element->appendChild($child_element);
}
function add_children_from_array($id, $chtag, $chvalues, $samevalue=true, $inneltag = "") { //no same value - make help
//$inneltag - element in the element
$element = $this->dom->getElementById($id);
foreach ($chvalues as $childvalue) {
$child = $element->appendChild($this->dom->createElement($chtag));
if ($samevalue) {
$child->setAttribute("value", $childvalue);
}
if ($inneltag === "") {
$this->set_inner_text($child, $childvalue);
$this->add_newline();
}
else {
$innchild = $child->appendChild($this->dom->createElement($inneltag));
$this->set_inner_text($innchild, $childvalue);
}
}
$this->add_newline();
}
function set_child_attr_by_chindex($id, $chtag, $chindex, $attr, $value) { //better edition
$thechild = $this->get_child_el_by_chindex($id, $chtag, $chindex);
$thechild->setAttribute($attr, $value);
return $thechild;
}
function set_child_attr_by_chvalattr($id, $chtag, $valattrvalue, $attr, $value) {
$element=$this->dom->getElementById($id);
$children = $element->getElementsByTagName($chtag);
foreach ($children as $child) {
if ($child->getAttribute("value") === $valattrvalue) $child->setAttribute($attr, $value);
else $child->removeAttribute($attr);
}
}
function get_child_el_by_chindex($id, $chtag, $chindex) { //same as above, just return element
$element=$this->dom->getElementById($id);
$children = $element->getElementsByTagName($chtag);
if ($chindex >= $children->length) throw new Exception("child index out of range!");
return $children[$chindex];
}
function set_value_by_id($id, $value) { //the name is quite misguiding...
$element = $this->dom->getElementById($id);
if (!$element) throw new Exception("no element with such id!");
$element_type = $element->getAttribute("type");
filelog("in setvaluebyid problematic type: " . $element_type);
filelog("in setvalue byid problematic value: " . $value);
if ($element_type == "textarea") {
$this->replace_inner_text($id, $value);
return;
}
}
function format_table_by_cols_array($id, $header_array) {
$table_element = $this->dom->getElementById($id);
$header = $table_element->appendChild($this->dom->createElement("tr"));
foreach ($header_array as $colname) {
$colhead = $header->appendChild($this->dom->createElement("th"));
$this->set_inner_text($colhead, $colname);
}
}
function populate_table_from_array($id, $rows_array, $autonumber = false) {
$table_element = $this->dom->getElementById($id);
foreach ($rows_array as $index=>$row) {
$row_element = $table_element->appendChild($this->dom->createElement("tr"));
if ($autonumber) {
$cell_element = $row_element->appendChild($this->dom->createElement("td"));
$this->set_inner_text($cell_element, strval(++$index));
}
//1.1
foreach ($row as $cell_data) {
$cell_element = $row_element->appendChild($this->dom->createElement("td"));
$this->set_inner_text($cell_element, $cell_data);
}
//\1.1
} //foreach $rows_array $index=>$row
} //populate f-n
function add_submit_callable($submit_id, $func) {
$this->submit_callables[$submit_id] = $func;
}
function set_init_callable($func) {
$this->init_callables[] = $func; //because it's easier
//equivalent mostly to array_push
}
function add_helper_callable($helper_id, $func) {
$this->helper_callables[$helper_id] = $func;
}
function post_set($POST) {
filelog("IN post_set of form " . $this->form->getAttribute("id") . " _POST: " . print_r($POST, true));
foreach ($this->input_tags as $itag) {
foreach ($this->form->getElementsByTagName($itag) as $finputable) {
if ($POST[$finputable->getAttribute("name")]) {
if ($finputable->getAttribute("type") === "checkbox" || $finputable->getAttribute("type") === "radio") { //radio
$finputable->setAttribute("checked", ""); //check it
continue 1; //doesn't need a value
}
if ($finputable->getAttribute("type") === "select") {
$this->set_child_attr_by_chvalattr($finputable->getAttribute("id"), "option", $POST[$finputable->getAttribute("name")], "selected", "selected");
continue 1; //doesn't need a value..
}
if ($finputable->getAttribute("type") === "textarea") {
$this->replace_inner_text($finputable->getAttribute("id"), $POST[$finputable->getAttribute("name")]);
continue 1; //doesn't need a value..
}
$finputable->setAttribute("value", $POST[$finputable->getAttribute("name")]); //sets value!!!
if ($finputable->getAttribute("type") === "submit") $submitter = $finputable->getAttribute("name");
}
elseif ($finputable->getAttribute("type") === "checkbox" || $finputable->getAttribute("type") === "radio") { //radio
$finputable->removeAttribute("checked"); //should uncheck...
}
}
}
//for the dynamic jquery submit (not necessary)
if (isset($POST["nonexistinginput"])) $submitter = $POST["nonexistinginput"];
//for dynam sumb
filelog("submitter: $submitter");
if ($submitter) $_SESSION['LAST_SUBMIT_NAME'] = $submitter;
else $_SESSION['LAST_SUBMIT_NAME'] = 'EMPTY';
filelog("in post_set, LAST_SUBMIT_NAME: " . $_SESSION['LAST_SUBMIT_NAME'] . "\n");
if ($this->submit_callables[$submitter]) $this->submit_callables[$submitter]($this);
}
function init_call() {
foreach ($this->init_callables as $init_callable) $init_callable($this); //1.1 successive call of inits
}
function helper_call($helper_id) {
$this->helper_callables[$helper_id]($this);
}
function submit_call($submit_id) {
if ($this->submit_callables[$submit_id]) $this->submit_callables[$submit_id]($this);
}
function reset_all() {
reset_it($this->get_string);
}
} //MyFormDom
?>