invalidate filters on other datasets
[MojoFacets.git] / lib / MojoFacets / Data.pm
index 202f794..eff5f2a 100644 (file)
@@ -20,6 +20,7 @@ use MojoFacets::Import::File;
 use MojoFacets::Import::HTMLTable;
 use MojoFacets::Import::CSV;
 use MojoFacets::Import::CouchDB;
+use MojoFacets::Import::SQL;
 
 our $loaded;
 our $filters;
@@ -42,7 +43,7 @@ sub index {
                } elsif ( -d $file && $file =~ m/\.html$/ ) {
                        $file =~ s/$data_dir\/*//;
                        push @files, $file;
-               } elsif ( -f $file && $file =~ m/\.(csv|storabe|couchdb)$/i ) {
+               } elsif ( -f $file && $file =~ m/\.(csv|storabe|couchdb|sql)$/i ) {
                        $file =~ s/$data_dir\/*//;
                        push @files, $file;
                } else {
@@ -188,6 +189,8 @@ sub _load_path {
        if ( -f $full_path ) {
                if ( $full_path =~ m/.csv/i ) {
                        $data = MojoFacets::Import::CSV->new( full_path => $full_path )->data;
+               } elsif ( $full_path =~ m/.sql/i ) {
+                       $data = MojoFacets::Import::SQL->new( full_path => $full_path )->data;
                } elsif ( $full_path =~ m/.couchdb/i ) {
                        $data = MojoFacets::Import::CouchDB->new( full_path => $full_path )->data;
                } elsif ( $full_path =~ m/.storable/ ) {
@@ -541,6 +544,12 @@ sub _filter_on_data {
 
        #warn "# filter $name ",dump($filtered_items);
 
+       # invalidate filters on other datasets
+       foreach my $dataset ( grep { exists $loaded->{$_}->{filters}->{$name} } keys %$loaded ) {
+               delete $loaded->{$dataset}->{filters}->{$name};
+               delete $loaded->{$dataset}->{filtered};
+       }
+
        $loaded->{$path}->{filters}->{$name} = $filtered_items;
        warn "filter $name with ", scalar keys %$filtered_items, " items created\n";
 }
@@ -623,9 +632,27 @@ sub __commit_path_code {
        #warn "__commit_path_code $path $i ",dump( $update );
 }
 
+# uses templates/admin.html.ep
+sub _switch_dataset {
+       my $self = shift;
+
+       my $datasets;
+
+       foreach my $path ( keys %$loaded ) {
+               next unless exists $loaded->{$path}->{data};
+               push @$datasets, $path;
+       }
+
+       warn "# datasets ",dump($datasets);
+
+       $self->stash( 'datasets' => $datasets );
+}
+
 sub items {
        my $self = shift;
 
+       $self->_switch_dataset;
+
        if ( my $show = $self->param('id') ) {
                $self->param('show', $show);
                warn "show $show\n";