--- loncom/interface/lonnotify.pm	2005/12/09 20:54:26	1.10
+++ loncom/interface/lonnotify.pm	2006/03/23 22:32:10	1.15
@@ -56,49 +56,78 @@ sub handler {
     my $origin = $env{'form.origin'};
 
     &Apache::lonhtmlcommon::clear_breadcrumbs();
-    my %ltext=&Apache::lonlocal::texthash(
-               'note' => 'Notification E-mail',
-    );
+
     my $function = &Apache::loncommon::get_users_function();
-    my $loadcode;
     my $tablecolor = &Apache::loncommon::designparm($function.'.tabbg');
-    if ((defined($env{'form.origin'})) && ($command eq 'compose' || $command eq 'pick_target' || $command eq 'pick_display')) {
-        unless ($env{'form.origin'} eq '') {
-            $loadcode = 'javascript:setFormElements(document.'.$env{'form.command'}.')';
-            if (($command eq 'pick_target') && (
-                     ($origin eq 'compose') || ($origin eq 'process'))) {
-                if ($env{'form.coursepick'} eq 'category') {
-                    $loadcode .= ';javascript:setCourseCat(document.'.$env{'form.command'}.')';
-                }
-            }
-        }
-    }
-    my $loaditems = ' onLoad="'.$loadcode.'" '; 
-    my $bodytag = &Apache::loncommon::bodytag('Broadcast e-mail to users',$function,$loaditems);
-    my $html=&Apache::lonxml::xmlbegin();
+
     &Apache::lonhtmlcommon::add_breadcrumb
         ({href=>'/adm/notify',
           text=>"Broadcast E-mail"});
     if ($command eq 'process') {
-        &print_request_receipt($r,$command,$cdom,$tablecolor,$bodytag,$html,\%ltext);
+        &print_request_receipt($r,$command,$cdom,$tablecolor);
     } elsif ($command eq 'compose') {
-        &print_composition_form($r,$command,$cdom,$tablecolor,$bodytag,$html,\%ltext);
+        &print_composition_form($r,$command,$cdom,$tablecolor);
     } elsif ($command eq 'pick_target') {
-        &print_selection_form($r,$command,$cdom,$tablecolor,$bodytag,$html,\%ltext);
+        &print_selection_form($r,$command,$cdom,$tablecolor);
     } elsif ($command eq 'pick_display') {
-        &print_display_option_form($r,$command,$cdom,$tablecolor,$bodytag,$html,\%ltext);
+        &print_display_option_form($r,$command,$cdom,$tablecolor);
     } elsif ($command eq 'display') {
-        &print_display($r,$command,$cdom,$tablecolor,$bodytag,$html,\%ltext);
+        &print_display($r,$command,$cdom,$tablecolor);
     } else {
-        &print_front_page($r,'front',$cdom,$tablecolor,$bodytag,$html,\%ltext);
+        &print_front_page($r,'front',$cdom,$tablecolor);
     }
     return OK;
 }
 
+sub add_script {
+    my ($js) = @_;
+    return '<script type="text/javascript">'."\n".$js."\n".'</script>';
+}
+
+sub start_page {
+    my ($jscript,$bread_title,$formname) = @_;
+
+    my $loadcode;
+    if ((defined($env{'form.origin'})) 
+	&& ($env{'form.command'} eq 'compose' 
+	    || $env{'form.command'} eq 'pick_target' 
+	    || $env{'form.command'} eq 'pick_display')) {
+        if ($env{'form.origin'} ne '') {
+            $loadcode = 'javascript:setFormElements(document.'.$env{'form.command'}.')';
+            if (($env{'form.command'} eq 'pick_target') 
+		&& (($env{'form.origin'} eq 'compose')
+		    || ($env{'form.origin'} eq 'process'))) {
+                if ($env{'form.coursepick'} eq 'category') {
+                    $loadcode .= ';javascript:setCourseCat(document.'.$env{'form.command'}.')';
+                }
+            }
+        }
+    }
+    
+    $loadcode = ' onLoad="'.$loadcode.'" '; 
+    my $start_page = 
+	&Apache::loncommon::start_page('Broadcast e-mail to users', $jscript,
+				       {'add_entries' => $loadcode,});
+    my $breadcrumbs =
+	&Apache::lonhtmlcommon::breadcrumbs(undef,$bread_title,
+					    'Broadcast_system_email');
+    my $output = <<"ENDONE";
+$start_page
+$breadcrumbs
+<br />
+<form name="$formname" method="POST">
+ENDONE
+
+    return $output;
+}
+
+sub end_page {
+    return '</form>'.&Apache::loncommon::end_page();
+}
+
 sub print_front_page {
-    my ($r,$formname,$cdom,$tablecolor,$bodytag,$html,$ltext) = @_;
-    my $breadcrumbs = &Apache::lonhtmlcommon::breadcrumbs
-            (undef,'Broadcast e-mail to Domain','Broadcast_system_email');
+    my ($r,$formname,$cdom,$tablecolor) = @_;
+
     my $jscript = qq|
 function next_page(caller) {
     if (caller == 'view') {
@@ -110,23 +139,11 @@ function next_page(caller) {
     document.front.submit()
 }
     |; 
-    my %lt=&Apache::lonlocal::texthash(
-               'note' => 'Notification E-mail',
-    );
-    my $output = <<"ENDONE";
-$html
-<head>
- <title>LON-CAPA $lt{'note'}</title>
-<script type"text/javascript">
-$jscript
-</script>
-</head>
-$bodytag
-$breadcrumbs
-<br />
-ENDONE
-    $output .= '<form name="'.$formname.'" method="post">'.
-              '<input type="hidden" name="command" />';
+
+    my $output = &start_page(&add_script($jscript),
+			     'Broadcast e-mail to Domain', $formname);
+
+    $output .= '<input type="hidden" name="command" />';
     $output .= &Apache::lonhtmlcommon::start_pick_box();
     $output .= '<table cellspacing="8" cellpadding="8">'.
               '<tr><td><a href="javascript:next_page('."'new'".')">'.
@@ -135,29 +152,23 @@ ENDONE
               'Display e-mail sent by Domain Coordinators in this domain'.
               '</a></td></tr></table>';
     $output .= &Apache::lonhtmlcommon::end_pick_box();
-    $output .= qq(
-</form>
-</body>
-</html>);
+    $output .= &end_page();
+
     $r->print($output);
     return;
 }
 
 sub print_display_option_form {
-    my ($r,$formname,$cdom,$tablecolor,$bodytag,$html,$ltext) = @_;
-    &Apache::lonhtmlcommon::add_breadcrumb
-         ({text=>"Display options"});
-    my $breadcrumbs = &Apache::lonhtmlcommon::breadcrumbs
-            (undef,'Broadcast e-mail display options','Broadcast_system_email');
+    my ($r,$formname,$cdom,$tablecolor) = @_;
+    &Apache::lonhtmlcommon::add_breadcrumb({text=>"Display options"});
+
     my $table_width = '';
     my $col_width = '200';
     my $cmd = 'display';
     my $submit_text = 'Display e-mail';
     my @roles = ('dc');
     my $now = time;
-    my %lt=&Apache::lonlocal::texthash(
-               'note' => 'Notification E-mail',
-    );
+
     my $startdateform = &Apache::lonhtmlcommon::date_setter($formname,
                                                             'startdate',
                                                             $now);
@@ -180,19 +191,10 @@ sub print_display_option_form {
         sender => 'checkbox',
     );
     my $jscript = &Apache::lonhtmlcommon::set_form_elements(\%elements);
-    my $output = <<"ENDONE";
-$html
-<head>
- <title>LON-CAPA $lt{'note'}</title>
-<script type"text/javascript">
-$jscript
-</script>
-</head>
-$bodytag
-$breadcrumbs
-<br />
-<form method="post" name="$formname">
-ENDONE
+
+    my $output = &start_page(&add_script($jscript),
+			     'Broadcast e-mail display options', $formname);
+
     $output .= &Apache::lonhtmlcommon::start_pick_box($table_width);
     $output .= &Apache::lonhtmlcommon::row_title($col_width,$tablecolor,&mt('Date range'));
     $output .= '<td><table><tr><td>Earliest to display: </td><td>'.
@@ -218,23 +220,19 @@ ENDONE
     $output .= &Apache::lonhtmlcommon::row_closure();
     $output .= &Apache::lonhtmlcommon::submit_row($col_width,$tablecolor,&mt('Submit'),$cmd,$submit_text);
     $output .= &Apache::lonhtmlcommon::end_pick_box();
-    $output .= qq(
-<input type="hidden" name="sortby" value="date" />
-</form>
-</body>
-</html>);
+    $output .= qq(<input type="hidden" name="sortby" value="date" />\n).
+	&end_page();
     $r->print($output);
     return;
 }
 
 sub print_display {
-    my ($r,$formname,$cdom,$tablecolor,$bodytag,$html,$ltext) = @_;
+    my ($r,$formname,$cdom,$tablecolor) = @_;
     &Apache::lonhtmlcommon::add_breadcrumb
          ({href=>"javascript:goBack('pick_display')",
           text=>"Display options"},
          {text=>"E-mail display"});
-    my $breadcrumbs = &Apache::lonhtmlcommon::breadcrumbs
-            (undef,'Display Broadcast e-mail','Broadcast_system_email');
+
     my $table_width = '';
     my $col_width = '200';
     my $rowColor1 = "#ffffff";
@@ -259,19 +257,9 @@ function goBack(target) {
 }
 
 ENDSCRIPT
-    my $output = <<"ENDONE";
-$html
-<head>
- <title>LON-CAPA $$ltext{'note'}</title>
- <script type"text/javascript">
-$jscript
- </script>
-</head>
-$bodytag
-$breadcrumbs
-<br />
-<form method="post" name="$formname">
-ENDONE
+
+    my $output = &start_page(&add_script($jscript),
+			     'Display Broadcast e-mail', $formname);
 
     foreach my $msgid (keys(%sentmail)) {
         my %content = &Apache::lonmsg::unpackagemsg($sentmail{$msgid});
@@ -306,7 +294,9 @@ ENDONE
                     $rowColor = $rowColor2;
                 }
                 my $recipients = '';
-                my ($date,$subj,$sname,$sdom,$cdom) = split(/:/,$msgid,5);
+                my ($date,$subj,$sname,$sdom) =
+                                  &Apache::lonmsg::unpackmsgid($msgid,undef,1);
+                $subj = &Apache::lonnet::escape($subj);
                 $date = &Apache::lonlocal::locallocaltime($date);
                 foreach my $user (sort(keys(%{$dcmail{$msgid}{recipient}}))) {
                     $recipients .= $dcmail{$msgid}{recipient}{$user}.', ';
@@ -317,7 +307,9 @@ ENDONE
             }
         } else {
             foreach my $msgid (sort(keys(%dcmail))) {
-                my ($date,$subj,$sname,$sdom,$cdom) = split(/:/,$msgid,5);
+                my ($date,$subj,$sname,$sdom) =
+                                   &Apache::lonmsg::unpackmsgid($msgid,undef,1);
+                $subj = &Apache::lonnet::escape($subj);
                 if ($env{'form.sortby'} eq 'subject') {
                     push @{$Sortby{$dcmail{$msgid}{subject}}},$msgid;
                 } elsif ($env{'form.sortby'} eq 'message') {
@@ -351,8 +343,9 @@ ENDONE
                         }
                         $recipients =~ s/,\s$//;
                     }
-
-                    my ($date,$subj,$sname,$sdom,$cdom) = split(/:/,$msgid,5);
+                    my ($date,$subj,$sname,$sdom) =
+                                   &Apache::lonmsg::unpackmsgid($msgid,undef,1);
+                    $subj = &Apache::lonnet::escape($subj);
                     $date = &Apache::lonlocal::locallocaltime($date);
                     $output .=  '<tr bgcolor="'.$rowColor.'"><td><small>'.$date.'</small></td><td><small>'.&cr_to_br($dcmail{$msgid}{subject}).'</small></td><td><small>'.$sname.':'.$sdom.'</small></td><td><small>'.&cr_to_br($dcmail{$msgid}{message}).'</small></td><td><small>'.$recipients.'</small></td></tr>'."\n";
                     $rowNum ++;
@@ -374,16 +367,13 @@ ENDONE
     $output .= qq(<input type="hidden" name="origin" value="$formname" />\n);
     $output .= qq(<input type="hidden" name="command" />\n);
     $output .= qq(<input type="hidden" name="sortby" value="$curr_sortby" />\n);
-    $output .= qq(
-</form>
-</body
-</html>);
+    $output .= &end_page();
     $r->print($output);
     return;
 }
 
 sub print_selection_form {
-    my ($r,$formname,$cdom,$tablecolor,$bodytag,$html,$ltext) = @_;
+    my ($r,$formname,$cdom,$tablecolor) = @_;
     my %coursecodes = ();
     my %codes = ();
     my @codetitles = ();
@@ -399,7 +389,6 @@ sub print_selection_form {
     my $table_width = '100%';
     my $col_width = '200';
     my %lt=&Apache::lonlocal::texthash(
-               'note' => 'Notification E-mail', 
                'buil' => 'Building valid e-mail address from username, if missing from preferences:',
                'kerb' => 'Kerberos: enter default for each realm used in the domain, with comma separation of entries',
                'infs' => 'Internal, Filesystem and Local authentication: enter single default.',
@@ -418,8 +407,6 @@ sub print_selection_form {
         }
     }
 
-    my $breadcrumbs = &Apache::lonhtmlcommon::breadcrumbs
-            (undef,'Choose e-mail audience','Broadcast_system_email');
     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($cdom);
 
     my %elements = (
@@ -469,48 +456,36 @@ function setCourseCat(formname) {
 }
 |; 
     }
-    my $output = <<"ENDONE";
-$html
-<head>
- <title>LON-CAPA $lt{'note'}</title>
-<script type"text/javascript">
-$jscript
-</script>
-$cb_jscript
-</head>
-$bodytag
-$breadcrumbs
-<br />
-<form method="post" name="$formname">
-ENDONE
-   $output .= &Apache::lonhtmlcommon::start_pick_box($table_width);
-   my @roles = ('ow','cc','in','ta','ep','st','cr');
-   my %longtypes = ();
-   my %authtypes = ();
-   &form_elements(\%longtypes,\%authtypes);
-   my $descrip = $lt{'buil'}.' 
+
+
+    my $output = &start_page(&add_script($jscript).$cb_jscript,
+			     'Choose e-mail audience', $formname);
+
+    $output .= &Apache::lonhtmlcommon::start_pick_box($table_width);
+    my @roles = ('ow','cc','in','ta','ep','st','cr');
+    my %longtypes = ();
+    my %authtypes = ();
+    &form_elements(\%longtypes,\%authtypes);
+    my $descrip = $lt{'buil'}.' 
 <ul>
 <li>'.$lt{'kerb'}.'<br />(e.g., MSU.EDU=msu.edu, MSUE.EDU=msue.msu.edu).</li>
 <li>'.$lt{'infs'}.'</li>
 </ul>'."\n";
-   my $submit_text = $lt{'comp'};
-   my $cmd = 'compose';
-   $output .= &Apache::lonhtmlcommon::role_select_row(\@roles,$col_width,$tablecolor,'Roles');
-   $output .= &Apache::lonhtmlcommon::course_select_row($col_width,$tablecolor,'Courses',$formname,$totcodes,\@codetitles,\%idlist,\%idlist_titles);
-   $output .= &Apache::lonhtmlcommon::status_select_row(\%longtypes,$col_width,$tablecolor,&mt('Access status'));
-   $output .= &Apache::lonhtmlcommon::email_default_row(\%authtypes,$col_width,$tablecolor,&mt('Username -> Email conversion'),$descrip);
-   $output .= &Apache::lonhtmlcommon::submit_row($col_width,$tablecolor,&mt('Submit'),$cmd,$submit_text);
-   $output .= &Apache::lonhtmlcommon::end_pick_box();
-   $output .= qq(
-</form>
-</body>
-</html>);
+    my $submit_text = $lt{'comp'};
+    my $cmd = 'compose';
+    $output .= &Apache::lonhtmlcommon::role_select_row(\@roles,$col_width,$tablecolor,'Roles');
+    $output .= &Apache::lonhtmlcommon::course_select_row($col_width,$tablecolor,'Courses',$formname,$totcodes,\@codetitles,\%idlist,\%idlist_titles);
+    $output .= &Apache::lonhtmlcommon::status_select_row(\%longtypes,$col_width,$tablecolor,&mt('Access status'));
+    $output .= &Apache::lonhtmlcommon::email_default_row(\%authtypes,$col_width,$tablecolor,&mt('Username -> Email conversion'),$descrip);
+    $output .= &Apache::lonhtmlcommon::submit_row($col_width,$tablecolor,&mt('Submit'),$cmd,$submit_text);
+    $output .= &Apache::lonhtmlcommon::end_pick_box();
+    $output .= &end_page();
     $r->print($output);
     return;
 }
 
 sub print_composition_form {
-    my ($r,$formname,$cdom,$tablecolor,$bodytag,$html,$ltext) = @_;
+    my ($r,$formname,$cdom,$tablecolor) = @_;
     &Apache::lonhtmlcommon::add_breadcrumb
         ({href=>"javascript:goBack('pick_target')",
           text=>"Select Audience"},
@@ -522,11 +497,8 @@ function goBack(target) {
     document.$formname.submit();
 }
 |;
-    my $breadcrumbs = (&Apache::lonhtmlcommon::breadcrumbs
-        (undef,'Broadcast e-mail to users','Broadcast_system_email'));
 
     my %lt=&Apache::lonlocal::texthash(
-                      'note' => 'Notification E-mail',
                       'nore' => 'No recipients identified',
                       'emad' => 'e-mail address',
                    );
@@ -538,17 +510,9 @@ function goBack(target) {
                    );
     $jscript .= &Apache::lonhtmlcommon::set_form_elements(\%elements);
 
-    $r->print(<<ENDONE);
-$html
-<head>
- <title>LON-CAPA $lt{'note'}</title>
-<script type="text/javascript">
-$jscript
-</script>
-</head>
-$bodytag $breadcrumbs
-<br /> 
-ENDONE
+    $r->print(&start_page(&add_script($jscript),
+			  'Broadcast e-mail to users', $formname));
+
     my $coursefilter = $env{'form.coursepick'};
     my %courses = ();
     if ($coursefilter eq 'all') {
@@ -641,7 +605,7 @@ ENDONE
             }
         }
     }
-    my $output  = '<form name="'.$formname.'" method="post">'."\n";
+    my $output;
   
     if ($totalrecip > 0) {
         $output .= &Apache::lonhtmlcommon::start_pick_box($table_width);
@@ -688,14 +652,14 @@ ENDONE
     }
     $output .= '<input type="hidden" name="origin" value="'.$formname.'" />'."\n";
     $output .= &Apache::lonhtmlcommon::echo_form_input(['command','origin','subject','message','recipient','sender'],);
-    $output .= '</form></body></html>';
+    $output .= &end_page();
     $r->print($output);
     return;
 }
 
 
 sub print_request_receipt {
-    my ($r,$formname,$dom,$tablecolor,$bodytag,$html,$ltext) =@_;
+    my ($r,$formname,$dom,$tablecolor) =@_;
     my @recipients = &Apache::loncommon::get_env_multiple('form.recipient');
     my $subject = $env{'form.subject'};
     my $message = $env{'form.message'};
@@ -706,6 +670,7 @@ function goBack(target) {
     document.$formname.submit();
 }
 ENDSCRIPT
+
     &Apache::lonhtmlcommon::add_breadcrumb
         ({href=>"javascript:goBack('pick_target')",
           text=>"Select audience"});
@@ -715,21 +680,11 @@ ENDSCRIPT
     &Apache::lonhtmlcommon::add_breadcrumb
         ({href=>"/adm/notify?command=process",
           text=>"Outcome"});
-    my $breadcrumbs = &Apache::lonhtmlcommon::breadcrumbs
-            (undef,'E-mail Delivery','Broadcast_system_email');
-    my $output = <<ENDONE;
-$html
-<head>
- <title>LON-CAPA Notification E-mail</title>
-<script type="text/javascript">
-$jscript
-</script>
-</head>
-$bodytag
-$breadcrumbs
-<br />
-<form name="$formname" method="post">
-ENDONE
+
+
+    my $output = &start_page(&add_script($jscript), 'E-mail Delivery',
+			     $formname);
+
     $output .= &Apache::lonhtmlcommon::start_pick_box();
     my @deliveries = ();
     &broadcast_email(\@recipients,$subject,$from,$message,\@deliveries);
@@ -766,16 +721,13 @@ ENDONE
     $output .= '<input type="hidden" name="command" />'."\n".
                '<input type="hidden" name="origin" value="'.$formname.'" />'."\n";
     $output .= &Apache::lonhtmlcommon::echo_form_input(['command','origin']);
-    $output .= '
-</form>
-</body>
-</html>';
+    $output .= &end_page();
     $r->print($output);
     return;
 }
 
 sub broadcast_email {
-    my ($recipients,$subject,$from,$message,$deliveries,$ltext)=@_;
+    my ($recipients,$subject,$from,$message,$deliveries)=@_;
 # Should implement staggered delivery for large numbers of recipients?.
     foreach my $user (@{$recipients}) {
         my $msg = new Mail::Send;
@@ -822,7 +774,7 @@ sub get_user_info {
 }
 
 sub form_elements {
-   my ($longtypes,$authtypes,$ltext) = @_;
+   my ($longtypes,$authtypes) = @_;
    %{$longtypes} = (
                    active => 'Currently has access',
                    previous => 'Previously had access',
@@ -839,7 +791,7 @@ sub form_elements {
 }
 
 sub store_mail {
-    my ($subject,$message,$domain,$recipients,$attachmenturl,$ltext) = @_;
+    my ($subject,$message,$domain,$recipients,$attachmenturl) = @_;
     my $msgid;
     ($msgid,$message) = &Apache::lonmsg::packagemsg($subject,$message,undef,undef,
                         $attachmenturl,$recipients,undef,undef,'dcmail');