-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathajax.php__
executable file
·113 lines (90 loc) · 4 KB
/
ajax.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
<?php
/**
* This script deals with AJAX calls from the client. The client may request for the current grid to be downloaded as
* an Excel spreadsheet, a spreadsheet to be uploaded for analysis, or simply load the tool.
*
* If a spreadsheet is being posted to the server, parse it and create the XML required to display the grid. If other
* data is being posted to the server, create a spreadsheet containing that data and send it for download. Else, create
* the XML required to display the grid.
*/
if(isset($_GET['tip'])){
require_once 'utils/FeedValAJAXAutoloadcalc.php';
}else{
require_once 'utils/FeedValAJAXAutoload.php';
}
if (isset($_POST['data']) && isset($_POST['minimize'])) {
// Decode the JSON encoded data.
if (get_magic_quotes_gpc()) {
$cleanData = stripslashes($_POST['data']);
$cleanColumnsToAppear = stripslashes($_POST['columnsToAppear']);
} else {
$cleanData = $_POST['data'];
$cleanColumnsToAppear = $_POST['columnsToAppear'];
}
$data = json_decode($cleanData, TRUE);
$columnsToAppear = json_decode($cleanColumnsToAppear, TRUE);
$minimization = FeedValMinimizationFactory::getCoefficients($columnsToAppear, $data);
$minimizationArray = $minimization->getMinArray();
echo json_encode($minimizationArray);
} else if (isset($_POST['data'])) {
// Decode the JSON encoded data.
if (get_magic_quotes_gpc()) {
$cleanData = stripslashes($_POST['data']);
$cleanColumnsToAppear = stripslashes($_POST['columnsToAppear']);
} else {
$cleanData = $_POST['data'];
$cleanColumnsToAppear = $_POST['columnsToAppear'];
}
$data = json_decode($cleanData, TRUE);
$columnsToAppear = json_decode($cleanColumnsToAppear, TRUE);
// Create the Excel spreadsheet and push it for download.
$spreadsheet = FeedValSpreadsheetFactory::createSpreadsheet($columnsToAppear, $data);
$spreadsheetFileName = $spreadsheet->getFileName();
echo json_encode(array('spreadsheetFilename' => $spreadsheetFileName));
} else if (isset($_GET['getMinMaxDates'])) {
$databaseDao = new FeedValDatabaseDao();
$minDate = $databaseDao->getMinDate();
$maxDate = $databaseDao->getMaxDate();
$dates = array('minDate' => $minDate, 'maxDate' => $maxDate);
echo json_encode($dates);
} else if (isset($_GET['pricesForDate'])) {
$databaseDao = new FeedValDatabaseDao();
$prices = $databaseDao->getPricesForDate($_GET['pricesForDate']);
echo json_encode($prices);
} else if (isset($_POST['convertUnits'])){
$ingredients = $_POST['ingredients'];
$convertedUnits = array();
foreach ($ingredients as $ingredient) {
$ingredientName = $ingredient['ingredientName'];
$ingredientID = intval($ingredient['ingredientID']);
$price = $ingredient['price'];
$fromUnit = isset($ingredient['fromUnit'])?$ingredient['fromUnit']:'ton';
$toUnit = isset($ingredient['toUnit'])?$ingredient['toUnit']:'ton';
if ($fromUnit && $price && $toUnit){
$convertedUnits[$ingredientID] = UnitConverter::getPriceInUnit($ingredientName, $price, $fromUnit, $toUnit);
}
}
echo json_encode($convertedUnits);
} else {
if (isset($_POST['data_file_submit'])) {
$userFile = FeedValUtil::uploadFile($_FILES['data_file']);
$feedValXML = new FeedValSpreadsheetXML($userFile);
} else {
$feedValXML = new FeedValDatabaseXML();
}
$xmlDoc = $feedValXML->getXML();
$jqGridSettings = $feedValXML->getJqGridSettings();
$nutrients = $feedValXML->getNutrients();
$xmlFile = FeedValUtil::createTemporaryFileWithExtension('xml');
$xmlDoc->save($xmlFile);
$jqGridSettings['url'] = getXMLFilePath($xmlFile);
$jsonArray = array(
'jqGridOptions' => $jqGridSettings,
'nutrients' => $nutrients);
echo json_encode($jsonArray);
}
function getXMLFilePath($xmlFile) {
$server_path = str_replace('/', DIRECTORY_SEPARATOR, $_SERVER['DOCUMENT_ROOT']);
return str_replace($server_path, '', $xmlFile);
}
?>