don't redirect to /data/index needlessly
[MojoFacets.git] / lib / MojoFacets / Data.pm
index 1afa188..c2283b8 100644 (file)
@@ -130,7 +130,7 @@ sub __stats {
                                }
                                if ( $unique ) {
                                        $stats->{$n}->{unique} = 1;
-                                       warn "# $n unique ",dump( $unique );
+                                       #warn "# $n unique ",dump( $unique );
                                }
                        }
                }
@@ -226,7 +226,6 @@ sub load {
        $self->_load_path( $path );
 
        $self->session( 'path' => $path );
-       $self->session( 'modified' => $loaded->{$path}->{modified} );
 
        my $redirect_to = '/data/items';
 
@@ -254,22 +253,32 @@ sub _loaded {
        my $path = $self->session('path') || $self->param('path');
        $self->redirect_to('/data/index') unless $path;
 
-       if ( $loaded->{$path}->{modified} > 1 ) {
-               warn "rebuild stats for $path forced by modified\n";
-               $loaded->{$path}->{stats} = __stats( $loaded->{$path}->{data}->{items} );
-               $loaded->{$path}->{modified} = 1;
+       if ( defined $loaded->{$path}->{modified} && $loaded->{$path}->{modified} > 1 ) {
+               my $caller = (caller(1))[3];
+               if ( $caller =~ m/::edit/ ) {
+                       warn "rebuild stats for $path ignored caller $caller\n";
+               } else {
+                       warn "rebuild stats for $path FORCED by modified caller $caller\n";
+                       $loaded->{$path}->{stats} = __stats( $loaded->{$path}->{data}->{items} );
+                       $loaded->{$path}->{modified} = 1;
+               }
        }
 
        if ( ! defined $loaded->{$path}->{$name} ) {
                warn "$path $name isn't loaded\n";
                $self->_load_path( $path );
-               $self->redirect_to('/data/index')
-                       unless defined $loaded->{$path}->{$name};
                if ( ! defined $loaded->{$path}->{stats} ) {
                        warn "rebuild stats for $path\n";
                        $loaded->{$path}->{stats} = __stats( $loaded->{$path}->{data}->{items} );
                }
+               if ( ! defined $loaded->{$path}->{$name} ) {
+                       warn "MISSING $name for $path\n";
+                       $self->redirect_to('/data/index')
+               }
        }
+
+       $self->session( 'modified' => $loaded->{$path}->{modified} );
+
        return $loaded->{$path}->{$name};
 }
 
@@ -290,7 +299,11 @@ sub _permanent_path {
 
 sub _export_path {
        my $self = shift;
-       my $path = $self->_param_or_session('path') || die "no path";
+       my $path = $self->_param_or_session('path');
+       if ( ! $path ) {
+               warn "no path in param or session";
+               return;
+       }
        my $dir = $self->app->home->rel_dir('public') . "/export/$path";
        mkpath $dir unless -e $dir;
        $dir . '/' . join('.', @_);
@@ -520,8 +533,12 @@ sub _data_sorted_by {
 sub items {
        my $self = shift;
 
+       if ( my $show = $self->param('id') ) {
+               $self->param('show', $show);
+               warn "show $show\n";
+       }
+
        my $path = $self->session('path');
-       $self->redirect_to('/data/index') unless defined $loaded->{ $path };
 
        my @columns = $self->_param_array('columns');
        $self->redirect_to('/data/columns') unless @columns;
@@ -731,6 +748,30 @@ sub facet {
        );
 }
 
+
+sub __invalidate_path_column {
+       my ( $path, $name ) = @_;
+
+       if ( defined $loaded->{$path}->{sorted}->{$name} ) {
+               delete $loaded->{$path}->{sorted}->{$name};
+               warn "# invalidate $path sorted $name\n";
+       }
+
+       foreach ( grep { m/$name/ } keys %{ $loaded->{$path}->{filtered} } ) {
+               delete $loaded->{$path}->{filtered}->{$_};
+               warn "# invalidate $path filtered $_\n";
+       }
+}
+
+sub __path_modified {
+       my ( $path, $value ) = @_;
+       $value = 1 unless defined $value;
+       
+       $loaded->{$path}->{modified}  = $value;
+
+       warn "# __path_modified $path $value\n";
+}
+
 sub edit {
        my $self = shift;
        my $new_content = $self->param('new_content');
@@ -783,20 +824,11 @@ sub edit {
                        warn "# change $path $i $old -> $new\n";
                        $loaded->{$path}->{data}->{items}->[$i]->{$name} = $v;
 
-                       if ( defined $loaded->{$path}->{sorted}->{$name} ) {
-                           delete $loaded->{$path}->{sorted}->{$name};
-                               warn "# invalidate $path sorted $name\n";
-                       }
-
-                       foreach ( grep { m/$name/ } keys %{ $loaded->{$path}->{filtered} } ) {
-                           delete $loaded->{$path}->{filtered}->{$_};
-                               warn "# invalidate $path filtered $_\n";
-                       }
+                       __invalidate_path_column( $path, $name );
 
                        $status = 201; # created
                        # modified = 2 -- force rebuild of stats
-                       $loaded->{$path}->{modified}  = 2;
-                       $self->session( 'modified' => 2 );
+                       __path_modified( $path, 2 );
        
                        $new_content = join("\xB6",@$v);
 
@@ -822,8 +854,7 @@ sub save {
        my $self = shift;
        my $path = $self->_param_or_session('path');
        my $dump_path = $self->_save( $path );
-       $loaded->{$path}->{modified} = 0;
-       $self->session( 'modified' => 0 );
+       __path_modified( $path, 0 );
 
        $self->redirect_to( '/data/items' );
 }
@@ -833,11 +864,10 @@ sub export {
 
        if ( my $import = $self->param('import') ) {
 
-               if ( $import =~ m{/filter\.(\w+)\.} ) {
+               if ( $import =~ m{/filter\.(.+?)\..+} ) {
                        my $name = $1;
                        my @vals = map { chomp; $_ }
                                read_file $self->app->home->rel_dir('public') . "/export/$import";
-warn dump(@vals);
                        $self->_remove_filter( $name );
                        $self->_filter_on_data( $name, @vals );
                        $self->session( 'offset' => 0 );