Skip to content

Commit 4d9f9d0

Browse files
authored
Merge pull request #31 from kasunmendis7/feature/admin-sign-up
Feature/admin sign up
2 parents 800691f + fe5db6d commit 4d9f9d0

16 files changed

+1000
-25
lines changed

.env

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

controllers/AuthController.php

+23
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use app\core\Controller;
77
use app\core\Request;
88
use app\core\Response;
9+
use app\models\Admin;
910
use app\models\Customer;
1011
use app\models\CustomerLoginForm;
1112

@@ -163,4 +164,26 @@ public function serviceCenterLogout(Request $request, Response $response)
163164
$response->redirect('/service-centre-landing');
164165
}
165166

167+
/* admin sign up method */
168+
public function adminSignUp(Request $request)
169+
{
170+
$admin = new Admin();
171+
if ($request->isPost()) {
172+
173+
$admin->loadData($request->getBody());
174+
if ($admin->validate() && $admin->save()) {
175+
Application::$app->session->setFlash('success', 'You have been registered successfully!');
176+
Application::$app->response->redirect('/admin-login');
177+
}
178+
$this->setLayout('auth');
179+
return $this->render('/admin/admin-sign-up', [
180+
'model' => $admin
181+
]);
182+
}
183+
$this->setLayout('auth');
184+
return $this->render('/admin/admin-sign-up', [
185+
'model' => $admin
186+
]);
187+
}
188+
166189
}

controllers/CommentController.php

+16-8
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,22 @@ class CommentController extends Controller
1212
// Create a new comment
1313
public function create(Request $request)
1414
{
15+
// Checks if the HTTP request method is POST
1516
if ($request->isPost()) {
17+
// Create a new instance of the Comment model, used to store and handle data of the new comment
1618
$comment = new Comment();
19+
// The loadData method populates the Comment instance with the data from the POST request
1720
$comment->loadData($request->getBody());
1821

1922
// Set the logged-in user's ID as the comment owner
2023
$comment->cus_id = Application::$app->customer->cus_id;
21-
24+
// Validate the data before saving it
2225
if ($comment->validate() && $comment->save()) {
2326
Application::$app->session->setFlash('success', 'Comment posted successfully');
2427
} else {
2528
Application::$app->session->setFlash('error', 'Failed to post comment');
2629
}
27-
30+
// Redirects the user to the fixmecommunity page after attempting to create the comment
2831
Application::$app->response->redirect('/fixme-community');
2932

3033
}
@@ -35,7 +38,9 @@ public function create(Request $request)
3538
// Edit an existing comment
3639
public function edit(Request $request)
3740
{
41+
// Fetch the comment ID from the request and find the comment
3842
$comment_id = $request->getBody()['comment_id'];
43+
// Retrives the comment from the database
3944
$comment = (new Comment)->findOne(['comment_id' => $comment_id]);
4045

4146
// Ensure the user is the owner of the comment
@@ -44,10 +49,11 @@ public function edit(Request $request)
4449
$this->response->redirect("/fixme-community");
4550
return;
4651
}
47-
52+
// Checks if the HTTP request is a POST request
4853
if ($request->isPost()) {
54+
// Updates the Comment object with the new values provided by the user.
4955
$comment->loadData($request->getBody());
50-
56+
// Validate the updated data before saving it
5157
if ($comment->validate() && $comment->update()) {
5258
Application::$app->session->setFlash('success', 'Comment updated successfully');
5359
} else {
@@ -68,17 +74,19 @@ public function delete(Request $request)
6874
{
6975
// Fetch the comment ID from the request
7076
$commentID = $request->getBody()['comment_id'] ?? null;
77+
// Get the logged-in customer's ID
7178
$cusID = Application::$app->customer->cus_id; // Get the logged-in customer ID
72-
79+
// Check if the comment ID or customer ID is not set (invalid request)
7380
if (!$commentID || !$cusID) {
7481
Application::$app->session->setFlash('error', 'Invalid request.');
7582
Application::$app->response->redirect('/fixme-community');
83+
// Stop the execution of the method
7684
return;
7785
}
7886

79-
// Find the comment by ID
87+
// Find the comment using its ID
8088
$comment = (new Comment)->findOne(['comment_id' => $commentID]);
81-
89+
// Check if the comment does not exist
8290
if (!$comment) {
8391
Application::$app->session->setFlash('error', 'Comment not found.');
8492
Application::$app->response->redirect('/fixme-community');
@@ -98,7 +106,7 @@ public function delete(Request $request)
98106
} else {
99107
Application::$app->session->setFlash('error', 'Failed to delete the comment.');
100108
}
101-
109+
// Redirect the user to the '/fixmecommunity' page after the operation
102110
Application::$app->response->redirect('/fixme-community');
103111
}
104112

controllers/ServiceCentreController.php

+6
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,10 @@ public function marketPlaceHome()
9292
return $this->render('service-centre/market-place/market-place-home');
9393
}
9494

95+
public function serviceCenterMessages()
96+
{
97+
$this->setLayout('auth');
98+
return $this->render('/service-centre/service-center-messages');
99+
}
100+
95101
}

models/Admin.php

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace app\models;
4+
5+
use app\core\Application;
6+
use app\core\DbModel;
7+
8+
class Admin extends DbModel
9+
{
10+
11+
public string $fname = '';
12+
public string $lname = '';
13+
public string $email = '';
14+
public string $phone_no = '';
15+
public string $address = '';
16+
public string $password = '';
17+
public string $confirmPassword = '';
18+
19+
public function tableName(): string
20+
{
21+
return 'admin';
22+
}
23+
24+
public function primaryKey(): string
25+
{
26+
return 'admin_id';
27+
}
28+
29+
public function save()
30+
{
31+
$this->password = password_hash($this->password, PASSWORD_DEFAULT);
32+
return parent::save();
33+
}
34+
35+
public function updateAdmin()
36+
{
37+
$sql = "UPDATE admin SET fname = :fname, lname = :lname, phone_no = :phone_no, address = :address WHERE admin_id = :admin_id";
38+
$stmt = self::prepare($sql);
39+
$stmt->bindValue(':fname', $this->fname);
40+
$stmt->bindValue(':lname', $this->lname);
41+
$stmt->bindValue(':phone_no', $this->phone_no);
42+
$stmt->bindValue(':address', $this->address);
43+
$stmt->bindValue(':admin_id', Application::$app->admin->{'admin_id'});
44+
return $stmt->execute();
45+
}
46+
47+
public function rules(): array
48+
{
49+
return [
50+
'fname' => [self::RULE_REQUIRED],
51+
'lname' => [self::RULE_REQUIRED],
52+
'email' => [self::RULE_REQUIRED, self::RULE_EMAIL, [
53+
self::RULE_UNIQUE,
54+
'class' => self::class
55+
]],
56+
'phone_no' => [self::RULE_REQUIRED, [self::RULE_MIN, 'min' => 10], [self::RULE_MAX, 'max' => 10]],
57+
'address' => [self::RULE_REQUIRED],
58+
'password' => [self::RULE_REQUIRED, [self::RULE_MIN, 'min' => 8]],
59+
'confirmPassword' => [self::RULE_REQUIRED, [self::RULE_MATCH, 'match' => 'password']],
60+
];
61+
}
62+
63+
public function updateRules(): array
64+
{
65+
return [
66+
'fname' => [self::RULE_REQUIRED],
67+
'lname' => [self::RULE_REQUIRED],
68+
'phone_no' => [self::RULE_REQUIRED, [self::RULE_MIN, 'min' => 10], [self::RULE_MAX, 'max' => 10]],
69+
'address' => [self::RULE_REQUIRED],
70+
];
71+
}
72+
73+
public function attributes(): array
74+
{
75+
return [
76+
'fname',
77+
'lname',
78+
'email',
79+
'phone_no',
80+
'address',
81+
'password',
82+
];
83+
}
84+
}

models/Post.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function save()
4747
// return $statement->fetchAll(\PDO::FETCH_ASSOC);
4848
// }
4949

50-
public static function getAllPostsWithLikes(int $userId)
50+
public static function getAllPostsWithLikes(?int $userId)
5151
{
5252
$sql = "SELECT p.*, t.fname, t.lname, t.profile_picture,
5353
(SELECT COUNT(*) FROM post_like WHERE post_id = p.post_id) AS like_count,

public/assets/products/OIP.jpeg

24.8 KB
Loading

public/assets/uploads/HomeImage3.webp

35.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)