-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
executable file
·142 lines (129 loc) · 4.12 KB
/
index.php
File metadata and controls
executable file
·142 lines (129 loc) · 4.12 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
<?php
/**
* Entry point for the application
*/
session_name('cruse_offers');
session_start();
require 'vendor/autoload.php';
require 'CruseLine.php';
?>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.18/datatables.min.css"/>
<link rel="stylesheet" type="text/css" href="style.css">
<?php
use PhpOffice\PhpSpreadsheet\IOFactory;
/**
* Handle form submittions
*/
if(isset($_POST['submit'])){
//validate for the correct file type
if($_FILES["offerlist"]['type'] != 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
{
$error = "<div class='error'>Please upload xlsx file.</div>";
}
else{
$inputFileName = $_FILES["offerlist"]["tmp_name"];
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName);
//check if the spread sheet is empty
if($spreadsheet->getActiveSheet()->getHighestRow()<2){
$error = "<div class='error'>The file doesn't contain any rows.</div>";
}
else{
//if file is valid, get an array of rows from the spreadsheet
$arrayFromSheet = getSpreadSheetToArray($spreadsheet);
//store an array of CruiseLine objects in the session
$_SESSION['cruseListArray'] = getCruseLineArrayFromArray($arrayFromSheet);
//output table
echo "
<script type=\"text/javascript\" src=\"https://code.jquery.com/jquery-3.3.1.js\"></script>
<script type=\"text/javascript\" src=\"https://cdn.datatables.net/v/dt/dt-1.10.18/datatables.min.js\"></script>
<script>
$(document).ready(function() {
$('#example').DataTable( {
\"processing\": true,
\"ajax\": {\"url\":\"getCruseLiners.php\",\"dataSrc\":\"\"},
\"columns\": [
{ \"data\": \"logo\" },
{ \"data\": \"name\" },
{ \"data\": \"offerId\" },
{ \"data\": \"offerName\" },
{ \"data\": \"departureDate\" },
{ \"data\": \"itinerary\" },
{ \"data\": \"shipName\" }
],
\"rowCallback\":function(row,data){
var imgLink = data['logo'];
var imgTag = '<img src=\"cruiselogos/' + imgLink + '\"/ height=\"30\" width=\"30\">';
$('td:eq(0)', row).html(imgTag);
return row;
}
} );
} );
</script>
<table id=\"example\" class=\"cruise-list\" style=\"width:100%\">
<thead>
<tr>
<th></th>
<th>Cruise Line</th>
<th>OfferId</th>
<th>Offer</th>
<th>Departure Date</th>
<th>Itinerary</th>
<th>Cruise Ship</th>
</tr>
</thead>
<tfoot>
<tr>
<th></th>
<th>Cruise Line</th>
<th>OfferId</th>
<th>Offer</th>
<th>Departure Date</th>
<th>Itinerary</th>
<th>Cruise Ship</th>
</tr>
</tfoot>
</table>";
}
}
}
/**
* Put spread sheet to an array where each row is one element
* @param Spreadsheet $spreadsheet
* @return array
*/
function getSpreadSheetToArray($spreadsheet){
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->removeRow(1); //get rid of the header
$listArray = [];
foreach($worksheet->getRowIterator() as $row)
{
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(FALSE);
$cells = [];
foreach ($cellIterator as $cell) {
array_push($cells,$cell);
}
array_push($listArray,$cells);
}
return $listArray;
}
/**
* Map each row of an array to a CruseLine object and store in an array
* @param array $arrayFromSheet
* @return array
*/
function getCruseLineArrayFromArray($arrayFromSheet){
$cruseLineArray = [];
foreach($arrayFromSheet as $row){
array_push($cruseLineArray, new CruseLine($row[0]->getValue(),$row[1]->getValue(),\PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($row[2]->getValue()),$row[3]->getValue(),$row[4]->getValue(),$row[5]->getValue(),$row[6]->getValue()));
}
return $cruseLineArray;
}
?>
<div class="container">
<form id="fileupload" action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" enctype="multipart/form-data">
<fieldset><input name="offerlist" type="file"/></fieldset>
<?php if(isset($_FILES["offerlist"]['name']) && isset($error)) echo $error;?>
<fieldset> <button name="submit" type="submit" id="contact-submit" data-submit="...Sending">Upload</button></fieldset>
</form>
</div>