1 <?php |
|
2 |
|
3 require($_SERVER['DOCUMENT_ROOT']."/config.php"); |
|
4 require($_SERVER['DOCUMENT_ROOT']."/version.php"); |
|
5 require($_SERVER['DOCUMENT_ROOT']."/includes/constants.php"); |
|
6 |
|
7 |
|
8 #Connect to the database |
|
9 $connect = mysqli_connect(DBASE_HOST, DBASE_USER, DBASE_PASS, DBASE_NAME); |
|
10 if (! $connect) { |
|
11 die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); |
|
12 } |
|
13 mysqli_set_charset($connect, "utf8" ); |
|
14 |
|
15 $response = array( |
|
16 'error' => false, |
|
17 'msg' => 'Ok', |
|
18 ); |
|
19 |
|
20 // get data and store in a json array |
|
21 if (isset($_POST['insert']) || isset($_POST['update'])) { |
|
22 if (isset($_POST['insert'])) { |
|
23 $sql = "INSERT INTO `inventory_fermentables` SET "; |
|
24 } |
|
25 if (isset($_POST['update'])) { |
|
26 $sql = "UPDATE `inventory_fermentables` SET "; |
|
27 } |
|
28 |
|
29 if (isset($_POST['uuid']) && (strlen($_POST['uuid']) == 36)) { |
|
30 $sql .= "uuid='" . $_POST['uuid']; |
|
31 } else { |
|
32 $uuid = str_replace("\n", "", file_get_contents('/proc/sys/kernel/random/uuid')); |
|
33 $sql .= "uuid='" . $uuid; |
|
34 } |
|
35 $sql .= "', name='" . mysqli_real_escape_string($connect, $_POST['name']); |
|
36 $sql .= "', type='" . array_search($_POST['type'], $fermentabletype); |
|
37 $sql .= "', yield='" . $_POST['yield']; |
|
38 $sql .= "', color='" . $_POST['color']; |
|
39 ($_POST['add_after_boil'] == 'true') ? $sql .= "', add_after_boil='1" : $sql .= "', add_after_boil='0"; |
|
40 $sql .= "', origin='" . mysqli_real_escape_string($connect, $_POST['origin']); |
|
41 $sql .= "', supplier='" . mysqli_real_escape_string($connect, $_POST['supplier']); |
|
42 $sql .= "', notes='" . mysqli_real_escape_string($connect, $_POST['notes']); |
|
43 $sql .= "', coarse_fine_diff='" . $_POST['coarse_fine_diff']; |
|
44 $sql .= "', moisture='" . $_POST['moisture']; |
|
45 $sql .= "', diastatic_power='" . $_POST['diastatic_power']; |
|
46 $sql .= "', protein='" . $_POST['protein']; |
|
47 $sql .= "', dissolved_protein='" . $_POST['dissolved_protein']; |
|
48 $sql .= "', max_in_batch='" . $_POST['max_in_batch']; |
|
49 ($_POST['recommend_mash'] == 'true') ? $sql .= "', recommend_mash='1" : $sql .= "', recommend_mash='0"; |
|
50 $sql .= "', added='" . array_search($_POST['added'], $added); |
|
51 ($_POST['always_on_stock'] == 'true') ? $sql .= "', always_on_stock='1" : $sql .= "', always_on_stock='0"; |
|
52 $sql .= "', di_ph='" . $_POST['di_ph']; |
|
53 $sql .= "', acid_to_ph_57='" . $_POST['acid_to_ph_57']; |
|
54 $sql .= "', graintype='" . array_search($_POST['graintype'], $graintype); |
|
55 $sql .= "', inventory='" . $_POST['inventory']; |
|
56 $sql .= "', cost='" . $_POST['cost'] . "'"; |
|
57 if ($_POST['production_date'] == '') |
|
58 $sql .= ", production_date=NULL"; |
|
59 else |
|
60 $sql .= ", production_date='" . $_POST['production_date'] . "'"; |
|
61 if ($_POST['tht_date'] == '') |
|
62 $sql .= ", tht_date=NULL"; |
|
63 else |
|
64 $sql .= ", tht_date='" . $_POST['tht_date'] . "'"; |
|
65 if (isset($_POST['insert'])) { |
|
66 $sql .= ";"; |
|
67 } |
|
68 if (isset($_POST['update'])) { |
|
69 $sql .= " WHERE record='" . $_POST['record'] . "';"; |
|
70 } |
|
71 $result = mysqli_query($connect, $sql); |
|
72 if (! $result) { |
|
73 syslog(LOG_NOTICE, "db_inventory_fermentables: ".$sql." result: ".mysqli_error($connect)); |
|
74 $response['error'] = true; |
|
75 $response['msg'] = "SQL fout: ".mysqli_error($connect); |
|
76 } |
|
77 exit(json_encode($response)); |
|
78 |
|
79 } else if (isset($_POST['delete'])) { |
|
80 // DELETE COMMAND |
|
81 $sql = "DELETE FROM `inventory_fermentables` WHERE record='".$_POST['record']."';"; |
|
82 $result = mysqli_query($connect, $sql); |
|
83 if (! $result) { |
|
84 syslog(LOG_NOTICE, "db_inventory_fermentables: ".$sql." result: ".mysqli_error($connect)); |
|
85 $response['error'] = true; |
|
86 $response['msg'] = "SQL fout: ".mysqli_error($connect); |
|
87 } |
|
88 exit(json_encode($response)); |
|
89 |
|
90 } else { |
|
91 // SELECT COMMAND |
|
92 $query = "SELECT * FROM inventory_fermentables ORDER BY supplier,name"; |
|
93 $result = mysqli_query($connect, $query) or die("SQL Error 1: " . mysqli_error($connect)); |
|
94 while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { |
|
95 $fermentables[] = array( |
|
96 'record' => $row['record'], |
|
97 'name' => $row['name'], |
|
98 'uuid' => $row['uuid'], |
|
99 'type' => $fermentabletype[$row['type']], |
|
100 'yield' => $row['yield'], |
|
101 'color' => $row['color'], |
|
102 'add_after_boil' => $row['add_after_boil'], |
|
103 'origin' => $row['origin'], |
|
104 'supplier' => $row['supplier'], |
|
105 'notes' => $row['notes'], |
|
106 'coarse_fine_diff' => $row['coarse_fine_diff'], |
|
107 'moisture' => $row['moisture'], |
|
108 'diastatic_power' => $row['diastatic_power'], |
|
109 'protein' => $row['protein'], |
|
110 'dissolved_protein' => $row['dissolved_protein'], |
|
111 'max_in_batch' => $row['max_in_batch'], |
|
112 'recommend_mash' => $row['recommend_mash'], |
|
113 'added' => $added[$row['added']], |
|
114 'always_on_stock' => $row['always_on_stock'], |
|
115 'di_ph' => $row['di_ph'], |
|
116 'acid_to_ph_57' => $row['acid_to_ph_57'], |
|
117 'graintype' => $graintype[$row['graintype']], |
|
118 'inventory' => $row['inventory'], |
|
119 'cost' => $row['cost'], |
|
120 'production_date' => $row['production_date'], |
|
121 'tht_date' => $row['tht_date'] |
|
122 ); |
|
123 } |
|
124 header("Content-type: application/json"); |
|
125 exit(json_encode($fermentables)); |
|
126 } |
|
127 |
|
128 syslog(LOG_NOTICE, "db_inventory_fermentables: missing arguments"); |
|
129 $response['error'] = true; |
|
130 $response['msg'] = "missing arguments"; |
|
131 echo json_encode($response); |
|
132 |
|
133 ?> |
|