forked from filipi/onde
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-cube.php
114 lines (111 loc) · 3.25 KB
/
json-cube.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
<?PHP
/**
* $Id: json-cube.php,v 1.8 2014/02/04 18:32:59 filipi Exp $
* á
*/
$useSessions = 1; $ehXML = 1;
$myPATH = ini_get('include_path') . ':./include:../include:../../include';
ini_set('include_path', $myPATH);
include "page_header.inc";
$action = "list";
if ($_POST['anim']){
$action = "insert";
$observacao = pg_escape_string($_POST['observacao']);
}
if ($_POST['codigo']){
$action = "update";
$codigo = intval($_POST['codigo']);
}
if ($_GET['codigo']){
$action = "load";
$codigo = intval($_GET['codigo']);
}
switch ($action){
case "load":
if ($codigo){
$query = "select \"Conteúdo\" as conteudo from framescubo where codigo = " . $codigo;
$result = pg_exec ($conn, $query);
$row = pg_fetch_row($result, 0);
$dados = preg_replace("/(\r\n|\n\r|\n|\r)/", ',', $row[0]);
$dataArray = explode(",", $dados);
$key = 0;
for ($frame = 0; $frame < 16; $frame++){
for ($line = 0; $line < 8; $line++){
for ($plane = 0; $plane < 8; $plane++){
$dataItem = $dataArray[$key];
for ($voxel = 0; $voxel < 8; $voxel++){
$anim[$frame]['frame'][$line]['line'][$plane]['plane'][$voxel]['voxel'] = ($dataItem & 1) ? true : false;
$dataItem = $dataItem >> 1;
}
$key++;
}
}
}
$anim = json_encode($anim);
//$anim = str_replace("]", "]\n", $anim);
//$anim = str_replace("}", "}\n", $anim);
//$anim = str_replace("{", "{\n ", $anim);
echo $anim;
}
break;
case "list":
$query = "select codigo, nome as observacao from framescubo order by nome";
$result = pg_exec ($conn, $query);
$frames = pg_fetch_all ($result);
$frames = json_encode($frames);
echo $frames;
break;
case "insert":
$query = "INSERT INTO framescubo (usuario, nome, \"Conteúdo\") VALUES (";
$query .= "'" . $_SESSION['matricula'] . "',";
$query .= "'" . $observacao . "','";
foreach ($_POST['anim'] as $frame => $frames){
foreach ($frames as $line => $lines){
foreach ($lines as $plane => $planes){
//foreach ($planes as $key => $voxel){
//$query .= "[" . $frame . "][" . $line . "][" . $plane . "][" . $key . "] = " . $voxel . "\n";
//}
$numero = 0;
for ($i = 7; $i>=0; $i--){
$numero = ($numero << 1) + ($planes[$i]=='true' ? 1 : 0 );
}
$query .= $numero . "\n";
}
}
}
$query .= "')";
$result = pg_exec($conn, $query);
if ($result)
echo "Salvo com sucesso";
else{
echo pg_last_error();
}
break;
case "update":
$query = "UPDATE framescubo SET";
$query .= " usuario = '" . $_SESSION['matricula'] . "',\n";
$query .= " nome = '" . $observacao . "',\n";
$query .= " \"Conteúdo\" = '";
foreach ($_POST['anim'] as $frame => $frames){
foreach ($frames as $line => $lines){
foreach ($lines as $plane => $planes){
$numero = 0;
for ($i = 7; $i>=0; $i--){
$numero = ($numero << 1) + ($planes[$i]=='true' ? 1 : 0 );
}
$query .= $numero . "\n";
}
}
}
$query .= "'\n";
$query .= " WHERE codigo = " . $codigo;
$result = pg_exec($conn, $query);
if ($result)
echo "Salvo com sucesso";
else{
echo pg_last_error();
}
break;
}
include "page_footer.inc";
?>