Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #30

Merged
merged 2 commits into from
Nov 20, 2024
Merged
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 .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
DB_DSN = mysql:host=localhost;port=3306;dbname=fixmedb
DB_DSN = mysql:host=127.0.0.1;port=3306;dbname=fixmedb
DB_USER = root
DB_PASSWORD =
61 changes: 43 additions & 18 deletions controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,33 @@ class PostController extends Controller
/* Create method of a post */
public function create(Request $request)
{

$post = new Post();

// Fetch logged-in technician's ID from session
// Fetch logged-in technician's tech_id from session
$techID = Application::$app->session->get('technician');
// Check if techID exists to confirm that the technician is logged in to create a new post
if ($techID) {
// Assign technician's id to tech_id attribute of the Post model
$post->tech_id = $techID;
} else {
// Set flash message and redirect if not logged in
Application::$app->session->setFlash('error', 'You must be logged in to create a post.');
Application::$app->response->redirect('/technician-login');
return;
}

// Checks if the HTTP request is a POST request
if ($request->isPost()) {
// Retrieves the form data and fills the Post model with incoming data for validation and saving
$post->loadData($request->getBody());

if ($post->PostValidate() && $post->save()) {
// Validates the data and saves the data on the database
if ($post->postValidate() && $post->save()) {
Application::$app->session->setFlash('success', 'Post uploaded successfully!');
Application::$app->response->redirect('/technician-community');
return;
}
}

// Renders the view and passes the Post model instance to the view
return $this->render('/technician/technician-create-post', [
'model' => $post
]);
Expand All @@ -47,11 +51,14 @@ public function create(Request $request)
/* Retrieve method of a post */
public function index()
{

// Fetch all the posts with likes along with the person's data
$posts = (new Post)->getAllPostsWithLikes(Application::$app->customer->cus_id);
// The reference operator(&) modifies the $post array directly during the loop
foreach ($posts as &$post) {
// Get all the comments relavent to the post
$post['comments'] = (new Comment)->getAllComments($post['post_id']);
}
// Renders the view
$this->setLayout('auth');
return $this->render('/technician/technician-community', [
'posts' => $posts
Expand All @@ -63,8 +70,9 @@ public function edit(Request $request)
{
// Fetch the post ID from the request and find the post
$postID = $request->getBody()['post_id'] ?? null;
// Finds the post with the corresponding post_id in order to edit it
$post = (new Post)->findOne(['post_id' => $postID]);

// If find one method returns null, a flash message saying 'Post not found" will appear
if (!$post) {
Application::$app->session->setFlash('error', 'Post not found.');
Application::$app->response->redirect('/technician-community');
Expand All @@ -73,28 +81,32 @@ public function edit(Request $request)

// Fetch the logged-in technician's ID from the session
$techID = Application::$app->session->get('technician');
// If the logged in technician id and the person who added the post are different
if ($post->tech_id !== $techID) {
Application::$app->session->setFlash('error', 'Unauthorized access.');
Application::$app->response->redirect('/technician-community');
return;
}

// Check is current request is a POST request
if ($request->isPost()) {
// Populates the Post model with updated data from the request.
$post->loadData($request->getBody());

// Check if a new media file is uploaded
if (!empty($_FILES['media']['name'])) {
// Stores the uploaded file's name in the media property of the Post model.
$post->media = $_FILES['media']['name'];
move_uploaded_file($_FILES['media']['tmp_name'], 'assets/uploads/' . $post->media);
}

if ($post->PostValidate() && $post->editPost()) {
// Validates the requirements before editing the post
if ($post->postValidate() && $post->editPost()) {
// Sets a success flash message and redirects the user to the technician community page
Application::$app->session->setFlash('success', 'Post updated successfully!');
Application::$app->response->redirect('/technician-community');
return;
}
}

// Passes the current post data to the view for display in the form
return $this->render('/technician/technician-edit-post', [
'post' => $post
]);
Expand All @@ -105,60 +117,73 @@ public function delete(Request $request)
{
// Fetch the post ID from the request
$postID = $request->getBody()['post_id'] ?? null;
// Retrieves the logged-in technician’s ID from the session
$techID = Application::$app->session->get('technician');

if (!$postID || !$techID) {
Application::$app->session->setFlash('error', 'Invalid request.');
Application::$app->response->redirect('/technician-community');
return;
}

// Retrieves a post with the given post_id
$post = (new Post)->findOne(['post_id' => $postID]);

if (!$post) {
Application::$app->session->setFlash('error', 'Post not found.');
Application::$app->response->redirect('/technician-community');
return;
}

// No access if logged tech_id is not equal to the tech_id of the post
if ($post->tech_id !== $techID) {
Application::$app->session->setFlash('error', 'Unauthorized access.');
Application::$app->response->redirect('/technician-community');
return;
}

// Deletes the post from the database and returns true if successful, false otherwise
if ((new Post)->deletePost($postID, $techID)) {
Application::$app->session->setFlash('success', 'Post deleted successfully!');
} else {
Application::$app->session->setFlash('error', 'Failed to delete the post.');
}

// Redirects the user to the technician community page
Application::$app->response->redirect('/technician-community');
}

//$request: An object representing the current HTTP request, containing request data.
//$response: An object for preparing the HTTP response, including returning JSON data.
public function like(Request $request, Response $response)
{
// To construct and send the response, such as returning JSON data
$response = new Response();
// Identifies which post is being liked
$postId = $request->getBody()['post_id'];
// Identifies the customer performing the "like" action
$customerId = Application::$app->customer->cus_id;

// Create an instance of the Like model to allow interaction with the database for handling likes
$likeModel = new Like();
// Calls the toggleLike method of the Like model, passing the $postId and $customerId as arguments
$success = $likeModel->toggleLike($postId, $customerId);
// Get the updated like count
$likeCount = Like::getLikeCountByPostId($postId);

// Prepares and sends a JSON response to the client
return $response->json(['success' => $success, 'like_count' => $likeCount]);
}


public function unlike(Request $request, Response $response)
{
// This post_id identifies the post that the customer wants to "unlike"
$postId = $request->getBody()['post_id'];
// Fetches the logged-in customer’s ID
$customerId = Application::$app->customer->cus_id;

// This instance of the model is used to handle the logic for unliking a post
$likeModel = new Like();
// Calls the unlikePost method of the Like model, passing the postId (the ID of the post being unliked) and customerId
$success = $likeModel->unlikePost($postId, $customerId);
// Update like count after the unlike action
$likeCount = Like::getLikeCountByPostId($postId);

// Informs the client about whether the "unlike" action was successful and provides the updated like count for the post.
return $response->json(['success' => $success, 'like_count' => $likeCount]);
}

Expand Down
7 changes: 4 additions & 3 deletions core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public function __construct($rootPath, array $config)
$this->customer = null;
}

$primaryValueSeviceCentre = $this->session->get('service_center');
if ($primaryValueSeviceCentre) {
$primaryValueServiceCentre = $this->session->get('service_center');
if ($primaryValueServiceCentre) {
$serviceCenterInstance = new $this->serviceCenterClass;
$primaryKey = $serviceCenterInstance->primaryKey();
$this->serviceCenter = $serviceCenterInstance->findOne([$primaryKey => $primaryValueSeviceCentre]);
$this->serviceCenter = $serviceCenterInstance->findOne([$primaryKey => $primaryValueServiceCentre]);
} else {
$this->serviceCenter = null;
}
Expand Down Expand Up @@ -130,4 +130,5 @@ public function logoutServiceCenter()
$this->serviceCenter = null;
$this->session->remove('service_center');
}

}
2 changes: 1 addition & 1 deletion core/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function validate()
return empty($this->errors);
}

public function PostValidate()
public function postValidate()
{
foreach ($this->postRules() as $attribute => $rules) {
$value = $this->{$attribute};
Expand Down
10 changes: 10 additions & 0 deletions core/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,27 @@ public function isPost()

public function getBody()
{
// Empty associative array
$body = [];
// Checks if the request method is GET
if ($this->method() === 'get') {
// $_GET: A PHP superglobal that holds query parameters from the URL (e.g., ?key=value).
// Iterates through each key-value pair in the $_GET array
foreach ($_GET as $key => $value) {
// Sanitizes the value associated with $key from $_GET
$body[$key] = filter_input(INPUT_GET, $key, FILTER_SANITIZE_SPECIAL_CHARS);
}
}
// Checks if the request method is POST
if ($this->method() === 'post') {
// $_POST: A PHP superglobal that holds form data submitted via HTTP POST.
// Iterates through each key-value pair in the $_POST array
foreach ($_POST as $key => $value) {
// Sanitizes the values and adds to the $body array
$body[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS);
}
}
// Returns the sanitized data
return $body;
}
}
2 changes: 1 addition & 1 deletion views/layouts/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<li><a href="#" class="nav-link px-2">About</a></li>
</ul>

<?php if (Application::isGuestTechnician()): ?>
<?php if (Application::isGuestTechnician() || Application::isGuestCustomer() || Application::isGuestServiceCenter()): ?>
<div class="col-md-3 text-center">
<button type="button" class="btn btn-outline-primary me-2"><a class="text-decoration-none"
href="/select-user-login">Login</a>
Expand Down
10 changes: 9 additions & 1 deletion views/service-centre/components/header.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
use app\core\Application;
?>


Expand All @@ -16,7 +17,14 @@
</label>
</div>

<h6 class="user-name">
<?php
$username = strtoupper(Application::$app->service_center->{'name'});
echo $username;
?>
</h6>

<div class="user">
<img src="/assets/technician-dashboard/customer02.jpg" alt="">
<img src="/assets/select-user-service-centre.png" alt="">
</div>
</div>