1 package Reaction::UI::ViewPort::Field::TimeRange;
4 use Reaction::Types::DateTime qw(SpanSet);
7 use Time::ParseDate ();
9 use namespace::clean -except => [ qw(meta) ];
10 extends 'Reaction::UI::ViewPort::Field';
14 has '+value' => (isa => SpanSet);
16 #has '+layout' => (default => 'timerange');
19 (isa => 'Str', is => 'rw', lazy_fail => 1, trigger_adopt('value_string'));
22 isa => 'Str', is => 'rw', required => 1, default => sub { 'Delete' },
26 isa => 'Reaction::UI::ViewPort::TimeRangeCollection',
31 sub _build_value_string {
33 #return '' unless $self->has_value;
34 #return $self->value_string;
38 return split(',', $self->value_string);
40 sub adopt_value_string {
42 my @values = $self->value_array;
43 for my $idx (0 .. 3) { # last value is repeat
44 if (length $values[$idx]) {
45 my ($epoch) = Time::ParseDate::parsedate($values[$idx], UK => 1);
46 $values[$idx] = DateTime->from_epoch( epoch => $epoch );
49 $self->value($self->range_to_spanset(@values));
51 sub range_to_spanset {
52 my ($self, $time_from, $time_to, $repeat_from, $repeat_to, $pattern) = @_;
53 my $spanset = DateTime::SpanSet->empty_set;
54 if (!$pattern || $pattern eq 'none') {
55 my $span = DateTime::Span->from_datetimes(
56 start => $time_from, end => $time_to
58 $spanset = $spanset->union( $span );
60 my $duration = $time_to - $time_from;
61 my %args = ( days => $time_from->day + 2,
62 hours => $time_from->hour,
63 minutes => $time_from->minute,
64 seconds => $time_from->second );
66 delete $args{'days'} if ($pattern eq 'daily');
67 delete @args{qw/hours days/} if ($pattern eq 'hourly');
68 $args{'days'} = $time_from->day if ($pattern eq 'monthly');
69 my $start_set = DateTime::Event::Recurrence->$pattern( %args );
70 my $iter = $start_set->iterator( start => $repeat_from, end => $repeat_to );
71 while ( my $dt = $iter->next ) {
72 my $endtime = $dt + $duration;
73 my $new_span = DateTime::Span->from_datetimes(
77 $spanset = $spanset->union( $new_span );
84 $self->parent->remove_range_vp($self);
87 override accept_events => sub { ('value_string', 'delete', super()) };
89 __PACKAGE__->meta->make_immutable;
96 Reaction::UI::ViewPort::Field::TimeRange
106 Accessor for a L<DateTime::SpanSet> object.
110 Returns: Encoded range string representing the value.
114 Returns: Arrayref of the elements of C<value_string>.
118 L<Reaction::UI::ViewPort::TimeRangeCollection> object.
120 =head2 range_to_spanset
122 Arguments: $self, $time_from, $time_to, $repeat_from, $repeat_to, $pattern
123 where $time_from, $time_to, $repeat_from, $repeat_to are L<DateTime>
124 objects, and $pattern is a L<DateTime::Event::Recurrence> method name
130 Removes TimeRange from C<parent> collection.
134 Label for the delete option. Default: 'Delete'.
138 =head2 L<Reaction::UI::ViewPort::Field>
140 =head2 L<Reaction::UI::ViewPort::TimeRangeCollection>
144 See L<Reaction::Class> for authors.
148 See L<Reaction::Class> for the license.