-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.php
149 lines (125 loc) · 4.19 KB
/
search.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
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Google\Cloud\Firestore\FirestoreClient;
putenv("FIRESTORE_EMULATOR_HOST=localhost:8080");
$firestore = new FirestoreClient([
'projectId' => 'finals-activity',
]);
if (isset($_GET['search'])) {
$searchTerm = $_GET['search'];
} else {
$searchTerm = '';
}
$searchTerm = trim($searchTerm);
$pattern = '/' . preg_quote($searchTerm, '/') . '/i';
$postsRef = $firestore->collection('posts');
$query = $postsRef->orderBy('content');
$posts = $query->documents();
$filteredPosts = [];
foreach ($posts as $post) {
$content = $post->get('content');
$userId = $post->get('user_id');
$userDoc = $firestore->collection('users')->document($userId)->snapshot();
$username = $userDoc->exists() ? $userDoc->get('username') : '';
$profilePicture = $userDoc->exists() ? $userDoc->get('profile_picture') : '';
if (preg_match($pattern, $content) || preg_match($pattern, $username)) {
$filteredPosts[] = [
'post' => $post,
'username' => $username,
'profilePicture' => $profilePicture
];
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>KKT Search</title>
<?php include 'assets\head.php' ?>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.0.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.0.1/firebase-firestore.js"></script>
<?php include 'css\index-css.php' ?>
</head>
<body>
<?php include 'assets\header.php' ?>
<?php
echo "<div id='particles-js'></div>";
if (empty($filteredPosts)) {
echo "<div class='no-posts'>";
echo "No posts found matching the search term: $searchTerm";
echo "</div>";
} else {
echo "<div class='cards'>";
echo "<div class='searchTerm'>";
echo "Search results for: $searchTerm";
echo "</div>";
foreach ($filteredPosts as $filteredPost) {
$post = $filteredPost['post'];
$username = $filteredPost['username'];
$profilePicture = $filteredPost['profilePicture'];
$postId = $post->id();
$content = $post->get('content');
$createdAt = $post->get('created_at')->get()->format('g:i A');
$likeCount = $post->get('likes');
$commentsRef = $firestore->collection('post_comments')->document($postId);
$commentsData = $commentsRef->snapshot()->data();
$commentCount = 0;
if (isset($commentsData['comments'])) {
$commentCount = count($commentsData['comments']);
}
echo "<div class='post-card'>";
echo '
<div class="profile">
<img class="profile_picture" src="' . $profilePicture . '" alt="Profile Picture">
<div class="name_time">
<h3>
' . $username . '
<span class="time">
' . $createdAt . '
</span>
</h3>
</div>
</div>';
echo "<p>";
echo $content;
echo "</p>";
echo "<div class='meta'>";
echo "<span class='likes'>Likes: ";
echo $likeCount;
echo "</span>";
echo "<span>Comments: ";
echo $commentCount;
echo "</span>";
echo "</div>";
echo "<div class='actions'>";
echo "<a href='commentsec.php?postId=$postId'><i class='ri-discuss-line'></i></a>";
echo "<form id='likeForm' method='POST' action='likepost.php'>";
echo "<input type='hidden' name='postId' value='$postId'>";
echo "<button type='submit' class='like'><i class='ri-thumb-up-line'></i></button>";
echo "</form>";
echo "</div>";
echo "</div>";
}
echo "</div>";
}
?>
<script src="functions/index.js"></script>
<script src="assets\particles.js"></script>
<script src="assets\app.js"></script>
</body>
</html>
<style>
.searchTerm {
font-size: 30px;
margin: 20px;
}
.time {
font-size: 12px;
color: #888;
margin-left: 10px;
}
.likes {
margin-right: 10px;
}
</style>