aboutsummaryrefslogtreecommitdiff
path: root/lib/Object/Groupware/Event.pm
blob: 73841769e884a6cc9a9c973bb8da86c5f0f41e03 (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( "Object %s contents:\n%s", __CLASS__,
  59. decode_utf8 $entry->as_string );
  60. }
  61. method attendees { !!@attendees ? [@attendees] : undef }
  62. method attachments { !!@attachments ? [@attachments] : undef }
  63. method span ()
  64. {
  65. return $span
  66. if defined($span);
  67. return $span = ''
  68. unless $end;
  69. require DateTime::Span;
  70. $span = DateTime::Span->from_datetimes( start => $start, before => $end );
  71. return $span;
  72. }
  73. method datespan ()
  74. {
  75. return ucfirst( $start->format_cldr( $dt_locale->date_format_medium() ) )
  76. if $entry->all_day;
  77. return ''
  78. if !$span
  79. or $end->clone->truncate( to => 'day' ) eq
  80. $start->clone->truncate( to => 'day' );
  81. return ucfirst(
  82. sprintf '%s - %s',
  83. $start->format_cldr( $dt_locale->date_format_medium() ),
  84. $end->format_cldr( $dt_locale->date_format_medium() )
  85. );
  86. }
  87. method timespan ()
  88. {
  89. return ''
  90. if $span
  91. and $end->clone->truncate( to => 'day' ) ne
  92. $start->clone->truncate( to => 'day' )
  93. or $self->span;
  94. return ucfirst( $start->format_cldr( $dt_locale->date_format_medium() ) )
  95. if $entry->all_day;
  96. return $span = ucfirst(
  97. sprintf '%s-%s',
  98. $start->format_cldr( $dt_locale->datetime_format_medium() ),
  99. $end->format_cldr( $dt_locale->time_format_medium() )
  100. );
  101. }
  102. method time_brief ()
  103. {
  104. return $self->datespan
  105. || ucfirst(
  106. $start->format_cldr( $dt_locale->datetime_format_medium() ) );
  107. }
  108. 1;