-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroom.php
More file actions
executable file
·186 lines (182 loc) · 4.97 KB
/
room.php
File metadata and controls
executable file
·186 lines (182 loc) · 4.97 KB
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
<?php
/**
* @file
* room add/edit
*/
include('functions.php');
/**
* Update a room row (or insert as necessary).
*
* @param array $room
* Assoc array of room fields.
*
* @return int
* Room number generated or updated.
*/
function room_update($room) {
// Walk through the array and convert the room array to a placeholder array.
$ph = array();
foreach ($room as $key => $value) {
if (is_string($key)) {
$ph[":{$key}"] = $value;
}
}
if ($room['upd'] == 0) {
// New row add
$sql1 = "INSERT INTO room (rid, rate, roomsize, sleeps";
$sql2 = " VALUES (:rid, :rate, :roomsize, :sleeps";
if (!empty($room['image'])) {
$sql1 .= ', image';
$sql2 .= ', :image';
}
$sql = $sql1 . ")" . $sql2 . ")";
$desc = 'Insert Room';
}
else {
// @TODO use a session var, see room.php
$ph[':rid'] = $room['upd'];
$sql1 = "UPDATE room SET rate = :rate, bedsize = :bedsize, sleeps = :sleeps";
$sql2 = " WHERE rid = :rid";
if (!empty($room['image'])) {
$sql1 .= ', image = :image';
}
$sql = $sql1 . $sql2;
$desc = 'Update Room';
}
unset($ph[':upd']);
list($pdo, $cnt) = sql_execute($sql, $ph, $desc);
if ($cnt === FALSE) {
header('Location: roomlist.php');
exit;
}
if ($room['upd'] == 0) {
$rtn = $cnt > 0 ? $pdo->lastInsertId() : 0;
}
else {
$rtn = $cnt > 0 ? $room['upd'] : 0;
}
return $rtn;
}
/**
* Handle room form requests.
*/
function process_room_form() {
if (empty($_POST)) {
$room_num = !empty($_GET['r']) ? $_GET['r'] : 0;
$room = room($room_num);
$rid = $room['rid'];
if ($rid < 0) {
msg_add("Room $room_num not found.", 'bg-danger');
header('Location: roomlist.php');
exit();
}
return $room;
}
// Did we get an uploaded file?
if (!empty($_FILES['image'])) {
// @TODO does directory exist?
$target = "images/uploads/" . $_FILES['image']['name'];
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
$_POST['image'] = $target;
}
}
// Check for changes and update the DB
// @TODO validation
// update the db
if ($rid = room_update($_POST)) {
msg_add("Saved changes to room $rid");
}
else {
msg_add("Update failed", 'bg-danger');
}
header("Location: roomlist.php");
exit();
}
// Generate the options html for the select tag for room sizes.
function room_sizes($room) {
$pdo = connect();
$sql = "SELECT * FROM room_size ORDER BY room_size";
$output = '';
foreach ($pdo->query($sql) as $row) {
$output .= '<option value="' . $row['abbr'] . '" ';
if ($row['abbr'] == $room['roomsize']) {
$output .= ' selected';
}
$output .= '>' . $row['room_size'] . "</option>\n";
}
return $output;
}
check_logged_in();
$room = process_room_form();
// Select options
$options = room_sizes($room);
// Clean up rid for new add
$rid = $room['rid'] ? $room['rid'] : '';
?><!DOCTYPE html>
<html>
<?php // @TODO convert to bootstrap ?>
<head>
<title> Room - Bates Motel</title>
<?php print head_elements(); ?>
</head>
<body>
<div style="border:thin solid #ccc; width:350px; height:auto; margin-left:auto; margin-right:auto; text-align:center; border-radius:5px; padding:50px; margin-top:100px;
-webkit-box-shadow: 9px 9px 22px -10px rgba(0,0,0,0.75);
-moz-box-shadow: 9px 9px 22px -10px rgba(0,0,0,0.75);
box-shadow: 9px 9px 22px -10px rgba(0,0,0,0.75); font-family:arial; text-align:left;">
<h1 style="text-align:left; color:rgb(9, 187, 9);">Room Information</h1>
<form method="post" action="room.php" enctype="multipart/form-data">
<table>
<tr><td colspan="2" style="text-align:left;">
<h3><?php if ($rid): ?>
Edit Room <?php print $rid; ?>
<?php else: ?>
Add a New Room
<?php endif; ?>
</h3>
</td></tr>
<?php msg_render(); ?>
<tr>
<td>Room Number:</td>
<?php // @TODO no edit here when updating a row ?>
<td><input type="text" name="rid" value="<?php print $rid; ?>"></td>
</tr>
<tr>
<td>Bed Type</td>
<td>
<select style="width:100%" name="roomsize">
<?php print $options; ?>
</select>
</td>
</tr>
<tr>
<td>Rate</td>
<td><input type="text" name="rate" value="<?php printf("%.2f", $room['rate']); ?>"></td>
</tr>
<tr>
<td>Number of People</td>
<td><input type="text" name="sleeps" value="<?php print $room['sleeps']; ?>"></td>
</tr>
<tr>
<td>Picture</td>
<td>
<?php if (!empty($room['image'])): ?>
<img src="<?php print $room['image']; ?>" alt="Room Picture" />
<?php endif; ?>
<input type="file" name="image" />
</td>
</tr>
<tr>
<td colspan="2" ><br></td>
</tr>
<tr>
<td colspan="2" style="text-align:right;"><input type="submit"></td>
</tr>
</table>
<?php // @TODO implement this as a session var, security problem ?>
<input type="hidden" name="upd" value="<?php print $rid; ?>" />
<?php // @TODO add cancel or go back link to not make any changes ?>
</form>
</div>
</body>
</html>