aboutsummaryrefslogtreecommitdiff
path: root/lib/Object/Groupware/Event.pm
blob: 1e1df87a93347240dc96f5d04c06595dea519f58 (plain)
  1. use v5.36;
  2. use Feature::Compat::Class 0.07;
  3. package Object::Groupware::Event 0.01;
  4. class Object::Groupware::Event : isa(Object::Groupware);
  5. use utf8;
  6. use Log::Any qw( );
  7. use Feature::Compat::Try;
  8. use DateTime::Locale;
  9. use Encode qw(decode_utf8);
  10. field $log = Log::Any->get_logger;
  11. field $entry : param;
  12. field $dt_locale;
  13. field $span;
  14. field $start : reader = $entry->start;
  15. field $end : reader = $entry->end;
  16. field $summary : reader = $entry->summary;
  17. field $description : reader = $entry->description || '';
  18. field $location : reader = $entry->_simple_property('location');
  19. field $price : reader;
  20. field @attendees;
  21. field @attachments;
  22. ADJUST {
  23. if ( $self->dt_locale ) {
  24. $dt_locale = $self->dt_locale;
  25. $start->set_locale($dt_locale);
  26. $end->set_locale($dt_locale) if defined $end;
  27. }
  28. else {
  29. # we need the object regardless, to fetch CLDR patterns
  30. $dt_locale = DateTime::Locale->load('en_US');
  31. }
  32. $start->set_locale( $self->dt_locale );
  33. for ( $self->dt_time_zone || () ) {
  34. $start->set_time_zone($_);
  35. $end->set_time_zone($_) if defined $end;
  36. }
  37. $description =~ s/\n\n[Pp]ris:\s*((?!\n).+)\s*\z//m;
  38. $price = $1;
  39. if ( $entry->property('attendee') ) {
  40. for ( @{ $entry->property('attendee') } ) {
  41. push @attendees, $_->parameters->{'CN'}
  42. || $_->value =~ s/^mailto://r;
  43. }
  44. }
  45. if ( $entry->property('attach') ) {
  46. for ( @{ $entry->property('attach') } ) {
  47. my $uri;
  48. try { $uri = URI->new( $_->value ) }
  49. catch ($e) {
  50. $log->errorf( 'failed to parse URI %s: %s', $uri, $e );
  51. next;
  52. }
  53. $uri->authority and $uri->host
  54. or next;
  55. push @attachments, $uri;
  56. }
  57. }
  58. $log->tracef(
  59. "Object %s contents:\n%s", __CLASS__,
  60. decode_utf8 $entry->as_string
  61. );
  62. }
  63. method start_date { $start->strftime('%F') }
  64. method end_date { $end->strftime('%F') }
  65. method attendees { !!@attendees ? [@attendees] : undef }
  66. method attachments { !!@attachments ? [@attachments] : undef }
  67. method span ()
  68. {
  69. return $span
  70. if defined($span);
  71. return $span = ''
  72. unless $end;
  73. require DateTime::Span;
  74. $span = DateTime::Span->from_datetimes( start => $start, before => $end );
  75. return $span;
  76. }
  77. method datespan ()
  78. {
  79. return ucfirst( $start->format_cldr( $dt_locale->date_format_medium() ) )
  80. if $entry->all_day;
  81. return ''
  82. if !$span
  83. or $end->clone->truncate( to => 'day' ) eq
  84. $start->clone->truncate( to => 'day' );
  85. return ucfirst(
  86. sprintf '%s - %s',
  87. $start->format_cldr( $dt_locale->date_format_medium() ),
  88. $end->format_cldr( $dt_locale->date_format_medium() )
  89. );
  90. }
  91. method timespan ()
  92. {
  93. return ''
  94. if $span
  95. and $end->clone->truncate( to => 'day' ) ne
  96. $start->clone->truncate( to => 'day' )
  97. or $self->span;
  98. return ucfirst( $start->format_cldr( $dt_locale->date_format_medium() ) )
  99. if $entry->all_day;
  100. return $span = ucfirst(
  101. sprintf '%s-%s',
  102. $start->format_cldr( $dt_locale->datetime_format_medium() ),
  103. $end->format_cldr( $dt_locale->time_format_medium() )
  104. );
  105. }
  106. method time_brief ()
  107. {
  108. return $self->datespan
  109. || ucfirst(
  110. $start->format_cldr( $dt_locale->datetime_format_medium() ) );
  111. }
  112. 1;