generated from teknasyon-bootcamp/homework4
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmanage.php
218 lines (198 loc) · 8.89 KB
/
manage.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
<?php
//imports this file to leverage its functionalities and attributes
require_once "post.class.php";
// Get all posts in my list
$posts = Post::All();
// Defines necessary variables for my post list and sets its default values
$action = '';
$postId = '';
$title = '';
$content = '';
//get the value of action variable which was given in the url
if(isset($_GET['action'])){ //isset function checks whether a variable is declared and is different than null.
$action = $_GET['action'];
}
if(isset($_REQUEST['post'])){
$postId = $_REQUEST['post'];
}
if(isset($_POST['title'])){
$title = $_POST['title'];
}
if(isset($_POST['content'])){
$content = $_POST['content'];
}
/**
* Performs create new post, update and delete existing post
* using the value of action variable in the url
*/
switch ($action) {
case 'edit' :
$post = Post::find($postId);
$post->title = $title;
$post->content = $content;
$post->update();
// Sends a raw http header to the browser in a raw form
header("Refresh:0; url=manage.php");
break;
case 'create':
$post = new Post;
$post->title = $title;
$post->content = $content;
$post->create();
header("Refresh:0; url=manage.php");
break;
case 'delete':
$post = Post::find($postId);
$post->delete();
header("Refresh:0; url=manage.php");
break;
}
?>
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title> Posts</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<style>
#more {display: none;}
</style>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<a class="navbar-brand" style="color:blue; width:120%; height:50px; background-color:#b0e0e6; right: 0px;" href="manage.php">Welcome</a>
</div>
</nav>
<div class="col-md-3"></div>
<div class="col-md-12 well well-sm">
<h3 class="text-primary">POSTS</h3>
<hr style="border-top:1px dotted #ccc;" />
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add</button>
<br /><br />
<table class="table table-bordered">
<thead class="alert-info">
<tr>
<th>Title</th>
<th>Content</th>
<th>Operations</th>
</tr>
</thead>
<tbody>
<!-- List created posts -->
<?php foreach($posts as $post) : ?>
<?php $contentFirstPart = substr($post->content, 0, 100);
$contentSecondPart = substr($post->content, 100, strlen($post->content));
?>
<tr>
<td><?=$post->title?></td> <td>
<p><?=$contentFirstPart?><span id="dots">...</span><span id="more"><?=$contentSecondPart?></span></p>
<a href="#" onclick="myFunction()" id="myBtn">Read more</a>
</td>
<!-- Creates update and delete buttons which are having given values on the right side of the page-->
<td><button class="btn btn-warning" type="button" data-toggle="modal" data-target="#update_<?=$post->id?>"><span class="glyphicon glyphicon-edit"></span>Update</button>
<a href="?action=delete&post=<?=$post->id?>" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span>Delete</a></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</div>
<?php foreach($posts as $post) : ?>
<div class="modal fade" id="update_<?= $post->id ?>">
<div class="modal-dialog">
<div class="modal-content">
<form action="manage.php?action=edit&post=<?= $post->id ?>" method="POST">
<div class="modal-header">
<h3 class="modal-title">Update</h3>
</div>
<div class="modal-body">
<div class="col-md-2"></div>
<div class="col-md-8">
<div class="form-group">
<label>Title</label>
<input type="text"
class="form-control"
value="<?= $post->title ?>"
name="title"/>
<!-- A hidden field let us include data that cannot be seen or modified
when the form is submitted.
-->
<input type="hidden"
class="form-control"
value="<?= $post->id ?>"
name="id" />
</div>
<div class="form-group">
<label>Content</label>
<textarea rows="5" cols= "20" type="text" class="form-control" name="content"><?=$post->content?></textarea>
</div>
</div>
</div>
<div style="clear:both;"></div>
<div class="modal-footer">
<button class="btn btn-warning" name="guncelle"><span class="glyphicon glyphicon-update"></span> Update</button>
<button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Exit</button>
</div>
</form>
</div>
</div>
</div>
<?php endforeach ?>
<!-- Add Modal -->
<div class="modal fade" id="form_modal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form action="manage.php?action=create" method="POST">
<div class="modal-header">
<h3 class="modal-title">Add</h3>
</div>
<div class="modal-body">
<div class="col-md-2"></div>
<div class="col-md-8">
<div class="form-group">
<label>Title</label>
<input type="text" class="form-control" name="title"/>
</div>
<div class="form-group">
<label>Content</label>
<textarea type="text" class="form-control" name="content" rows="5" cols="20">
</textarea><!--Converted from input to textarea control-->
</div>
</div>
</div>
<div style="clear:both;"></div>
<div class="modal-footer">
<button class="btn btn-primary" name="save">Save</button>
<button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span>Exit</button>
</div>
</form>
</div>
</div>
</div>
<script>
function myFunction() {
var dots = document.getElementById("dots");
var moreText = document.getElementById("more");
var btnText = document.getElementById("myBtn");
if (dots.style.display === "none") {
dots.style.display = "inline";
btnText.innerHTML = "Read more";
moreText.style.display = "none";
} else {
dots.style.display = "none";
btnText.innerHTML = "Read less";
moreText.style.display = "inline";
}
}
</script>
</body>
</html>