Skip to content

Commit 8e9556d

Browse files
authored
Merge pull request #345 from abhishek-webkul/gli-728
Improve room type duplication feature
2 parents 7573d29 + 13487b3 commit 8e9556d

File tree

5 files changed

+418
-16
lines changed

5 files changed

+418
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{**
2+
* 2010-2021 Webkul.
3+
*
4+
* NOTICE OF LICENSE
5+
*
6+
* All right is reserved,
7+
* Please go through LICENSE.txt file inside our module
8+
*
9+
* DISCLAIMER
10+
*
11+
* Do not edit or add to this file if you wish to upgrade this module to newer
12+
* versions in the future. If you wish to customize this module for your
13+
* needs please refer to CustomizationPolicy.txt file inside our module for more information.
14+
*
15+
* @author Webkul IN
16+
* @copyright 2010-2021 Webkul IN
17+
* @license LICENSE.txt
18+
*}
19+
20+
{if isset($hotels_info)}
21+
<div class="modal-body text-center">
22+
<div class="row">
23+
<div class="col-lg-12">
24+
<form class="defaultForm form-horizontal duplicate-options" action="{$action|escape:'html':'UTF-8'}" method="post" enctype="multipart/form-data" novalidate>
25+
<input type="hidden" name="id_product" value="0" id="duplicate_id_product">
26+
<div class="form-group">
27+
<label class="control-label col-lg-3">
28+
<span class="label-tooltip" data-toggle="tooltip" data-html="true"
29+
title="{l s="Select hotel to assign duplicated room type to."}">
30+
{l s="Select hotel"}
31+
</span>
32+
</label>
33+
<div class="col-lg-9">
34+
<select name="id_hotel" class="fixed-width-xl" id="duplicate_id_hotel">
35+
{foreach $hotels_info as $hotel}
36+
<option value="{$hotel.id_hotel}">
37+
{$hotel.hotel_name|escape:'html':'UTF-8'} - {$hotel.city|escape:'html':'UTF-8'}
38+
</option>
39+
{/foreach}
40+
</select>
41+
</div>
42+
</div>
43+
44+
<div class="form-group">
45+
<label class="control-label col-lg-3">
46+
<span class="label-tooltip" data-toggle="tooltip" data-html="true"
47+
title="{l s="Choose whether to copy images to duplicated room type."}">
48+
{l s="Duplicate images"}
49+
</span>
50+
</label>
51+
<div class="col-lg-9">
52+
<span class="switch prestashop-switch fixed-width-lg">
53+
<input type="radio" name="noimage" id="noimage_on" value="0"
54+
{if $duplicate_images|intval}checked="checked"{/if}>
55+
<label for="noimage_on">{l s="Yes"}</label>
56+
<input type="radio" name="noimage" id="noimage_off" value="1"
57+
{if !$duplicate_images|intval}checked="checked"{/if}>
58+
<label for="noimage_off">{l s="No"}</label>
59+
<a class="slide-button btn"></a>
60+
</span>
61+
</div>
62+
</div>
63+
<input type="hidden" name="submitDuplicate" value="1">
64+
</form>
65+
</div>
66+
</div>
67+
</div>
68+
{/if}

controllers/admin/AdminProductsController.php

+130-16
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,6 @@ public function ajaxProcessAddAttachment()
724724
}
725725
}
726726

727-
728727
/**
729728
* Attach an existing attachment to the product
730729
*
@@ -758,17 +757,63 @@ public function processDuplicate()
758757
unset($product->id_product);
759758
$product->indexed = 0;
760759
$product->active = 0;
760+
761+
// suffix 'Duplicate' if same hotel
762+
$id_hotel_new = Tools::getValue('id_hotel');
763+
$obj_hotel_room_type = new HotelRoomType();
764+
$room_type_info = $obj_hotel_room_type->getRoomTypeInfoByIdProduct($id_product_old);
765+
if ($room_type_info && $room_type_info['id_hotel'] == $id_hotel_new) {
766+
foreach (Language::getLanguages(true) as $language) {
767+
$product->name[$language['id_lang']] = $product->name[$language['id_lang']].
768+
' - '.$this->l('Duplicate');
769+
}
770+
}
771+
772+
// update lang fields
773+
foreach (Language::getLanguages(true) as $language) {
774+
$product->link_rewrite[$language['id_lang']] = Tools::str2url($product->name[$language['id_lang']]);
775+
}
761776
if ($product->add()
762-
&& Category::duplicateProductCategories($id_product_old, $product->id)
763-
&& Product::duplicateSuppliers($id_product_old, $product->id)
764-
&& ($combination_images = Product::duplicateAttributes($id_product_old, $product->id)) !== false
765-
&& GroupReduction::duplicateReduction($id_product_old, $product->id)
766-
&& Product::duplicateAccessories($id_product_old, $product->id)
767-
&& Product::duplicateFeatures($id_product_old, $product->id)
768-
&& Pack::duplicate($id_product_old, $product->id)
769-
&& Product::duplicateCustomizationFields($id_product_old, $product->id)
770-
&& Product::duplicateTags($id_product_old, $product->id)
771-
&& Product::duplicateDownload($id_product_old, $product->id)) {
777+
// && Category::duplicateProductCategories($id_product_old, $product->id)
778+
&& Product::duplicateSuppliers($id_product_old, $product->id)
779+
&& ($combination_images = Product::duplicateAttributes($id_product_old, $product->id)) !== false
780+
&& GroupReduction::duplicateReduction($id_product_old, $product->id)
781+
&& Product::duplicateAccessories($id_product_old, $product->id)
782+
&& Product::duplicateFeatures($id_product_old, $product->id)
783+
&& Pack::duplicate($id_product_old, $product->id)
784+
&& Product::duplicateCustomizationFields($id_product_old, $product->id)
785+
&& Product::duplicateTags($id_product_old, $product->id)
786+
&& Product::duplicateDownload($id_product_old, $product->id)
787+
) {
788+
$obj_hotel_room_type = new HotelRoomType();
789+
$room_type_info = $obj_hotel_room_type->getRoomTypeInfoByIdProduct($id_product_old);
790+
$id_room_type_old = $room_type_info['id'];
791+
if (!$id_hotel_new) {
792+
$id_hotel_new = $room_type_info['id_hotel'];
793+
}
794+
$id_room_type_new = HotelRoomType::duplicateRoomType(
795+
$id_product_old,
796+
$product->id,
797+
$id_hotel_new,
798+
true
799+
);
800+
if ($id_room_type_new) {
801+
if (!HotelRoomType::duplicateRooms(
802+
$id_product_old,
803+
$id_room_type_new,
804+
$product->id,
805+
$id_hotel_new
806+
)) {
807+
$this->errors[] = Tools::displayError('An error occurred while duplicating rooms.');
808+
}
809+
if (!HotelRoomTypeDemand::duplicateRoomTypeDemands($id_product_old, $product->id)) {
810+
$this->errors[] = Tools::displayError(
811+
'An error occurred while duplicating additional facilities.'
812+
);
813+
}
814+
} else {
815+
$this->errors[] = Tools::displayError('An error occurred while duplicating room type.');
816+
}
772817
if ($product->hasAttributes()) {
773818
Product::updateDefaultAttribute($product->id);
774819
} else {
@@ -1540,6 +1585,10 @@ public function postProcess()
15401585
parent::postProcess();
15411586
}
15421587

1588+
$this->addJS(array(
1589+
_PS_JS_DIR_.'admin/products.js',
1590+
));
1591+
15431592
if ($this->display == 'edit' || $this->display == 'add') {
15441593
$this->addJqueryUI(array(
15451594
'ui.core',
@@ -1558,7 +1607,6 @@ public function postProcess()
15581607
));
15591608

15601609
$this->addJS(array(
1561-
_PS_JS_DIR_.'admin/products.js',
15621610
_PS_JS_DIR_.'admin/attributes.js',
15631611
_PS_JS_DIR_.'admin/price.js',
15641612
_PS_JS_DIR_.'tiny_mce/tiny_mce.js',
@@ -2734,6 +2782,12 @@ public function renderList()
27342782
return parent::renderList();
27352783
}
27362784

2785+
public function displayDuplicateLink($token = null, $id, $name = null)
2786+
{
2787+
return '<a href="#" title="'.$this->l('Duplicate').'"
2788+
onclick="initDuplicateRoomType('.(int)$id.');return false;"><i class="icon-copy"></i>'.$this->l('Duplicate').'</a>';
2789+
}
2790+
27372791
public function ajaxProcessProductManufacturers()
27382792
{
27392793
$manufacturers = Manufacturer::getManufacturers(false, 0, true, false, false, false, true);
@@ -2843,10 +2897,7 @@ public function initPageHeaderToolbar()
28432897
);
28442898
}
28452899

2846-
$js = (bool)Image::getImages($this->context->language->id, (int)$product->id) ?
2847-
'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'.'\')'
2848-
:
2849-
'document.location = \''.$this->context->link->getAdminLink('AdminProducts', true).'&id_product='.(int)$product->id.'&duplicateproduct&noimage=1'.'\'';
2900+
$js = 'initDuplicateRoomType('.(int)$product->id.');return false;';
28502901

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

2936+
public function initModal()
2937+
{
2938+
parent::initModal();
2939+
$this->modals[] = $this->getModalDuplicateOptions();
2940+
}
2941+
28852942
public function initToolbar()
28862943
{
28872944
parent::initToolbar();
@@ -5427,6 +5484,50 @@ public function initFormFeatures($obj)
54275484
}
54285485
$this->tpl_form_vars['custom_form'] = $data->fetch();
54295486
}
5487+
5488+
public function getModalDuplicateOptions()
5489+
{
5490+
$tpl = $this->createTemplate('modal-duplicate-options.tpl');
5491+
$idsHotel = HotelBranchInformation::getProfileAccessedHotels($this->context->employee->id_profile, 1, 1);
5492+
$hotelsInfo = array();
5493+
foreach ($idsHotel as $idHotel) {
5494+
$objHotelBranchInfo = new HotelBranchInformation($idHotel, $this->context->language->id);
5495+
if (Validate::isLoadedObject($objHotelBranchInfo)) {
5496+
$hotelInfo = array(
5497+
'id_hotel' => $objHotelBranchInfo->id,
5498+
'hotel_name' => $objHotelBranchInfo->hotel_name,
5499+
'rating' => $objHotelBranchInfo->rating,
5500+
'city' => $objHotelBranchInfo->city,
5501+
);
5502+
$hotelsInfo[] = $hotelInfo;
5503+
}
5504+
}
5505+
$formAction = $this->context->link->getAdminLink('AdminProducts', true).'&duplicateproduct';
5506+
$tpl->assign(array(
5507+
'action' => $formAction,
5508+
'hotels_info' => $hotelsInfo,
5509+
'duplicate_images' => 1,
5510+
));
5511+
5512+
$modalActions = array(
5513+
array(
5514+
'type' => 'button',
5515+
'value' => 'submitDuplicate',
5516+
'class' => 'btn-primary submit-duplicate',
5517+
'label' => $this->l('Submit'),
5518+
),
5519+
);
5520+
5521+
// set modal options
5522+
$modal = array(
5523+
'modal_id' => 'modal-duplicate-options',
5524+
'modal_class' => 'modal-md',
5525+
'modal_title' => $this->l('Duplication options'),
5526+
'modal_content' => $tpl->fetch(),
5527+
'modal_actions' => $modalActions,
5528+
);
5529+
return $modal;
5530+
}
54305531

54315532
public function ajaxProcessProductQuantity()
54325533
{
@@ -5746,6 +5847,19 @@ public function ajaxProcessPublishProduct()
57465847
}
57475848
}
57485849

5850+
public function ajaxProcessGetIdHotelByIdProduct()
5851+
{
5852+
$response = array('status' => 'failed');
5853+
$idProduct = Tools::getValue('id_product');
5854+
$objHotelRoomType = new HotelRoomType();
5855+
$roomTypeInfo = $objHotelRoomType->getRoomTypeInfoByIdProduct($idProduct);
5856+
if ($roomTypeInfo) {
5857+
$response['status'] = 'success';
5858+
$response['id_hotel'] = (int)$roomTypeInfo['id_hotel'];
5859+
}
5860+
die(json_encode($response));
5861+
}
5862+
57495863
public function processImageLegends()
57505864
{
57515865
if (Tools::getValue('key_tab') == 'Images' && Tools::getValue('submitAddproductAndStay') == 'update_legends' && Validate::isLoadedObject($product = new Product((int)Tools::getValue('id_product')))) {

js/admin/products.js

+26
Original file line numberDiff line numberDiff line change
@@ -1883,7 +1883,29 @@ var ProductMultishop = new function()
18831883
var tabs_manager = new ProductTabsManager();
18841884
tabs_manager.setTabs(product_tabs);
18851885

1886+
function initDuplicateRoomType(idProduct) {
1887+
$('#modal-duplicate-options').modal('show');
1888+
$('#modal-duplicate-options #duplicate_id_product').attr('value', idProduct);
1889+
var url = 'index.php?controller=AdminProducts&token=' + token;
1890+
var data = {
1891+
ajax: true,
1892+
action: 'getIdHotelByIdProduct',
1893+
id_product: parseInt(idProduct),
1894+
};
1895+
1896+
$.post(url, data,
1897+
function(response) {
1898+
if (response.status == 'success') {
1899+
var idHotel = response.id_hotel;
1900+
$('#modal-duplicate-options #duplicate_id_hotel option[value="'+idHotel+'"]').attr('selected', 'selected');
1901+
}
1902+
},
1903+
'JSON'
1904+
);
1905+
}
1906+
18861907
$(document).ready(function() {
1908+
id_lang_default = default_language;
18871909
// The manager schedules the onReady() methods of each tab to be called when the tab is loaded
18881910
tabs_manager.init();
18891911
updateCurrentText();
@@ -1908,4 +1930,8 @@ $(document).ready(function() {
19081930
$('#selectedCarriers option').attr('selected', 'selected');
19091931
$('#selectAttachment1 option').attr('selected', 'selected');
19101932
});
1933+
1934+
$('#modal-duplicate-options .submit-duplicate').on('click', function (e) {
1935+
$('#modal-duplicate-options form.duplicate-options').submit();
1936+
});
19111937
});

0 commit comments

Comments
 (0)