<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># Copyright 2002-2006, Fred Steinberg, Brown Bear Software

# Event - formatting routines for dumping Calcium events (mostly for mail)

package Event;
use strict;

sub formatForMail {
    my ($self, $date, $calName, $prefs, $i18n) = @_;

    my %vals = (calName   =&gt; $calName,
                text      =&gt; $self-&gt;text,
                category  =&gt; $self-&gt;getCategoryScalar,
                scheduled =&gt; $date-&gt;pretty ($i18n),
                popup     =&gt; $self-&gt;popup || $self-&gt;link);

    # And custom fields
    if (Defines-&gt;has_feature ('custom fields')) {
        require Calendar::Template;
        my $templ = Template-&gt;new (name     =&gt; 'Mail',
                                   cal_name =&gt; $calName,
                                   convert_newlines =&gt; 1);

        $vals{custom} = $self-&gt;custom_fields_display (template =&gt; $templ,
                                                      prefs    =&gt; $prefs,
                                                      escape   =&gt; undef,
                                                      format   =&gt; 'html');
        $vals{custom_text} = $self-&gt;custom_fields_display (template =&gt; $templ,
                                                           prefs    =&gt; $prefs,
                                                           escape   =&gt; undef,
                                                           format   =&gt; 'text');
    }

    if ($self-&gt;startTime) {
        $vals{scheduled} .= ', ' . $self-&gt;getTimeString ('both', $prefs);
    }

    my %labels = (calName   =&gt; $i18n-&gt;get ('Calendar Name:'),
                  text      =&gt; $i18n-&gt;get ('Event text:'),
                  category  =&gt; $i18n-&gt;get ('Category:'),
                  scheduled =&gt; $i18n-&gt;get ('Scheduled for:'),
                  popup     =&gt; $i18n-&gt;get ('Details:'),
                 );

    my @usedLabels = grep {$vals{$_}} keys %labels; # labels which are used
    # find longest label
    my $max = [sort {$a &lt;=&gt; $b}
                map {length}
                map {$labels{$_}} @usedLabels]-&gt;[-1];

    # Wrap long text to indent properly
    if (eval "require Text::Wrap") {
        my $indent = ' ' x ($max + 1);
        $Text::Wrap::columns = 72;
        $Text::Wrap::huge    = 'overflow';
        $Text::Wrap::columns = 72;         # avoid 'used only once' warnings
        $Text::Wrap::huge    = 'overflow';
        foreach (qw /text popup/) {
            next unless defined ($vals{$_});
            $vals{$_} = Text::Wrap::wrap ($indent, $indent, ($vals{$_}));
            $vals{$_} =~ s/^\s*//;
        }
    }

    my $text = '';
    foreach (qw /calName scheduled text category popup/) {
        next unless $vals{$_};
        $vals{$_} =~ s/\r//g;
        $text .= sprintf ("%-*s %s\n", $max, $labels{$_}, $vals{$_});
    }
    if (defined $vals{custom_text}) {
        $text .= "\n$vals{custom_text}\n";
    }

    $vals{text}  = $self-&gt;escapedText  (undef, 1);
    $vals{popup} = $self-&gt;escapedPopup (undef, 1) if ($vals{popup});


    my $html = '&lt;table&gt;';
    foreach (qw /calName scheduled text category popup/) {
        next unless $vals{$_};
        $html .= qq (&lt;tr&gt;&lt;td valign="top"&gt;&lt;b&gt;$labels{$_}&lt;/b&gt;&lt;/td&gt;) .
                 qq (&lt;td&gt;$vals{$_}&lt;/td&gt;&lt;/tr&gt;);
    }
    $html .= '&lt;/table&gt;&lt;br/&gt;';
    if (defined $vals{custom}) {
        $html .= "\n$vals{custom}\n";
    }

    return ($text, $html);
}

sub expandString {
    my ($self, $string, $date, $i18n) = @_;
    return unless defined $string;
    $string =~ s/ \$text     / $self-&gt;text                     /xeg;
    $string =~ s/ \$date     / $date-&gt;pretty ($i18n)           /xeg;
    $string =~ s/ \$category / $self-&gt;getCategoryScalar || '-' /xeg;
    $string =~ s/ \$user     / $self-&gt;owner             || '-' /xeg;
    $string;
}

1;
</pre></body></html>