-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
249 lines (232 loc) · 6.92 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
require_once('includes/util.php');
require_once('includes/db_login.php');
require_once('includes/Bug.php');
if (isDebugMode()) {
echo '<div style="background: #fdd;">';
echo "_SERVER is ";
print_r($_SERVER);
echo "_REQUEST is ";
var_dump($_REQUEST);
}
/****** initialization ******/
$notice = '';
$error = new Error;
/**
* map formAction to text button
*/
$formButtonText = array (
'add' => 'Add new bug',
'update' => 'Update'
);
// XXX should I initialize to new Bug? default or lazy-init?
$bug = null;
/**
* Action routing:
* @param ?action : from query string
* if missing, default is to list recent bugs unless param ?id set.
* action=query list recent bugs
* =new : show bug form with add formAction
* =add : insert bug details in DB, on fail show user input
* =update : update bug matching id in DB, on fail show user input
*
* @param ?id : from query string
* if ?id=NNN then retrieve bug details
* and if successful
* show bug in bug form with update formAction
*
* Result: set pageAction and possibly formAction
* TODO Could reuse same form to do a search?
*/
// Get the same-page URL: remember if we're in debug mode.
// and ensure we can always append &key=val to query string.
$samePageURL = $_SERVER['SCRIPT_NAME'] . ( isDebugMode() ? '?debug=1' : '?' );
// TODO filter against valid actions.
$pageAction = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : 'list';
$formAction = '';
/**
* Process update or add bug actions.
* if successful then show notice and list bugs
* else show error notice and redisplay form.
*/
if ($pageAction === 'add' or $pageAction === 'update') {
$notice = "Need to $pageAction ...";
try {
// First create Bug object (no DB interaction)
$bug = new Bug( $_POST );
if (! $bug->validate( $error ) ) {
$dbUpdated = false;
} else {
if ($pageAction === 'add') {
$bug->insert();
} else if ($pageAction === 'update') {
// Should I retrieve bug then UPDATE it, or a static function, or ???
$bug->update();
}
// If we get here then successful
$notice .= "... success!";
$dbUpdated = true;
}
} catch (Exception $e) {
$error->err( "ERROR, could not " . $pageAction . " (" . $e->getMessage() .")" );
$dbUpdated = false;
}
if ( $dbUpdated ) {
$pageAction = 'list';
} else {
// change pageAction back to new/modify, but XXX don't throw away user input.
// XXX or maybe better to leave pageAction, but have a failed validation state.
if ($pageAction === 'add') {
$pageAction = 'new retry';
$formAction = 'add';
} elseif ($pageAction === 'update') {
$pageAction = 'update retry';
$formAction = 'update';
}
}
}
if ($pageAction === 'new') {
$formAction = 'add';
// no params, so create a dummy bug;
$bug = new Bug();
}
$bug_id = isset( $_REQUEST['id'] ) ? $_REQUEST['id'] : null;
// Retrieve bug information
// XXX but not if we failed to update a bug.
if ($pageAction != 'insert' and $bug_id > 0) {
try {
$bug = new Bug();
$bug->retrieve( (int) $bug_id );
// If bug retrieved from DB is bad, inform but don't fail,
// user will have to fix on update.
$bug->validate( $error );
$retrievedBug = true;
} catch (Exception $e) {
$error->err( "ERROR, could not retrieve bug $bug_id (" . $e->getMessage() .")" );
$retrievedBug = false;
}
if ($retrievedBug) {
$pageAction='modify';
$formAction='update';
} else {
$bug_id = null;
}
}
$pageTitle = "Minibugz - " . $pageAction;
// extract bug values from request
// TODO initialize a Bug object from these.
// TODO add a wantvalues() that pulls these out of $_REQUEST?
$title = isset( $_REQUEST['title'] ) ? $_REQUEST['title'] : null;
$description = isset( $_REQUEST['description'] ) ? $_REQUEST['description'] : null;
$status_id = isset( $_REQUEST['status_id'] ) ? $_REQUEST['status_id'] : null;
if (isDebugMode()) {
echo '<br><br></div>';
}
?>
<html>
<head>
<title><?= $pageTitle ?></title>
<link rel="stylesheet" media="screen" href="css/style.css?v=2">
</head>
<body>
<nav>
<? if ($formAction != 'add' ) { ?>
<form id="addbutton" method="get" action="<?= $samePageURL ?>&action=new">
<input type="hidden" name="action" value="new">
<input type="submit" value="Add new bug">
</form>
<? } ?>
<form id="search" action="<?= $samePageURL ?>">
Search:
<input type="hidden" name="action" value="search">
<input type="text" name="id"
size="20" maxlength="255"
placeholder="enter bug ID or term"
>
</form>
</nav>
<br style="clear: both;">
<div class="notice"><?= $notice ?></div>
<?
$outStr = $error->renderHTML();
if ($outStr !== '') {
?>
<div class="error"><?= $outStr ?></div>
<? } ?>
<?
if ($pageAction === 'list') {
require_once('includes/Buglist.php');
$buglist = new Buglist( $_REQUEST );
$err = $buglist->renderHTML();
// Collect the error, if any, from rendering.
// Note the error object has already dumped out,
// and for performance we want to stream out a big bug listing
// rather than capturing it.
if ($err != '') {
print '<div class="error">'; print htmlspecialchars($err); print '</div>';
}
}
?>
<? if ($formAction) { ?>
<form method="POST" action="<?= $samePageURL . '&action=' . $formAction ?>">
<input type="hidden" name="bug_id" value="<?= $bug->bug_id ?>">
<table>
<tr>
<th><?= $bug->bug_id ? "showing $bug->bug_id" : "New bug" ?></th>
</tr>
<tr>
<th>Title</th>
<td>
<input type="text" name="title"
size="40" maxlength="255"
required
value="<?=$bug->title ?>"
id="title"
>
</td>
</tr>
<tr>
<th valign="top">Description</th>
<td>
<textarea name="description"
rows="5" cols="40" maxlength="1000"
required
id="description"
><?=$bug->description ?></textarea>
</td>
</tr>
<tr>
<th>Status</th>
<td>
<?= makeSelect($bug->status_id) ?>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="submit" value="<?= $formButtonText[$formAction] ?>">
</td>
</tr>
<? if ( $formAction === 'update' ) {
require_once('includes/Bughistory.php'); ?>
<tr>
<td>Status history</td>
<td id="status_last_modified">
<table>
<tr>
<td>
<?= Bug::getStatusHTML( (int)$bug->status_id) ?>
<td>
<?= $bug->status_last_modified ?>
</td>
</tr>
<? Bughistory::renderHTML ( (int)$bug->bug_id, true ) ?>
</table>
</td>
</tr>
<? } ?>
</table>
</form>
<? } ?>
</body>
</html>