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 if (isset($_POST['insert']) || isset($_POST['update'])) { |
|
21 if (isset($_POST['insert'])) { |
|
22 $sql = "INSERT INTO `inventory_hops` SET "; |
|
23 } |
|
24 if (isset($_POST['update'])) { |
|
25 $sql = "UPDATE `inventory_hops` 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 .= "', alpha='" . $_POST['alpha']; |
|
36 $sql .= "', beta='" . $_POST['beta']; |
|
37 $sql .= "', humulene='" . $_POST['humulene']; |
|
38 $sql .= "', caryophyllene='" . $_POST['caryophyllene']; |
|
39 $sql .= "', cohumulone='" . $_POST['cohumulone']; |
|
40 $sql .= "', myrcene='" . $_POST['myrcene']; |
|
41 $sql .= "', hsi='" . $_POST['hsi']; |
|
42 $sql .= "', type='" . array_search($_POST['type'], $hoptype); |
|
43 $sql .= "', form='" . array_search($_POST['form'], $hopform); |
|
44 $sql .= "', notes='" . mysqli_real_escape_string($connect, $_POST['notes']); |
|
45 $sql .= "', origin='" . mysqli_real_escape_string($connect, $_POST['origin']); |
|
46 $sql .= "', substitutes='" . mysqli_real_escape_string($connect, $_POST['substitutes']); |
|
47 ($_POST['always_on_stock'] == 'true') ? $sql .= "', always_on_stock='1" : $sql .= "', always_on_stock='0"; |
|
48 $sql .= "', inventory='" . floatval($_POST['inventory']) / 1000.0; |
|
49 $sql .= "', cost='" . $_POST['cost'] . "'"; |
|
50 if ($_POST['production_date'] == '') |
|
51 $sql .= ", production_date=NULL"; |
|
52 else |
|
53 $sql .= ", production_date='" . $_POST['production_date'] . "'"; |
|
54 if ($_POST['tht_date'] == '') |
|
55 $sql .= ", tht_date=NULL"; |
|
56 else |
|
57 $sql .= ", tht_date='" . $_POST['tht_date'] . "'"; |
|
58 $sql .= ", total_oil='" . $_POST['total_oil']; |
|
59 if (isset($_POST['insert'])) { |
|
60 $sql .= "';"; |
|
61 } |
|
62 if (isset($_POST['update'])) { |
|
63 $sql .= "' WHERE record='" . $_POST['record'] . "';"; |
|
64 } |
|
65 $result = mysqli_query($connect, $sql); |
|
66 if (! $result) { |
|
67 syslog(LOG_NOTICE, "db_inventory_hops: ".$sql." result: ".mysqli_error($connect)); |
|
68 $response['error'] = true; |
|
69 $response['msg'] = "SQL fout: ".mysqli_error($connect); |
|
70 } |
|
71 exit(json_encode($response)); |
|
72 |
|
73 } else if (isset($_POST['delete'])) { |
|
74 // DELETE COMMAND |
|
75 $sql = "DELETE FROM `inventory_hops` WHERE record='".$_POST['record']."';"; |
|
76 $result = mysqli_query($connect, $sql); |
|
77 if (! $result) { |
|
78 syslog(LOG_NOTICE, "db_inventory_hops: ".$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 { |
|
85 // SELECT COMMAND |
|
86 $query = "SELECT * FROM inventory_hops ORDER BY origin,name"; |
|
87 $result = mysqli_query($connect, $query) or die("SQL Error 1: " . mysqli_error($connect)); |
|
88 while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { |
|
89 $hops[] = array( |
|
90 'record' => $row['record'], |
|
91 'name' => $row['name'], |
|
92 'uuid' => $row['uuid'], |
|
93 'alpha' => $row['alpha'], |
|
94 'beta' => $row['beta'], |
|
95 'humulene' => $row['humulene'], |
|
96 'caryophyllene' => $row['caryophyllene'], |
|
97 'cohumulone' => $row['cohumulone'], |
|
98 'myrcene' => $row['myrcene'], |
|
99 'hsi' => $row['hsi'], |
|
100 'type' => $hoptype[$row['type']], |
|
101 'form' => $hopform[$row['form']], |
|
102 'notes' => $row['notes'], |
|
103 'origin' => $row['origin'], |
|
104 'substitutes' => $row['substitutes'], |
|
105 'always_on_stock' => $row['always_on_stock'], |
|
106 'inventory' => floatval($row['inventory']) * 1000.0, |
|
107 'cost' => $row['cost'], |
|
108 'production_date' => $row['production_date'], |
|
109 'tht_date' => $row['tht_date'], |
|
110 'total_oil' => $row['total_oil'] |
|
111 ); |
|
112 } |
|
113 header("Content-type: application/json"); |
|
114 exit(json_encode($hops)); |
|
115 } |
|
116 |
|
117 syslog(LOG_NOTICE, "db_inventory_hops: missing arguments"); |
|
118 $response['error'] = true; |
|
119 $response['msg'] = "missing arguments"; |
|
120 echo json_encode($response); |
|
121 |
|
122 ?> |
|