From 8185b31ef0fa0a51cf49fe4c04f79fbc822de123 Mon Sep 17 00:00:00 2001 From: Shreesh Arora Date: Fri, 10 Mar 2023 18:46:56 +0530 Subject: [PATCH] fix mulitple hotel cover image when uploading multiple images --- .../classes/HotelImage.php | 33 +++- .../admin/AdminAddHotelController.php | 30 ++-- .../views/js/hotelImage.js | 149 +++++++----------- .../_partials/htl-images-list-row.tpl | 39 +++++ .../add_hotel/_partials/htl-images-list.tpl | 47 ++++++ .../admin/add_hotel/helpers/form/form.tpl | 48 +----- 6 files changed, 195 insertions(+), 151 deletions(-) create mode 100644 modules/hotelreservationsystem/views/templates/admin/add_hotel/_partials/htl-images-list-row.tpl create mode 100644 modules/hotelreservationsystem/views/templates/admin/add_hotel/_partials/htl-images-list.tpl diff --git a/modules/hotelreservationsystem/classes/HotelImage.php b/modules/hotelreservationsystem/classes/HotelImage.php index 3c9efcb4b..e3e538706 100644 --- a/modules/hotelreservationsystem/classes/HotelImage.php +++ b/modules/hotelreservationsystem/classes/HotelImage.php @@ -44,7 +44,7 @@ class HotelImage extends ObjectModel $this->source_index = _PS_HOTEL_IMG_DIR_.'index.php'; - $this->image_dir = _PS_HOTEL_IMG_.$this->getImgFolder(); + $this->image_dir = _PS_HOTEL_IMG_DIR_.$this->getImgFolder(); } /** @@ -61,6 +61,33 @@ class HotelImage extends ObjectModel return true; } + /** + * Delete the hotel image from disk and remove the containing folder if empty + * Handles both legacy and new image filesystems + */ + public function deleteImage($force_delete = false) + { + parent::deleteImage(); + // Can we delete the image folder? + if (is_dir($this->image_dir)) { + $delete_folder = true; + foreach (scandir($this->image_dir) as $file) { + if (($file != '.' && $file != '..' && $file != 'index.php')) { + $delete_folder = false; + break; + } + } + } + + if (isset($delete_folder) && $delete_folder) { + // delete index image before deleting folder + unlink($this->image_dir.'index.php'); + @rmdir($this->image_dir); + } + + return true; + } + /** * [getImagesByHotelId :: To get paginated hotel images data] * @param [int] $id_hotel [id_hotel to get images of] @@ -232,9 +259,9 @@ class HotelImage extends ObjectModel } } $addedImage = array( - 'id_image' => $objHtlImage->id, + 'id' => $objHtlImage->id, 'cover' => $objHtlImage->cover, - 'image_url' => Context::getContext()->link->getMediaLink($objHtlImage->getImageLink($objHtlImage->id)), + 'image_link' => Context::getContext()->link->getMediaLink($objHtlImage->getImageLink($objHtlImage->id)), ); return $addedImage; } diff --git a/modules/hotelreservationsystem/controllers/admin/AdminAddHotelController.php b/modules/hotelreservationsystem/controllers/admin/AdminAddHotelController.php index 57e616fce..8dafbd11f 100644 --- a/modules/hotelreservationsystem/controllers/admin/AdminAddHotelController.php +++ b/modules/hotelreservationsystem/controllers/admin/AdminAddHotelController.php @@ -576,6 +576,7 @@ class AdminAddHotelController extends ModuleAdminController public function ajaxProcessUploadHotelImages() { + $response = array('success' => false); $idHotel = Tools::getValue('id_hotel'); if ($idHotel) { $invalidImg = ImageManager::validateUpload( @@ -584,27 +585,32 @@ class AdminAddHotelController extends ModuleAdminController ); if (!$invalidImg) { // Add Hotel images - $kwargs = [ - 'id_hotel' => $idHotel, - 'hotel_image' => $_FILES['hotel_image'], - ]; $objHotelImage = new HotelImage(); $imageDetail = $objHotelImage->uploadHotelImages($_FILES['hotel_image'], $idHotel); if ($imageDetail) { - die(json_encode($imageDetail)); + $response['success'] = true; + $imageDetail['image_link'] = $this->context->link->getMediaLink($objHotelImage->getImageLink($imageDetail['id'],ImageType::getFormatedName('large'))); + $imageDetail['image_link_small'] = $this->context->link->getMediaLink($objHotelImage->getImageLink($imageDetail['id'], ImageType::getFormatedName('small'))); + $response['data']['image_info'] = $imageDetail; + // get image row + $this->context->smarty->assign(array( + 'image' => $imageDetail, + 'hotel_info' => array('id' => $idHotel) + )); + $response['data']['image_row'] = $this->context->smarty->fetch( + _PS_MODULE_DIR_.$this->module->name. + '/views/templates/admin/add_hotel/_partials/htl-images-list-row.tpl' + ); } else { - die(json_encode(array('hasError' => true))); + $response['errors'][] = $this->l('Unable to uploade image. Please try again'); } } else { - die( - json_encode( - array('hasError' => true, 'message' => $_FILES['hotel_image']['name'].': '.$invalidImg) - ) - ); + $response['errors'][] = $_FILES['hotel_image']['name'].': '.$invalidImg; } } else { - die(json_encode(array('hasError' => true))); + $response['errors'][] = $this->l('Hotel info not found. Please try reloading the page'); } + $this->ajaxDie(json_encode($response)); } public function ajaxProcessChangeCoverImage() diff --git a/modules/hotelreservationsystem/views/js/hotelImage.js b/modules/hotelreservationsystem/views/js/hotelImage.js index 81e2ba5f7..f893b9748 100644 --- a/modules/hotelreservationsystem/views/js/hotelImage.js +++ b/modules/hotelreservationsystem/views/js/hotelImage.js @@ -27,104 +27,75 @@ $(document).ready(function () { }); $("#hotel_images").on("change", function(event) { - files = event.target.files; - for (var i = 0; i < files.length; i++) { - var formData = new FormData(); - var file = files[i]; - - if (typeof file != 'undefined') { - if (file.size > maxSizeAllowed) { - showErrorMessage(filesizeError + '[' + file.name + ']'); - } else { - formData.append('hotel_image', file); - uploadHotelImages(formData); - } - } - } - $('#hotel_images').val(null); + const files = Array.from(event.target.files); + uploadSelectedImages(files); }); - function uploadHotelImages(formData) { - var idHotel = $("#id-hotel").val(); - formData.append('id_hotel', idHotel); - formData.append('ajax', true); - formData.append('action', 'uploadHotelImages'); + function uploadSelectedImages(files) + { + if (files.length == 0) return; // all done + let file = files[0]; + files.splice(0, 1); + createImageUploadRequest(file).then((formData) => { + uploadHotelImages(formData).then(() => { + uploadSelectedImages(files) + }); + }) + } - $.ajax({ - type:'POST', - url: adminHotelCtrlUrl, - data: formData, - cache:false, - contentType: false, - processData: false, - success:function(image){ - image = JSON.parse(image); - if (!image.hasError) { - $('.list-empty-tr').remove(); - var html = ''; - html += ''; - html += ''+image.id_image+''; - html += ''; - html += ''; - html += ''; - html += ''; - html += ''; - html += ''; - html += ''; - if(image.cover) { - html += ''; - } else { - html += ''; - } - html += ''; - html += ''; - html += ''; - html += ''; - html += ''; - html += ''; - $("#hotel-image-table tbody").append(html); - - showSuccessMessage(imgUploadSuccessMsg); + function createImageUploadRequest(file) + { + return new Promise((resolve, reject) => { + if (typeof file != 'undefined') { + if (file.size > maxSizeAllowed) { + reject(filesizeError + '[' + file.name + ']'); } else { - if (typeof image.message != 'undefined') { - showErrorMessage(image.message); - } else { - showErrorMessage(imgUploadErrorMsg); - } + var formData = new FormData(); + var idHotel = $("#id-hotel").val(); + formData.append('hotel_image', file); + formData.append('id_hotel', idHotel); + formData.append('ajax', true); + formData.append('action', 'uploadHotelImages'); + resolve(formData); } - }, - error: function(data) { - showErrorMessage(imgUploadErrorMsg); } }); } + function uploadHotelImages(formData) { + return new Promise((resolve, reject) => { + $.ajax({ + type:'POST', + dataType:'JSON', + url: adminHotelCtrlUrl, + data: formData, + cache:false, + contentType: false, + processData: false, + success:function(response){ + if (response.success) { + $('.list-empty-tr').remove(); + $("#hotel-image-table tbody").append(response.data.image_row); + showSuccessMessage(imgUploadSuccessMsg); + resolve(imgUploadSuccessMsg); + } else { + if (typeof response.errors != 'undefined') { + showErrorMessage(response.errors); + reject(response.errors); + } else { + showErrorMessage(imgUploadErrorMsg); + reject(imgUploadErrorMsg); + } + } + }, + error: function(data) { + showErrorMessage(imgUploadErrorMsg); + reject(imgUploadErrorMsg); + } + }); + }); + } + $('body').on('click', '.changer-cover-image', function(e){ e.preventDefault(); var idHotel = $(this).attr('data-id-hotel'); diff --git a/modules/hotelreservationsystem/views/templates/admin/add_hotel/_partials/htl-images-list-row.tpl b/modules/hotelreservationsystem/views/templates/admin/add_hotel/_partials/htl-images-list-row.tpl new file mode 100644 index 000000000..b8b238731 --- /dev/null +++ b/modules/hotelreservationsystem/views/templates/admin/add_hotel/_partials/htl-images-list-row.tpl @@ -0,0 +1,39 @@ +{** +* 2010-2022 Webkul. +* +* NOTICE OF LICENSE +* +* All right is reserved, +* Please go through LICENSE.txt file inside our module +* +* DISCLAIMER +* +* Do not edit or add to this file if you wish to upgrade this module to newer +* versions in the future. If you wish to customize this module for your +* needs please refer to CustomizationPolicy.txt file inside our module for more information. +* +* @author Webkul IN +* @copyright 2010-2022 Webkul IN +* @license LICENSE.txt +*} + + + {$image.id|escape:'html':'UTF-8'} + + + + + + + + {if $image.cover == 1} + + {else} + + {/if} + + + + + + \ No newline at end of file diff --git a/modules/hotelreservationsystem/views/templates/admin/add_hotel/_partials/htl-images-list.tpl b/modules/hotelreservationsystem/views/templates/admin/add_hotel/_partials/htl-images-list.tpl new file mode 100644 index 000000000..2f132ba9f --- /dev/null +++ b/modules/hotelreservationsystem/views/templates/admin/add_hotel/_partials/htl-images-list.tpl @@ -0,0 +1,47 @@ +{** +* 2010-2022 Webkul. +* +* NOTICE OF LICENSE +* +* All right is reserved, +* Please go through LICENSE.txt file inside our module +* +* DISCLAIMER +* +* Do not edit or add to this file if you wish to upgrade this module to newer +* versions in the future. If you wish to customize this module for your +* needs please refer to CustomizationPolicy.txt file inside our module for more information. +* +* @author Webkul IN +* @copyright 2010-2022 Webkul IN +* @license LICENSE.txt +*} + +
+ + + + + + + + + + + {if isset($hotelImages) && $hotelImages} + {foreach from=$hotelImages item=image name=hotelImage} + {include file="./htl-images-list-row.tpl"} + {/foreach} + {else} + + + + {/if} + +
{l s='Image Id' mod='hotelreservationsystem'}{l s='Image' mod='hotelreservationsystem'}{l s='Cover' mod='hotelreservationsystem'}{l s='Action' mod='hotelreservationsystem'}
+
+ + {l s='No Image Found' mod='hotelreservationsystem'} +
+
+
\ No newline at end of file diff --git a/modules/hotelreservationsystem/views/templates/admin/add_hotel/helpers/form/form.tpl b/modules/hotelreservationsystem/views/templates/admin/add_hotel/helpers/form/form.tpl index 37e41b5f2..57e6d3c6e 100644 --- a/modules/hotelreservationsystem/views/templates/admin/add_hotel/helpers/form/form.tpl +++ b/modules/hotelreservationsystem/views/templates/admin/add_hotel/helpers/form/form.tpl @@ -304,53 +304,7 @@

{l s='Hotel Images' mod='hotelreservationsystem'}

-
- - - - - - - - - - - {if isset($hotelImages) && $hotelImages} - {foreach from=$hotelImages item=image name=hotelImage} - - - - - - - {/foreach} - {else} - - - - {/if} - -
{l s='Image Id' mod='hotelreservationsystem'}{l s='Image' mod='hotelreservationsystem'}{l s='Cover' mod='hotelreservationsystem'}{l s='Action' mod='hotelreservationsystem'}
{$image.id|escape:'html':'UTF-8'} - - - - - - {if $image.cover == 1} - - {else} - - {/if} - - - -
-
- - {l s='No Image Found' mod='hotelreservationsystem'} -
-
-
+ {include file="../../_partials/htl-images-list.tpl"}
{else}