web interface for importing and exporting products. Additional tests for the import/export API

This commit is contained in:
Colin Kuskie 2008-06-05 04:10:48 +00:00
parent 9a9e94a0de
commit 994da561a5
7 changed files with 264 additions and 88 deletions

View file

@ -141,26 +141,32 @@ sub importProducts {
}
##Okay, if we got this far, then the data looks fine.
return unless scalar @productData;
my $fetchProductId = $session->db->prepare('select assetId from Product where mastersku=? order by revisionDate DESC limit 1');
my $fetchProductId = $session->db->prepare('select p.assetId from Product as p join sku as s on p.assetId=s.assetId and p.revisionDate=s.revisionDate where s.sku=? order by p.revisionDate DESC limit 1');
my $node = WebGUI::Asset::Sku::Product->getProductImportNode($session);
@headers = map { $_ eq 'shortdescription' ? 'shortdesc' : $_ } @headers;
PRODUCT: foreach my $productRow (@productData) {
my %productRow;
##Order the data according to the headers, in whatever order they exist.
@productRow{ @headers } = @{ $productRow };
$fetchProductId->execute([$productRow->{mastersku}]);
my ($assetId) = $fetchProductId->hashRef->{assetId};
$fetchProductId->execute([$productRow{mastersku}]);
my $asset = $fetchProductId->hashRef;
##If the assetId exists, we update data for it
if ($assetId) {
if ($asset->{assetId}) {
$session->log->warn("Modifying an existing product: $productRow{sku} = $asset->{assetId}\n");
my $assetId = $asset->{assetId};
my $product = WebGUI::Asset->newPending($session, $assetId);
if ($productRow{title} ne $product->getTitle) {
$product->update({ title => $product->fixTitle($productRow{title}) });
}
##Error handling for locked assets
$session->log->warn("Product is locked") if $product->isLocked;
delete $productRow{ title };
delete $productRow{ mastersku };
next PRODUCT if $product->isLocked;
my $collaterals = $product->getAllCollateral('variantsJSON');
my $collateralSet = 0;
ROW: foreach my $collateral (@{ $collaterals }) {
next ROW unless $collateral->{sku} eq $productRow->{sku};
next ROW unless $collateral->{sku} eq $productRow{sku};
@{ $collateral}{@headers} = @productRow{ @headers };
$product->setCollateral('variantsJSON', 'variantId', $collateral->{variantId}, $collateral);
$collateralSet=1;
@ -172,12 +178,96 @@ sub importProducts {
}
else {
##Insert a new product;
$session->log->warn("Making a new product: $productRow{sku}\n");
my $newProduct = $node->addChild({className => 'WebGUI::Asset::Sku::Product'});
$newProduct->update({ title => $newProduct->fixTitle($productRow{title}) });
$newProduct->update({
title => $newProduct->fixTitle($productRow{title}),
sku => $productRow{mastersku},
});
delete $productRow{ title };
delete $productRow{ mastersku };
$newProduct->setCollateral('variantsJSON', 'variantId', 'new', \%productRow);
$newProduct->commit;
}
}
return 1;
}
#-------------------------------------------------------------------
=head2 www_exportProducts ( )
Export all product SKUs as a CSV file. Returns a WebGUI::Storage
object containg the product file, named 'siteProductData.csv'.
=cut
sub www_exportProducts {
my $self = shift;
my $session = $self->session;
my $admin = WebGUI::Shop::Admin->new($session);
return $session->privilege->insufficient
unless $admin->canManage;
my $storage = $self->exportProducts();
$self->session->http->setRedirect($storage->getUrl($storage->getFiles->[0]));
return "redirect";
}
#-------------------------------------------------------------------
=head2 www_importProducts ( )
Import new product data from a file provided by the user. This will create new products
or alter existing products.
=cut
sub www_importProducts {
my $self = shift;
my $session = $self->session;
my $admin = WebGUI::Shop::Admin->new($session);
return $session->privilege->insufficient
unless $admin->canManage;
my $storage = WebGUI::Storage->create($session);
my $productFile = $storage->addFileFromFormPost('importFile', 1);
$self->importProducts($storage->getPath($productFile)) if $productFile;
return $self->www_manage;
}
#-------------------------------------------------------------------
=head2 www_manage ( )
User interface to synchronize product data. Provides an interface for
exporting all products on the site, and importing sets of products.
=cut
sub www_manage {
my $self = shift;
my $session = $self->session;
my $admin = WebGUI::Shop::Admin->new($session);
return $session->privilege->insufficient
unless $admin->canManage;
##YUI specific datatable CSS
my ($style, $url) = $session->quick(qw(style url));
##Default CSS
$style->setRawHeadTags('<style type="text/css"> #paging a { color: #0000de; } #search, #export form { display: inline; } </style>');
my $i18n=WebGUI::International->new($session, 'Shop');
my $exportForm = WebGUI::Form::formHeader($session,{action => $url->page('shop=products;method=exportProducts')})
. WebGUI::Form::submit($session,{value=>$i18n->get('export'), extras=>q{style="float: left;"} })
. WebGUI::Form::formFooter($session);
my $importForm = WebGUI::Form::formHeader($session,{action => $url->page('shop=products;method=importProducts')})
. WebGUI::Form::submit($session,{value=>$i18n->get('import'), extras=>q{style="float: left;"} })
. q{<input type="file" name="importFile" size="10" />}
. WebGUI::Form::formFooter($session);
my $output =sprintf <<EODIV, $exportForm, $importForm;
<div id="importExport">%s%s</div>
EODIV
return $admin->getAdminConsole->render($output, $i18n->get('products'));
}
1;

View file

@ -529,10 +529,10 @@ sub www_manage {
my $i18n=WebGUI::International->new($session, 'Tax');
my $exportForm = WebGUI::Form::formHeader($session,{action => $url->page('shop=tax;method=exportTax')})
. WebGUI::Form::submit($session,{value=>$i18n->get('export'), extras=>q{style="float: left;"} })
. WebGUI::Form::submit($session,{value=>$i18n->get('export','Shop'), extras=>q{style="float: left;"} })
. WebGUI::Form::formFooter($session);
my $importForm = WebGUI::Form::formHeader($session,{action => $url->page('shop=tax;method=importTax')})
. WebGUI::Form::submit($session,{value=>$i18n->get('import'), extras=>q{style="float: left;"} })
. WebGUI::Form::submit($session,{value=>$i18n->get('import','Shop'), extras=>q{style="float: left;"} })
. q{<input type="file" name="importFile" size="10" />}
. WebGUI::Form::formFooter($session);