Added inline yeast editor

Sat, 22 Sep 2018 19:14:02 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 22 Sep 2018 19:14:02 +0200
changeset 61
3469979f83be
parent 60
dbbe408108ea
child 62
12c5eae75d1e

Added inline yeast editor

www/getyeastsources.php file | annotate | diff | comparison | revisions
www/import/from_brouwhulp.php file | annotate | diff | comparison | revisions
www/includes/db_recipes.php file | annotate | diff | comparison | revisions
www/js/recipes.js file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/www/getyeastsources.php	Sat Sep 22 19:14:02 2018 +0200
@@ -0,0 +1,26 @@
+<?php
+require_once('config.php');
+
+$query = "SELECT * FROM inventory_yeasts ORDER BY name";
+$connect = mysqli_connect(DBASE_HOST,DBASE_USER,DBASE_PASS,DBASE_NAME);
+if (! $connect) {
+	echo "[]";
+	return;
+}
+$result = mysqli_query($connect, $query);
+while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
+	$yeasts[] = array(
+		'record' => $row['record'],
+		'name' => $row['name'],
+		'type' => $row['type'],
+		'form' => $row['form'],
+		'laboratory' => $row['laboratory'],
+		'product_id' => $row['product_id'],
+		'min_temperature' => $row['min_temperature'],
+		'max_temperature' => $row['max_temperature'],
+		'attenuation' => $row['attenuation'],
+		'inventory' => $row['inventory'],
+		'cost' => $row['cost']
+	);
+}
+echo json_encode($yeasts);
--- a/www/import/from_brouwhulp.php	Sat Sep 22 15:02:43 2018 +0200
+++ b/www/import/from_brouwhulp.php	Sat Sep 22 19:14:02 2018 +0200
@@ -676,6 +676,8 @@
 				$yeasts .= ',"y_amount":' . floatval($yeast->AMOUNT);
 				if ($yeast->COST)
 					$yeasts .= ',"y_cost":' . floatval($yeast->COST);
+				else
+					$yeasts .= ',"y_cost":0';
 				$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) . '"';
@@ -688,6 +690,9 @@
 				} else {
 					$yeasts .= ',"y_use":"Secondary"';
 				}
+				$yeasts .= ',"y_min_temperature":' . floatval($yeast->MIN_TEMPERATURE);
+				$yeasts .= ',"y_max_temperature":' . floatval($yeast->MAX_TEMPERATURE);
+				$yeasts .= ',"y_attenuation":' . floatval($yeast->ATTENUATION);
 				$yeasts .= "}";
 			}
 
--- a/www/includes/db_recipes.php	Sat Sep 22 15:02:43 2018 +0200
+++ b/www/includes/db_recipes.php	Sat Sep 22 19:14:02 2018 +0200
@@ -11,6 +11,8 @@
 
 $escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c");
 $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b");
+$rescapers = array("'");
+$rreplacements = array("\\'");
 $disallowed = array('visibleindex','uniqueid','boundindex','uid');
 
 // get data and store in a json array
@@ -64,8 +66,8 @@
 				unset($array[$key]["$disallowed_key"]);
 			}
 		}
-		syslog(LOG_NOTICE, "json_fermentables=: ".json_encode($array));
-		$sql .= "', json_fermentables='" . json_encode($array);
+		syslog(LOG_NOTICE, "json_fermentables=: ".str_replace($rescapers,$rreplacements,json_encode($array)));
+		$sql .= "', json_fermentables='" . str_replace($rescapers,$rreplacements,json_encode($array));
 	}
 
 	if (isset($_POST['hops'])) {
@@ -75,8 +77,8 @@
 				unset($array[$key]["$disallowed_key"]);
 			}
 		}
-		syslog(LOG_NOTICE, "json_hops: ".json_encode($array));
-		$sql .= "', json_hops='" . json_encode($array);
+		syslog(LOG_NOTICE, "json_hops: ".str_replace($rescapers,$rreplacements,json_encode($array)));
+		$sql .= "', json_hops='" . str_replace($rescapers,$rreplacements,json_encode($array));
 	}
 
 	if (isset($_POST['miscs'])) {
@@ -86,8 +88,8 @@
 				unset($array[$key]["$disallowed_key"]);
 			}
 		}
-		syslog(LOG_NOTICE, "json_miscs: ".json_encode($array));
-		$sql .= "', json_miscs='" . json_encode($array);
+		syslog(LOG_NOTICE, "json_miscs: ".str_replace($rescapers,$rreplacements,json_encode($array)));
+		$sql .= "', json_miscs='" . str_replace($rescapers,$rreplacements,json_encode($array));
 	}
 
 	if (isset($_POST['yeasts'])) {
@@ -97,8 +99,8 @@
 				unset($array[$key]["$disallowed_key"]);
 			}
 		}
-		syslog(LOG_NOTICE, "json_yeasts: ".json_encode($array));
-		$sql .= "', json_yeasts='" . json_encode($array);
+		syslog(LOG_NOTICE, "json_yeasts: ". str_replace($rescapers,$rreplacements,json_encode($array)));
+		$sql .= "', json_yeasts='" . str_replace($rescapers,$rreplacements,json_encode($array));
 	}
 
 	if (isset($_POST['waters'])) {
@@ -108,8 +110,8 @@
 				unset($array[$key]["$disallowed_key"]);
 			}
 		}
-		syslog(LOG_NOTICE, "json_waters: ".json_encode($array));
-		$sql .= "', json_waters='" . json_encode($array);
+		syslog(LOG_NOTICE, "json_waters: ".str_replace($rescapers,$rreplacements,json_encode($array)));
+		$sql .= "', json_waters='" . str_replace($rescapers,$rreplacements,json_encode($array));
 	}
 
 	if (isset($_POST['mashs'])) {
@@ -119,8 +121,8 @@
 				unset($array[$key]["$disallowed_key"]);
 			}
 		}
-		syslog(LOG_NOTICE, "json_mashs: ".json_encode($array));
-		$sql .= "', json_mashs='" . json_encode($array);
+		syslog(LOG_NOTICE, "json_mashs: ".str_replace($rescapers,$rreplacements,json_encode($array)));
+		$sql .= "', json_mashs='" . str_replace($rescapers,$rreplacements,json_encode($array));
 	}
 
 	if (isset($_POST['insert'])) {
@@ -132,7 +134,7 @@
 
 	$result = mysqli_query($connect, $sql);
 	if (! $result) {
-		syslog(LOG_NOTICE, "db_recipes: ".$sql." result: ".mysqli_error($connect));
+		syslog(LOG_NOTICE, "db_recipes: result: ".mysqli_error($connect));
 	} else {
 		if (isset($_POST['update'])) {
 			syslog(LOG_NOTICE, "db_recipes: updated record ".$_POST['record']);
@@ -147,7 +149,8 @@
 	$sql = "DELETE FROM `recipes` WHERE record='".$_POST['record']."';";
 	$result = mysqli_query($connect, $sql);
 	if (! $result) {
-		syslog(LOG_NOTICE, "db_recipes: ".$sql." result: ".mysqli_error($connect));
+//		syslog(LOG_NOTICE, "db_recipes: ".$sql." result: ".mysqli_error($connect));
+		syslog(LOG_NOTICE, "db_recipes: result: ".mysqli_error($connect));
 	} else {
 		syslog(LOG_NOTICE, "db_recipes: deleted record ".$_POST['record']);
 	}
--- a/www/js/recipes.js	Sat Sep 22 15:02:43 2018 +0200
+++ b/www/js/recipes.js	Sat Sep 22 19:14:02 2018 +0200
@@ -593,8 +593,139 @@
                 })
         };
 
+	// Inline yeasts editor
+        var editYeast = function (data) {
+                var yeastSource = {
+                        localdata: data.yeasts,
+                        datatype: "local",
+                        cache: false,
+                        datafields: [
+                                { name: 'y_name', type: 'string' },
+				{ name: 'y_laboratory', type: 'string' },
+				{ name: 'y_product_id', type: 'string' },
+                                { name: 'y_amount', type: 'float' },
+                                { name: 'y_cost', type: 'float' },
+                                { name: 'y_type', type: 'string' },
+                                { name: 'y_form', type: 'string' },
+                                { name: 'y_time', type: 'float' },
+				{ name: 'y_min_temperature', type: 'float' },
+				{ name: 'y_max_temperature', type: 'float' },
+				{ name: 'y_attenuation', type: 'float' },
+                                { name: 'y_amount_is_weight', type: 'bool' },
+				{ name: 'y_use', type: 'string' }
+                        ],
+                        addrow: function (rowid, rowdata, position, commit) {
+                                commit(true);
+                        },
+                        deleterow: function (rowid, commit) {
+                                commit(true);
+                        }
+                };
+                var yeastAdapter = new $.jqx.dataAdapter(yeastSource);
+                // dropdownlist datasource from inventory_yeasts
+                var yeastUrl = "getyeastsources.php";
+                var yeastInvSource = {
+                        datatype: "json",
+                        datafields: [
+                                { name: 'record', type: 'number' },
+                                { name: 'name', type: 'string' },
+                                { name: 'type', type: 'string' },
+                                { name: 'form', type: 'string' },
+				{ name: 'laboratory', type: 'string' },
+				{ name: 'product_id', type: 'string' },
+				{ name: 'min_temperature', type: 'float' },
+				{ name: 'max_temperature', type: 'float' },
+				{ name: 'attenuation', type: 'float' },
+                                { name: 'cost', type: 'float' }
+                        ],
+                        url: yeastUrl,
+                        async: true
+                };
+                var yeastlist = new $.jqx.dataAdapter(yeastInvSource);
 
-	// Inline yeasts editor
+                $("#yeastGrid").jqxGrid({
+                        width: 1050,
+                        height: 300,
+                        source: yeastAdapter,
+                        theme: theme,
+                        selectionmode: 'singlerow',
+                        editmode: 'selectedrow',
+                        editable: true,
+                        localization: getLocalization(),
+                        showtoolbar: true,
+                        rendertoolbar: function (toolbar) {
+                                var me = this;
+                                var container = $("<div style='overflow: hidden; position: relative; margin: 5px;'></div>");
+                                toolbar.append(container);
+                                container.append('<div style="float: left; margin-left: 165px;" id="yaddrowbutton"></div>');
+                                container.append('<input style="float: left; margin-left: 230px;" id="ydeleterowbutton" type="button" value="Verwijder gist" />');
+                                // add yeast from dropdownlist.
+                                $("#yaddrowbutton").jqxDropDownList({
+                                        placeHolder: "Kies gist:",
+                                        theme: theme,
+                                        source: yeastlist,
+                                        displayMember: "name",
+                                        width: 150,
+                                        height: 27,
+                                        dropDownWidth: 300
+                                });
+                                $("#yaddrowbutton").on('select', function (event) {
+                                        if (event.args) {
+                                                var index = event.args.index;
+                                                var datarecord = yeastlist.records[index];
+                                                var row = {};
+                                                row["y_name"] = datarecord.name;
+						row["y_laboratory"] = datarecord.laboratory;
+						row["y_product_id"] = datarecord.product_id;
+						row["y_type"] = datarecord.type;
+						row["y_form"] = datarecord.form;
+                                                row["y_amount"] = 0;
+                                                row["y_cost"] = datarecord.cost;
+                                                row["y_use"] = "Primary";
+                                                row["y_time"] = 0;
+						if (datarecord.form == "Dry") {
+							row["y_amount_is_weight"] = 1;
+						} else {
+							row["y_amount_is_weight"] = 0;
+						}
+						row["y_min_temperature"] = datarecord.min_temperature;
+						row["y_max_temperature"] = datarecord.max_temperature;
+						row["y_attenuation"] = datarecord.attenuation;
+                                                var commit = $("#yeastGrid").jqxGrid('addrow', null, row);
+                                        }
+                                });
+                                // delete selected yeast.
+                                $("#ydeleterowbutton").jqxButton({ theme: theme, height: 27, width: 150 });
+                                $("#ydeleterowbutton").on('click', function () {
+                                        var selectedrowindex = $("#yeastGrid").jqxGrid('getselectedrowindex');
+                                        var rowscount = $("#yeastGrid").jqxGrid('getdatainformation').rowscount;
+                                        if (selectedrowindex >= 0 && selectedrowindex < rowscount) {
+                                                var id = $("#yeastGrid").jqxGrid('getrowid', selectedrowindex);
+                                                var commit = $("#yeastGrid").jqxGrid('deleterow', id);
+                                        }
+                                });
+                        },
+                        columns: [
+                                { text: 'Gist', editable: false, datafield: 'y_name' },
+				{ text: 'Laboratorium', editable: false, width: 150, datafield: 'y_laboratory' },
+				{ text: 'Code', editable: false, width: 90, datafield: 'y_product_id' },
+                                { text: 'Soort', editable: false, width: 80, align: 'center', cellsalign: 'center', datafield: 'y_form' },
+				{ text: 'Min.', editable: false, width: 70, align: 'right', cellsalign: 'right', datafield: 'y_min_temperature' },
+				{ text: 'Max.', editable: false, width: 70, align: 'right', cellsalign: 'right', datafield: 'y_max_temperature' },
+				{ text: 'Attn.', editable: false, width: 70, align: 'right', cellsalign: 'right', datafield: 'y_attenuation', cellsformat: 'f1' },
+				{ text: 'Voor', editable: false, width: 80, align: 'center', cellsalign: 'center', datafield: 'y_use' },
+                                { text: 'Hoeveel', datafield: 'y_amount', width: 110, align: 'right', cellsalign: 'right', cellsformat: 'f5',
+                                  columntype: 'numberinput',
+                                  validation: function (cell, value) {
+                                        if (value < 0 || value > 100000000000 ) {
+                                                return { result: false, message: "Volume moet  0-~ zijn" };
+                                        }
+                                        return true;
+                                  }
+                                }
+                        ]
+                })
+        };
 
 	// Inline waters editor
 	var editWater = function (data) {
@@ -793,6 +924,7 @@
 				editFermentable('');
 				editHop('');
 				editMisc('');
+				editYeast('');
 				editWater('');
 				$("#popupWindow").jqxWindow('open');
 			});
@@ -838,6 +970,7 @@
 					editFermentable(dataRecord);
 					editHop(dataRecord);
 					editMisc(dataRecord);
+					editYeast(dataRecord);
 					editWater(dataRecord);
 					// show the popup window.
 					$("#popupWindow").jqxWindow('open');
@@ -890,6 +1023,7 @@
 		var fermentablerow = $('#fermentableGrid').jqxGrid('getrows');
 		var hoprow = $('#hopGrid').jqxGrid('getrows');
 		var miscrow = $('#miscGrid').jqxGrid('getrows');
+		var yeastrow = $('#yeastGrid').jqxGrid('getrows');
 		var waterrow = $('#waterGrid').jqxGrid('getrows');
 		if (editrow >= 0) {
 			var rowRecord = $("#jqxgrid").jqxGrid('getrowdata', editrow);
@@ -929,6 +1063,7 @@
 				fermentables: fermentablerow,
 				hops: hoprow,
 				miscs: miscrow,
+				yeasts: yeastrow,
 				waters: waterrow
 			};
 			$('#jqxgrid').jqxGrid('updaterow', rowID, row);
@@ -969,6 +1104,7 @@
 				fermentables: fermentablerow,
 				hops: hoprow,
 				miscs: miscrow,
+				yeasts: yeastrow,
 				waters: waterrow
 			};
 			$('#jqxgrid').jqxGrid('addrow', null, newrow);

mercurial