| 1 | package App::JobLog::Vacation; | ||
| 2 | $App::JobLog::Vacation::VERSION = '1.042'; | ||
| 3 | # ABSTRACT: controller for the vacation model | ||
| 4 | |||
| 5 | |||
| 6 | use Modern::Perl; | ||
| 7 | use App::JobLog::Vacation::Period; | ||
| 8 | use App::JobLog::Config qw( | ||
| 9 | vacation | ||
| 10 | init_file | ||
| 11 | ); | ||
| 12 | use Carp qw(carp); | ||
| 13 | use FileHandle; | ||
| 14 | |||
| 15 | |||
| 16 | sub new { | ||
| 17 | my $class = shift; | ||
| 18 | $class = ref $class if ref $class; | ||
| 19 | my $self = bless { changed => 0 }, $class; | ||
| 20 | if ( -e vacation ) { | ||
| 21 | my $fh = FileHandle->new(vacation); | ||
| 22 | my @data; | ||
| 23 | while ( my $line = <$fh> ) { | ||
| 24 | chomp $line; | ||
| 25 | my $v = App::JobLog::Vacation::Period->parse($line); | ||
| 26 | push @data, $v; | ||
| 27 | } | ||
| 28 | $self->{data} = [ sort { $a->cmp($b) } @data ]; | ||
| 29 | } | ||
| 30 | return $self; | ||
| 31 | } | ||
| 32 | |||
| 33 | |||
| 34 | sub periods { @{ $_[0]->{data} || [] } } | ||
| 35 | |||
| 36 | |||
| 37 | sub close { | ||
| 38 | my ($self) = @_; | ||
| 39 | if ( $self->{changed} ) { | ||
| 40 | $self->{changed} = 0; | ||
| 41 | if ( @{ $self->{data} } ) { | ||
| 42 | |||
| 43 | # something to save | ||
| 44 | init_file(vacation) unless -e vacation; | ||
| 45 | my $fh = FileHandle->new( vacation, 'w' ); | ||
| 46 | for my $v ( @{ $self->{data} } ) { | ||
| 47 | print $fh $v, "\n"; | ||
| 48 | } | ||
| 49 | $fh->close; | ||
| 50 | } | ||
| 51 | elsif ( -e vacation ) { | ||
| 52 | unlink(vacation); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | # make sure changes are written to the file | ||
| 58 | sub DESTROY { | ||
| 59 | my ($self) = @_; | ||
| 60 | $self->close if $self->{changed}; | ||
| 61 | } | ||
| 62 | |||
| 63 | |||
| 64 | sub add { | ||
| 65 | my ( $self, %opts ) = @_; | ||
| 66 | my ( $end, $type, $repeats ) = @opts{qw(end type repeats)}; | ||
| 67 | delete @opts{qw(end type repeats)}; | ||
| 68 | my $ll = App::JobLog::Log::Line->new(%opts); | ||
| 69 | my $v = App::JobLog::Vacation::Period->new( | ||
| 70 | $ll, | ||
| 71 | end => $end, | ||
| 72 | type => $type, | ||
| 73 | repeats => $repeats | ||
| 74 | ); | ||
| 75 | my @data = @{ $self->{data} || [] }; | ||
| 76 | |||
| 77 | for my $other (@data) { | ||
| 78 | if ( $other->conflicts($v) ) { | ||
| 79 | my $d1 = join ' ', $v->parts; | ||
| 80 | my $d2 = join ' ', $other->parts; | ||
| 81 | carp "$d1 conflicts with existing period $d2"; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | push @data, $v; | ||
| 85 | $self->{data} = [ sort { $a->cmp($b) } @data ]; | ||
| 86 | $self->{changed} = 1; | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | sub remove { | ||
| 91 | my ( $self, $index ) = @_; | ||
| 92 | carp 'vacation date index must be non-negative' if $index < 0; | ||
| 93 | my $data = $self->{data}; | ||
| 94 | carp "unknown vacation index: $index" unless $data && @$data >= $index; | ||
| 95 | splice @$data, $index - 1, 1; | ||
| 96 | $self->{changed} = 1; | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | sub show { | ||
| 101 | my ($self) = @_; | ||
| 102 | my @parts; | ||
| 103 | my $widths; | ||
| 104 | for my $v ( $self->periods ) { | ||
| 105 | my @p = $v->parts; | ||
| 106 | push @parts, \@p; | ||
| 107 | my $w = _widths( \@p ); | ||
| 108 | if ($widths) { | ||
| 109 | for ( 0 .. $#$w ) { | ||
| 110 | my ( $l1, $l2 ) = ( $w->[$_], $widths->[$_] ); | ||
| 111 | $widths->[$_] = $l1 if $l1 > $l2; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | else { | ||
| 115 | $widths = $w; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | return [] unless @parts; | ||
| 119 | my $format = sprintf "%%%dd) %%%ds %%-%ds %%-%ds %%-%ds\n", | ||
| 120 | length scalar(@parts), | ||
| 121 | @$widths; | ||
| 122 | for my $i ( 0 .. $#parts ) { | ||
| 123 | $parts[$i] = sprintf $format, $i + 1, @{ $parts[$i] }; | ||
| 124 | } | ||
| 125 | return \@parts; | ||
| 126 | } | ||
| 127 | |||
| 128 | |||
| 129 | sub add_overlaps { | ||
| 130 | my ( $self, $events ) = @_; | ||
| 131 | my ( %day_map, @overlaps ); | ||
| 132 | for my $e (@$events) { | ||
| 133 | for my $v ( @{ $self->{data} } ) { | ||
| 134 | my $o = $v->overlap($e); | ||
| 135 | if ($o) { | ||
| 136 | my $s = $o->start . ' ' . $o->end; | ||
| 137 | unless ( $day_map{$s} ) { | ||
| 138 | $day_map{$s} = 1; | ||
| 139 | push @overlaps, $o; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | } | ||
| 143 | } | ||
| 144 | return $events unless @overlaps; | ||
| 145 | push @overlaps, @$events; | ||
| 146 | return [ sort { $a->cmp($b) } @overlaps ]; | ||
| 147 | } | ||
| 148 | |||
| 149 | # collect the widths of a list of strings | ||
| 150 | sub _widths { | ||
| 151 | my ($ar) = @_; | ||
| 152 | my @w; | ||
| 153 | push @w, length $_ for @$ar; | ||
| 154 | return \@w; | ||
| 155 | } | ||
| 156 | |||
| 157 | 1; | ||
| 158 | |||
| 159 | __END__ |