-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.php
More file actions
216 lines (193 loc) · 9.03 KB
/
clock.php
File metadata and controls
216 lines (193 loc) · 9.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
include('includes/load.php');
if(isset($_GET["method"])) {
if($_GET["method"] == "in") {
$db = getMySQL();
$time = time();
if(isset($_POST["proj"])) {
$ii = intval($_POST["proj"]);
}else {
$ii = intval($_GET["proj"]);
}
$res = $db->query("INSERT INTO actions (time, action, user, project) VALUES ($time, 1, {$_SESSION["user"]->id}, $ii)");
header('location: /clock.php');
}else if($_GET["method"] == "out") {
$db = getMySQL();
$time = time();
$ii = intval($_GET['proj']);
$res = $db->query("INSERT INTO actions (time, action, user, project) VALUES ($time, 0, {$_SESSION["user"]->id}, $ii)");
header('location: /clock.php');
}else if($_GET["method"] == "project") {
if($_POST) {
$db = getMySQL();
$name = $db->escape_string($_POST['name']);
$res = $db->query("INSERT INTO projects (projectname, projectowner) VALUES ('$name', {$_SESSION["user"]->id})");
if(!empty($_POST["check"])) {
header('location: /clock.php?method=in&proj='.$db->insert_id);
exit;
}
}
header('location: /clock.php');
}
}
function lastAction() {
if(isset($data["clocked"])) {
return $data["clocked"];
}else {
$db = getMySQL();
$res = $db->query("SELECT * FROM actions WHERE user={$_SESSION["user"]->id} ORDER BY time DESC LIMIT 1"); //Pull last action from DB
if($obj = $res->fetch_object())
return ["id"=>$obj->actionid, "time"=>$obj->time, "project"=>$obj->project, "action"=>$obj->action];
return ["action"=>0];
}
}
function timeToString($seconds) {
$ret = [];
if($seconds >= 86400) {$ret["days"] = floor($seconds/86400); $seconds = $seconds%86400;}
if($seconds >= 3600) {$ret["hours"] = floor($seconds/3600); $seconds = $seconds%3600;}
if($seconds >= 60) {$ret["minutes"] = floor($seconds/60); $seconds = $seconds%60;}
if($seconds > 0) {$ret["seconds"] = $seconds;}
$out = "";
foreach($ret as $k=>$v) {
if($v > 1) {
$out .= " ".$v." ".$k."s";
}else {
$out .= " ".$v." ".$k;
}
}
$ret["string"] = trim($out);
return $ret;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>PunchIO</title>
<link href="/assets/css/bootstrap.min.css" rel="stylesheet">
<body>
<?php getNavbar(); ?>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading"><center>Time Clock</center></div>
<div class="panel-body" style="text-align: center;">
<?php $data = lastAction(); $clocked = $data["action"] == 1; ?>
<h1>You Are Currently Clocked: <?php if($clocked){echo '<span style="color: green;">IN</span>';}else{echo '<span style="color: red;">OUT</span>';} ?></h1><br />
<?php
if($clocked) {
?>
<h3>Clocked Into Project: <?php $db = getMySQL(); $res = $db->query("SELECT * FROM projects WHERE projectid={$data["project"]}"); $dat = $res->fetch_object(); echo $dat->projectname; ?></h3>
<h3>Clocked in For: <span class="time"></span></h3>
<center><a href="clock.php?method=out&proj=<?php echo $data["project"]; ?>" class="btn btn-danger btn-lg" style="width: 95%;">Clock Out</a></center>
<?php
}else {
?>
<center>
<div style="max-width: 360px; min-width: 200px;">
<form method="post" action="clock.php?method=in" id="1">
<div>
<div class="input-group" style="margin-bottom: 5px;">
<span class="input-group-addon">Project</span>
<select class="form-control" name="proj">
<option value="-1">Create a New Project</option>
<?php
$db = getMySQL();
$res = $db->query("SELECT * FROM projects WHERE projectowner={$_SESSION["user"]->id} ORDER BY projectname ASC");
while($obj = $res->fetch_object()) {
echo "<option value='{$obj->projectid}'>{$obj->projectname}</option>";
}
?>
</select>
</div>
<input type="submit" class="btn btn-success form-control" value="Clock In">
</div>
</form>
</div>
</center>
<?php
}
?>
</div>
</div>
</div>
</div>
</div> <!-- /container -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="/assets/js/bootstrap.min.js"></script>
<?php if($clocked) {
?>
<script>
var startTime = <?php echo $data["time"]*1000; ?>;
var offset = -1 * ((new Date()) - <?php echo time()*1000; ?>);
function display() {
var endTime = new Date();
var timeDiff = (endTime - startTime) + offset;
timeDiff /= 1000;
var seconds = Math.round(timeDiff % 60);
timeDiff = Math.floor(timeDiff / 60);
var minutes = Math.round(timeDiff % 60);
timeDiff = Math.floor(timeDiff / 60);
var hours = Math.round(timeDiff % 24);
$(".time").html(hours + " hours, " + minutes + " minutes, and " + seconds + " seconds");
setTimeout(display, 1000);
}
display();
</script>
<?php
}else {
?>
<div id="wrap" style="background: rgba(0,0,0,.3); height: 100vh; width: 100vw; postion: absolute; left: 0px; top: 0px; display: none; z-index: 100000;">
<div class="panel panel-success" style="display: none; width: 350px; height: auto;" id="popup">
<div class="panel-heading">Create a new project<a class="pull-right" style="cursor: pointer;" id="close">X</a></div>
<div class="panel-body">
<form action="clock.php?method=project" method="post">
<input type="text" name="name" id="nme" placeholder="Project Name" class="form-control" style="margin-bottom: 5px"/>
<center><input type="checkbox" name="check" value="check" checked> Clock In Automatically<br /></center>
<input type="submit" value="Create Project" class="btn btn-success form-control" style="margin-top: 3px">
</form>
</div>
</div>
</div>
<script>
jQuery.fn.center = function (boo) {
this.css("position","absolute");
if(boo) {
this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) * .25) +
$(window).scrollTop()) + "px");
}else {
this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) +
$(window).scrollTop()) + "px");
}
this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) +
$(window).scrollLeft()) + "px");
return this;
}
$(document).keyup(function(e) {
if (e.keyCode == 27) { $('#close').click(); } // esc
});
$('#popup').center(true);
$('#wrap').center(false);
$('#close').click(function() {
$("#popup").fadeOut();
$("#wrap").fadeOut();
});
$('#1').submit(function(e) {
if($('select').val() == -1) {
e.preventDefault();
$("#popup").fadeIn();
$("#wrap").fadeIn();
$("#nme").focus();
}
});
</script>
<?php
}
?>
</body>
</html>