/
admin.php
223 lines (156 loc) · 6.62 KB
/
admin.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
214
215
216
217
218
219
220
221
222
223
<?php
require_once('includes/db-connect.php');
require_once('includes/functions.php');
if (!hasRights(100)) {
header('Location: index.php');
exit;
}
if (isset($_POST['title']))
{
extract($_POST);
if ( !empty($title) && !empty($teaser) && !empty($content))
{
if ( !isset($_GET['edit'])) {
$statement = $conn->prepare("INSERT INTO articles(title, teaser, content, postedOn, teaserIMG) VALUES(:title, :teaser, :content, :postedOn, :teaserIMG
)"
);
$statement->execute(array(
':title' => $title,
':teaser' => $teaser,
':content' => $content,
':postedOn' => time(),
':teaserIMG' => '',
));
$id = $conn->lastInsertId();
$feedback = getFeedback("Your article has been posted. See it <a href='article.php?id=$id'>here</a>.", 'success');
}
else
{
$statement = $conn->prepare(
"UPDATE articles SET
title = :title,
teaser = :teaser,
content = :content
WHERE id = :id"
);
$statement->execute(array(
':title' => $title,
':teaser' => $teaser,
':content' => $content,
':id' => $_GET['edit']
));
$feedback = getFeedback("Your article has been updated. See it <a href='article.php?id=$_GET[edit]'>here</a>.", 'success');
}
}
else
{
$feedback = getFeedback('Please add a title, teaser and image to your blog post.', 'warning');
}
}
if ( isset($_GET['deleteId']))
{
$statement = $conn->prepare("DELETE FROM articles WHERE id = :id");
$statement->execute(array(
':id' => $_GET['deleteId']
));
$feedback = getFeedback('Your Post has been deleted.', 'success');
}
if (isset($_GET['edit'])) {
$statement = $conn->prepare("SELECT * FROM articles WHERE id = :id");
$statement->execute(array(
':id' => $_GET['edit']
));
$articleToEdit = $statement->fetch(PDO::FETCH_OBJ);
}
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>littleBlog | Startseite</title>
<link rel="stylesheet" href="http://getbootstrap.com/dist/css/bootstrap.css">
<link rel="stylesheet" href="css/main.css">
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src='//cdn.tinymce.com/4/tinymce.min.js'></script>
<script src="js/tinymce-placeholder.js"></script>
<script>
tinymce.init({
selector: '.mytextarea',
plugins: 'link, image, placeholder',
menubar: false
});
</script>
</head>
<body>
<div id="site-wrap">
<h2>Adminbereich</h2>
<?php require_once('includes/include.nav.php'); ?>
<hr>
<h3><?php if ( isset($_GET['edit'])){ echo 'Edit this'; } else { echo 'Create a new'; } ?> Post</h3>
<?php
if ( isset($feedback) )
{
echo $feedback;
}
?>
<form action="<?= $_SERVER['REQUEST_URI']; ?>" method="post">
<div class="form-group">
<input
type="text"
class="form-control"
name="title"
placeholder="Title"
value="<?php if ( isset($_GET['edit'])) { echo $articleToEdit->title;} ?>"
>
</div>
<div class="form-group">
<input type="text" class="form-control" name="teaser" placeholder="Teaser" value="<?php if ( isset($_GET['edit'])) { echo $articleToEdit->teaser;} ?>">
</div>
<textarea class="mytextarea form-control" name="content" rows="3" placeholder="Content"><?php if ( isset($_GET['edit'])) { echo $articleToEdit->content;} ?></textarea>
<input type="submit" class="btn btn-primary" value="<?php if ( isset($_GET['edit'])){ echo 'Update'; } else { echo 'Post'; } ?>"
>
</form>
<h3>Edit existing posts</h3>
<table class="table table-striped">
<thead>
<td>Date</td>
<td>Title</td>
<td>Edit / Delete</td>
</thead>
<?php
$result = $conn->query("SELECT * FROM articles ORDER BY postedOn DESC");
while ( $article = $result->fetch(PDO::FETCH_OBJ)) {
?>
<tr>
<td><?= date('H:i, d.m', $article->postedOn)?></td>
<td><?= substr($article->title, 0, 80).'...'; ?></td>
<td>
<a href="<?="article.php?id=$article->id"; ?>" class="btn btn-primary btn-xs">View</a>
<a href="<?="admin.php?edit=$article->id"; ?>" class="btn btn-primary btn-xs">Edit</a>
<button class="btn btn-warning btn-xs" data-toggle="modal" data-target=".bs-example-modal-sm<?= $article->id; ?>">Delete</button>
<div class="modal fade bs-example-modal-sm<?= $article->id; ?>" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title"><?= $article->title; ?></h4>
</div>
<div class="modal-body">
<p>Would you really like to delete this post</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<a href="admin.php?deleteId=<?= $article->id; ?>" class="btn btn-warning">Delete</a>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</td>
</tr>
<?php
}
?>
</table>
</div>
</body>
</html>