optimization, begin refactor, basic reports
This commit is contained in:
102
punchlog.php
102
punchlog.php
@@ -17,48 +17,34 @@ else
|
||||
{
|
||||
//********** BEGIN CONTENT **********//
|
||||
$userid = $_SESSION['user_id'];
|
||||
$nowarray = explode("-", date("Y-m-d-H-i"));
|
||||
$timenow = date('Y-m-d H:i');
|
||||
|
||||
// This is to get the current user status - in or out - and the notes and times associated for use in the form
|
||||
$result = $sql->prepare("SELECT punches.id as punchid, users.id as user, punches.intime as intime, punches.outtime as outtime, punches.notes as notes FROM punches INNER JOIN users ON punches.userid = users.id WHERE users.id = $userid ORDER BY punches.id DESC LIMIT 1");
|
||||
$result->execute();
|
||||
$last = $result->fetchObject();
|
||||
|
||||
// Let's build the page - this is the header with current status
|
||||
echo "<h2 class=\"content-subhead\">Advanced Punch</h2>";
|
||||
if(!isset($last->user))
|
||||
{
|
||||
$status = "Out";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!empty($last->outtime)) { $status = "Out"; $statustime = $last->outtime; } else { $status = "In"; $statustime = $last->intime; $punchid = $last->punchid; $notes = $last->notes; }
|
||||
if(!isset($last->user)) {
|
||||
echo "<p>You do not appear to have any punches on record.</p>";
|
||||
$status = "Out";
|
||||
} else {
|
||||
if (!empty($last->outtime)) { $status = "Out"; $statustime = $last->outtime; } else { $status = "In"; $statustime = $last->intime; $punchid = $last->punchid; $notes = $last->notes; }
|
||||
echo "<p>You have been Punched $status since " . date('g:i a \o\n M jS, Y', strtotime($statustime)) . ".</p>";
|
||||
}
|
||||
|
||||
echo "<p>Use this form to enter a specific time on your punch. NOTE: changing the time from the current time will cause a flag on your log for the administrator to review, so we suggest you enter a reason why in the notes field (i.e. forgot punch, working from home, system down, etc).</p>";
|
||||
echo "<form class=\"pure-form pure-form-stacked\" action=\"punchlog.php\" method=\"post\">";
|
||||
echo "<fieldset>";
|
||||
echo "<div class=\"pure-g\">";
|
||||
echo "<div class=\"pure-u-1 pure-u-md-1-3\">";
|
||||
echo "<label for=\"year\">Year</label>";
|
||||
echo "<input type=\"text\" name=\"year\" maxlength=\"4\" placeholder=" . $nowarray[0] . ">";
|
||||
echo "</div>";
|
||||
echo "<div class=\"pure-u-1 pure-u-md-1-3\">";
|
||||
echo "<label for=\"month\">Month</label>";
|
||||
echo "<input type=\"text\" name=\"month\" maxlength=\"2\" placeholder=" . $nowarray[1] . ">";
|
||||
echo "</div>";
|
||||
echo "<div class=\"pure-u-1 pure-u-md-1-3\">";
|
||||
echo "<label for=\"day\">Day</label>";
|
||||
echo "<input type=\"text\" name=\"day\" maxlength=\"2\" placeholder=" . $nowarray[2] . ">";
|
||||
echo "</div>";
|
||||
echo "<div class=\"pure-u-1 pure-u-md-1-3\">";
|
||||
echo "<label for=\"hour\">Hour (24-hr format)</label>";
|
||||
echo "<input type=\"text\" name=\"hour\" maxlength=\"2\" placeholder=" . $nowarray[3] . ">";
|
||||
echo "</div>";
|
||||
echo "<div class=\"pure-u-1 pure-u-md-1-3\">";
|
||||
echo "<label for=\"minute\">Minute</label>";
|
||||
echo "<input type=\"text\" name=\"minute\" maxlength=\"2\" placeholder=" . $nowarray[4] . ">";
|
||||
echo "</div>";
|
||||
echo "<div class=\"pure-u-1 pure-u-md-1-3\">";
|
||||
echo "<label for=\"punchtime\">Punch Time</label>";
|
||||
echo "<input type=\"text\" name=\"punchtime\" placeholder=\"$timenow\" maxlength=\"20\">";
|
||||
echo "<label for=\"notes\">Notes</label>";
|
||||
if (isset($notes)) {
|
||||
echo "<input type=\"text\" name=\"notes\" placeholder=\"Enter notes if needed\" maxlength=\"255\" value=\"$notes\">";
|
||||
echo "</div>";
|
||||
echo "</div>";
|
||||
} else {
|
||||
echo "<input type=\"text\" name=\"notes\" placeholder=\"Enter notes if needed\" maxlength=\"255\">";
|
||||
}
|
||||
echo "<div class=\"pure-controls\">";
|
||||
if ($status=="In") {
|
||||
echo "<button type=\"submit\" class=\"pure-button button-xlarge button-success pure-button-disabled\">Punch IN</button>";
|
||||
@@ -68,39 +54,47 @@ echo "<button type=\"submit\" class=\"pure-button button-xlarge button-success\"
|
||||
echo "<button type=\"submit\" class=\"pure-button button-xlarge button-error pure-button-disabled\">Punch OUT</button>";
|
||||
}
|
||||
echo "</div>";
|
||||
if (!empty($_POST)) {
|
||||
if (!empty($_POST['notes'])) {
|
||||
$p_notes = $_POST['notes'];
|
||||
} else {
|
||||
$p_notes = "";
|
||||
}
|
||||
$p_punchtime = $_POST['year'] . "-" . $_POST['month'] . "-" . $_POST['day'] . " " . $_POST['hour'] . ":" . $_POST['minute'] . ":00";
|
||||
|
||||
|
||||
|
||||
|
||||
// If the posted variables are not empty, we must be trying to insert a new punch. Use the form values to insert new record
|
||||
if (!empty($_POST)) {
|
||||
|
||||
// Is the notes field set? If so, use, otherwise set to null
|
||||
if (isset($_POST['notes'])) { if (!empty($_POST['notes'])) { $p_notes = $_POST['notes']; } else { $p_notes = NULL; } } else { $p_notes = NULL; }
|
||||
|
||||
// Is the punch time field set? If so, use, otherwise set to now
|
||||
if (isset($_POST['punchtime'])) {
|
||||
if (!empty($_POST['punchtime'])) { $p_punchtime = $_POST['punchtime'] . ':00'; $p_modified = "1"; } else { $p_punchtime = $timenow . ':00'; $p_modified = "0"; }
|
||||
} else { $p_punchtime = $timenow . ':00'; $p_modified = "0"; }
|
||||
|
||||
// Is the user currently punched in? If so, insert the punch out record, otherwise, insert a new punch in
|
||||
if ($status=="In") {
|
||||
$query = "UPDATE punches SET outtime = :p_punchtime, notes = :p_notes WHERE id = :p_punchid";
|
||||
$stmt = $sql->prepare($query);
|
||||
$stmt->execute(array(
|
||||
$query = "UPDATE punches SET outtime = :p_punchtime, notes = :p_notes, modified = :p_modified WHERE id = :p_punchid";
|
||||
$stmt = $sql->prepare($query);
|
||||
$stmt->execute(array(
|
||||
':p_punchid' => $punchid,
|
||||
':p_notes' => $p_notes,
|
||||
':p_punchtime' => $p_punchtime
|
||||
':p_punchtime' => $p_punchtime,
|
||||
':p_modified' => $p_modified,
|
||||
));
|
||||
} else {
|
||||
$query = "INSERT INTO punches (userid, notes, intime) VALUES (:p_userid, :p_notes, :p_punchtime)";
|
||||
$stmt = $sql->prepare($query);
|
||||
$stmt->execute(array(
|
||||
':p_userid' => $_SESSION['user_id'],
|
||||
$query = "INSERT INTO punches (userid, notes, intime, modified) VALUES (:p_userid, :p_notes, :p_punchtime, :p_modified)";
|
||||
$stmt = $sql->prepare($query);
|
||||
$stmt->execute(array(
|
||||
':p_userid' => $_SESSION['user_id'],
|
||||
':p_notes' => $p_notes,
|
||||
':p_punchtime' => $p_punchtime
|
||||
':p_punchtime' => $p_punchtime,
|
||||
':p_modified' => $p_modified,
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// And then send user back to this page to see the updates
|
||||
header('Location: '.$_SERVER['PHP_SELF']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Close out the form...
|
||||
echo "</fieldset>";
|
||||
echo "</form>";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user