www/import/from_brouwhulp.php

Tue, 18 Sep 2018 23:16:14 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 18 Sep 2018 23:16:14 +0200
changeset 56
d4f4762e59b3
parent 54
294dda7f1779
child 57
bb9a06aa9acd
permissions
-rw-r--r--

Start inline fermentables editor

<?php
/* 
 * Import inventory from brouwhulp
 */

require("../config.php");
require("../version.php");
require("../includes/formulas.php");

echo "Start adding data from brouwhulp\n";

$db = mysqli_connect(DBASE_HOST, DBASE_USER, DBASE_PASS, DBASE_NAME);
if (! $db) {
	die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}

$brouwhulp = '/home/mbroek/Documents/brouwhulp';


function do_fermentables()
{
	global $brouwhulp, $db;

	echo "  Start adding fermentables to the database\n";
	$sql = "TRUNCATE TABLE inventory_fermentables;";
	if (! $result = mysqli_query($db, $sql)) {
		printf("Error: %s\n", mysqli_error($db));
	}
	$myfermentables = simplexml_load_file($brouwhulp . '/fermentables.xml');

	foreach ($myfermentables->FERMENTABLE as $fermentable) {

		$sql  = "INSERT INTO inventory_fermentables SET name='" . mysqli_real_escape_string($db, $fermentable->NAME);
		$sql .= "', type='" . $fermentable->TYPE;
		if ($fermentable->YIELD)
			$sql .= "', yield='" . $fermentable->YIELD;
		if ($fermentable->COLOR) {
			$srm = floatval($fermentable->COLOR);
			$ebc = srm_to_ebc($srm);
			$sql .= "', color='" . $ebc;
		}
		($fermentable->ADD_AFTER_BOIL == "TRUE") ? $sql .= "', add_after_boil='1" : $sql .= "', add_after_boil='0";
		$sql .= "', origin='" . mysqli_real_escape_string($db, $fermentable->ORIGIN);
		$sql .= "', supplier='" . mysqli_real_escape_string($db, $fermentable->SUPPLIER);
		$sql .= "', notes='" . mysqli_real_escape_string($db, $fermentable->NOTES);
		if ($fermentable->COARSE_FINE_DIFF)
			$sql .= "', coarse_fine_diff='" . $fermentable->COARSE_FINE_DIFF;
		if ($fermentable->MOISTURE)
			$sql .= "', moisture='" . $fermentable->MOISTURE;
		if ($fermentable->DIASTATIC_POWER)
			$sql .= "', diastatic_power='" . $fermentable->DIASTATIC_POWER;
		if ($fermentable->PROTEIN)
			$sql .= "', protein='" . $fermentable->PROTEIN;
		if ($fermentable->MAX_IN_BATCH)
			$sql .= "', max_in_batch='" . $fermentable->MAX_IN_BATCH;
		($fermentable->RECOMMEND_MASH == "TRUE") ? $sql .= "', recommend_mash='1" : $sql .= "', recommend_mash='0";
		if ($fermentable->IBU_GAL_PER_LB)
			$sql .= "', ibu_gal_per_lb='" . $fermentable->IBU_GAL_PER_LB;
		($fermentable->ALWAYS_ON_STOCK == "TRUE") ? $sql .= "', always_on_stock='1" : $sql .= "', always_on_stock='0";
		if ($fermentable->INVENTORY)
			$sql .= "', inventory='" . floatval($fermentable->INVENTORY);
		if ($fermentable->COST)
			$sql .= "', cost='" . floatval($fermentable->COST);
		/*
		 * These are not beerxml standard:
		 */
		if ($fermentable->DI_pH)
			$sql .= "', di_ph='" . $fermentable->DI_pH;
		if ($fermentable->{'ACID_TO_pH_5.7'})
			$sql .= "', acid_to_ph_57='" . $fermentable->{'ACID_TO_pH_5.7'};
		$sql .= "', graintype='" . $fermentable->GRAINTYPE;
		$sql .= "';";
		if (! $result = mysqli_query($db, $sql)) {
			printf("Error: %s\n", mysqli_error($db));
		}
	}
}


function do_hops()
{
	global $brouwhulp, $db;

	echo "  Start adding hops to the database\n";
	$sql = "TRUNCATE TABLE inventory_hops;";
	if (! $result = mysqli_query($db, $sql)) {
		printf("Error: %s\n", mysqli_error($db));
	}

	$myhops = simplexml_load_file($brouwhulp . '/hops.xml');

	foreach ($myhops->HOP as $hop) {

		$sql  = "INSERT INTO inventory_hops SET name='" . mysqli_real_escape_string($db, $hop->NAME);
		if ($hop->ALPHA)
			$sql .= "', alpha='" . $hop->ALPHA;
		if ($hop->BETA)
			$sql .= "', beta='" . $hop->BETA;
		if ($hop->HUMULENE)
			$sql .= "', humulene='" . $hop->HUMULENE;
		if ($hop->CARYOPHYLLENE)
			$sql .= "', caryophyllene='" . $hop->CARYOPHYLLENE;
		if ($hop->COHUMULONE)
			$sql .= "', cohumulone='" . $hop->COHUMULONE;
		if ($hop->MYCRENE)
			$sql .= "', myrcene='" . $hop->MYCRENE;
		if ($hop->HSI)
			$sql .= "', hsi='" . $hop->HSI;
		$sql .= "', useat='" . $hop->USE;
		$sql .= "', type='" . $hop->TYPE;
		$sql .= "', form='" . $hop->FORM;
		$sql .= "', notes='" . mysqli_real_escape_string($db, $hop->NOTES);
		$sql .= "', origin='" . mysqli_real_escape_string($db, $hop->ORIGIN);
		$sql .= "', substitutes='" . mysqli_real_escape_string($db, $hop->SUBSTITUTES);
		($hop->ALWAYS_ON_STOCK == 'TRUE') ? $sql .= "', always_on_stock='1" : $sql .= "', always_on_stock='0";
		if ($hop->INVENTORY)
			$sql .= "', inventory='" . floatval($hop->INVENTORY) / 1000.0;
		if ($hop->COST)
			$sql .= "', cost='" . floatval($hop->COST);
		if ($hop->TOTAL_OIL)
			$sql .= "', total_oil='" . $hop->TOTAL_OIL;
		$sql .= "';";
		if (! $result = mysqli_query($db, $sql)) {
			printf("Error: %s\n", mysqli_error($db));
		}
	}

}



function do_yeasts()
{
	global $brouwhulp, $db;

	echo "  Start adding yeasts to the database\n";
	$sql = "TRUNCATE TABLE inventory_yeasts;";
	if (! $result = mysqli_query($db, $sql)) {
		printf("Error: %s\n", mysqli_error($db));
	}

	$yeasts = simplexml_load_file($brouwhulp . '/yeasts.xml');

	foreach ($yeasts->YEAST as $yeast) {

		$sql  = "INSERT INTO inventory_yeasts SET name='" . mysqli_real_escape_string($db, $yeast->NAME);
		$sql .= "', type='" . $yeast->TYPE;
		$sql .= "', form='" . $yeast->FORM;
		$sql .= "', laboratory='" . mysqli_real_escape_string($db, $yeast->LABORATORY);
		$sql .= "', product_id='" . mysqli_real_escape_string($db, $yeast->PRODUCT_ID);
		if ($yeast->MIN_TEMPERATURE)
			$sql .= "', min_temperature='" . $yeast->MIN_TEMPERATURE;
		if ($yeast->MAX_TEMPERATURE)
			$sql .= "', max_temperature='" . $yeast->MAX_TEMPERATURE;
		$sql .= "', flocculation='" . $yeast->FLOCCULATION;
		if ($yeast->ATTENUATION)
			$sql .= "', attenuation='" . $yeast->ATTENUATION;
		$sql .= "', notes='" . mysqli_real_escape_string($db, $yeast->NOTES);
		$sql .= "', best_for='" . mysqli_real_escape_string($db, $yeast->BEST_FOR);
		if ($yeast->MAX_REUSE)
			$sql .= "', max_reuse='" . $yeast->MAX_REUSE;
		if ($yeast->INVENTORY)
			$sql .= "', inventory='" . floatval($yeast->INVENTORY) / 1000.0;
		if ($yeast->COST)
			$sql .= "', cost='" . floatval($yeast->COST) * 1000.0;
		if ($yeast->CULTURE_DATE) {
			$date = substr($yeast->CULTURE_DATE, 6, 4) . '-' . substr($yeast->CULTURE_DATE, 3, 2) . '-' . substr($yeast->CULTURE_DATE, 0, 2);
			$sql .= "', production_date='" . $date;
		}
		$sql .= "';";
		if (! $result = mysqli_query($db, $sql)) {
			printf("Error: %s\n", mysqli_error($db));
		}
	}
}



function do_waters()
{
	global $brouwhulp, $db;

	echo "  Start adding waters to the database\n";
	$sql = "TRUNCATE TABLE profile_water;";
	if (! $result = mysqli_query($db, $sql)) {
		printf("Error: %s\n", mysqli_error($db));
	}
	$sql = "TRUNCATE TABLE inventory_water_sources;";
	if (! $result = mysqli_query($db, $sql)) {
		printf("Error: %s\n", mysqli_error($db));
	}

	$waters = simplexml_load_file($brouwhulp . '/waters.xml');

	foreach ($waters->WATER as $water) {

		if (($water->NAME == "Gedemineraliseerd water") || ($water->NAME == "Cristalline") || ($water->NAME == "Montille (Delhaize)") ||
		    ($water->NAME == "Spa Reine") || ($water->NAME == "Velsen") || ($water->NAME == "Zutphen") || 
		    ($water->NAME == "Aurele Bronwater (Colruyt)")) {
		    $sql  = "INSERT INTO inventory_water_sources SET name='" . mysqli_real_escape_string($db, $water->NAME);
		    ($water->DEFAULT_WATER == 'TRUE') ? $sql .= "', default_water='1" : $sql .= "', default_water='0";
		    ($water->ALWAYS_ON_STOCK == 'TRUE') ? $sql .= "', always_on_stock='1" : $sql .= "', always_on_stock='0";
		} else {
			$sql  = "INSERT INTO profile_water SET name='" . mysqli_real_escape_string($db, $water->NAME);
		}

		if ($water->NOTES)
			$sql .= "', notes='" . mysqli_real_escape_string($db, $water->NOTES);
		$sql .= "', calcium='" . $water->CALCIUM;
		$sql .= "', bicarbonate='" . $water->BICARBONATE;
		$sql .= "', sulfate='" . $water->SULFATE;
		$sql .= "', chloride='" . $water->CHLORIDE;
		$sql .= "', sodium='" . $water->SODIUM;
		$sql .= "', magnesium='" . $water->MAGNESIUM;
		$sql .= "', ph='" . $water->PH;
		if ($water->TOTAL_ALKALINITY)
			$sql .= "', total_alkalinity='" . $water->TOTAL_ALKALINITY;
		$sql .= "';";
		if (! $result = mysqli_query($db, $sql)) {
			printf("Error: %s\n", mysqli_error($db));
		}
	}
}



function do_miscs()
{
	global $brouwhulp, $db;

	echo "  Start adding miscs to the database\n";
	$sql = "TRUNCATE TABLE inventory_miscs;";
	if (! $result = mysqli_query($db, $sql)) {
		printf("Error: %s\n", mysqli_error($db));
	}

	$miscs = simplexml_load_file($brouwhulp . '/miscs.xml');

	foreach ($miscs->MISC as $misc) {

		$sql  = "INSERT INTO inventory_miscs SET name='" . mysqli_real_escape_string($db, $misc->NAME);
		if ($misc->NOTES)
			$sql .= "', notes='" . mysqli_real_escape_string($db, $misc->NOTES);
		$sql .= "', type='" . $misc->TYPE;
		$sql .= "', use_use='" . $misc->USE;
		$sql .= "', time='" . $misc->TIME;
		($misc->AMOUNT_IS_WEIGHT == 'TRUE') ? $sql .= "', amount_is_weight='1" : $sql .= "', amount_is_weight='0";
		if ($misc->USE_FOR)
			$sql .= "', use_for='" . mysqli_real_escape_string($db, $misc->USE_FOR);
		if ($misc->ALWAYS_ON_STOCK)
			($misc->ALWAYS_ON_STOCK == 'TRUE') ? $sql .= "', always_on_stock='1" : $sql .= "', always_on_stock='0";
		if ($misc->INVENTORY)
			$sql .= "', inventory='" . floatval($misc->INVENTORY) / 1000.0;
		if ($misc->COST)
			$sql .= "', cost='" . floatval($misc->COST);
		$sql .= "';";
		if (! $result = mysqli_query($db, $sql)) {
			printf("Error: %s\n", mysqli_error($db));
		}
	}
}



function do_equipments()
{
	global $brouwhulp, $db;

	echo "  Start adding equipments to the database\n";
	$sql = "TRUNCATE TABLE inventory_equipments;";
	if (! $result = mysqli_query($db, $sql)) {
		printf("Error: %s\n", mysqli_error($db));
	}

	$equipments = simplexml_load_file($brouwhulp . '/equipments.xml');

	foreach ($equipments->EQUIPMENT as $equipment) {

		$sql  = "INSERT INTO inventory_equipments SET name='" . mysqli_real_escape_string($db, $equipment->NAME);
		$sql .= "', boil_size='" . $equipment->BOIL_SIZE;
		$sql .= "', batch_size='" . $equipment->BATCH_SIZE;
		$sql .= "', tun_volume='" . $equipment->TUN_VOLUME;
		$sql .= "', tun_weight='" . $equipment->TUN_WEIGHT;
		$sql .= "', tun_specific_heat='" . $equipment->TUN_SPECIFIC_HEAT;
		$sql .= "', top_up_water='" . $equipment->TOP_UP_WATER;
		$sql .= "', trub_chiller_loss='" . $equipment->TRUB_CHILLER_LOSS;
		/*
		 * Brouwhulp uses a percentage for the evaporation rate. This is wrong
		 * but was made so because the beerxml standard requires this. What we
		 * do is calculate the actual evaporation and store that.
		 * This is what we use. Brouwhulp calculates this on the fly.
		 */
		$sql .= "', evap_rate='" . ($equipment->EVAP_RATE * $equipment->BOIL_SIZE) / 100.0;
		$sql .= "', boil_time='" . $equipment->BOIL_TIME;
		($equipment->CALC_BOIL_VOLUME == 'TRUE') ? $sql .= "', calc_boil_volume='1" : $sql .= "', calc_boil_volume='0";
		$sql .= "', lauter_deadspace='" . $equipment->LAUTER_DEADSPACE;
		$sql .= "', top_up_kettle='" . $equipment->TOP_UP_KETTLE;
		$sql .= "', hop_utilization='" . $equipment->HOP_UTILIZATION;
		if ($equipment->NOTES)
			$sql .= "', notes='" . mysqli_real_escape_string($db, $equipment->NOTES);
		$sql .= "', lauter_volume='" . $equipment->LAUTER_VOLUME;
		$sql .= "', kettle_volume='" . $equipment->KETTLE_VOLUME;
		if ($equipment->TUN_MATERIAL)
			$sql .= "', tun_material='" . mysqli_real_escape_string($db, $equipment->TUN_MATERIAL);
		$sql .= "', tun_height='" . $equipment->TUN_HEIGHT;
		$sql .= "', kettle_height='" . $equipment->KETTLE_HEIGHT;
		$sql .= "', lauter_height='" . $equipment->LAUTER_HEIGHT;
		$sql .= "', mash_volume='" . $equipment->MASH_VOLUME;
		$sql .= "', efficiency='" . $equipment->EFFICIENCY;

		$sql .= "';";
		if (! $result = mysqli_query($db, $sql)) {
			printf("Error: %s\n", mysqli_error($db));
		}
	}
}



function do_styles()
{
	global $brouwhulp, $db;

	echo "  Start adding styles to the database\n";
	$sql = "TRUNCATE TABLE profile_styles;";
	if (! $result = mysqli_query($db, $sql)) {
		printf("Error: %s\n", mysqli_error($db));
	}

	$styles = simplexml_load_file($brouwhulp . '/styles.xml');

	foreach ($styles->STYLE as $style) {

		$sql  = "INSERT INTO profile_styles SET name='" . mysqli_real_escape_string($db, $style->NAME);
		if ($style->NOTES)
			$sql .= "', notes='" . mysqli_real_escape_string($db, $style->NOTES);
		if ($style->CATEGORY)
			$sql .= "', category='" . mysqli_real_escape_string($db, $style->CATEGORY);
		if ($style->CATEGORY_NUMBER)
			$sql .= "', category_number='" . $style->CATEGORY_NUMBER;
		if ($style->STYLE_LETTER)
			$sql .= "', style_letter='" . mysqli_real_escape_string($db, $style->STYLE_LETTER);
		if ($style->STYLE_GUIDE)
			$sql .= "', style_guide='" . mysqli_real_escape_string($db, $style->STYLE_GUIDE);
		$sql .= "', type='" . $style->TYPE;
		$sql .= "', og_min='" . floatval($style->OG_MIN);
		$sql .= "', og_max='" . floatval($style->OG_MAX);
		$sql .= "', fg_min='" . floatval($style->FG_MIN);
		$sql .= "', fg_max='" . floatval($style->FG_MAX);
		$sql .= "', ibu_min='" . floatval($style->IBU_MIN);
		$sql .= "', ibu_max='" . floatval($style->IBU_MAX);
		$sql .= "', color_min='" . floatval($style->COLOR_MIN);
		$sql .= "', color_max='" . floatval($style->COLOR_MAX);
		$sql .= "', carb_min='" . floatval($style->CARB_MIN);
		$sql .= "', carb_max='" . floatval($style->CARB_MAX);
		$sql .= "', abv_min='" . floatval($style->ABV_MIN);
		$sql .= "', abv_max='" . floatval($style->ABV_MAX);
		if ($style->PROFILE)
			$sql .= "', profile='" . mysqli_real_escape_string($db, $style->PROFILE);
		if ($style->INGREDIENTS)
			$sql .= "', ingredients='" . mysqli_real_escape_string($db, $style->INGREDIENTS);
		if ($style->EXAMPLES)
			$sql .= "', examples='" . mysqli_real_escape_string($db, $style->EXAMPLES);

		$sql .= "';";
		if (! $result = mysqli_query($db, $sql)) {
			printf("Error: %s\n", mysqli_error($db));
		}
	}
}



function do_mash()
{
	global $brouwhulp, $db;

	echo "  Start adding mash profiles to the database\n";
	$sql = "TRUNCATE TABLE profile_mash;";
	if (! $result = mysqli_query($db, $sql)) {
		printf("Error: %s\n", mysqli_error($db));
	}

	$mashes = simplexml_load_file($brouwhulp . '/mashs.xml');

	foreach ($mashes->MASH as $mash) {
		$sql  = "INSERT INTO profile_mash SET name='" . mysqli_real_escape_string($db, $mash->NAME);
		if ($mash->NOTES)
			$sql .= "', notes='" . mysqli_real_escape_string($db, $mash->NOTES);

		/*
		 * Put the steps in a json array
		 */
		if ($mash->MASH_STEPS) {
			$steps = "[";
			$comma = FALSE;
			foreach ($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 .= ']';
			$sql .= "', steps='" . $steps;
		}
		$sql .= "';";
		if (! $result = mysqli_query($db, $sql)) {
			printf("Error: %s\n", mysqli_error($db));
		}
	}
}




function do_recipes()
{
	global $brouwhulp, $db;
	$len_fermentables = 0;
	$len_hops = 0;
	$len_miscs = 0;
	$len_yeasts = 0;
	$len_waters = 0;
	$len_mash = 0;

	echo "  Start adding recipes to the database\n";
	$sql = "TRUNCATE TABLE recipes;";
	if (! $result = mysqli_query($db, $sql)) {
		printf("Error: %s\n", mysqli_error($db));
	}

	$recipes= simplexml_load_file($brouwhulp . '/recipes.xml');

	foreach ($recipes->RECIPE as $recipe) {
		$sql  = "INSERT INTO recipes SET name='" . mysqli_real_escape_string($db, $recipe->NAME);
		if ($recipe->NOTES)
			$sql .= "', notes='" . mysqli_real_escape_string($db, $recipe->NOTES);
		if ($recipe->TYPE)
			$sql .= "', type='" . mysqli_real_escape_string($db, $recipe->TYPE);
		if ($recipe->BATCH_SIZE)
			$sql .= "', batch_size='" . floatval($recipe->BATCH_SIZE);
		if ($recipe->BOIL_TIME)
			$sql .= "', boil_time='" . floatval($recipe->BOIL_TIME);
		if ($recipe->EFFICIENCY)
			$sql .= "', efficiency='" . floatval($recipe->EFFICIENCY);
		if ($recipe->EST_OG)
			$sql .= "', est_og='" . floatval($recipe->EST_OG);
		if ($recipe->EST_FG)
			$sql .= "', est_fg='" . floatval($recipe->EST_FG);
		if ($recipe->EST_COLOR) {
			$srm = floatval($recipe->EST_COLOR);
			$ebc = srm_to_ebc($srm);
			$sql .= "', est_color='" . $ebc;
		}
		if ($recipe->COLOR_METHOD)
			$sql .= "', color_method='" . mysqli_real_escape_string($db, $recipe->COLOR_METHOD);
		if ($recipe->IBU)
			$sql .= "', est_ibu='" . floatval($recipe->IBU);
		if ($recipe->IBU_METHOD)
			$sql .= "', ibu_method='" . mysqli_real_escape_string($db, $recipe->IBU_METHOD);

		if ($recipe->STYLE) {
			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 2015";
				} else if ($recipe->STYLE->STYLE_GUIDE == "BKG Biertypen") {
					$sql .= "', st_guide='BKG 2015";
				} else {
					$sql .= "', st_guide='" . mysqli_real_escape_string($db, $recipe->STYLE->STYLE_GUIDE);
				}
			}
			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);
		}

		/*
		 * Put the fermentables in a json array
		 */
		if ($recipe->FERMENTABLES) {
			$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) . '"';
				$fermentables .= ',"f_amount":' . floatval($fermentable->AMOUNT);
				$fermentables .= ',"f_cost":' . floatval($fermentable->COST);
				$fermentables .= ',"f_type":"' . mysqli_real_escape_string($db, $fermentable->TYPE) . '"';
				$fermentables .= ',"f_yield":' . floatval($fermentable->YIELD);
				if ($fermentable->COLOR) {
					$srm = floatval($fermentable->COLOR);
					$ebc = srm_to_ebc($srm);
					$fermentables .= ',"f_color":' . $ebc;
				}
				if ($fermentable->COARSE_FINE_DIFF) {
					$fermentables .= ',"f_coarse_fine_diff":' . floatval($fermentable->COARSE_FINE_DIFF);
				}
				if ($fermentable->MOISTURE) {
					$fermentables .= ',"f_moisture":' . floatval($fermentable->MOISTURE);
				}
				if ($fermentable->DIASTATIC_POWER) {
				        $fermentables .= ',"f_diastatic_power":' . floatval($fermentable->DIASTATIC_POWER);
				}
				if ($fermentable->PROTEIN) {
					$fermentables .= ',"f_protein":' . floatval($fermentable->PROTEIN);
				}
				if ($fermentable->MAX_IN_BATCH) {
					$fermentables .= ',"f_max_in_batch":' . floatval($fermentable->MAX_IN_BATCH);
				}
				if ($fermentable->GRAINTYPE) {
					$fermentables .= ',"f_graintype":"' . mysqli_real_escape_string($db, $fermentable->GRAINTYPE) . '"';
				}
				if ($fermentable->ADDED) {
					$fermentables .= ',"f_added":"' . mysqli_real_escape_string($db, $fermentable->ADDED) . '"';
				}
				($fermentable->ADD_AFTER_BOIL== "TRUE") ? $fermentables .= ',"f_add_after_boil":true' : $fermentables .= ',"f_add_after_boil":false';
				($fermentable->RECOMMEND_MASH== "TRUE") ? $fermentables .= ',"f_recommend_mash":true' : $fermentables .= ',"f_recommend_mash":false';
				if ($fermentable->DISSOLVED_PROTEIN) {
					$fermentables .= ',"f_dissolved_protein":' . floatval($fermentable->DISSOLVED_PROTEIN);
				}
				($fermentable->ADJUST_TO_TOTAL_100 == "TRUE") ? $fermentables .= ',"f_adjust_to_total_100":true' : $fermentables .= ',"f_adjust_to_total_100":false';
				if ($fermentable->DI_pH) {
					$fermentables .= ',"f_di_ph":' . floatval($fermentable->DI_pH);
				}
				$fermentables .= "}";
			}

			$fermentables .= ']';
//			echo $fermentables . PHP_EOL;
			$sql .= "', json_fermentables='" . $fermentables;
			if (strlen($fermentables) > $len_fermentables)
				$len_fermentables = strlen($fermentables);
		}

		/*
		 * Put the hops in a json array
		 */
		if ($recipe->HOPS) {
			$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);
				$hops .= ',"h_type":"' . mysqli_real_escape_string($db, $hop->TYPE) . '"';
				$hops .= ',"h_form":"' . mysqli_real_escape_string($db, $hop->FORM) . '"';
				$hops .= ',"h_use":"' . mysqli_real_escape_string($db, $hop->USE) . '"';
				if ($hop->TIME)
					$hops .= ',"h_time":' . floatval($hop->TIME);
				$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_carophyllene":' . 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);
				$hops .= "}";
			}

			$hops .= ']';
//			echo $hops . PHP_EOL;
			$sql .= "', json_hops='" . $hops;
			if (strlen($hops) > $len_hops)
				$len_hops = strlen($hops);
		}

		/*
		 * Put the miscs in a json array
		 */
		if ($recipe->MISCS) {
			$miscs = "[";
			$comma = FALSE;
			foreach ($recipe->MISCS->MISC as $misc) {
				if ($comma)
					$miscs .= ',';
				$comma = TRUE;
				$miscs .= '{"m_name":"' . mysqli_real_escape_string($db, $misc->NAME) . '"';
				$miscs .= ',"m_amount":' . floatval($misc->AMOUNT);
				if ($misc->COST)
					$miscs .= ',"m_cost":' . floatval($misc->COST);
				$miscs .= ',"m_type":"' . mysqli_real_escape_string($db, $misc->TYPE) . '"';
				$miscs .= ',"m_use":"' . mysqli_real_escape_string($db, $misc->USE) . '"';
				($misc->AMOUNT_IS_WEIGHT== "TRUE") ? $miscs .= ',"m_amount_is_weight":true' : $miscs.= ',"m_amount_is_weight":false';
				if ($misc->TIME)
					$miscs .= ',"m_time":' . floatval($misc->TIME);
				$miscs .= "}";
			}

			$miscs .= ']';
//			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 .= '{"y_name":"' . mysqli_real_escape_string($db, $yeast->NAME) . '"';
				$yeasts .= ',"y_amount":' . floatval($yeast->AMOUNT);
				if ($yeast->COST)
					$yeasts .= ',"y_cost":' . floatval($yeast->COST);
				$yeasts .= ',"y_laboratory":"' . mysqli_real_escape_string($db, $yeast->LABORATORY) . '"';
				$yeasts .= ',"y_product_id":"' . mysqli_real_escape_string($db, $yeast->PRODUCT_ID) . '"';
				$yeasts .= ',"y_type":"' . mysqli_real_escape_string($db, $yeast->TYPE) . '"';
				$yeasts .= ',"y_form":"' . mysqli_real_escape_string($db, $yeast->FORM) . '"';
				($yeast->AMOUNT_IS_WEIGHT== "TRUE") ? $yeasts .= ',"y_amount_is_weight":true' : $yeasts.= ',"y_amount_is_weight":false';
				if ($yeast->ADD_TO_SECONDARY=="FALSE") {
					$yeasts .= ',"y_use":"Primary"';
				} else if ($yeast->PRODUCT_ID=="F2") {
					$yeasts .= ',"y_use":"Bottle"';
				} else {
					$yeasts .= ',"y_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 .= '{"w_name":"' . mysqli_real_escape_string($db, $water->NAME) . '"';
				$waters .= ',"w_amount":' . floatval($water->AMOUNT);
				if ($water->CALCIUM)
					$waters .= ',"w_calcium":' . floatval($water->CALCIUM);
				if ($water->SULFATE)
					$waters .= ',"w_sulfate":' . floatval($water->SULFATE);
				if ($water->CHLORIDE)
					$waters .= ',"w_chloride":' . floatval($water->CHLORIDE);
				if ($water->SODIUM)
					$waters .= ',"w_sodium":' . floatval($water->SODIUM);
				if ($water->MAGNESIUM)
					$waters .= ',"w_magnesium":' . floatval($water->MAGNESIUM);
				if ($water->PH)
					$waters .= ',"w_ph":' . floatval($water->PH);
				if ($water->TOTAL_ALKALINITY)
					$waters .= ',"w_total_alkalinity":' . floatval($water->TOTAL_ALKALINITY);
				if ($water->COST)
					$waters .= ',"w_cost":' . floatval($water->COST);
				else
					$waters .= ',"w_cost":0.00';
				($water->DEFAULT_WATER== "TRUE") ? $waters .= ',"w_default_water":true' : $waters.= ',"w_default_water":false';
				$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) {
			$sql .= "',mash_sparge_temp='" . floatval($recipe->MASH->SPARGE_TEMP);
			$sql .= "',mash_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) {
				$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->INFUSE_AMOUNT)
						$steps .= ',"step_infuse_amount":"' . floatval($step->INFUSE_AMOUNT) . '"';
					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 .= ']';
//				echo $steps . PHP_EOL;
				$sql .= "', json_mashs='" . $steps;
				if (strlen($steps) > $len_mash)
					$len_mash = strlen($steps);
			}
		}

		$sql .= "';";
		if (! $result = mysqli_query($db, $sql)) {
			printf("Error: %s\n", mysqli_error($db));
		}
	}
	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;
}



//do_fermentables();
//do_hops();
//do_yeasts();
//do_miscs();
//do_waters();
//do_equipments();
do_styles();
//do_mash();
do_recipes();


mysqli_close($db);

echo "Finished adding data\n";


?>

mercurial