| 1 | package MT::CMS::AddressBook; |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use MT::Util qw( is_valid_email ); |
|---|
| 5 | |
|---|
| 6 | sub export { |
|---|
| 7 | my $app = shift; |
|---|
| 8 | my $user = $app->user; |
|---|
| 9 | my $perms = $app->permissions; |
|---|
| 10 | my $blog = $app->blog |
|---|
| 11 | or return $app->error( $app->translate("Please select a blog.") ); |
|---|
| 12 | return $app->error( $app->translate("Permission denied.") ) |
|---|
| 13 | unless $user->is_superuser |
|---|
| 14 | || ( $perms && $perms->can_edit_notifications ); |
|---|
| 15 | $app->validate_magic() or return; |
|---|
| 16 | |
|---|
| 17 | $| = 1; |
|---|
| 18 | my $enc = $app->config('ExportEncoding'); |
|---|
| 19 | $enc = $app->config('LogExportEncoding') if ( !$enc ); |
|---|
| 20 | $enc = ( $app->charset || '' ) if ( !$enc ); |
|---|
| 21 | |
|---|
| 22 | my $not_class = $app->model('notification'); |
|---|
| 23 | my $iter = $not_class->load_iter( { blog_id => $blog->id }, |
|---|
| 24 | { 'sort' => 'created_on', 'direction' => 'ascend' } ); |
|---|
| 25 | |
|---|
| 26 | my $file = ''; |
|---|
| 27 | $file = dirify( $blog->name ) . '-' if $blog; |
|---|
| 28 | $file = "Blog-" . $blog->id . '-' if $file eq '-'; |
|---|
| 29 | $file .= "notifications_list.csv"; |
|---|
| 30 | $app->{no_print_body} = 1; |
|---|
| 31 | $app->set_header( "Content-Disposition" => "attachment; filename=$file" ); |
|---|
| 32 | $app->send_http_header( |
|---|
| 33 | $enc |
|---|
| 34 | ? "text/csv; charset=$enc" |
|---|
| 35 | : 'text/csv' |
|---|
| 36 | ); |
|---|
| 37 | |
|---|
| 38 | while ( my $note = $iter->() ) { |
|---|
| 39 | $app->print( $note->email ); |
|---|
| 40 | $app->print(','); |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | sub can_save { |
|---|
| 45 | my ( $eh, $app, $id ) = @_; |
|---|
| 46 | my $perms = $app->permissions; |
|---|
| 47 | return $perms->can_edit_notifications; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | sub save_filter { |
|---|
| 51 | my $eh = shift; |
|---|
| 52 | my ($app) = @_; |
|---|
| 53 | my $email = lc $app->param('email'); |
|---|
| 54 | $email =~ s/(^\s+|\s+$)//gs; |
|---|
| 55 | my $blog_id = $app->param('blog_id'); |
|---|
| 56 | if ( !is_valid_email($email) ) { |
|---|
| 57 | return $eh->error( |
|---|
| 58 | $app->translate( |
|---|
| 59 | "The value you entered was not a valid email address") |
|---|
| 60 | ); |
|---|
| 61 | } |
|---|
| 62 | require MT::Notification; |
|---|
| 63 | |
|---|
| 64 | # duplicate check |
|---|
| 65 | my $notification_iter = |
|---|
| 66 | MT::Notification->load_iter( { blog_id => $blog_id } ); |
|---|
| 67 | while ( my $obj = $notification_iter->() ) { |
|---|
| 68 | if ( ( lc( $obj->email ) eq $email ) |
|---|
| 69 | && ( $obj->id ne $app->param('id') ) ) |
|---|
| 70 | { |
|---|
| 71 | return $eh->error( |
|---|
| 72 | $app->translate( |
|---|
| 73 | "The e-mail address you entered is already on the Notification List for this blog." |
|---|
| 74 | ) |
|---|
| 75 | ); |
|---|
| 76 | } |
|---|
| 77 | } |
|---|
| 78 | return 1; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | sub post_delete { |
|---|
| 82 | my ( $eh, $app, $obj ) = @_; |
|---|
| 83 | |
|---|
| 84 | $app->log( |
|---|
| 85 | { |
|---|
| 86 | message => $app->translate( |
|---|
| 87 | "Subscriber '[_1]' (ID:[_2]) deleted from address book by '[_3]'", |
|---|
| 88 | $obj->email, $obj->id, $app->user->name |
|---|
| 89 | ), |
|---|
| 90 | level => MT::Log::INFO(), |
|---|
| 91 | class => 'system', |
|---|
| 92 | category => 'delete' |
|---|
| 93 | } |
|---|
| 94 | ); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | 1; |
|---|