generated from teknasyon-bootcamp/homework5
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.php
219 lines (191 loc) · 8.52 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
214
215
216
217
218
219
<?php
require_once __DIR__ . "/vendor/autoload.php";
use Database\DynamicDB;
use App\Log\Logger;
use App\Backup\Export;
$logger = new Logger;
$logger->log("Index.php'ye giriş yapıldı.", Logger::WARNING);
$database = new DynamicDB;
// Get all books in my list
$books = $database->all('books');
// Defines necessary variables for my book list and sets its default values
$action = '';
$bookId = '';
$name = '';
$author = '';
//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['book'])) {
$bookId = $_REQUEST['book'];
}
if (isset($_POST['name'])) {
$name = $_POST['name'];
}
if (isset($_POST['author'])) {
$author = $_POST['author'];
}
/**
* Performs create new book, update and delete existing post
* using the value of action variable in the url
*/
switch ($action) {
case 'import':
Export::importBackupFile();
// Sends a raw http header to the browser in a raw form
header("Refresh:0; url=index.php");
break;
case 'backup':
Export::createBackupFile();
break;
case 'edit':
$values = [
'name' => $name,
'author' => $author,
];
$database->update('books', $bookId, $values);
// Sends a raw http header to the browser in a raw form
header("Refresh:0; url=index.php");
break;
case 'create':
$values = [
'name' => $name,
'author' => $author,
];
$database->create('books', $values);
header("Refresh:0; url=index.php");
break;
case 'delete':
$database->delete('books', $bookId);
header("Refresh:0; url=index.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> Books</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="index.php">Welcome</a>
</div>
</nav>
<div class="col-md-3"></div>
<div class="col-md-12 well well-sm">
<h3 class="text-primary">BOOKS</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>
<a href="index.php?action=backup" class="btn btn-primary"><span class="glyphicon glyphicon-plus"></span> Export All Data </a>
<a href="index.php?action=import" class="btn btn-danger"><span class="glyphicon glyphicon-plus"></span> Import All Data </a>
<br /><br />
<table class="table table-bordered">
<thead class="alert-info">
<tr>
<th>Name</th>
<th>Author</th>
<th>Operations</th>
</tr>
</thead>
<tbody>
<!-- List created posts -->
<?php
// Show all books if there isn't any specific get request
foreach ($books as $book) {
echo "<tr>";
echo '<td>';
echo '<a class="navbar-brand" href=book.php?book=' . $book['id'] . '>' . $book['name'] . "</a>";
echo '</td>';
echo '<td>' . $book['author'] . '</td>';
echo '<td><button type="button" data-toggle="modal" data-target="#update_' . $book['id'] . '"class="btn btn-warning"><span class="glyphicon glyphicon-edit"></span>Update</button>
<a href="index.php?action=delete&book=' . $book['id'] . '"class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span>Delete</a></td>';
echo "</tr>";
}
?>
</tbody>
</table>
</div>
<?php foreach ($books as $book) : ?>
<div class="modal fade" id="update_<?= $book['id'] ?>">
<div class="modal-dialog">
<div class="modal-content">
<form action="index.php?action=edit&book=<?= $book['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>Name</label>
<input type="text" class="form-control" value="<?= $book['name'] ?>" name="name" />
<!-- 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="<?php $book['id'] ?>" name="id" />
</div>
<div class="form-group">
<label>Author</label>
<input type="text" class="form-control" value="<?= $book['author'] ?>" name="author" />
</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="index.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>Name</label>
<input type="text" class="form-control" name="name" />
</div>
<div class="form-group">
<label>Author</label>
<input type="text" class="form-control" name="author" />
</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>
</body>
</html>