prepare($sql);
$sth->execute([$user->vid]);
$opt_storage = array();
foreach ($sth->fetchAll() as $row) {
$opt_storage[$row['sid']] = $row['sname'];
}
// ========== ACTIONS START ===================================================
switch ($submit = form_get_action()) {
case NULL: break;
case 'add': $action = ACT_ADD; break;
case 'view': $action = ACT_VIEW; break;
case 'edit': $action = ACT_EDIT; break;
case 'del': $action = ACT_DELETE; break;
case 'insert':
$p[':label'] = gpc_get_string($_POST, 'label');
$p[':sid'] = gpc_get_int($_POST, 'sid');
$p[':content'] = gpc_get_string($_POST, 'content');
$p[':remarks'] = gpc_get_string($_POST, 'remarks');
$keys = array_keys($p);
$fields = join(',', array_map(function($s) { return ltrim($s, ':'); }, $keys));
$pnames = join(',', $keys);
$sth = $pdo->prepare("INSERT INTO box ($fields) VALUES ($pnames)");
try {
$sth->execute($p);
} catch (PDOexception $e) {
$g_error->Add('SQL-Error: '. $e->getMessage());
}
$id = $pdo->LastInsertId();
$action = ACT_VIEW;
break;
case 'update':
$p[':label'] = gpc_get_string($_POST, 'label');
$p[':content'] = gpc_get_string($_POST, 'content');
$p[':color'] = gpc_get_color($_POST, 'color');
$p[':weight'] = min(gpc_get_float($_POST, 'weight'), 99.99);
$p[':remarks'] = gpc_get_string($_POST, 'remarks', 150);
$fields = join(',', array_map(function($s) { return ltrim($s, ':') . '=' . $s; }, array_keys($p)));
$sth = $pdo->prepare("UPDATE box SET $fields WHERE boxid=:boxid");
$p[':boxid'] = $id;
try {
$sth->execute($p);
} catch (PDOexception $e) {
$g_error->Add('SQL-Error: '. $e->getMessage());
}
$action = ACT_VIEW;
break;
default:
$g_error->Add(sprintf(_("Unknown function '%s'!"), $submit));
$valid = FALSE;
}
// ========== ACTIONS END =====================================================
require 'header.php';
// ========== PAGE CONTENT ====================================================
if ($action == ACT_DEFAULT):
// ========== VARIANT: default behavior =======================================
echo "
$pagetitle
\n";
$sql = "SELECT boxid, sid, label, color, content, weight, remarks "
. "FROM box "
. "ORDER BY label";
$sth = $pdo->query($sql);
$res = $sth->fetchAll();
echo '', "\n";
echo "\n";
echo "";
echo "| " . _('Label') . " | ";
echo "" . _('Color') . " | ";
echo "" . _('Content') . " | ";
echo '', _('Weight'), " | ";
echo " | ";
echo "
\n";
foreach ($res as $row) {
echo "\n";
echo "| ". $row['label'] ." | \n";
$style = 'background-color:#' . $row['color'];
if (get_color_brightness($row['color']) < 0.4) {
$style .= ';color:white';
}
echo '#'. $row['color'] ." | \n";
echo "". $row['content'] ." | \n";
echo '', format_float($row['weight'], 2), " | \n";
form_action_buttons($g_scriptname, $row['boxid']);
echo "
\n";
}
// Table summary
echo "\n";
echo '| ', sprintf(_('%d records'), count($res)), ' |
', "\n";
echo "\n";
echo "
\n";
form_add_button('box.php');
elseif ($action == ACT_ADD):
// ========== VARIANT: add record =============================================
echo '', _('Add Box'), "
\n";
?>
prepare($sql);
$sth->execute([$id]);
$box = $sth->fetch(PDO::FETCH_OBJ);
echo '', $box->label, "
\n";
echo '', "\n";
echo '| ', _('Content')," | ", $box->content, " |
\n";
echo '| ', _('Color'), ' | #', $box->color, " |
\n";
echo '| ', _('Weight'), ' | ', format_float($box->weight, 2, 'kg'), " |
\n";
echo '| ', _('Remarks')," | ", $box->remarks, " |
\n";
echo "
\n";
form_view_buttons($g_scriptname, $id);
// Inventory in box
// WIP
$sql = "SELECT invid, invname FROM inventory WHERE boxtype='box' AND boxid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$res = $sth->fetchAll();
echo '', _('Inventory in box'), "
\n";
if ($res) {
echo '', "\n";
echo "\n";
echo "";
echo '| ', _('Inventory'), " | ";
echo " | ";
echo "
\n";
foreach ($res as $row) {
echo "\n";
echo "| ", $row['invname'], " | \n";
form_action_buttons('inventory.php', $row['invid']);
echo "
\n";
}
echo "
\n";
} else {
echo '', _('Box is empty'), "
\n";
}
elseif ($action == ACT_EDIT):
// ========== VARIANT: edit single record =====================================
$sql = "SELECT label, content, color, weight, remarks "
. "FROM box "
. "WHERE boxid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$box = $sth->fetch(PDO::FETCH_OBJ);
echo '', _('Edit Box'), "
\n";
?>