Merge branch 'master' into save_change
[MojoFacets.git] / lib / MojoFacets / Changes.pm
1 package MojoFacets::Changes;
2
3 use strict;
4 use warnings;
5
6 use base 'Mojolicious::Controller';
7
8 use Storable;
9
10 sub index {
11         my $self = shift;
12
13         my $max = $self->param('max') || 50;
14         my $action_regex = join('|', $self->param('action_filter'));
15         warn "# action_regex $action_regex\n";
16
17         my $actions;
18
19         my $changes;
20         foreach my $path ( sort { $b cmp $a } glob '/tmp/changes/*' ) {
21                 if ( $path =~ m{/((\d+\.\d+)\.data\.(.+))$} ) {
22                         my ( $uid, $t, $action ) = ( $1, $2, $3 );
23                         $actions->{$action}++;
24                         next if $action_regex && $action !~ m/^($action_regex)$/;
25                         push @$changes, { uid => $uid, t => $t, action => $action }
26                                 if $#$changes < $max;
27                 } else {
28                         warn "ignore: $path\n";
29                 }
30         }
31
32         # Render template "changes/index.html.ep" with message
33         $self->render(message => 'Latest Changes', changes => $changes, actions => $actions );
34 }
35
36
37 sub view {
38         my $self = shift;
39         my $uid = $self->param('uid');
40         $self->render( change => retrieve( "/tmp/changes/$uid" ), uid => $uid );
41 }
42
43 sub _edit_path {
44         my $self = shift;
45         my $path = $self->param('path') || $self->session('path');
46         $self->app->home->rel_dir('data') . '/' . $path . '.edits';
47 }
48
49 sub edits {
50         my ( $self ) = @_;
51         my $path = $self->param('path') || $self->session('path');
52         my $edits;
53         my $glob = $self->_edit_path . '/*';
54         foreach my $t ( sort { $b cmp $a } glob $glob ) {
55                 push @$edits, retrieve("$t");
56         }
57         $self->render( edits => $edits );
58 }
59
60 sub edit {
61         my $self = shift;
62
63         if ( my $t = $self->param('remove') ) {
64                 unlink $self->_edit_path . '/' . $t;
65         }
66
67         $self->redirect_to('/changes/edits');
68 }
69
70 1;