Skip to content

Commit 0bb28bf

Browse files
authored
Merge pull request #30 from kasunmendis7/develop
Develop
2 parents 33d313a + 991bc03 commit 0bb28bf

File tree

7 files changed

+69
-25
lines changed

7 files changed

+69
-25
lines changed

.env

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
DB_DSN = mysql:host=localhost;port=3306;dbname=fixmedb
1+
DB_DSN = mysql:host=127.0.0.1;port=3306;dbname=fixmedb
22
DB_USER = root
33
DB_PASSWORD =

controllers/PostController.php

+43-18
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,33 @@ class PostController extends Controller
1616
/* Create method of a post */
1717
public function create(Request $request)
1818
{
19+
1920
$post = new Post();
2021

21-
// Fetch logged-in technician's ID from session
22+
// Fetch logged-in technician's tech_id from session
2223
$techID = Application::$app->session->get('technician');
24+
// Check if techID exists to confirm that the technician is logged in to create a new post
2325
if ($techID) {
26+
// Assign technician's id to tech_id attribute of the Post model
2427
$post->tech_id = $techID;
2528
} else {
2629
// Set flash message and redirect if not logged in
2730
Application::$app->session->setFlash('error', 'You must be logged in to create a post.');
2831
Application::$app->response->redirect('/technician-login');
2932
return;
3033
}
31-
34+
// Checks if the HTTP request is a POST request
3235
if ($request->isPost()) {
36+
// Retrieves the form data and fills the Post model with incoming data for validation and saving
3337
$post->loadData($request->getBody());
34-
35-
if ($post->PostValidate() && $post->save()) {
38+
// Validates the data and saves the data on the database
39+
if ($post->postValidate() && $post->save()) {
3640
Application::$app->session->setFlash('success', 'Post uploaded successfully!');
3741
Application::$app->response->redirect('/technician-community');
3842
return;
3943
}
4044
}
41-
45+
// Renders the view and passes the Post model instance to the view
4246
return $this->render('/technician/technician-create-post', [
4347
'model' => $post
4448
]);
@@ -47,11 +51,14 @@ public function create(Request $request)
4751
/* Retrieve method of a post */
4852
public function index()
4953
{
50-
54+
// Fetch all the posts with likes along with the person's data
5155
$posts = (new Post)->getAllPostsWithLikes(Application::$app->customer->cus_id);
56+
// The reference operator(&) modifies the $post array directly during the loop
5257
foreach ($posts as &$post) {
58+
// Get all the comments relavent to the post
5359
$post['comments'] = (new Comment)->getAllComments($post['post_id']);
5460
}
61+
// Renders the view
5562
$this->setLayout('auth');
5663
return $this->render('/technician/technician-community', [
5764
'posts' => $posts
@@ -63,8 +70,9 @@ public function edit(Request $request)
6370
{
6471
// Fetch the post ID from the request and find the post
6572
$postID = $request->getBody()['post_id'] ?? null;
73+
// Finds the post with the corresponding post_id in order to edit it
6674
$post = (new Post)->findOne(['post_id' => $postID]);
67-
75+
// If find one method returns null, a flash message saying 'Post not found" will appear
6876
if (!$post) {
6977
Application::$app->session->setFlash('error', 'Post not found.');
7078
Application::$app->response->redirect('/technician-community');
@@ -73,28 +81,32 @@ public function edit(Request $request)
7381

7482
// Fetch the logged-in technician's ID from the session
7583
$techID = Application::$app->session->get('technician');
84+
// If the logged in technician id and the person who added the post are different
7685
if ($post->tech_id !== $techID) {
7786
Application::$app->session->setFlash('error', 'Unauthorized access.');
7887
Application::$app->response->redirect('/technician-community');
7988
return;
8089
}
81-
90+
// Check is current request is a POST request
8291
if ($request->isPost()) {
92+
// Populates the Post model with updated data from the request.
8393
$post->loadData($request->getBody());
8494

8595
// Check if a new media file is uploaded
8696
if (!empty($_FILES['media']['name'])) {
97+
// Stores the uploaded file's name in the media property of the Post model.
8798
$post->media = $_FILES['media']['name'];
8899
move_uploaded_file($_FILES['media']['tmp_name'], 'assets/uploads/' . $post->media);
89100
}
90-
91-
if ($post->PostValidate() && $post->editPost()) {
101+
// Validates the requirements before editing the post
102+
if ($post->postValidate() && $post->editPost()) {
103+
// Sets a success flash message and redirects the user to the technician community page
92104
Application::$app->session->setFlash('success', 'Post updated successfully!');
93105
Application::$app->response->redirect('/technician-community');
94106
return;
95107
}
96108
}
97-
109+
// Passes the current post data to the view for display in the form
98110
return $this->render('/technician/technician-edit-post', [
99111
'post' => $post
100112
]);
@@ -105,60 +117,73 @@ public function delete(Request $request)
105117
{
106118
// Fetch the post ID from the request
107119
$postID = $request->getBody()['post_id'] ?? null;
120+
// Retrieves the logged-in technician’s ID from the session
108121
$techID = Application::$app->session->get('technician');
109122

110123
if (!$postID || !$techID) {
111124
Application::$app->session->setFlash('error', 'Invalid request.');
112125
Application::$app->response->redirect('/technician-community');
113126
return;
114127
}
115-
128+
// Retrieves a post with the given post_id
116129
$post = (new Post)->findOne(['post_id' => $postID]);
117130

118131
if (!$post) {
119132
Application::$app->session->setFlash('error', 'Post not found.');
120133
Application::$app->response->redirect('/technician-community');
121134
return;
122135
}
123-
136+
// No access if logged tech_id is not equal to the tech_id of the post
124137
if ($post->tech_id !== $techID) {
125138
Application::$app->session->setFlash('error', 'Unauthorized access.');
126139
Application::$app->response->redirect('/technician-community');
127140
return;
128141
}
129-
142+
// Deletes the post from the database and returns true if successful, false otherwise
130143
if ((new Post)->deletePost($postID, $techID)) {
131144
Application::$app->session->setFlash('success', 'Post deleted successfully!');
132145
} else {
133146
Application::$app->session->setFlash('error', 'Failed to delete the post.');
134147
}
135-
148+
// Redirects the user to the technician community page
136149
Application::$app->response->redirect('/technician-community');
137150
}
138151

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

163+
// Create an instance of the Like model to allow interaction with the database for handling likes
145164
$likeModel = new Like();
165+
// Calls the toggleLike method of the Like model, passing the $postId and $customerId as arguments
146166
$success = $likeModel->toggleLike($postId, $customerId);
167+
// Get the updated like count
147168
$likeCount = Like::getLikeCountByPostId($postId);
148-
169+
// Prepares and sends a JSON response to the client
149170
return $response->json(['success' => $success, 'like_count' => $likeCount]);
150171
}
151172

152173

153174
public function unlike(Request $request, Response $response)
154175
{
176+
// This post_id identifies the post that the customer wants to "unlike"
155177
$postId = $request->getBody()['post_id'];
178+
// Fetches the logged-in customer’s ID
156179
$customerId = Application::$app->customer->cus_id;
157-
180+
// This instance of the model is used to handle the logic for unliking a post
158181
$likeModel = new Like();
182+
// Calls the unlikePost method of the Like model, passing the postId (the ID of the post being unliked) and customerId
159183
$success = $likeModel->unlikePost($postId, $customerId);
184+
// Update like count after the unlike action
160185
$likeCount = Like::getLikeCountByPostId($postId);
161-
186+
// Informs the client about whether the "unlike" action was successful and provides the updated like count for the post.
162187
return $response->json(['success' => $success, 'like_count' => $likeCount]);
163188
}
164189

core/Application.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ public function __construct($rootPath, array $config)
5555
$this->customer = null;
5656
}
5757

58-
$primaryValueSeviceCentre = $this->session->get('service_center');
59-
if ($primaryValueSeviceCentre) {
58+
$primaryValueServiceCentre = $this->session->get('service_center');
59+
if ($primaryValueServiceCentre) {
6060
$serviceCenterInstance = new $this->serviceCenterClass;
6161
$primaryKey = $serviceCenterInstance->primaryKey();
62-
$this->serviceCenter = $serviceCenterInstance->findOne([$primaryKey => $primaryValueSeviceCentre]);
62+
$this->serviceCenter = $serviceCenterInstance->findOne([$primaryKey => $primaryValueServiceCentre]);
6363
} else {
6464
$this->serviceCenter = null;
6565
}
@@ -130,4 +130,5 @@ public function logoutServiceCenter()
130130
$this->serviceCenter = null;
131131
$this->session->remove('service_center');
132132
}
133+
133134
}

core/Model.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function validate()
6767
return empty($this->errors);
6868
}
6969

70-
public function PostValidate()
70+
public function postValidate()
7171
{
7272
foreach ($this->postRules() as $attribute => $rules) {
7373
$value = $this->{$attribute};

core/Request.php

+10
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,27 @@ public function isPost()
3232

3333
public function getBody()
3434
{
35+
// Empty associative array
3536
$body = [];
37+
// Checks if the request method is GET
3638
if ($this->method() === 'get') {
39+
// $_GET: A PHP superglobal that holds query parameters from the URL (e.g., ?key=value).
40+
// Iterates through each key-value pair in the $_GET array
3741
foreach ($_GET as $key => $value) {
42+
// Sanitizes the value associated with $key from $_GET
3843
$body[$key] = filter_input(INPUT_GET, $key, FILTER_SANITIZE_SPECIAL_CHARS);
3944
}
4045
}
46+
// Checks if the request method is POST
4147
if ($this->method() === 'post') {
48+
// $_POST: A PHP superglobal that holds form data submitted via HTTP POST.
49+
// Iterates through each key-value pair in the $_POST array
4250
foreach ($_POST as $key => $value) {
51+
// Sanitizes the values and adds to the $body array
4352
$body[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS);
4453
}
4554
}
55+
// Returns the sanitized data
4656
return $body;
4757
}
4858
}

views/layouts/main.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<li><a href="#" class="nav-link px-2">About</a></li>
4343
</ul>
4444

45-
<?php if (Application::isGuestTechnician()): ?>
45+
<?php if (Application::isGuestTechnician() || Application::isGuestCustomer() || Application::isGuestServiceCenter()): ?>
4646
<div class="col-md-3 text-center">
4747
<button type="button" class="btn btn-outline-primary me-2"><a class="text-decoration-none"
4848
href="/select-user-login">Login</a>

views/service-centre/components/header.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
use app\core\Application;
23
?>
34

45

@@ -16,7 +17,14 @@
1617
</label>
1718
</div>
1819

20+
<h6 class="user-name">
21+
<?php
22+
$username = strtoupper(Application::$app->service_center->{'name'});
23+
echo $username;
24+
?>
25+
</h6>
26+
1927
<div class="user">
20-
<img src="/assets/technician-dashboard/customer02.jpg" alt="">
28+
<img src="/assets/select-user-service-centre.png" alt="">
2129
</div>
2230
</div>

0 commit comments

Comments
 (0)