-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild-form.php
87 lines (67 loc) · 3.01 KB
/
build-form.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
<?php
// Require ODR API demo class
require_once '../Api/Odr.php';
// Configuration array, with user API Keys
$config = array(
'api_key' => '#API_KEY#',
'api_secret' => '#API_SECRET#',
);
// Create new instance of API demo class
$demo = new Api_Odr($config);
// Login into API
$demo->login();
$loginResult = $demo->getResult();
if ($loginResult['status'] === Api_Odr::STATUS_ERROR) {
echo 'Can\'t login, reason - '. $loginResult['response'];
exit(1);
}
if (empty($_REQUEST['__sent'])) {
$demo->info('/domain/test.eu/', Api_Odr::METHOD_POST);
$info = $demo->getResult();
$form = '<form action="" method="post">';
foreach ($info['response']['fields'] as $name => $field) {
$form .= '<div style="margin-bottom: 10px;">' . fieldToHtml($name, $field) . '</div>';
}
$form .= '<button type="submit" name="__sent">Submit generated form</button>';
$form .= '</form>';
echo $form;
} else {
// Submit the data, it's located in the $_POST variable
}
/**
* Converts info response to HTML input
* Notice! This is a bare-bone function, it doesn't wrap generated input or display help or anything
* It just builds input
*
* @param string $name
* @param array $field
*
* @return string
*/
function fieldToHtml($name, array $field)
{
$html = '<input type="#TYPE#" name="#NAME#"#REQUIRED##READONLY##CLASSNAME##TITLE#>';
$r = array(
'#TYPE#' => in_array($field['class'], array('String_Email', 'Text_Email'), true) ? 'email' : 'text',
'#NAME#' => $name,
'#REQUIRED#' => empty($field['is_required']) ? '' : ' required',
'#READONLY#' => empty($field['is_readonly']) ? '' : ' readonly',
'#CLASSNAME#' => empty($field['classname']) ? '' : ' class="' . $field['classname'] . '"',
'#TITLE#' => empty($field['title']) ? ' placeholder="' . $name . '"' : ' title="' . $field['title'] . '" placeholder="' . $field['title'] . '"',
);
if (strpos($field['class'], 'String_Textarea') === 0 || strpos($field['class'], 'Text_Textarea') === 0) {
$html = '<textarea name="#NAME#"#REQUIRED##READONLY##CLASSNAME##TITLE#></textarea>';
} elseif (in_array($field['class'], array('String_Nameserver', 'Text_Nameserver'), true)) {
$html = '<input type="#TYPE#" name="#NAME#[host]"#REQUIRED##READONLY##CLASSNAME##TITLE#>';
} elseif (strpos($field['class'], 'Checkbox') === 0) {
$html = '<label><input type="checkbox" value="1"#REQUIRED##READONLY##CLASSNAME#>' . (!empty($field['title']) ? $field['title'] : $name) . '</label>';
} elseif (strpos($field['class'], 'Select') === 0) {
$html = '<select name="#NAME#"#REQUIRED##READONLY##CLASSNAME##TITLE#>#OPTIONS#</select>';
$r['#OPTIONS#'] = '<option value="">Select...</option>';
foreach ($field['select_options'] as $optionName => $optionValue) {
$r['#OPTIONS#'] .= '<option value="'. $optionName .'">' . $optionValue . '</option>';
}
}
$html = str_replace(array_keys($r), array_values($r), $html);
return $html;
}