www/upl_recipe.php

Tue, 31 Aug 2021 20:48:37 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 31 Aug 2021 20:48:37 +0200
changeset 774
92e1e8f175a2
parent 709
e251cceec547
permissions
-rw-r--r--

Split batch, adjust mash step volume. In the duplicated log_brew handle the missing values. In save product, round the mash step sg to 4 decimals. In prod_edit, ingredients are stored as strings, not arrays. This triggered a memory corruption that only happened in rare circumstances. Don't fix mash step fields in the javascript, it is already done during load from the database. Calculation of the mash volume is rounded to 6 decimals. Enter mash step Brix/Plato value, the SG result is rounded to 4 decimals.

<?php
require_once('config.php');
require("version.php");
require("includes/formulas.php");


$target_dir = "tmp/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if file already exists
if (file_exists($target_file)) {
	echo "Fout 1: bestand bestaat al. ";
	$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000 && $uploadOk) {
	echo "Fout 2: het bestand is te groot. ";
	$uploadOk = 0;
}
// Allow certain file formats
if ($imageFileType != "xml" && $uploadOk) {
	echo "Fout 3: alleen XML bestanden toegestaan. ";
	$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
	exit;
}

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
	echo "Verwerken bestand ". basename( $_FILES["fileToUpload"]["name"]). "<br />";
} else {
	echo "Fout 4: er ging iets fout met de upload.";
	exit;
}


$db = mysqli_connect(DBASE_HOST, DBASE_USER, DBASE_PASS, DBASE_NAME);
if (! $db) {
	echo "Fout 5: ".mysqli_connect_errno()." ".mysqli_connect_error();
	exit;
}
mysqli_set_charset($db, "utf8" );
date_default_timezone_set('Europe/Amsterdam');


$pCara = 0;
$pSugar = 0;
$svg = 77;
$efficiency = 75;
$batch_size = 20;
$colorw = 0;
$f_sugars = 0;



function get_miscs_cost($miscname) {
	global $db;

	if ($result = mysqli_query($db, "SELECT cost FROM inventory_miscs WHERE name='$miscname'")) {
		if ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
			return floatval($row['cost']);
		}
	}
	return 0.0;
}



function recipe_style($recipe)
{
	global  $db;

	if ($recipe->STYLE->NAME)
		$sql  = "', st_name='" . mysqli_real_escape_string($db, $recipe->STYLE->NAME);
	if ($recipe->STYLE->STYLE_LETTER)
		$sql .= "', st_letter='" . mysqli_real_escape_string($db, $recipe->STYLE->STYLE_LETTER);
	if ($recipe->STYLE->STYLE_GUIDE) {
		if ($recipe->STYLE->STYLE_GUIDE == "Biertypengids Derek Walsh") {
			$sql .= "', st_guide='BKG 2019";
		} else if ($recipe->STYLE->STYLE_GUIDE == "BKG Biertypen") {
			$sql .= "', st_guide='BKG 2019";
		} else if ($recipe->STYLE->STYLE_GUIDE == "BKG 2015") {
			$sql .= "', st_guide='BKG 2019";
		} else {
			$sql .= "', st_guide='" . mysqli_real_escape_string($db, $recipe->STYLE->STYLE_GUIDE);
		}
	} else {
		$sql .= "', st_guide='BKG 2019";
	}
	if ($recipe->STYLE->CATEGORY)
		$sql .= "', st_category='" . mysqli_real_escape_string($db, $recipe->STYLE->CATEGORY);
	if ($recipe->STYLE->CATEGORY_NUMBER)
		$sql .= "', st_category_number='" . floatval($recipe->STYLE->CATEGORY_NUMBER);
	else
		$sql .= "', st_category_number='0";
	if ($recipe->STYLE->TYPE == 'Lager')
		$sql .= "', st_type='0";
	else if (($recipe->STYLE->TYPE == 'Ale') || ($recipe->STYLE->TYPE == 'Strong Ale'))
		$sql .= "', st_type='1";
	else if ($recipe->STYLE->TYPE == 'Mead')
		$sql .= "', st_type='2";
	else if ($recipe->STYLE->TYPE == 'Wheat')
		$sql .= "', st_type='3";
	else if ($recipe->STYLE->TYPE == 'Mixed')
		$sql .= "', st_type='4";
	else if ($recipe->STYLE->TYPE == 'Cider')
		$sql .= "', st_type='5";
	else
		echo "Unknown TYPE " . $recipe->STYLE->TYPE . "<br />";

	if ($recipe->STYLE->OG_MIN)
		$sql .= "', st_og_min='" . floatval($recipe->STYLE->OG_MIN);
	if ($recipe->STYLE->OG_MAX)
		$sql .= "', st_og_max='" . floatval($recipe->STYLE->OG_MAX);
	if ($recipe->STYLE->FG_MIN)
		$sql .= "', st_fg_min='" . floatval($recipe->STYLE->FG_MIN);
	if ($recipe->STYLE->FG_MAX)
		$sql .= "', st_fg_max='" . floatval($recipe->STYLE->FG_MAX);
	if ($recipe->STYLE->IBU_MIN)
		$sql .= "', st_ibu_min='" . floatval($recipe->STYLE->IBU_MIN);
	if ($recipe->STYLE->IBU_MAX)
		$sql .= "', st_ibu_max='" . floatval($recipe->STYLE->IBU_MAX);
	if ($recipe->STYLE->COLOR_MIN) {
		$srm = floatval($recipe->STYLE->COLOR_MIN);
		$sql .= "', st_color_min='" . srm_to_ebc($srm);
	}
	if ($recipe->STYLE->COLOR_MAX) {
		$srm = floatval($recipe->STYLE->COLOR_MAX);
		$sql .= "', st_color_max='" . srm_to_ebc($srm);
	}
	if ($recipe->STYLE->CARB_MIN)
		$sql .= "', st_carb_min='" . floatval($recipe->STYLE->CARB_MIN);
	if ($recipe->STYLE->CARB_MAX)
		$sql .= "', st_carb_max='" . floatval($recipe->STYLE->CARB_MAX);
	if ($recipe->STYLE->ABV_MIN)
		$sql .= "', st_abv_min='" . floatval($recipe->STYLE->ABV_MIN);
	if ($recipe->STYLE->ABV_MAX)
		$sql .= "', st_abv_max='" . floatval($recipe->STYLE->ABV_MAX);
	return $sql;
}



function recipe_fermentables($recipe)
{
	global  $db;
	global  $pCara;
	global  $pSugar;
	global  $batch_size;
	global  $efficiency;
	global  $colorw;
	global  $f_sugars;

	$fermentables = "[";
	$comma = FALSE;
	foreach ($recipe->FERMENTABLES->FERMENTABLE as $fermentable) {
		if ($comma)
			$fermentables .= ',';
		$comma = TRUE;
		$fermentables .= '{"f_name":"' . mysqli_real_escape_string($db, $fermentable->NAME) . '"';
		$fermentables .= ',"f_origin":"' . mysqli_real_escape_string($db, $fermentable->ORIGIN) . '"';
		$fermentables .= ',"f_supplier":"' . mysqli_real_escape_string($db, $fermentable->SUPPLIER) . '"';
		$famount = floatval($fermentable->AMOUNT);
		$fermentables .= ',"f_amount":' . $famount;
		$fermentables .= ',"f_cost":' . floatval($fermentable->COST);

		if ($fermentable->TYPE == 'Grain')
			$fermentables .= ',"f_type":0';
		else if ($fermentable->TYPE == 'Sugar')
			$fermentables .= ',"f_type":1';
		else if ($fermentable->TYPE == 'Extract')
			$fermentables .= ',"f_type":2';
		else if (($fermentable->TYPE == 'Dry extract') || ($fermentable->TYPE == 'Dry Extract'))
			$fermentables .= ',"f_type":3';
		else if ($fermentable->TYPE == 'Adjunct')
			$fermentables .= ',"f_type":4';
		else
			echo "Unknown TYPE " . $fermentable->TYPE . "<br / >";

		$fyield = floatval($fermentable->YIELD);
		$fermentables .= ',"f_yield":' . $fyield;
		if ($fermentable->COLOR) {
			$srm = floatval($fermentable->COLOR);
			$ebc = srm_to_ebc($srm);
		} else {
			$srm = 0;
			$ebc = 0;
		}
		$colorw += ($famount * $srm / $batch_size) * 8.34436;   /* Kleurwerking */
		$fermentables .= ',"f_color":' . $ebc;
		if ($fermentable->COARSE_FINE_DIFF)
			$fermentables .= ',"f_coarse_fine_diff":' . floatval($fermentable->COARSE_FINE_DIFF);
		else
			$fermentables .= ',"f_coarse_fine_diff":0';
		if ($fermentable->MOISTURE)
			$fmoisture = floatval($fermentable->MOISTURE);
		else
			$fmoisture = 0;
		$fermentables .= ',"f_moisture":' . $fmoisture;
		if ($fermentable->DIASTATIC_POWER)
			$fermentables .= ',"f_diastatic_power":' . floatval($fermentable->DIASTATIC_POWER);
		else
			$fermentables .= ',"f_diastatic_power":0';
		if ($fermentable->PROTEIN)
			$fermentables .= ',"f_protein":' . floatval($fermentable->PROTEIN);
		else
			$fermentables .= ',"f_protein":0';
		if ($fermentable->MAX_IN_BATCH)
			$fermentables .= ',"f_max_in_batch":' . floatval($fermentable->MAX_IN_BATCH);
		else
			$fermentables .= ',"f_max_in_batch":100.0';

		if ($fermentable->GRAINTYPE == 'Roast')
			$fermentables .= ',"f_graintype":1';
		else if ($fermentable->GRAINTYPE == 'Crystal')
			$fermentables .= ',"f_graintype":2';
		else if ($fermentable->GRAINTYPE == 'Kilned')
			$fermentables .= ',"f_graintype":3';
		else if (($fermentable->GRAINTYPE == 'Sour malt') || ($fermentable->GRAINTYPE == 'Sour Malt'))
			$fermentables .= ',"f_graintype":4';
		else if ($fermentable->GRAINTYPE == 'Special')
			$fermentables .= ',"f_graintype":5';
		else if (($fermentable->GRAINTYPE == 'No malt') || ($fermentable->GRAINTYPE == 'No Malt'))
			$fermentables .= ',"f_graintype":6';
		else
			$fermentables .= ',"f_graintype":0'; // Base, default

		if ($fermentable->ADDED == 'Boil')
			$fermentables .= ',"f_added":1';
		else if ($fermentable->ADDED == 'Fermentation')
			$fermentables .= ',"f_added":2';
		else if ($fermentable->ADDED == 'Lagering')
			$fermentables .= ',"f_added":3';
		else if ($fermentable->ADDED == 'Bottle')
			$fermentables .= ',"f_added":4';
		else
			$fermentables .= ',"f_added":0'; // Mash, default.

		($fermentable->ADD_AFTER_BOIL== "TRUE") ? $fermentables .= ',"f_add_after_boil":1' : $fermentables .= ',"f_add_after_boil":0';
		($fermentable->RECOMMEND_MASH== "TRUE") ? $fermentables .= ',"f_recommend_mash":1' : $fermentables .= ',"f_recommend_mash":0';
		if ($fermentable->DISSOLVED_PROTEIN)
			$fermentables .= ',"f_dissolved_protein":' . floatval($fermentable->DISSOLVED_PROTEIN);
		else
			$fermentables .= ',"f_dissolved_protein":0';
		($fermentable->ADJUST_TO_TOTAL_100 == "TRUE") ? $fermentables .= ',"f_adjust_to_total_100":1' : $fermentables .= ',"f_adjust_to_total_100":0';
		$percent = floatval($fermentable->PERCENTAGE);
		$fermentables .= ',"f_percentage":' . round($percent * 10) / 10.0;
		if ($fermentable->DI_pH)
			$fermentables .= ',"f_di_ph":' . floatval($fermentable->DI_pH);
		else
			$fermentables .= ',"f_di_ph":0.0';
		if ($fermentable->{'ACID_TO_pH_5.7'})
			$fermentables .= ',"f_acid_to_ph_57":' . floatval($fermentable->{'ACID_TO_pH_5.7'});
		else
			$fermentables .= ',"f_acid_to_ph_57":0.0';
		$fermentables .= "}";

		/* Sugars */
		$d = $famount * ($fyield / 100) * (1 - $fmoisture / 100);
		if ($fermentable->ADDED == 'Mash')
			$d = floatval($efficiency) / 100 * $d;
		$f_sugars += $d;
		if ($fermentable->GRAINTYPE == 'Crystal')
			$pCara += $percent;
		if ($fermentable->TYPE == 'Sugar')
			$pSugar += $percent;
	}
	$fermentables .= ']';
	return $fermentables;
}



function recipe_hops($recipe)
{
	global  $db;

	$hops = "[";
	$comma = FALSE;
	foreach ($recipe->HOPS->HOP as $hop) {
		if ($comma)
			$hops .= ',';
		$comma = TRUE;
		$hops .= '{"h_name":"' . mysqli_real_escape_string($db, $hop->NAME) . '"';
		$hops .= ',"h_amount":' . floatval($hop->AMOUNT);
		$hops .= ',"h_cost":' . floatval($hop->COST);

		if ($hop->TYPE == 'Aroma')
			$hops .= ',"h_type":1';
		else if ($hop->TYPE == 'Both')
			$hops .= ',"h_type":2';
		else
			$hops .= ',"h_type":0'; // Default bittering

		if ($hop->FORM == 'Pellet')
			$hops .= ',"h_form":0';
		else if ($hop->FORM == 'Plug')
			$hops .= ',"h_form":1';
		else
			$hops .= ',"h_form":2'; // Default.

		if ($hop->USE == 'Mash')
			$hops .= ',"h_useat":0';
		else if (($hop->USE == 'First wort') || ($hop->USE == 'First Wort'))
			$hops .= ',"h_useat":1';
		else if ($hop->USE == 'Aroma')
			$hops .= ',"h_useat":3';
		else if ($hop->USE == 'Whirlpool')
			$hops .= ',"h_useat":4';
		else if (($hop->USE == 'Dry hop') || ($hop->USE == 'Dry Hop'))
			$hops .= ',"h_useat":5';
		else
			$hops .= ',"h_useat":2'; // Default boil.

		if ($hop->TIME)
			$hops .= ',"h_time":' . floatval($hop->TIME);
		else
			$hops .= ',"h_time":0';
		$hops .= ',"h_alpha":' . floatval($hop->ALPHA);
		$hops .= ',"h_beta":' . floatval($hop->BETA);
		$hops .= ',"h_hsi":' . floatval($hop->HSI);
		$hops .= ',"h_humulene":' . floatval($hop->HUMULENE);
		$hops .= ',"h_caryophyllene":' . floatval($hop->CAROPHYLLENE);
		$hops .= ',"h_cohumulone":' . floatval($hop->COHUMULONE);
		$hops .= ',"h_myrcene":' . floatval($hop->MYRCENE);
		if ($hop->TOTAL_OIL)
			$hops .= ',"h_total_oil":' . floatval($hop->TOTAL_OIL);
		else
			$hops .= ',"h_total_oil":0';
		if ($hop->ORIGIN)
			$hops .= ',"h_origin":"' . mysqli_real_escape_string($db, $hop->ORIGIN) . '"';
		else
			$hops .= ',"h_origin":""';
		$hops .= "}";
	}
	$hops .= ']';
	return $hops;
}



function recipe_miscs($recipe)
{
	global  $db;

	$miscs = "[";
	$comma = FALSE;
	foreach ($recipe->MISCS->MISC as $misc) {
		if ($comma)
			$miscs .= ',';
		$comma = TRUE;
		$mname = mysqli_real_escape_string($db, $misc->NAME);
		$miscs .= '{"m_name":"' . $mname . '"';
		$miscs .= ',"m_amount":' . floatval($misc->AMOUNT);
		if ($misc->COST) {
			$miscs .= ',"m_cost":' . floatval($misc->COST);
		} else {
			/* Brouwhulp bug, added water agents have no cost field. */
			if (($misc->TYPE == "Water agent") || ($misc->TYPE == "Water Agent")) {
				$miscs .= ',"m_cost":'. get_miscs_cost($mname);
			} else {
				$miscs .= ',"m_cost":0';
			}
		}

		if ($misc->TYPE == 'Spice')
			$miscs .= ',"m_type":0';
		else if ($misc->TYPE == 'Herb')
			$miscs .= ',"m_type":1';
		else if ($misc->TYPE == 'Flavor')
			$miscs .= ',"m_type":2';
		else if ($misc->TYPE == 'Fining')
			$miscs .= ',"m_type":3';
		else if (($misc->TYPE == 'Water agent') || ($misc->TYPE == 'Water Agent'))
			$miscs .= ',"m_type":4';
		else if (($misc->TYPE == 'Yeast nutrient') || ($misc->TYPE == 'Yeast Nutrient'))
			$miscs .= ',"m_type":5';
		else if ($misc->TYPE == 'Other')
			$miscs .= ',"m_type":6';
		else
			echo "Unknown TYPE " . $misc->TYPE . "<br />";

		if ($misc->USE == 'Starter')
			$miscs .= ',"m_use_use":0';
		else if ($misc->USE == 'Mash')
			$miscs .= ',"m_use_use":1';
		else if ($misc->USE == 'Primary')
			$miscs .= ',"m_use_use":3';
		else if ($misc->USE == 'Secondary')
			$miscs .= ',"m_use_use":4';
		else if ($misc->USE == 'Bottling')
			$miscs .= ',"m_use_use":5';
		else
			$miscs .= ',"m_use_use":2'; // Default boil. BrewFather defines 'Sparge', we don't support this even if it's a nice idea.

		($misc->AMOUNT_IS_WEIGHT== "TRUE") ? $miscs .= ',"m_amount_is_weight":1' : $miscs.= ',"m_amount_is_weight":0';
		if ($misc->TIME) {
			$miscs .= ',"m_time":' . floatval($misc->TIME);
		} else
			$miscs .= ',"m_time":0';
		$miscs .= "}";
	}
	$miscs .= ']';
	return $miscs;
}



function recipe_yeasts($recipe)
{
	global  $db;
	global  $svg;

	$yeasts = "[";
	$comma = FALSE;
	foreach ($recipe->YEASTS->YEAST as $yeast) {
		if ($comma)
			$yeasts .= ',';
		$comma = TRUE;
		$yeasts .= '{"y_name":"' . mysqli_real_escape_string($db, $yeast->NAME) . '"';
		if ($yeast->FORM == "Liquid") {
			$paks = floatval($yeast->AMOUNT) / 0.0588;
		$yeasts .= ',"y_amount":' . $paks;
			$yeasts .= ',"y_cost":' . floatval($yeast->COST);
		} else {
			$yeasts .= ',"y_amount":' . floatval($yeast->AMOUNT);
			$yeasts .= ',"y_cost":' . floatval($yeast->COST) * 1000;
		}

		$yeasts .= ',"y_laboratory":"' . mysqli_real_escape_string($db, $yeast->LABORATORY) . '"';
		$yeasts .= ',"y_product_id":"' . mysqli_real_escape_string($db, $yeast->PRODUCT_ID) . '"';

		if ($yeast->TYPE == 'Lager')
			$yeasts .= ',"y_type":0';
		else if ($yeast->TYPE == 'Ale')
			$yeasts .= ',"y_type":1';
		else if ($yeast->TYPE == 'Wheat')
			$yeasts .= ',"y_type":2';
		else if ($yeast->TYPE == 'Wine')
			$yeasts .= ',"y_type":3';
		else if ($yeast->TYPE == 'Champagne')
			$yeasts .= ',"y_type":4';
		else
			echo "Unknown TYPE " . $yeast->TYPE . "<br />";

		if ($yeast->FORM == 'Liquid') {
			if ($yeast->LABORATORY == 'Imperial Yeast')
				$yeasts .= ',"y_form":0,"y_cells":200000000000';
			else
				$yeasts .= ',"y_form":0,"y_cells":100000000000';
		} else if ($yeast->FORM == 'Dry')
			$yeasts .= ',"y_form":1,"y_cells":15000000000';
		else if ($yeast->FORM == 'Slant')
			$yeasts .= ',"y_form":2,"y_cells":1700000000';
		else if ($yeast->FORM == 'Culture')
			$yeasts .= ',"y_form":3,"y_cells":1700000000';
		else if ($yeast->FORM == 'Frozen')
			$yeasts .= ',"y_form":4,"y_cells":1700000000';
		else if ($yeast->FORM == 'Bottle')
			$yeasts .= ',"y_form":5,"y_cells":1700000000';
		else
			echo "Unknown FORM " . $yeast->FORM . "<br />";

		if ($yeast->FLOCCULATION == 'Medium')
			$yeasts .= ',"y_flocculation":1';
		else if ($yeast->FLOCCULATION == 'High')
			$yeasts .= ',"y_flocculation":2';
		else if ($yeast->FLOCCULATION == 'Very high')
			$yeasts .= ',"y_flocculation":3';
		else
			$yeasts .= ',"y_flocculation":0'; // Low, default.

		if ($yeast->PRODUCT_ID=="F2" || $yeast->PRODUCT_ID=="CBC-1") {
			$yeasts .= ',"y_use":3';        // Bottle
		} else if ($yeast->ADD_TO_SECONDARY=="TRUE") {
			$yeasts .= ',"y_use":1';        // Secondary
		} else {
			$yeasts .= ',"y_use":0';        // Primary
			$svg = floatval($yeast->ATTENUATION);
		}
		$yeasts .= ',"y_min_temperature":' . floatval($yeast->MIN_TEMPERATURE);
		$yeasts .= ',"y_max_temperature":' . floatval($yeast->MAX_TEMPERATURE);
		$yeasts .= ',"y_attenuation":' . floatval($yeast->ATTENUATION);
		$yeasts .= "}";
	}
	$yeasts .= ']';
	return $yeasts;
}



function recipe_waters($recipe, $db)
{
	$waters = "";
	$index = 0;
	foreach ($recipe->WATERS->WATER as $water) {
		$index++;
		$waters .= "', w" . $index . "_name='" . mysqli_real_escape_string($db, $water->NAME);
		$waters .= "', w" . $index . "_amount='" . floatval($water->AMOUNT);
		if ($water->CALCIUM)
			$waters .= "', w" . $index . "_calcium='" . floatval($water->CALCIUM);
		if ($water->SULFATE)
			$waters .= "', w" . $index . "_sulfate='" . floatval($water->SULFATE);
		if ($water->CHLORIDE)
			$waters .= "', w" . $index . "_chloride='" . floatval($water->CHLORIDE);
		if ($water->SODIUM)
			$waters .= "', w" . $index . "_sodium='" . floatval($water->SODIUM);
		if ($water->MAGNESIUM)
			$waters .= "', w" . $index . "_magnesium='" . floatval($water->MAGNESIUM);
		if ($water->PH)
			$waters .= "', w" . $index . "_ph='" . floatval($water->PH);
		if ($water->TOTAL_ALKALINITY)
			$waters .= "', w" . $index . "_total_alkalinity='" . floatval($water->TOTAL_ALKALINITY);
		if ($water->COST)
			$waters .= "', w" . $index . "_cost='" . floatval($water->COST);
		else
			$waters .= "', w" . $index . "_cost='0";
	}
	return $waters;
}



function recipe_mash_steps($recipe)
{
	global  $db;

	$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 == 'Infusion')
			$steps .= ',"step_type":0';
		else if ($step->TYPE == 'Temperature')
			$steps .= ',"step_type":1';
		else if ($step->TYPE == 'Decoction')
			$steps .= ',"step_type":2';
		else
			echo "Unknown step TYPE " . $step->TYPE . "<br />";

		if ($step->INFUSE_AMOUNT)
			$steps .= ',"step_infuse_amount":' . floatval($step->INFUSE_AMOUNT);
		else
			$steps .= ',"step_infuse_amount":0';
		if ($step->STEP_TEMP)
			$steps .= ',"step_temp":' . floatval($step->STEP_TEMP);
		if ($step->STEP_TIME)
			$steps .= ',"step_time":' . floatval($step->STEP_TIME);
		if ($step->RAMP_TIME)
			$steps .= ',"ramp_time":' . floatval($step->RAMP_TIME);
		if ($step->END_TEMP)
			$steps .= ',"end_temp":' . floatval($step->END_TEMP);
		$steps .= "}";
	}
	$steps .= ']';
	return $steps;
}


$imported = 0;
$recipes = simplexml_load_file($target_file);
foreach ($recipes->RECIPE as $recipe) {
	$f_sugars = 0;
	$efficiency = 75;
	$batch_size = 20;
	$boil_size = 22;
	$pCara = 0;
	$pSugar = 0;
	$svg = 77;
	$colorw = 0;
	$uuid = str_replace("\n", "", file_get_contents('/proc/sys/kernel/random/uuid'));

	$sql  = "INSERT INTO recipes SET uuid='" . $uuid;
	$sql .= "', name='" . mysqli_real_escape_string($db, $recipe->NAME);
	$sql .= "', locked='0";
	if ($recipe->NOTES)
		$sql .= "', notes='" . mysqli_real_escape_string($db, $recipe->NOTES);
	else
		$sql .= "', notes='";

	if ($recipe->TYPE == 'Extract')
		$sql .= "', type='0";
	else if ($recipe->TYPE == 'Partial Mash')
		$sql .= "', type='1";
	else if ($recipe->TYPE == 'All Grain')
		$sql .= "', type='2";
	else
		echo "Unknown TYPE " . $recipe->TYPE . "<br />";

	if ($recipe->BATCH_SIZE)
		$batch_size = floatval($recipe->BATCH_SIZE);
	$sql .= "', batch_size='" . $batch_size;
	if ($recipe->BOIL_SIZE)
		$boil_size = floatval($recipe->BOIL_SIZE);
	$sql .= "', boil_size='" . $boil_size;
	if ($recipe->BOIL_TIME)
		$sql .= "', boil_time='" . floatval($recipe->BOIL_TIME);
	else
		$sql .= "', boil_time='90";
	if ($recipe->EFFICIENCY)
		$efficiency = floatval($recipe->EFFICIENCY);
	$sql .= "', efficiency='" . $efficiency;

	$color_method = 0; // Morey
	if ($recipe->COLOR_METHOD == 'Mosher') {
		$color_method = 1;
	} else if ($recipe->COLOR_METHOD == 'Daniels') {
		$color_method = 2;
	}
	$sql .= "', color_method='" . $color_method;;


	if ($recipe->IBU)
		$sql .= "', est_ibu='" . floatval($recipe->IBU);
	if ($recipe->IBU_METHOD == 'Rager')
		$sql .= "', ibu_method='1";
	else if ($recipe->IBU_METHOD == 'Daniels')
		$sql .= "', ibu_method='2";
	else
		$sql .= "', ibu_method='0";  // Tinseth, default

	if ($recipe->CARBONATION)
		$sql .= "', est_carb='" . floatval($recipe->CARBONATION);

	if ($recipe->STYLE) {
		$sql .= recipe_style($recipe);
	}
	if ($recipe->CALC_ACID) {
		($recipe->CALC_ACID == "TRUE") ? $sql .= "', calc_acid='1" : $sql .= "', calc_acid='0";
	}
	if ($recipe->TARGET_PH) {
		$sql .= "', mash_ph='" . floatval($recipe->TARGET_PH);
	}
	if ($recipe->SPARGE_ACID_TYPE && ($recipe->SPARGE_ACID_TYPE == "Lactic")) {
		$sql .= "', sparge_acid_type='0";
	} else if ($recipe->SPARGE_ACID_TYPE && ($recipe->SPARGE_ACID_TYPE == "Hydrochloric")) {
		$sql .= "', sparge_acid_type='1";
	} else if ($recipe->SPARGE_ACID_TYPE && ($recipe->SPARGE_ACID_TYPE == "Phosphoric")) {
		$sql .= "', sparge_acid_type='2";
	} else if ($recipe->SPARGE_ACID_TYPE && ($recipe->SPARGE_ACID_TYPE == "Sulfuric")) {
		$sql .= "', sparge_acid_type='3";
	}
	if ($recipe->ACID_SPARGE_PERC) {
		$sql .= "', sparge_acid_perc='" . floatval($recipe->ACID_SPARGE_PERC);
	}
	if ($recipe->LACTIC_SPARGE) {
		$sql .= "', sparge_acid_amount='" . floatval($recipe->LACTIC_SPARGE);
	}
	if ($recipe->VOLUME_HLT) {
		$sql .= "', sparge_volume='" . floatval($recipe->VOLUME_HLT);
	}
	$sql .= "', sparge_source='0";

	/*
	 * Put all ingredients in json arrays
	 */
	if ($recipe->FERMENTABLES)
		$sql .= "', json_fermentables='" . recipe_fermentables($recipe);
	if ($recipe->HOPS)
		$sql .= "', json_hops='" . recipe_hops($recipe);
	if ($recipe->MISCS)
		$sql .= "', json_miscs='" . recipe_miscs($recipe);
	if ($recipe->YEASTS)
		$sql .= "', json_yeasts='" . recipe_yeasts($recipe);
	if ($recipe->WATERS)
		$sql .= recipe_waters($recipe, $db);
	if ($recipe->MASH) {
		$sql .= "',sparge_temp='" . floatval($recipe->MASH->SPARGE_TEMP);
		$sql .= "',sparge_ph='" . floatval($recipe->MASH->PH);
		if ($recipe->MASH->NAME)
			$sql .= "',mash_name='" . mysqli_real_escape_string($db, $recipe->MASH->NAME);
		if ($recipe->MASH->MASH_STEPS)
			$sql .= "', json_mashs='" . recipe_mash_steps($recipe);
	}

	/*
	 * Added the calculated values
	 *  OG, FG, color, IBU
	 */
	$og = estimate_sg($f_sugars, $batch_size);
	$sql .= "', est_og='" . floatval($og);
	$fg = estimate_fg($pSugar, $pCara, 0, 0, 0, $svg, $og);
	$sql .= "', est_fg='" . floatval($fg);
	$abv = abvol($og, $fg);
	$sql .= "', est_abv='" . floatval($abv);
	$color = kw_to_ebc($color_method, $colorw);
	$sql .= "', est_color='" . floatval($color);
	$sql .= "';";
	if (! $result = mysqli_query($db, $sql)) {
		echo "Fout 6: " . mysqli_error($db) . "<br />";
		syslog(LOG_NOTICE, "upl_recipe: result: ".mysqli_error($db));
	} else {
		echo "Recept `" . $recipe->NAME . "' toegevoegd<br />";
		$lastid = mysqli_insert_id($db);
		syslog(LOG_NOTICE, "upl_recipe: inserted record ".$lastid);
	}
	$imported++;
}
if ($imported == 0) {
	echo "Fout 7: geen recepten in dit bestand.<br />";
}


// Don't clutter the upload directory.
unlink($target_file);

?>

mercurial