-
Notifications
You must be signed in to change notification settings - Fork 1
/
damage_by_time.php
162 lines (137 loc) · 4.55 KB
/
damage_by_time.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
<?php
$pageTitle = "Damage by Time";
$navbarLink1 = "index.php";
$navbarLink1Title = "HOME";
$navbarLink2 = "auth.php";
$navbarLink2Title = "LOG-IN/OUT";
$navbarLink3 = "portal.php";
$navbarLink3Title = "PORTAL";
require_once("header.php");
require_once("database.php");
if (!isset($_SESSION["logged_in"]) || (!isset($_SESSION["admin"]) && !isset($_SESSION["read_only"]) &&
!isset($_SESSION["insurer"]) &&
!isset($_SESSION["farmer"]))) header("Location: login.php");
?>
<div class="container">
<section id="main">
<h1>Damage by Time</h1>
<p>Look for instances of farm damage that started and ended in the given time period.</p>
</section>
</div>
<!--Upload Entity Attributes or Assign them NULL values-->
<?php
$uploadName = NULL;
$uploadStartDate = NULL;
$uploadEndDate = NULL;
if(isset($_POST["uploadName"]))
{
$uploadName = $_POST["uploadName"];
$uploadStartDate = $_POST["uploadStartDate"];
$uploadEndDate = $_POST["uploadEndDate"];
}
?>
<?php if (isset($_SESSION["admin"]) || isset($_SESSION["farmer"])): ?>
<div class="container">
<!--Forms for Uploading Damage-->
<h3>Upload Damage Instance</h3>
<form action="damage_by_time.php" method="POST">
<table id="formTable">
<tr>
<td>Damage Name:</td>
<td><input type="text" name="uploadName" value="<?php echo ""; ?>" /></td>
<td>Start Date:</td>
<td><input type="date" name="uploadStartDate" value="<?php echo date('Y-m-d'); ?>" /></td>
<td>End Date:</td>
<td><input type="date" name="uploadEndDate" value="<?php echo date('Y-m-d') ?>" /></td>
</tr>
</table>
<div id="centeredDiv">
<input type="submit" value="Upload"/>
</div>
</form>
</div>
<?php endif; ?>
<!--Upload Damage Entry-->
<?php
if($uploadName != "")
{
$db = new Database();
//$sql = "INSERT INTO `DAMAGE` (`damage_id`, `name`, `start_date`, `end_date`) VALUES (NULL, 'name', '2019-11-10', '2019-11-11')";
$sql = "INSERT INTO `DAMAGE` (`damage_id`, `name`, `start_date`, `end_date`) VALUES (NULL, ?, ?, ?)";
$result = $db->preparedQuery($sql, "sss", array($uploadName, $uploadStartDate, $uploadEndDate));
}
?>
<!--Load Search Criteria or Assign them NULL values-->
<?php
$queryStartDate = NULL;
$queryEndDate = NULL;
if(isset($_POST["queryStartDate"]))
{
$queryStartDate = $_POST["queryStartDate"];
$queryEndDate = $_POST["queryEndDate"];
}
?>
<div class="container">
<!--Forms for Search Criteria-->
<h3>Search for Damage</h3>
<form action="damage_by_time.php" method="POST">
<table id="searchTable">
<tr>
<td>Start Date:</td>
<td><input type="date" name="queryStartDate" value="<?php echo ''.(is_null($queryStartDate)? date('Y-m-d') : $queryStartDate); ?>" /></td>
<td>End Date:</td>
<td><input type="date" name="queryEndDate" value="<?php echo ''.(is_null($queryEndDate)? date('Y-m-d') : $queryEndDate); ?>" /></td>
</tr>
</table>
<div id="centeredDiv">
<input type="submit" value="Search"/>
</div>
</form>
<!--Search Function gets query in $result if search criteria are submitted-->
<?php if(!is_null($queryStartDate)): ?>
<!--Retrieve and Store Query Result-->
<?php
$db = new Database();
$sql = "SELECT *
FROM `DAMAGE` AS D
WHERE D.start_date >= ? AND
D.end_date <= ?";
$result = $db->preparedQuery($sql, "ss", array($queryStartDate, $queryEndDate));
$resultArr = array();
while($row = $result->fetch_array()){ $resultArr[] = $row; }
$resultArrLen = count($resultArr);
$resultArrWidth = 4;
$i = 0;
?>
<!--Table to Display Query Results-->
<table id = "resultTable">
<tr>
<th>Damage Name</th>
<th>Start Date</th>
<th>End Date</th>
</tr>
<?php while($i < $resultArrLen): ?>
<tr>
<td><?php echo $resultArr[$i]['name']; ?></td>
<td><?php echo $resultArr[$i]['start_date']; ?></td>
<td><?php echo $resultArr[$i]['end_date']; ?></td>
</tr>
<?php $i++; endwhile; ?>
</table>
<!--Functionality to Download Result Table as CSV in JS-->
<script src="downloadCSVFromJSArray.js"></script>
<script type="text/javascript">
//Convert php array to JS array
var resultArrJS = <?php echo json_encode($resultArr); ?> ;
var resultArrJSHeaders = ["Damage ID", "Damage Name", "Start Date", "End Date"];
var filename = "damageByTime";
</script>
<div id="centeredDiv">
<input type="button" onclick="downloadCSVFromArray(resultArrJS, resultArrJSHeaders, filename)" value="Download Result">
</div>
<p>Enable pop-ups and disable your adblocker to enable downloads.</p>
<?php endif; ?>
</div>
<?php
require_once("footer.php");
?>