enable XLS parsing in web controller
[scpubgit/stemmaweb.git] / lib / stemmaweb / Controller / Root.pm
index d600a33..7bdbf1e 100644 (file)
@@ -219,13 +219,12 @@ sub newtradition :Local :Args(0) {
                my $user = $c->user->get_object;
                # Grab the file upload, check its name/extension, and call the
                # appropriate parser(s).
-               my $upload = $c->request->upload('inputfile');
+               my $upload = $c->request->upload('file');
                my $name = $c->request->param('name') || 'Uploaded tradition';
                my( $ext ) = $upload->filename =~ /\.(\w+)$/;
                my %newopts = (
                        'name' => $name,
-                       'file' => $upload->tempname,
-                       'user' => $user
+                       'file' => $upload->tempname
                        );
                my $tradition;
                my $errmsg;
@@ -241,22 +240,26 @@ sub newtradition :Local :Args(0) {
                                }
                                last if $tradition;
                        }
-               } elsif( $ext eq 'txt' || $ext eq 'csv' ) {
-                       my $sep_char = $ext eq 'txt' ? "\t" : ',';
+               } elsif( $ext eq 'txt' || $ext eq 'csv' || $ext eq 'xls' ) {
+                       # If it's Excel we need to pass xls => [true value];
+                       # otherwise we need to pass sep_char => [record separator].
+                       # Good thing record separators are true values.
+                       my $extrafield = $ext eq 'xls' ? 'xls' : 'sep_char';
+                       my $extraarg = $ext eq 'txt' ? "\t" : ',';
                        try {
                                $tradition = Text::Tradition->new( 
                                        %newopts,
                                        'input' => 'Tabular',
-                                       'sep_char' => $sep_char
+                                       $extrafield => $extraarg
                                        );
                        } catch ( Text::Tradition::Error $e ) {
                                $errmsg = $e->message;
                        } catch {
                                $errmsg = "Unexpected parsing error";
                        }
-               } elsif( $ext =~ /^xls(x)?$/ ) {
+               } elsif( $ext eq 'xlsx' ) {
                        $c->stash->{'result'} = 
-                               { 'error' => "Excel parsing not supported yet" };
+                               { 'error' => "Excel XML parsing not supported yet" };
                        $c->response->status( 500 );
                } else {
                        # Error unless we have a recognized filename extension
@@ -268,7 +271,10 @@ sub newtradition :Local :Args(0) {
                # Save the tradition if we have it, and return its data or else the
                # error that occurred trying to make it.
                if( $tradition ) {
+                       my $m = $c->model('Directory');
+                       $user->add_tradition( $tradition );
                        my $id = $c->model('Directory')->store( $tradition );
+                       $c->model('Directory')->store( $user );
                        $c->stash->{'result'} = { 'id' => $id, 'name' => $tradition->name };
                } else {
                        $c->stash->{'result'} = 
@@ -287,16 +293,17 @@ sub _check_permission {
        my( $c, $tradition ) = @_;
     my $user = $c->user_exists ? $c->user->get_object : undef;
     if( $user ) {
-       return 'full' if ( $user->is_admin || $tradition->user->id eq $user->id );
-    } elsif( $tradition->public ) {
-       return 'readonly';
-    } else {
-       # Forbidden!
-       $c->response->status( 403 );
-       $c->response->body( 'You do not have permission to view this tradition.' );
-       $c->detach( 'View::Plain' );
-       return 0;
+       return 'full' if ( $user->is_admin || 
+               ( $tradition->has_user && $tradition->user->id eq $user->id ) );
     }
+       # Text doesn't belong to us, so maybe it's public?
+       return 'readonly' if $tradition->public;
+
+       # ...nope. Forbidden!
+       $c->response->status( 403 );
+       $c->response->body( 'You do not have permission to view this tradition.' );
+       $c->detach( 'View::Plain' );
+       return 0;
 }
 
 =head2 default