Merge pull request #616 from shreesh-webkul/gli-1247

Resolved: Mulitple hotel cover image created when uploading multiple images
This commit is contained in:
Rohit Jain
2023-03-10 19:06:56 +05:30
committed by GitHub
6 changed files with 195 additions and 151 deletions

View File

@@ -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;
}

View File

@@ -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()

View File

@@ -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 += '<tr class="';
if(image.cover) {
html += 'cover-image-tr';
}
html += '">';
html += '<td class="text-center">'+image.id_image+'</td>';
html += '<td class="text-center">';
html += '<a class="htl-img-preview" href="'+image.image_url+'">';
html += '<img class="img-thumbnail" width="100" src="'+image.image_url+'"/>';
html += '</a>';
html += '</td>';
html += '<td class="text-center ';
if(image.cover) {
html += 'cover-image-td';
}
html += '">';
html += '<a href="#" class="';
if(image.cover) {
html += 'text-success';
} else {
html += 'text-danger';
}
html += ' changer-cover-image" data-id-hotel="'+idHotel+'" data-is-cover="';
if(image.cover) {
html += '1';
} else {
html += '0';
}
html += '" data-id-image="'+image.id_image+'">';
if(image.cover) {
html += '<i class="icon-check"></i>';
} else {
html += '<i class="icon-times"></i>';
}
html += '</a>';
html += '</td>';
html += '<td class="text-center">';
html += '<button type="button" class="btn btn-default delete-hotel-image" data-id-hotel="'+idHotel+'" data-is-cover="';
if(image.cover) {
html += '1';
} else {
html += '0';
}
html += '" data-id-image="'+image.id_image+'"><i class="icon-trash"></i></button>';
html += '</td>';
html += '</tr>';
$("#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');

View File

@@ -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
*}
<tr class="{if $image.cover == 1}cover-image-tr{/if}">
<td class="text-center">{$image.id|escape:'html':'UTF-8'}</td>
<td class="text-center">
<a class="htl-img-preview" href="{$image.image_link|escape:'html':'UTF-8'}">
<img class="img-thumbnail" width="100" src="{$image.image_link_small|escape:'html':'UTF-8'}"/>
</a>
</td>
<td class="text-center {if $image.cover == 1}cover-image-td{/if}">
<a href="#" class="{if $image.cover == 1}text-success{else}text-danger{/if} changer-cover-image" data-id-hotel="{$hotel_info.id|escape:'html':'UTF-8'}" data-is-cover="{if $image.cover == 1}1{else}0{/if}" data-id-image="{$image.id|escape:'html':'UTF-8'}">
{if $image.cover == 1}
<i class="icon-check"></i>
{else}
<i class="icon-times"></i>
{/if}
</a>
</td>
<td class="text-center">
<button type="button" class="btn btn-default delete-hotel-image" data-id-hotel="{$hotel_info.id|escape:'html':'UTF-8'}" data-is-cover="{if $image.cover == 1}1{else}0{/if}" data-id-image="{$image.id|escape:'html':'UTF-8'}"><i class="icon-trash"></i></button>
</td>
</tr>

View File

@@ -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
*}
<div class="table-responsive">
<table class="table" id="hotel-image-table">
<thead>
<tr>
<th class="text-center">{l s='Image Id' mod='hotelreservationsystem'}</th>
<th class="text-center">{l s='Image' mod='hotelreservationsystem'}</th>
<th class="text-center">{l s='Cover' mod='hotelreservationsystem'}</th>
<th class="text-center">{l s='Action' mod='hotelreservationsystem'}</th>
</tr>
</thead>
<tbody>
{if isset($hotelImages) && $hotelImages}
{foreach from=$hotelImages item=image name=hotelImage}
{include file="./htl-images-list-row.tpl"}
{/foreach}
{else}
<tr class="list-empty-tr">
<td class="list-empty" colspan="5">
<div class="list-empty-msg">
<i class="icon-warning-sign list-empty-icon"></i>
{l s='No Image Found' mod='hotelreservationsystem'}
</div>
</td>
</tr>
{/if}
</tbody>
</table>
</div>

View File

@@ -304,53 +304,7 @@
<h4><i class="icon-image"></i> <span>{l s='Hotel Images' mod='hotelreservationsystem'}</span></h4>
<div class="row">
<div class="col-sm-12">
<div class="table-responsive">
<table class="table" id="hotel-image-table">
<thead>
<tr>
<th class="text-center">{l s='Image Id' mod='hotelreservationsystem'}</th>
<th class="text-center">{l s='Image' mod='hotelreservationsystem'}</th>
<th class="text-center">{l s='Cover' mod='hotelreservationsystem'}</th>
<th class="text-center">{l s='Action' mod='hotelreservationsystem'}</th>
</tr>
</thead>
<tbody>
{if isset($hotelImages) && $hotelImages}
{foreach from=$hotelImages item=image name=hotelImage}
<tr class="{if $image.cover == 1}cover-image-tr{/if}">
<td class="text-center">{$image.id|escape:'html':'UTF-8'}</td>
<td class="text-center">
<a class="htl-img-preview" href="{$image.image_link|escape:'html':'UTF-8'}">
<img class="img-thumbnail" width="100" src="{$image.image_link_small|escape:'html':'UTF-8'}"/>
</a>
</td>
<td class="text-center {if $image.cover == 1}cover-image-td{/if}">
<a href="#" class="{if $image.cover == 1}text-success{else}text-danger{/if} changer-cover-image" data-id-hotel="{$hotel_info.id|escape:'html':'UTF-8'}" data-is-cover="{if $image.cover == 1}1{else}0{/if}" data-id-image="{$image.id|escape:'html':'UTF-8'}">
{if $image.cover == 1}
<i class="icon-check"></i>
{else}
<i class="icon-times"></i>
{/if}
</a>
</td>
<td class="text-center">
<button type="button" class="btn btn-default delete-hotel-image" data-id-hotel="{$hotel_info.id|escape:'html':'UTF-8'}" data-is-cover="{if $image.cover == 1}1{else}0{/if}" data-id-image="{$image.id|escape:'html':'UTF-8'}"><i class="icon-trash"></i></button>
</td>
</tr>
{/foreach}
{else}
<tr class="list-empty-tr">
<td class="list-empty" colspan="5">
<div class="list-empty-msg">
<i class="icon-warning-sign list-empty-icon"></i>
{l s='No Image Found' mod='hotelreservationsystem'}
</div>
</td>
</tr>
{/if}
</tbody>
</table>
</div>
{include file="../../_partials/htl-images-list.tpl"}
</div>
</div>
{else}