major updates to functions, split off from config file, etc

This commit is contained in:
2015-02-20 05:16:28 -05:00
parent d74c62649e
commit 45911a2da1
10 changed files with 201 additions and 248 deletions

146
index.php
View File

@@ -1,112 +1,56 @@
<?php
session_start();
require_once("config.inc.php");
require_once($yaptc_inc . "functions.inc.php");
$yaptc_pagename = "Home";
require_once($yaptc_inc . "header.inc.php");
require_once($yaptc_inc . "menu.inc.php");
if (getSessionStatus() == false):
killSession();
else:
//********** BEGIN CONTENT **********// ?>
else: ?>
<!-- ********** BEGIN CONTENT ********** -->
<?php $punchStatus = getPunchStatus($yaptc_db, $_SESSION['user_id']); ?>
<h2 class="content-subhead">Current Status</h2>
<?php if (!isset($punchStatus['0'])): $status = "Out"; ?>
<p>You do not appear to have any punches on record.</p>
<?php else:
if (!empty($punchStatus['3'])): $status = "Out"; $statustime = $punchStatus['3'];
else: $status = "In"; $statustime = $punchStatus['2']; $punchid = $punchStatus['0']; $notes = $punchStatus['4'];
endif; ?>
<p>You have been Punched <?php echo $status; ?> since <?php echo date('g:i a \o\n M jS, Y', strtotime($statustime)); ?>.</p>
<?php endif; ?>
<h2 class="content-subhead">Quick Punch</h2>
<p>Clicking the button below will immediately enter a new punch for you depending on your current status. Any notes you enter will be attached to the punch for your administrator to review.</p>
<form class="pure-form pure-form-stacked" action="index.php" method="post">
<fieldset>
<input class="pure-input-1" type="text" name="notes" placeholder="Enter notes if needed" maxlength="255" value="<?php if (isset($notes)): echo $notes; endif; ?>">
<div class="pure-controls">
<?php if ($status == "In"): ?>
<button type="submit" class="pure-button button-xlarge button-success pure-button-disabled">Punch IN</button>
<button type="submit" class="pure-button button-xlarge button-error">Punch OUT</button>
<?php elseif ($status == "Out"): ?>
<button type="submit" class="pure-button button-xlarge button-success">Punch IN</button>
<button type="submit" class="pure-button button-xlarge button-error pure-button-disabled">Punch OUT</button>
<?php endif; ?>
</div>
</fieldset>
</form>
<?php
$userid = $_SESSION['user_id'];
if (!empty($_POST)):
if (isset($_POST['notes'])):
if (!empty($_POST['notes'])): $notes = $_POST['notes'];
else: $notes = NULL;
endif;
else: $notes = NULL;
endif;
if ($status == "In"): punchOut($yaptc_db, $punchid, $notes);
elseif ($status == "Out"): punchIn($yaptc_db, $_SESSION['user_id'], $notes);
endif;
header('Location: ' . $_SERVER['PHP_SELF']);
endif; ?>
// This is to get the current user status - in or out - and the notes and times associated for use in the form
$result = $yaptc_db->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\">Current Status</h2>";
if (!isset($last->user)) {
echo "<p>You do not appear to have any punches on record.</p>";
$status = "Out";
} //!isset($last->user)
else {
if (!empty($last->outtime)) {
$status = "Out";
$statustime = $last->outtime;
} //!empty($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 "<h2 class=\"content-subhead\">Quick Punch</h2>";
echo "<p>Clicking the button below will immediately enter a new punch for you depending on your current status. Any notes you enter will be attached to the punch for your administrator to review.</p>";
echo "<form class=\"pure-form pure-form-stacked\" action=\"index.php\" method=\"post\">";
echo "<fieldset>";
if (isset($notes)) {
echo "<input class=\"pure-input-1\" type=\"text\" name=\"notes\" placeholder=\"Enter notes if needed\" maxlength=\"255\" value=\"$notes\">";
} //isset($notes)
else {
echo "<input class=\"pure-input-1\" 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>";
echo "<button type=\"submit\" class=\"pure-button button-xlarge button-error\">Punch OUT</button>";
} //$status == "In"
else {
echo "<button type=\"submit\" class=\"pure-button button-xlarge button-success\">Punch IN</button>";
echo "<button type=\"submit\" class=\"pure-button button-xlarge button-error pure-button-disabled\">Punch OUT</button>";
}
echo "</div>";
// 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'];
} //!empty($_POST['notes'])
else {
$p_notes = NULL;
}
} //isset($_POST['notes'])
else {
$p_notes = NULL;
}
// 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 = NOW(), notes = :p_notes WHERE id = :p_punchid";
$stmt = $yaptc_db->prepare($query);
$stmt->execute(array(
':p_punchid' => $punchid,
':p_notes' => $p_notes
));
} //$status == "In"
else {
$query = "INSERT INTO punches (userid, notes, intime) VALUES (:p_userid, :p_notes, NOW())";
$stmt = $yaptc_db->prepare($query);
$stmt->execute(array(
':p_userid' => $_SESSION['user_id'],
':p_notes' => $p_notes
));
}
// And then send user back to this page to see the updates
header('Location: ' . $_SERVER['PHP_SELF']);
} //!empty($_POST)
// Close out the form...
echo "</fieldset>";
echo "</form>";
?>
<?php //********** END CONTENT **********//
endif;
require_once($yaptc_inc . "footer.inc.php");
?>
<!-- ********** END CONTENT ********** -->
<?php endif; require_once($yaptc_inc . "footer.inc.php"); ?>