From 8fc30bf5132ce021f52f505cb5a66b4a010b208a Mon Sep 17 00:00:00 2001 From: haidar <89732397+haidarhamda@users.noreply.github.com> Date: Tue, 3 Oct 2023 21:17:56 +0700 Subject: [PATCH] feat: sighting edit & delete --- .htaccess | 10 -- index.php | 5 +- public/js/cats.js | 3 +- public/js/sighting.js | 150 +++++++++++++++--- public/view/sighting.php | 141 ++++++++++------ .../sightings/SightingController.php | 41 ++++- src/repositories/SightingRepository.php | 12 ++ src/services/SightingSrv.php | 29 ++++ 8 files changed, 314 insertions(+), 77 deletions(-) diff --git a/.htaccess b/.htaccess index 5d0c861..608100c 100644 --- a/.htaccess +++ b/.htaccess @@ -1,13 +1,3 @@ -#RewriteEngine On -# -## Redirect all requests to index.php -#RewriteCond %{REQUEST_FILENAME} !-f -#RewriteCond %{REQUEST_FILENAME} !-d -#RewriteRule ^ index.php [QSA,L] - -#RewriteEngine On -#RewriteRule ^(.*)$ /index.php [L,QSA] - RewriteEngine On RewriteBase / diff --git a/index.php b/index.php index 02c05a2..bdd27c4 100644 --- a/index.php +++ b/index.php @@ -25,7 +25,10 @@ $router->route("/cat", CatController::getInstance(), []); $router->route("/cat/*", CatController::getInstance(), []); $router->route("/sighting", SightingController::getInstance(), []); - +$router->route("/sighting/*",SightingController::getInstance(),[]); +//echo $_SERVER['REQUEST_URI']; +//echo $_SERVER['REQUEST_METHOD']; +echo $_SESSION['isAdmin']; $router->route("/user", UserController::getInstance(), ["GET" => "admin"]); $router->route("/user/*", UserController::getInstance(), ["POST" => "admin", "PUT" => "admin", "DELETE" => "admin"]); $router->run($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD']); \ No newline at end of file diff --git a/public/js/cats.js b/public/js/cats.js index ddc859f..6c35cfc 100644 --- a/public/js/cats.js +++ b/public/js/cats.js @@ -70,6 +70,7 @@ document.addEventListener("DOMContentLoaded", function () { deleteButtons.forEach(function (button) { button.addEventListener("click", function (event) { + event.preventDefault(); let conf = confirm('Are you sure you want to delete this cat?'); if (!conf) { event.preventDefault(); @@ -85,7 +86,7 @@ document.addEventListener("DOMContentLoaded", function () { }) .then(function (response) { if (response.ok) { - + window.location.href="/cat"; location.reload(); } else { diff --git a/public/js/sighting.js b/public/js/sighting.js index 58a6e88..99f4326 100644 --- a/public/js/sighting.js +++ b/public/js/sighting.js @@ -8,26 +8,138 @@ function closeAddSightingModal(){ addSightingModal.style.display="none"; } -const addSightingForm = document.getElementById("add-sighting-form"); - -addSightingForm.addEventListener("submit", function (e){ - e.preventDefault(); - const formData = new FormData(addSightingForm); - fetch("/sighting",{ - method: "POST", - body: formData, +const editSightingModal = document.getElementById("edit-sighting-modal"); + +function openEditSightingModal(){ + editSightingModal.style.display="block"; +} +function closeEditSightingModal(){ + editSightingModal.style.display="none"; +} + + + +document.addEventListener("DOMContentLoaded", function () { + + const addSightingForm = document.getElementById("add-sighting-form"); + console.log("aaa",addSightingForm); + + addSightingForm.addEventListener("submit", function (e){ + e.preventDefault(); + const formData = new FormData(addSightingForm); + fetch("/sighting",{ + method: "POST", + body: formData, + }) + .then((response) =>{ + if (response.ok){ + console.log(addSightingForm); + console.log("Sighting added"); + closeAddSightingModal(); + window.location.reload(); + } else { + console.log("failed"); + } + }) + .catch((error) =>{ + console.error(error); + }) }) - .then((response) =>{ - if (response.ok){ - console.log(addSightingForm); - console.log("Sighting added"); - closeAddSightingModal(); - window.location.reload(); - } else { - console.log("failed"); + + const deleteButton = document.querySelectorAll(".delete-button"); + deleteButton.forEach( function ( button){ + button.addEventListener("click", function (event){ + event.preventDefault(); + console.log("aaaa"); + let conf = confirm("Are you sure you want to delete this sighting?"); + if (!conf){ + event.preventDefault(); + return; } + + const deleteUrl=button.getAttribute("href"); + fetch(deleteUrl,{ + method:"DELETE", + }) + .then(function (response){ + if (response.ok){ + window.location.href="/sighting"; + location.reload(); + } else { + console.error("Delete failed"); + } + }) + .catch(function (error){ + console.error("networkError",error); + }) }) - .catch((error) =>{ - console.error(error); + }) + const editSightingForm = document.getElementById("edit-sighting-form"); + + console.log("bbb",editSightingForm); + + editSightingForm.addEventListener("submit", function (event){ + event.preventDefault(); + const sightingId=document.getElementById("edit-sighting-id").value; + console.log(sightingId) + const formData = new FormData(); + formData.append("cat_id", document.getElementById("edit-cat-name-select").value); + formData.append("sighting_location", document.getElementById("edit-sighting-location").value); + formData.append("date", document.getElementById("edit-date").value); + formData.append("time", document.getElementById("edit-time").value); + formData.append("sighting_description", document.getElementById("edit-sighting-description").value); + formData.append("image_url", document.getElementById("edit-image_url").files[0]); + + fetch(`/sighting/${sightingId}`,{ + method: "POST", + body: formData, }) -}) \ No newline at end of file + .then((response) =>{ + if (response.ok){ + closeEditSightingModal(); + location.reload(); + } else { + console.error("error editing sighting: ", response) + } + }) + .catch((error) =>{ + console.error("error editing sighting: ", error); + }); + }); +}) + +function editSighting(sightingId){ + const sightingCard = document.getElementById(`sighting-card-${sightingId}`); + + if (sightingId){ + openEditSightingModal(); + } else { + console.error("sighting card not found", sightingId); + } +} + +function populateSightingForm(sightingCard, sightingId){ + const catId = sightingCard.querySelector(".sighting-cat").textContent.trim(); + const sightingLocation = sightingCard.querySelector(".sighting-location").textContent.trim(); + const sightingDate = sightingCard.querySelector(".sighting-date").textContent.trim(); + const sightingTime = sightingCard.querySelector(".sighting-time").textContent.trim(); + const sightingDescription = sightingCard.querySelector(".sighting-description").textContent.trim(); + console.log(catId, sightingId) + // document.getElementById("edit-cat-name-select").value=catId; + document.getElementById("edit-sighting-location").value = sightingLocation; + document.getElementById("edit-date").value=sightingDate; + document.getElementById("edit-time").value=sightingTime; + document.getElementById("edit-sighting-description").value=sightingDescription; + document.getElementById("edit-sighting-id").value=sightingId; +} + +function editSighting(sightingId){ + const sightingCard=document.getElementById(`sighting-card-${sightingId}`); + + if (sightingCard){ + populateSightingForm(sightingCard,sightingId); + openEditSightingModal(); + } else { + console.error("Cat card not found for sightingId:", catId); + } +} \ No newline at end of file diff --git a/public/view/sighting.php b/public/view/sighting.php index 3d3e8c4..1573d55 100644 --- a/public/view/sighting.php +++ b/public/view/sighting.php @@ -11,11 +11,15 @@ <?php require_once(PROJECT_ROOT_PATH . '/public/components/Navbar.php'); ?> <div class="sighting-cards-container"> <?php foreach ($responseSightings as $sighting):?> - <div class="sighting-card" id="sighting-card<?=$sighting['sighting_id']?>"> -<!-- //if ($_SESSION['user_id']==$sighting['user_id']){--> -<!--echo ("<a class=\"delete-button\" href=\"/sighting/--><?php //=htmlspecialchars(" . $sighting['sighting_id']. "?><!--\">");}*/?>--> + <div class="sighting-card" id="sighting-card-<?=$sighting['sighting_id']?>"> + <?php if ($_SESSION['user_id']!=null &&$_SESSION['user_id']==$sighting['user_id']){ + echo ("<a class=\"delete-button\" href='sighting/" . $sighting['sighting_id'] . "'>X<a>"); + }?> +<!-- --><?php //if ($_SESSION['user_id']===$sighting['user_id']): ?> +<!-- <a class="delete-button" href="/sighting/--><?php //= htmlspecialchars($sighting['sighting_id'])?><!--">X</a>--> +<!-- --><?php //endif;?> <strong>Kucing:</strong> - <p class="cat-title"> + <p class="sighting-cat"> <?=htmlspecialchars($sighting['name'])?> </p> <strong>Sighted by:</strong> @@ -31,7 +35,7 @@ <?=htmlspecialchars($sighting['date'])?> </p> <strong>Time:</strong> - <p class="sigting-time"> + <p class="sighting-time"> <?=htmlspecialchars($sighting['time'])?> </p> <br> @@ -39,6 +43,9 @@ <?=htmlspecialchars($sighting['sighting_description'])?> </p> <img class="cat-image" src="public/<?= htmlspecialchars($sighting['image_url'])?>" alt="cat_image"> + <?php if ($_SESSION['user_id']===$sighting['user_id']): ?> + <a class="edit-button" onclick="editSighting(<?= $sighting['sighting_id']?>)">Edit</a> + <?php endif;?> </div> <!-- <p>--> <!-- --><?php //= htmlspecialchars($sighting['nama'])?> @@ -49,48 +56,92 @@ <!-- foreach ($responseCats as $cat){--> <!-- echo ('<option value="' . $cat['cat_id'] . '">' . $cat['name'] . '</option>');--> <!-- }?>--> - - <div> - <div id="add-sighting-modal" class="modal" > - <div class="modal-content"> - <span class="close" onclick="closeAddSightingModal()">×</span> - <h2>Add New Sighting</h2> - <form id="add-sighting-form" enctype="multipart/form-data"> - <div class="form-group"> - <label for="cat-name">Cat name:</label> - <select id="cat-name-select" name="cat_id" class="form-control"> - <?php foreach ($responseCats as $cat){ - echo ('<option value="' . $cat['cat_id'] . '">' . $cat['name'] . '</option>'); - }?> - </select> - </div> - <div class="form-group"> - <label for="sighting_location">Location:</label> - <textarea id="sighting_location" name="sighting_location" class="form-control"></textarea> - </div> - <div class="form-group"> - <label for="date">Date:</label> - <input type="date" id="date" name="date" class="form-control"> - </div> - <div class="form-group"> - <label for="time">Time:</label> - <input type="time" value="00:00:00" id="time" name="time" class="form-control"> - </div> - <div class="form-group"> - <label for="sighting_description">Description:</label> - <textarea id="sighting_description" name="sighting_description" class="form-control"></textarea> - </div> - <div class="form-group"> - <label for="image_url">Image File (JPG, JPEG, PNG):</label> - <input type="file" id="image_url" name="image_url" class="file-input" - accept=".jpg, .jpeg, .png"> - </div> - <button type="submit" class="submit-button">Add Cat</button> - </form> + <?php if (isset($_SESSION['user_id'])): ?> + <div> + <div id="edit-sighting-modal" class="modal"> + <div class="modal-content"> + <span class="close" onclick="closeEditSightingModal()">×</span> + <h2>Edit Sighting</h2> + <form id="edit-sighting-form" enctype="multipart/form-data"> + <input type="hidden" id="edit-sighting-id" name="sighting_id"> + <div class="form-group"> + <label for="edit-cat-name-select">Cat name:</label> + <select id="edit-cat-name-select" name="cat_id" class="form-control"> + <?php foreach ($responseCats as $cat){ + echo ('<option value="' . $cat['cat_id'] . '">' . $cat['name'] . '</option>'); + }?> + </select> + </div> + <div class="form-group"> + <label for="edit-sighting-location">Location:</label> + <textarea id="edit-sighting-location" name="sighting_location" class="form-control"></textarea> + </div> + <div class="form-group"> + <label for="edit-date">Date:</label> + <input type="date" id="edit-date" name="date" class="form-control"> + </div> + <div class="form-group"> + <label for="edit-time">Time:</label> + <input type="time" value="00:00:00" id="edit-time" name="time" class="form-control"> + </div> + <div class="form-group"> + <label for="edit-sighting-description">Description:</label> + <textarea id="edit-sighting-description" name="sighting_description" class="form-control"></textarea> + </div> + <div class="form-group"> + <label for="edit-image_url">Image File (JPG, JPEG, PNG):</label> + <input type="file" id="edit-image_url" name="image_url" class="file-input" + accept=".jpg, .jpeg, .png"> + </div> + <button type="submit" class="submit-button">Edit Sighting</button> + </form> + </div> </div> </div> - <button id="add-sighting-button" class="add-button" onclick="openAddSightingModal()">+</button> - </div> + <div> +<!-- --><?php //echo ("<p>aaaA</p>")?> + <div id="add-sighting-modal" class="modal" > + <div class="modal-content"> + <span class="close" onclick="closeAddSightingModal()">×</span> + <h2>Add New Sighting</h2> + <form id="add-sighting-form" enctype="multipart/form-data"> + <input type="hidden" id="edit-sighting-id" name="sighting_id" class="form-control"> + <div class="form-group"> + <label for="cat-name">Cat name:</label> + <select id="cat-name-select" name="cat_id" class="form-control"> + <?php foreach ($responseCats as $cat){ + echo ('<option value="' . $cat['cat_id'] . '">' . $cat['name'] . '</option>'); + }?> + </select> + </div> + <div class="form-group"> + <label for="sighting_location">Location:</label> + <textarea id="sighting_location" name="sighting_location" class="form-control"></textarea> + </div> + <div class="form-group"> + <label for="date">Date:</label> + <input type="date" id="date" name="date" class="form-control"> + </div> + <div class="form-group"> + <label for="time">Time:</label> + <input type="time" value="00:00:00" id="time" name="time" class="form-control"> + </div> + <div class="form-group"> + <label for="sighting_description">Description:</label> + <textarea id="sighting_description" name="sighting_description" class="form-control"></textarea> + </div> + <div class="form-group"> + <label for="image_url">Image File (JPG, JPEG, PNG):</label> + <input type="file" id="image_url" name="image_url" class="file-input" + accept=".jpg, .jpeg, .png"> + </div> + <button type="submit" class="submit-button">Add Cat</button> + </form> + </div> + </div> + <button id="add-sighting-button" class="add-button" onclick="openAddSightingModal()">+</button> + </div> + <?php endif;?> <script src="public/js/sighting.js"></script> </body> </html> \ No newline at end of file diff --git a/src/controllers/sightings/SightingController.php b/src/controllers/sightings/SightingController.php index 9a9b810..db6e2f7 100644 --- a/src/controllers/sightings/SightingController.php +++ b/src/controllers/sightings/SightingController.php @@ -60,12 +60,40 @@ class SightingController extends BaseController public function post($urlParams) { + if ($urlParams){ + $user_id=$_SESSION['user_id']; +// echo $_POST['user_id']; + $cat_id=$_POST['cat_id']; + $sighting_location=$_POST['sighting_location']; + $date=$_POST['date']; + $time=$_POST['time']; + $sighting_description=$_POST['sighting_description']; + $image_url="images/" . handleFileUpload('image_file','public/images/'); + $sightingData = [ + 'user_id' => $user_id, + 'cat_id' => $cat_id, + 'sighting_location' => $sighting_location, + 'date' => $date, + 'time' => $time, + 'sighting_description' => $sighting_description, + 'image_url' => $image_url, + ]; + + $sighting = $this->srv->updateSighting($urlParams[0], $sightingData); + + if ($sighting){ + $response = new BaseResponse(true, $sighting->toResponse(), "Sighting successfully updated", 200); + } else{ + $response = new BaseResponse(true, $sighting->toResponse(), "fail", 400); + } + + return $response->toJSON(); + } // $_POST+=['user_id'=>$_SESSION['user_id']]; // foreach ($_POST) $user_id=$_SESSION['user_id']; // echo $_POST['user_id']; $cat_id=$_POST['cat_id']; - echo $cat_id; $sighting_location=$_POST['sighting_location']; $date=$_POST['date']; $time=$_POST['time']; @@ -91,4 +119,15 @@ class SightingController extends BaseController } return $response->toJSON(); } + + public function delete($urlParams) + { + if ($urlParams){ + $sighting_id=$urlParams[0]; + + $sighting = $this->srv->deleteSighting($sighting_id); + $response = new BaseResponse(true, $sighting->toResponse(), "successfully deleted",200); + return $response->toJSON(); + } + } } \ No newline at end of file diff --git a/src/repositories/SightingRepository.php b/src/repositories/SightingRepository.php index 43dc3ad..b0facde 100644 --- a/src/repositories/SightingRepository.php +++ b/src/repositories/SightingRepository.php @@ -36,6 +36,18 @@ class SightingRepository extends BaseRepository )); } + public function updateSighting($sightingId, $sightingData){ + return $this->update("sighting_id",$sightingId,$sightingData, array( + 'cat_id' => PDO::PARAM_INT, + 'user_id' => PDO::PARAM_INT, + 'sighting_location' => PDO::PARAM_STR, + 'date' => PDO::PARAM_STR, + 'time' => PDO::PARAM_STR, + 'sighting_description' => PDO::PARAM_STR, + 'image_url' => PDO::PARAM_STR + )); + } + public function deleteSighting($sighting) { return $this->delete("sighting_id", $sighting); diff --git a/src/services/SightingSrv.php b/src/services/SightingSrv.php index a7d31bf..14a4219 100644 --- a/src/services/SightingSrv.php +++ b/src/services/SightingSrv.php @@ -49,4 +49,33 @@ class SightingSrv extends BaseSrv $sighting = new SightingModel(); return $sighting->constructFromArray($sqlRes); } + + public function deleteSighting($sightingId){ + $sighting = $this->getSightingById($sightingId); + $this->repository->delete("sighting_id",$sightingId); + return $sighting; + } + + public function getSightingById($sightingId){ + $sightingData = $this->repository->getSightingById($sightingId); + if (!$sightingData){ + throw new Exception("sighting not found"); + } + + $sighting = new SightingModel(); + return $sighting->constructFromArray($sightingData); + } + + public function updateSighting($sightingId, $sightingData){ + $existingSighting = $this->getSightingById($sightingId); + $updatedSighting = (new SightingModel()) -> constructFromArray($sightingData); + + foreach (get_object_vars($updatedSighting) as $property => $value){ + $existingSighting->$property = $value; + } + + $this->repository->updateSighting($sightingId, $existingSighting->toArray()); + + return $existingSighting; + } } \ No newline at end of file -- GitLab