-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
106 lines (94 loc) · 2.87 KB
/
lib.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
<?php
require_once dirname(__FILE__) . '/../../config.php';
//require_once('config.php');
function dump()
{
echo '<pre>';
foreach (func_get_args() as $key => $value) {
var_dump($value);
}
echo '</pre>';
}
function dumpd()
{
echo '<pre>';
foreach (func_get_args() as $key => $value) {
var_dump($value);
}
echo '</pre>';
die();
}
function raise_error()
{
echo '<pre>';
foreach (func_get_args() as $key => $value) {
var_dump($value);
}
echo '</pre>';
die();
}
function json_request($service, $params)
{
global $CFG;
$curl = curl_init("$CFG->block_suap_url_api/$service/");
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt(
$curl,
CURLOPT_HTTPHEADER,
array("Content-type: application/json",
"Authorization: Token $CFG->block_suap_token")
);
curl_setopt($curl, CURLOPT_POST, true);
if (isset($params)) {
$content = json_encode($params);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
}
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($status != 200) {
die("REST ERRO: $status");
}
if (substr($json_response, 0, 8) == '{"erro":') {
$erro = json_decode($json_response, true);
die("REST ERRO: " . $erro['erro']);
}
$result = json_decode($json_response, true);
return count($result) == 1 && array_key_exists(0, $result) ? [] : $result;
}
function render_selectbox($name, $list, $prop_id = 'id', $prop_name = 'name', $selected_value = null, $vazio = 'Escolha...', $another_key = null)
{
if (in_array($name, $_REQUEST) && !$selected_value) {
$selected_value = $_REQUEST[$name];
}
echo "<select name='$name'>";
if ($vazio) {
echo "<option>$vazio</option>";
}
echo $selected_value;
foreach ($list as $elem) {
$value = $elem->{$prop_id};
$label = $elem->{$prop_name};
$key_value = $another_key ? $elem->{$another_key} : $value;
$selected = $key_value == $selected_value ? " selected='selected' " : "";
echo "<option value='$value' $selected >$label</option>";
}
echo "</select>";
}
function render_datalist($name, $list, $prop_id = 'id', $prop_name = 'name', $selected_value = null, $another_key = null)
{
if (in_array($name, $_REQUEST) && !$selected_value) {
$selected_value = $_REQUEST[$name];
}
echo "<input list='datalist_$name' name='$name' size='150'>";
echo "<datalist id='datalist_$name'>";
echo $selected_value;
foreach ($list as $elem) {
$value = $elem->{$prop_id};
$label = $elem->{$prop_name};
$key_value = $another_key ? $elem->{$another_key} : $value;
echo "<option value='$value :: $label' />";
}
echo "</datalist>";
}