aboutsummaryrefslogtreecommitdiff
path: root/lib/Object/Groupware/Event.pm
blob: 4e8ed8ec134d2f82c92c55599cea61a6131b3c0d (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 $start : reader = $entry->start;
  13. field $end : reader = $entry->end;
  14. field $datespan : reader;
  15. field $timespan : reader;
  16. field $time_brief : reader;
  17. field $summary : reader = $entry->summary;
  18. field $description : reader = $entry->description || '';
  19. field $location : reader = $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. $datespan
  39. = ( defined $end
  40. and $end->clone->truncate( to => 'day' ) ne
  41. $start->clone->truncate( to => 'day' ) )
  42. ? ucfirst(
  43. sprintf '%s - %s',
  44. $start->format_cldr( $dt_locale->date_format_medium() ),
  45. $end->format_cldr( $dt_locale->date_format_medium() )
  46. )
  47. : ucfirst( $start->format_cldr( $dt_locale->date_format_medium() ) );
  48. $timespan = ( defined $end and not $entry->all_day )
  49. ? ucfirst(
  50. sprintf '%s-%s',
  51. $start->format_cldr( $dt_locale->datetime_format_medium() ),
  52. $end->format_cldr( $dt_locale->time_format_medium() )
  53. )
  54. : undef;
  55. $time_brief
  56. = $entry->all_day
  57. ? $datespan
  58. : ucfirst(
  59. $start->format_cldr( $dt_locale->datetime_format_medium() ) );
  60. $description =~ s/\n\n[Pp]ris:\s*((?!\n).+)\s*\z//m;
  61. $price = $1;
  62. if ( $entry->property('attendee') ) {
  63. for ( @{ $entry->property('attendee') } ) {
  64. push @attendees, $_->parameters->{'CN'}
  65. || $_->value =~ s/^mailto://r;
  66. }
  67. }
  68. if ( $entry->property('attach') ) {
  69. for ( @{ $entry->property('attach') } ) {
  70. my $uri;
  71. try { $uri = URI->new( $_->value ) }
  72. catch ($e) {
  73. $log->errorf( 'failed to parse URI %s: %s', $uri, $e );
  74. next;
  75. }
  76. $uri->authority and $uri->host
  77. or next;
  78. push @attachments, $uri;
  79. }
  80. }
  81. if ( $log->is_trace ) {
  82. use DDP;
  83. p $entry;
  84. p $start;
  85. p $end;
  86. }
  87. }
  88. method attendees { !!@attendees ? [@attendees] : undef }
  89. method attachments { !!@attachments ? [@attachments] : undef }
  90. 1;