@@ -16,29 +16,33 @@ class PostController extends Controller
16
16
/* Create method of a post */
17
17
public function create (Request $ request )
18
18
{
19
+
19
20
$ post = new Post ();
20
21
21
- // Fetch logged-in technician's ID from session
22
+ // Fetch logged-in technician's tech_id from session
22
23
$ techID = Application::$ app ->session ->get ('technician ' );
24
+ // Check if techID exists to confirm that the technician is logged in to create a new post
23
25
if ($ techID ) {
26
+ // Assign technician's id to tech_id attribute of the Post model
24
27
$ post ->tech_id = $ techID ;
25
28
} else {
26
29
// Set flash message and redirect if not logged in
27
30
Application::$ app ->session ->setFlash ('error ' , 'You must be logged in to create a post. ' );
28
31
Application::$ app ->response ->redirect ('/technician-login ' );
29
32
return ;
30
33
}
31
-
34
+ // Checks if the HTTP request is a POST request
32
35
if ($ request ->isPost ()) {
36
+ // Retrieves the form data and fills the Post model with incoming data for validation and saving
33
37
$ 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 ()) {
36
40
Application::$ app ->session ->setFlash ('success ' , 'Post uploaded successfully! ' );
37
41
Application::$ app ->response ->redirect ('/technician-community ' );
38
42
return ;
39
43
}
40
44
}
41
-
45
+ // Renders the view and passes the Post model instance to the view
42
46
return $ this ->render ('/technician/technician-create-post ' , [
43
47
'model ' => $ post
44
48
]);
@@ -47,11 +51,14 @@ public function create(Request $request)
47
51
/* Retrieve method of a post */
48
52
public function index ()
49
53
{
50
-
54
+ // Fetch all the posts with likes along with the person's data
51
55
$ posts = (new Post )->getAllPostsWithLikes (Application::$ app ->customer ->cus_id );
56
+ // The reference operator(&) modifies the $post array directly during the loop
52
57
foreach ($ posts as &$ post ) {
58
+ // Get all the comments relavent to the post
53
59
$ post ['comments ' ] = (new Comment )->getAllComments ($ post ['post_id ' ]);
54
60
}
61
+ // Renders the view
55
62
$ this ->setLayout ('auth ' );
56
63
return $ this ->render ('/technician/technician-community ' , [
57
64
'posts ' => $ posts
@@ -63,8 +70,9 @@ public function edit(Request $request)
63
70
{
64
71
// Fetch the post ID from the request and find the post
65
72
$ postID = $ request ->getBody ()['post_id ' ] ?? null ;
73
+ // Finds the post with the corresponding post_id in order to edit it
66
74
$ post = (new Post )->findOne (['post_id ' => $ postID ]);
67
-
75
+ // If find one method returns null, a flash message saying 'Post not found" will appear
68
76
if (!$ post ) {
69
77
Application::$ app ->session ->setFlash ('error ' , 'Post not found. ' );
70
78
Application::$ app ->response ->redirect ('/technician-community ' );
@@ -73,28 +81,32 @@ public function edit(Request $request)
73
81
74
82
// Fetch the logged-in technician's ID from the session
75
83
$ techID = Application::$ app ->session ->get ('technician ' );
84
+ // If the logged in technician id and the person who added the post are different
76
85
if ($ post ->tech_id !== $ techID ) {
77
86
Application::$ app ->session ->setFlash ('error ' , 'Unauthorized access. ' );
78
87
Application::$ app ->response ->redirect ('/technician-community ' );
79
88
return ;
80
89
}
81
-
90
+ // Check is current request is a POST request
82
91
if ($ request ->isPost ()) {
92
+ // Populates the Post model with updated data from the request.
83
93
$ post ->loadData ($ request ->getBody ());
84
94
85
95
// Check if a new media file is uploaded
86
96
if (!empty ($ _FILES ['media ' ]['name ' ])) {
97
+ // Stores the uploaded file's name in the media property of the Post model.
87
98
$ post ->media = $ _FILES ['media ' ]['name ' ];
88
99
move_uploaded_file ($ _FILES ['media ' ]['tmp_name ' ], 'assets/uploads/ ' . $ post ->media );
89
100
}
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
92
104
Application::$ app ->session ->setFlash ('success ' , 'Post updated successfully! ' );
93
105
Application::$ app ->response ->redirect ('/technician-community ' );
94
106
return ;
95
107
}
96
108
}
97
-
109
+ // Passes the current post data to the view for display in the form
98
110
return $ this ->render ('/technician/technician-edit-post ' , [
99
111
'post ' => $ post
100
112
]);
@@ -105,60 +117,73 @@ public function delete(Request $request)
105
117
{
106
118
// Fetch the post ID from the request
107
119
$ postID = $ request ->getBody ()['post_id ' ] ?? null ;
120
+ // Retrieves the logged-in technician’s ID from the session
108
121
$ techID = Application::$ app ->session ->get ('technician ' );
109
122
110
123
if (!$ postID || !$ techID ) {
111
124
Application::$ app ->session ->setFlash ('error ' , 'Invalid request. ' );
112
125
Application::$ app ->response ->redirect ('/technician-community ' );
113
126
return ;
114
127
}
115
-
128
+ // Retrieves a post with the given post_id
116
129
$ post = (new Post )->findOne (['post_id ' => $ postID ]);
117
130
118
131
if (!$ post ) {
119
132
Application::$ app ->session ->setFlash ('error ' , 'Post not found. ' );
120
133
Application::$ app ->response ->redirect ('/technician-community ' );
121
134
return ;
122
135
}
123
-
136
+ // No access if logged tech_id is not equal to the tech_id of the post
124
137
if ($ post ->tech_id !== $ techID ) {
125
138
Application::$ app ->session ->setFlash ('error ' , 'Unauthorized access. ' );
126
139
Application::$ app ->response ->redirect ('/technician-community ' );
127
140
return ;
128
141
}
129
-
142
+ // Deletes the post from the database and returns true if successful, false otherwise
130
143
if ((new Post )->deletePost ($ postID , $ techID )) {
131
144
Application::$ app ->session ->setFlash ('success ' , 'Post deleted successfully! ' );
132
145
} else {
133
146
Application::$ app ->session ->setFlash ('error ' , 'Failed to delete the post. ' );
134
147
}
135
-
148
+ // Redirects the user to the technician community page
136
149
Application::$ app ->response ->redirect ('/technician-community ' );
137
150
}
138
151
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.
139
154
public function like (Request $ request , Response $ response )
140
155
{
156
+ // To construct and send the response, such as returning JSON data
141
157
$ response = new Response ();
158
+ // Identifies which post is being liked
142
159
$ postId = $ request ->getBody ()['post_id ' ];
160
+ // Identifies the customer performing the "like" action
143
161
$ customerId = Application::$ app ->customer ->cus_id ;
144
162
163
+ // Create an instance of the Like model to allow interaction with the database for handling likes
145
164
$ likeModel = new Like ();
165
+ // Calls the toggleLike method of the Like model, passing the $postId and $customerId as arguments
146
166
$ success = $ likeModel ->toggleLike ($ postId , $ customerId );
167
+ // Get the updated like count
147
168
$ likeCount = Like::getLikeCountByPostId ($ postId );
148
-
169
+ // Prepares and sends a JSON response to the client
149
170
return $ response ->json (['success ' => $ success , 'like_count ' => $ likeCount ]);
150
171
}
151
172
152
173
153
174
public function unlike (Request $ request , Response $ response )
154
175
{
176
+ // This post_id identifies the post that the customer wants to "unlike"
155
177
$ postId = $ request ->getBody ()['post_id ' ];
178
+ // Fetches the logged-in customer’s ID
156
179
$ customerId = Application::$ app ->customer ->cus_id ;
157
-
180
+ // This instance of the model is used to handle the logic for unliking a post
158
181
$ likeModel = new Like ();
182
+ // Calls the unlikePost method of the Like model, passing the postId (the ID of the post being unliked) and customerId
159
183
$ success = $ likeModel ->unlikePost ($ postId , $ customerId );
184
+ // Update like count after the unlike action
160
185
$ 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.
162
187
return $ response ->json (['success ' => $ success , 'like_count ' => $ likeCount ]);
163
188
}
164
189
0 commit comments