← Back to team overview

mvhub-dev team mailing list archive

[Merge] lp:~omacneil/mvhub/add_text_wrap_to_reminders into lp:mvhub

 

Dan MacNeil has proposed merging lp:~omacneil/mvhub/add_text_wrap_to_reminders into lp:mvhub.

Requested reviews:
  mvhub-dev (mvhub-dev)


wrap text at 75 columns in email reminders so it looks nice.

Don't wrap things that shouldn't be wrapped
-- 
https://code.launchpad.net/~omacneil/mvhub/add_text_wrap_to_reminders/+merge/22676
Your team mvhub-dev is subscribed to branch lp:mvhub.
=== modified file 'lib-mvhub/lib/MVHub/Notifications.pm'
--- lib-mvhub/lib/MVHub/Notifications.pm	2010-03-30 21:00:29 +0000
+++ lib-mvhub/lib/MVHub/Notifications.pm	2010-04-02 02:39:17 +0000
@@ -10,24 +10,25 @@
 use MVHub::Utils::DB qw/get_sql_select_statement get_sql_update_statement/;
 
 use HTML::Template;
+use Text::Wrap qw/ fill $columns/;
 
 our ($VERSION) = '$Revision: 1334 $' =~ /([.\d]+)/;
 
 use base 'Exporter';
 our @EXPORT_OK = qw(
-    _push_and_return_programs
-    create_notifications_using
-    create_standard_notification_emails_with
-    create_fourth_notification_emails_with
-    create_final_notification_emails_with
-    die_if_record_count_mismatch
-    get_email_constants_from
-    get_expired_records
-    increment_reminder_levels_for
-    print_emails_contained_in
-    send_emails_contained_in
-    set_reminder_level_in_db
-    shift_off_notifications_with_reminder_count
+  _push_and_return_programs
+  create_notifications_using
+  create_standard_notification_emails_with
+  create_fourth_notification_emails_with
+  create_final_notification_emails_with
+  die_if_record_count_mismatch
+  get_email_constants_from
+  get_expired_records
+  increment_reminder_levels_for
+  print_emails_contained_in
+  send_emails_contained_in
+  set_reminder_level_in_db
+  shift_off_notifications_with_reminder_count
 );
 
 sub _format_expired_record_list {
@@ -43,23 +44,40 @@
 
     my $tmpl = HTML::Template->new( filename => $template_file );
 
-    my $quick_login_url
-        = MVHub::Utils::generate_quick_login_url(
-        $email_constants_href->{website_name},
-        $notice_href->{agency_id} );
-    my $expired_records
-        = _format_expired_record_list( $notice_href->{expired_record_list} );
-
     $tmpl->param(
         first_name       => $notice_href->{contact_first_name},
         agency_name      => $notice_href->{agency_name},
         contact_us_email => $email_constants_href->{admin_email},
         contact_us_phone => $email_constants_href->{admin_phone},
-        expired_records  => $expired_records,
+        expired_records  => '<!--TMPL_VAR name="expired_records"-->',
         signer           => $email_constants_href->{admin_name},
         team_name        => $email_constants_href->{team_name},
         website_name     => $email_constants_href->{website_name},
-        quick_login_url  => $quick_login_url,
+        quick_login_url  => '<!--TMPL_VAR name="quick_login_url"-->',
+    );
+
+    # wrap text to 75 columns
+    $Text::Wrap::columns = 75;
+    my @paragraphs = split /\n/, $tmpl->output();
+    my @text = Text::Wrap::fill( '', '', @paragraphs );
+    my $text = join "\n", @text;
+
+    # we jump through this hoop because we
+    # we don't want to wrap the indented list
+    # of expired records
+    $tmpl = HTML::Template->new( scalarref => \$text );
+    my $expired_records =
+      _format_expired_record_list( $notice_href->{expired_record_list} );
+
+    # we jump through this hoop because we
+    # NEVER want wrap a URL and break it
+    my $quick_login_url = MVHub::Utils::generate_quick_login_url(
+        $email_constants_href->{website_name},
+        $notice_href->{agency_id} );
+
+    $tmpl->param(
+        expired_records => $expired_records,
+        quick_login_url => $quick_login_url,
     );
 
     return $tmpl->output();
@@ -76,8 +94,9 @@
         4 => 'FINAL REMINDER: ',
     );
 
-    my $subject = $nth{ $record_href->{reminders_sent} }
-        . "Time to update your $record_href->{agency_name} listing";
+    my $subject =
+      $nth{ $record_href->{reminders_sent} }
+      . "Time to update your $record_href->{agency_name} listing";
 
     return $subject;
 }
@@ -98,41 +117,42 @@
 
     my $expired_records_aref = shift;
     croak "First Parameter not an array reference"
-        if ref $expired_records_aref ne 'ARRAY';
+      if ref $expired_records_aref ne 'ARRAY';
 
     my $notifications_href = shift || {};
     croak "Second Parameter not a hash reference"
-        if ref $notifications_href ne 'HASH';
+      if ref $notifications_href ne 'HASH';
 
     my %notifications_for = %$notifications_href;
 
     foreach my $expired_record_href (@$expired_records_aref) {
-        my $unique_id = $expired_record_href->{'agency_id'}
-            . $expired_record_href->{'contact_email'};
-
-        $notifications_for{$unique_id}{'agency_id'}
-            = $expired_record_href->{'agency_id'};
-
-        $notifications_for{$unique_id}{'agency_name'}
-            = $expired_record_href->{'agency_name'};
-
-        $notifications_for{$unique_id}{'contact_email'}
-            = $expired_record_href->{'contact_email'};
-
-        $notifications_for{$unique_id}{'agency_contact_email'}
-            = $expired_record_href->{'agency_contact_email'};
-
-        $notifications_for{$unique_id}{'public_email'}
-            = $expired_record_href->{'email'};
-
-        $notifications_for{$unique_id}{'contact_first_name'}
-            = $expired_record_href->{'contact_first_name'};
-
-        $notifications_for{$unique_id}{'contact_last_name'}
-            = $expired_record_href->{'contact_last_name'};
-
-        $notifications_for{$unique_id}{expired_record_list}
-            = _push_and_return_programs(
+        my $unique_id =
+            $expired_record_href->{'agency_id'}
+          . $expired_record_href->{'contact_email'};
+
+        $notifications_for{$unique_id}{'agency_id'} =
+          $expired_record_href->{'agency_id'};
+
+        $notifications_for{$unique_id}{'agency_name'} =
+          $expired_record_href->{'agency_name'};
+
+        $notifications_for{$unique_id}{'contact_email'} =
+          $expired_record_href->{'contact_email'};
+
+        $notifications_for{$unique_id}{'agency_contact_email'} =
+          $expired_record_href->{'agency_contact_email'};
+
+        $notifications_for{$unique_id}{'public_email'} =
+          $expired_record_href->{'email'};
+
+        $notifications_for{$unique_id}{'contact_first_name'} =
+          $expired_record_href->{'contact_first_name'};
+
+        $notifications_for{$unique_id}{'contact_last_name'} =
+          $expired_record_href->{'contact_last_name'};
+
+        $notifications_for{$unique_id}{expired_record_list} =
+          _push_and_return_programs(
             $notifications_for{$unique_id}{expired_record_list},
             $expired_record_href->{'record_name'} );
 
@@ -140,11 +160,11 @@
         # We set the reminder count for all records to the
         # highest count found in any record.
         if ( !( defined $notifications_for{$unique_id}{'reminders_sent'} )
-            || $expired_record_href->{'reminders_sent'}
-            > $notifications_for{$unique_id}{'reminders_sent'} )
+            || $expired_record_href->{'reminders_sent'} >
+            $notifications_for{$unique_id}{'reminders_sent'} )
         {
-            $notifications_for{$unique_id}{reminders_sent}
-                = $expired_record_href->{'reminders_sent'};
+            $notifications_for{$unique_id}{reminders_sent} =
+              $expired_record_href->{'reminders_sent'};
         }
     }
     return %notifications_for;
@@ -154,21 +174,20 @@
     my $template_file        = shift;
     my $email_constants_href = shift;
     croak "Second parameter must be a hash reference"
-        if ref $email_constants_href ne 'HASH';
+      if ref $email_constants_href ne 'HASH';
     my %notifications = @_;
     my @emails;
 
     foreach my $unique_id ( keys %notifications ) {
-        my $to
-            = qq{$notifications{$unique_id}{contact_first_name} $notifications{$unique_id}{contact_last_name} <$notifications{$unique_id}{contact_email}>};
+        my $to =
+qq{$notifications{$unique_id}{contact_first_name} $notifications{$unique_id}{contact_last_name} <$notifications{$unique_id}{contact_email}>};
 
         my $email = MIME::Entity->build(
-            Type => 'text/plain',
-            From => $email_constants_href->{from},
-            To   => $to,
-            Subject =>
-                _prepare_standard_subject( $notifications{$unique_id} ),
-            Data => _prepare_standard_body(
+            Type    => 'text/plain',
+            From    => $email_constants_href->{from},
+            To      => $to,
+            Subject => _prepare_standard_subject( $notifications{$unique_id} ),
+            Data    => _prepare_standard_body(
                 $template_file, $email_constants_href,
                 $notifications{$unique_id}
             ),
@@ -182,26 +201,25 @@
     my $template_file        = shift;
     my $email_constants_href = shift;
     croak "Second parameter must be a hash reference"
-        if ref $email_constants_href ne 'HASH';
+      if ref $email_constants_href ne 'HASH';
     my %notifications = @_;
     my @emails;
 
     foreach my $unique_id ( keys %notifications ) {
         next
-            if $notifications{$unique_id}{'contact_email'} eq
-                $notifications{$unique_id}{'agency_contact_email'};
+          if $notifications{$unique_id}{'contact_email'} eq
+              $notifications{$unique_id}{'agency_contact_email'};
 
-        my $to
-            = qq{$notifications{$unique_id}{contact_first_name} $notifications{$unique_id}{contact_last_name} <$notifications{$unique_id}{contact_email}>};
+        my $to =
+qq{$notifications{$unique_id}{contact_first_name} $notifications{$unique_id}{contact_last_name} <$notifications{$unique_id}{contact_email}>};
 
         my $email = MIME::Entity->build(
-            Type => 'text/plain',
-            From => $email_constants_href->{from},
-            To   => $to,
-            CC   => $notifications{$unique_id}{'agency_contact_email'},
-            Subject =>
-                _prepare_standard_subject( $notifications{$unique_id} ),
-            Data => _prepare_standard_body(
+            Type    => 'text/plain',
+            From    => $email_constants_href->{from},
+            To      => $to,
+            CC      => $notifications{$unique_id}{'agency_contact_email'},
+            Subject => _prepare_standard_subject( $notifications{$unique_id} ),
+            Data    => _prepare_standard_body(
                 $template_file, $email_constants_href,
                 $notifications{$unique_id}
             ),
@@ -215,27 +233,26 @@
     my $template_file        = shift;
     my $email_constants_href = shift;
     croak "Second parameter must be a hash reference"
-        if ref $email_constants_href ne 'HASH';
+      if ref $email_constants_href ne 'HASH';
     my %notifications = @_;
     my @emails;
 
     foreach my $unique_id ( keys %notifications ) {
         next
-            if !( defined $notifications{$unique_id}{'public_email'} )
-                || $notifications{$unique_id}{'contact_email'} eq
-                $notifications{$unique_id}{'public_email'};
+          if !( defined $notifications{$unique_id}{'public_email'} )
+              || $notifications{$unique_id}{'contact_email'} eq
+              $notifications{$unique_id}{'public_email'};
 
-        my $contact_email
-            = qq{$notifications{$unique_id}{contact_first_name} $notifications{$unique_id}{contact_last_name} <$notifications{$unique_id}{contact_email}>};
+        my $contact_email =
+qq{$notifications{$unique_id}{contact_first_name} $notifications{$unique_id}{contact_last_name} <$notifications{$unique_id}{contact_email}>};
 
         my $email = MIME::Entity->build(
-            Type => 'text/plain',
-            From => $email_constants_href->{from},
-            To   => $notifications{$unique_id}{'public_email'},
-            CC   => $contact_email,
-            Subject =>
-                _prepare_standard_subject( $notifications{$unique_id} ),
-            Data => _prepare_standard_body(
+            Type    => 'text/plain',
+            From    => $email_constants_href->{from},
+            To      => $notifications{$unique_id}{'public_email'},
+            CC      => $contact_email,
+            Subject => _prepare_standard_subject( $notifications{$unique_id} ),
+            Data    => _prepare_standard_body(
                 $template_file, $email_constants_href,
                 $notifications{$unique_id}
             ),
@@ -259,56 +276,55 @@
     }
     MVHub::Utils::assert(
         $out_count == $in_count,
-        "\n****Record count mismatch****\n input records = $in_count\n output records = $out_count"
+"\n****Record count mismatch****\n input records = $in_count\n output records = $out_count"
     );
 }
 
 sub get_email_constants_from {
     my $cfg = shift;
     croak "Parameter must be a Config::Simple object"
-        if ref $cfg ne 'Config::Simple';
+      if ref $cfg ne 'Config::Simple';
 
     my %email_constants;
     my $error_msg = '';
 
     $email_constants{admin_name} = $cfg->param('NOTIFICATION.admin_name')
-        or $error_msg .= "\tNOTIFICATION.admin_name\n";
+      or $error_msg .= "\tNOTIFICATION.admin_name\n";
     $email_constants{admin_email} = $cfg->param('NOTIFICATION.admin_email')
-        or $error_msg .= "\tNOTIFICATION.admin_email\n";
+      or $error_msg .= "\tNOTIFICATION.admin_email\n";
     $email_constants{admin_phone} = $cfg->param('NOTIFICATION.admin_phone')
-        or $error_msg .= "\tNOTIFICATION.admin_phone\n";
+      or $error_msg .= "\tNOTIFICATION.admin_phone\n";
     $email_constants{team_name} = $cfg->param('NOTIFICATION.team_name')
-        or $error_msg .= "\tNOTIFICATION.team_name\n";
+      or $error_msg .= "\tNOTIFICATION.team_name\n";
     $email_constants{website_name} = $cfg->param('SITE.website_name')
-        or $error_msg .= "\tSITE.website_name\n";
+      or $error_msg .= "\tSITE.website_name\n";
 
     my $config_filename = $cfg->param('META.this_config_file');
 
     if ($error_msg) {
-        $error_msg
-            = "config file: ${config_filename}\nmissing keys: \n$error_msg";
+        $error_msg =
+          "config file: ${config_filename}\nmissing keys: \n$error_msg";
         croak $error_msg;
     }
 
-    $email_constants{from}
-        = "$email_constants{admin_name} <$email_constants{admin_email}>";
+    $email_constants{from} =
+      "$email_constants{admin_name} <$email_constants{admin_email}>";
 
     return \%email_constants;
 }
 
 sub get_expired_records {
     scalar @_ == 8
-        or croak "Bad number of parameters";
+      or croak "Bad number of parameters";
     my %param = @_;
 
-    my $sql
-        = MVHub::Utils::DB::get_sql_select_statement( $param{'query_name'} );
+    my $sql =
+      MVHub::Utils::DB::get_sql_select_statement( $param{'query_name'} );
 
-    my @bind_variables
-        = ( $param{'expire_date'}, $param{'max_notifications'} );
+    my @bind_variables = ( $param{'expire_date'}, $param{'max_notifications'} );
 
     return $param{'dbh'}
-        ->selectall_arrayref( $sql, { Slice => {} }, @bind_variables );
+      ->selectall_arrayref( $sql, { Slice => {} }, @bind_variables );
 }
 
 sub increment_reminder_levels_for {
@@ -317,9 +333,9 @@
     my $max_notifications = shift;
     foreach my $unique_id ( keys %$reminders_href ) {
         warn
-            "SHOULDN'T SEE THIS: Reminder count greater than MAX_REMINDER for $unique_id\n"
-            if ( $reminders_href->{$unique_id}{'reminders_sent'}
-            >= $max_notifications );
+"SHOULDN'T SEE THIS: Reminder count greater than MAX_REMINDER for $unique_id\n"
+          if ( $reminders_href->{$unique_id}{'reminders_sent'} >=
+            $max_notifications );
         $reminders_href->{$unique_id}{'reminders_sent'} += 1;
     }
 }
@@ -340,7 +356,7 @@
 
 sub set_reminder_level_in_db {
     scalar @_ == 4
-        or croak "Bad number of parameters";
+      or croak "Bad number of parameters";
     my $dbh            = shift;
     my $expire_date    = shift;
     my $reminders_href = shift;
@@ -360,21 +376,21 @@
 
 sub shift_off_notifications_with_reminder_count {
     scalar @_ == 2
-        or croak "Bad number of parameters";
+      or croak "Bad number of parameters";
 
     my $reminder_count     = shift;
     my $notifications_href = shift;
     croak "Second Parameter not a hash reference"
-        if ref $notifications_href ne 'HASH';
+      if ref $notifications_href ne 'HASH';
 
     my %shifted_reminders;
 
     foreach my $unique_id ( keys %$notifications_href ) {
-        if ( $$notifications_href{$unique_id}{reminders_sent}
-            == $reminder_count )
+        if ( $$notifications_href{$unique_id}{reminders_sent} ==
+            $reminder_count )
         {
-            $shifted_reminders{$unique_id}
-                = delete $$notifications_href{$unique_id};
+            $shifted_reminders{$unique_id} =
+              delete $$notifications_href{$unique_id};
         }
     }
     return %shifted_reminders;


Follow ups