Changeset 1940

Show
Ignore:
Timestamp:
04/16/08 23:36:38 (22 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

Location:
branches/release-35/lib/MT/CMS
Files:
2 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} 
  • branches/release-35/lib/MT/CMS/Entry.pm

    r1873 r1940  
    16401640    _finish_rebuild_ping( $app, $entry, scalar $q->param('is_new'), 
    16411641        $has_errors ); 
    1642 } 
    1643  
    1644 sub entry_notify { 
    1645     my $app   = shift; 
    1646     my $user  = $app->user; 
    1647     my $perms = $app->permissions; 
    1648     return $app->error( $app->translate("No permissions.") ) 
    1649       unless $perms->can_send_notifications; 
    1650  
    1651     my $q        = $app->param; 
    1652     my $entry_id = $q->param('entry_id') 
    1653       or return $app->error( $app->translate("No entry ID provided") ); 
    1654     require MT::Entry; 
    1655     require MT::Blog; 
    1656     my $entry = MT::Entry->load($entry_id) 
    1657       or return $app->error( 
    1658         $app->translate( "No such entry '[_1]'", $entry_id ) ); 
    1659     my $blog  = MT::Blog->load( $entry->blog_id ); 
    1660     my $param = {}; 
    1661     $param->{entry_id} = $entry_id; 
    1662     return $app->load_tmpl( "dialog/entry_notify.tmpl", $param ); 
    1663 } 
    1664  
    1665 sub send_notify { 
    1666     my $app = shift; 
    1667     $app->validate_magic() or return; 
    1668     my $q        = $app->param; 
    1669     my $entry_id = $q->param('entry_id') 
    1670       or return $app->error( $app->translate("No entry ID provided") ); 
    1671     require MT::Entry; 
    1672     require MT::Blog; 
    1673     my $entry = MT::Entry->load($entry_id) 
    1674       or return $app->error( 
    1675         $app->translate( "No such entry '[_1]'", $entry_id ) ); 
    1676     my $blog = MT::Blog->load( $entry->blog_id ); 
    1677  
    1678     my $user = $app->user; 
    1679     $app->blog($blog); 
    1680     my $perms = $user->permissions($blog); 
    1681     return $app->error( $app->translate("No permissions.") ) 
    1682       unless $perms->can_send_notifications; 
    1683  
    1684     my $author = $entry->author; 
    1685     return $app->error( 
    1686         $app->translate( "No email address for user '[_1]'", $author->name ) ) 
    1687       unless $author->email; 
    1688  
    1689     my $cols = 72; 
    1690     my %params; 
    1691     $params{blog} = $blog; 
    1692     $params{entry} = $entry; 
    1693     $params{author} = $author; 
    1694  
    1695     if ( $q->param('send_excerpt') ) { 
    1696         $params{send_excerpt} = 1; 
    1697     } 
    1698     $params{message} = wrap_text( $q->param('message'), $cols, '', '' ); 
    1699     if ( $q->param('send_body') ) { 
    1700         $params{send_body} = 1; 
    1701     } 
    1702  
    1703     my $entry_editurl = $app->uri( 
    1704         'mode' => 'view', 
    1705         args   => { 
    1706             '_type' => 'entry', 
    1707             blog_id => $entry->blog_id, 
    1708             id      => $entry->id, 
    1709         } 
    1710     ); 
    1711     if ( $entry_editurl =~ m|^/| ) { 
    1712         my ($blog_domain) = $blog->archive_url =~ m|(.+://[^/]+)|; 
    1713         $entry_editurl = $blog_domain . $entry_editurl; 
    1714     } 
    1715     $params{entry_editurl} = $entry_editurl; 
    1716  
    1717     my $addrs; 
    1718     if ( $q->param('send_notify_list') ) { 
    1719         require MT::Notification; 
    1720         my $iter = MT::Notification->load_iter( { blog_id => $blog->id } ); 
    1721         while ( my $note = $iter->() ) { 
    1722             next unless is_valid_email( $note->email ); 
    1723             $addrs->{ $note->email } = 1; 
    1724         } 
    1725     } 
    1726  
    1727     if ( $q->param('send_notify_emails') ) { 
    1728         my @addr = split /[\n\r,]+/, $q->param('send_notify_emails'); 
    1729         for my $a (@addr) { 
    1730             next unless is_valid_email($a); 
    1731             $addrs->{$a} = 1; 
    1732         } 
    1733     } 
    1734  
    1735     keys %$addrs 
    1736       or return $app->error( 
    1737         $app->translate( 
    1738             "No valid recipients found for the entry notification.") 
    1739       ); 
    1740  
    1741     my $body = $app->build_email( 'notify-entry.tmpl', \%params ); 
    1742  
    1743     my $subj = 
    1744       $app->translate( "[_1] Update: [_2]", $blog->name, $entry->title ); 
    1745     if ( $app->current_language ne 'ja' ) {    # FIXME perhaps move to MT::I18N 
    1746         $subj =~ s![\x80-\xFF]!!g; 
    1747     } 
    1748     my $address = 
    1749       defined $author->nickname 
    1750       ? $author->nickname . ' <' . $author->email . '>' 
    1751       : $author->email; 
    1752     my %head = ( 
    1753         id      => 'notify_entry', 
    1754         To      => $address, 
    1755         From    => $address, 
    1756         Subject => $subj, 
    1757     ); 
    1758     my $charset = $app->config('MailEncoding') 
    1759       || $app->charset; 
    1760     $head{'Content-Type'} = qq(text/plain; charset="$charset"); 
    1761     my $i = 1; 
    1762     require MT::Mail; 
    1763     MT::Mail->send( \%head, $body ) 
    1764       or return $app->error( 
    1765         $app->translate( 
    1766             "Error sending mail ([_1]); try another MailTransfer setting?", 
    1767             MT::Mail->errstr 
    1768         ) 
    1769       ); 
    1770     delete $head{To}; 
    1771  
    1772     foreach my $email ( keys %{$addrs} ) { 
    1773         next unless $email; 
    1774         if ( $app->config('EmailNotificationBcc') ) { 
    1775             push @{ $head{Bcc} }, $email; 
    1776             if ( $i++ % 20 == 0 ) { 
    1777                 MT::Mail->send( \%head, $body ) 
    1778                   or return $app->error( 
    1779                     $app->translate( 
    1780 "Error sending mail ([_1]); try another MailTransfer setting?", 
    1781                         MT::Mail->errstr 
    1782                     ) 
    1783                   ); 
    1784                 @{ $head{Bcc} } = (); 
    1785             } 
    1786         } 
    1787         else { 
    1788             $head{To} = $email; 
    1789             MT::Mail->send( \%head, $body ) 
    1790               or return $app->error( 
    1791                 $app->translate( 
    1792 "Error sending mail ([_1]); try another MailTransfer setting?", 
    1793                     MT::Mail->errstr 
    1794                 ) 
    1795               ); 
    1796             delete $head{To}; 
    1797         } 
    1798     } 
    1799     if ( $head{Bcc} && @{ $head{Bcc} } ) { 
    1800         MT::Mail->send( \%head, $body ) 
    1801           or return $app->error( 
    1802             $app->translate( 
    1803                 "Error sending mail ([_1]); try another MailTransfer setting?", 
    1804                 MT::Mail->errstr 
    1805             ) 
    1806           ); 
    1807     } 
    1808     $app->redirect( 
    1809         $app->uri( 
    1810             'mode' => 'view', 
    1811             args   => { 
    1812                 '_type'      => $entry->class, 
    1813                 blog_id      => $entry->blog_id, 
    1814                 id           => $entry->id, 
    1815                 saved_notify => 1 
    1816             } 
    1817         ) 
    1818     ); 
    18191642} 
    18201643