Added support for lists.
authorocto <octo>
Sun, 24 Apr 2005 15:20:34 +0000 (15:20 +0000)
committerocto <octo>
Sun, 24 Apr 2005 15:20:34 +0000 (15:20 +0000)
More CSS, esp. printing, fixes.

book.cgi

index cf4653e..4af14ad 100755 (executable)
--- a/book.cgi
+++ b/book.cgi
@@ -39,6 +39,7 @@ our %Actions =
        default => [\&html_start, \&action_browse,  \&html_end],
        detail  => [\&html_start, \&action_detail,  \&html_end],
        edit    => [\&html_start, \&action_edit,    \&html_end],
+       list    => [\&html_start, \&action_list,    \&html_end],
        save    => [\&html_start, \&action_save,    \&html_end],
        search  => [\&html_start, \&action_search,  \&html_end],
        verify  => [\&html_start, \&action_verify,  \&html_end],
@@ -159,7 +160,81 @@ sub action_browse
        }
        print qq(\t\t</ul>\n\n);
 
-       print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse">back</a>]</div>\n) if ($group);
+       print qq(\t\t<div class="menu">\n);
+       if ($group)
+       {
+               my $group_esc = uri_escape ($group);
+               print qq(\t\t\t[<a href="$MySelf?action=list&group=$group_esc">List</a>]\n),
+               qq(\t\t\t[<a href="$MySelf?action=browse">Back</a>]\n);
+       }
+       else
+       {
+               print qq(\t\t\t[<a href="$MySelf?action=list">List</a>]\n);
+       }
+       print qq(\t\t</div>\n);
+}
+
+sub action_list
+{
+       my $group = param ('group');
+       $group = shift if (@_);
+       $group ||= '';
+
+       my $title = $group ? "List of group &quot;$group&quot;" : 'List of all addresses';
+       my @fields = (qw(address homephone cellphone officephone fax mail));
+
+       my @all = ();
+       if ($group)
+       {
+               @all = Person->search ([[group => $group]]);
+       }
+       else
+       {
+               @all = Person->search ();
+       }
+
+       print <<EOF;
+               <h2>$title</h2>
+
+               <table class="list">
+                       <tr>
+                               <th>Name</th>
+EOF
+       for (@fields)
+       {
+               print "\t\t\t\t<th>" . (defined ($FieldNames{$_}) ? $FieldNames{$_} : $_) . "</th>\n";
+       }
+       print "\t\t\t</tr>\n";
+
+       for (sort { $a->name () cmp $b->name () } (@all))
+       {
+               my $person = $_;
+               my $sn = $person->lastname ();
+               my $gn = $person->firstname ();
+
+               print "\t\t\t<tr>\n",
+               "\t\t\t\t<td>$sn, $gn</td>\n";
+
+               for (@fields)
+               {
+                       my $field = $_;
+                       my @values = $person->get ($field);
+                       print "\t\t\t\t<td>" . join ('<br />', @values) . "</td>\n";
+               }
+
+               print "\t\t\t</tr>\n";
+       }
+       print "\t\t</table>\n\n";
+
+       if ($group)
+       {
+               my $group_esc = uri_escape ($group);
+               print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse&group=$group_esc">Back</a>]</div>\n);
+       }
+       else
+       {
+               print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse">Back</a>]</div>\n);
+       }
 }
 
 sub action_detail
@@ -175,16 +250,16 @@ sub action_detail
                return;
        }
 
-       print qq(\t<h2>Details for $cn</h2>\n);
+       print qq(\t\t<h2>Details for $cn</h2>\n);
 
        my $cn_esc = uri_escape ($cn);
 
        print <<EOF;
-       <table class="detail">
-               <tr>
-                       <th>Name</th>
-                       <td>$cn</td>
-               </tr>
+               <table class="detail">
+                       <tr>
+                               <th>Name</th>
+                               <td>$cn</td>
+                       </tr>
 EOF
        for (@MultiFields)
        {
@@ -195,14 +270,14 @@ EOF
 
                next unless ($num);
 
-               print "\t\t<tr>\n";
+               print "\t\t\t<tr>\n";
                if ($num > 1)
                {
-                       print qq(\t\t\t<th rowspan="$num">$print</th>\n);
+                       print qq(\t\t\t\t<th rowspan="$num">$print</th>\n);
                }
                else
                {
-                       print qq(\t\t\t<th>$print</th>\n);
+                       print qq(\t\t\t\t<th>$print</th>\n);
                }
 
                for (my $i = 0; $i < $num; $i++)
@@ -225,18 +300,20 @@ EOF
                                $val = qq(<a href="mailto:$val" class="mail">$val</a>);
                        }
                        
-                       print "\t\t<tr>\n" if ($i);
-                       print "\t\t\t<td>$val</td>\n",
-                       "\t\t</tr>\n";
+                       print "\t\t\t<tr>\n" if ($i);
+                       print "\t\t\t\t<td>$val</td>\n",
+                       "\t\t\t</tr>\n";
                }
        }
        print <<EOF;
-               <th colspan="2" class="menu">
+               </table>
+
+               <div class="menu">
                        [<a href="$MySelf?action=verify&cn=$cn_esc">Verify</a>]
                        [<a href="$MySelf?action=vcard&cn=$cn_esc">vCard</a>]
                        [<a href="$MySelf?action=edit&cn=$cn_esc">Edit</a>]
-               </th>
-       </table>
+               </div>
+
 EOF
 }
 
@@ -614,8 +691,10 @@ sub action_verify
 
        if ($mail)
        {
-               action_verify_send_mail ($person);
-               $message .= qq( A request for verification has been sent to $mail.);
+               if (action_verify_send_mail ($person))
+               {
+                       $message .= qq( A request for verification has been sent to $mail.);
+               }
        }
        else
        {
@@ -633,6 +712,14 @@ sub action_verify_send_mail
        my $owner = Person->load ($UserCN);
        my $smh;
 
+       my ($owner_mail) = $owner->get ('mail');
+       if (!$owner_mail)
+       {
+               my $cn = uri_escape ($UserCN);
+               print qq(\t\t<div class="error">You have no email set in your own profile. <a href="$MySelf?action=edit&cn=$cn">Edit it now</a>!</div>\n);
+               return (0);
+       }
+
        my $max_width = 0;
        for (keys %FieldNames)
        {
@@ -645,17 +732,13 @@ sub action_verify_send_mail
        my $person_gn = $person->firstname ();
        my $password = $person->password ();
 
-       my $owner_name = $owner->name ();
-       my ($owner_mail) = $owner->get ('mail');
-       $owner_mail ||= $ENV{'SERVER_ADMIN'};
-
        my $host = $ENV{'HTTP_HOST'};
        my $url = 'http://' . $host . $MySelf;
        
-       open ($smh, '| /usr/sbin/sendmail -t') or die ("open pipe to sendmail: $!");
+       open ($smh, "| /usr/sbin/sendmail -t -f $owner_mail") or die ("open pipe to sendmail: $!");
        print $smh <<EOM;
 To: $person_name <$person_mail>
-From: $owner_name <$owner_mail>
+From: $UserCN <$owner_mail>
 Subject: Please verify our entry in my address book
 
 Hello $person_gn,
@@ -681,9 +764,11 @@ If this entry is outdated or incomplete, please take a minute and correct it.
  Password: $password
 
 Thank you very much :) Regards,
-$owner_name
+$UserCN
 EOM
        close ($smh);
+
+       return (1);
 }
 
 sub html_start
@@ -699,110 +784,203 @@ Content-Type: text/html; charset=UTF-8
                <title>$title</title>
                <style type="text/css">
                <!--
-a
-{
-       color: blue;
-       background-color: inherit;
-       text-decoration: none;
-}
+               \@media screen
+               {
+                       a
+                       {
+                               color: blue;
+                               background-color: inherit;
+                               text-decoration: none;
+                       }
 
-a:hover
-{
-       text-decoration: underline;
-}
+                       a:hover
+                       {
+                               text-decoration: underline;
+                       }
 
-a:visited
-{
-       color: navy;
-       background-color: inherit;
-}
+                       a:visited
+                       {
+                               color: navy;
+                               background-color: inherit;
+                       }
 
-body
-{
-       color: black;
-       background-color: white;
-}
+                       body
+                       {
+                               color: black;
+                               background-color: white;
+                       }
 
-div.error
-{
-       color: red;
-       background-color: yellow;
-       
-       font-weight: bold;
-       padding: 1ex;
-       border: 2px solid red;
-}
+                       div.error
+                       {
+                               color: red;
+                               background-color: yellow;
 
-div.foot
-{
-       color: gray;
-       background-color: white;
-
-       position: fixed;
-       top: auto;
-       right: 0px;
-       bottom: 0px;
-       left: 0px;
-       
-       font-size: x-small;
-       text-align: right;
-       border-top: 1px solid black;
-       width: 100%;
-}
+                               font-weight: bold;
+                               padding: 1ex;
+                               border: 2px solid red;
+                       }
 
-div.foot a
-{
-       color: black;
-       background-color: inherit;
-       text-decoration: none;
-}
+                       div.foot
+                       {
+                               color: gray;
+                               background-color: white;
+
+                               position: fixed;
+                               top: auto;
+                               right: 0px;
+                               bottom: 0px;
+                               left: 0px;
+
+                               font-size: x-small;
+                               text-align: right;
+                               border-top: 1px solid black;
+                               width: 100%;
+                       }
 
-div.foot a:hover
-{
-       text-decoration: underline;
-}
+                       div.foot a
+                       {
+                               color: black;
+                               background-color: inherit;
+                               text-decoration: none;
+                       }
 
-div.menu form
-{
-       display: inline;
-       margin-right: 5ex;
-}
+                       div.foot a:hover
+                       {
+                               text-decoration: underline;
+                       }
 
-img
-{
-       border: none;
-}
+                       div.menu
+                       {
+                               border-top: 1px solid black;
+                               margin-top: 1ex;
+                               font-weight: bold;
+                       }
 
-td
-{
-       color: black;
-       background-color: #cccccc;
-}
+                       div.menu a
+                       {
+                               color: blue;
+                               background-color: transparent;
+                       }
 
-th
-{
-       color: black;
-       background-color: #999999;
-       padding: 0.3ex;
-       text-align: left;
-       vertical-align: top;
-}
+                       div.topmenu
+                       {
+                               margin-bottom: 1ex;
+                               padding-bottom: 1ex;
+                               border-bottom: 1px solid black;
+                       }
 
-th.menu
-{
-       text-align: right;
-}
+                       div.topmenu form
+                       {
+                               display: inline;
+                               margin-right: 5ex;
+                       }
 
-.menu
-{
-       font-weight: bold;
-}
+                       h1
+                       {
+                               position: absolute;
+                               top: 1ex;
+                               right: 1ex;
+                               bottom: auto;
+                               left: auto;
+
+                               font-size: 100%;
+                               font-weight: bold;
+                       }
 
-.menu a
-{
-       color: blue;
-       background-color: transparent;
-}
+                       img
+                       {
+                               border: none;
+                       }
+
+                       table.list
+                       {
+                               width: 100%;
+                       }
+
+                       table.list td
+                       {
+                               empty-cells: show;
+                       }
+
+                       td
+                       {
+                               color: black;
+                               background-color: #cccccc;
+                               vertical-align: top;
+                       }
+
+                       th
+                       {
+                               color: black;
+                               background-color: #999999;
+                               padding: 0.3ex;
+                               text-align: left;
+                               vertical-align: top;
+                       }
+               }
+
+               \@media print
+               {
+                       a
+                       {
+                               color: inherit;
+                               background-color: inherit;
+                               text-decoration: underline;
+                       }
+                       
+                       div.topmenu, div.menu
+                       {
+                               display: none;
+                       }
+
+                       div.foot
+                       {
+                               font-size: 50%;
+                               text-align: right;
+                       }
+
+                       h1
+                       {
+                               display: none;
+                       }
+
+                       h2
+                       {
+                               font-size: 100%;
+                       }
+
+                       table
+                       {
+                               border-collapse: collapse;
+                       }
+
+                       table.list
+                       {
+                               width: 100%;
+                       }
+
+                       table.list td
+                       {
+                               empty-cells: show;
+                       }
+
+                       table.list th
+                       {
+                               border-bottom-width: 2px;
+                       }
+
+                       td, th
+                       {
+                               border: 1px solid black;
+                               vertical-align: top;
+                       }
+
+                       th
+                       {
+                               font-weight: bold;
+                               text-align: center;
+                       }
+               }
                //-->
                </style>
        </head>
@@ -813,7 +991,7 @@ EOF
        {
                my $search = param ('search') || '';
                print <<EOF;
-               <div class="menu">
+               <div class="topmenu">
                        <form action="$MySelf" method="post">
                                <input type="hidden" name="action" value="browse" />
                                <input type="submit" name="button" value="Browse" />
@@ -829,7 +1007,6 @@ EOF
                                <input type="submit" name="button" value="Add New" />
                        </form>
                </div>
-               <hr />
 EOF
        }
        print "\t\t<h1>$title</h1>\n";