import tool for xls sheets
[bse.git] / site / util / bsexlsprod.pl
CommitLineData
97469012
TC
1#!perl -w
2use strict;
3use Getopt::Long;
4use FindBin;
5use lib "$FindBin::Bin/../cgi-bin/modules";
6use BSE::Cfg;
7use BSE::API qw(bse_cfg bse_make_product bse_encoding);
8use BSE::ProductImportXLS;
9use Carp qw(confess);
10
11chdir "$FindBin::Bin/../cgi-bin"
12 or warn "Could not change to cgi-bin directory: $!\n";
13
14my $verbose;
15my $delete;
16GetOptions("v", \$verbose,
17 "d", \$delete);
18$verbose = defined $verbose;
19
20my $cfg = bse_cfg();
21
22my $profile = shift;
23my $filename = shift
24 or die "Usage: $0 profile filename\n";
25
26my $importer = BSE::ProductImportXLS->new($cfg, $profile);
27
28my $callback;
29$verbose
30 and $callback = sub { print "@_\n" };
31
32$importer->process($filename, $callback);
33
34if ($delete) {
35 my @products = $importer->products;
36 my @catalogs = $importer->catalogs;
37 for my $product (@products) {
38 print "Removing product $product->{id}: $product->{title}\n";
39 $product->remove($cfg);
40 }
41 require BSE::Permissions;
42 my $perms = BSE::Permissions->new($cfg);
43 for my $catalog (reverse @catalogs) {
44 my $msg;
45 if ($perms->check_edit_delete_article({}, $catalog, '', \$msg)) {
46 print "Removing catalog $catalog->{id}: $catalog->{title}\n";
47 $catalog->remove($cfg);
48 }
49 else {
50 print "Cannot remove $catalog->{id}: $msg\n";
51 }
52 }
53}
54
55my @errors = $importer->errors;
56unless ($verbose) { # unless we already reported them
57 print STDERR $_, "\n" for @errors;
58}
59@errors
60 and exit 1;
61
62exit;