diff --git a/api/add.php b/api/add.php index b27c03c..6962f26 100644 --- a/api/add.php +++ b/api/add.php @@ -78,4 +78,4 @@ function generateRandomString($length = 10) { $randomString .= $characters[rand(0, $charactersLength - 1)]; } return $randomString; -} \ No newline at end of file +} diff --git a/api/all.php b/api/all.php index 2ceec31..522b912 100644 --- a/api/all.php +++ b/api/all.php @@ -5,7 +5,17 @@ include('./database.php'); $sql = $db->prepare("SELECT * FROM `$tablename`"); $sql->execute(); -$plants = $sql->fetchAll(PDO::FETCH_ASSOC); +$plants = []; + +foreach ($sql->fetchAll(PDO::FETCH_ASSOC) as $plant) { + $uploaddir = '../files/' . $plant['id']; + + if (file_exists($uploaddir) && is_dir($uploaddir)) { + $plant['files'] = $files = array_map(fn($value) => $uploaddir . '/' . $value, array_values(array_diff(scandir($uploaddir), array('.', '..')))); + } + + $plants[] = $plant; +} echo json_encode([ 'error' => false, @@ -15,12 +25,19 @@ ]); /* Struktura: -[ - { - "name": "", - "description": "", - "file": "http://...", <-- image - }, - ... -] -*/ \ No newline at end of file +{ + "error": false, + "status": 200, + "message": "Success", + "plants": [ + { + "id": 3, + "name": "", + "description": "", + "files": [ + "" + ] + } + ] +} +*/ diff --git a/api/info.php b/api/info.php index d95f97a..c9294ef 100644 --- a/api/info.php +++ b/api/info.php @@ -17,34 +17,13 @@ // FILES +$files = []; $uploaddir = '../files/' . $id; -if (!file_exists($uploaddir)) { - http_response_code(404); - echo json_encode([ - 'error' => true, - 'status' => 404, - 'id' => $id, - 'message' => "Can't find directory", - 'dir' => $uploaddir - ]); - die(); -} - -if (!is_dir($uploaddir)) { - http_response_code(500); - echo json_encode([ - 'error' => true, - 'status' => 500, - 'id' => $id, - 'message' => "Path is not directory", - 'dir' => $uploaddir - ]); - die(); +if (file_exists($uploaddir) && is_dir($uploaddir)) { + $files = array_map(fn($value) => $uploaddir . '/' . $value, array_values(array_diff(scandir($uploaddir), array('.', '..')))); } -$files = array_map(fn($value) => $uploaddir . '/' . $value, array_values(array_diff(scandir($uploaddir), array('.', '..')))); - // DATA FROM DB include('./database.php'); @@ -74,4 +53,21 @@ 'message' => 'Success', 'files' => $files, 'plant' => $plant -]); \ No newline at end of file +]); + +/* +{ + "error": false, + "status": 200, + "id": "", + "message": "Success", + "files": [ + "" + ], + "plant": { + "id": 3, + "name": "", + "description": "" + } +} +*/ diff --git a/panel/fragments/plants.html b/panel/fragments/plants.html index 26ff3bb..d2fdf96 100644 --- a/panel/fragments/plants.html +++ b/panel/fragments/plants.html @@ -15,24 +15,9 @@