-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_quacks.php
118 lines (99 loc) · 3.72 KB
/
my_quacks.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
<?php
require_once 'db.class.php';
require_once 'common_functions.php';
session_start();
initial_commands();
$database = DB::getConnection();
if (isset($_POST['post-button'])) {
if (empty($_POST['new-quack']))
$error_message = 'Quack cannot be empty.';
else if (strlen($_POST['new-quack']) <= 140)
handleNewQuack();
else
$error_message = 'Unexcpected error occurred while posting a new quack.';
} else {
$error_message = isset($_SESSION['error_message']) ? $_SESSION['error_message'] : '';
unset($_SESSION['error_message']);
}
$user_quacks_statement = getUserQuacks();
// Dohvaca korisnikove quackove koje je dosad objavio
function getUserQuacks() {
$id_user = getUserIdByUsername($_SESSION['username']);
global $database;
try {
$statement = $database->prepare(
'SELECT username, date, quack
FROM dz2_quacks, dz2_users
WHERE id_user = :id_user
AND dz2_users.id = dz2_quacks.id_user
ORDER BY date DESC;'
);
$statement->execute(
array(
'id_user' => $id_user
)
);
} catch (PDOException $e) {
unset($_SESSION['username']);
$_SESSION['error_message'] = $e->getMessage();
header('Location: login.php');
exit();
}
return $statement;
}
// Obradjuje pokusaj objave novog quacka
function handleNewQuack() {
global $database;
$id_user = getUserIdByUsername($_SESSION['username']);
try {
$statement = $database->prepare(
'INSERT INTO dz2_quacks (id_user, quack, date)
VALUES (:id_user, :quack, :date);'
);
$date = date('Y-m-d H:i:s');
$date = date('Y-m-d H:i:s', strtotime($date . '+2 hours'));
$statement->execute(
array(
'id_user' => $id_user,
'quack' => $_POST['new-quack'],
'date' => $date
)
);
unset($_POST['new-quack']);
$_SESSION['executed_well'] = 'New quack posted!';
} catch (PDOException $e) {
$error_message = $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Quacks</title>
<link rel="stylesheet" href="quack.css" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Pacifico">
</head>
<body>
<?php
display_header_and_nav();
?>
<?php
if (!empty($error_message)) echo '<p id=warning>' . $error_message . '</p>';
if (isset($_SESSION['executed_well'])) {
echo '<p id=executed-well>' . $_SESSION['executed_well'] . '</p>';
unset($_SESSION['executed_well']);
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<textarea maxlength="140" name="new-quack" id="new-quack-textarea" cols="60" rows="5" placeholder="What's on your mind, @<?php echo $_SESSION['username']; ?>?"><?php if (isset($_POST['new-quack'])) echo $_POST['new-quack']; ?></textarea>
<input type="submit" value="Post" name="post-button">
</form>
<h2>My quacks:</h2>
<?php
displayQuacks($user_quacks_statement);
?>
</body>
</html>