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 #Connect to the database |
|
8 $connect = mysqli_connect(DBASE_HOST, DBASE_USER, DBASE_PASS, DBASE_NAME); |
|
9 if (! $connect) { |
|
10 die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); |
|
11 } |
|
12 mysqli_set_charset($connect, "utf8" ); |
|
13 |
|
14 $response = array( |
|
15 'error' => false, |
|
16 'msg' => 'Ok', |
|
17 ); |
|
18 |
|
19 // get data and store in a json array |
|
20 if (isset($_POST['insert']) || isset($_POST['update'])) { |
|
21 if (isset($_POST['insert'])) { |
|
22 $sql = "INSERT INTO `inventory_yeasts` SET "; |
|
23 } |
|
24 if (isset($_POST['update'])) { |
|
25 $sql = "UPDATE `inventory_yeasts` SET "; |
|
26 } |
|
27 |
|
28 if (isset($_POST['uuid']) && (strlen($_POST['uuid']) == 36)) { |
|
29 $sql .= "uuid='" . $_POST['uuid']; |
|
30 } else { |
|
31 $uuid = str_replace("\n", "", file_get_contents('/proc/sys/kernel/random/uuid')); |
|
32 $sql .= "uuid='" . $uuid; |
|
33 } |
|
34 $sql .= "', name='" . mysqli_real_escape_string($connect, $_POST['name']); |
|
35 $sql .= "', type='" . array_search($_POST['type'], $yeasttype); |
|
36 $sql .= "', form='" . array_search($_POST['form'], $yeastform); |
|
37 $sql .= "', laboratory='" . mysqli_real_escape_string($connect, $_POST['laboratory']); |
|
38 $sql .= "', product_id='" . mysqli_real_escape_string($connect, $_POST['product_id']); |
|
39 $sql .= "', short_desc='" . mysqli_real_escape_string($connect, $_POST['short_desc']); |
|
40 $sql .= "', min_temperature='" . $_POST['min_temperature']; |
|
41 $sql .= "', max_temperature='" . $_POST['max_temperature']; |
|
42 $sql .= "', flocculation='" . $_POST['flocculation']; |
|
43 $sql .= "', attenuation='" . $_POST['attenuation']; |
|
44 $sql .= "', notes='" . mysqli_real_escape_string($connect, $_POST['notes']); |
|
45 $sql .= "', best_for='" . mysqli_real_escape_string($connect, $_POST['best_for']); |
|
46 $sql .= "', max_reuse='" . $_POST['max_reuse']; |
|
47 $sql .= "', cells='" . floatval($_POST['cells']) * 1000000000.0; |
|
48 $sql .= "', tolerance='" . $_POST['tolerance']; |
|
49 ($_POST['sta1'] == 'true') ? $sql .= "', sta1='1" : $sql .= "', sta1='0"; |
|
50 ($_POST['bacteria'] == 'true') ? $sql .= "', bacteria='1" : $sql .= "', bacteria='0"; |
|
51 ($_POST['harvest_top'] == 'true') ? $sql .= "', harvest_top='1" : $sql .= "', harvest_top='0"; |
|
52 $sql .= "', harvest_time='" . $_POST['harvest_time']; |
|
53 $sql .= "', pitch_temperature='" . floatval($_POST['pitch_temperature']); |
|
54 ($_POST['pofpos'] == 'true') ? $sql .= "', pofpos='1" : $sql .= "', pofpos='0"; |
|
55 $sql .= "', zymocide='" . $_POST['zymocide']; |
|
56 $sql .= "', gr_hl_lo='" . $_POST['gr_hl_lo']; |
|
57 $sql .= "', sg_lo='" . floatval($_POST['sg_lo']); |
|
58 $sql .= "', gr_hl_hi='" . $_POST['gr_hl_hi']; |
|
59 $sql .= "', sg_hi='" . floatval($_POST['sg_hi']); |
|
60 $sql .= "', inventory='" . $_POST['inventory']; |
|
61 $sql .= "', cost='" . $_POST['cost'] . "'"; |
|
62 if ($_POST['production_date'] == '') |
|
63 $sql .= ", production_date=NULL"; |
|
64 else |
|
65 $sql .= ", production_date='" . $_POST['production_date'] . "'"; |
|
66 if ($_POST['tht_date'] == '') |
|
67 $sql .= ", tht_date=NULL"; |
|
68 else |
|
69 $sql .= ", tht_date='" . $_POST['tht_date'] . "'"; |
|
70 if (isset($_POST['insert'])) { |
|
71 $sql .= ";"; |
|
72 } |
|
73 if (isset($_POST['update'])) { |
|
74 $sql .= " WHERE record='" . $_POST['record'] . "';"; |
|
75 } |
|
76 $result = mysqli_query($connect, $sql); |
|
77 if (! $result) { |
|
78 syslog(LOG_NOTICE, "db_inventory_yeasts: ".$sql." result: ".mysqli_error($connect)); |
|
79 $response['error'] = true; |
|
80 $response['msg'] = "SQL fout: ".mysqli_error($connect); |
|
81 } |
|
82 exit(json_encode($response)); |
|
83 |
|
84 } else if (isset($_POST['delete'])) { |
|
85 // DELETE COMMAND |
|
86 $sql = "DELETE FROM `inventory_yeasts` WHERE record='".$_POST['record']."';"; |
|
87 $result = mysqli_query($connect, $sql); |
|
88 if (! $result) { |
|
89 syslog(LOG_NOTICE, "db_inventory_yeasts: ".$sql." result: ".mysqli_error($connect)); |
|
90 $response['error'] = true; |
|
91 $response['msg'] = "SQL fout: ".mysqli_error($connect); |
|
92 } |
|
93 exit(json_encode($response)); |
|
94 |
|
95 } else { |
|
96 // SELECT COMMAND |
|
97 $query = "SELECT * FROM inventory_yeasts ORDER BY laboratory,product_id,name"; |
|
98 $result = mysqli_query($connect, $query) or die("SQL Error 1: " . mysqli_error($connect)); |
|
99 while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { |
|
100 $yeasts[] = array( |
|
101 'record' => $row['record'], |
|
102 'name' => $row['name'], |
|
103 'uuid' => $row['uuid'], |
|
104 'type' => $yeasttype[$row['type']], |
|
105 'form' => $yeastform[$row['form']], |
|
106 'laboratory' => $row['laboratory'], |
|
107 'product_id' => $row['product_id'], |
|
108 'short_desc' => $row['short_desc'], |
|
109 'min_temperature' => $row['min_temperature'], |
|
110 'max_temperature' => $row['max_temperature'], |
|
111 'flocculation' => $row['flocculation'], |
|
112 'attenuation' => $row['attenuation'], |
|
113 'notes' => $row['notes'], |
|
114 'best_for' => $row['best_for'], |
|
115 'max_reuse' => $row['max_reuse'], |
|
116 'inventory' => $row['inventory'], |
|
117 'cost' => $row['cost'], |
|
118 'production_date' => $row['production_date'], |
|
119 'tht_date' => $row['tht_date'], |
|
120 'cells' => floatval($row['cells']) / 1000000000.0, |
|
121 'tolerance' => $row['tolerance'], |
|
122 'sta1' => $row['sta1'], |
|
123 'bacteria' => $row['bacteria'], |
|
124 'harvest_top' => $row['harvest_top'], |
|
125 'harvest_time' => $row['harvest_time'], |
|
126 'pitch_temperature' => floatval($row['pitch_temperature']), |
|
127 'pofpos' => $row['pofpos'], |
|
128 'zymocide' => $row['zymocide'], |
|
129 'gr_hl_lo' => $row['gr_hl_lo'], |
|
130 'sg_lo' => floatval($row['sg_lo']), |
|
131 'gr_hl_hi' => $row['gr_hl_hi'], |
|
132 'sg_hi' => floatval($row['sg_hi']) |
|
133 ); |
|
134 } |
|
135 header("Content-type: application/json"); |
|
136 exit(json_encode($yeasts)); |
|
137 } |
|
138 |
|
139 syslog(LOG_NOTICE, "db_inventory_yeasts: missing arguments"); |
|
140 $response['error'] = true; |
|
141 $response['msg'] = "missing arguments"; |
|
142 echo json_encode($response); |
|
143 |
|
144 ?> |
|