From a9d755c3cee7542d3ca552174af0b7b71693ae58 Mon Sep 17 00:00:00 2001 From: ivan Date: Thu, 14 Aug 2008 11:56:12 +0000 Subject: customer import: add progress bar & redirect to a search of the imported customers, #3475 --- httemplate/misc/cust_main-import.cgi | 65 +++++++++++++++++++--------- httemplate/misc/file-upload.html | 53 +++++++++++++++++++++++ httemplate/misc/process/cust_main-import.cgi | 37 +--------------- 3 files changed, 99 insertions(+), 56 deletions(-) create mode 100644 httemplate/misc/file-upload.html (limited to 'httemplate/misc') diff --git a/httemplate/misc/cust_main-import.cgi b/httemplate/misc/cust_main-import.cgi index e1580e5a1..31bf1ee62 100644 --- a/httemplate/misc/cust_main-import.cgi +++ b/httemplate/misc/cust_main-import.cgi @@ -3,31 +3,44 @@ Import a file containing customer records.

-
+<% include( '/elements/form-file_upload.html', + 'name' => 'CustomerImportForm', + 'action' => 'process/cust_main-import.cgi', + 'num_files' => 1, + 'fields' => [ 'agentnum', 'custbatch', 'format' ], + 'message' => 'Customer import successful', + 'url' => $p."search/cust_main.html?custbatch=$custbatch", + ) +%> <% &ntable("#cccccc", 2) %> -<% include('/elements/tr-select-agent.html', '', #$agentnum, - 'label' => "Agent", - 'empty_label' => 'Select agent', - ) -%> + <% include('/elements/tr-select-agent.html', '', #$agentnum, + 'label' => "Agent", + 'empty_label' => 'Select agent', + ) + %> + + + + + Format + + + + + + <% include( '/elements/file-upload.html', + 'field' => 'file', + 'label' => 'Filename', + ) + %> - - Format - - - - - - Filename - - % #include('/elements/tr-select-part_referral.html') % @@ -48,7 +61,15 @@ Import a file containing customer records. --> - + + + + + @@ -108,4 +129,6 @@ my $req = qq!*!; die "access denied" unless $FS::CurrentUser::CurrentUser->access_right('Import'); +my $custbatch = time2str('webimport-%Y/%m/%d-%T'. "-$$-". rand() * 2**32, time); + diff --git a/httemplate/misc/file-upload.html b/httemplate/misc/file-upload.html new file mode 100644 index 000000000..469274c69 --- /dev/null +++ b/httemplate/misc/file-upload.html @@ -0,0 +1,53 @@ +<% include('/elements/header-minimal.html', 'File Upload') %> +% if ($error) { +Error: <% $error %> +% }else{ +File Upload Successful <% join(',', @filenames) %>; +% } +<% include('/elements/footer.html') %> +<%init> + +die "access denied" + unless $FS::CurrentUser::CurrentUser->access_right('Import'); #? + +my @filenames = (); +my $error = ''; # could be extended to the access control + +$cgi->param('upload_fields') =~ /^([,\w]+)$/ + or $error = "invalid upload_fields"; +my $fields = $1; + +my $dir = $FS::UID::cache_dir. "/cache.". $FS::UID::datasrc; + +foreach my $field (split /,/, $fields) { + next if $error; + + my $fh = $cgi->upload($field) + or $error = "No valid file was provided."; + + my $suffix = ''; + if ( $cgi->param($field) =~ /(\.\w+)$/i ) { + $suffix = lc($1); + } + + my $sh = new File::Temp( TEMPLATE => 'upload.XXXXXXXX', + SUFFIX => $suffix, + DIR => $dir, + UNLINK => 0, + ) + or $error ||= "can't open temporary file to store upload: $!\n"; + + unless ($error) { + while(<$fh>) { + print $sh $_; + } + $sh->filename =~ m!.*/([.\w]+)$!; + push @filenames, "$field:$1"; + close $sh + } + +} + +$error = "No files" unless scalar(@filenames); + + diff --git a/httemplate/misc/process/cust_main-import.cgi b/httemplate/misc/process/cust_main-import.cgi index 2568d1c7b..df61eb632 100644 --- a/httemplate/misc/process/cust_main-import.cgi +++ b/httemplate/misc/process/cust_main-import.cgi @@ -1,42 +1,9 @@ -% if ( $error ) { -% errorpage($error); -% } else { - <% include('/elements/header.html','Import successful') %> - <% include('/elements/footer.html') %> -% } +<% $server->process %> <%init> die "access denied" unless $FS::CurrentUser::CurrentUser->access_right('Import'); -my $fh = $cgi->upload('file'); -my $error = ''; -if ( defined($fh) ) { - - my $type; - if ( $cgi->param('file') =~ /\.(\w+)$/i ) { - $type = lc($1); - } else { - #or error out??? - warn "can't parse file type from filename ". $cgi->param('file'). - '; defaulting to CSV'; - $type = 'csv'; - } - - $error = - FS::cust_main::batch_import( { - filehandle => $fh, - type => $type, - agentnum => scalar($cgi->param('agentnum')), - refnum => scalar($cgi->param('refnum')), - pkgpart => scalar($cgi->param('pkgpart')), - #'fields' => [qw( cust_pkg.setup dayphone first last address1 address2 - # city state zip comments )], - 'format' => scalar($cgi->param('format')), - } ); - -} else { - $error = 'No file'; -} +my $server = new FS::UI::Web::JSRPC 'FS::cust_main::process_batch_import', $cgi; -- cgit v1.2.1