Skip to content

Commit

Permalink
#24 refactoring image upload
Browse files Browse the repository at this point in the history
  • Loading branch information
tramyardg committed Oct 26, 2019
1 parent c00dc9b commit 2451f71
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 30 deletions.
9 changes: 4 additions & 5 deletions admin/api/uploadCarImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

// car diagram upload with ajax
$v = new CarDAO();

if (isset($_POST)) {
echo 'hello....';
print_r($_POST);
var_dump(file_get_contents('php://input'));
// echo $this->addDiagram($_POST['filesData'], $_GET["id"]) != null ? 1 : 0;
// exit();
if (isset($_POST['fd']) && isset($_POST['id'])) {
echo $v->isDiagramAdded($_POST['fd'], $_POST['id']);
}
}
7 changes: 2 additions & 5 deletions admin/inventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@

<div class="row">
<div class="col-md-12">
<form method="post" id="add-car-photos-form" enctype="multipart/form-data">
<form id="add-car-photos-form" enctype="multipart/form-data">
<div class="panel panel-info">
<div class="panel-heading">Upload photos for this car</div>
<div class="panel-body">
Expand All @@ -273,8 +273,7 @@
<output id="list"></output>
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-success btn-sm" value="Upload" id="upload-car-photos-btn"
name="upload-car-photos-btn">
<input type="submit" name="upload-car-photos-btn" value="Submit" carid="null" />
</div>
</div>
</div>
Expand All @@ -288,7 +287,6 @@
<?php } ?>

</div>

<script src="../js/jquery-3.1.1.min.js"></script>
<script src="../js/jquery.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
Expand Down Expand Up @@ -332,7 +330,6 @@ function previewSelectedImages(evt) {

// Read in the image file as a data URL.
reader.readAsDataURL(f);
console.log(f);
}
}
document.getElementById('files').addEventListener('change', previewSelectedImages, false);
Expand Down
58 changes: 38 additions & 20 deletions admin/js/routine/car-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ let template = new CommonTemplate();
let CarActions = (function () {

let deleteCarSel = {},
confirmDeleteRecordSel = {},
deleteConfirmBtnSel = {};
confirmDeleteRecordSel = {},
deleteConfirmBtnSel = {};

let uploadCarPhotoLink = {},
uploadDeleteCarPhotoModal = {},
uploadCarPhotoBtn = {};
uploadDeleteCarPhotoModal = {},
uploadCarPhotoSubmitBtn = {},
uploadCarPhotoForm = {};

let getPhotosByCarIdFn = {},
displayImagesOfThisCarSel = {},
updateCarLink = {},
updateCarModal = {},
updateCarInfoModalContent = {},
checkedField = {};
displayImagesOfThisCarSel = {},
updateCarLink = {},
updateCarModal = {},
updateCarInfoModalContent = {},
checkedField = {};

let loader = {};

Expand All @@ -33,7 +34,8 @@ let CarActions = (function () {

uploadCarPhotoLink = $('.dropdown a.upload-car-photos');
uploadDeleteCarPhotoModal = $('#upload-delete-car-photos-modal');
uploadCarPhotoBtn = $('input[name=upload-car-photos-btn]');
uploadCarPhotoForm = $('#add-car-photos-form');
uploadCarPhotoSubmitBtn = $('input[name=upload-car-photos-btn]');

updateCarLink = $('a#updateCar_link');
updateCarModal = $('.update-car-modal');
Expand Down Expand Up @@ -71,7 +73,7 @@ let CarActions = (function () {
});

// load data of this car to be updated
updateCarLink.click(function(event) {
updateCarLink.click(function (event) {
let carId = $(this).attr('data-id');
// console.log($(this).attr('data-id'));
updateCarModal.modal('show');
Expand Down Expand Up @@ -125,29 +127,45 @@ let CarActions = (function () {
// ajax request to display list of photos of this car
// not using DataTables
getPhotosByCarIdFn(carId);
uploadCarPhotoSubmitBtn.attr('carid', carId);

return false;
});

uploadCarPhotoBtn.click(function () {
uploadCarPhotoForm.submit(function (e) {
uploadDeleteCarPhotoModal.modal('hide');
e.preventDefault();

let fd = new FormData(this);
fd.append('id', uploadCarPhotoSubmitBtn.attr('carid'));

let thumbImageSel = $('img[model=thumb]');
let filesDataArray = [];
for (let i = 0; i < thumbImageSel.length; i++) {
filesDataArray.push(thumbImageSel.eq(i).attr('src'));
let srcImg = thumbImageSel.eq(i)[0].currentSrc;
let cleanBase64Img = srcImg.substring(srcImg.indexOf(',') + 1);
fd.append('fd[]', cleanBase64Img);

This comment has been minimized.

Copy link
@tramyardg

tramyardg Oct 26, 2019

Author Owner

You may append more data to new FormData(this)

}
console.log(filesDataArray);

loader.css('display', 'block');

$.ajax({
url: 'api/uploadCarImages.php',
type: 'POST',
data: {imagesData: filesDataArray},
url: "api/uploadCarImages.php",
data: fd,
dataType: 'json',
success: function (data) {
console.log(data);
contentType: false,
cache: false,
processData: false,
success: function (r) {
if (r) {
loader.css('display', 'none');
} else {
alert('fail');
}
}
});

e.preventDefault();
return false;
});

Expand All @@ -172,7 +190,7 @@ let CarActions = (function () {
success: function (diagramArray) {
// console.log(diagramArray);
let diagramData = {diagrams: diagramArray};
console.log(diagramData);
// console.log(diagramData);
if (diagramArray.length > 0) {
let html = Mustache.to_html(template.getPhotosByCarIdModalContent(), diagramData);
displayImagesOfThisCarSel.empty();
Expand Down
4 changes: 4 additions & 0 deletions admin/server/CarDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ function addDiagram($files, $id) {
}
}

function isDiagramAdded($files, $id) {
return $this->addDiagram($files, $id) ? 1 : 0;
}

function update(&$carObject) {
// some of them needs to be a varchar (string)
$sql = ' UPDATE '.
Expand Down

1 comment on commit 2451f71

@tramyardg
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made a wrong reference in the commit message, it should have been #26

Please sign in to comment.