www/upl_recipe.php

changeset 812
d286bf289d45
parent 811
4e3409cfd2b1
child 813
752bb80362fb
equal deleted inserted replaced
811:4e3409cfd2b1 812:d286bf289d45
1 <?php
2 require_once('config.php');
3 require("version.php");
4 require("includes/formulas.php");
5
6
7 $target_dir = "tmp/";
8 $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
9 $uploadOk = 1;
10 $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
11
12 // Check if file already exists
13 if (file_exists($target_file)) {
14 echo "Fout 1: bestand bestaat al. ";
15 $uploadOk = 0;
16 }
17 // Check file size
18 if ($_FILES["fileToUpload"]["size"] > 500000 && $uploadOk) {
19 echo "Fout 2: het bestand is te groot. ";
20 $uploadOk = 0;
21 }
22 // Allow certain file formats
23 if ($imageFileType != "xml" && $uploadOk) {
24 echo "Fout 3: alleen XML bestanden toegestaan. ";
25 $uploadOk = 0;
26 }
27 // Check if $uploadOk is set to 0 by an error
28 if ($uploadOk == 0) {
29 exit;
30 }
31
32 if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
33 echo "Verwerken bestand ". basename( $_FILES["fileToUpload"]["name"]). "<br />";
34 } else {
35 echo "Fout 4: er ging iets fout met de upload.";
36 exit;
37 }
38
39
40 $db = mysqli_connect(DBASE_HOST, DBASE_USER, DBASE_PASS, DBASE_NAME);
41 if (! $db) {
42 echo "Fout 5: ".mysqli_connect_errno()." ".mysqli_connect_error();
43 exit;
44 }
45 mysqli_set_charset($db, "utf8" );
46 date_default_timezone_set('Europe/Amsterdam');
47
48
49 $pCara = 0;
50 $pSugar = 0;
51 $svg = 77;
52 $efficiency = 75;
53 $batch_size = 20;
54 $colorw = 0;
55 $f_sugars = 0;
56
57
58
59 function get_miscs_cost($miscname) {
60 global $db;
61
62 if ($result = mysqli_query($db, "SELECT cost FROM inventory_miscs WHERE name='$miscname'")) {
63 if ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
64 return floatval($row['cost']);
65 }
66 }
67 return 0.0;
68 }
69
70
71
72 function recipe_style($recipe)
73 {
74 global $db;
75
76 if ($recipe->STYLE->NAME)
77 $sql = "', st_name='" . mysqli_real_escape_string($db, $recipe->STYLE->NAME);
78 if ($recipe->STYLE->STYLE_LETTER)
79 $sql .= "', st_letter='" . mysqli_real_escape_string($db, $recipe->STYLE->STYLE_LETTER);
80 if ($recipe->STYLE->STYLE_GUIDE) {
81 if ($recipe->STYLE->STYLE_GUIDE == "Biertypengids Derek Walsh") {
82 $sql .= "', st_guide='BKG 2019";
83 } else if ($recipe->STYLE->STYLE_GUIDE == "BKG Biertypen") {
84 $sql .= "', st_guide='BKG 2019";
85 } else if ($recipe->STYLE->STYLE_GUIDE == "BKG 2015") {
86 $sql .= "', st_guide='BKG 2019";
87 } else {
88 $sql .= "', st_guide='" . mysqli_real_escape_string($db, $recipe->STYLE->STYLE_GUIDE);
89 }
90 } else {
91 $sql .= "', st_guide='BKG 2019";
92 }
93 if ($recipe->STYLE->CATEGORY)
94 $sql .= "', st_category='" . mysqli_real_escape_string($db, $recipe->STYLE->CATEGORY);
95 if ($recipe->STYLE->CATEGORY_NUMBER)
96 $sql .= "', st_category_number='" . floatval($recipe->STYLE->CATEGORY_NUMBER);
97 else
98 $sql .= "', st_category_number='0";
99 if ($recipe->STYLE->TYPE == 'Lager')
100 $sql .= "', st_type='0";
101 else if (($recipe->STYLE->TYPE == 'Ale') || ($recipe->STYLE->TYPE == 'Strong Ale'))
102 $sql .= "', st_type='1";
103 else if ($recipe->STYLE->TYPE == 'Mead')
104 $sql .= "', st_type='2";
105 else if ($recipe->STYLE->TYPE == 'Wheat')
106 $sql .= "', st_type='3";
107 else if ($recipe->STYLE->TYPE == 'Mixed')
108 $sql .= "', st_type='4";
109 else if ($recipe->STYLE->TYPE == 'Cider')
110 $sql .= "', st_type='5";
111 else
112 echo "Unknown TYPE " . $recipe->STYLE->TYPE . "<br />";
113
114 if ($recipe->STYLE->OG_MIN)
115 $sql .= "', st_og_min='" . floatval($recipe->STYLE->OG_MIN);
116 if ($recipe->STYLE->OG_MAX)
117 $sql .= "', st_og_max='" . floatval($recipe->STYLE->OG_MAX);
118 if ($recipe->STYLE->FG_MIN)
119 $sql .= "', st_fg_min='" . floatval($recipe->STYLE->FG_MIN);
120 if ($recipe->STYLE->FG_MAX)
121 $sql .= "', st_fg_max='" . floatval($recipe->STYLE->FG_MAX);
122 if ($recipe->STYLE->IBU_MIN)
123 $sql .= "', st_ibu_min='" . floatval($recipe->STYLE->IBU_MIN);
124 if ($recipe->STYLE->IBU_MAX)
125 $sql .= "', st_ibu_max='" . floatval($recipe->STYLE->IBU_MAX);
126 if ($recipe->STYLE->COLOR_MIN) {
127 $srm = floatval($recipe->STYLE->COLOR_MIN);
128 $sql .= "', st_color_min='" . srm_to_ebc($srm);
129 }
130 if ($recipe->STYLE->COLOR_MAX) {
131 $srm = floatval($recipe->STYLE->COLOR_MAX);
132 $sql .= "', st_color_max='" . srm_to_ebc($srm);
133 }
134 if ($recipe->STYLE->CARB_MIN)
135 $sql .= "', st_carb_min='" . floatval($recipe->STYLE->CARB_MIN);
136 if ($recipe->STYLE->CARB_MAX)
137 $sql .= "', st_carb_max='" . floatval($recipe->STYLE->CARB_MAX);
138 if ($recipe->STYLE->ABV_MIN)
139 $sql .= "', st_abv_min='" . floatval($recipe->STYLE->ABV_MIN);
140 if ($recipe->STYLE->ABV_MAX)
141 $sql .= "', st_abv_max='" . floatval($recipe->STYLE->ABV_MAX);
142 return $sql;
143 }
144
145
146
147 function recipe_fermentables($recipe)
148 {
149 global $db;
150 global $pCara;
151 global $pSugar;
152 global $batch_size;
153 global $efficiency;
154 global $colorw;
155 global $f_sugars;
156
157 $fermentables = "[";
158 $comma = FALSE;
159 foreach ($recipe->FERMENTABLES->FERMENTABLE as $fermentable) {
160 if ($comma)
161 $fermentables .= ',';
162 $comma = TRUE;
163 $fermentables .= '{"f_name":"' . mysqli_real_escape_string($db, $fermentable->NAME) . '"';
164 $fermentables .= ',"f_origin":"' . mysqli_real_escape_string($db, $fermentable->ORIGIN) . '"';
165 $fermentables .= ',"f_supplier":"' . mysqli_real_escape_string($db, $fermentable->SUPPLIER) . '"';
166 $famount = floatval($fermentable->AMOUNT);
167 $fermentables .= ',"f_amount":' . $famount;
168 $fermentables .= ',"f_cost":' . floatval($fermentable->COST);
169
170 if ($fermentable->TYPE == 'Grain')
171 $fermentables .= ',"f_type":0';
172 else if ($fermentable->TYPE == 'Sugar')
173 $fermentables .= ',"f_type":1';
174 else if ($fermentable->TYPE == 'Extract')
175 $fermentables .= ',"f_type":2';
176 else if (($fermentable->TYPE == 'Dry extract') || ($fermentable->TYPE == 'Dry Extract'))
177 $fermentables .= ',"f_type":3';
178 else if ($fermentable->TYPE == 'Adjunct')
179 $fermentables .= ',"f_type":4';
180 else
181 echo "Unknown TYPE " . $fermentable->TYPE . "<br / >";
182
183 $fyield = floatval($fermentable->YIELD);
184 $fermentables .= ',"f_yield":' . $fyield;
185 if ($fermentable->COLOR) {
186 $srm = floatval($fermentable->COLOR);
187 $ebc = srm_to_ebc($srm);
188 } else {
189 $srm = 0;
190 $ebc = 0;
191 }
192 $colorw += ($famount * $srm / $batch_size) * 8.34436; /* Kleurwerking */
193 $fermentables .= ',"f_color":' . $ebc;
194 if ($fermentable->COARSE_FINE_DIFF)
195 $fermentables .= ',"f_coarse_fine_diff":' . floatval($fermentable->COARSE_FINE_DIFF);
196 else
197 $fermentables .= ',"f_coarse_fine_diff":0';
198 if ($fermentable->MOISTURE)
199 $fmoisture = floatval($fermentable->MOISTURE);
200 else
201 $fmoisture = 0;
202 $fermentables .= ',"f_moisture":' . $fmoisture;
203 if ($fermentable->DIASTATIC_POWER)
204 $fermentables .= ',"f_diastatic_power":' . floatval($fermentable->DIASTATIC_POWER);
205 else
206 $fermentables .= ',"f_diastatic_power":0';
207 if ($fermentable->PROTEIN)
208 $fermentables .= ',"f_protein":' . floatval($fermentable->PROTEIN);
209 else
210 $fermentables .= ',"f_protein":0';
211 if ($fermentable->MAX_IN_BATCH)
212 $fermentables .= ',"f_max_in_batch":' . floatval($fermentable->MAX_IN_BATCH);
213 else
214 $fermentables .= ',"f_max_in_batch":100.0';
215
216 if ($fermentable->GRAINTYPE == 'Roast')
217 $fermentables .= ',"f_graintype":1';
218 else if ($fermentable->GRAINTYPE == 'Crystal')
219 $fermentables .= ',"f_graintype":2';
220 else if ($fermentable->GRAINTYPE == 'Kilned')
221 $fermentables .= ',"f_graintype":3';
222 else if (($fermentable->GRAINTYPE == 'Sour malt') || ($fermentable->GRAINTYPE == 'Sour Malt'))
223 $fermentables .= ',"f_graintype":4';
224 else if ($fermentable->GRAINTYPE == 'Special')
225 $fermentables .= ',"f_graintype":5';
226 else if (($fermentable->GRAINTYPE == 'No malt') || ($fermentable->GRAINTYPE == 'No Malt'))
227 $fermentables .= ',"f_graintype":6';
228 else
229 $fermentables .= ',"f_graintype":0'; // Base, default
230
231 if ($fermentable->ADDED == 'Boil')
232 $fermentables .= ',"f_added":1';
233 else if ($fermentable->ADDED == 'Fermentation')
234 $fermentables .= ',"f_added":2';
235 else if ($fermentable->ADDED == 'Lagering')
236 $fermentables .= ',"f_added":3';
237 else if ($fermentable->ADDED == 'Bottle')
238 $fermentables .= ',"f_added":4';
239 else
240 $fermentables .= ',"f_added":0'; // Mash, default.
241
242 ($fermentable->ADD_AFTER_BOIL== "TRUE") ? $fermentables .= ',"f_add_after_boil":1' : $fermentables .= ',"f_add_after_boil":0';
243 ($fermentable->RECOMMEND_MASH== "TRUE") ? $fermentables .= ',"f_recommend_mash":1' : $fermentables .= ',"f_recommend_mash":0';
244 if ($fermentable->DISSOLVED_PROTEIN)
245 $fermentables .= ',"f_dissolved_protein":' . floatval($fermentable->DISSOLVED_PROTEIN);
246 else
247 $fermentables .= ',"f_dissolved_protein":0';
248 ($fermentable->ADJUST_TO_TOTAL_100 == "TRUE") ? $fermentables .= ',"f_adjust_to_total_100":1' : $fermentables .= ',"f_adjust_to_total_100":0';
249 $percent = floatval($fermentable->PERCENTAGE);
250 $fermentables .= ',"f_percentage":' . round($percent * 10) / 10.0;
251 if ($fermentable->DI_pH)
252 $fermentables .= ',"f_di_ph":' . floatval($fermentable->DI_pH);
253 else
254 $fermentables .= ',"f_di_ph":0.0';
255 if ($fermentable->{'ACID_TO_pH_5.7'})
256 $fermentables .= ',"f_acid_to_ph_57":' . floatval($fermentable->{'ACID_TO_pH_5.7'});
257 else
258 $fermentables .= ',"f_acid_to_ph_57":0.0';
259 $fermentables .= "}";
260
261 /* Sugars */
262 $d = $famount * ($fyield / 100) * (1 - $fmoisture / 100);
263 if ($fermentable->ADDED == 'Mash')
264 $d = floatval($efficiency) / 100 * $d;
265 $f_sugars += $d;
266 if ($fermentable->GRAINTYPE == 'Crystal')
267 $pCara += $percent;
268 if ($fermentable->TYPE == 'Sugar')
269 $pSugar += $percent;
270 }
271 $fermentables .= ']';
272 return $fermentables;
273 }
274
275
276
277 function recipe_hops($recipe)
278 {
279 global $db;
280
281 $hops = "[";
282 $comma = FALSE;
283 foreach ($recipe->HOPS->HOP as $hop) {
284 if ($comma)
285 $hops .= ',';
286 $comma = TRUE;
287 $hops .= '{"h_name":"' . mysqli_real_escape_string($db, $hop->NAME) . '"';
288 $hops .= ',"h_amount":' . floatval($hop->AMOUNT);
289 $hops .= ',"h_cost":' . floatval($hop->COST);
290
291 if ($hop->TYPE == 'Aroma')
292 $hops .= ',"h_type":1';
293 else if ($hop->TYPE == 'Both')
294 $hops .= ',"h_type":2';
295 else
296 $hops .= ',"h_type":0'; // Default bittering
297
298 if ($hop->FORM == 'Pellet')
299 $hops .= ',"h_form":0';
300 else if ($hop->FORM == 'Plug')
301 $hops .= ',"h_form":1';
302 else
303 $hops .= ',"h_form":2'; // Default.
304
305 if ($hop->USE == 'Mash')
306 $hops .= ',"h_useat":0';
307 else if (($hop->USE == 'First wort') || ($hop->USE == 'First Wort'))
308 $hops .= ',"h_useat":1';
309 else if ($hop->USE == 'Aroma')
310 $hops .= ',"h_useat":3';
311 else if ($hop->USE == 'Whirlpool')
312 $hops .= ',"h_useat":4';
313 else if (($hop->USE == 'Dry hop') || ($hop->USE == 'Dry Hop'))
314 $hops .= ',"h_useat":5';
315 else
316 $hops .= ',"h_useat":2'; // Default boil.
317
318 if ($hop->TIME)
319 $hops .= ',"h_time":' . floatval($hop->TIME);
320 else
321 $hops .= ',"h_time":0';
322 $hops .= ',"h_alpha":' . floatval($hop->ALPHA);
323 $hops .= ',"h_beta":' . floatval($hop->BETA);
324 $hops .= ',"h_hsi":' . floatval($hop->HSI);
325 $hops .= ',"h_humulene":' . floatval($hop->HUMULENE);
326 $hops .= ',"h_caryophyllene":' . floatval($hop->CAROPHYLLENE);
327 $hops .= ',"h_cohumulone":' . floatval($hop->COHUMULONE);
328 $hops .= ',"h_myrcene":' . floatval($hop->MYRCENE);
329 if ($hop->TOTAL_OIL)
330 $hops .= ',"h_total_oil":' . floatval($hop->TOTAL_OIL);
331 else
332 $hops .= ',"h_total_oil":0';
333 if ($hop->ORIGIN)
334 $hops .= ',"h_origin":"' . mysqli_real_escape_string($db, $hop->ORIGIN) . '"';
335 else
336 $hops .= ',"h_origin":""';
337 $hops .= "}";
338 }
339 $hops .= ']';
340 return $hops;
341 }
342
343
344
345 function recipe_miscs($recipe)
346 {
347 global $db;
348
349 $miscs = "[";
350 $comma = FALSE;
351 foreach ($recipe->MISCS->MISC as $misc) {
352 if ($comma)
353 $miscs .= ',';
354 $comma = TRUE;
355 $mname = mysqli_real_escape_string($db, $misc->NAME);
356 $miscs .= '{"m_name":"' . $mname . '"';
357 $miscs .= ',"m_amount":' . floatval($misc->AMOUNT);
358 if ($misc->COST) {
359 $miscs .= ',"m_cost":' . floatval($misc->COST);
360 } else {
361 /* Brouwhulp bug, added water agents have no cost field. */
362 if (($misc->TYPE == "Water agent") || ($misc->TYPE == "Water Agent")) {
363 $miscs .= ',"m_cost":'. get_miscs_cost($mname);
364 } else {
365 $miscs .= ',"m_cost":0';
366 }
367 }
368
369 if ($misc->TYPE == 'Spice')
370 $miscs .= ',"m_type":0';
371 else if ($misc->TYPE == 'Herb')
372 $miscs .= ',"m_type":1';
373 else if ($misc->TYPE == 'Flavor')
374 $miscs .= ',"m_type":2';
375 else if ($misc->TYPE == 'Fining')
376 $miscs .= ',"m_type":3';
377 else if (($misc->TYPE == 'Water agent') || ($misc->TYPE == 'Water Agent'))
378 $miscs .= ',"m_type":4';
379 else if (($misc->TYPE == 'Yeast nutrient') || ($misc->TYPE == 'Yeast Nutrient'))
380 $miscs .= ',"m_type":5';
381 else if ($misc->TYPE == 'Other')
382 $miscs .= ',"m_type":6';
383 else
384 echo "Unknown TYPE " . $misc->TYPE . "<br />";
385
386 if ($misc->USE == 'Starter')
387 $miscs .= ',"m_use_use":0';
388 else if ($misc->USE == 'Mash')
389 $miscs .= ',"m_use_use":1';
390 else if ($misc->USE == 'Primary')
391 $miscs .= ',"m_use_use":3';
392 else if ($misc->USE == 'Secondary')
393 $miscs .= ',"m_use_use":4';
394 else if ($misc->USE == 'Bottling')
395 $miscs .= ',"m_use_use":5';
396 else
397 $miscs .= ',"m_use_use":2'; // Default boil. BrewFather defines 'Sparge', we don't support this even if it's a nice idea.
398
399 ($misc->AMOUNT_IS_WEIGHT== "TRUE") ? $miscs .= ',"m_amount_is_weight":1' : $miscs.= ',"m_amount_is_weight":0';
400 if ($misc->TIME) {
401 $miscs .= ',"m_time":' . floatval($misc->TIME);
402 } else
403 $miscs .= ',"m_time":0';
404 $miscs .= "}";
405 }
406 $miscs .= ']';
407 return $miscs;
408 }
409
410
411
412 function recipe_yeasts($recipe)
413 {
414 global $db;
415 global $svg;
416
417 $yeasts = "[";
418 $comma = FALSE;
419 foreach ($recipe->YEASTS->YEAST as $yeast) {
420 if ($comma)
421 $yeasts .= ',';
422 $comma = TRUE;
423 $yeasts .= '{"y_name":"' . mysqli_real_escape_string($db, $yeast->NAME) . '"';
424 if ($yeast->FORM == "Liquid") {
425 $paks = floatval($yeast->AMOUNT) / 0.0588;
426 $yeasts .= ',"y_amount":' . $paks;
427 $yeasts .= ',"y_cost":' . floatval($yeast->COST);
428 } else {
429 $yeasts .= ',"y_amount":' . floatval($yeast->AMOUNT);
430 $yeasts .= ',"y_cost":' . floatval($yeast->COST) * 1000;
431 }
432
433 $yeasts .= ',"y_laboratory":"' . mysqli_real_escape_string($db, $yeast->LABORATORY) . '"';
434 $yeasts .= ',"y_product_id":"' . mysqli_real_escape_string($db, $yeast->PRODUCT_ID) . '"';
435
436 if ($yeast->TYPE == 'Lager')
437 $yeasts .= ',"y_type":0';
438 else if ($yeast->TYPE == 'Ale')
439 $yeasts .= ',"y_type":1';
440 else if ($yeast->TYPE == 'Wheat')
441 $yeasts .= ',"y_type":2';
442 else if ($yeast->TYPE == 'Wine')
443 $yeasts .= ',"y_type":3';
444 else if ($yeast->TYPE == 'Champagne')
445 $yeasts .= ',"y_type":4';
446 else
447 echo "Unknown TYPE " . $yeast->TYPE . "<br />";
448
449 if ($yeast->FORM == 'Liquid') {
450 if ($yeast->LABORATORY == 'Imperial Yeast')
451 $yeasts .= ',"y_form":0,"y_cells":200000000000';
452 else
453 $yeasts .= ',"y_form":0,"y_cells":100000000000';
454 } else if ($yeast->FORM == 'Dry')
455 $yeasts .= ',"y_form":1,"y_cells":15000000000';
456 else if ($yeast->FORM == 'Slant')
457 $yeasts .= ',"y_form":2,"y_cells":1700000000';
458 else if ($yeast->FORM == 'Culture')
459 $yeasts .= ',"y_form":3,"y_cells":1700000000';
460 else if ($yeast->FORM == 'Frozen')
461 $yeasts .= ',"y_form":4,"y_cells":1700000000';
462 else if ($yeast->FORM == 'Bottle')
463 $yeasts .= ',"y_form":5,"y_cells":1700000000';
464 else
465 echo "Unknown FORM " . $yeast->FORM . "<br />";
466
467 if ($yeast->FLOCCULATION == 'Medium')
468 $yeasts .= ',"y_flocculation":1';
469 else if ($yeast->FLOCCULATION == 'High')
470 $yeasts .= ',"y_flocculation":2';
471 else if ($yeast->FLOCCULATION == 'Very high')
472 $yeasts .= ',"y_flocculation":3';
473 else
474 $yeasts .= ',"y_flocculation":0'; // Low, default.
475
476 if ($yeast->PRODUCT_ID=="F2" || $yeast->PRODUCT_ID=="CBC-1") {
477 $yeasts .= ',"y_use":3'; // Bottle
478 } else if ($yeast->ADD_TO_SECONDARY=="TRUE") {
479 $yeasts .= ',"y_use":1'; // Secondary
480 } else {
481 $yeasts .= ',"y_use":0'; // Primary
482 $svg = floatval($yeast->ATTENUATION);
483 }
484 $yeasts .= ',"y_min_temperature":' . floatval($yeast->MIN_TEMPERATURE);
485 $yeasts .= ',"y_max_temperature":' . floatval($yeast->MAX_TEMPERATURE);
486 $yeasts .= ',"y_attenuation":' . floatval($yeast->ATTENUATION);
487 $yeasts .= "}";
488 }
489 $yeasts .= ']';
490 return $yeasts;
491 }
492
493
494
495 function recipe_waters($recipe, $db)
496 {
497 $waters = "";
498 $index = 0;
499 foreach ($recipe->WATERS->WATER as $water) {
500 $index++;
501 $waters .= "', w" . $index . "_name='" . mysqli_real_escape_string($db, $water->NAME);
502 $waters .= "', w" . $index . "_amount='" . floatval($water->AMOUNT);
503 if ($water->CALCIUM)
504 $waters .= "', w" . $index . "_calcium='" . floatval($water->CALCIUM);
505 if ($water->SULFATE)
506 $waters .= "', w" . $index . "_sulfate='" . floatval($water->SULFATE);
507 if ($water->CHLORIDE)
508 $waters .= "', w" . $index . "_chloride='" . floatval($water->CHLORIDE);
509 if ($water->SODIUM)
510 $waters .= "', w" . $index . "_sodium='" . floatval($water->SODIUM);
511 if ($water->MAGNESIUM)
512 $waters .= "', w" . $index . "_magnesium='" . floatval($water->MAGNESIUM);
513 if ($water->PH)
514 $waters .= "', w" . $index . "_ph='" . floatval($water->PH);
515 if ($water->TOTAL_ALKALINITY)
516 $waters .= "', w" . $index . "_total_alkalinity='" . floatval($water->TOTAL_ALKALINITY);
517 if ($water->COST)
518 $waters .= "', w" . $index . "_cost='" . floatval($water->COST);
519 else
520 $waters .= "', w" . $index . "_cost='0";
521 }
522 return $waters;
523 }
524
525
526
527 function recipe_mash_steps($recipe)
528 {
529 global $db;
530
531 $steps = '[';
532 $comma = FALSE;
533 foreach ($recipe->MASH->MASH_STEPS->MASH_STEP as $step) {
534 if ($comma)
535 $steps .= ',';
536 $comma = TRUE;
537 $steps .= '{"step_name":"' . mysqli_real_escape_string($db, $step->NAME) . '"';
538
539 if ($step->TYPE == 'Infusion')
540 $steps .= ',"step_type":0';
541 else if ($step->TYPE == 'Temperature')
542 $steps .= ',"step_type":1';
543 else if ($step->TYPE == 'Decoction')
544 $steps .= ',"step_type":2';
545 else
546 echo "Unknown step TYPE " . $step->TYPE . "<br />";
547
548 if ($step->INFUSE_AMOUNT)
549 $steps .= ',"step_infuse_amount":' . floatval($step->INFUSE_AMOUNT);
550 else
551 $steps .= ',"step_infuse_amount":0';
552 if ($step->STEP_TEMP)
553 $steps .= ',"step_temp":' . floatval($step->STEP_TEMP);
554 if ($step->STEP_TIME)
555 $steps .= ',"step_time":' . floatval($step->STEP_TIME);
556 if ($step->RAMP_TIME)
557 $steps .= ',"ramp_time":' . floatval($step->RAMP_TIME);
558 if ($step->END_TEMP)
559 $steps .= ',"end_temp":' . floatval($step->END_TEMP);
560 $steps .= "}";
561 }
562 $steps .= ']';
563 return $steps;
564 }
565
566
567 $imported = 0;
568 $recipes = simplexml_load_file($target_file);
569 foreach ($recipes->RECIPE as $recipe) {
570 $f_sugars = 0;
571 $efficiency = 75;
572 $batch_size = 20;
573 $boil_size = 22;
574 $pCara = 0;
575 $pSugar = 0;
576 $svg = 77;
577 $colorw = 0;
578 $uuid = str_replace("\n", "", file_get_contents('/proc/sys/kernel/random/uuid'));
579
580 $sql = "INSERT INTO recipes SET uuid='" . $uuid;
581 $sql .= "', name='" . mysqli_real_escape_string($db, $recipe->NAME);
582 $sql .= "', locked='0";
583 if ($recipe->NOTES)
584 $sql .= "', notes='" . mysqli_real_escape_string($db, $recipe->NOTES);
585 else
586 $sql .= "', notes='";
587
588 if ($recipe->TYPE == 'Extract')
589 $sql .= "', type='0";
590 else if ($recipe->TYPE == 'Partial Mash')
591 $sql .= "', type='1";
592 else if ($recipe->TYPE == 'All Grain')
593 $sql .= "', type='2";
594 else
595 echo "Unknown TYPE " . $recipe->TYPE . "<br />";
596
597 if ($recipe->BATCH_SIZE)
598 $batch_size = floatval($recipe->BATCH_SIZE);
599 $sql .= "', batch_size='" . $batch_size;
600 if ($recipe->BOIL_SIZE)
601 $boil_size = floatval($recipe->BOIL_SIZE);
602 $sql .= "', boil_size='" . $boil_size;
603 if ($recipe->BOIL_TIME)
604 $sql .= "', boil_time='" . floatval($recipe->BOIL_TIME);
605 else
606 $sql .= "', boil_time='90";
607 if ($recipe->EFFICIENCY)
608 $efficiency = floatval($recipe->EFFICIENCY);
609 $sql .= "', efficiency='" . $efficiency;
610
611 $color_method = 0; // Morey
612 if ($recipe->COLOR_METHOD == 'Mosher') {
613 $color_method = 1;
614 } else if ($recipe->COLOR_METHOD == 'Daniels') {
615 $color_method = 2;
616 }
617 $sql .= "', color_method='" . $color_method;;
618
619
620 if ($recipe->IBU)
621 $sql .= "', est_ibu='" . floatval($recipe->IBU);
622 if ($recipe->IBU_METHOD == 'Rager')
623 $sql .= "', ibu_method='1";
624 else if ($recipe->IBU_METHOD == 'Daniels')
625 $sql .= "', ibu_method='2";
626 else
627 $sql .= "', ibu_method='0"; // Tinseth, default
628
629 if ($recipe->CARBONATION)
630 $sql .= "', est_carb='" . floatval($recipe->CARBONATION);
631
632 if ($recipe->STYLE) {
633 $sql .= recipe_style($recipe);
634 }
635 if ($recipe->CALC_ACID) {
636 ($recipe->CALC_ACID == "TRUE") ? $sql .= "', calc_acid='1" : $sql .= "', calc_acid='0";
637 }
638 if ($recipe->TARGET_PH) {
639 $sql .= "', mash_ph='" . floatval($recipe->TARGET_PH);
640 }
641 if ($recipe->SPARGE_ACID_TYPE && ($recipe->SPARGE_ACID_TYPE == "Lactic")) {
642 $sql .= "', sparge_acid_type='0";
643 } else if ($recipe->SPARGE_ACID_TYPE && ($recipe->SPARGE_ACID_TYPE == "Hydrochloric")) {
644 $sql .= "', sparge_acid_type='1";
645 } else if ($recipe->SPARGE_ACID_TYPE && ($recipe->SPARGE_ACID_TYPE == "Phosphoric")) {
646 $sql .= "', sparge_acid_type='2";
647 } else if ($recipe->SPARGE_ACID_TYPE && ($recipe->SPARGE_ACID_TYPE == "Sulfuric")) {
648 $sql .= "', sparge_acid_type='3";
649 }
650 if ($recipe->ACID_SPARGE_PERC) {
651 $sql .= "', sparge_acid_perc='" . floatval($recipe->ACID_SPARGE_PERC);
652 }
653 if ($recipe->LACTIC_SPARGE) {
654 $sql .= "', sparge_acid_amount='" . floatval($recipe->LACTIC_SPARGE);
655 }
656 if ($recipe->VOLUME_HLT) {
657 $sql .= "', sparge_volume='" . floatval($recipe->VOLUME_HLT);
658 }
659 $sql .= "', sparge_source='0";
660
661 /*
662 * Put all ingredients in json arrays
663 */
664 if ($recipe->FERMENTABLES)
665 $sql .= "', json_fermentables='" . recipe_fermentables($recipe);
666 if ($recipe->HOPS)
667 $sql .= "', json_hops='" . recipe_hops($recipe);
668 if ($recipe->MISCS)
669 $sql .= "', json_miscs='" . recipe_miscs($recipe);
670 if ($recipe->YEASTS)
671 $sql .= "', json_yeasts='" . recipe_yeasts($recipe);
672 if ($recipe->WATERS)
673 $sql .= recipe_waters($recipe, $db);
674 if ($recipe->MASH) {
675 $sql .= "',sparge_temp='" . floatval($recipe->MASH->SPARGE_TEMP);
676 $sql .= "',sparge_ph='" . floatval($recipe->MASH->PH);
677 if ($recipe->MASH->NAME)
678 $sql .= "',mash_name='" . mysqli_real_escape_string($db, $recipe->MASH->NAME);
679 if ($recipe->MASH->MASH_STEPS)
680 $sql .= "', json_mashs='" . recipe_mash_steps($recipe);
681 }
682
683 /*
684 * Added the calculated values
685 * OG, FG, color, IBU
686 */
687 $og = estimate_sg($f_sugars, $batch_size);
688 $sql .= "', est_og='" . floatval($og);
689 $fg = estimate_fg($pSugar, $pCara, 0, 0, 0, $svg, $og);
690 $sql .= "', est_fg='" . floatval($fg);
691 $abv = abvol($og, $fg);
692 $sql .= "', est_abv='" . floatval($abv);
693 $color = kw_to_ebc($color_method, $colorw);
694 $sql .= "', est_color='" . floatval($color);
695 $sql .= "';";
696 if (! $result = mysqli_query($db, $sql)) {
697 echo "Fout 6: " . mysqli_error($db) . "<br />";
698 syslog(LOG_NOTICE, "upl_recipe: result: ".mysqli_error($db));
699 } else {
700 echo "Recept `" . $recipe->NAME . "' toegevoegd<br />";
701 $lastid = mysqli_insert_id($db);
702 syslog(LOG_NOTICE, "upl_recipe: inserted record ".$lastid);
703 }
704 $imported++;
705 }
706 if ($imported == 0) {
707 echo "Fout 7: geen recepten in dit bestand.<br />";
708 }
709
710
711 // Don't clutter the upload directory.
712 unlink($target_file);
713
714 ?>

mercurial