Guest User

logbook_pdf_report

a guest
Sep 15th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.82 KB | None | 0 0
  1. #! /usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use DBI;
  7. use DateTime::Format::MySQL;
  8. use HTML::Restrict;
  9.  
  10. # html remover
  11. my $hr = HTML::Restrict->new();
  12.  
  13. # database connection stuff
  14. my $dbh = DBI->connect(          
  15.     "dbi:mysql:dbname=olin2",
  16.     "user",                          
  17.     "password",                          
  18.     { RaiseError => 1 },        
  19. ) or die $DBI::errstr;
  20.  
  21.  
  22. # query to grab ALL entries from `olin2`.`lobby`, ordered by date
  23. my $sth = $dbh->prepare("select l.id, l.date_added, l.username,
  24.             l.entry
  25.             from logbook l
  26.             order by l.date_added desc");
  27. $sth->execute();
  28.  
  29. # the LaTeX document preamble
  30. print<<EOF;
  31. \\documentclass{article}
  32. \\usepackage{framed,graphicx,xcolor}
  33. \\usepackage[top=.5in, bottom=.5in, left=.5in, right=.5in]{geometry}
  34. \\usepackage{etoolbox}
  35. \\BeforeBeginEnvironment{shaded}{\\begin{center} \\noindent\\begin{minipage}{.9\\linewidth}}
  36. \\AfterEndEnvironment{shaded}{ \\end{minipage}\\end{center} }
  37. \\raggedbottom
  38. \\setlength{\\parskip}{.2em}
  39. \\setlength{\\parindent}{0cm}
  40. \\nonstopmode
  41. \\setlength{\\topsep}{0pt}
  42.  
  43. \\begin{document}
  44. \\definecolor{shadecolor}{gray}{.9}
  45.  
  46. EOF
  47.  
  48. my $row;
  49.  
  50. # set the `logbook` variables
  51. while ($row = $sth->fetchrow_hashref()) {
  52.     my  $id = $row->{id};
  53.     my  $date_added = DateTime::Format::MySQL->parse_timestamp($row->{date_added});
  54.     my  $username = $row->{username};
  55.     my  $entry = $row->{entry};
  56.  
  57.     # filter some stuff out to make LaTeX behave
  58.     $entry =~ s/<br \/>/\\\\/g;
  59.     $entry =~ s/\\//g;
  60.     $entry =~ s/\#/\\#/g;
  61.     $entry =~ s/\&/\\&/g;
  62.     $entry =~ s/\_/\\_/g;
  63.    
  64.     # filters out HTML tags
  65.     $entry = $hr->process($entry);
  66.  
  67.     # get the first name of the `logbook`.`entry` author
  68.     my $fname_query = 'select fname from users where username = ?';
  69.     my @row1 = $dbh->selectrow_array($fname_query,undef,$username);
  70.     my $fname = $row1[0];
  71.    
  72.     #print " \\fbox{";
  73.  
  74.     # each logbook entry is in a minipage (prevents line breaks in the middle
  75.     # of an entry)
  76.     print " \\begin{minipage}{\\linewidth}";
  77.  
  78.     # the date and author are in a table (easier to read)
  79.     print " \\begin{tabular}{l r} \\\\ ";
  80.     print $date_added->strftime("%a, %d %b %Y at %l:%M %p & ") .
  81.         'By: ' . $fname .
  82.         '\\end{tabular} \\\\ \\vspace{.2em} \\\\' . $entry . ' \\smallskip ';
  83.    
  84.     # query to grab the comments associated with this particular logbook entry
  85.     my $comment_sth = $dbh->prepare("select c.date_added as cdate, c.username cuser, c.comment from comments as c
  86.                         where c.logbook_id = ?
  87.                     order by c.date_added asc");
  88.     $comment_sth->execute($id);
  89.  
  90.     # set up the comments variables
  91.     while (my $row = $comment_sth->fetchrow_hashref()) {
  92.         if(defined($row->{comment}) && $row->{comment} ne '') {
  93.             # comments are shaded to stand out
  94.             print "\\begin{shaded}";   
  95.             my  $comment_date = DateTime::Format::MySQL->parse_timestamp($row->{cdate});
  96.             my  $comment_username = $row->{cuser};
  97.             my  $comment = $row->{comment};
  98.            
  99.             # filter some stuff out so LaTeX behaves
  100.             $comment =~ s/<br \/>//g;
  101.             $comment =~ s/\\//g;
  102.             $comment=~ s/\#/\\#/g;
  103.             $comment =~ s/\&/\\&/g;
  104.             $comment =~ tr/\n//d;
  105.             $comment =~ s/\_/\\_/g;
  106.  
  107.             # filters out HTML
  108.             $comment = $hr->process($comment);
  109.    
  110.             # gets the comment author's first name (functionize this!) 
  111.             my $fname_query = 'select fname from users where username = ?';
  112.             my @row2 = $dbh->selectrow_array($fname_query,undef,$comment_username);
  113.             my $comment_fname = $row2[0];
  114.            
  115.             print "Date: " . $comment_date->strftime("%a, %d %b %Y at %l:%M %p") . " \\\\ ";
  116.             print "By: $comment_fname \\\\";
  117.             print "$comment \\\\ ";
  118.             print "\\end{shaded} ";
  119.         }
  120.     }
  121. print '\\vspace{.1em} \\\\ ';
  122. print "\\end{minipage}";
  123. print " \\hrule"; # seperates the logbook entries from each other
  124.  
  125. } # end of the main loop   
  126.  
  127. print '\end{document}';
  128.  
  129. # clean up
  130. $sth->finish();
  131. $dbh->disconnect();
Advertisement
Add Comment
Please, Sign In to add comment