forked from rogermoka/Timesheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple.php
1203 lines (983 loc) · 43 KB
/
simple.php
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
//$Header: /cvsroot/tsheet/timesheet.php/simple.php,v 1.7 2005/05/23 05:39:39 vexil Exp $
error_reporting(E_ALL);
ini_set('display_errors', true);
// Authenticate
require("class.AuthenticationManager.php");
require("class.CommandMenu.php");
require("class.Pair.php");
if (!$authenticationManager->isLoggedIn()) {
Header("Location: login.php?redirect=$_SERVER[PHP_SELF]");
exit;
}
// Connect to database.
$dbh = dbConnect();
$contextUser = strtolower($_SESSION['contextUser']);
$loggedInUser = strtolower($_SESSION['loggedInUser']);
if (empty($loggedInUser))
errorPage("Could not determine the logged in user");
if (empty($contextUser))
errorPage("Could not determine the context user");
//define the command menu
include("timesheet_menu.inc");
//bug fix - we must display all projects
$proj_id = 0;
// Check project assignment.
if ($proj_id != 0) { // id 0 means 'All Projects'
list($qh, $num) = dbQuery("SELECT * FROM $ASSIGNMENTS_TABLE where proj_id='$proj_id' and username='$contextUser'");
if ($num < 1)
errorPage("You cannot access this project, because you are not assigned to it.");
}
else
$task_id = 0;
//a useful constant
define("A_DAY", 24 * 60 * 60);
//get the passed date (context date)
$todayDate = mktime(0, 0, 0,$month, $day, $year);
$todayYear = date("Y", $todayDate);
$todayMonth = date("n", $todayDate);
$todayDay = date("j", $todayDate);
$dateValues = getdate($todayDate);
$todayDayOfWeek = $dateValues["wday"];
//the day the week should start on: 0=Sunday, 1=Monday
$startDayOfWeek = getWeekStartDay();
$daysToMinus = $todayDayOfWeek - $startDayOfWeek;
if ($daysToMinus < 0)
$daysToMinus += 7;
//work out the start date by minusing enough seconds to make it the start day of week
$startDate = $todayDate - $daysToMinus * A_DAY;
$startYear = date("Y", $startDate);
$startMonth = date("n", $startDate);
$startDay = date("j", $startDate);
//work out the end date by adding 7 days
$endDate = $startDate + 7 * A_DAY;
$endYear = date("Y", $endDate);
$endMonth = date("n", $endDate);
$endDay = date("j", $endDate);
// Calculate the previous week
$previousWeekDate = $todayDate - A_DAY * 7;
$previousWeekYear = date("Y", $previousWeekDate);
$previousWeekMonth = date("n", $previousWeekDate);
$previousWeekDay = date("j", $previousWeekDate);
//calculate next week
$nextWeekDate = $todayDate + A_DAY * 7;
$nextWeekYear = date("Y", $nextWeekDate);
$nextWeekMonth = date("n", $nextWeekDate);
$nextWeekDay = date("j", $nextWeekDate);
//get the timeformat
list($qh2, $numq) = dbQuery("select timeformat from $CONFIG_TABLE where config_set_id = '1'");
$configData = dbResult($qh2);
//build the database query
$query = "SELECT date_format(start_time,'%d') AS day_of_month, ";
if ($configData["timeformat"] == "12")
$query .= "date_format(end_time, '%l:%i%p') AS endd, date_format(start_time, '%l:%i%p') AS start, ";
else
$query .= "date_format(end_time, '%k:%i') AS endd, date_format(start_time, '%k:%i') AS start, ";
$query .= "unix_timestamp(end_time) - unix_timestamp(start_time) AS diff_sec, ".
"end_time AS end_time_str, ".
"start_time AS start_time_str, ".
"unix_timestamp(start_time) AS start_time, ".
"unix_timestamp(end_time) AS end_time, ".
"$PROJECT_TABLE.title AS projectTitle, " .
"$TASK_TABLE.name AS taskName, " .
"$TIMES_TABLE.proj_id, " .
"$TIMES_TABLE.task_id " .
"FROM $TIMES_TABLE, $TASK_TABLE, $PROJECT_TABLE WHERE " .
"$PROJECT_TABLE.proj_id = $TIMES_TABLE.proj_id AND " .
"uid='$contextUser' AND ";
if ($proj_id > 0) //otherwise want all records no matter what project
$query .= "$TIMES_TABLE.proj_id=$proj_id AND ";
$query .= "$TASK_TABLE.task_id = $TIMES_TABLE.task_id AND ".
"((start_time >= '$startYear-$startMonth-$startDay 00:00:00' AND " .
"start_time < '$endYear-$endMonth-$endDay 00:00:00') ".
"OR (end_time >= '$startYear-$startMonth-$startDay 00:00:00' AND " .
"end_time < '$endYear-$endMonth-$endDay 00:00:00') ".
"OR (start_time < '$startYear-$startMonth-$startDay 00:00:00' AND end_time >= '$endYear-$endMonth-$endDay 00:00:00')) ".
"ORDER BY proj_id, start_time";
?>
<html>
<head>
<title>Simple Weekly Timesheet for <? echo "$contextUser" ?></title>
<?
include ("header.inc");
?>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('#emailButton').click(function(){
var htmlText = jQuery('#contentFetch').html();
var sheets = jQuery('#sheetsExists').data('sheets');
var mail = jQuery(this).data('mail');
if(sheets == 0){
alert("You haven't declared your hours yet.");
}else{
$.ajax({
url: "sendMail.php?action=sendmail",
data: {
data : htmlText,
mail : mail,
},
type: 'post',
success: function(result){
alert(result);
location.reload();
}
});
}
});
});
</script>
<script language="Javascript">
//define the hash table
var projectTasksHash = {};
<?php
//get all of the projects and put them into the hashtable
$getProjectsQuery = "SELECT $PROJECT_TABLE.proj_id, $PROJECT_TABLE.title, $PROJECT_TABLE.client_id, ".
"$CLIENT_TABLE.client_id, $CLIENT_TABLE.organisation FROM ".
"$PROJECT_TABLE, $ASSIGNMENTS_TABLE, $CLIENT_TABLE where ".
"$PROJECT_TABLE.proj_id=$ASSIGNMENTS_TABLE.proj_id AND ".
"$ASSIGNMENTS_TABLE.username='$contextUser' AND ".
"$PROJECT_TABLE.client_id=$CLIENT_TABLE.client_id ".
"ORDER BY $CLIENT_TABLE.organisation, $PROJECT_TABLE.title";
list($qh3, $num3) = dbQuery($getProjectsQuery);
//iterate through results
for ($i=0; $i<$num3; $i++) {
//get the current record
$data = dbResult($qh3, $i);
print("projectTasksHash['" . $data["proj_id"] . "'] = {};\n");
print("projectTasksHash['" . $data["proj_id"] . "']['name'] = '". addslashes($data["title"]) . "';\n");
print("projectTasksHash['" . $data["proj_id"] . "']['clientId'] = '". $data["client_id"] . "';\n");
print("projectTasksHash['" . $data["proj_id"] . "']['clientName'] = '". addslashes($data["organisation"]) . "';\n");
print("projectTasksHash['" . $data["proj_id"] . "']['tasks'] = {};\n");
}
//get all of the tasks and put them into the hashtable
$getTasksQuery = "SELECT $TASK_TABLE.proj_id, $TASK_TABLE.task_id, $TASK_TABLE.name from ".
"$TASK_TABLE, $TASK_ASSIGNMENTS_TABLE where ".
"$TASK_TABLE.task_id = $TASK_ASSIGNMENTS_TABLE.task_id AND ".
"$TASK_ASSIGNMENTS_TABLE.username='$contextUser'";
list($qh4, $num4) = dbQuery($getTasksQuery);
//iterate through results
for ($i=0; $i<$num4; $i++) {
//get the current record
$data = dbResult($qh4, $i);
print("if (projectTasksHash['" . $data["proj_id"] . "'] != null)\n");
print(" projectTasksHash['" . $data["proj_id"] . "']['tasks']['" . $data["task_id"] . "'] = '" . addslashes($data["name"]) . "';\n");
}
?>
//function to populate existing rows with project and task names and select the right one in each
function populateExistingSelects() {
//get the number of existing rows
var existingRows = parseInt(document.getElementById('existingRows').value);
//alert('There are ' + existingRows + ' existing rows');
//iterate to plus one to do the additional row
for (i=0; i<=existingRows; i++) {
//alert('existing row ' + i);
//get the project and task id for this row
var projectId = document.getElementById('project_row' + i).value;
var taskId = document.getElementById('task_row' + i).value;
//get the selects
var projectSelect = document.getElementById('projectSelect_row' + i);
var taskSelect = document.getElementById('taskSelect_row' + i);
//add None to the selects
projectSelect.options[projectSelect.options.length] = new Option('None', '-1');
taskSelect.options[taskSelect.options.length] = new Option('None', '-1');
//add the projects
var clientId = -1;
for (key in projectTasksHash) {
if (projectTasksHash[key]['clientId'] != clientId) {
projectSelect.options[projectSelect.options.length] = new Option('[' + projectTasksHash[key]['clientName'] + ']', -1);
clientId = projectTasksHash[key]['clientId'];
}
projectSelect.options[projectSelect.options.length] = new Option(String.fromCharCode(160, 160) + projectTasksHash[key]['name'], key);
if (key == projectId)
projectSelect.options[projectSelect.options.length-1].selected = true;
}
if (projectId != -1) {
//add the tasks
var thisProjectTasks = projectTasksHash[projectId]['tasks'];
for (taskKey in thisProjectTasks) {
taskSelect.options[taskSelect.options.length] = new Option(thisProjectTasks[taskKey], taskKey);
if (taskKey == taskId)
taskSelect.options[taskSelect.options.length-1].selected = true;
}
}
}
}
function populateTaskSelect(row, projectId, selectedTaskId) {
//get the task select for this row
var taskSelect = document.getElementById('taskSelect_row' + row);
//add the tasks
var thisProjectTasks = projectTasksHash[projectId]['tasks'];
for (taskKey in thisProjectTasks) {
taskSelect.options[taskSelect.options.length] = new Option(thisProjectTasks[taskKey], taskKey);
if (taskKey == selectedTaskId)
taskSelect.options[taskSelect.options.length-1].selected = true;
}
}
function clearTaskSelect(row) {
taskSelect = document.getElementById('taskSelect_row' + row);
for (i=1; i<taskSelect.options.length; i++)
taskSelect.options[i] = null;
//set the length back to 1
taskSelect.options.length = 1;
//select the 'None' option
taskSelect.options[0].selected = true;
onChangeTaskSelectRow(row);
}
function rowFromIdStr(idStr) {
var pos1 = idStr.indexOf("row") + 3;
var pos2 = idStr.indexOf('_', pos1);
if (pos2 == -1)
pos2 = idStr.length;
return parseInt(idStr.substring(pos1, pos2));
}
function colFromIdStr(idStr) {
var pos1 = idStr.indexOf("col") + 3;
var pos2 = idStr.indexOf('_', pos1);
if (pos2 == -1)
pos2 = idStr.length;
return parseInt(idStr.substring(pos1, pos2));
}
function onChangeProjectSelect(idStr) {
row = rowFromIdStr(idStr);
clearTaskSelect(row);
//get the project id
var projectSelect = document.getElementById('projectSelect_row' + row);
var projectId = projectSelect.options[projectSelect.selectedIndex].value;
if (projectId != -1)
//populate the select with tasks for this project
populateTaskSelect(row, projectId);
setDirty();
}
function onChangeTaskSelect(idStr) {
onChangeTaskSelectRow(rowFromIdStr(idStr));
}
function onChangeTaskSelectRow(row) {
taskSelect = document.getElementById('taskSelect_row' + row);
if (taskSelect.options[0].selected == true) {
//disable fields
for (i=1; i<=7; i++) {
document.getElementById('hours_row' + row + '_col' + i).disabled = true;
document.getElementById('mins_row' + row + '_col' + i).disabled = true;
}
}
else {
//get the total number of rows
var totalRows = parseInt(document.getElementById('totalRows').value);
//alert('change task droplist on row ' + row + ', totalRows=' + totalRows);
if (row == (totalRows-1)) {
//get the row to copy
//$("#row"+row).children("select").select2("destroy");
$("#row"+row+" .newSelect").select2("destroy");
var tempNode = document.getElementById('row' + row);
//clone the row
var newNode = tempNode.cloneNode(true);
//setup the pattern to match
var rowRegex = new RegExp("row(\\d+)");
//iterate through with dom and replace all name and id attributes with regexp
replaceIdAndNameAttributes(newNode, rowRegex, totalRows);
//increment totalRows by one
//alert('totalRows was ' + document.getElementById('totalRows').value);
document.getElementById('totalRows').value = parseInt(document.getElementById('totalRows').value) + 1;
//alert('totalRows is now ' + document.getElementById('totalRows').value);
//get the totals node
var totalsNode = document.getElementById('totalsRow');
//insert the new node before the totals node
totalsNode.parentNode.insertBefore(newNode, totalsNode);
$("#row"+row+" .newSelect").select2();
$("#row"+totalRows+" .newSelect").select2();
//clear the task select
clearTaskSelect(totalRows);
}
//enable fields
for (i=1; i<=7; i++) {
document.getElementById('hours_row' + row + '_col' + i).disabled = false;
document.getElementById('mins_row' + row + '_col' + i).disabled = false;
}
}
setDirty();
}
function replaceIdAndNameAttributes(node, rowRegex, rowNumber) {
while (node != null) {
if (node.getAttribute != null && node.getAttribute("id") != null)
node.setAttribute("id", node.getAttribute("id").replace(rowRegex, "row" + rowNumber));
if (node.getAttribute != null && node.getAttribute("name") != null)
node.setAttribute("name", node.getAttribute("name").replace(rowRegex, "row" + rowNumber));
//call this function recursively for children
if (node.firstChild != null && node.firstChild.tagName != null)
replaceIdAndNameAttributes(node.firstChild, rowRegex, rowNumber);
//do the same for the next sibling
node = node.nextSibling;
}
}
function recalculateRowCol(idStr) {
recalculateRow(rowFromIdStr(idStr));
recalculateCol(colFromIdStr(idStr));
setDirty();
}
function recalculateRow(row) {
var totalMins = 0;
for (i=1; i<=7; i++) {
hours = parseInt(document.getElementById("hours_row" + row + "_col" + i).value);
if (!isNaN(hours))
totalMins += hours * 60;
mins = parseInt(document.getElementById("mins_row" + row + "_col" + i).value);
if (!isNaN(mins))
totalMins += mins;
}
hours = Math.floor(totalMins / 60);
mins = totalMins - (hours * 60);
//get the total cell
var totalCell = document.getElementById("subtotal_row" + row);
totalCell.innerHTML = '' + hours + 'h ' + mins + 'm';
}
function recalculateCol(col) {
//get the total number of rows
var totalRows = parseInt(document.getElementById('totalRows').value);
var totalMins = 0;
for (i=0; i<totalRows; i++) {
hours = parseInt(document.getElementById("hours_row" + i + "_col" + col).value);
if (!isNaN(hours))
totalMins += hours * 60;
mins = parseInt(document.getElementById("mins_row" + i + "_col" + col).value);
if (!isNaN(mins))
totalMins += mins;
}
hours = Math.floor(totalMins / 60);
mins = totalMins - (hours * 60);
//get the total cell
var totalCell = document.getElementById("subtotal_col" + col);
totalCell.innerHTML = '' + hours + 'h ' + mins + 'm';
recalculateGrandTotal();
}
function recalculateGrandTotal() {
var totalMins = 0;
for (i=1; i<=7; i++) {
var currentInnerHTML = document.getElementById("subtotal_col" + i).innerHTML;
//get the hours
hPos = currentInnerHTML.indexOf('h');
hours = parseInt(currentInnerHTML.substring(0, hPos));
if (!isNaN(hours))
totalMins += hours * 60;
//get the minutes
mPos = currentInnerHTML.indexOf('m');
mins = parseInt(currentInnerHTML.substring(hPos+7, mPos));
if (!isNaN(mins))
totalMins += mins;
}
hours = Math.floor(totalMins / 60);
mins = totalMins - (hours * 60);
//get the grand total cell
var grandTotalCell = document.getElementById("grand_total");
grandTotalCell.innerHTML = '' + hours + 'h ' + mins + 'm';
}
function setDirty() {
document.getElementById("saveButton").disabled = false;
}
function validate() {
//get the total number of rows
var totalRows = parseInt(document.getElementById('totalRows').value);
//iterate through rows
for (i=0; i<totalRows; i++) {
//iterate through cols
for (j=1; j<=7; j++) {
hours = parseInt(document.getElementById("hours_row" + i + "_col" + j).value);
if (document.getElementById("hours_row" + i + "_col" + j).value != "" && isNaN(hours) || hours > 23) {
alert('The hours field in row ' + i + ' column ' + j + ' must be a number between 0 and 23.');
document.getElementById("hours_row" + i + "_col" + j).focus();
return;
}
mins = parseInt(document.getElementById("mins_row" + i + "_col" + j).value);
if (document.getElementById("mins_row" + i + "_col" + j).value != "" && isNaN(mins) || mins > 59) {
alert('The minutes field in row ' + i + ' column ' + j + ' must be a number between 0 and 59.');
document.getElementById("mins_row" + i + "_col" + j).focus();
return;
}
}
}
document.theForm.submit();
}
</script>
</head>
<?php
echo "<body width=\"100%\" height=\"100%\" onLoad=\"populateExistingSelects();\"";
include ("body.inc");
if (isset($popup))
echo "onLoad=window.open(\"popup.php?proj_id=$proj_id&task_id=$task_id\",\"Popup\",\"location=0,directories=no,status=no,menubar=no,resizable=1,width=420,height=205\");";
echo ">\n";
include ("banner.inc");
?>
<form name="theForm" action="simple_action.php" method="post">
<input type="hidden" name="year" value=<? echo $year; ?>>
<input type="hidden" name="month" value=<? echo $month; ?>>
<input type="hidden" name="day" value=<? echo $day; ?>>
<input type="hidden" name="startYear" value=<? echo $startYear; ?>>
<input type="hidden" name="startMonth" value=<? echo $startMonth; ?>>
<input type="hidden" name="startDay" value=<? echo $startDay; ?>>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="100%" class="face_padding_cell">
<!-- include the timesheet face up until the heading start section -->
<? include("timesheet_face_part_1.inc"); ?>
<table width="100%" border="0">
<tr>
<td align="left" nowrap class="outer_table_heading">
Week Start: <? echo date('D F j, Y',mktime(0,0,0,$startMonth, $startDay, $startYear)); ?>
</td>
<td align="right" nowrap>
<a href="<? echo $_SERVER["PHP_SELF"]; ?>?proj_id=<?echo $proj_id; ?>&task_id=<? echo $task_id; ?>&year=<?echo $previousWeekYear ?>&month=<? echo $previousWeekMonth ?>&day=<? echo $previousWeekDay ?>" class="outer_table_action">Prev</a>
<a href="<? echo $_SERVER["PHP_SELF"]; ?>?proj_id=<? echo $proj_id; ?>&task_id=<? echo $task_id; ?>&year=<? echo $nextWeekYear ?>&month=<? echo $nextWeekMonth ?>&day=<? echo $nextWeekDay ?>" class="outer_table_action">Next</a>
</td>
<td align="right" nowrap>
<?php
/* Custom code : For disable fields after mail report send */
$mailDate = date('j',mktime(0,0,0,$startMonth, $startDay, $startYear));
$mailmonth = date('m',mktime(0,0,0,$startMonth, $startDay, $startYear));
$mailyear = date('Y',mktime(0,0,0,$startMonth, $startDay, $startYear));
$checkDate = $mailDate."-".$mailmonth."-".$mailyear;
for ($i=0; $i < 7; $i++) { $finalArr[] = $mailDate++."-".$mailmonth."-".$mailyear; }
$whereVal = '.*;s:[0-9]+:"'.$checkDate.'"*';
$condition = "WHERE timesheet_id REGEXP '".$whereVal."'";
list($mailList, $numq) = dbQuery("select * from $EMAIL_TABLE $condition AND user = '$contextUser'");
$mailData = dbResult($mailList);
$disable = "";
if ( !empty($mailData) ) {
$disable = 'disabled="true"';?>
<input type="button" value="Send email" disabled />
<input type="button" value="Save Changes" disabled />
<? }else{ ?>
<input type="button" data-mail='<?= serialize($finalArr); ?>' name="emailButton" id="emailButton" value="Send email" />
<input type="button" name="saveButton" id="saveButton" value="Save Changes" disabled="true" onClick="validate();" />
<? } ?>
</td>
</tr>
</table>
<!-- include the timesheet face up until the heading start section -->
<? include("timesheet_face_part_2.inc"); ?>
<table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" class="outer_table">
<tr>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="table_body">
<tr class="inner_table_head">
<td class="inner_table_column_heading" align="center">Project / Task</td>
<td align="center" width="2"> </td>
<?php
//print the days of the week
$currentDayDate = $startDate;
for ($i=0; $i<7; $i++) {
$currentDayStr = strftime("%a", $currentDayDate);
$currentDayDate += A_DAY;
print
"<td align=\"center\" width=\"65\">" .
"<table width=\"65\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>" .
"<td class=\"inner_table_column_heading\" align=\"center\">" .
"$currentDayStr" .
"</td></tr></table></td>\n";
}
?>
<td align="center" width="2"> </td>
<td class="inner_table_column_heading" align="center" width="50">Total</td>
</tr>
<tr>
<?php
//debug
//$startDateStr = strftime("%D", $startDate);
//$endDateStr = strftime("%D", $endDate);
//print "<p>WEEK start: $startDateStr WEEK end: $endDateStr</p>";
class TaskInfo extends Pair {
var $projectId;
var $projectTitle;
var $taskName;
function TaskInfo($value1, $value2, $projectId, $projectTitle, $taskName) {
parent::Pair($value1, $value2);
$this->projectId = $projectId;
$this->projectTitle = $projectTitle;
$this->taskName = $taskName;
}
}
// Get the Weekly data.
list($qh5, $num5) = dbQuery($query);
//print "<p>Query: $query </p>";
//print "<p>there were $num3 results</p>";
//we're going to put the data into an array of
//different (unique) TASKS
//which has an array of DAYS (7) which has
//and array of size 4:
// -index 0 is task entries array for tasks which started on a previous day and finish on a following day
// -index 1 is task entries array for tasks which started on a previous day and finish today
// -index 2 is task entreis array for tasks which started and finished today
// -index 3 is task entries array for tasks which started today and finish on a following day
$structuredArray = array();
$previousTaskId = -1;
$currentTaskId = -1;
//iterate through results
for ($i=0; $i<$num5; $i++) {
//get the record for this task entry
$data = dbResult($qh5,$i);
//Due to a bug in mysql with converting to unix timestamp from the string,
//we are going to use php's strtotime to make the timestamp from the string.
//the problem has something to do with timezones.
$data["start_time"] = strtotime($data["start_time_str"]);
$data["end_time"] = strtotime($data["end_time_str"]);
//get the current task properties
$currentTaskId = $data["task_id"];
$currentTaskStartDate = $data["start_time"];
$currentTaskEndDate = $data["end_time"];
$currentTaskName = stripslashes($data["taskName"]);
$currentProjectTitle = stripslashes($data["projectTitle"]);
$currentProjectId = $data["proj_id"];
//debug
//print "<p>taskId:$currentTaskId '$data[taskName]', start time:$data[start_time_str], end time:$data[end_time_str]</p>";
//find the current task id in the array
$taskCount = count($structuredArray);
unset($matchedPair);
for ($j=0; $j<$taskCount; $j++) {
//does its value1 (the task id) match?
if ($structuredArray[$j]->value1 == $currentTaskId) {
//store the pair we matched with
$matchedPair = &$structuredArray[$j];
//debug
//print "<p> found existing matched pair so adding to that one </p>";
//break since it matched
break;
}
}
//was it not matched
if (!isset($matchedPair)) {
//debug
//print "<p> creating a new matched pair for this task </p>";
//create a new days array
$daysArray = array();
//put an array in each day (this internal array will be of size 4)
for ($j=0; $j<7; $j++) {
//create a task event types array
$taskEventTypes = array();
//add 4 arrays to it
for ($k=0; $k<4; $k++)
$taskEventTypes[] = array();
//add the task event types array to the days array for this day
$daysArray[] = $taskEventTypes;
}
//create a new pair
$matchedPair = new TaskInfo($currentTaskId, $daysArray, $currentProjectId, $currentProjectTitle, $currentTaskName);
//add the matched pair to the structured array
$structuredArray[] = $matchedPair;
//make matchedPair be a reference to where we copied it to
$matchedPair = &$structuredArray[count($structuredArray)-1];
//print "<p> added matched pair with task '$matchedPair->taskName'</p>";
}
//iterate through the days array
for ($k=0; $k<7; $k++) {
//$dayStart = strftime("%D %T", $startDate + $k * A_DAY);
//$dayEnd = strftime("%D %T", $startDate + ($k + 1) * A_DAY);
//print "<p>DAY start: $dayStart, DAY end: $dayEnd</p>";
//work out some booleans
$startsOnPreviousDay = ($currentTaskStartDate < ($startDate + $k * A_DAY));
$endsOnFollowingDay = ($currentTaskEndDate >= ($startDate + ($k + 1) * A_DAY));
$startsToday = ($currentTaskStartDate >= ($startDate + $k * A_DAY) &&
$currentTaskStartDate < ($startDate + ($k + 1) * A_DAY));
$endsToday = ($currentTaskEndDate >= ($startDate + $k * A_DAY) &&
$currentTaskEndDate < ($startDate + ($k + 1) * A_DAY));
//$currentTaskStartDateStr = strftime("%D %T", $currentTaskStartDate);
//$currentTaskEndDateStr = strftime("%D %T", $currentTaskEndDate);
//print "<p>task start: $currentTaskStartDateStr task end: $currentTaskEndDateStr</p>";
//print "<p>startsOnPreviousDay=$startsOnPreviousDay, endsOnFollowingDay=$endsOnFollowingDay" .
// ", startsToday=$startsToday, endsToday=$endsToday</p>";
//does it start before this day and end after this day?
if ($startsOnPreviousDay && $endsOnFollowingDay)
//add this task entry to the array for index 0
$matchedPair->value2[$k][0][] = $data;
//does it start before this day and end on this day?
else if ($startsOnPreviousDay && $endsToday)
//add this task entry to the arry for index 1
$matchedPair->value2[$k][1][] = $data;
//does it start and end on this day?
else if ($startsToday && $endsToday)
//add this task entry to the array for index 2
$matchedPair->value2[$k][2][] = $data;
//does it start on this day and end on a following day
else if ($startsToday && $endsOnFollowingDay)
//add this task entry to the array for index 3
$matchedPair->value2[$k][3][] = $data;
}
}
//by now we should have our results structured in such a way that it it easy to output it
//set vars
$previousProjectId = -1;
$allTasksDayTotals = array(0,0,0,0,0,0,0); //totals for each day
/* $previousTaskId = -1;
$thisTaskId = -1;
$columnDay = -1;
$columnStartDate = $startDate;*/
//iterate through the structured array
$count = count($structuredArray);
unset($matchedPair);
for ($i=0; $i<$count; $i++) {
$matchedPair = &$structuredArray[$i];
?><tr id="row<?php echo $i; ?>"><?
?><td class="calendar_cell_middle" valign="top"><?
?><table width="100%" border="0" cellspacing="0" cellpadding="0"><?
?><tr><?
?><td width="50%" align="left"><?
?><input type="hidden" id="project_row<? echo $i; ?>" name="project_row<? echo $i; ?>" value="<? echo $matchedPair->projectId; ?>" /><?
?><select <?= $disable ?> class="newSelect" id="projectSelect_row<? echo $i; ?>" name="projectSelect_row<? echo $i; ?>" onChange="onChangeProjectSelect(this.id);" style="width: 100%;" /><?
?></td><?
?><td width="50%" align="left"><?
?><input type="hidden" id="task_row<? echo $i; ?>" name="task_row<? echo $i; ?>" value="<? echo $matchedPair->value1; ?>" /><?
?><select <?= $disable ?> class="newSelect" id="taskSelect_row<? echo $i; ?>" name="taskSelect_row<? echo $i; ?>" onChange="onChangeTaskSelect(this.id);" style="width: 100%;" /><?
?></td><?
?></tr><?
?><tr><?
?><td height="1"><img src="images/spacer.gif" width="100" height="1" /></td><?
?></tr><?
?></table><?
?></td><?
//print the spacer column
print "<td class=\"calendar_cell_disabled_middle\" width=\"2\"> </td>";
//iterate through the days array
$currentDay = 0;
$weeklyTotal = 0;
foreach ($matchedPair->value2 as $currentDayArray) {
//open the column
print "<td class=\"calendar_cell_middle\" valign=\"top\" align=\"left\">";
//while we are printing times set the style
print "<span class=\"task_time_small\">";
//declare todays vars
$todaysStartTime = $startDate + $currentDay * A_DAY;
$todaysEndTime = $startDate + ($currentDay + 1) * A_DAY;
$currentDay++;
$todaysTotal = 0;
//create a flag for empty cell
$emptyCell = true;
//iterate through the current day array
for ($j=0; $j<4; $j++) {
$currentTaskEntriesArray = $currentDayArray[$j];
//print "C" . count($currentTaskEntriesArray) . " ";
//iterate through the task entries
foreach ($currentTaskEntriesArray as $currentTaskEntry) {
//is the cell empty?
if ($emptyCell)
//the cell is not empty since we found a task entry
$emptyCell = false;
//else
//print a break for the next entry
//print "<br>";
//format printable times
$formattedStartTime = $currentTaskEntry["start"];
$formattedEndTime = $currentTaskEntry["endd"];
switch($j) {
case 0: //tasks which started on a previous day and finish on a following day
//print "...-...";
$todaysTotal += A_DAY;
break;
case 1: //tasks which started on a previous day and finish today
//print "...-" . $formattedEndTime;
$todaysTotal += $currentTaskEntry["end_time"] - $todaysStartTime;
break;
case 2: //tasks which started and finished today
//print $formattedStartTime . "-" . $formattedEndTime;
$todaysTotal += $currentTaskEntry["end_time"] - $currentTaskEntry["start_time"];
break;
case 3: //tasks which started today and finish on a following day
//print $formattedStartTime . "-...";
$todaysTotal += $todaysEndTime - $currentTaskEntry["start_time"];
break;
default:
print "error";
}
}
}
//create a string to be used in form input names
$rowCol = "_row" . $i . "_col" . $currentDay;
//make sure the cell has at least a space in it so that its rendered by the browser
if ($emptyCell) {
print "<span nowrap><input ".$disable." type=\"text\" id=\"hours" . $rowCol . "\" name=\"hours" . $rowCol . "\" size=\"1\" onChange=\"recalculateRowCol(this.id)\" onKeyDown=\"setDirty()\" />h</span>";
print "<span nowrap><input ".$disable." type=\"text\" id=\"mins" . $rowCol . "\" name=\"mins" . $rowCol . "\" size=\"1\" onChange=\"recalculateRowCol(this.id)\" onKeyDown=\"setDirty()\" />m</span>";
}
else {
//split todays total into hours and minutes
$todaysHours = floor($todaysTotal / 60 / 60);
$todaysMinutes = ($todaysTotal - ($todaysHours * 60 * 60)) / 60;
print "<span nowrap><input ".$disable." type=\"text\" id=\"hours" . $rowCol . "\" name=\"hours" . $rowCol . "\" size=\"1\" value=\"" . $todaysHours . "\" onChange=\"recalculateRowCol(this.id)\" onKeyDown=\"setDirty()\" />h</span>";
print "<span nowrap><input ".$disable." type=\"text\" id=\"mins" . $rowCol . "\" name=\"mins" . $rowCol . "\" size=\"1\" value=\"" . $todaysMinutes . "\" onChange=\"recalculateRowCol(this.id)\" onKeyDown=\"setDirty()\" />m</span>";
}
//close the times class
print "</span>";
/*if (!$emptyCell) {
//print todays total
$todaysTotalStr = formatSeconds($todaysTotal);
print "<br><span class=\"task_time_total_small\">$todaysTotalStr</span>";
}*/
//end the column
print "</td>";
//add this days total to the weekly total
$weeklyTotal += $todaysTotal;
//add this days total to the all tasks total for this day
$allTasksDayTotals[$currentDay - 1] += $todaysTotal;
}
//print the spacer column
print "<td class=\"calendar_cell_disabled_middle\" width=\"2\"> </td>";
//format the weekly total
$weeklyTotalStr = formatSeconds($weeklyTotal);
//print the total column
print "<td class=\"calendar_totals_line_weekly\" valign=\"bottom\" align=\"right\" class=\"subtotal\">";
print "<span class=\"calendar_total_value_weekly\" align=\"right\" id=\"subtotal_row" . $i . "\">$weeklyTotalStr</span></td>";
//end the row
print "</tr>";
//store the previous task and project ids
$previousTaskId = $currentTaskId;
$previousProjectId = $matchedPair->projectId;
}
/////////////////////////////////////////
//add an extra row for new data entry
/////////////////////////////////////////
if ($disable == "") {
?><tr id="row<? echo $count; ?>"><?
?><td class="calendar_cell_middle" valign="top"><?
?><table width="100%" border="0" cellspacing="0" cellpadding="0"><?
?><tr><?
?><td width="50%" align="left"><?
?><input type="hidden" id="project_row<? echo $count; ?>" name="project_row<? echo $count; ?>" value="-1" /><?
?><select class="newSelect" id="projectSelect_row<? echo $count; ?>" name="projectSelect_row<? echo $count; ?>" onChange="onChangeProjectSelect(this.id);" style="width: 100%;" /><?
?></td><?
?><td width="50%" align="left"><?
?><input type="hidden" id="task_row<? echo $count; ?>" name="task_row<? echo $count; ?>" value="-1" /><?
?><select class="newSelect" id="taskSelect_row<? echo $count; ?>" name="taskSelect_row<? echo $count; ?>" onChange="onChangeTaskSelect(this.id);" style="width: 100%;" /><?
?></td><?
?></tr><?
?><tr><?
?><td height="1"><img src="images/spacer.gif" width="100" height="1" /></td><?
?></tr><?
?></table><?
?></td><?
//print the spacer column
print "<td class=\"calendar_cell_disabled_middle\" width=\"2\"> </td>";
for ($currentDay=1; $currentDay<=7; $currentDay++) {
//open the column
print "<td class=\"calendar_cell_middle\" valign=\"top\" align=\"left\" width=\"65\">";
//while we are printing times set the style
print "<span class=\"task_time_small\">";
//create a string to be used in form input names
$rowCol = "_row" . $count . "_col" . $currentDay;
print "<span nowrap><input type=\"text\" id=\"hours" . $rowCol . "\" name=\"hours" . $rowCol . "\" size=\"1\" onChange=\"recalculateRowCol(this.id)\" onKeyDown=\"setDirty()\" disabled=\"true\" />h</span>";
print "<span nowrap><input type=\"text\" id=\"mins" . $rowCol . "\" name=\"mins" . $rowCol . "\" size=\"1\" onChange=\"recalculateRowCol(this.id)\" onKeyDown=\"setDirty()\" disabled=\"true\" />m</span>";
//close the times class
print "</span>";
//end the column
print "</td>";
}
//print the spacer column
print "<td class=\"calendar_cell_disabled_middle\" width=\"2\"> </td>";
//print the total column
print "<td class=\"calendar_totals_line_weekly\" valign=\"bottom\" align=\"right\" class=\"subtotal\" width=\"50\">";
print "<span class=\"calendar_total_value_weekly\" align=\"right\" id=\"subtotal_row" . $count . "\">0h 0m</span></td>";
//end the row
print "</tr>";
}
//////////////////////////////////////////////////////////////////////
//store a hidden form field containing the number of existing rows
print "<input type=\"hidden\" id=\"existingRows\" name=\"existingRows\" value=\"" . $count . "\" />";
//store a hidden form field containing the total number of rows
print "<input type=\"hidden\" id=\"totalRows\" name=\"totalRows\" value=\"" . ($count+1) . "\" />";
////////////////////////////////////////////////////
//Changes reequired to enter data on form -define 10 entry rows
// for ($i=0; $i<10; $i
////////////////////////////////////////////////////
//create a new totals row
print "<tr id=\"totalsRow\">\n";
print "<td class=\"calendar_cell_disabled_middle\" align=\"right\">Total Hours:</td>\n";
print "<td class=\"calendar_cell_disabled_middle\" width=\"2\"> </td>\n";
//iterate through day totals for all tasks
$grandTotal = 0;
$col = 0;
foreach ($allTasksDayTotals as $currentAllTasksDayTotal) {
$col++;
$grandTotal += $currentAllTasksDayTotal;
$formattedTotal = formatSeconds($currentAllTasksDayTotal);
print "<td class=\"calendar_totals_line_weekly_right\" align=\"right\">\n";
print "<span class=\"calendar_total_value_weekly\" id=\"subtotal_col" . $col . "\">$formattedTotal</span></td>";
}
//print grand total
$formattedGrandTotal = formatSeconds($grandTotal);
print "<td class=\"calendar_cell_disabled_middle\" width=\"2\"> </td>\n";
print "<td class=\"calendar_totals_line_monthly\" align=\"right\">\n";
print "<span class=\"calendar_total_value_monthly\" id=\"grand_total\">$formattedGrandTotal</span></td>";
print "</tr>";
?>
</table>
</td>
</tr>
</table>
<!-- include the timesheet face up until the end -->
<? include("timesheet_face_part_3.inc"); ?>
</td>
</tr>