default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / httemplate / view / deploy_zone-shp.cgi
1 <% $content %>\
2 <%init>
3
4 my $curuser = $FS::CurrentUser::CurrentUser;
5 my $acl_edit = $curuser->access_right('Edit FCC report configuration');
6 my $acl_edit_global = $curuser->access_right('Edit FCC report configuration for all agents');
7 die "access denied"
8   unless $acl_edit or $acl_edit_global;
9
10 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
11
12 my %shapelib_opts = (
13   Shapetype  => Geo::Shapelib::POLYGON,
14   FieldNames => [ 'Tech', 'Down', 'Up' ],
15   FieldTypes => [ 'String:32', 'Double', 'Double' ],
16 );
17
18 my( $name, $shapefile );
19
20 my($query) = $cgi->keywords;
21 if ( $query =~ /^(\d+)$/ || $cgi->param('zonenum') =~ /^(\d+$)/ ) {
22   my $zonenum = $1;
23   $name = $zonenum;
24   my $deploy_zone = qsearchs('deploy_zone', { 'zonenum' => $zonenum })
25     or die 'unknown zonenum';
26
27   $shapefile = new Geo::Shapelib {
28     Name => "$dir/$zonenum-$$",
29     %shapelib_opts
30   };
31
32   $deploy_zone->shapefile_add($shapefile);
33
34 } elsif ( $cgi->param('zonetype') =~ /^(\w)$/ ) {
35   my $zonetype = $1;
36   $name = $zonetype;
37   my @deploy_zone = qsearch('deploy_zone', { 'zonetype' => $zonetype,
38                                              'disabled' => '',        });
39
40   $shapefile = new Geo::Shapelib {
41     Name => "$dir/$zonetype-$$",
42     %shapelib_opts
43   };
44
45   $_->shapefile_add($shapefile) foreach @deploy_zone;
46
47 } else {
48   die "no zonenum or zonetype\n";
49 }
50
51 $shapefile->set_bounds;
52
53 $shapefile->save;
54
55 #slurp up .shp .shx and .dbf files and put them in a zip.. return that
56 #and delete the files
57
58 my $content = '';
59 open(my $fh, '>', \$content);
60
61 my $zip = new Archive::Zip;
62 $zip->addFile("$dir/$name-$$.$_", "$name.$_") foreach qw( shp shx dbf );
63 unless ( $zip->writeToFileHandle($fh) == Archive::Zip::AZ_OK() ) {
64   die "failed to create .shz file\n";
65 }
66 close $fh;
67
68 unlink("$dir/$name-$$.$_") foreach qw( shp shx dbf );
69
70 #http_header('Content-Type'        => 'x-gis/x-shapefile' );
71 http_header('Content-Type'        => 'archive/zip' );
72 http_header('Content-Disposition' => "filename=$name.shz" );
73 http_header('Content-Length'      => length($content) );
74 http_header('Cache-control'       => 'max-age=60' );
75
76 </%init>