From f041c3f6e17bdacf8ad6b0da87342ef84e7db404 Mon Sep 17 00:00:00 2001 From: Mark Wells Date: Tue, 30 Dec 2014 16:04:48 -0800 Subject: [PATCH] import deployment zone census block list, #32625 --- FS/FS/Record.pm | 10 +- FS/FS/deploy_zone.pm | 51 +++++++++ httemplate/edit/deploy_zone-fixed.html | 15 +++ httemplate/edit/process/deploy_zone-fixed.html | 15 ++- httemplate/edit/process/deploy_zone-mobile.html | 2 +- httemplate/edit/process/elements/process.html | 137 +++++++++++++++++++----- httemplate/elements/file-upload.html | 5 + httemplate/elements/progress-init.html | 55 ++++++++-- httemplate/elements/progress-popup.html | 12 ++- httemplate/elements/tr-file-upload.html | 1 + httemplate/misc/process/deploy_zone-import.html | 9 ++ 11 files changed, 265 insertions(+), 47 deletions(-) create mode 100644 httemplate/elements/tr-file-upload.html create mode 100644 httemplate/misc/process/deploy_zone-import.html diff --git a/FS/FS/Record.pm b/FS/FS/Record.pm index 9fa82961b..10b1cf64c 100644 --- a/FS/FS/Record.pm +++ b/FS/FS/Record.pm @@ -1711,7 +1711,7 @@ Table name (required). =item params -Listref of field names for static fields. They will be given values from the +Arrayref of field names for static fields. They will be given values from the PARAMS hashref and passed as a "params" hashref to batch_import. =item formats @@ -1760,10 +1760,10 @@ format_types). =back -PARAMS is a base64-encoded Storable string containing the POSTed data as -a hash ref. It normally contains at least one field, "uploaded files", -generated by /elements/file-upload.html and containing the list of uploaded -files. Currently only supports a single file named "file". +PARAMS is a hashref (or base64-encoded Storable hashref) containing the +POSTed data. It must contain the field "uploaded files", generated by +/elements/file-upload.html and containing the list of uploaded files. +Currently only supports a single file named "file". =cut diff --git a/FS/FS/deploy_zone.pm b/FS/FS/deploy_zone.pm index 6142b9181..38dd7dc2d 100644 --- a/FS/FS/deploy_zone.pm +++ b/FS/FS/deploy_zone.pm @@ -3,6 +3,8 @@ package FS::deploy_zone; use strict; use base qw( FS::o2m_Common FS::Record ); use FS::Record qw( qsearch qsearchs dbh ); +use Storable qw(thaw); +use MIME::Base64; =head1 NAME @@ -265,6 +267,55 @@ sub deploy_zone_vertex { }); } +=back + +=head2 SUBROUTINES + +=over 4 + +=item process_batch_import JOB, PARAMS + +=cut + +sub process_batch_import { + eval { + use FS::deploy_zone_block; + use FS::deploy_zone_vertex; + }; + my $job = shift; + my $param = shift; + if (!ref($param)) { + $param = thaw(decode_base64($param)); + } + + # even if creating a new zone, the deploy_zone object should already + # be inserted by this point + my $zonenum = $param->{zonenum} + or die "zonenum required"; + my $zone = FS::deploy_zone->by_key($zonenum) + or die "deploy_zone #$zonenum not found"; + my $opt; + if ( $zone->zonetype eq 'B' ) { + $opt = { 'table' => 'deploy_zone_block', + 'params' => [ 'zonenum', 'censusyear' ], + 'formats' => { 'plain' => [ 'censusblock' ] }, + 'default_csv' => 1, + }; + $job->update_statustext('1,Inserting census blocks'); + } elsif ( $zone->zonetype eq 'P' ) { + $opt = { 'table' => 'deploy_zone_vertex', + 'params' => [ 'zonenum' ], + 'formats' => { 'plain' => [ 'latitude', 'longitude' ] }, + 'default_csv' => 1, + }; + } else { + die "don't know how to import to zonetype ".$zone->zonetype; + } + + FS::Record::process_batch_import( $job, $opt, $param ); + +} + =head1 BUGS =head1 SEE ALSO diff --git a/httemplate/edit/deploy_zone-fixed.html b/httemplate/edit/deploy_zone-fixed.html index fb26c4958..90d1b6667 100644 --- a/httemplate/edit/deploy_zone-fixed.html +++ b/httemplate/edit/deploy_zone-fixed.html @@ -16,6 +16,8 @@ 'is_business' => 'Business/government', 'blocknum' => '', 'active_date' => 'Active since', + 'file' => 'Import blocks from text file', + 'censusyear' => 'as census year', }, 'fields' => [ { field => 'zonetype', @@ -54,6 +56,19 @@ 'cir_speed_up', { type => 'tablebreak-tr-title', value => 'Census blocks'}, + { field => 'file', + type => 'file-upload', + }, + { field => 'format', + type => 'hidden', + value => 'plain', + }, + { field => 'censusyear', + type => 'select', + options => [ '', qw( 2013 2012 2011 ) ], + }, + + { type => 'tablebreak-tr-title', value => '', }, { field => 'blocknum', type => 'deploy_zone_block', o2m_table => 'deploy_zone_block', diff --git a/httemplate/edit/process/deploy_zone-fixed.html b/httemplate/edit/process/deploy_zone-fixed.html index c14c81c58..eae3a746d 100644 --- a/httemplate/edit/process/deploy_zone-fixed.html +++ b/httemplate/edit/process/deploy_zone-fixed.html @@ -1,9 +1,14 @@ <& elements/process.html, - error_redirect => popurl(2).'deploy_zone-fixed.html?', + debug => 1, + error_redirect => popurl(2).'deploy_zone-fixed.html', table => 'deploy_zone', viewall_dir => 'browse', - process_o2m => - { 'table' => 'deploy_zone_block', - 'fields' => [qw( censusblock censusyear )] - }, + process_o2m => { + 'table' => 'deploy_zone_block', + 'fields' => [qw( censusblock censusyear )] + }, + process_upload => { + 'process' => 'misc/process/deploy_zone-import.html', + 'fields' => [qw( censusyear format )], + }, &> diff --git a/httemplate/edit/process/deploy_zone-mobile.html b/httemplate/edit/process/deploy_zone-mobile.html index c913c5cd6..7b8f911ec 100644 --- a/httemplate/edit/process/deploy_zone-mobile.html +++ b/httemplate/edit/process/deploy_zone-mobile.html @@ -1,5 +1,5 @@ <& elements/process.html, - error_redirect => popurl(2).'deploy_zone-mobile.html?', + error_redirect => popurl(2).'deploy_zone-mobile.html', table => 'deploy_zone', viewall_dir => 'browse', process_o2m => diff --git a/httemplate/edit/process/elements/process.html b/httemplate/edit/process/elements/process.html index 0439d4e9c..eba03041c 100644 --- a/httemplate/edit/process/elements/process.html +++ b/httemplate/edit/process/elements/process.html @@ -62,6 +62,13 @@ Example: 'fields' => [qw( fieldname fieldname2 )], }, + 'process_upload' => { + 'process' => 'misc/mytable-import.html', + # fields to pass to the back end job, besides the + # primary key of the object + 'fields' => [qw( fieldname fieldname2 )], + }, + 'skip_process' => 0, #boolean, if set true, will skip the main table #add/edit processing and only run any linked table #process_ items @@ -94,9 +101,6 @@ Example: %if ( $error ) { % -% my $edit_ext = $opt{'edit_ext'} || 'html'; -% my $url = $opt{'error_redirect'} || popurl(2)."$table.$edit_ext"; - % #my $query = $m->scomp('/elements/create_uri_query'); % #$cgi->redirect("$url?$query"); % if ( length($cgi->query_string) > 1920 ) { #stupid IE 2083 URL limit @@ -114,16 +118,48 @@ Example: % " attempting to set redirect$session to ". $cgi->query_string."\n"; % } % -<% $cgi->redirect("$url?redirect=$session") %> +<% $cgi->redirect("$error_redirect?redirect=$session") %> % % } else { % -<% $cgi->redirect("$url?". $cgi->query_string ) %> +<% $cgi->redirect("$error_redirect?". $cgi->query_string ) %> % -% } +% } % % #different ways of handling success % +%} elsif ( $opt{'process_upload'} and @uploaded_files ) { +% +% # construct a form to pass all the requested fields, the +% +<& /elements/header.html &> +
+% my $job_fields = $opt{'process_upload'}{'fields'}; +% foreach my $field ( @$job_fields ) { + +% } +% push @$job_fields, 'uploaded_files', $pkey; +% + + +<& /elements/progress-init.html, + 'UploadForm', + $job_fields, + $fsurl . $opt{'process_upload'}{'process'}, + { + url => $redirect, + error_url => "$error_redirect?$new_pkey" + }, +&> + +
+ +<& /elements/footer.html &> + %} elsif ( $opt{'popup_reload'} ) { <% include('/elements/header-popup.html', $opt{'popup_reload'} ) %> @@ -136,26 +172,8 @@ Example: %} else { -% -% $opt{'redirect'} = &{$opt{'redirect'}}($cgi, $new) -% if ref($opt{'redirect'}) eq 'CODE'; -% -% if ( $opt{'redirect'} ) { -% -<% $cgi->redirect( $opt{'redirect'}. $new_pkey) %> -% -% } else { -% -% my $ext = $opt{'viewall_ext'} || 'html'; -% my $viewall_dir = $opt{'viewall_dir'} || 'search'; -% my $viewall_url = $opt{'viewall_url'} || ($viewall_dir . "/$table.$ext"); -% -%#<% $cgi->redirect( popurl(3). ($opt{viewall_dir}||'search'). "/$table.$ext" ) %> -<% $cgi->redirect( popurl(3) . $viewall_url ) %> -% } -% +<% $cgi->redirect($redirect) %> %} -% <%init> my $me = 'process.html:'; @@ -207,6 +225,8 @@ if ( $bfield ) { #warn join(',', @values); } +my @uploaded_files; + my $new; my $new_pkey = ''; foreach my $value ( @values ) { @@ -345,6 +365,47 @@ foreach my $value ( @values ) { } + if ( !$error and $opt{'process_upload'} ) { + # mostly duplicates misc/file-upload.html + $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 $source_name = $cgi->param($field); + + if ( $opt{'debug'} ) { + warn "$me processing upload: $source_name\n"; + } + my $fh = $cgi->upload( $field ); # if no file provided, that's fine + next if !$fh; + + my $suffix = ''; + if ( $source_name =~ /(\.\w+)$/ ) { + $suffix = lc($1); + } + + my $sh = File::Temp->new( TEMPLATE => 'upload.XXXXXXXX', + SUFFIX => $suffix, + DIR => $dir, + UNLINK => 0, + ) + or $error ||= "can't open temporary file to store upload: $!\n"; + next if $error; + while(<$fh>) { + print $sh $_; + } + $sh->filename =~ m!.*/([.\w]+)$!; + push @uploaded_files, "$field:$1"; + if ( $opt{'debug'} ) { + warn "uploaded as $1\n"; + } + } + } + if ( $error ) { $cgi->param('error', $error); @@ -365,4 +426,30 @@ foreach my $value ( @values ) { last if $error; } + +# set up redirect URLs + +my $redirect; +if ( !$error ) { + $opt{'redirect'} = &{$opt{'redirect'}}($cgi, $new) + if ref($opt{'redirect'}) eq 'CODE'; + + if ( $opt{'redirect'} ) { + + $redirect = $opt{'redirect'} . $new_pkey; + + } else { + + my $ext = $opt{'viewall_ext'} || 'html'; + my $viewall_dir = $opt{'viewall_dir'} || 'search'; + my $viewall_url = $opt{'viewall_url'} || ($viewall_dir . "/$table.$ext"); + + $redirect = popurl(3) . $viewall_url; + + } +} + +my $edit_ext = $opt{'edit_ext'} || 'html'; +my $error_redirect = $opt{'error_redirect'} + || popurl(2)."$table.$edit_ext"; diff --git a/httemplate/elements/file-upload.html b/httemplate/elements/file-upload.html index 034eaec38..f69df343c 100644 --- a/httemplate/elements/file-upload.html +++ b/httemplate/elements/file-upload.html @@ -47,6 +47,11 @@ } } +<&| onload.js &> +// force the form to submit as multipart +var thisform = document.getElementById('uploaded_files').form; +thisform.enctype = 'multipart/form-data'; + diff --git a/httemplate/elements/progress-init.html b/httemplate/elements/progress-init.html index cef54b824..5b42aa1a8 100644 --- a/httemplate/elements/progress-init.html +++ b/httemplate/elements/progress-init.html @@ -17,6 +17,9 @@ In misc/something.html: # redirecting to the URL. #or { popup_url => $p.'popup_contents.html' } # which loads that URL into the popup after completion + #or { url => $p.'where_to_go.html', + error_url => $p.'where_we_just_were.html' } + # to redirect somewhere different on error ) %>