-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecho.php
275 lines (240 loc) · 7.34 KB
/
echo.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
<?php
//Error Reporting and Commmon Functions
require_once('common.php');
error_reporting(E_ALL);
ini_set('display_errors', 'On');
//Set DEBUG to true in common
if(DEBUG){
die(print_r($_FILES)."<br />".print_r($_POST));
//die(print_r($_FILES['newFile']));
//die(print_r($_POST));
}
//Declare variables
$titleName = 'Temperature Chart';
$yAxisTitle = 'Temperature';
$celcius = array();
$fahrenheit = array();
$time = array();
$date = array();
$fileName;
$fileContent;
$fileLines;
$chartType = array_key_exists('chartType', $_POST) ? escape_html($_POST['chartType']) : 'No Type Chosen';
$uploadPath = "uploads/";
//Logic
if(array_key_exists('newFile', $_FILES) && !empty($_FILES['newFile']['name']) && $_POST['existingFile'] == 'default'){
$fileName = escape_html($_FILES['newFile']['name']);
storeFile($uploadPath, $fileName);
makeFileLines();
makeHTML($fileLines);
}else if(array_key_exists('existingFile', $_POST) && $_POST['existingFile'] != 'default' && $_FILES['newFile']['name'] == ''){
$fileName = $_POST['existingFile'];
existingFileContent();
makeFileLines();
makeHTML($fileLines);
}else if(array_key_exists('existingFile', $_POST) && $_POST['existingFile'] != 'default' && array_key_exists('newFile', $_FILES) && !empty($_FILES['newFile']['name'])){
$fileName = escape_html($_FILES['newFile']['name']);
$previousFileName = $_POST['existingFile'];
bothMethods();
}else{
die('Sorry buddy, you either didn\'t upload a file or it wasn\'t a CSV file.');
}
//Page Level Functions
function storeFile($path, $name){
global $fileContent;
$fileAlreadyExists = FALSE;
$tmp_name = $_FILES["newFile"]["tmp_name"];
$name = $_FILES["newFile"]["name"];
$uploaded = move_uploaded_file($tmp_name, 'uploads/'.$name);
$fileContent = file_get_contents('uploads'.'/'.$name);
//---------------------------
$dir = 'uploads';
$files = scandir($dir, 1);
$j = 0;
foreach($files as $fileName/* File in directory */){
if($files[$j] == 'about_uploads_folder.txt.txt' || $files[$j] == '.' || $files[$j] == '..'){
//Don't add a load option
}else{
if($name == $fileName){
$fileAlreadyExists = TRUE;
}
}
$j++;
}
//---------------------
if($fileAlreadyExists = FALSE){
$uploaded = move_uploaded_file($tmp_name, 'uploads/'.$name);
echo "<script type='text/javascrpt'>
console.log('file uploaded');
</script>";
}else{
echo "<script type='text/javascrpt'>
console.log('file was not uploaded, already existed');
</script>";
}
if(DEBUG){
//comment out other debug die statements to make this work
if($uploaded){
die(file_get_contents('uploads'.'/'.$name));
}else{
die($tmp_name);
}
}
}
function bothMethods(){
redirect('index.php?msg=sameName');
}
function existingFileContent(){
global $fileName;
global $fileContent;
global $uploadPath;
$fileContent = file_get_contents($uploadPath.$fileName);
}
function makeFileLines(){
global $fileLines;
global $fileContent;
$fileLines = explode("\n", $fileContent);
}
function makeHTML($lines){
//Create Data Variables
global $time;
global $date;
global $chartType;
global $fahrenheit;
global $celcius;
global $titleName;
global $yAxisTitle;
$j = 0;
foreach($lines as $value){
//make cells a multidimensional array with elements for each cell in the csv file
$cells[$j] = explode(",", $lines[$j]);
$j++;
}
$k = 0;
foreach($cells as $value){
foreach($value as $secondvalue){
if($secondvalue == $cells[$k][0]){
array_push($date, $secondvalue);
//Need to format date properly
}else if($secondvalue == $cells[$k][1]){
array_push($time, $secondvalue);
}else if($secondvalue == $cells[$k][2]){
array_push($celcius, $secondvalue);
}else if($secondvalue == $cells[$k][3]){
array_push($fahrenheit, $secondvalue);
}
}
$k++;
}
//Get rid of quotation marks in array values
$p = 0;
foreach($celcius as $value){
str_replace('"'," ",$celcius[$p]);
$p++;
}
$jsonC = json_encode($celcius, JSON_NUMERIC_CHECK);
$jsonF = json_encode($fahrenheit, JSON_NUMERIC_CHECK);
$jsonD = json_encode($date);
//die(print_r($date).print_r($time).print_r($celcius).print_r($fahrenheit));
$html = "<DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Enjoy your cool graph...</title>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js'></script>
<script src='http://code.highcharts.com/highcharts.js'></script>
<script src='http://code.highcharts.com/modules/exporting.js'></script>
<script src='http://code.jquery.com/jquery-1.11.1.min.js'></script>
<script type='text/javascript' name='chartCode'>
//Script for displaying a chart, printing it, and downloading it
//DeviceReady event handling
$(document).on('pageinit', deviceReady);
function deviceReady(){
console.log('DeviceReady Function fired');
}
console.log('In chart js from echo.php');
//Define variables
//Create Functions
function Print(){
chart.setTitle(null, { text: ' ' });
chart.print();
chart.setTitle(null, { text: 'Click and drag in the plot area to zoom in' });
}
function generateChart(chartToPass){
return chartToPass;
}
function userDownload(){
var d = document.getElementById('ExportOption');
var ExportAs = d.options[d.selectedIndex].value;
if(ExportAs == 'PNG')
{
chart.exportChart({type: 'image/png', filename: titleName}, {subtitle: {text:''}});
}
if(ExportAs == 'JPEG')
{
chart.exportChart({type: 'image/jpeg', filename: titleName}, {subtitle: {text:''}});
}
if(ExportAs == 'PDF')
{
chart.exportChart({type: 'application/pdf', filename: titleName}, {subtitle: {text:''}});
}
if(ExportAs == 'SVG')
{
chart.exportChart({type: 'image/svg+xml', filename: titleName}, {subtitle: {text:''}});
}
}
//Define initial skeleton for chart
var options = {
chart:{
renderTo: 'container',
defaultChartType: 'column',
type: '".$chartType."',
zoomType: 'x'
},
title:{
text: '".$titleName."',
x: -20
},
xAxis:{
categories: ".$jsonD."
},
yAxis:{
title:{
text: '".$yAxisTitle."'
}
},
series: [{
name:'Celsius',
data:".$jsonC."
},
{
name:'Fahrenheit',
data:".$jsonF."
}]
};
//Buttons and Processes
//Display chart process
$(function(){
var chart = new Highcharts.Chart(options);
});
//Print button
$('#buttonPrint').click(function(){
console.log('User clicked the Print button');
Print();
});
//Download button
$('#downloadButton').click(function (){
console.log('User clicked the Download Button');
userDownload()
});
</script>
</head>
<body>
<!-- The highcharts graph will render inside this container -->
<div id='container' style='width:100%; height: 400px;'>
</div>
<!-- Below are exporting options -->
</body>
</html>";
echo $html;
}