-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditartwork.php
422 lines (386 loc) · 14.7 KB
/
editartwork.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
<?php
// editartwork.php
// author jrs
// 2018
$errors = array();
$selected = "";
$selectedTitle = "";
$selectedID = 0;
$selectWork = "";
$disabled="";
$validWork=false;
// Get member's artwork info from database
$artworkResult = selectQuery($db, "*", "imageData", "1", "title");
if(!$artworkResult) {
die("Connection error: select query. " . mysqli_connect_error());
} else { // got artwork
// Dropdown showing pieces to edit and edit button
?>
<div class="container-fluid">
<!-- <div class="col-lg-12"> -->
<form id="selectwork" method="post" action="">
<div class="form-row justify-content-center col-lg-12 offset-lg-2">
<div class="form-group full">
<label for="workSelected" class="bold-with-color">Select a work to edit</label>
<select id="piece" class="form-control" name="workSelected" >
<option value="newWork">*Add New Work*</option>
<?php
// Populate drop-down
if(isset($_POST['editwork']) || isset($_POST['deletework'])) {
if(!empty($_POST['workSelected'])) {
global $selectedTitle;
$s = $_POST['workSelected'];
$selectedID = $_POST['workSelected'];
} else {
$errors['validwork'] = "Choose a work to edit or delete.";
}
}
// Work is selected
//if(count($errors) == 0)
$validWork = true;
while($work = mysqli_fetch_assoc($artworkResult)) {
$n = $work['title'];
$id = $work['imgID'];
// Correct Updated Artwork for dropdown and display
if(isset($_POST['updatework']) && ($n == $_POST['oldtitle'])) {
$n = $_POST['updatetitle'];
}
// Only display on Dropdown menu if not deleted
if(!(isset($_POST['submitdeletion']) && $n == $_POST['oldtitle'])) {
if(isset($_POST['workSelected']) && $_POST['workSelected'] === $n)
print "<option value=\"${id}\" selected>$n</option>";
else
print "<option value=\"${id}\">$n</option>";
}
}
?>
</select>
<small class='errorText'><?php echo array_key_exists('validwork',$errors) ? $errors['validwork'] : ''; ?></small></td>
</div>
<div class="form-group full">
<input type="submit" value="Add/Edit Work" name="editwork">
<input type="submit" value="Delete Work" name="deletework">
</div>
</div>
</form>
</div>
</div>
<?php
if($validWork && (isset($_POST['editwork']) || isset($_POST['deletework']))) {
// TODO: validate entry
$isNew = false;
if(isset($_POST['workSelected']) && $_POST['workSelected'] === "newWork")
$isNew = true;
$editResult = selectQuery($db, "*", "imageData", "imgID = '$selectedID'", "1");
if(!$editResult) {
die("Database error here.");
} else {
// This is the current info, to be edited
$editWork = mysqli_fetch_assoc($editResult);
// Get $workID, the artworkID to update
$workID = $editWork['imgID'];
$imgLocation = $editWork['filename'];
if(isset($_POST['editwork']) || isset($_POST['deletework'])) {
if(isset($_POST['deletework']))
$disabled="disabled";
// =========================================================
// ================ EDIT or ADD NEW Form ===================
// =========================================================
if(!$isNew) {
$path="";
if($editWork['arrangement'] == -1)
$path = "./nomophobiaImages/";
else
$path = "./img/";
// Enable "" in title and media
$htmlTitle = htmlentities($editWork['title']);
$htmlMedia = htmlentities($editWork['media']);
?>
<div><img class="img-responsive center-it thumbnail" src="<?php echo "$path$imgLocation"; ?>" alt="Image Loading Error"></div>
<?php } ?>
<form id="updateform" class="container-fluid" method="post" action="" enctype="multipart/form-data">
<div class="form-row justify-content-center col-lg-12 offset-lg-2">
<?php if($disabled !== "disabled") { ?>
<div class="form-group full">
<label class="bold-with-color" for="updatefilename">Filename</label>
<input class="form-control" type='file' name='updatefilename'>
<input class="form-control" type="checkbox" name="bypassupload" <?php echo $isNew ? "" : "checked" ?>>Bypass Image Upload
</div>
<?php } ?>
<div class="form-group full">
<label class="bold-with-color" for="updatetitle">Title</label>
<input class="form-control" type="text" name="updatetitle" value="<?php echo $isNew ? "" : $htmlTitle;?>" <?php echo $disabled; ?>>
<small class='errorText'><?php echo array_key_exists('updatetitle',$errors) ? $errors['updatetitle'] : ''; ?></small>
</div>
<div class="form-group full">
<label class="bold-with-color" for="updatemedium">Medium</label>
<input class="form-control" type="text" name="updatemedium" value="<?php echo $isNew ? "" : $htmlMedia;?>" <?php echo $disabled; ?>>
</div>
<div class="form-group full">
<label class="bold-with-color" for="updateyear">Year</label>
<input class="form-control" type="text" name="updateyear" value="<?php echo $isNew ? "" : "$editWork[yearCreated]";?>" <?php echo $disabled; ?>>
</div>
<div class="form-group full">
<label class="bold-with-color" for="updatearrangement">Arrangement</label>
<input class="form-control" type="text" name="updatearrangement" value="<?php echo $isNew ? "" : "$editWork[arrangement]";?>" <?php echo $disabled; ?>>
</div>
<div class="form-group full">
<label class="bold-with-color" for="updategrouping">Grouping</label>
(Available Groupings:
<?php
$groupingsResult = selectQuery($db, "DISTINCT grouping", "imageData", "1", "1");
if(!$groupingsResult) {
echo ("couldn't find grouping");
} else {
while($group = mysqli_fetch_array($groupingsResult)) {
echo " ${group[0]} ";
}
}
?>
)
<input class="form-control" type="text" name="updategrouping" value="<?php echo $isNew ? "" : "$editWork[grouping]";?>" <?php echo $disabled; ?>>
</div>
<div class="form-group full">
<label class="bold-with-color" for="updateprice">Price</label>
<input class="form-control" type="text" name="updateprice" value="<?php echo $isNew ? "" : "$editWork[price]";?>" <?php echo $disabled; ?>>
</div>
<div class="form-group full">
<label class="bold-with-color" for="updatebuyer">Buyer</label>
<select class="form-control" name="updatebuyer" <?php echo " $disabled "; ?>>
<option value="">Select...</option>
<?php
$contactResults = selectQuery($db, "c_id,c_name,c_lastname", "contacts", "1", "c_name");
if(!$contactResults) {
die("Contacts could not be retrieved");
} else {
while($contact = mysqli_fetch_assoc($contactResults)) {
$contactID = $contact['c_id'];
$firstname = $contact['c_name'];
$lastname = $contact['c_lastname'];
if($editWork['buyerID'] === $contactID)
echo "<option value='$contactID' selected>$firstname $lastname</option>";
else
echo "<option value='$contactID'>$firstname $lastname</option>";
}
}
?>
</select>
</div>
<div class="form-group full">
<label class="bold-with-color" for="updatesaledate">Sale Date</label>
<input class="form-control" type="date" name="updatesaledate" value="<?php echo $isNew ? "" : "$editWork[saleDate]";?>" <?php echo $disabled; ?>>
</div>
<div class="form-group full">
<input class="form-control" type="checkbox" name="ishomepage" <?php echo $editWork['isHomePage'] ? "Checked" : ""; echo " $disabled "; ?>>
<label class="bold-with-color" for="ishomepage">Put on homepage</label>
</div>
<?php
$ok = " <tr>
<td><small class='errorText'>Selecting \"Delete\" will remove this piece from the database.</small></td>
</tr>";
if(isset($_POST['deletework'])) {
$value = "Delete";
$name = "submitdeletion";
echo $ok;
} else {
$value = "Update";
$name = "updatework";
}
?>
<div class="form-group full">
<input class="form-control" type="submit" value="<?php echo "$value"; ?>" name="<?php echo "$name"; ?>">
</div>
<input type="hidden" name="artworkid" value="<?php echo $workID; ?>">
<input type="hidden" name="oldtitle" value="<?php echo $editWork['title']; ?>">
<input type="hidden" name="oldfilename" value="<?php echo $editWork['filename']; ?>">
<input type="hidden" name="isnew" value="<?php echo $isNew; ?>">
</div>
</form>
<?php
}
}
} elseif(isset($_POST['updatework'])) {
// ===========================================================
// ===================== INSERT or UPDATE ====================
// ===========================================================
// Set bypassUpload if it's set
$bypassUpload = isset($_POST['bypassupload']);
// If not bypassed
if(!$bypassUpload) {
// Validate filename
if(empty($_FILES['updatefilename']['name'])) {
$errors['filename'] = "Please Select a File.";
} else {
$newFilename = $_FILES["updatefilename"]["name"];
}
}
// Validate Title
if(!empty($_POST['updatetitle'])) {
$newTitle = addslashes(trim($_POST['updatetitle']));
if(strlen($newTitle) == 0)
$errors['updatetitle'] = "Enter a title";
} else {
$errors['updatetitle'] = "Enter a title";
}
// Validate Medium
if(!empty($_POST['updatemedium'])) {
$newMedium = addslashes(trim($_POST['updatemedium']));
if(strlen($newMedium) == 0)
$errors['updatemedium'] = "Enter a medium";
} else {
$errors['updatemedium'] = "Enter a medium";
}
// Validate Year
if(!empty($_POST['updateyear'])) {
$newYear = $_POST['updateyear'];
if(strlen($newYear) == 0)
$errors['updateyear'] = "Enter a year";
} else {
$errors['updateyear'] = "Enter a year";
}
// Validate arrangement
if(!empty($_POST['updatearrangement'])) {
$newArrangement = $_POST['updatearrangement'];
if(strlen($newArrangement) == 0)
$errors['updatearrangement'] = "Enter an arrangement";
} else {
$errors['updatearrangement'] = "Enter an arrangement";
}
// Validate Price
$hasPrice = false;
if(!empty($_POST['updateprice'])) {
$newPrice = $_POST['updateprice'];
$hasPrice = true;
}
// Validate Buyer
$hasBuyer = false;
if(!empty($_POST['updatebuyer'])) {
$newBuyer = $_POST['updatebuyer'];
$hasBuyer = true;
}
// Validate SaleDate
$hasSaleDate = false;
if(!empty($_POST['updatesaledate'])) {
$newSaleDate = $_POST['updatesaledate'];
$hasSaleDate = true;
}
// Validate Grouping
$hasGrouping = false;
if(!empty($_POST['updategrouping'])) {
$newGrouping = $_POST['updategrouping'];
$hasGrouping = true;
}
// Set isHomePage if it's set
$isHomePage = isset($_POST['ishomepage']) ? 1 : 0;
$artworkID = $_POST['artworkid'];
$validUpdate = false;
if(count($errors) === 0) {
// Upload image if not bypassed
if(!$bypassUpload) {
include "uploadProcessing.php";
$target_dir = "./img/";
uploadFile($target_dir, "updatefilename");
}
$query = "";
$isNew = $_POST['isnew'];
if(!$isNew) {
// Update Query
$query = "UPDATE imageData SET title = '$newTitle',
media = '$newMedium',
yearCreated = '$newYear',
arrangement = '$newArrangement',
isHomePage = '$isHomePage'";
// Insert new filename unless bypassed
if(!$bypassUpload) $query .= ", filename = '$newFilename'";
// Insert new price if has price
if($hasPrice) $query .= ", price = '$newPrice'";
// Insert new buyer if has buyer
if($hasBuyer) $query .= ", buyerID = '$newBuyer'";
// Insert new sale date if has sale date
if($hasSaleDate) $query .= ", saleDate = '$newSaleDate'";
// Insert new grouping if has grouping
if($hasGrouping) $query .= ", grouping = '$newGrouping'";
// WHERE clause
$query .= " WHERE imgID = $artworkID;";
} else {
// Insert Query, filename is required
$query = "INSERT INTO imageData (title, media, yearCreated, filename, arrangement, isHomePage";
if($hasPrice) $query .= ", price";
if($hasBuyer) $query .= ", buyerID";
if($hasSaleDate) $query .= ", saleDate";
if($hasGrouping) $query .= ", grouping";
$query .= ") VALUES ('$newTitle', '$newMedium', '$newYear', '$newFilename', '$newArrangement', '$isHomePage'";
if($hasPrice) $query .= ", '$newPrice'";
if($hasBuyer) $query .= ", '$newBuyer'";
if($hasSaleDate) $query .= ", '$newSaleDate'";
if($hasGrouping) $query .= ", '$newGrouping'";
$query .= ");";
}
$queryResult = mysqli_query($db, $query);
if(!$queryResult) {
die("Update Error. Unable to access the database." . mysqli_error($db));
} else {
?>
<table align="center">
<tr>
<td>Artwork <?php echo $isNew ? "Inserted" : "Updated"; ?> Successfully!</td>
</tr>
</table>
<?php
if($hasPrice && is_numeric($newPrice)) {
$newPrice = "$$newPrice";
$newTitle = str_replace("\'", "'", $newTitle);
$newMedium = str_replace("\'", "'", $newMedium);
}
// Only show image if uploaded
if($bypassUpload) $newFilename = $_POST['oldfilename'];
?><table align="center"><tr><td><?php
print "<img class='img-responsive center-it thumbnail' src='./img/$newFilename' alt='Image Loading Error'>";
print "<p>$newTitle, $newYear</br>$newMedium</br>";
if($hasPrice)
echo "$newPrice";
if($hasBuyer) {
$nameResult = selectQuery($db, "c_name,c_lastname", "contacts", "c_id = $newBuyer", "1");
if($nameResult) {
$names = mysqli_fetch_array($nameResult);
echo (count($names) > 1) ? "</br>(Purchased by $names[0] $names[1])" : "</br>(Purchased by $names[0])";
}
}
if($hasSaleDate) {
echo "<br/>Sale Date: $newSaleDate";
}
if($hasGrouping) {
echo "</br>Grouping: $newGrouping";
}
echo "</p>";
?></td></tr></table><?php
}
} else {
print_r($errors);
}
} elseif(isset($_POST['submitdeletion'])) {
// ===========================================================
// ========================= DELETE ==========================
// ===========================================================
$artworkID = $_POST['artworkid'];
$title = $_POST['oldtitle'];
// Query for deletion
$deletionQuery = "DELETE FROM imageData WHERE imgID = '$artworkID';";
// Delete Work
$deleteWork = mysqli_query($db, $deletionQuery);
// Tell user what happened
if(!$deleteWork)
die("Deletion Failed. Try again or contact label people");
else {
?>
<table align="center">
<tr>
<td>Deleted<?php echo " <em>$title</em>"; ?> Successfully!</td>
</tr>
</table>
<?php
}
}
}
?>