root/branches/release-36/lib/MT/CMS/AddressBook.pm @ 2094

Revision 2094, 8.0 kB (checked in by bchoate, 19 months ago)

Added missing import. BugId:79464

  • Property svn:keywords set to Id Revision
Line 
1package MT::CMS::AddressBook;
2
3use strict;
4use MT::Util qw( is_valid_email dirify );
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}
183
184sub export {
185    my $app   = shift;
186    my $user  = $app->user;
187    my $perms = $app->permissions;
188    my $blog  = $app->blog
189      or return $app->error( $app->translate("Please select a blog.") );
190    return $app->error( $app->translate("Permission denied.") )
191      unless $user->is_superuser
192      || ( $perms && $perms->can_edit_notifications );
193    $app->validate_magic() or return;
194
195    $| = 1;
196    my $enc = $app->config('ExportEncoding');
197    $enc = $app->config('LogExportEncoding') if ( !$enc );
198    $enc = ( $app->charset || '' ) if ( !$enc );
199
200    my $not_class = $app->model('notification');
201    my $iter = $not_class->load_iter( { blog_id => $blog->id },
202        { 'sort' => 'created_on', 'direction' => 'ascend' } );
203
204    my $file = '';
205    $file = dirify( $blog->name ) . '-' if $blog;
206    $file = "Blog-" . $blog->id . '-' if $file eq '-';
207    $file .= "notifications_list.csv";
208    $app->{no_print_body} = 1;
209    $app->set_header( "Content-Disposition" => "attachment; filename=$file" );
210    $app->send_http_header(
211        $enc
212        ? "text/csv; charset=$enc"
213        : 'text/csv'
214    );
215
216    while ( my $note = $iter->() ) {
217        $app->print( $note->email . "\n" );
218    }
219}
220
221sub can_save {
222    my ( $eh, $app, $id ) = @_;
223    my $perms = $app->permissions;
224    return $perms->can_edit_notifications;
225}
226
227sub save_filter {
228    my $eh    = shift;
229    my ($app) = @_;
230    my $email = lc $app->param('email');
231    $email =~ s/(^\s+|\s+$)//gs;
232    my $blog_id = $app->param('blog_id');
233    if ( !is_valid_email($email) ) {
234        return $eh->error(
235            $app->translate(
236                "The value you entered was not a valid email address")
237        );
238    }
239    require MT::Notification;
240
241    # duplicate check
242    my $notification_iter =
243      MT::Notification->load_iter( { blog_id => $blog_id } );
244    while ( my $obj = $notification_iter->() ) {
245        if (   ( lc( $obj->email ) eq $email )
246            && ( $obj->id ne $app->param('id') ) )
247        {
248            return $eh->error(
249                $app->translate(
250"The e-mail address you entered is already on the Notification List for this blog."
251                )
252            );
253        }
254    }
255    return 1;
256}
257
258sub post_delete {
259    my ( $eh, $app, $obj ) = @_;
260
261    $app->log(
262        {
263            message => $app->translate(
264"Subscriber '[_1]' (ID:[_2]) deleted from address book by '[_3]'",
265                $obj->email, $obj->id, $app->user->name
266            ),
267            level    => MT::Log::INFO(),
268            class    => 'system',
269            category => 'delete'
270        }
271    );
272}
273
2741;
Note: See TracBrowser for help on using the browser.