www/includes/db_recipes.php

Wed, 06 May 2020 14:14:14 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Wed, 06 May 2020 14:14:14 +0200
changeset 667
1246550451ca
parent 565
dfc850e6ee1f
child 710
9646123ea063
permissions
-rw-r--r--

Removed the last compressed css file. Reworked all mash steps, implemented deconction steps. Added calculations for infuse amounts and decoctions amounts. The mash steps are now manually sorted in the editor grids to have full control over the steps order. Display errors in red in the grid. Updated beerxml export, the product checklist and print output of the products and recipes for all these mash steps changes.

<?php

require($_SERVER['DOCUMENT_ROOT']."/config.php");
require($_SERVER['DOCUMENT_ROOT']."/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());
}
mysqli_set_charset($connect, "utf8" );

$escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c");
$replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b");
$rescapers = array("'");
$rreplacements = array("\\'");
$disallowed = array('visibleindex','uniqueid','boundindex','uid','h_weight','m_weight');

if (isset($_POST['insert']) || isset($_POST['update'])) {
	if (isset($_POST['insert'])) {
		// INSERT COMMAND
		$sql  = "INSERT INTO `recipes` SET ";
	}
	if (isset($_POST['update'])) {
		// UPDATE COMMAND
		$sql  = "UPDATE `recipes` SET ";
	}
	// Basic settings
	if (isset($_POST['uuid'])) {
		$sql .= "uuid='" . $_POST['uuid'];
	} else {
		$uuid = str_replace("\n", "", file_get_contents('/proc/sys/kernel/random/uuid'));
		$sql .= "uuid='" . $uuid;
	}
	$sql .= "', locked='" . $_POST['locked'];
	$sql .= "', st_name='" . mysqli_real_escape_string($connect, $_POST['st_name']);
	$sql .= "', st_letter='" . mysqli_real_escape_string($connect, $_POST['st_letter']);
	$sql .= "', st_guide='" . mysqli_real_escape_string($connect, $_POST['st_guide']);
	$sql .= "', st_type='" . $_POST['st_type'];
	$sql .= "', st_category='" . mysqli_real_escape_string($connect, $_POST['st_category']);
	$sql .= "', st_category_number='" . $_POST['st_category_number'];
	$sql .= "', st_og_min='" . $_POST['st_og_min'];
	$sql .= "', st_og_max='" . $_POST['st_og_max'];
	$sql .= "', st_fg_min='" . $_POST['st_fg_min'];
	$sql .= "', st_fg_max='" . $_POST['st_fg_max'];
	$sql .= "', st_ibu_min='" . $_POST['st_ibu_min'];
	$sql .= "', st_ibu_max='" . $_POST['st_ibu_max'];
	$sql .= "', st_color_min='" . $_POST['st_color_min'];
	$sql .= "', st_color_max='" . $_POST['st_color_max'];
	$sql .= "', st_carb_min='" . $_POST['st_carb_min'];
	$sql .= "', st_carb_max='" . $_POST['st_carb_max'];
	$sql .= "', st_abv_min='" . $_POST['st_abv_min'];
	$sql .= "', st_abv_max='" . $_POST['st_abv_max'];
	$sql .= "', name='" . mysqli_real_escape_string($connect, $_POST['name']);
	$sql .= "', notes='" . mysqli_real_escape_string($connect, $_POST['notes']);
	$sql .= "', type='" . $_POST['type'];
	$sql .= "', batch_size='" . $_POST['batch_size'];
	$sql .= "', boil_size='" . $_POST['boil_size'];
	$sql .= "', boil_time='" . $_POST['boil_time'];
	$sql .= "', efficiency='" . $_POST['efficiency'];
	$sql .= "', est_og='" . $_POST['est_og'];
	$sql .= "', est_fg='" . $_POST['est_fg'];
	$sql .= "', est_abv='" . $_POST['est_abv'];
	$sql .= "', est_carb='" . $_POST['est_carb'];
	$sql .= "', est_color='" . $_POST['est_color'];
	$sql .= "', color_method='" . $_POST['color_method'];
	$sql .= "', est_ibu='" . $_POST['est_ibu'];
	$sql .= "', ibu_method='" . $_POST['ibu_method'];
	$sql .= "', sparge_temp='" . $_POST['sparge_temp'];
	$sql .= "', sparge_ph='" . $_POST['sparge_ph'];
	$sql .= "', sparge_volume='" . $_POST['sparge_volume'];
	$sql .= "', sparge_source='" . $_POST['sparge_source'];
	$sql .= "', sparge_acid_type='" . $_POST['sparge_acid_type'];
	$sql .= "', sparge_acid_perc='" . $_POST['sparge_acid_perc'];
	$sql .= "', sparge_acid_amount='" . $_POST['sparge_acid_amount'];
	$sql .= "', mash_ph='" . $_POST['mash_ph'];
	$sql .= "', mash_name='" . $_POST['mash_name'];
	$sql .= "', calc_acid='" . $_POST['calc_acid'];
	if (isset($_POST['w1_name'])) {
		$sql .= "', w1_name='" . mysqli_real_escape_string($connect, $_POST['w1_name']);
		$sql .= "', w1_amount='" . $_POST['w1_amount'];
		$sql .= "', w1_calcium='" . $_POST['w1_calcium'];
		$sql .= "', w1_sulfate='" . $_POST['w1_sulfate'];
		$sql .= "', w1_chloride='" . $_POST['w1_chloride'];
		$sql .= "', w1_sodium='" . $_POST['w1_sodium'];
		$sql .= "', w1_magnesium='" . $_POST['w1_magnesium'];
		$sql .= "', w1_total_alkalinity='" . $_POST['w1_total_alkalinity'];
		$sql .= "', w1_ph='" . $_POST['w1_ph'];
		$sql .= "', w1_cost='" . $_POST['w1_cost'];
	}
	if (isset($_POST['w2_name'])) {
		$sql .= "', w2_name='" . mysqli_real_escape_string($connect, $_POST['w2_name']);
		$sql .= "', w2_amount='" . $_POST['w2_amount'];
		$sql .= "', w2_calcium='" . $_POST['w2_calcium'];
		$sql .= "', w2_sulfate='" . $_POST['w2_sulfate'];
		$sql .= "', w2_chloride='" . $_POST['w2_chloride'];
		$sql .= "', w2_sodium='" . $_POST['w2_sodium'];
		$sql .= "', w2_magnesium='" . $_POST['w2_magnesium'];
		$sql .= "', w2_total_alkalinity='" . $_POST['w2_total_alkalinity'];
		$sql .= "', w2_ph='" . $_POST['w2_ph'];
		$sql .= "', w2_cost='" . $_POST['w2_cost'];
	}
	$sql .= "', wg_amount='" . $_POST['wg_amount'];
	$sql .= "', wg_calcium='" . $_POST['wg_calcium'];
	$sql .= "', wg_sulfate='" . $_POST['wg_sulfate'];
	$sql .= "', wg_chloride='" . $_POST['wg_chloride'];
	$sql .= "', wg_sodium='" . $_POST['wg_sodium'];
	$sql .= "', wg_magnesium='" . $_POST['wg_magnesium'];
	$sql .= "', wg_total_alkalinity='" . $_POST['wg_total_alkalinity'];
	$sql .= "', wg_ph='" . $_POST['wg_ph'];
	$sql .= "', wb_calcium='" . $_POST['wb_calcium'];
	$sql .= "', wb_sulfate='" . $_POST['wb_sulfate'];
	$sql .= "', wb_chloride='" . $_POST['wb_chloride'];
	$sql .= "', wb_sodium='" . $_POST['wb_sodium'];
	$sql .= "', wb_magnesium='" . $_POST['wb_magnesium'];
	$sql .= "', wb_total_alkalinity='" . $_POST['wb_total_alkalinity'];
	$sql .= "', wb_ph='" . $_POST['wb_ph'];
	$sql .= "', wa_acid_name='" . $_POST['wa_acid_name'];
	$sql .= "', wa_acid_perc='" . $_POST['wa_acid_perc'];
	$sql .= "', wa_base_name='" . $_POST['wa_base_name'];
	//syslog(LOG_NOTICE, $sql);

	$fermentables = '[';
	$comma = FALSE;
	if (isset($_POST['fermentables'])) {
		$array = $_POST['fermentables'];
		// Sort the array
                $added = array();
                $amount = array();
                for ($i = 0; $i < count($array); $i++) {
                        $added[] = $array[$i]['f_added'];
                        $amount[] = $array[$i]['f_amount'];
                }
                array_multisort($added, SORT_ASC, SORT_NUMERIC,
                        $amount, SORT_DESC, SORT_NUMERIC,
                        $array);
                // Write the sorted array.
		foreach($array as $key => $item){
			/*
			 * Manual encode to json.
			 */
			if ($comma)
				$fermentables .= ',';
			$comma = TRUE;
			$fermentable  = '{"f_name":"' . str_replace($rescapers,$rreplacements,$item['f_name']);
			$fermentable .= '","f_origin":"' . str_replace($rescapers,$rreplacements,$item['f_origin']);
			$fermentable .= '","f_supplier":"' . str_replace($rescapers,$rreplacements,$item['f_supplier']);
			$fermentable .= '","f_amount":' . $item['f_amount'];
			$fermentable .= ',"f_cost":' . $item['f_cost'];
			$fermentable .= ',"f_type":' . $item['f_type'];
			$fermentable .= ',"f_yield":' . $item['f_yield'];
			$fermentable .= ',"f_color":' . $item['f_color'];
			$fermentable .= ',"f_coarse_fine_diff":' . $item['f_coarse_fine_diff'];
			$fermentable .= ',"f_moisture":' . $item['f_moisture'];
			$fermentable .= ',"f_diastatic_power":' . $item['f_diastatic_power'];
			$fermentable .= ',"f_protein":' . $item['f_protein'];
			$fermentable .= ',"f_dissolved_protein":' . $item['f_dissolved_protein'];
			$fermentable .= ',"f_max_in_batch":' . $item['f_max_in_batch'];
			$fermentable .= ',"f_graintype":' . $item['f_graintype'];
			$fermentable .= ',"f_added":' . $item['f_added'];
			$fermentable .= ',"f_recommend_mash":' . $item['f_recommend_mash'];
			$fermentable .= ',"f_add_after_boil":' . $item['f_add_after_boil'];
			$fermentable .= ',"f_adjust_to_total_100":' . $item['f_adjust_to_total_100'];
			$fermentable .= ',"f_percentage":' . $item['f_percentage'];
			$fermentable .= ',"f_di_ph":' . $item['f_di_ph'];
			$fermentable .= ',"f_acid_to_ph_57":' . $item['f_acid_to_ph_57'] . '}';
			//syslog(LOG_NOTICE, $fermentable);
			$fermentables .= $fermentable;
		}
	}
	$fermentables .= ']';
	$sql .= "', json_fermentables='" . $fermentables;

	$hops = '[';
	$comma = FALSE;
	if (isset($_POST['hops'])) {
		$array = $_POST['hops'];
		// Sort the array
                $useat = array();
                $time = array();
                $amount = array();
                for ($i = 0; $i < count($array); $i++) {
                        $useat[] = $array[$i]['h_useat'];
                        $time[] = $array[$i]['h_time'];
                        $amount[] = $array[$i]['h_amount'];
                }
                array_multisort($useat, SORT_ASC, SORT_NUMERIC,
                                $time, SORT_DESC, SORT_NUMERIC,
                                $amount, SORT_DESC, SORT_NUMERIC,
                                $array);
                // Write the sorted array.
		foreach($array as $key => $item){
			if ($comma)
				$hops .= ',';
			$comma = TRUE;
			$hop  = '{"h_name":"' . str_replace($rescapers,$rreplacements,$item['h_name']);
			$hop .= '","h_origin":"' . str_replace($rescapers,$rreplacements,$item['h_origin']);
			$hop .= '","h_amount":' . $item['h_amount'];
			$hop .= ',"h_cost":' . $item['h_cost'];
			$hop .= ',"h_type":' . $item['h_type'];
			$hop .= ',"h_form":' . $item['h_form'];
			$hop .= ',"h_useat":' . $item['h_useat'];
			$hop .= ',"h_time":' . $item['h_time'];
			$hop .= ',"h_alpha":' . $item['h_alpha'];
			$hop .= ',"h_beta":' . $item['h_beta'];
			$hop .= ',"h_hsi":' . $item['h_hsi'];
			$hop .= ',"h_humulene":' . $item['h_humulene'];
			$hop .= ',"h_caryophyllene":' . $item['h_caryophyllene'];
			$hop .= ',"h_cohumulone":' . $item['h_cohumulone'];
			$hop .= ',"h_myrcene":' . $item['h_myrcene'];
			$hop .= ',"h_total_oil":' . $item['h_total_oil'] . '}';
			//syslog(LOG_NOTICE, $hop);
			$hops .= $hop;
		}
	}
	$hops .= ']';
	$sql .= "', json_hops='" . $hops;

	$miscs = '[';
	$comma = FALSE;
	if (isset($_POST['miscs'])) {
		$array = $_POST['miscs'];
		// Sort the array
                $use = array();
                $type = array();
                $amount = array();
                for ($i = 0; $i < count($array); $i++) {
                        $use[] = $array[$i]['m_use_use'];
                        $type[] = $array[$i]['m_type'];
                        $amount[] = $array[$i]['m_amount'];
                }
                array_multisort($use, SORT_ASC, SORT_NUMERIC,
                        $type, SORT_ASC, SORT_NUMERIC,
                        $amount, SORT_DESC, SORT_NUMERIC,
                        $array);
                // Write the sorted array.
		foreach($array as $key => $item) {
			if ($comma)
				$miscs .= ',';
			$comma = TRUE;
			$misc  = '{"m_name":"' . str_replace($rescapers,$rreplacements,$item['m_name']);
			$misc .= '","m_amount":' . $item['m_amount'];
			$misc .= ',"m_type":' . $item['m_type'];
			$misc .= ',"m_use_use":' . $item['m_use_use'];
			$misc .= ',"m_time":' . $item['m_time'];
			$misc .= ',"m_amount_is_weight":' . $item['m_amount_is_weight'];
			$misc .= ',"m_cost":' . $item['m_cost'] . '}';
			//syslog(LOG_NOTICE, $misc);
			$miscs .= $misc;
		}
	}
	$miscs .= ']';
	$sql .= "', json_miscs='" . $miscs;

	$yeasts = '[';
	$comma = FALSE;
	if (isset($_POST['yeasts'])) {
		$array = $_POST['yeasts'];
		// Sort the array
                $use = array();
                $amount = array();
                for ($i = 0; $i < count($array); $i++) {
                        $use[] = $array[$i]['y_use'];
                        $amount[] = $array[$i]['y_amount'];
                }
                array_multisort($use, SORT_ASC, SORT_NUMERIC,
                        $amount, SORT_DESC, SORT_NUMERIC,
                        $array);
                // Write the sorted array.
		foreach($array as $key => $item) {
			if ($comma)
				$yeasts .= ',';
			$comma = TRUE;
			$yeast  = '{"y_name":"' . str_replace($rescapers,$rreplacements,$item['y_name']);
			$yeast .= '","y_laboratory":"' . str_replace($rescapers,$rreplacements,$item['y_laboratory']);
			$yeast .= '","y_product_id":"' . str_replace($rescapers,$rreplacements,$item['y_product_id']);
			$yeast .= '","y_amount":' . $item['y_amount'];
			$yeast .= ',"y_type":' . $item['y_type'];
			$yeast .= ',"y_form":' . $item['y_form'];
			$yeast .= ',"y_min_temperature":' . $item['y_min_temperature'];
			$yeast .= ',"y_max_temperature":' . $item['y_max_temperature'];
			$yeast .= ',"y_flocculation":' . $item['y_flocculation'];
			$yeast .= ',"y_attenuation":' . $item['y_attenuation'];
			$yeast .= ',"y_cells":' . $item['y_cells'];
			$yeast .= ',"y_tolerance":' . $item['y_tolerance'];
			$yeast .= ',"y_inventory":' . $item['y_inventory'];
			$yeast .= ',"y_use":' . $item['y_use'];
			$yeast .= ',"y_sta1":' . $item['y_sta1'];
                        $yeast .= ',"y_bacteria":' . $item['y_bacteria'];
                        $yeast .= ',"y_harvest_top":' . $item['y_harvest_top'];
                        $yeast .= ',"y_harvest_time":' . $item['y_harvest_time'];
                        $yeast .= ',"y_pitch_temperature":' . $item['y_pitch_temperature'];
                        $yeast .= ',"y_pofpos":' . $item['y_pofpos'];
                        $yeast .= ',"y_zymocide":' . $item['y_zymocide'];
			$yeast .= ',"y_cost":' . $item['y_cost'] . '}';
			syslog(LOG_NOTICE, $yeast);
			$yeasts .= $yeast;
		}
	}
	$yeasts .= ']';
	$sql .= "', json_yeasts='" . $yeasts;

	$mashs = '[';
	$comma = FALSE;
	if (isset($_POST['mashs'])) {
		$array = $_POST['mashs'];
                // Write the array and fix missing data.
		foreach($array as $key => $item) {
			if ($comma)
				$mashs .= ',';
			$comma = TRUE;
			$mash  = '{"step_name":"' . str_replace($rescapers,$rreplacements,$item['step_name']);
			$mash .= '","step_type":' . $item['step_type'];
			if (isset($item['step_volume']) && $item['step_volume'] != "")
				$mash .= ',"step_volume":' . $item['step_volume'];
			else
				$mash .= ',"step_volume":0';
			if (isset($item['step_infuse_amount']) && $item['step_infuse_amount'] != "")
				$mash .= ',"step_infuse_amount":' . $item['step_infuse_amount'];
			else
				$mash .= ',"step_infuse_amount":0';
			if (isset($item['step_infuse_temp']) && $item['step_infuse_temp'] != "")
				$mash .= ',"step_infuse_temp":' . $item['step_infuse_temp'];
			else
				$mash .= ',"step_infuse_temp":0';
			$mash .= ',"step_temp":' . $item['step_temp'];
			$mash .= ',"step_time":' . $item['step_time'];
			$mash .= ',"ramp_time":' . $item['ramp_time'];
			$mash .= ',"end_temp":' . $item['end_temp'];
			if (isset($item['step_wg_ratio']) && $item['step_wg_ratio'] != "")
				$mash .= ',"step_wg_ratio":' . $item['step_wg_ratio'];
			else
				$mash .= ',"step_wg_ratio":0';
			$mash .= '}';
			$mashs .= $mash;
			//syslog(LOG_NOTICE, $mash);
		}
	}
	$mashs .= ']';
	$sql .= "', json_mashs='" . $mashs;

	if (isset($_POST['insert'])) {
		$sql .= "';";
	}
	if (isset($_POST['update'])) {
		$sql .= "' WHERE record='" . $_POST['record'] . "';";
	}

	$result = mysqli_query($connect, $sql);
	if (! $result) {
		syslog(LOG_NOTICE, "db_recipes: result: ".mysqli_error($connect));
	} else {
		if (isset($_POST['update'])) {
			syslog(LOG_NOTICE, "db_recipes: updated record ".$_POST['record']);
		} else {
			$lastid = mysqli_insert_id($connect);
			syslog(LOG_NOTICE, "db_recipes: inserted record ".$lastid);
		}
	}
	echo $result;

} else if (isset($_POST['delete'])) {
	// DELETE COMMAND
	$sql = "DELETE FROM `recipes` WHERE record='".$_POST['record']."';";
	$result = mysqli_query($connect, $sql);
	if (! $result) {
		syslog(LOG_NOTICE, "db_recipes: result: ".mysqli_error($connect));
	} else {
		syslog(LOG_NOTICE, "db_recipes: deleted record ".$_POST['record']);
	}
	echo $result;

} else {
	/*
	 * Select all or a given record.
	 */
	if (isset($_GET['record'])) {
		$query = "SELECT * FROM recipes WHERE record='" . $_GET['record'] . "';";
	} else {
		$query = "SELECT * FROM recipes ORDER BY st_guide,st_letter,st_name,name";
	}
	$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 .= ',"uuid":"' . str_replace($escapers, $replacements, $row['uuid']);
		$recipes .= '","locked":' . $row['locked'];
		$recipes .=  ',"st_guide":"' . str_replace($escapers, $replacements, $row['st_guide']);
		$recipes .= '","st_letter":"' . str_replace($escapers, $replacements, $row['st_letter']);
		$recipes .= '","st_name":"'  . str_replace($escapers, $replacements, $row['st_name']);
		$recipes .= '","st_type":' . $row['st_type'];
		$recipes .= ',"st_category":"' . str_replace($escapers, $replacements, $row['st_category']);
		$recipes .= '","st_category_number":' . floatval($row['st_category_number']);
		$recipes .= ',"st_og_min":' . floatval($row['st_og_min']);
		$recipes .= ',"st_og_max":' . floatval($row['st_og_max']);
		$recipes .= ',"st_fg_min":' . floatval($row['st_fg_min']);
		$recipes .= ',"st_fg_max":' . floatval($row['st_fg_max']);
		$recipes .= ',"st_ibu_min":' . floatval($row['st_ibu_min']);
		$recipes .= ',"st_ibu_max":' . floatval($row['st_ibu_max']);
		$recipes .= ',"st_color_min":' . floatval($row['st_color_min']);
		$recipes .= ',"st_color_max":' . floatval($row['st_color_max']);
		$recipes .= ',"st_carb_min":' . floatval($row['st_carb_min']);
		$recipes .= ',"st_carb_max":' . floatval($row['st_carb_max']);
		$recipes .= ',"st_abv_min":' . floatval($row['st_abv_min']);
		$recipes .= ',"st_abv_max":' . floatval($row['st_abv_max']);
		$recipes .= ',"name":"' . str_replace($escapers, $replacements, $row['name']);
		$recipes .= '","notes":"' . str_replace($escapers, $replacements, $row['notes']);
		$recipes .= '","type":' . $row['type'];
		$recipes .= ',"batch_size":' . floatval($row['batch_size']);
		$recipes .= ',"boil_size":' . floatval($row['boil_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_abv":' . floatval($row['est_abv']);
		$recipes .= ',"est_carb":' . floatval($row['est_carb']);
		$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 .= ',"sparge_temp":' . floatval($row['sparge_temp']);
		$recipes .= ',"sparge_ph":' . floatval($row['sparge_ph']);
		$recipes .= ',"sparge_volume":' . floatval($row['sparge_volume']);
		$recipes .= ',"sparge_source":"' . $row['sparge_source'];
		$recipes .= '","sparge_acid_type":' . $row['sparge_acid_type'];
		$recipes .= ',"sparge_acid_perc":' . floatval($row['sparge_acid_perc']);
		$recipes .= ',"sparge_acid_amount":' . floatval($row['sparge_acid_amount']);
		$recipes .= ',"mash_ph":' . floatval($row['mash_ph']);
		$recipes .= ',"mash_name":"' . str_replace($escapers, $replacements, $row['mash_name']);
		$recipes .= '","calc_acid":' . $row['calc_acid'];
		$recipes .= ',"w1_name":"' . str_replace($escapers, $replacements, $row['w1_name']);
		$recipes .= '","w1_amount":' . floatval($row['w1_amount']);
		$recipes .= ',"w1_calcium":' . floatval($row['w1_calcium']);
		$recipes .= ',"w1_sulfate":' . floatval($row['w1_sulfate']);
		$recipes .= ',"w1_chloride":' . floatval($row['w1_chloride']);
		$recipes .= ',"w1_sodium":' . floatval($row['w1_sodium']);
		$recipes .= ',"w1_magnesium":' . floatval($row['w1_magnesium']);
		$recipes .= ',"w1_total_alkalinity":' . floatval($row['w1_total_alkalinity']);
		$recipes .= ',"w1_ph":' . floatval($row['w1_ph']);
		$recipes .= ',"w1_cost":' . floatval($row['w1_cost']);
		$recipes .= ',"w2_name":"' . str_replace($escapers, $replacements, $row['w2_name']);
		$recipes .= '","w2_amount":' . floatval($row['w2_amount']);
		$recipes .= ',"w2_calcium":' . floatval($row['w2_calcium']);
		$recipes .= ',"w2_sulfate":' . floatval($row['w2_sulfate']);
		$recipes .= ',"w2_chloride":' . floatval($row['w2_chloride']);
		$recipes .= ',"w2_sodium":' . floatval($row['w2_sodium']);
		$recipes .= ',"w2_magnesium":' . floatval($row['w2_magnesium']);
		$recipes .= ',"w2_total_alkalinity":' . floatval($row['w2_total_alkalinity']);
		$recipes .= ',"w2_ph":' . floatval($row['w2_ph']);
		$recipes .= ',"w2_cost":' . floatval($row['w2_cost']);
		$recipes .= ',"wg_amount":' . floatval($row['wg_amount']);
		$recipes .= ',"wg_calcium":' . floatval($row['wg_calcium']);
		$recipes .= ',"wg_sulfate":' . floatval($row['wg_sulfate']);
		$recipes .= ',"wg_chloride":' . floatval($row['wg_chloride']);
		$recipes .= ',"wg_sodium":' . floatval($row['wg_sodium']);
		$recipes .= ',"wg_magnesium":' . floatval($row['wg_magnesium']);
		$recipes .= ',"wg_total_alkalinity":' . floatval($row['wg_total_alkalinity']);
		$recipes .= ',"wg_ph":' . floatval($row['wg_ph']);
		$recipes .= ',"wb_calcium":' . floatval($row['wb_calcium']);
		$recipes .= ',"wb_sulfate":' . floatval($row['wb_sulfate']);
		$recipes .= ',"wb_chloride":' . floatval($row['wb_chloride']);
		$recipes .= ',"wb_sodium":' . floatval($row['wb_sodium']);
		$recipes .= ',"wb_magnesium":' . floatval($row['wb_magnesium']);
		$recipes .= ',"wb_total_alkalinity":' . floatval($row['wb_total_alkalinity']);
		$recipes .= ',"wb_ph":' . floatval($row['wb_ph']);
		$recipes .= ',"wa_acid_name":' . $row['wa_acid_name'];
		$recipes .= ',"wa_acid_perc":' . floatval($row['wa_acid_perc']);
		$recipes .= ',"wa_base_name":' . $row['wa_base_name'];
		if (isset($_GET['record'])) {
			// Append stock information.
			$fermentables = json_decode($row['json_fermentables'], true);
			for ($i = 0; $i < count($fermentables); $i++) {
				$fermentables[$i]['f_inventory'] = 0;	// Not in stock
				$fermentables[$i]['f_avail'] = 0;	// Ingredient not in db
				$sql2 = "SELECT inventory FROM inventory_fermentables ";
				$sql2 .= "WHERE name='".str_replace($rescapers, $rreplacements, $fermentables[$i]['f_name'])."' AND";
			        $sql2 .= " supplier='".str_replace($rescapers, $rreplacements, $fermentables[$i]['f_supplier'])."'";
				if ($result2 = mysqli_query($connect, $sql2)) {
					if ($obj = mysqli_fetch_object($result2)) {
						$fermentables[$i]['f_inventory'] = floatval($obj->inventory);
						$fermentables[$i]['f_avail'] = 1;
					}
					mysqli_free_result($result2);
				}
			}
			$recipes .= ',"fermentables":' . json_encode($fermentables, JSON_UNESCAPED_UNICODE);

			$hops = json_decode($row['json_hops'], true);
			for ($i = 0; $i < count($hops); $i++) {
				$hops[$i]['h_inventory'] = 0;   // Not in stock
				$hops[$i]['h_avail'] = 0;       // Ingredient not in db
				$sql2 = "SELECT inventory FROM inventory_hops ";
				$sql2 .= "WHERE name='".str_replace($rescapers, $rreplacements, $hops[$i]['h_name'])."' AND";
				$sql2 .= " origin='".str_replace($rescapers, $rreplacements, $hops[$i]['h_origin'])."' AND";
			        $sql2 .= " form='".$hops[$i]['h_form']."'";
				if ($result2 = mysqli_query($connect, $sql2)) {
					if ($obj = mysqli_fetch_object($result2)) {
						$hops[$i]['h_inventory'] = floatval($obj->inventory);
						$hops[$i]['h_avail'] = 1;
					}
					mysqli_free_result($result2);
				}
			}
			$recipes .= ',"hops":' . json_encode($hops, JSON_UNESCAPED_UNICODE);

			$miscs = json_decode($row['json_miscs'], true);
			for ($i = 0; $i < count($miscs); $i++) {
				$miscs[$i]['m_inventory'] = 0;   // Not in stock
				$miscs[$i]['m_avail'] = 0;       // Ingredient not in db
				$sql2 = "SELECT inventory FROM inventory_miscs ";
				$sql2 .= "WHERE name='".str_replace($rescapers, $rreplacements, $miscs[$i]['m_name'])."' AND";
			        $sql2 .= " type='".$miscs[$i]['m_type']."'";
				if ($result2 = mysqli_query($connect, $sql2)) {
					if ($obj = mysqli_fetch_object($result2)) {
						$miscs[$i]['m_inventory'] = floatval($obj->inventory);
						$miscs[$i]['m_avail'] = 1;
					}
					mysqli_free_result($result2);
				}
			}
			$recipes .= ',"miscs":' . json_encode($miscs, JSON_UNESCAPED_UNICODE);

			$yeasts = json_decode($row['json_yeasts'], true);
			for ($i = 0; $i < count($yeasts); $i++) {
				$yeasts[$i]['y_inventory'] = 0;   // Not in stock
				$yeasts[$i]['y_avail'] = 0;       // Ingredient not in db
				if (! isset($yeasts[$i]['y_tolerance']))
					$yeasts[$i]['y_tolerance'] = 0;
				$sql2 = "SELECT inventory,tolerance,sta1,bacteria,harvest_top,harvest_time,pitch_temperature,pofpos,zymocide ";
				$sql2 .= "FROM inventory_yeasts ";
				$sql2 .= "WHERE name='".str_replace($rescapers, $rreplacements, $yeasts[$i]['y_name'])."' AND";
			       	$sql2 .= " form='".str_replace($rescapers, $rreplacements, $yeasts[$i]['y_form'])."' AND";
				$sql2 .= " laboratory='".str_replace($rescapers, $rreplacements, $yeasts[$i]['y_laboratory'])."' AND";
			        $sql2 .= " product_id='".str_replace($rescapers, $rreplacements, $yeasts[$i]['y_product_id'])."'";
				if ($result2 = mysqli_query($connect, $sql2)) {
					if ($obj = mysqli_fetch_object($result2)) {
						$yeasts[$i]['y_inventory'] = floatval($obj->inventory);
						$yeasts[$i]['y_avail'] = 1;
						if ($yeasts[$i]['y_tolerance'] == 0)
							$yeasts[$i]['y_tolerance'] = floatval($obj->tolerance); // Upgrade alcohol tolerance
						// Upgrade new fields
                                                if (! isset($yeasts[$i]['y_sta1']))
                                                        $yeasts[$i]['y_sta1'] = intval($obj->sta1);
                                                if (! isset($yeasts[$i]['y_bacteria']))
                                                        $yeasts[$i]['y_bacteria'] = intval($obj->bacteria);
                                                if (! isset($yeasts[$i]['y_harvest_top']))
                                                        $yeasts[$i]['y_harvest_top'] = intval($obj->harvest_top);
                                                if (! isset($yeasts[$i]['y_harvest_time']))
                                                        $yeasts[$i]['y_harvest_time'] = intval($obj->harvest_time);
                                                if (! isset($yeasts[$i]['y_pitch_temperature']))
                                                        $yeasts[$i]['y_pitch_temperature'] = floatval($obj->pitch_temperature);
                                                if (! isset($yeasts[$i]['y_pofpos']))
                                                        $yeasts[$i]['y_pofpos'] = intval($obj->pofpos);
                                                if (! isset($yeasts[$i]['y_zymocide']))
                                                        $yeasts[$i]['y_zymocide'] = intval($obj->zymocide);
					} else {
                                                if (! isset($yeasts[$i]['y_sta1']))
                                                        $yeasts[$i]['y_sta1'] = 0;
                                                if (! isset($yeasts[$i]['y_bacteria']))
                                                        $yeasts[$i]['y_bacteria'] = 0;
                                                if (! isset($yeasts[$i]['y_harvest_top']))
                                                        $yeasts[$i]['y_harvest_top'] = 0;
                                                if (! isset($yeasts[$i]['y_harvest_time']))
                                                        $yeasts[$i]['y_harvest_time'] = 0;
                                                if (! isset($yeasts[$i]['y_pitch_temperature']))
                                                        $yeasts[$i]['y_pitch_temperature'] = 0;
                                                if (! isset($yeasts[$i]['y_pofpos']))
                                                        $yeasts[$i]['y_pofpos'] = 0;
                                                if (! isset($yeasts[$i]['y_zymocide']))
                                                        $yeasts[$i]['y_zymocide'] = 0;
					}
					mysqli_free_result($result2);
				}
			}
			syslog(LOG_NOTICE, json_encode($yeasts, JSON_UNESCAPED_UNICODE));
			$recipes .= ',"yeasts":' . json_encode($yeasts, JSON_UNESCAPED_UNICODE);
		} else {
			// Just leave it.
			$recipes .= ',"fermentables":' . $row['json_fermentables'];
			$recipes .= ',"hops":' . $row['json_hops'];
			$recipes .= ',"miscs":' . $row['json_miscs'];
			$recipes .= ',"yeasts":' . $row['json_yeasts'];
		}
		$recipes .= ',"mashs":' . $row['json_mashs'];
		$recipes .= '}';
	}
	$recipes .= ']';
	header("Content-type: application/json");
	echo $recipes;
}
?>

mercurial