-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathposts.php
191 lines (146 loc) · 8 KB
/
posts.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
// Start the session
session_start();
$userLoggedIn = '';
$userLoggedInViewContents = '';
require './classes/dbConnect.php'; // DbConnect
require './classes/post.queryDb.php'; // PostDbConnector
require './classes/post.validator.php'; // PostValidator
require './classes/userSession.validator.php'; // UserSession
require './classes/user.dbQuery.php'; // UserDbQuer
if (isset($_POST['savePostData']))
{
$validatePostData = new PostValidator();
$validatePostData->setTitle($_POST['title']);
$validatePostData->setCategory($_POST['category']);
$validatePostData->setDescription($_POST['description']);
$validatePostData->setFileName($_FILES['photo']['name']);
$validatePostData->setFileSize($_FILES['photo']['size']);
$validatePostData->setFileError($_FILES['photo']['error']);
$errors = $validatePostData->validatePostData();
if (!$errors)
{
$dbQuery = new PostQueryDb();
$dbQuery->setTitle($_POST['title']);
$dbQuery->setCategory($_POST['category']);
$dbQuery->setDescription($_POST['description']);
$savedPostData = $dbQuery->insertPost();
}
}
if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true)
{
// User is logged in
$userLoggedIn = true;
$user = new UserDbQuery();
$userData = $user->fetchOne();
if (isset($_POST['logOutUser']))
{
// User is logged in
$userStatus = new UserSessionValidator();
$logOutUser = $userStatus->logOutUser();
}
}
else
{
// User is not logged in
$userLoggedIn = false;
$userLoggedInViewContents = true; // Do not display post contents to a user who is not logged in
}
?>
<body>
<div class="mainContainer"> <!-- contains all the page contents -->
<?php include $userLoggedIn ? './headers/postsHeader.php' : './headers/notLoggedInPostsHeader.php'; ?>
<section class="hero border-bottom">
<section class="caption">
<?php if ($userLoggedInViewContents):?> <!-- Do not display post contents to a user who is not logged in -->
<div class="notLoggedIndashboardView">
<div class="d-flex justify-content-evenly">
<a href="./signup.php" class="actionBtn rounded-1">Sign up</a>
<a href="./login.php" class="actionBtn rounded-1">Login</a>
</div>
</div>
<?php else: ?>
<div class="dashboardView d-flex justify-content-center align-items-center"> <!--- Signed in Dashboard -->
<div class="userProfile d-flex flex-column align-items-center">
<div class="userProfileAvatar d-flex justify-content-between align-items-center">
<img src="./assets/images/moji.png" alt="user avatar">
<p class="likeCount p-1 pt-2 px-2"><i class="bi bi-heart-fill"></i> 1.4M</p>
</div>
<p class="mt-2">Mojisola Badmus</p>
<div class="userProfileInfo d-flex justify-content-between">
<p>Followers 214</p>
<i class="bi bi-dot"></i>
<p>Email: [email protected]</p>
</div>
</div>
</div>
<?php endif; ?>
</section>
</section>
<section class="blogContents pt-5">
<section class="blogContentContainer"> <!-- blog contents container starts here -->
<section class="blogPostContainer">
<?php
if ($userLoggedInViewContents):?> <!-- Do not display post contents to a user who is not logged in -->
<div class="m-auto pt-5">
<p class="m-auto mt-3">Sign up or Login to see all your posts and posts you've liked.</p>
</div>
<?php else:
$dbQuery = new PostQueryDb();
$allPostData = $dbQuery->fetchAllPosts();
$validatePostData = new PostQueryDb();
foreach ($allPostData as $postData): ?>
<div class="postCard border rounded-top rounded-3 pb-1">
<!-- blog post card starts here -->
<div class="postPhoto rounded-top">
<div class="upperInfo d-flex justify-content-between px-2">
<h5 class="postTime">
<?php
$post = new PostValidator();
$created_at = $postData['created_at'];
$time = strtotime($created_at);
$post->setTimeAgo($time);
$timeAgo = $post->getTimeAgo();
echo $timeAgo;
?>
</h5>
<div class="moreInfo ms-3"><i class="bi bi-three-dots-vertical"></i></div>
</div>
<div class="rightSideInfo d-flex flex-column align-items-end pt-2 pe-2 mb-5">
<div class="iconDiv mb-2"><i id="heart-icon" class="bi bi-heart" onclick="replaceLikeIcon(this)"></i></div>
</div>
<a href="./postDetails.php?id=<?=$postData['post_id']?>">
<img class="img-fluid rounded-top" src="<?=$postData['photo']?>"> <!-- fetches photo -->
</a>
</div>
<div class="postCategory d-flex justify-content-between py-2 px-2">
<p><?=$postData['categoryName'] ?></p> <!-- fetches categoryName -->
<p>
<?php
$totalNumWords = str_word_count($postData['description'], 0);
$wpm = 200; // where "wpm" is number of words per minute.
$readPerMinute = floor($totalNumWords / $wpm);
$readTime = $readPerMinute. ' Min read';
print_r($readTime);
?>
</p>
</div>
<div class="postTitle px-2 mt-4">
<h6><?=substr_replace($postData['title'], "...", 55)?></h6> <!-- fetches title from db -->
</div>
<div class="postParagraph px-2">
<p><?=substr_replace($postData['description'], "...", 70)?></p> <!-- fetches description -->
</div>
</div>
<?php
endforeach;
endif;
?>
</section>
</section>
<?php include './footers/dropUsAMessage.php' ?>
</section>
<?php include './footers/globalFooter.php' ?>
</div>
</body>
</html>