Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/add.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@ function generateRandomString($length = 10) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
}
37 changes: 27 additions & 10 deletions api/all.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -15,12 +25,19 @@
]);

/* Struktura:
[
{
"name": "",
"description": "",
"file": "http://...", <-- image
},
...
]
*/
{
"error": false,
"status": 200,
"message": "Success",
"plants": [
{
"id": 3,
"name": "",
"description": "",
"files": [
""
]
}
]
}
*/
46 changes: 21 additions & 25 deletions api/info.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -74,4 +53,21 @@
'message' => 'Success',
'files' => $files,
'plant' => $plant
]);
]);

/*
{
"error": false,
"status": 200,
"id": "",
"message": "Success",
"files": [
""
],
"plant": {
"id": 3,
"name": "",
"description": ""
}
}
*/
19 changes: 2 additions & 17 deletions panel/fragments/plants.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,9 @@ <h4>Plants Database</h4>
<th>Zdjęcia</th>
</tr>
</thead>
<tbody id="table-plants">
</tbody>
<script type="text/javascript">
const json = getPlants();
const tbody = document.getElementById("table-plants");
let codeInner = "";

// To jest obrzydliwe... ale czas to pieniądz :D
json.forEach(element => codeInner += "<td>\n"
+ "<td>" + element["name"] + "</td>\n"
+ "<td>" + element["description"] + "</td>\n"
+ "<td><a href=\"" + element["file"] + "\"><img width=\"100%\" src=\"../../res/link_icon.png\"></a></td>\n"
+ "</tr>\n"
);
tbody.innerHTML = codeInner;
</script>
<tbody id="table-plants"></tbody>
</table>
</div>
</div>
</div>
<script src="../js/plants.js"></script>
<script src="../js/plants.js"></script>