Skip to content

Commit ded767c

Browse files
Version 2.0 Cottage Management Added
1 parent c459090 commit ded767c

File tree

11 files changed

+239
-234
lines changed

11 files changed

+239
-234
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
functions/img/*

assets/js/main.js

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
(function () {
22
'use strict'
3-
4-
// Fetch all the forms we want to apply custom Bootstrap validation styles to
53
var forms = document.querySelectorAll('.needs-validation')
64

7-
// Loop over them and prevent submission
85
Array.prototype.slice.call(forms)
96
.forEach(function (form) {
107
form.addEventListener('submit', function (event) {
@@ -81,7 +78,6 @@ new DataTable('table.table-display',{
8178
var address = $(this).data('address');
8279
var phone = $(this).data('phone');
8380

84-
// console.log(id, fullname, address, phone, email, date);
8581
$('input[name="id"]').val(id);
8682
$('input[name="fullname"]').val(fullname);
8783
$('input[name="address"]').val(address);
@@ -101,28 +97,17 @@ new DataTable('table.table-display',{
10197
console.log(id, username);
10298
});
10399

104-
} else if (currentPath.includes("/CUSTMBRS/inventory.php")) {
105-
$('a[data-bs-target="#update"]').on('click', function() {
100+
} else if (currentPath.includes("/CUSTMBRS/cottage.php")) {
101+
$('button[data-bs-target="#update"]').on('click', function() {
106102
var id = $(this).data('id');
107103
var name = $(this).data('name');
108-
var description = $(this).data('description');
109-
$('input[name="data_id"]').val(id);
104+
var day = $(this).data('day');
105+
var night = $(this).data('night');
106+
$('input[name="id"]').val(id);
110107
$('input[name="name"]').val(name);
111-
$('input[name="description"]').val(description);
108+
$('input[name="priceDay"]').val(day);
109+
$('input[name="priceNight"]').val(night);
112110
console.log(id);
113-
$('input[name="data_id"]').val(id);
114-
});
115-
116-
$('a[data-bs-target="#stock-in"]').on('click', function() {
117-
var id = $(this).data('id');
118-
console.log(id);
119-
$('input[name="data_id"]').val(id);
120-
});
121-
122-
$('a[data-bs-target="#stock-out"]').on('click', function() {
123-
var id = $(this).data('id');
124-
console.log(id);
125-
$('input[name="data_id"]').val(id);
126111
});
127112
}else if (currentPath.includes("/CUSTMBRS/rents.php")) {
128113
$('a[data-bs-target="#return"]').on('click', function() {
@@ -153,5 +138,10 @@ new DataTable('table.table-display',{
153138
console.log(id);
154139
$('input[name="id"]').val(id);
155140
});
141+
$('button[data-bs-target="#remove"]').on('click', function() {
142+
var id = $(this).data('id');
143+
console.log(id);
144+
$('input[name="id"]').val(id);
145+
});
156146

157147
} );

cottage.php

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
include_once 'functions/menu/offcanva-menu.php';
33
include_once 'functions/authentication.php';
4+
include_once 'functions/tables/datatables.php';
45
?>
56
<!DOCTYPE html>
67
<html data-bs-theme="light" lang="en">
@@ -73,25 +74,16 @@
7374
<div class="col">
7475
<p class="text-primary m-0 fw-bold">Cottage List</p>
7576
</div>
76-
<div class="col-3">
77+
<!-- <div class="col-3">
7778
<form class="d-none d-sm-inline-block me-auto ms-md-3 my-2 my-md-0 mw-100 navbar-search">
7879
<div class="input-group"><input class="bg-light form-control border-0 small bg-white" type="text" data-bs-toggle="tooltip" data-bss-tooltip="" placeholder="Search for ..." title="Here you can search for cottage name." style="background: var(--bs-info-border-subtle);"><button class="btn btn-primary py-0" type="button"><i class="fas fa-search"></i></button></div>
7980
</form>
80-
</div>
81+
</div> -->
8182
</div>
8283
</div>
8384
<div class="card-body">
8485
<div class="row gy-4 row-cols-1 row-cols-md-2 row-cols-xl-3">
85-
<div class="col-xl-4">
86-
<div class="card"><img class="card-img-top w-100 d-block fit-cover" style="height: 200px;" src="https://cdn.bootstrapstudio.io/placeholders/1400x800.png">
87-
<div class="card-body p-4">
88-
<p class="text-primary card-text mb-0">&lt;cottage_id&gt;</p>
89-
<h4 class="card-title">&lt;cottage&gt;</h4>
90-
<p class="card-text">&lt;price&gt;</p>
91-
<div class="d-flex"><button class="btn btn-primary mx-1" type="button">View</button><button class="btn btn-warning mx-1" type="button" data-bs-target="#update" data-bs-toggle="modal">Update</button><button class="btn btn-danger mx-1" type="button" data-bs-target="#remove" data-bs-toggle="modal">Remove</button></div>
92-
</div>
93-
</div>
94-
</div>
86+
<?php cottage_list(); ?>
9587
</div>
9688
</div>
9789
</div>
@@ -112,13 +104,23 @@
112104
<h4 class="modal-title">Add Cottage</h4><button class="btn-close" type="button" aria-label="Close" data-bs-dismiss="modal"></button>
113105
</div>
114106
<div class="modal-body">
115-
<form class="needs-validation" method="post" novalidate>
116-
<div class="mb-1"><label class="form-label">Name</label><input class="form-control" type="text" name="name" required="" placeholder="Cottage Name">
107+
<form class="needs-validation" action="functions/cottage-create.php" method="post" enctype="multipart/form-data" novalidate>
108+
<div class="mb-1"><label class="form-label">Name</label><input class="form-control" type="text" name="name" required="" placeholder="Cottage Name">
117109
<div class="invalid-feedback">
118110
Please enter your cottage name.
119111
</div>
120112
</div>
121-
<div class="mb-1"><label class="form-label">Picture</label><input class="form-control" type="file" name="picture" required="" accept="image/*">
113+
<div class="mb-1"><label class="form-label">Price Day</label><input class="form-control" type="number" name="priceDay" required="" value="0">
114+
<div class="invalid-feedback">
115+
Please enter your price.
116+
</div>
117+
</div>
118+
<div class="mb-1"><label class="form-label">Price Night</label><input class="form-control" type="number" name="priceNight" required="" value="0">
119+
<div class="invalid-feedback">
120+
Please enter your price.
121+
</div>
122+
</div>
123+
<div class="mb-1"><label class="form-label">Picture</label><input class="form-control" type="file" name="picture" required="" accept="image/*">
122124
<div class="invalid-feedback">
123125
Please add cottage picture.
124126
</div>
@@ -137,13 +139,24 @@
137139
<h4 class="modal-title">Update Cottage</h4><button class="btn-close" type="button" aria-label="Close" data-bs-dismiss="modal"></button>
138140
</div>
139141
<div class="modal-body">
140-
<form class="needs-validation" method="post" novalidate>
141-
<div class="mb-1"><label class="form-label">Name</label><input class="form-control" type="text" name="name" required="" placeholder="Cottage Name">
142+
<form class="needs-validation" action="functions/cottage-update.php" method="post" enctype="multipart/form-data" novalidate>
143+
<input type="hidden" name="id">
144+
<div class="mb-1"><label class="form-label">Name</label><input class="form-control" type="text" name="name" required="" placeholder="Cottage Name">
142145
<div class="invalid-feedback">
143146
Please enter your cottage name.
144147
</div>
145148
</div>
146-
<div class="mb-1"><label class="form-label">Picture</label><input class="form-control" type="file" name="picture" required="" accept="image/*">
149+
<div class="mb-1"><label class="form-label">Price Day</label><input class="form-control" type="number" name="priceDay" required="" value="0">
150+
<div class="invalid-feedback">
151+
Please enter your price.
152+
</div>
153+
</div>
154+
<div class="mb-1"><label class="form-label">Price Night</label><input class="form-control" type="number" name="priceNight" required="" value="0">
155+
<div class="invalid-feedback">
156+
Please enter your price.
157+
</div>
158+
</div>
159+
<div class="mb-1"><label class="form-label">Picture</label><input class="form-control" type="file" name="picture" required="" accept="image/*">
147160
<div class="invalid-feedback">
148161
Please add cottage picture.
149162
</div>
@@ -155,6 +168,7 @@
155168
</div>
156169
</div>
157170
</div>
171+
158172

159173
<div class="modal fade" role="dialog" tabindex="-1" id="remove">
160174
<div class="modal-dialog" role="document">
@@ -165,7 +179,10 @@
165179
<div class="modal-body">
166180
<p>Are you sure you want to remove this customer?</p>
167181
</div>
168-
<div class="modal-footer"><button class="btn btn-light" type="button" data-bs-dismiss="modal">Close</button><button class="btn btn-danger" type="button">Remove</button></div>
182+
<form action="functions/cottage-remove.php" method="post">
183+
<input type="hidden" name="id">
184+
<div class="modal-footer"><button class="btn btn-light" type="button" data-bs-dismiss="modal">Close</button><button class="btn btn-danger" type="submit">Remove</button></div>
185+
</form>
169186
</div>
170187
</div>
171188
</div>
@@ -182,7 +199,6 @@
182199
<script src="assets/js/buttons.html5.min.js"></script>
183200
<script src="assets/js/sweetalert2.all.min.js"></script>
184201
<script src="assets/js/main.js"></script>
185-
<script src="assets/js/fullcalendar.js"></script>
186202
<script src="assets/js/index.global.min.js"></script>
187203
<script src="assets/js/tinymce.min.js"></script>
188204
</body>

functions/cottage-create.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
include_once 'connection.php';
3+
4+
$name = $_POST['name'];
5+
$day = $_POST['priceDay'];
6+
$night = $_POST['priceNight'];
7+
$picture = $_FILES['picture'];
8+
9+
$sql = "SELECT * FROM cottages WHERE `name` = :name";
10+
$stmt = $db->prepare($sql);
11+
$stmt->bindParam(':name', $name);
12+
$stmt->execute();
13+
14+
if ($stmt->rowCount() > 0) {
15+
header('Location: ../cottage.php?type=error&message=' . $name . ' is already exist');
16+
exit;
17+
}
18+
19+
if ($picture['error'] === UPLOAD_ERR_OK) {
20+
$file_extension = pathinfo($picture['name'], PATHINFO_EXTENSION);
21+
$new_filename = uniqid() . '.' . $file_extension;
22+
$upload_dir = 'img/';
23+
$upload_path = $upload_dir . $new_filename;
24+
25+
if (move_uploaded_file($picture['tmp_name'], $upload_path)) {
26+
$sql = "INSERT INTO cottages (`name`, `picture`, `priceDay`, `priceNight`) VALUES (:name, :picture, :priceDay, :priceNight)";
27+
$stmt = $db->prepare($sql);
28+
$stmt->bindParam(':name', $name);
29+
$stmt->bindParam(':priceDay', $day);
30+
$stmt->bindParam(':priceNight', $night);
31+
$stmt->bindParam(':picture', $upload_path);
32+
$stmt->execute();
33+
34+
generate_logs('Adding cottage', $name . '| New cottage was added');
35+
header('Location: ../cottage.php?type=success&message=New cottage was added successfully');
36+
exit;
37+
} else {
38+
header('Location: ../cottage.php?type=error&message=Error uploading the image');
39+
exit;
40+
}
41+
} else {
42+
header('Location: ../cottage.php?type=error&message=Image upload failed');
43+
exit;
44+
}
45+
?>

functions/cottage-remove.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
include_once 'connection.php';
3+
4+
try {
5+
$id = $_POST['id'];
6+
7+
$sql = "SELECT * FROM cottages WHERE id = :id";
8+
$statement = $db->prepare($sql);
9+
$statement->bindParam(':id', $id);
10+
$statement->execute();
11+
$result = $statement->fetch(PDO::FETCH_ASSOC);
12+
13+
$sql = "DELETE FROM cottages WHERE id = :id";
14+
$statement = $db->prepare($sql);
15+
$statement->bindParam(':id', $id);
16+
$statement->execute();
17+
generate_logs('Removing cottage', $result['name'].' was removed');
18+
header('Location: ../cottage.php?type=success&message='.$result['name'].' was removed successfully!');
19+
} catch (\Throwable $th) {
20+
generate_logs('Removing cottages', $th);
21+
header('Location: ../cottage.php?type=error&message=Something went wrong, please try again');
22+
}

functions/cottage-update.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
include_once 'connection.php';
3+
$id = $_POST['id'];
4+
$name = $_POST['name'];
5+
$day = $_POST['priceDay'];
6+
$night = $_POST['priceNight'];
7+
$picture = $_FILES['picture'];
8+
9+
$sql = "SELECT * FROM cottages WHERE `name` = :name AND `id` != :id";
10+
$stmt = $db->prepare($sql);
11+
$stmt->bindParam(':name', $name);
12+
$stmt->bindParam(':id', $id);
13+
$stmt->execute();
14+
15+
if ($stmt->rowCount() > 0) {
16+
header('Location: ../cottage.php?type=error&message=' . $name . ' is already exist');
17+
exit;
18+
}
19+
20+
$upload_path = '';
21+
if ($picture['error'] === UPLOAD_ERR_OK) {
22+
$file_extension = pathinfo($picture['name'], PATHINFO_EXTENSION);
23+
$new_filename = uniqid() . '.' . $file_extension;
24+
$upload_dir = 'img/';
25+
$upload_path = $upload_dir . $new_filename;
26+
27+
if (!move_uploaded_file($picture['tmp_name'], $upload_path)) {
28+
header('Location: ../cottage.php?type=error&message=Error uploading the image');
29+
exit;
30+
}
31+
}
32+
33+
$sql = "UPDATE cottages SET `name` = :name, `picture` = :picture, `priceDay` = :priceDay, `priceNight` = :priceNight WHERE `id` = :id";
34+
$stmt = $db->prepare($sql);
35+
$stmt->bindParam(':name', $name);
36+
$stmt->bindParam(':priceDay', $day);
37+
$stmt->bindParam(':priceNight', $night);
38+
$stmt->bindParam(':picture', $upload_path);
39+
$stmt->bindParam(':id', $id);
40+
$stmt->execute();
41+
42+
if ($stmt->rowCount() > 0) {
43+
generate_logs('Adding cottage', $name . '| New cottage was added');
44+
header('Location: ../cottage.php?type=success&message=New cottage was added successfully');
45+
exit;
46+
} else {
47+
header('Location: ../cottage.php?type=error&message=Failed to update the cottage');
48+
exit;
49+
}

functions/login.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
<?php
22
include_once 'connection.php';
33

4-
// Get the form data
54
$username = $_POST['username'];
65
$password = $_POST['password'];
76

8-
// Check if the user exists
97
$sql = "SELECT * FROM users WHERE username = ?";
108
$stmt = $db->prepare($sql);
119
$stmt->execute([$username]);
@@ -21,6 +19,5 @@
2119
generate_logs('Login', $username.'| Logged in');
2220
header('location: ../index.php');
2321
} else {
24-
// Show an error message
2522
header('location: ../login.php?type=error&message=Wrong username or password');
2623
}

0 commit comments

Comments
 (0)