[-] BO : fix #PSTEST-1128 - when saving a pack, a validation error causes pack data loss

This commit is contained in:
tDidierjean
2012-04-04 12:38:42 +00:00
parent 91e7097652
commit dffaf9b621
5 changed files with 95 additions and 23 deletions
@@ -91,6 +91,7 @@
var empty_link_rewrite_msg = '{l s='Friendly URL is empty. You need to enter a friendly URL at least for the default language before you can save the product.' slashes=1}';
$('#product-tab-content-wait').show();
var post_data = {$post_data};
var save_error = {if $save_error}true{else}false{/if};
var product_type = {$product_type};
{*var mce_maximum = '{l s='Maximum'}';
@@ -57,14 +57,14 @@
{l s='Add this product to the pack'}
</span>
<p class="product_description listOfPack" style="display:{if count($product->packItems) > 0}block{else}none{/if};text-align: left;">
<p class="product_description listOfPack" style="display:{if count($pack_items) > 0}block{else}none{/if};text-align: left;">
<br />{l s='List of products for that pack:'}
</p>
<div id="divPackItems">
{foreach from=$product->packItems item=packItem}
{$packItem->pack_quantity} x {$packItem->name}
<span class="delPackItem" name="{$packItem->id}" style="cursor: pointer;">
{foreach $pack_items as $pack_item}
{$pack_item.pack_quantity} x {$pack_item.name}
<span class="delPackItem" name="{$pack_item.id}" style="cursor: pointer;">
<img src="../img/admin/delete.gif" />
</span><br />
{/foreach}
+14
View File
@@ -31,6 +31,13 @@ class PackCore extends Product
protected static $cacheIsPack = array();
protected static $cacheIsPacked = array();
/**
* Is product a pack?
*
* @static
* @param $id_product
* @return bool
*/
public static function isPack($id_product)
{
if (!Pack::isFeatureActive())
@@ -47,6 +54,13 @@ class PackCore extends Product
return self::$cacheIsPack[$id_product];
}
/**
* Is product in a pack?
*
* @static
* @param $id_product
* @return bool
*/
public static function isPacked($id_product)
{
if (!Pack::isFeatureActive())
+70 -19
View File
@@ -1965,6 +1965,7 @@ class AdminProductsControllerCore extends AdminController
}
// @todo module free
$this->tpl_form_vars['vat_number'] = file_exists(_PS_MODULE_DIR_.'vatnumber/ajax.php');
parent::initContent();
}
@@ -2150,7 +2151,7 @@ class AdminProductsControllerCore extends AdminController
asort($this->available_tabs, SORT_NUMERIC);
$this->tpl_form_vars['tabs_preloaded'] = $this->available_tabs;
$this->tpl_form_vars['product_type'] = $product->getType();
$this->tpl_form_vars['product_type'] = (int)Tools::getValue('type_product', $product->getType());
// getLanguages init this->_languages
$this->getLanguages();
@@ -2164,6 +2165,7 @@ class AdminProductsControllerCore extends AdminController
$this->tpl_form_vars['token'] = $this->token;
$this->tpl_form_vars['combinationImagesJs'] = $this->getCombinationImagesJs();
$this->tpl_form_vars['post_data'] = Tools::jsonEncode($_POST);
$this->tpl_form_vars['save_error'] = !empty($this->errors);
// autoload rich text editor (tiny mce)
$this->tpl_form_vars['tinymce'] = true;
@@ -2687,34 +2689,83 @@ class AdminProductsControllerCore extends AdminController
$this->tpl_form_vars['custom_form'] = $data->fetch();
}
/**
* Get an array of pack items for display from the product object if specified, else from POST/GET values
*
* @param Product $product
* @return array of pack items
*/
public function getPackItems($product = null)
{
$pack_items = array();
if (!$product)
{
$names_input = Tools::getValue('namePackItems');
$ids_input = Tools::getValue('inputPackItems');
if (!$names_input || !$ids_input)
return array();
// ids is an array of string with format : QTYxID
$ids = array_unique(explode('-', $ids_input));
$names = array_unique(explode('¤', $names_input));
if (!empty($ids))
{
$length = count($ids);
for ($i = 0; $i < $length; $i++)
if (!empty($ids[$i]) && !empty($names[$i]))
{
list($pack_items[$i]['pack_quantity'], $pack_items[$i]['id']) = explode('x', $ids[$i]);
$exploded_name = explode('x', $names[$i]);
$pack_items[$i]['name'] = $exploded_name[1];
}
}
}
else
{
$i = 0;
foreach ($product->packItems as $pack_item)
{
$pack_items[$i]['id'] = $pack_item->id;
$pack_items[$i]['pack_quantity'] = $pack_item->pack_quantity;
$pack_items[$i]['name'] = $pack_item->name;
$i++;
}
}
return $pack_items;
}
public function initFormPack($product, $languages, $default_language)
{
$data = $this->createTemplate($this->tpl_form);
$product->packItems = Pack::getItems($product->id, $this->context->language->id);
$input_pack_items = '';
if (Tools::getValue('inputPackItems'))
$input_pack_items = Tools::getValue('inputPackItems');
else
foreach ($product->packItems as $pack_item)
$input_pack_items .= $pack_item->pack_quantity.'x'.$pack_item->id.'-';
$input_namepack_items = '';
// If pack items have been submitted, we want to display them instead of the actuel content of the pack
// in database. In case of a submit error, the posted data is not lost and can be sent again.
if (Tools::getValue('namePackItems'))
{
$input_pack_items = Tools::getValue('inputPackItems');
$input_namepack_items = Tools::getValue('namePackItems');
$pack_items = $this->getPackItems();
}
else
foreach ($product->packItems as $pack_item)
$input_namepack_items .= $pack_item->pack_quantity.' x '.$pack_item->name.'¤';
{
$product->packItems = Pack::getItems($product->id, $this->context->language->id);
$pack_items = $this->getPackItems($product);
$input_namepack_items = '';
$input_pack_items = '';
foreach ($pack_items as $pack_item)
{
$input_pack_items .= $pack_item['pack_quantity'].'x'.$pack_item['id'].'-';
$input_namepack_items .= $pack_item['pack_quantity'].' x '.$pack_item['name'].'¤';
}
}
$data->assign(array(
'product' => $product,
'languages' => $languages,
'default_language' => $default_language,
'ps_ssl_enabled' => Configuration::get('PS_SSL_ENABLED'),
'input_pack_items' => $input_pack_items,
'input_namepack_items' => $input_namepack_items,
'product_type' => $product->getType()
'pack_items' => $pack_items,
'product_type' => (int)Tools::getValue('type_product', $product->getType())
));
$this->tpl_form_vars['custom_form'] = $data->fetch();
@@ -3147,7 +3198,7 @@ class AdminProductsControllerCore extends AdminController
$product->tags = Tag::getProductTags($product->id);
$data->assign('product_type', $product->getType());
$data->assign('product_type', (int)Tools::getValue('type_product', $product->getType()));
// TinyMCE
$iso_tiny_mce = $this->context->language->iso_code;
+6
View File
@@ -82,11 +82,17 @@ function ProductTabsManager(){
if (selected)
$('#product-tab-content-wait').show();
// send $_POST array with the request to be able to retrieve posted data if there was an error while saving product
var data;
if (save_error)
data = post_data;
$.ajax({
url : myurl,
async : true,
cache: false, // cache needs to be set to false or IE will cache the page with outdated product values
type: 'POST',
data: data,
success : function(data)
{
tab_selector.html(data);