This repository was archived by the owner on Apr 10, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathNutrition.php
More file actions
executable file
·132 lines (119 loc) · 4.45 KB
/
Copy pathNutrition.php
File metadata and controls
executable file
·132 lines (119 loc) · 4.45 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
<?php
/*****************************************************
* Colin Tremblay
* Grinnell College '14
*
* This file creates .json file from the xml of all nutritional information.
* It also adds the nutrition information to the day json
*
*****************************************************/
function create_nutrition_json(){
// load input file
$file = "nutrition.xml";
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");
// start building the JSON
$output = "{\n";
// iterate through each menu item
foreach ($xml->xpath('//d_itm_recipe_perportion_nutr_analysis_group1') as $item){
// Add the ID of the item
$tempName = str_replace('"','\\"',$item->srv_itemuofm_intid);
$tempName = trim($tempName);
$output = $output."\t\"$tempName\": {";
// Add serving size
$serving = $item->ls_srvuofm;
$serving = str_replace('(', '', $serving);
$serving = str_replace(')', '', $serving);
$serving = ucwords(strtolower($serving));
$serving = str_replace('Oz', 'Oz.', $serving);
$serving = str_replace('"','\\"', $serving);
$pos = strpos($serving, "Cut");
if ($pos !== false)
$serving = "1 Piece";
$pos = strpos($serving, "Slice");
if ($pos !== false)
$serving = "1 Slice";
$pos = strpos($serving, "X");
if ($pos !== false)
$serving = "1 Piece";
$output = $output."\n\t\t\"ServSize\":\"".$serving."\",";
//Check to make sure nutrition is by a valid serving size (not by the dozen)
$pos = strpos($serving, "Dozen");
if ($pos === false)
$output = $output."\n\t\t\"Dozen\":\"false\",";
else
$output = $output."\n\t\t\"Dozen\":\"true\",";
// iterate to the nutrition for the item itself (not its ingredients) and add this
foreach ($item->d_itm_nutr_analysis_nup_25_values_x->d_itm_nutr_analysis_nup_25_values_x_row as $element){
$nutrient = trim($element->ls_element, " ");
$output = $output."\n\t\t\"$nutrient\":\"$element->ptn1_qty\",";
}
$output = $output."\n\t\t\"ingredients\":[";
// iterate through the ingredients and add them
foreach ($item->d_itm_recipe_perportion_ingr_analysis_25->d_itm_recipe_perportion_ingr_analysis_25_row as $ingredientRow){
$ingredient = trim($ingredientRow->name, " ");
$ingredient = str_replace("\"", "\\\"", $ingredient);
$ingredient = ucwords(strtolower($ingredient));
$ingredient = str_replace("In Processing Frozen Home-prepared Oven Heated", "", $ingredient);
$output = $output."\n\t\t\t\"$ingredient\",";
}
// remove trailing coma and finalize the output
$output = trim($output, ",");
$output = $output."\n\t\t]\n\t},\n";
}
// remove trailing coma and finalize the output
$output = trim($output, ",\n");
$output = $output."\n} ";
return $output;
}
function build_nutrition($dishID, &$json_a){
//echo("here");
//$dishID = trim($dishID, ".00");
//If the nutrition.json has an entry for the given dish
if (isset($json_a[$dishID]))
//If that dish has a value for KCAL
if (isset($json_a[$dishID]["KCAL"])){
if ((!strcmp($json_a[$dishID]["KCAL"], "0")) && (!strcmp($json_a[$dishID]["FAT"], "0"))){
$output = "\"NIL\"";
return $output;
}
else{
$array = array('KCAL', 'FAT', 'CHO', 'PRO', 'SFA', 'POLY', 'MONO',
'CHOL', 'TDFB', 'VITC', 'B12', 'NA', 'ZN', 'FE', 'FATRN', 'K',
'CA', 'VTAIU', 'B6', 'SUGR');
$output = "{";
$dozen_str = $json_a[$dishID]["Dozen"];
$pos = strpos($dozen_str, "true");
for ($i = 0; $i < 20; $i++){
//Get each nutritional value and crop it to 3 decimals
$str = $array[$i];
$number = $json_a[$dishID][$str];
if ($pos !== false)
$number = $number/12;
$number = number_format($number, 3, '.', '');
//Build the output
$output = $output."\"$str\":".$number;
$output = trim($output, "0");
$output = trim($output, ".").",";
}
}
if ($pos !== false)
$servSizeStr = "1 Cookie";
else
$servSizeStr = $json_a[$dishID]["ServSize"];
$output = trim($output, ",")."},";
$output = $output."\n\"ServSize\":\"$servSizeStr\",";
// add ingredients list
$ingredientsArray = $json_a[$dishID]["ingredients"];
$output = $output."\n\"Ingredients\":[";
/* foreach($ingredientsArray as $ingredient) {
$ingredient = str_replace("\"", "\\\"", $ingredient);
$output = $output."\"$ingredient\",";
}
$output = trim($output, ",")."]";*/
// Remove ingredients:
$output = $output."\"Ingredients list coming soon\"]";
return $output;
}
return null;
}
?>