Repository navigation
Expand file tree
/
Copy pathHTMLForms.php
More file actions
96 lines (74 loc) · 3.14 KB
/
Copy pathHTMLForms.php
File metadata and controls
96 lines (74 loc) · 3.14 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
<?php
class HTMLForms {
var $dictionary;
public function HTMLForms($dictionary = []) {
$this->dictionary = $dictionary;
}
private function translate($key) {
if (array_key_exists($key, $this->dictionary)) {
return $this->dictionary[$key];
} else {
return $key;
}
}
/*
* $tableInf - column information
* $form_action - where to point to
* $operation - insert OR update
* $args - used in case of $operation = 'update'
*/
public function getForm($tablename, $form_action, $args = []) {
$operation = "update";
if (empty($args)) {
$operation = "insert";
}
$tableInf = BD::getTableInfo($tablename);
$form = "<form id='updateForm' action='server/action.php' method='post'>";
$form .= $this->getHiddenInput("action", explode("F", $form_action)[0]);
$form .= $this->getHiddenInput("table", $tablename);
while ($column = pg_fetch_array($tableInf)) {
if (array_key_exists($column["name"], $args)) {
$field_value = $args[$column["name"]];
} else {
$field_value = "";
}
if ($column["primary_key"] === "t") {
$form = $form . $this->getHiddenInput("id_" . $column["name"], $field_value);
if ($operation === "update") {
$form .= $this->getTextInput($column["name"], $column["typ"], $field_value);
}
} else {
switch ($column['typ']) {
case 'text':
$form .= $this->getTextArea($column["name"], $column["typ"], $field_value);
break;
default:
$form .= $this->getTextInput($column["name"], $column["typ"], $field_value);
}
}
$form .= "</br>";
}
$form .= "<input type='submit' class='btn btn-primary pull-right' value='Guardar' />"
. "<input id='$form_action-FormBack' class='btn btn-default pull-right'type='button' value='Voltar'/>";
return $form . "</form>";
}
private function getHiddenInput($name, $val) {
$input = "<input name='$name' type='hidden' value='$val'></input>";
return $input;
}
private function getTextInput($name, $validation, $value = "") {
$trans_name = $this->translate($name);
$label = "<label class='col-md-4 control-label' for='$name'>$trans_name:</label>";
$input = "<input class='form-control input-md'name='$name' type='text' validate='$validation' value='$value'></input>";
return $label . $input;
}
private function getTextArea($name, $validation, $value = "") {
$trans_name = $this->translate($name);
$label = "<label class='col-md-4 control-label' for='$name'>$trans_name:</label>";
$textarea = "<textarea class='form-control input-md' name='$name' rows='6' validate='$validation'>$value</textarea>";
return $label . $textarea;
}
public function setDictionary($dictionary) {
$this->dictionary = $dictionary;
}
}