Skip to content

Commit

Permalink
Merge pull request Qloapps#345 from abhishek-webkul/gli-728
Browse files Browse the repository at this point in the history
Improve room type duplication feature
  • Loading branch information
rohit053 authored May 5, 2022
2 parents 7573d29 + 13487b3 commit 8e9556d
Show file tree
Hide file tree
Showing 5 changed files with 418 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{**
* 2010-2021 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-2021 Webkul IN
* @license LICENSE.txt
*}

{if isset($hotels_info)}
<div class="modal-body text-center">
<div class="row">
<div class="col-lg-12">
<form class="defaultForm form-horizontal duplicate-options" action="{$action|escape:'html':'UTF-8'}" method="post" enctype="multipart/form-data" novalidate>
<input type="hidden" name="id_product" value="0" id="duplicate_id_product">
<div class="form-group">
<label class="control-label col-lg-3">
<span class="label-tooltip" data-toggle="tooltip" data-html="true"
title="{l s="Select hotel to assign duplicated room type to."}">
{l s="Select hotel"}
</span>
</label>
<div class="col-lg-9">
<select name="id_hotel" class="fixed-width-xl" id="duplicate_id_hotel">
{foreach $hotels_info as $hotel}
<option value="{$hotel.id_hotel}">
{$hotel.hotel_name|escape:'html':'UTF-8'} - {$hotel.city|escape:'html':'UTF-8'}
</option>
{/foreach}
</select>
</div>
</div>

<div class="form-group">
<label class="control-label col-lg-3">
<span class="label-tooltip" data-toggle="tooltip" data-html="true"
title="{l s="Choose whether to copy images to duplicated room type."}">
{l s="Duplicate images"}
</span>
</label>
<div class="col-lg-9">
<span class="switch prestashop-switch fixed-width-lg">
<input type="radio" name="noimage" id="noimage_on" value="0"
{if $duplicate_images|intval}checked="checked"{/if}>
<label for="noimage_on">{l s="Yes"}</label>
<input type="radio" name="noimage" id="noimage_off" value="1"
{if !$duplicate_images|intval}checked="checked"{/if}>
<label for="noimage_off">{l s="No"}</label>
<a class="slide-button btn"></a>
</span>
</div>
</div>
<input type="hidden" name="submitDuplicate" value="1">
</form>
</div>
</div>
</div>
{/if}
146 changes: 130 additions & 16 deletions controllers/admin/AdminProductsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,6 @@ public function ajaxProcessAddAttachment()
}
}


/**
* Attach an existing attachment to the product
*
Expand Down Expand Up @@ -758,17 +757,63 @@ public function processDuplicate()
unset($product->id_product);
$product->indexed = 0;
$product->active = 0;

// suffix 'Duplicate' if same hotel
$id_hotel_new = Tools::getValue('id_hotel');
$obj_hotel_room_type = new HotelRoomType();
$room_type_info = $obj_hotel_room_type->getRoomTypeInfoByIdProduct($id_product_old);
if ($room_type_info && $room_type_info['id_hotel'] == $id_hotel_new) {
foreach (Language::getLanguages(true) as $language) {
$product->name[$language['id_lang']] = $product->name[$language['id_lang']].
' - '.$this->l('Duplicate');
}
}

// update lang fields
foreach (Language::getLanguages(true) as $language) {
$product->link_rewrite[$language['id_lang']] = Tools::str2url($product->name[$language['id_lang']]);
}
if ($product->add()
&& Category::duplicateProductCategories($id_product_old, $product->id)
&& Product::duplicateSuppliers($id_product_old, $product->id)
&& ($combination_images = Product::duplicateAttributes($id_product_old, $product->id)) !== false
&& GroupReduction::duplicateReduction($id_product_old, $product->id)
&& Product::duplicateAccessories($id_product_old, $product->id)
&& Product::duplicateFeatures($id_product_old, $product->id)
&& Pack::duplicate($id_product_old, $product->id)
&& Product::duplicateCustomizationFields($id_product_old, $product->id)
&& Product::duplicateTags($id_product_old, $product->id)
&& Product::duplicateDownload($id_product_old, $product->id)) {
// && Category::duplicateProductCategories($id_product_old, $product->id)
&& Product::duplicateSuppliers($id_product_old, $product->id)
&& ($combination_images = Product::duplicateAttributes($id_product_old, $product->id)) !== false
&& GroupReduction::duplicateReduction($id_product_old, $product->id)
&& Product::duplicateAccessories($id_product_old, $product->id)
&& Product::duplicateFeatures($id_product_old, $product->id)
&& Pack::duplicate($id_product_old, $product->id)
&& Product::duplicateCustomizationFields($id_product_old, $product->id)
&& Product::duplicateTags($id_product_old, $product->id)
&& Product::duplicateDownload($id_product_old, $product->id)
) {
$obj_hotel_room_type = new HotelRoomType();
$room_type_info = $obj_hotel_room_type->getRoomTypeInfoByIdProduct($id_product_old);
$id_room_type_old = $room_type_info['id'];
if (!$id_hotel_new) {
$id_hotel_new = $room_type_info['id_hotel'];
}
$id_room_type_new = HotelRoomType::duplicateRoomType(
$id_product_old,
$product->id,
$id_hotel_new,
true
);
if ($id_room_type_new) {
if (!HotelRoomType::duplicateRooms(
$id_product_old,
$id_room_type_new,
$product->id,
$id_hotel_new
)) {
$this->errors[] = Tools::displayError('An error occurred while duplicating rooms.');
}
if (!HotelRoomTypeDemand::duplicateRoomTypeDemands($id_product_old, $product->id)) {
$this->errors[] = Tools::displayError(
'An error occurred while duplicating additional facilities.'
);
}
} else {
$this->errors[] = Tools::displayError('An error occurred while duplicating room type.');
}
if ($product->hasAttributes()) {
Product::updateDefaultAttribute($product->id);
} else {
Expand Down Expand Up @@ -1540,6 +1585,10 @@ public function postProcess()
parent::postProcess();
}

$this->addJS(array(
_PS_JS_DIR_.'admin/products.js',
));

if ($this->display == 'edit' || $this->display == 'add') {
$this->addJqueryUI(array(
'ui.core',
Expand All @@ -1558,7 +1607,6 @@ public function postProcess()
));

$this->addJS(array(
_PS_JS_DIR_.'admin/products.js',
_PS_JS_DIR_.'admin/attributes.js',
_PS_JS_DIR_.'admin/price.js',
_PS_JS_DIR_.'tiny_mce/tiny_mce.js',
Expand Down Expand Up @@ -2734,6 +2782,12 @@ public function renderList()
return parent::renderList();
}

public function displayDuplicateLink($token = null, $id, $name = null)
{
return '<a href="#" title="'.$this->l('Duplicate').'"
onclick="initDuplicateRoomType('.(int)$id.');return false;"><i class="icon-copy"></i>'.$this->l('Duplicate').'</a>';
}

public function ajaxProcessProductManufacturers()
{
$manufacturers = Manufacturer::getManufacturers(false, 0, true, false, false, false, true);
Expand Down Expand Up @@ -2843,10 +2897,7 @@ public function initPageHeaderToolbar()
);
}

$js = (bool)Image::getImages($this->context->language->id, (int)$product->id) ?
'confirm_link(\'\', \''.$this->l('This will copy the images too. If you wish to proceed, click "Yes". If not, click "No".', null, true, false).'\', \''.$this->l('Yes', null, true, false).'\', \''.$this->l('No', null, true, false).'\', \''.$this->context->link->getAdminLink('AdminProducts', true).'&id_product='.(int)$product->id.'&duplicateproduct'.'\', \''.$this->context->link->getAdminLink('AdminProducts', true).'&id_product='.(int)$product->id.'&duplicateproduct&noimage=1'.'\')'
:
'document.location = \''.$this->context->link->getAdminLink('AdminProducts', true).'&id_product='.(int)$product->id.'&duplicateproduct&noimage=1'.'\'';
$js = 'initDuplicateRoomType('.(int)$product->id.');return false;';

// adding button for duplicate this product
if ($this->tabAccess['add']) {
Expand Down Expand Up @@ -2882,6 +2933,12 @@ public function initPageHeaderToolbar()
parent::initPageHeaderToolbar();
}

public function initModal()
{
parent::initModal();
$this->modals[] = $this->getModalDuplicateOptions();
}

public function initToolbar()
{
parent::initToolbar();
Expand Down Expand Up @@ -5427,6 +5484,50 @@ public function initFormFeatures($obj)
}
$this->tpl_form_vars['custom_form'] = $data->fetch();
}

public function getModalDuplicateOptions()
{
$tpl = $this->createTemplate('modal-duplicate-options.tpl');
$idsHotel = HotelBranchInformation::getProfileAccessedHotels($this->context->employee->id_profile, 1, 1);
$hotelsInfo = array();
foreach ($idsHotel as $idHotel) {
$objHotelBranchInfo = new HotelBranchInformation($idHotel, $this->context->language->id);
if (Validate::isLoadedObject($objHotelBranchInfo)) {
$hotelInfo = array(
'id_hotel' => $objHotelBranchInfo->id,
'hotel_name' => $objHotelBranchInfo->hotel_name,
'rating' => $objHotelBranchInfo->rating,
'city' => $objHotelBranchInfo->city,
);
$hotelsInfo[] = $hotelInfo;
}
}
$formAction = $this->context->link->getAdminLink('AdminProducts', true).'&duplicateproduct';
$tpl->assign(array(
'action' => $formAction,
'hotels_info' => $hotelsInfo,
'duplicate_images' => 1,
));

$modalActions = array(
array(
'type' => 'button',
'value' => 'submitDuplicate',
'class' => 'btn-primary submit-duplicate',
'label' => $this->l('Submit'),
),
);

// set modal options
$modal = array(
'modal_id' => 'modal-duplicate-options',
'modal_class' => 'modal-md',
'modal_title' => $this->l('Duplication options'),
'modal_content' => $tpl->fetch(),
'modal_actions' => $modalActions,
);
return $modal;
}

public function ajaxProcessProductQuantity()
{
Expand Down Expand Up @@ -5746,6 +5847,19 @@ public function ajaxProcessPublishProduct()
}
}

public function ajaxProcessGetIdHotelByIdProduct()
{
$response = array('status' => 'failed');
$idProduct = Tools::getValue('id_product');
$objHotelRoomType = new HotelRoomType();
$roomTypeInfo = $objHotelRoomType->getRoomTypeInfoByIdProduct($idProduct);
if ($roomTypeInfo) {
$response['status'] = 'success';
$response['id_hotel'] = (int)$roomTypeInfo['id_hotel'];
}
die(json_encode($response));
}

public function processImageLegends()
{
if (Tools::getValue('key_tab') == 'Images' && Tools::getValue('submitAddproductAndStay') == 'update_legends' && Validate::isLoadedObject($product = new Product((int)Tools::getValue('id_product')))) {
Expand Down
26 changes: 26 additions & 0 deletions js/admin/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -1883,7 +1883,29 @@ var ProductMultishop = new function()
var tabs_manager = new ProductTabsManager();
tabs_manager.setTabs(product_tabs);

function initDuplicateRoomType(idProduct) {
$('#modal-duplicate-options').modal('show');
$('#modal-duplicate-options #duplicate_id_product').attr('value', idProduct);
var url = 'index.php?controller=AdminProducts&token=' + token;
var data = {
ajax: true,
action: 'getIdHotelByIdProduct',
id_product: parseInt(idProduct),
};

$.post(url, data,
function(response) {
if (response.status == 'success') {
var idHotel = response.id_hotel;
$('#modal-duplicate-options #duplicate_id_hotel option[value="'+idHotel+'"]').attr('selected', 'selected');
}
},
'JSON'
);
}

$(document).ready(function() {
id_lang_default = default_language;
// The manager schedules the onReady() methods of each tab to be called when the tab is loaded
tabs_manager.init();
updateCurrentText();
Expand All @@ -1908,4 +1930,8 @@ $(document).ready(function() {
$('#selectedCarriers option').attr('selected', 'selected');
$('#selectAttachment1 option').attr('selected', 'selected');
});

$('#modal-duplicate-options .submit-duplicate').on('click', function (e) {
$('#modal-duplicate-options form.duplicate-options').submit();
});
});
Loading

0 comments on commit 8e9556d

Please sign in to comment.