1
+ <?php
2
+ if (!defined ('pvault_panel ' )){ die ('Not Found ' );}
3
+ global $ conn ;
4
+
5
+ $ json = file_get_contents ('php://input ' );
6
+ $ data = json_decode ($ json , true );
7
+
8
+ if (!$ data || !is_array ($ data )) {
9
+ http_response_code (400 );
10
+ echo json_encode (['error ' => 'Invalid JSON data ' ]);
11
+ return ;
12
+ }
13
+
14
+ if (empty ($ data ['formulas ' ]) || !is_array ($ data ['formulas ' ])) {
15
+ http_response_code (400 );
16
+ echo json_encode (['error ' => 'Missing or invalid formulas array ' ]);
17
+ return ;
18
+ }
19
+
20
+ foreach ($ data ['formulas ' ] as $ row ) {
21
+ // Parse formula metadata
22
+ $ fid = mysqli_real_escape_string ($ conn , $ row ['fid ' ]);
23
+ $ name = mysqli_real_escape_string ($ conn , $ row ['name ' ]);
24
+ $ product_name = mysqli_real_escape_string ($ conn , $ row ['product_name ' ] ?? '- ' );
25
+ $ notes = mysqli_real_escape_string ($ conn , $ row ['notes ' ] ?? '- ' );
26
+ $ concentration = (int )($ row ['concentration ' ] ?? 100 );
27
+ $ status = (int )($ row ['status ' ] ?? 0 );
28
+ $ isProtected = (int )($ row ['isProtected ' ] ?? 0 );
29
+ $ rating = (int )($ row ['rating ' ] ?? 0 );
30
+ $ profile = mysqli_real_escape_string ($ conn , $ row ['profile ' ] ?? 'Default ' );
31
+ $ src = (int )($ row ['src ' ] ?? 0 );
32
+ $ customer_id = (int )($ row ['customer_id ' ] ?? 0 );
33
+ $ revision = (int )($ row ['revision ' ] ?? 0 );
34
+ $ madeOn = mysqli_real_escape_string ($ conn , $ row ['madeOn ' ] ?? date ('Y-m-d H:i:s ' ));
35
+
36
+ $ query = "INSERT INTO formulasMetaData (fid, name, product_name, notes, finalType, status, isProtected, rating, profile, src, customer_id, revision, madeOn)
37
+ VALUES (' $ fid', ' $ name', ' $ product_name', ' $ notes', ' $ concentration', ' $ status', ' $ isProtected', ' $ rating', ' $ profile', ' $ src', ' $ customer_id', ' $ revision', ' $ madeOn') " ;
38
+
39
+ if (!mysqli_query ($ conn , $ query )) {
40
+ error_log (mysqli_error ($ conn ));
41
+ echo json_encode (['error ' => 'Failed to insert formula ' , 'details ' => mysqli_error ($ conn )]);
42
+ return ;
43
+ }
44
+
45
+ // Parse associated ingredients
46
+ if (!empty ($ row ['ingredients ' ]) && is_array ($ row ['ingredients ' ])) {
47
+ foreach ($ row ['ingredients ' ] as $ ingredient ) {
48
+ $ ingredient_fid = mysqli_real_escape_string ($ conn , $ ingredient ['fid ' ]);
49
+ $ ingredient_name = mysqli_real_escape_string ($ conn , $ ingredient ['ingredient ' ]);
50
+ $ ingredient_concentration = (float )($ ingredient ['concentration ' ] ?? 100 );
51
+ $ ingredient_quantity = (float )($ ingredient ['quantity ' ] ?? 0 );
52
+ $ ingredient_dilutant = mysqli_real_escape_string ($ conn , $ ingredient ['dilutant ' ]);
53
+
54
+ $ ingredient_query = "INSERT INTO formulas (fid, name, ingredient, concentration, dilutant, quantity, notes)
55
+ VALUES (' $ ingredient_fid', ' $ name', ' $ ingredient_name', ' $ ingredient_concentration', ' $ ingredient_dilutant', ' $ ingredient_quantity', '-') " ;
56
+
57
+ if (!mysqli_query ($ conn , $ ingredient_query )) {
58
+ error_log (mysqli_error ($ conn ));
59
+ echo json_encode (['error ' => 'Failed to insert ingredient ' , 'details ' => mysqli_error ($ conn )]);
60
+ }
61
+ }
62
+ }
63
+ }
64
+
65
+ echo json_encode (['success ' => 'Data inserted successfully ' ]);
66
+
67
+ ?>
68
+
69
+
0 commit comments