/
item.php
117 lines (107 loc) · 3.92 KB
/
item.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
<?php
// No HTTP GZIP must be here!
require_once "overall.php";
$PREVIEW_TABLES_COLS = 40;
$tables = null;
$id = @$_GET['id']? @$_GET['id'] : @$_POST['item']['id'];
$retpath = isset($_GET['retpath'])? $_GET['retpath'] : 'index.php';
if (!empty($_POST['doDelete'])) {
$DB->update("DELETE FROM item WHERE id=?", $id);
$DB->update("DELETE FROM data WHERE item_id=?", $id);
redirect($retpath, "Item deleted.");
}
if (!empty($_POST['doClear'])) {
$DB->update("DELETE FROM data WHERE item_id=?", $id);
addMessage("Item data cleared.");
} else if (!empty($_POST['doSave']) || !empty($_POST['doTest']) || !empty($_POST['doRecalc'])) {
try {
$DB->beginTransaction();
$item = validateItem($_POST['item']);
if (!$id) {
$DB->update(
'INSERT INTO item(id, name, "sql", dsn_id, recalculatable, archived, dim, tags, created, modified, relative_to) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
$id = $DB->getSeq(), $item['name'], $item['sql'], $item['dsn_id'], $item['recalculatable'], $item['archived'], $item['dim'], $item['tags'], time(), time(), $item['relative_to']
);
} else {
$DB->update(
'UPDATE item SET name=?, "sql"=?, dsn_id=?, recalculatable=?, archived=?, dim=?, tags=?, modified=?, relative_to=? WHERE id=?',
$item['name'], $item['sql'], $item['dsn_id'], $item['recalculatable'], $item['archived'], $item['dim'], $item['tags'], time(), $item['relative_to'], $id
);
}
if (!empty($_POST['doSave'])) {
$DB->commit();
redirect("{$retpath}#$id", "Data is saved.");
} else if (!empty($_POST['doTest']) || !empty($_POST['doRecalc'])) {
list ($to, $back, $period) = parseToBackPeriod($_POST);
$periods = $period? array($period) : array_keys(getPeriods());
$tables = array();
$hideLogJs = '</div><script type="text/javascript">document.getElementById("log").style.display="none"</script>';
try {
echo '<div id="log">';
foreach ($periods as $period) {
recalcItemRow($id, $to, $back, $period);
$data = generateTableData($to + 1, $back, $period, $id);
$periods = getPeriods();
$tables[$periods[$period]] = generateHtmlTableFromData($data, true);
}
echo $hideLogJs;
} catch (Exception $e) {
echo $hideLogJs;
throw $e;
}
if (!empty($_POST['doTest'])) {
$DB->rollBack();
} else {
$DB->commit();
$_POST['item']['id'] = $id;
}
}
} catch (Exception $e) {
$DB->rollBack();
addMessage($e->getMessage());
}
} else {
$_POST['item'] = array();
if ($id) {
$_POST['item'] = fetchItem($id);
} else if (@$_GET['clone']) {
$_POST['item'] = fetchItem($_GET['clone']);
unset($_POST['item']['id']); // very important!
} else {
$_POST['item']['sql'] = "SELECT COUNT(*)\nFROM some_table\nWHERE created BETWEEN \$FROM AND \$TO\n";
}
}
$SELECT_DSNS = array();
foreach ($DB->select("SELECT id, name FROM dsn ORDER BY name") as $row) {
$SELECT_DSNS[$row['id']] = $row['name'];
}
$SELECT_ITEMS = array();
$seenArchived = 0;
foreach ($DB->select("SELECT id, name, archived FROM item ORDER BY archived, name") as $row) {
if (!$seenArchived && $row['archived']) {
$SELECT_ITEMS[0] = "";
}
$SELECT_ITEMS[$row['id']] = $row['name'];
}
$SELECT_PERIODS = getPeriods();
if (!$tables && $id) {
$to = $DB->selectCell("SELECT MAX(created) FROM data WHERE item_id=?", $id);
if (!$to) $to = time();
foreach ($SELECT_PERIODS as $period => $periodName) {
$data = generateTableData($to, $PREVIEW_TABLES_COLS, $period, $id);
$tables[$periodName] = generateHtmlTableFromData($data, true);
}
}
$title = $id
? 'Edit item <a href="' . htmlspecialchars($_SERVER['REQUEST_URI']) . '">' . htmlspecialchars($_POST['item']['name']) . '</a>' .
' <a href="item.php?clone=' . htmlspecialchars($id) . '" title="Clone this item"><img src="static/clone.gif" width="10" height="10" border="0" /></a>'
: "Add a new item";
template(
"item",
array(
"titleHtml" => $title,
"title" => strip_tags($title),
"tables" => $tables,
"canAjaxTestSql" => canAjaxTestSql(),
)
);