<?php require 'validation.php'; //Initialize a cart as an array $cart = array(); $totalCart = 0; //If the cart is not empty, fill it with the data from the cookie if (!empty($_COOKIE['cart'])) { $cart = json_decode($_COOKIE['cart'], true); } //Check if the products in the POST are empty or not, if not add them to the cart array if (!empty($_POST['product'])) { if (validateItem($_POST['product'])) { //Increment if item exists, else add it to the cookie !empty($cart[$_POST['product']]) ? $cart[$_POST['product']]['qty']++ : ($cart[$_POST['product']] = array('qty' => 1)); if ($cart[$_POST['product']]['qty'] <= 0) { //Check if quantity has been altered with $cart[$_POST['product']]['qty'] = 1; } setcookie('cart', json_encode($cart, true), time() + 86400 * 30); //Update the cookie } } if (!empty($_POST['remove'])) { unset($cart[$_POST['remove']]); setcookie('cart', json_encode($cart, true), time() + 86400 * 30); //Update the cookie } //Because empty() and php is dumb and uses 0 as false and causes conditional problems //assume that if update_product isn't empty then check update if it's 0, if so remove the item if (!empty($_POST['update_product']) && $_POST['update'] == 0) {
$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 {