somewhat nicer results of apply
[MojoFacets.git] / lib / MojoFacets / Data.pm
index f4d7a61..86a5286 100644 (file)
@@ -90,6 +90,8 @@ sub __stats {
 
        my $nr_items = $#{ $_[0] } + 1;
 
+       warn "__stats $nr_items\n";
+
        foreach my $e ( @{ $_[0] } ) {
                foreach my $n ( keys %$e ) {
                        $stats->{$n}->{count}++;
@@ -145,7 +147,8 @@ sub _param_or_session {
 sub stats {
        my $self = shift;
        my $path = $self->_param_or_session('path');
-       $loaded->{$path}->{stats} = __stats( $loaded->{$path}->{data}->{items} );
+       warn "stats $path\n";
+       delete $loaded->{$path}->{stats};
        $self->redirect_to( '/data/columns' );
 }
 
@@ -261,16 +264,24 @@ sub load {
        $self->session('path' => $path);
        $self->_load_path( $path );
 
+       my $redirect_to = '/data/items';
+
        $self->session( 'header' => $loaded->{$path}->{header} );
        if ( ! defined $loaded->{$path}->{columns} ) {
-               $self->session( 'columns' => $loaded->{$path}->{header} );
-               $self->session( 'order'   => $loaded->{$path}->{header}->[0] );
-               $self->redirect_to( '/data/columns' );
-       } else {
-               $self->session( 'columns' => $loaded->{$path}->{columns} );
-               $self->session( 'order'   => $loaded->{$path}->{columns}->[0] );
-               $self->redirect_to( '/data/items' );
+               my $columns_path = $self->_permanent_path( 'columns' );
+               if ( -e $columns_path ) {
+                       my @columns = map { s/[\r\n]+$//; $_ } read_file $columns_path;
+                       $loaded->{$path}->{columns} = [ @columns ];
+                       warn "# columns_path $columns_path ",dump(@columns);
+               } else {
+                       $loaded->{$path}->{columns} = $loaded->{$path}->{header}
+               }
+
+               $redirect_to = '/data/columns';
        }
+       $self->session( 'columns' => $loaded->{$path}->{columns} );
+       $self->session( 'order'   => $loaded->{$path}->{columns}->[0] );
+       $self->redirect_to( $redirect_to );
 }
 
 
@@ -283,6 +294,10 @@ sub _loaded {
                $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} );
+               }
        }
        return $loaded->{$path}->{$name};
 }
@@ -296,16 +311,32 @@ sub _checked {
        return $checked;
 }
 
+sub _permanent_path {
+       my $self = shift;
+       my $path = $self->_param_or_session('path');
+       $self->app->home->rel_dir('data') . '/' . join('.', $path, @_);
+}
+
+sub _export_path {
+       my $self = shift;
+       my $path = $self->_param_or_session('path');
+       my $dir = $self->app->home->rel_dir('public') . '/export/';
+       mkdir $dir unless -e $dir;
+       $dir .= $path;
+       mkdir $dir unless -e $dir;
+       $dir . '/' . join('.', @_);
+}
 
 sub columns {
     my $self = shift;
 
        if ( $self->param('columns') ) {
-               $self->_perm_array('columns');
+               my @columns = $self->_param_array('columns');
+               write_file( $self->_permanent_path( 'columns' ), map { "$_\n" } @columns );
                $self->redirect_to('/data/items');
        }
 
-       my $stats = $self->_loaded( 'stats' ); # || $self->redirect_to( '/data/index' );
+       my $stats = $self->_loaded( 'stats' );
 
        my @columns;
        @columns = grep { defined $stats->{$_}->{count} } @{ $self->session('columns') } if $self->session('columns');
@@ -318,11 +349,11 @@ sub columns {
                message => 'Select columns to display',
                stats => $stats,
                columns => \@columns,
-               checked => $self->_checked( $self->_perm_array('columns') ),
+               checked => $self->_checked( $self->_param_array('columns') ),
        );
 }
 
-sub _perm_array {
+sub _param_array {
     my ($self,$name) = @_;
 
        my @array = $self->param($name);
@@ -342,7 +373,7 @@ sub _perm_array {
        return @array;
 }
 
-sub _perm_scalar {
+sub _param_scalar {
     my ($self,$name,$default) = @_;
 
        my $scalar = $self->param($name);
@@ -370,7 +401,14 @@ sub filter {
        my @vals = $self->param('filter_vals');
 
        $self->_remove_filter( $name );
-       $self->_filter_on_data( $name, @vals ) if @vals;
+       if ( @vals ) {
+               $self->_filter_on_data( $name, @vals );
+               if ( my $permanent = $self->param('_permanent') ) {
+                       my $permanent_path = $self->_export_path( 'filter', $name, $permanent );
+                       write_file $permanent_path, map { "$_\n" } @vals;
+                       warn "permanent filter $permanent_path ", -s $permanent_path;
+               }
+       }
 
        $self->session( 'offset' => 0 );
 
@@ -516,13 +554,13 @@ sub items {
        my $path = $self->session('path');
        $self->redirect_to('/data/index') unless defined $loaded->{ $path };
 
-       my @columns = $self->_perm_array('columns');
+       my @columns = $self->_param_array('columns');
        $self->redirect_to('/data/columns') unless @columns;
-       my $order   = $self->_perm_scalar('order', $columns[0]);
-       my $sort    = $self->_perm_scalar('sort', 'a');
-       my $offset  = $self->_perm_scalar('offset', 0);
-       my $limit   = $self->_perm_scalar('limit', 20);
-       $self->_perm_scalar('show', 'table');
+       my $order   = $self->_param_scalar('order', $columns[0]);
+       my $sort    = $self->_param_scalar('sort', 'a');
+       my $offset  = $self->_param_scalar('offset', 0);
+       my $limit   = $self->_param_scalar('limit', 20);
+       $self->_param_scalar('show', 'table');
 
        # fix offset when changing limit
        $offset = int( $offset / $limit ) * $limit;
@@ -661,7 +699,7 @@ sub facet {
        my $facet;
        my $name = $self->param('name') || die "no name";
 
-       my $all = $self->_perm_scalar('all', 1);
+       my $all = $self->_param_scalar('all', 1);
        my $data = $self->_loaded('data');
 
        my $filters = $self->_current_filters;
@@ -765,7 +803,7 @@ sub edit {
                                        keys %{ $loaded->{$path}->{stats} }
                                },
                        };
-                       my $edit_path = $self->app->home->rel_dir('data') . '/' . $path . '.edits';
+                       my $edit_path = $self->_permanent_path( 'edits' );
                        mkdir $edit_path unless -d $edit_path;
                        $edit_path .= '/' . $edit->{time};
                        store $edit, $edit_path;
@@ -815,4 +853,18 @@ sub save {
        $self->redirect_to( '/data/items' );
 }
 
+sub export {
+       my $self = shift;
+       $self->render( export => [
+               map { s{^.+/public/export/}{}; $_ }
+               glob( $self->_export_path . '*' )
+       ] );
+}
+
+sub __loaded_paths {
+       return
+               grep { defined $loaded->{$_}->{data} }
+               keys %$loaded;
+}
+
 1;