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