Skip to content

Commit

Permalink
Add date field on basic submit form - #78
Browse files Browse the repository at this point in the history
  • Loading branch information
tsumaru720 committed Apr 30, 2023
1 parent 04576ef commit b348c31
Showing 1 changed file with 43 additions and 8 deletions.
51 changes: 43 additions & 8 deletions public/submit.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,54 @@ function is_valid($field) {

$all_pass = 1;
foreach ($_POST as $k => $v) {
if ($k == 'date') { continue; } // Date validation is done later
if (!is_valid($k)) { $all_pass = 0; break; }
}

if (!isset($_POST['date'])) {
$date = date('Y-m-d');
} else {
$date = $_POST['date'];
}

if (date('Y-m-d', strtotime($date)) != "1970-01-01") {
$date = date('Y-m-d', strtotime($date)); //Strip extra bits, if there are any
$SQLDate = $date." 00:00:00";
}

if ((!isset($SQLDate)) || (time() < strtotime($date))) {
$all_pass = 0;
$invalid_date = true;
}

if (!$all_pass) {
?>
<div class="alert alert-danger" role="alert">
Please ensure all fields are filled correctly
</div>
<?php
$display_form = true;
$date = $_POST['date'];
} else {
foreach ($_POST as $k => $v) {
$tmp = explode("_", $k);
$id = $tmp[0];
$asset[$id][$tmp[1]] = $v;
if ($k != 'date') {
$tmp = explode("_", $k);
$id = $tmp[0];
$asset[$id][$tmp[1]] = $v;
} else {
$date = $v;
}
}

foreach ($asset as $id => $v) {
$data = array(':id' => $id, ':deposit' => $v['deposit'], ':latest' => $v['latest']);
$q = $mysql->query("INSERT INTO `asset_log` (`id`, `asset_id`, `epoch`, `deposit_value`, `asset_value`) VALUES (NULL, :id, CURRENT_TIMESTAMP, :deposit, :latest);", $data);
$data = array(':id' => $id, ':deposit' => $v['deposit'], ':latest' => $v['latest']);
if (isset($SQLDate)) {
$data[':date'] = $SQLDate;
$q = $mysql->query("INSERT INTO `asset_log` (`id`, `asset_id`, `epoch`, `deposit_value`, `asset_value`) VALUES (NULL, :id, :date, :deposit, :latest);", $data);
} else {
$q = $mysql->query("INSERT INTO `asset_log` (`id`, `asset_id`, `epoch`, `deposit_value`, `asset_value`) VALUES (NULL, :id, CURRENT_TIMESTAMP, :deposit, :latest);", $data);
}


}
?>
Expand All @@ -95,7 +123,8 @@ function is_valid($field) {
<?php

}

} else {
$date = date('Y-m-d');
}

if ($display_form == true) {
Expand All @@ -107,6 +136,14 @@ function removeElement(elementId) {
}
</script>
<form method="post">

<div class="form-group row justify-content-center mb-5">
<label class="col-sm-2 col-form-label text-right" for="date">Date</label>
<div class="col-4 text-center font-weight-bold">
<input type="date" class="form-control form-control-sm <?php echo isset($invalid_date) ? 'is-invalid' : ''; ?>" name="date" id="date" value="<?php echo $date; ?>" maxlength="10" required>
</div>
</div>

<div class="form-group row">
<div class="col-4 text-center font-weight-bold">
Asset
Expand Down Expand Up @@ -162,5 +199,3 @@ function removeElement(elementId) {

}
?>


0 comments on commit b348c31

Please sign in to comment.