-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.php
213 lines (174 loc) · 5.47 KB
/
index.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
include("lib/functions.php");
session_start();
/*BUSINESS LOGIC CODE*/
/*if($_SESSION[authenticated]= true){
?>
<script>var x = document.getElementById("signIn");
x.style.display = "none";
</script>
<?php
}else{
?>
<script>var x = document.getElementById("welcome");
x.style.display = "none";
</script>
<?php
}*/
/*BUSINESS LOGIC CODE END*/
?><!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D-Board</title>
<meta name="description" content="The D-Board project">
<!-- CSS INCLUSION -->
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/index.css">
<link rel="stylesheet" href="css/allert.css">
</head>
<body>
<!-- ADD HERE YOUR HTML CODE -->
<?php
// Following lines are to force autentication for testing
// $_SESSION[authenticated] = true;
// $_SESSION[username] = 'Stefano';
if($_SESSION[authenticated] == true){
?>
<div id="welcome">
<h3 id="h3Welcome">Welcome <?php echo ($_SESSION[username]);?> </h3>
</div>
<?php
$pageNumber = $_POST['pageNumberBtn'];
$allertText = null;
$allertType = null;
/*Creation of New Topics*/
$title = $_POST['topicTitle'];
$userId = $_SESSION[userId];
/* $userID to be changed with the ID of the current logged user */
if ($title != "") {
$checktopic = saveNewTopic($title, $userId);
if ($checktopic == "-1") {
$allertText="ERROR";
$allertType="error";
}
else {$allertText="Topic created";
$allertType="success";
}
}
?>
<div class="header">
<h1>Discussions</h1>
</div>
<form method="post" action="index.php" id="topic_creation" class="content">
<?php /* include('errors.php'); */ ?>
<div class="input-group">
<formTitle>Create a discussion</formTitle>
<label>Title</label>
<input type="text" name="topicTitle" id="topicTitle" required>
<br><button type="submit" id="formNewTopicButton" class="formNewTopicBtnDisabled" name="new_topic">Add Discussion</button>
</div>
</form>
</div>
<!-- Table with paginated topics -->
<table>
<?php
/* Getting the amount of pages to display */
if ($pageNumber == 0 or Null){
$pageNumber = 1;
}
$arrayTopicsPaginated = getTopicsPaginated(10,$pageNumber);
$numberOfPages = $arrayTopicsPaginated['totalpages'];
/* echo to check total number of pages is retrieved correctly */
/* echo('<h2>num pages prima: '.$numberOfPages.'</h2>'); */
foreach ($arrayTopicsPaginated['data'] as $item) {
$idFromArray = $item['userId'];
$arrayUserFromId = getUserById($idFromArray);
$topicDate = $item['created_at'];
echo '<tr>
<td><a href=./topic.php?topicID='.$item['topicId'].'&page=first>'.$item['title'].'</a></br>Created by: '.$arrayUserFromId['username'].'<span style="margin-right: 100px"></span>Created at: '.(new \DateTime($topicDate))->format('l, F d, Y').'</td>
</tr>';
}
?>
</table>
<!-- Create clickable links to pages -->
<form method="post" id="pager" class="numeriDiPagina">
<?php
if ($pageNumber == 1) {
echo '<button class="pageNavBtnDisabled" disabled><</button>';
}
else{
$prevPage = $pageNumber - 1;
echo '<button class="pageNavBtnEnabled" name="pageNumberBtn" value="'.$prevPage.'"><</button>';
}
for ($i = 1; $i <= $numberOfPages; $i++) {
if ($i == $pageNumber) {
echo '<button class="pageBtn" disabled>'.$i.'</button>';
}
else{
echo '<button type="submit" class="pageBtnNo" name="pageNumberBtn" value="'.$i.'">'.$i.'</button>';
}
}
if ($pageNumber == $numberOfPages) {
echo '<button class="pageNavBtnDisabled" disabled>></button>';
}
else{
$nextPage = $pageNumber + 1;
echo '<button class="pageNavBtnEnabled" name="pageNumberBtn" value="'.$nextPage.'">></button>';
}
?>
</form>
<?php
}
else{
?>
<center>
<div class="loginContainer"><p>Welcome to Coding Certification Academy</p>
<a href="signin.php" class="btnLogin"> SIGN-IN </a>
<a href="register.php" class="btnRegister">REGISTER</a>
</div>
</center>
<?php
}
?>
<!-- JS SCRIPT INCLUSION -->
<script
src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
crossorigin="anonymous"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment-with-locales.min.js"
crossorigin="anonymous"></script>
<script src="js/script.js"></script>
<script src="js/index.js"></script>
<script type="text/javascript">
// jquery to change button class in the form to create new topics
$(document).ready(function(){
$('#formNewTopicButton').prop("disabled", "true");
$('#formNewTopicButton').attr("class", "formNewTopicBtnDisabled");
$('#topicTitle').keyup(function(){
if($(this).val().length !=0){
$('#formNewTopicButton').removeAttr("disabled");
$('#formNewTopicButton').attr("class", "formNewTopicBtnEnabled");
}
else
{
$('#formNewTopicButton').prop("disabled", "true");
$('#formNewTopicButton').attr("class", "formNewTopicBtnDisabled");
}
})
});
</script>
<?php
if ($allertText!==null && $allertType !==null){
?>
<script>
allert("<?= $allertText ?>", {
type: "<?= $allertType ?>",
});
</script>
<?php
}
?>
</body>
</html>