Skip to content

Commit ed5757e

Browse files
Make it clean
Delete include files and convert them into a class
1 parent f2f850a commit ed5757e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2181
-847
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
44

55
# User-specific stuff
6+
.idea/*
67
.idea/**/workspace.xml
78
.idea/**/tasks.xml
89
.idea/**/usage.statistics.xml

.idea/php-blog.iml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

admin/class/Posts.php

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
require_once __DIR__ . "/../../class/Database.php";
3+
4+
/**
5+
* Class Posts
6+
*/
7+
class Posts
8+
{
9+
10+
public int $postId;
11+
public string $postTitle;
12+
public string $postBody;
13+
public string $isPublished;
14+
private object $db;
15+
private int $userId;
16+
17+
/**
18+
* Posts constructor.
19+
*/
20+
public function __construct()
21+
{
22+
session_start();
23+
$this->userId = intval($_SESSION["id"]);
24+
$this->db = new Database();
25+
}
26+
27+
/**
28+
* Create new post in database
29+
* @param string $title
30+
* @param string $body
31+
* @param string $isPublished
32+
*/
33+
public function addPost(string $title, string $body, string $isPublished): void
34+
{
35+
// Prepare the sql statement
36+
$this->db->query("INSERT INTO `posts` (`user_id`, `title`, `body`, `published`) VALUES (:userId, :postTitle, :postBody, :postPublish)");
37+
$this->db->bind(":userId", $this->userId, PDO::PARAM_INT);
38+
$this->db->bind(":postTitle", $title, PDO::PARAM_STR);
39+
$this->db->bind(":postBody", $body, PDO::PARAM_STR);
40+
$this->db->bind("postPublish", $isPublished, PDO::PARAM_STR_CHAR);
41+
42+
// Execute the statement
43+
if ($this->db->execute()) {
44+
header("location: /admin/posts.php?newPostStatus=1");
45+
die();
46+
} else {
47+
header("location: /admin/posts.php?newPostStatus=2");
48+
die();
49+
}
50+
}
51+
52+
/**
53+
* Delete the post in database
54+
* @param int $id
55+
*/
56+
public function deletePost(int $id): void
57+
{
58+
$this->db->query("DELETE FROM `posts` WHERE `id`=:postId");
59+
$this->db->bind(":postId", $id, PDO::PARAM_INT);
60+
61+
if ($this->db->execute()) {
62+
header("location: /admin/posts.php?deletePostStatus=1");
63+
die();
64+
} else {
65+
header("location: /admin/posts.php?deletePostStatus=2");
66+
die();
67+
}
68+
}
69+
70+
/**
71+
* Get post
72+
* @param int $id
73+
*/
74+
public function getPost(int $id): void
75+
{
76+
$this->db->query("SELECT `title`, `body`, `published` FROM `posts` WHERE `id`=:id");
77+
$this->db->bind(":id", $id, PDO::PARAM_INT);
78+
$this->db->execute();
79+
$result = $this->db->fetch();
80+
$this->postId = $id;
81+
$this->postTitle = $result->title;
82+
$this->postBody = $result->body;
83+
$this->isPublished = $result->published;
84+
}
85+
86+
/**
87+
* Update the post
88+
* @param string $title
89+
* @param string $body
90+
* @param string $isPublished
91+
* @param int $postId
92+
*/
93+
public function updatePost(string $title, string $body, string $isPublished, int $postId): void
94+
{
95+
$this->db->query("UPDATE `posts` SET `title` = :title, `body` = :body, `published` = :published WHERE `id` = :id");
96+
$this->db->bind(":title", $title, PDO::PARAM_STR);
97+
$this->db->bind(":body", $body, PDO::PARAM_STR);
98+
$this->db->bind(":published", $isPublished, PDO::PARAM_STR);
99+
$this->db->bind(":id", $postId, PDO::PARAM_INT);
100+
101+
if ($this->db->execute()) {
102+
header("location: /admin/posts.php?updatePostStatus=1");
103+
die();
104+
} else {
105+
header("location: /admin/posts.php?updatePostStatus=2");
106+
die();
107+
}
108+
}
109+
110+
/**
111+
* Print messages received from request
112+
* @param string $type
113+
* @param int $errorCode
114+
*/
115+
public function printMessages(string $type, int $errorCode): void
116+
{
117+
$errorMessage = "";
118+
if ($type == "newPost") {
119+
switch ($errorCode) {
120+
case 1:
121+
$errorMessage = "Post Created Successfully";
122+
break;
123+
case 2:
124+
$errorMessage = "Something goes wrong";
125+
break;
126+
}
127+
} elseif ($type == "deletePost") {
128+
switch ($errorCode) {
129+
case 1:
130+
$errorMessage = "Post Deleted Successfully";
131+
break;
132+
case 2:
133+
$errorMessage = "Something goes wrong";
134+
break;
135+
}
136+
} elseif ($type == "updatePost") {
137+
switch ($errorCode) {
138+
case 1:
139+
$errorMessage = "Post Updated Successfully";
140+
break;
141+
case 2:
142+
$errorMessage = "Something goes wrong";
143+
break;
144+
}
145+
}
146+
?>
147+
<div class="pt-3 pb-3 text-center text-white bg-<?php echo ($errorCode == 1) ? "success" : "danger" ?> w-100 h-auto">
148+
<b><?php echo $errorMessage; ?></b>
149+
</div>
150+
<?php
151+
}
152+
}

admin/class/Settings.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
require_once __DIR__ . "/../../class/Database.php";
3+
4+
/**
5+
* Class Settings
6+
*/
7+
class Settings
8+
{
9+
public string $blogTitle;
10+
public string $blogAuthor;
11+
public string $blogAuthorInfo;
12+
13+
private object $db;
14+
15+
/**
16+
* Settings constructor.
17+
*/
18+
public function __construct()
19+
{
20+
// Create database connection
21+
$this->db = new Database();
22+
// Get blog settings from database
23+
$this->db->query("SELECT `blogTitle`,`blogAuthor`,`blogAuthorInfo` FROM `settings`");
24+
$this->db->execute();
25+
$result = $this->db->fetch();
26+
// Assign the values
27+
$this->blogTitle = $result->blogTitle;
28+
$this->blogAuthor = $result->blogAuthor;
29+
$this->blogAuthorInfo = $result->blogAuthorInfo;
30+
}
31+
32+
/**
33+
* Update blog settings
34+
* @param string $blogTitle
35+
* @param string $blogAuthor
36+
* @param string $blogAuthorInfo
37+
*/
38+
public function updateSettings(string $blogTitle, string $blogAuthor, string $blogAuthorInfo): void
39+
{
40+
// Send the query
41+
$this->db->query("UPDATE `settings` SET `blogTitle` = :blogTitle, `blogAuthor` = :blogAuthor, `blogAuthorInfo` = :blogAuthorInfo");
42+
$this->db->bind(":blogTitle", $blogTitle, PDO::PARAM_STR);
43+
$this->db->bind(":blogAuthor", $blogAuthor, PDO::PARAM_STR);
44+
$this->db->bind(":blogAuthorInfo", $blogAuthorInfo, PDO::PARAM_STR);
45+
// Execute the statement
46+
if ($this->db->execute()) {
47+
header("location: /admin/settings.php?updateStatus=1");
48+
die();
49+
} else {
50+
header("location: /admin/settings.php?updateStatus=2");
51+
die();
52+
}
53+
54+
}
55+
}

admin/include/permission.php

-5
This file was deleted.

admin/include/posts/addPost.php

-26
This file was deleted.

admin/include/posts/deletePost.php

-18
This file was deleted.

admin/include/posts/editPost.php

-45
This file was deleted.

admin/include/settings/getSettings.php

-20
This file was deleted.

admin/include/settings/updateSettings.php

-22
This file was deleted.

admin/include/totalCalculator.php

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<?php
2-
require_once(__DIR__ . "/../../include/config.php");
2+
require_once __DIR__ . "/../../class/Database.php";
3+
// Create database connection
4+
$db = new Database();
35
// Send request to database
4-
$posts = $conn->prepare("SELECT `id` FROM `posts`");
5-
$posts->execute();
6-
$users = $conn->prepare("SELECT `id` FROM `users`");
7-
$users->execute();
6+
$db->query("SELECT `id` FROM `posts`");
7+
$db->execute();
8+
$totalPosts = $db->rowCount();
89

9-
// Define the counter variables
10-
$totalUsers = $users->rowCount();
11-
$totalPosts = $posts->rowCount();
10+
$db->query("SELECT `id` FROM `users`");
11+
$db->execute();
12+
$totalUsers = $db->rowCount();

0 commit comments

Comments
 (0)