Implemented Edit Found Items
This commit is contained in:
parent
03655c579b
commit
efe17de00c
3 changed files with 289 additions and 2 deletions
43
ajax.php
43
ajax.php
|
@ -97,6 +97,49 @@ switch($_GET["action"]) {
|
|||
$errormsg = "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
|
||||
}
|
||||
$successmsg = "one item deleted";
|
||||
}else{
|
||||
$errormsg = "id not set";
|
||||
}
|
||||
break;
|
||||
case "edit_found_item":
|
||||
if(hasval($_POST["id"]) && hasval($_POST["was"]) && hasval($_POST["wann"])) {
|
||||
/* Prepared statement, stage 1: prepare */
|
||||
$was=$_POST["was"];
|
||||
if (!($stmt = $mysqli->prepare("UPDATE found_items SET was=?, wo=?, `date`=? WHERE id = ?"))) {
|
||||
$errormsg = "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
|
||||
}
|
||||
if (!$stmt->bind_param("sssi", $_POST["was"] , $_POST["wo"], $_POST["wann"], $_POST["id"])) {
|
||||
$errormsg = "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
|
||||
}
|
||||
if (!$stmt->execute()) {
|
||||
$errormsg = "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
|
||||
}
|
||||
|
||||
if(isset($_FILES["image"])){
|
||||
if(!file_exists ( "upload/")){
|
||||
mkdir("upload/");
|
||||
}
|
||||
|
||||
$hash = md5($_FILES['image']['name'].time());
|
||||
if(move_uploaded_file($_FILES['image']['tmp_name'], "upload/".$hash)){
|
||||
if (!($stmt = $mysqli->prepare("INSERT INTO files(hash, item_id) VALUES (?, ?)"))) {
|
||||
$errormsg = "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
|
||||
}
|
||||
if (!$stmt->bind_param("si", $hash, $_POST["id"])) {
|
||||
$errormsg = "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
|
||||
}
|
||||
if (!$stmt->execute()) {
|
||||
$errormsg = "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
|
||||
}
|
||||
$successmsg = "one item edited";
|
||||
}else{
|
||||
$errormsg = "upload failed";
|
||||
}
|
||||
}else{
|
||||
$errormsg = "upload failed";
|
||||
}
|
||||
|
||||
|
||||
}else{
|
||||
$errormsg = "id not set";
|
||||
}
|
||||
|
|
16
index.php
16
index.php
|
@ -23,9 +23,9 @@ function auth(){
|
|||
function get_founditems($onlyUnmatched = false){
|
||||
global $mysqli;
|
||||
if ($onlyUnmatched) {
|
||||
$res = $mysqli->query("SELECT found_items.*, files.hash FROM files, found_items LEFT JOIN matches ON found_items.id = matches.f_id WHERE files.item_id = found_items.id AND found_items.del = 0 AND matches.f_id IS NULL ORDER BY found_items.id DESC");
|
||||
$res = $mysqli->query("SELECT distinct found_items.*, files.hash FROM (SELECT files.* from files ORDER BY files.id DESC LIMIT 100000) as files, found_items LEFT JOIN matches ON found_items.id = matches.f_id WHERE found_items.del = 0 AND files.item_id = found_items.id AND matches.f_id IS NULL GROUP BY files.item_id ORDER BY found_items.id DESC");
|
||||
} else {
|
||||
$res = $mysqli->query("SELECT found_items.*, files.hash FROM files, found_items WHERE files.item_id = found_items.id AND found_items.del = 0 ORDER BY found_items.id ASC");
|
||||
$res = $mysqli->query("SELECT found_items.*, files.hash FROM (SELECT files.* from files ORDER BY files.id DESC LIMIT 100000) as files, found_items WHERE files.item_id = found_items.id AND found_items.del = 0 GROUP BY files.item_id ORDER BY found_items.id ASC");
|
||||
}
|
||||
$ret = array();
|
||||
while ($row = $res->fetch_assoc()) {
|
||||
|
@ -48,6 +48,18 @@ function get_lostitems($onlyUnmatched = false){
|
|||
return $ret;
|
||||
}
|
||||
|
||||
function get_founditem($id){
|
||||
global $mysqli;
|
||||
|
||||
$res = $mysqli->query("SELECT * FROM found_items, files WHERE found_items.id = $id AND found_items.id = files.item_id ORDER BY files.id DESC");
|
||||
|
||||
$ret = array();
|
||||
while ($row = $res->fetch_assoc()) {
|
||||
$ret[] = $row;
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
function get_matches(){
|
||||
global $mysqli;
|
||||
$res = $mysqli->query("SELECT matches.id AS id, matches.f_id as f_id, matches.l_id as l_id, lost.was as l_desc, items.was as f_desc "
|
||||
|
|
232
templates/found_item_edit.php
Normal file
232
templates/found_item_edit.php
Normal file
|
@ -0,0 +1,232 @@
|
|||
<?php
|
||||
$id = $_GET["id"];
|
||||
|
||||
$item = get_founditem($id)[0];
|
||||
?>
|
||||
|
||||
<style>
|
||||
#img-upload{
|
||||
width: 100%;
|
||||
}
|
||||
</style>
|
||||
|
||||
<div class="container-fluid">
|
||||
|
||||
<!-- Breadcrumbs-->
|
||||
<ol class="breadcrumb">
|
||||
<li class="breadcrumb-item">
|
||||
<a href="#">Dashboard</a>
|
||||
</li>
|
||||
<li class="breadcrumb-item">Found</li>
|
||||
<li class="breadcrumb-item active">Edit</li>
|
||||
</ol>
|
||||
|
||||
<div id="alertmessage" class="collapse alert alert-danger" role="alert">
|
||||
This is a danger alert—check it out!
|
||||
</div>
|
||||
|
||||
|
||||
<div id="successmessage" class="collapse alert alert-success" role="alert">
|
||||
A simple success alert—check it out!
|
||||
</div>
|
||||
|
||||
<!-- Area Chart Example-->
|
||||
<div class="card mb-3">
|
||||
<div class="card-header">
|
||||
<i class="fas fa-edit"></i>
|
||||
Edit Item
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<form method="POST" action="/action.php?action=edit_found" id="found_form">
|
||||
<p><input type="text" readonly="true" class="form-control" placeholder="ID" name="id" value="<?php echo $id; ?>"></p>
|
||||
<p><input type="text" class="form-control" placeholder="WANN" name="wann" value="<?php echo $item["date"]; ?>"></p>
|
||||
<p><input type="text" class="form-control" placeholder="WO" name="wo" value="<?php echo $item["wo"]; ?>"></p>
|
||||
<p><input type="text" class="form-control" placeholder="WAS" name="was" value="<?php echo $item["was"]; ?>"></p>
|
||||
<div class="form-group">
|
||||
<div class="input-group">
|
||||
<div class="custom-file btn-file">
|
||||
<input type="file" class="custom-file-input" id="inputGroupImage" name="image" accept="image/*" capture="environment">
|
||||
<label class="custom-file-label" for="inputGroupImage">Choose Image</label>
|
||||
<input type="text" class="form-control" readonly>
|
||||
</div>
|
||||
</div>
|
||||
<img id='img-upload' src="/upload/<?php echo $item["hash"]; ?>"/>
|
||||
</div>
|
||||
<p><button type="submit" class="form-control" onclick="edit_found_item(); return false;">Save</button></p>
|
||||
</form>
|
||||
</div>
|
||||
<div class="card-footer small text-muted">Updated yesterday at 11:59 PM</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<!-- /.container-fluid -->
|
||||
|
||||
|
||||
<script>
|
||||
|
||||
|
||||
function edit_found_item(){
|
||||
|
||||
var fileInput = document.querySelector('#inputGroupImage');
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('POST', '/ajax.php?action=edit_found_item');
|
||||
|
||||
xhr.upload.onprogress = function(e)
|
||||
{
|
||||
/*
|
||||
* values that indicate the progression
|
||||
* e.loaded
|
||||
* e.total
|
||||
*/
|
||||
|
||||
$("#alertmessage").collapse('hide');
|
||||
$("#successmessage").html((100*e.loaded/e.total)+"%").collapse('show');
|
||||
console.log("progress");
|
||||
console.log(e);
|
||||
|
||||
};
|
||||
|
||||
xhr.upload.onabort = function (e) {
|
||||
console.log("abort");
|
||||
console.log(e);
|
||||
};
|
||||
|
||||
xhr.upload.onload = function (e) {
|
||||
console.log("load");
|
||||
console.log(e);
|
||||
};
|
||||
|
||||
xhr.upload.ontimeout = function (e) {
|
||||
console.log("timeout");
|
||||
console.log(e);
|
||||
};
|
||||
|
||||
xhr.upload.onerror = function (e) {
|
||||
console.log("error");
|
||||
console.log(e);
|
||||
$("#successmessage").collapse('hide');
|
||||
$("#alertmessage").html("error").collapse('show');
|
||||
};
|
||||
|
||||
xhr.onload = function(e)
|
||||
{
|
||||
console.log("foo load");
|
||||
console.log(e);
|
||||
reply = JSON.parse(xhr.responseText);
|
||||
console.log(reply);
|
||||
|
||||
if(reply.status == "ok"){
|
||||
$( "#lost_form" ).trigger("reset");
|
||||
$('#img-upload').attr('src', "");
|
||||
$("#alertmessage").collapse('hide');
|
||||
$("#successmessage").html(reply.message).collapse('show');
|
||||
}else{
|
||||
$("#successmessage").collapse('hide');
|
||||
$("#alertmessage").html(reply.message).collapse('show');
|
||||
}
|
||||
};
|
||||
|
||||
// upload success
|
||||
xhr.onreadystatechange = function () {
|
||||
if (xhr.readyState == 4) {
|
||||
if (xhr.status != 200) {
|
||||
reply = JSON.parse(xhr.responseText);
|
||||
console.log(reply);
|
||||
$("#successmessage").collapse('hide');
|
||||
$("#alertmessage").html(reply.message).collapse('show');
|
||||
|
||||
}
|
||||
|
||||
}else{
|
||||
console.log(xhr.readyState);
|
||||
}
|
||||
}
|
||||
|
||||
var form = new FormData(document.querySelector("#found_form"));
|
||||
form.append('title', fileInput.files[0].name);
|
||||
form.append('image', fileInput.files[0]);
|
||||
|
||||
xhr.send(form);
|
||||
|
||||
|
||||
/*
|
||||
$.post( "/ajax.php?action=add_found_item", $( "#lost_form" ).serialize() ).done(function( data ) {;
|
||||
reply = JSON.parse(data);
|
||||
if(reply.status == "ok"){
|
||||
$( "#lost_form" ).trigger("reset");
|
||||
$("#alertmessage").collapse('hide');
|
||||
$("#successmessage").html(reply.message).collapse('show');
|
||||
}else{
|
||||
$("#successmessage").collapse('hide');
|
||||
$("#alertmessage").html(reply.message).collapse('show');
|
||||
}
|
||||
}).fail(function() {
|
||||
$("#successmessage").collapse('hide');
|
||||
$("#alertmessage").html("error").collapse('show');
|
||||
});*/
|
||||
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
|
||||
|
||||
|
||||
<!-- Bootstrap core JavaScript-->
|
||||
<script src="/vendor/jquery/jquery.min.js"></script>
|
||||
<script src="/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
|
||||
|
||||
<!-- Core plugin JavaScript-->
|
||||
<script src="/vendor/jquery-easing/jquery.easing.min.js"></script>
|
||||
|
||||
<!-- Page level plugin JavaScript-->
|
||||
<script src="/vendor/chart.js/Chart.min.js"></script>
|
||||
|
||||
<!-- Custom scripts for all pages-->
|
||||
<script src="/js/sb-admin.min.js"></script>
|
||||
|
||||
<!-- Demo scripts for this page-->
|
||||
<script src="/js/demo/chart-area-demo.js"></script>
|
||||
<script src="/js/demo/chart-bar-demo.js"></script>
|
||||
<script src="/js/demo/chart-pie-demo.js"></script>
|
||||
|
||||
<script>
|
||||
|
||||
$(document).ready( function() {
|
||||
$(document).on('change', '.btn-file :file', function() {
|
||||
var input = $(this),
|
||||
label = input.val().replace(/\\/g, '/').replace(/.*\//, '');
|
||||
input.trigger('fileselect', [label]);
|
||||
});
|
||||
|
||||
$('.btn-file :file').on('fileselect', function(event, label) {
|
||||
|
||||
var input = $(this).parents('.input-group').find(':text'),
|
||||
log = label;
|
||||
|
||||
if( input.length ) {
|
||||
input.val(log);
|
||||
} else {
|
||||
if( log ) alert(log);
|
||||
}
|
||||
|
||||
});
|
||||
function readURL(input) {
|
||||
if (input.files && input.files[0]) {
|
||||
var reader = new FileReader();
|
||||
|
||||
reader.onload = function (e) {
|
||||
$('#img-upload').attr('src', e.target.result);
|
||||
}
|
||||
|
||||
reader.readAsDataURL(input.files[0]);
|
||||
}
|
||||
}
|
||||
|
||||
$("#inputGroupImage").change(function(){
|
||||
readURL(this);
|
||||
});
|
||||
});
|
||||
|
||||
</script>
|
Loading…
Reference in a new issue