Show
Ignore:
Timestamp:
04/16/08 23:36:38 (20 months ago)
Author:
bchoate
Message:

Moved handler code for address book to the right place, and changed export to output one address per line instead of comma delimited. BugId:79306

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/release-35/lib/MT/CMS/AddressBook.pm

    r1810 r1940  
    33use strict; 
    44use MT::Util qw( is_valid_email ); 
     5use MT::I18N qw( wrap_text ); 
     6 
     7sub entry_notify { 
     8    my $app   = shift; 
     9    my $user  = $app->user; 
     10    my $perms = $app->permissions; 
     11    return $app->error( $app->translate("No permissions.") ) 
     12      unless $perms->can_send_notifications; 
     13 
     14    my $q        = $app->param; 
     15    my $entry_id = $q->param('entry_id') 
     16      or return $app->error( $app->translate("No entry ID provided") ); 
     17    require MT::Entry; 
     18    require MT::Blog; 
     19    my $entry = MT::Entry->load($entry_id) 
     20      or return $app->error( 
     21        $app->translate( "No such entry '[_1]'", $entry_id ) ); 
     22    my $blog  = MT::Blog->load( $entry->blog_id ); 
     23    my $param = {}; 
     24    $param->{entry_id} = $entry_id; 
     25    return $app->load_tmpl( "dialog/entry_notify.tmpl", $param ); 
     26} 
     27 
     28sub send_notify { 
     29    my $app = shift; 
     30    $app->validate_magic() or return; 
     31    my $q        = $app->param; 
     32    my $entry_id = $q->param('entry_id') 
     33      or return $app->error( $app->translate("No entry ID provided") ); 
     34    require MT::Entry; 
     35    require MT::Blog; 
     36    my $entry = MT::Entry->load($entry_id) 
     37      or return $app->error(  
     38        $app->translate( "No such entry '[_1]'", $entry_id ) ); 
     39    my $blog = MT::Blog->load( $entry->blog_id ); 
     40 
     41    my $user = $app->user; 
     42    $app->blog($blog); 
     43    my $perms = $user->permissions($blog); 
     44    return $app->error( $app->translate("No permissions.") ) 
     45      unless $perms->can_send_notifications; 
     46 
     47    my $author = $entry->author; 
     48    return $app->error( 
     49        $app->translate( "No email address for user '[_1]'", $author->name ) ) 
     50      unless $author->email; 
     51 
     52    my $cols = 72; 
     53    my %params; 
     54    $params{blog} = $blog; 
     55    $params{entry} = $entry; 
     56    $params{author} = $author; 
     57 
     58    if ( $q->param('send_excerpt') ) { 
     59        $params{send_excerpt} = 1; 
     60    } 
     61    $params{message} = wrap_text( $q->param('message'), $cols, '', '' ); 
     62    if ( $q->param('send_body') ) { 
     63        $params{send_body} = 1; 
     64    } 
     65 
     66    my $entry_editurl = $app->uri( 
     67        'mode' => 'view', 
     68        args   => {  
     69            '_type' => 'entry', 
     70            blog_id => $entry->blog_id, 
     71            id      => $entry->id, 
     72        } 
     73    );  
     74    if ( $entry_editurl =~ m|^/| ) { 
     75        my ($blog_domain) = $blog->archive_url =~ m|(.+://[^/]+)|; 
     76        $entry_editurl = $blog_domain . $entry_editurl; 
     77    } 
     78    $params{entry_editurl} = $entry_editurl; 
     79 
     80    my $addrs; 
     81    if ( $q->param('send_notify_list') ) { 
     82        require MT::Notification; 
     83        my $iter = MT::Notification->load_iter( { blog_id => $blog->id } ); 
     84        while ( my $note = $iter->() ) { 
     85            next unless is_valid_email( $note->email ); 
     86            $addrs->{ $note->email } = 1; 
     87        } 
     88    } 
     89 
     90    if ( $q->param('send_notify_emails') ) { 
     91        my @addr = split /[\n\r,]+/, $q->param('send_notify_emails'); 
     92        for my $a (@addr) { 
     93            next unless is_valid_email($a); 
     94            $addrs->{$a} = 1; 
     95        } 
     96    } 
     97 
     98    keys %$addrs 
     99      or return $app->error( 
     100        $app->translate( 
     101            "No valid recipients found for the entry notification.") 
     102      ); 
     103 
     104    my $body = $app->build_email( 'notify-entry.tmpl', \%params ); 
     105 
     106    my $subj = 
     107      $app->translate( "[_1] Update: [_2]", $blog->name, $entry->title ); 
     108    if ( $app->current_language ne 'ja' ) {    # FIXME perhaps move to MT::I18N 
     109        $subj =~ s![\x80-\xFF]!!g; 
     110    } 
     111    my $address = 
     112      defined $author->nickname 
     113      ? $author->nickname . ' <' . $author->email . '>' 
     114      : $author->email; 
     115    my %head = ( 
     116        id      => 'notify_entry', 
     117        To      => $address, 
     118        From    => $address, 
     119        Subject => $subj, 
     120    ); 
     121    my $charset = $app->config('MailEncoding') 
     122      || $app->charset; 
     123    $head{'Content-Type'} = qq(text/plain; charset="$charset"); 
     124    my $i = 1; 
     125    require MT::Mail; 
     126    MT::Mail->send( \%head, $body ) 
     127      or return $app->error( 
     128        $app->translate( 
     129            "Error sending mail ([_1]); try another MailTransfer setting?", 
     130            MT::Mail->errstr 
     131        ) 
     132      ); 
     133    delete $head{To}; 
     134 
     135    foreach my $email ( keys %{$addrs} ) { 
     136        next unless $email; 
     137        if ( $app->config('EmailNotificationBcc') ) { 
     138            push @{ $head{Bcc} }, $email; 
     139            if ( $i++ % 20 == 0 ) { 
     140                MT::Mail->send( \%head, $body ) 
     141                  or return $app->error( 
     142                    $app->translate( 
     143"Error sending mail ([_1]); try another MailTransfer setting?", 
     144                        MT::Mail->errstr 
     145                    ) 
     146                  ); 
     147                @{ $head{Bcc} } = (); 
     148            } 
     149        } 
     150        else { 
     151            $head{To} = $email; 
     152            MT::Mail->send( \%head, $body ) 
     153              or return $app->error( 
     154                $app->translate( 
     155"Error sending mail ([_1]); try another MailTransfer setting?", 
     156                    MT::Mail->errstr 
     157                ) 
     158              ); 
     159            delete $head{To}; 
     160        } 
     161    } 
     162    if ( $head{Bcc} && @{ $head{Bcc} } ) { 
     163        MT::Mail->send( \%head, $body ) 
     164          or return $app->error( 
     165            $app->translate( 
     166                "Error sending mail ([_1]); try another MailTransfer setting?", 
     167                MT::Mail->errstr 
     168            ) 
     169          ); 
     170    } 
     171    $app->redirect( 
     172        $app->uri( 
     173            'mode' => 'view', 
     174            args   => { 
     175                '_type'      => $entry->class, 
     176                blog_id      => $entry->blog_id, 
     177                id           => $entry->id, 
     178                saved_notify => 1 
     179            } 
     180        ) 
     181    ); 
     182} 
    5183 
    6184sub export { 
     
    37215 
    38216    while ( my $note = $iter->() ) { 
    39         $app->print( $note->email ); 
    40         $app->print(','); 
     217        $app->print( $note->email . "\n" ); 
    41218    } 
    42219}