Skip to content

Commit

Permalink
Merge pull request #57 from wanderlust-group-project-1/nirmal
Browse files Browse the repository at this point in the history
feat: Admin->tips crud
  • Loading branch information
nsavinda authored Oct 31, 2023
2 parents b79d102 + c89113d commit 04971d7
Show file tree
Hide file tree
Showing 8 changed files with 372 additions and 73 deletions.
46 changes: 46 additions & 0 deletions app/controllers/Admin/Tips.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

class Tips
{
use Controller;

public function index(string $a = '', string $b = '', string $c = ''): void
{

$tips = new TipsModel();
$data['tips'] = $tips->findAll();
// show($data);
$this->view('admin/tips', $data);
}

public function update(string $a = '', string $b = '', string $c = ''): void
{

$tips = new TipsModel();
$tips->updateTip($_POST);

redirect('admin/tips');
}

public function add(string $a = '', string $b = '', string $c = ''): void
{

$tips = new TipsModel();
$tips->addTip($_POST);
// show($tips);

redirect('admin/tips');
}

public function delete(string $a = '', string $b = '', string $c = ''): void
{

// show($a);
// show($b);
// show($c);
$tips = new TipsModel();
$tips->delete($a, 'id');

redirect('admin/tips');
}
}
2 changes: 1 addition & 1 deletion app/controllers/Tips.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public function update(string $a = '', string $b = '', string $c = ''): void
$tips = new TipsModel();
$tips->updateTips($_POST);

redirect('profile'); //methana mokadda?
redirect('profile');
}
}
2 changes: 1 addition & 1 deletion app/core/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function query(string $query, array $data = []): ?array {
return $result;
}
}
return null;
return [];
}

public function get_row(string $query, array $data = []): ?object {
Expand Down
2 changes: 1 addition & 1 deletion app/core/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function findAll():array{
}


public function where(array $data, array $data_not = []): array{
public function where(array $data, array $data_not = []){

$keys = array_keys($data);
$keys_not = array_keys($data_not);
Expand Down
32 changes: 29 additions & 3 deletions app/models/Tips.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class TipsModel
//'id',
'title',
'description',
'author',
'id',
];

public function validateCustomerSignup(array $data)
Expand All @@ -25,18 +27,42 @@ public function validateCustomerSignup(array $data)
return empty($this->errors);
}

public function updateTips(array $data)
public function updateTip(array $data)
{

// $user = new UserModel;

$data['id'] = $_SESSION['USER']->id;
// $data['id'] = $_SESSION['USER']->id;
$id = $data['id'];
// alowed column
$data = array_filter($data, function ($key) {
return in_array($key, $this->allowedColumns);
}, ARRAY_FILTER_USE_KEY);

// show($data);
return $this->update($id, $data, 'id');
}

public function addTip(array $data)
{

// $user = new UserModel;

// $data['id'] = $_SESSION['USER']->id;
$data['author'] = "admin";

// alowed column
$data = array_filter($data, function ($key) {
return in_array($key, $this->allowedColumns);
}, ARRAY_FILTER_USE_KEY);

return $this->update($_SESSION['USER']->id, $data, 'id');
return $this->insert($data);
}

public function findAll(): array
{
$data['author'] = "admin";
return $this->where($data);
return false;
}
}
2 changes: 1 addition & 1 deletion app/views/admin/layout/sidebar.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
</li>

<li class="nav-item">
<a class="nav-link" href="index.html">
<a class="nav-link" href="<?php echo ROOT_DIR ?>/admin/tips">
<i class="ti-shield menu-icon"></i>
<span class="menu-title">Tips and Know-hows</span>
</a>
Expand Down
185 changes: 119 additions & 66 deletions app/views/admin/tips.view.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,91 +4,144 @@

?>

<!-- Add button -->
<div class="tips-page">

<div class="table-container">
<table class="data-table">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>Lahiru Sandaruwan</td>
<td>lahiru@example.com</td>
<td><span class="status accepted">Accepted</span></td>
<td><button class="view-button">View</button></td>
</tr>
<div >
<button class="add-button" >Add</button>



</div>
<table class="data-table">
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($tips as $tip): ?>
<tr>
<td>Dasun Thathsara</td>
<td>dasun@example.com</td>
<td><span class="status not-accepted">Not Accepted</span></td>
<td><button class="view-button">View</button></td>
<td><?php echo $tip->title; ?></td>
<td><?php echo $tip->description; ?></td>
<td><button key="<?php echo $tip->id; ?>" class="view-button">Update</button></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

<!-- Add more rows as needed -->
</tbody>
</table>
</div>

<!-- Modal -->
<div class="rental-services-modal" id="rental-services-modal">
<!-- Add Form Modal -->
<div class="modal" id="add-modal">
<div class="modal-content">
<span class="close">&times;</span>
<div class="profile-info">
<img src="<?php echo ROOT_DIR ?>/assets/images/dp.jpg" alt="Profile Image" class="profile-image">
<h2 id="profile-name">Lahiru Sandaruwan </h2>
<p id="profile-email">lahiru@example.com</p>
<p id="profile-address">Kandy</p>
<p id="profile-status" class="accepted">Accepted</p>
<div class="profile-links">
<a href="#" id="link-1">Link 1</a>
<a href="#" id="link-2">Link 2</a>
</div>
</div>
<h2>Add Form</h2>
<form id="add-form" action="<?php echo ROOT_DIR ?>/admin/tips/add" method="post" >
<label for="title">Title:</label>
<input type="text" id="title" name="title" required><br>

<label for="description">Description:</label>
<textarea id="description" name="description" rows="4" required></textarea><br>

<button type="submit">Add</button>
</form>
</div>
</div>

<!-- Update Form Modal -->
<div class="modal" id="update-modal">
<div class="modal-content">
<span class="close">&times;</span>
<h2>Tips Update Form</h2>
<form id="update-form" action="<?php echo ROOT_DIR ?>/admin/tips/update" method="post" >
<input name="id" id="tip-id" type="hidden" value="">

<label for="updated-title">Title:</label>
<input type="text" id="updated-title" name="title" required><br>

<script>
// Get the modal
var modal = document.getElementById("rental-services-modal");
<label for="updated-description">Description:</label>
<textarea id="updated-description" name="description" rows="4" required></textarea><br>

<button type="submit">Update</button>
<button class="delete-button"> <a id="delete-tip" href="<?php echo ROOT_DIR ?>/admin/tips/delete/ "> Delete </a> </button>
</form>
</div>
</div>

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
</div>

// Get all view buttons
var viewButtons = document.querySelectorAll('.view-button');
</div>

// Function to handle modal display
function openModal(content) {
// document.getElementById("modal-content").innerHTML = content;
modal.style.display = "block";
}
<script>
// Get the modal elements
var addModal = document.getElementById("add-modal");
var updateModal = document.getElementById("update-modal");

// Get the buttons that open the modals
var addButton = document.querySelector('.add-button');
var updateButtons = document.querySelectorAll('.view-button');

// Function to open add form modal
function openAddModal() {
addModal.style.display = "block";
}

// Function to open update form modal
function openUpdateModal(name, email,id) {
// Populate update form fields with existing data
document.getElementById("updated-title").value = name;
document.getElementById("updated-description").value = email;
document.getElementById("tip-id").value = id;
document.getElementById("delete-tip").href = "<?php echo ROOT_DIR ?>/admin/tips/delete/"+id;
updateModal.style.display = "block";
}

// Event listener for add button click
addButton.addEventListener('click', function() {
openAddModal();
});

// Event listeners for update buttons click
updateButtons.forEach(function(button) {
button.addEventListener('click', function() {
var name = this.parentElement.parentElement.querySelector('td:first-child').textContent;
var email = this.parentElement.parentElement.querySelector('td:nth-child(2)').textContent;
var id = this.getAttribute('key');
openUpdateModal(name, email , id);
});
});

// Add click event listener to view buttons
viewButtons.forEach(function(button) {
button.addEventListener('click', function() {
// Close modals when the close button is clicked
var closeButtons = document.querySelectorAll('.close');

var name = this.parentElement.parentElement.querySelector('td:first-child').textContent;
var email = this.parentElement.parentElement.querySelector('td:nth-child(2)').textContent;
openModal("Name: " + name + "<br>Email: " + email);
});
closeButtons.forEach(function(button) {
button.addEventListener('click', function() {
addModal.style.display = "none";
updateModal.style.display = "none";
});
});

// Handle form submissions (you can add your form submission logic here)
document.getElementById("add-form").addEventListener('submit', function(event) {
// event.preventDefault();
// Handle add form submission logic
// ...
// Close the add modal after submission if successful
addModal.style.display = "none";
});

document.getElementById("update-form").addEventListener('submit', function(event) {
// event.preventDefault();
// Handle update form submission logic
// ...
// Close the update modal after submission if successful
updateModal.style.display = "none";
});

// Close the modal when the close button is clicked
span.onclick = function() {
modal.style.display = "none";
}

// Close the modal if the user clicks outside of it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>


Expand Down
Loading

0 comments on commit 04971d7

Please sign in to comment.