www/includes/db_profile_mash.php

Sat, 08 Dec 2018 15:01:23 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 08 Dec 2018 15:01:23 +0100
changeset 129
7f41b61c3faf
parent 120
b28a3d6143bc
child 213
b0d484a5525e
permissions
-rw-r--r--

Added a loader during recipe load. Recalculate percentages if a fermentable row is deleted. Don't edit amounts if using weight percentages. Update grid percentages if a fermentable amount is changed. Recalculate IBU's after changes with fermentables.

23
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1 <?php
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
2
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
3 require($_SERVER['DOCUMENT_ROOT']."/config.php");
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
4 require($_SERVER['DOCUMENT_ROOT']."/version.php");
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
5
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
6 #Connect to the database
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
7 $connect = mysqli_connect(DBASE_HOST, DBASE_USER, DBASE_PASS, DBASE_NAME);
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
8 if (! $connect) {
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
9 die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
10 }
77
a9f8de2d7b2b Fixed most charset problems. Added fpdf library. Added inventory pdf creation.
Michiel Broek <mbroek@mbse.eu>
parents: 50
diff changeset
11 mysqli_set_charset($connect, "utf8" );
23
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
12
50
6d94167c2697 Next steps to implement a recipe editor
Michiel Broek <mbroek@mbse.eu>
parents: 45
diff changeset
13 $escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c");
6d94167c2697 Next steps to implement a recipe editor
Michiel Broek <mbroek@mbse.eu>
parents: 45
diff changeset
14 $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b");
120
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
15 $rescapers = array("'");
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
16 $rreplacements = array("\\'");
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
17 $disallowed = array('visibleindex','uniqueid','boundindex','uid');
50
6d94167c2697 Next steps to implement a recipe editor
Michiel Broek <mbroek@mbse.eu>
parents: 45
diff changeset
18
23
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
19 // get data and store in a json array
45
95251bedfab4 Moved three inventory tables and screens to profiles
Michiel Broek <mbroek@mbse.eu>
parents: 38
diff changeset
20 $query = "SELECT * FROM profile_mash ORDER BY name";
120
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
21 if (isset($_GET['insert']) || isset($_GET['update'])) {
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
22 if (isset($_GET['insert'])) {
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
23 $sql = "INSERT INTO";
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
24 }
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
25 if (isset($_GET['update'])) {
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
26 $sql = "UPDATE";
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
27 }
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
28 $sql .= " `profile_mash` SET name='" . mysqli_real_escape_string($connect, $_GET['name']);
23
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
29 $sql .= "', notes='" . mysqli_real_escape_string($connect, $_GET['notes']);
120
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
30 $array = $_GET['steps'];
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
31 foreach($array as $key => $item){
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
32 foreach ($disallowed as $disallowed_key) {
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
33 unset($array[$key]["$disallowed_key"]);
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
34 }
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
35 }
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
36 $sql .= "', steps='" . str_replace($rescapers,$rreplacements,json_encode($array));
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
37 if (isset($_GET['insert'])) {
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
38 $sql .= "';";
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
39 }
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
40 if (isset($_GET['update'])) {
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
41 $sql .= "' WHERE record='" . $_GET['record'] . "';";
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
42 }
23
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
43 $result = mysqli_query($connect, $sql);
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
44 if (! $result) {
45
95251bedfab4 Moved three inventory tables and screens to profiles
Michiel Broek <mbroek@mbse.eu>
parents: 38
diff changeset
45 syslog(LOG_NOTICE, "db_profile_mash: ".$sql." result: ".mysqli_error($connect));
23
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
46 } else {
120
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
47 if (isset($_GET['update'])) {
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
48 syslog(LOG_NOTICE, "db_profile_mash: updated record ".$_GET['record']);
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
49 } else {
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
50 $lastid = mysqli_insert_id($connect);
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
51 syslog(LOG_NOTICE, "db_profile_mash: inserted record ".$lastid);
b28a3d6143bc Screen layout changes.
Michiel Broek <mbroek@mbse.eu>
parents: 77
diff changeset
52 }
23
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
53 }
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
54 echo $result;
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
55
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
56 } else if (isset($_GET['delete'])) {
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
57 // DELETE COMMAND
45
95251bedfab4 Moved three inventory tables and screens to profiles
Michiel Broek <mbroek@mbse.eu>
parents: 38
diff changeset
58 $sql = "DELETE FROM `profile_mash` WHERE record='".$_GET['record']."';";
23
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
59 $result = mysqli_query($connect, $sql);
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
60 if (! $result) {
45
95251bedfab4 Moved three inventory tables and screens to profiles
Michiel Broek <mbroek@mbse.eu>
parents: 38
diff changeset
61 syslog(LOG_NOTICE, "db_profile_mash: ".$sql." result: ".mysqli_error($connect));
23
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
62 } else {
45
95251bedfab4 Moved three inventory tables and screens to profiles
Michiel Broek <mbroek@mbse.eu>
parents: 38
diff changeset
63 syslog(LOG_NOTICE, "db_profile_mash: deleted record ".$_GET['record']);
23
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
64 }
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
65 echo $result;
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
66
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
67 } else {
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
68 // SELECT COMMAND
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
69 $result = mysqli_query($connect, $query) or die("SQL Error 1: " . mysqli_error($connect));
38
b7d44c98d609 Initial setup for mash steps editing.
Michiel Broek <mbroek@mbse.eu>
parents: 25
diff changeset
70 $mashprofiles = '[';
b7d44c98d609 Initial setup for mash steps editing.
Michiel Broek <mbroek@mbse.eu>
parents: 25
diff changeset
71 $comma = FALSE;
23
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
72 while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
38
b7d44c98d609 Initial setup for mash steps editing.
Michiel Broek <mbroek@mbse.eu>
parents: 25
diff changeset
73 // Manual encode to JSON.
b7d44c98d609 Initial setup for mash steps editing.
Michiel Broek <mbroek@mbse.eu>
parents: 25
diff changeset
74 if ($comma) {
b7d44c98d609 Initial setup for mash steps editing.
Michiel Broek <mbroek@mbse.eu>
parents: 25
diff changeset
75 $mashprofiles .= ',';
b7d44c98d609 Initial setup for mash steps editing.
Michiel Broek <mbroek@mbse.eu>
parents: 25
diff changeset
76 }
b7d44c98d609 Initial setup for mash steps editing.
Michiel Broek <mbroek@mbse.eu>
parents: 25
diff changeset
77 $comma = TRUE;
b7d44c98d609 Initial setup for mash steps editing.
Michiel Broek <mbroek@mbse.eu>
parents: 25
diff changeset
78 $mashprofiles .= '{"record":' . $row['record'];
50
6d94167c2697 Next steps to implement a recipe editor
Michiel Broek <mbroek@mbse.eu>
parents: 45
diff changeset
79 $mashprofiles .= ',"name":"' . str_replace($escapers, $replacements, $row['name']);
6d94167c2697 Next steps to implement a recipe editor
Michiel Broek <mbroek@mbse.eu>
parents: 45
diff changeset
80 $mashprofiles .= '","notes":"' . str_replace($escapers, $replacements, $row['notes']);
38
b7d44c98d609 Initial setup for mash steps editing.
Michiel Broek <mbroek@mbse.eu>
parents: 25
diff changeset
81 $mashprofiles .= '","steps":' . $row['steps'];
b7d44c98d609 Initial setup for mash steps editing.
Michiel Broek <mbroek@mbse.eu>
parents: 25
diff changeset
82 $mashprofiles .= '}';
23
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
83 }
38
b7d44c98d609 Initial setup for mash steps editing.
Michiel Broek <mbroek@mbse.eu>
parents: 25
diff changeset
84 $mashprofiles .= ']';
b7d44c98d609 Initial setup for mash steps editing.
Michiel Broek <mbroek@mbse.eu>
parents: 25
diff changeset
85 header("Content-type: application/json");
b7d44c98d609 Initial setup for mash steps editing.
Michiel Broek <mbroek@mbse.eu>
parents: 25
diff changeset
86 echo $mashprofiles;
23
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
87 }
4b157d7a1cee Added first part of mash profiles. Steps are in the database but not in the web ui.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
88 ?>

mercurial