Fri, 31 Aug 2018 15:18:20 +0200
Finisched recipe import. Added recipe retrieve script.
www/import/from_brouwhulp.php | file | annotate | diff | comparison | revisions | |
www/includes/db_recipes.php | file | annotate | diff | comparison | revisions |
--- a/www/import/from_brouwhulp.php Thu Aug 30 22:22:09 2018 +0200 +++ b/www/import/from_brouwhulp.php Fri Aug 31 15:18:20 2018 +0200 @@ -436,6 +436,7 @@ $len_fermentables = 0; $len_hops = 0; $len_miscs = 0; + $len_yeasts = 0; $len_waters = 0; $len_mash = 0; @@ -599,12 +600,130 @@ } $miscs .= ']'; - echo $miscs . PHP_EOL; +// echo $miscs . PHP_EOL; $sql .= "', json_miscs='" . $miscs; if (strlen($miscs) > $len_miscs) $len_miscs = strlen($miscs); } + /* + * Put the yeasts in a json array + */ + if ($recipe->YEASTS) { + $yeasts = "["; + $comma = FALSE; + foreach ($recipe->YEASTS->YEAST as $yeast) { + if ($comma) + $yeasts .= ','; + $comma = TRUE; + $yeasts .= '{"name":"' . mysqli_real_escape_string($db, $yeast->NAME) . '"'; + $yeasts .= ',"amount":' . floatval($yeast->AMOUNT); + if ($yeast->COST) + $yeasts .= ',"cost":' . floatval($yeast->COST); + $yeasts .= ',"laboratory":"' . mysqli_real_escape_string($db, $yeast->LABORATORY) . '"'; + $yeasts .= ',"product_id":"' . mysqli_real_escape_string($db, $yeast->PRODUCT_ID) . '"'; + $yeasts .= ',"type":"' . mysqli_real_escape_string($db, $yeast->TYPE) . '"'; + $yeasts .= ',"form":"' . mysqli_real_escape_string($db, $yeast->FORM) . '"'; + ($yeast->AMOUNT_IS_WEIGHT== "TRUE") ? $yeasts .= ',"amount_is_weight":true' : $yeasts.= ',"amount_is_weight":false'; + if ($yeast->ADD_TO_SECONDARY=="FALSE") { + $yeasts .= ',"use":"Primary"'; + } else if ($yeast->PRODUCT_ID=="F2") { + $yeasts .= ',"use":"Bottle"'; + } else { + $yeasts .= ',"use":"Secondary"'; + } + $yeasts .= "}"; + } + + $yeasts .= ']'; +// echo $yeasts . PHP_EOL; + $sql .= "', json_yeasts='" . $yeasts; + if (strlen($yeasts) > $len_yeasts) + $len_yeasts = strlen($yeasts); + } + + /* + * Put the waters in a json array + */ + if ($recipe->WATERS) { + $waters = "["; + $comma = FALSE; + foreach ($recipe->WATERS->WATER as $water) { + if ($comma) + $waters .= ','; + $comma = TRUE; + $waters .= '{"name":"' . mysqli_real_escape_string($db, $water->NAME) . '"'; + $waters .= ',"amount":' . floatval($water->AMOUNT); + if ($water->CALCIUM) + $waters .= ',"calcium":' . floatval($water->CALCIUM); + if ($water->BICARBONATE) + $waters .= ',"bicarbonate":' . floatval($water->BICARBONATE); + if ($water->SULFATE) + $waters .= ',"sulfate":' . floatval($water->SULFATE); + if ($water->CHLORIDE) + $waters .= ',"chloride":' . floatval($water->CHLORIDE); + if ($water->SODIUM) + $waters .= ',"sodium":' . floatval($water->SODIUM); + if ($water->MAGNESIUM) + $waters .= ',"magnesium":' . floatval($water->MAGNESIUM); + if ($water->PH) + $waters .= ',"ph":' . floatval($water->PH); + if ($water->TOTAL_ALKALINITY) + $waters .= ',"total_alkalinity":' . floatval($water->TOTAL_ALKALINITY); + $waters .= "}"; + } + + $waters .= ']'; +// echo $waters . PHP_EOL; + $sql .= "', json_waters='" . $waters; + if (strlen($waters) > $len_waters) + $len_waters = strlen($waters); + } + + /* + * Put the mash in a json array + */ + if ($recipe->MASH) { + $mash = '{"sparge_temp":' . floatval($recipe->MASH->SPARGE_TEMP); + $mash .= ',"ph":' . floatval($recipe->MASH->PH); + if ($recipe->MASH->NAME) + $mash .= ',"name":"' . mysqli_real_escape_string($db, $recipe->MASH->NAME) . '"'; + if ($recipe->MASH->MASH_STEPS) { + $steps = ',"mash_steps":['; + $comma = FALSE; + foreach ($recipe->MASH->MASH_STEPS->MASH_STEP as $step) { + if ($comma) + $steps .= ','; + $comma = TRUE; + $steps .= '{"step_name":"' . mysqli_real_escape_string($db, $step->NAME) . '"'; + if ($step->TYPE) { + $steps .= ',"step_type":"' . $step->TYPE . '"'; + } + if ($step->STEP_TEMP) { + $steps .= ',"step_temp":"' . $step->STEP_TEMP . '"'; + } + if ($step->STEP_TIME) { + $steps .= ',"step_time":"' . $step->STEP_TIME . '"'; + } + if ($step->RAMP_TIME) { + $steps .= ',"ramp_time":"' . $step->RAMP_TIME . '"'; + } + if ($step->END_TEMP) { + $steps .= ',"end_temp":"' . $step->END_TEMP . '"'; + } + $steps .= "}"; + } + $steps .= ']'; + $mash .= $steps; + } + + $mash .= '}'; + echo $mash . PHP_EOL; + $sql .= "', json_mash='" . $mash; + if (strlen($mash) > $len_mash) + $len_mash = strlen($mash); + } + $sql .= "';"; if (! $result = mysqli_query($db, $sql)) { printf("Error: %s\n", mysqli_error($db)); @@ -613,6 +732,7 @@ echo "Fermentables: " . $len_fermentables . PHP_EOL; echo "Hops: " . $len_hops . PHP_EOL; echo "Miscs: " . $len_miscs . PHP_EOL; + echo "Yeasts: " . $len_yeasts . PHP_EOL; echo "Waters: " . $len_waters . PHP_EOL; echo "Mash: " . $len_mash . PHP_EOL; }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/www/includes/db_recipes.php Fri Aug 31 15:18:20 2018 +0200 @@ -0,0 +1,95 @@ +<?php + +//require($_SERVER['DOCUMENT_ROOT']."/config.php"); +//require($_SERVER['DOCUMENT_ROOT']."/version.php"); +require("../config.php"); +require("../version.php"); + + +#Connect to the database +$connect = mysqli_connect(DBASE_HOST, DBASE_USER, DBASE_PASS, DBASE_NAME); +if (! $connect) { + die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); +} + +// get data and store in a json array +$query = "SELECT * FROM recipes ORDER BY style_guide,style_letter,style_name,name"; +if (isset($_GET['insert'])) { +// // INSERT COMMAND +// $sql = "INSERT INTO `profile_mash` SET name='" . mysqli_real_escape_string($connect, $_GET['name']); +// $sql .= "', notes='" . mysqli_real_escape_string($connect, $_GET['notes']); +// $sql .= "', steps='" . json_encode($_GET['steps']); +// $sql .= "';"; +// $result = mysqli_query($connect, $sql); +// if (! $result) { +// syslog(LOG_NOTICE, "db_profile_mash: ".$sql." result: ".mysqli_error($connect)); +// } else { +// syslog(LOG_NOTICE, "db_profile_mash: inserted ".$_GET['name']); +// } +// echo $result; + +//} else if (isset($_GET['update'])) { +// // UPDATE COMMAND +// $sql = "UPDATE `profile_mash` SET name='" . mysqli_real_escape_string($connect, $_GET['name']); +// $sql .= "', notes='" . mysqli_real_escape_string($connect, $_GET['notes']); +// $sql .= "', steps='" . json_encode($_GET['steps']); +// $sql .= "' WHERE record='" . $_GET['record'] . "';"; +// $result = mysqli_query($connect, $sql); +// if (! $result) { +// syslog(LOG_NOTICE, "db_profile_mash: ".$sql." result: ".mysqli_error($connect)); +// } else { +// syslog(LOG_NOTICE, "db_profile_mash: updated record ".$_GET['record']); +// } +// echo $result; + +//} else if (isset($_GET['delete'])) { +// // DELETE COMMAND +// $sql = "DELETE FROM `profile_mash` WHERE record='".$_GET['record']."';"; +// $result = mysqli_query($connect, $sql); +// if (! $result) { +// syslog(LOG_NOTICE, "db_profile_mash: ".$sql." result: ".mysqli_error($connect)); +// } else { +// syslog(LOG_NOTICE, "db_profile_mash: deleted record ".$_GET['record']); +// } +// echo $result; + +} else { + // SELECT COMMAND + $result = mysqli_query($connect, $query) or die("SQL Error 1: " . mysqli_error($connect)); + $recipes = '['; + $comma = FALSE; + while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { + // Manual encode to JSON. + if ($comma) { + $recipes .= ','; + } + $comma = TRUE; + $recipes .= '{"record":' . $row['record']; + $recipes .= ',"style_guide":"' . $row['style_guide']; + $recipes .= '","style_letter":"' . $row['style_letter']; + $recipes .= '","style_name":"' . $row['style_name']; + $recipes .= '","name":"' . $row['name']; + $recipes .= '","notes":"' . $row['notes']; + $recipes .= '","type":"' . $row['type']; + $recipes .= '","batch_size":' . floatval($row['batch_size']); + $recipes .= ',"boil_time":' . floatval($row['boil_time']); + $recipes .= ',"efficiency":' . floatval($row['efficiency']); + $recipes .= ',"est_og":' . floatval($row['est_og']); + $recipes .= ',"est_fg":' . floatval($row['est_fg']); + $recipes .= ',"est_color":' . floatval($row['est_color']); + $recipes .= ',"color_method":"' . $row['color_method']; + $recipes .= '","est_ibu":' . floatval($row['est_ibu']); + $recipes .= ',"ibu_method":"' . $row['ibu_method']; + $recipes .= '","fermentables":' . $row['json_fermentables']; + $recipes .= ',"hops":' . $row['json_hops']; + $recipes .= ',"miscs":' . $row['json_miscs']; + $recipes .= ',"yeasts":' . $row['json_yeasts']; + $recipes .= ',"waters":' . $row['json_waters']; + $recipes .= ',"mash":' . $row['json_mash']; + $recipes .= '}'; + } + $recipes .= ']'; + header("Content-type: application/json"); + echo $recipes; +} +?>