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