More fixed/additions..
[licom.git] / book.cgi
index 8a616b1..461815b 100755 (executable)
--- a/book.cgi
+++ b/book.cgi
@@ -20,7 +20,7 @@ our %FieldNames =
 (
        address         => 'Address',
        homephone       => 'Home Phone',
-       cellphone       => 'Cellphone',
+       cellphone       => 'Cell Phone',
        officephone     => 'Office Phone',
        fax             => 'FAX',
        mail            => 'E-Mail',
@@ -35,11 +35,13 @@ $Action ||= 'default';
 
 our %Actions =
 (
-       default => \&action_default,
-       detail  => \&action_detail,
-       edit    => \&action_edit,
-       save    => \&action_save,
-       search  => \&action_search
+       browse  => [\&html_start, \&action_browse, \&html_end],
+       default => [\&html_start, \&action_browse, \&html_end],
+       detail  => [\&html_start, \&action_detail,  \&html_end],
+       edit    => [\&html_start, \&action_edit,    \&html_end],
+       save    => [\&html_start, \&action_save,    \&html_end],
+       search  => [\&html_start, \&action_search,  \&html_end],
+       vcard   => \&action_vcard
 );
 
 read_config ();
@@ -63,13 +65,6 @@ if (!$UserID and $Action ne 'save')
        $Action = 'edit';
 }
 
-print <<HEADER;
-Content-Type: text/html; charset=UTF-8
-
-HEADER
-
-print_html_start ("octo's Address Book");
-
 if (!$UserCN)
 {
        die;
@@ -80,26 +75,78 @@ if (!defined ($Actions{$Action}))
        die;
 }
 
-$Actions{$Action}->();
+if (ref ($Actions{$Action}) eq 'CODE')
+{
+       $Actions{$Action}->();
+}
+elsif (ref ($Actions{$Action}) eq 'ARRAY')
+{
+       for (@{$Actions{$Action}})
+       {
+               $_->();
+       }
+}
 
 #print qq#<div>Authenticated as ($UserCN, $UserID, #, $Config{'base_dn'}, qq#)</div>\n#;
 
-print_html_end ();
-
 Person->disconnect ();
 
 exit (0);
 
 ###
 
-sub action_default
+sub action_browse
 {
-       print "<code>action_default</code>\n";
+       my $group = param ('group');
+       $group = shift if (@_);
+       $group ||= '*';
+
+       my @all = Person->search ([[group => $group]]);
+
+       if ($group eq '*')
+       {
+               my %groups = ();
+               for (@all)
+               {
+                       my $person = $_;
+                       my @g = $person->get ('group');
+
+                       $groups{$_} = 1 for (@g);
+               }
+
+               print qq(\t<h2>Contact Groups</h2>\n\t<ul class="groups">\n);
+               for (sort (keys (%groups)))
+               {
+                       my $group = $_;
+                       my $group_esc = uri_escape ($group);
+
+                       print qq(\t\t<li><a href="$MySelf?action=browse&group=$group_esc">$group</a></li>\n);
+               }
+               if (!%groups)
+               {
+                       print qq(\t\t<li class="empty">There are no groups yet.</li>\n);
+               }
+               print qq(\t</ul>\n);
+       }
+       else
+       {
+               print qq(\t<h2>Contact Group &quot;$group&quot;</h2>\n\t<ul class="results">\n);
+               for (@all)
+               {
+                       my $person = $_;
+                       my $cn = $person->name ();
+                       my $cn_esc = uri_escape ($cn);
+
+                       print qq(\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
+               }
+               print qq(\t</ul>\n);
+       }
 }
 
 sub action_detail
 {
        my $cn = param ('cn');
+       $cn = shift if (@_);
        die unless ($cn);
 
        my $person = Person->load ($cn);
@@ -109,6 +156,8 @@ sub action_detail
                return;
        }
 
+       print qq(\t<h2>Details for $cn</h2>\n);
+
        my $cn_esc = uri_escape ($cn);
 
        print <<EOF;
@@ -149,6 +198,7 @@ EOF
        </table>
        <div class="detail menu">
                [<a href="$MySelf?action=edit&cn=$cn_esc">edit</a>]
+               [<a href="$MySelf?action=vcard&cn=$cn_esc">vCard</a>]
        </div>
 EOF
 }
@@ -184,6 +234,14 @@ sub action_search
                return;
        }
 
+       if (scalar (@matches) == 1)
+       {
+               my $person = shift (@matches);
+               my $cn = $person->name ();
+               action_detail ($cn);
+               return;
+       }
+
        print qq(\t<ul class="result">\n);
        for (@matches)
        {
@@ -230,20 +288,22 @@ sub action_edit
        
                $lastname    = $person->lastname ();
                $firstname   = $person->firstname ();
-               $contacts->{'address'}     = $person->address ();
-               $contacts->{'homephone'}   = $person->homephone ();
-               $contacts->{'cellphone'}   = $person->cellphone ();
-               $contacts->{'officephone'} = $person->officephone ();
-               $contacts->{'fax'}         = $person->fax ();
-               $contacts->{'mail'}        = $person->mail ();
-               $contacts->{'uri'}         = $person->uri ();
-               $contacts->{'group'}       = $person->group ();
+
+               for (@MultiFields)
+               {
+                       $contacts->{$_} = $person->get ($_);
+               }
        }
 
        $lastname    = param ('lastname')    if (param ('lastname')  and $UserID);
        $firstname   = param ('firstname')   if (param ('firstname') and $UserID);
 
-       get_contacts ($contacts);
+       for (@MultiFields)
+       {
+               my $field = $_;
+               my @values = grep { $_ } (param ($field));
+               $contacts->{$field} = [@values] if (@values);
+       }
        
        $lastname    =   $opts{'lastname'}     if (defined ($opts{'lastname'}));
        $firstname   =   $opts{'firstname'}    if (defined ($opts{'firstname'}));
@@ -255,103 +315,77 @@ sub action_edit
 
        if ($cn)
        {
-               print "<h2>Edit contact $cn</h2>\n";
+               print "\t\t<h2>Edit contact $cn</h2>\n";
        }
        else
        {
-               print "<h2>Create new contact</h2>\n";
+               print "\t\t<h2>Create new contact</h2>\n";
        }
 
-       my $selector = sub
-       {
-               my $selected = @_ ? shift : '';
-
-               my @options =
-               (
-                       [none           => '-- Contact --'],
-                       [address        => 'Address'],
-                       [homephone      => 'Home Phone'],
-                       [cellphone      => 'Cellphone'],
-                       [officephone    => 'Office Phone'],
-                       [fax            => 'FAX'],
-                       [mail           => 'E-Mail'],
-                       [uri            => 'URI (Homepage)'],
-                       [group          => 'Group']
-               );
-
-               print qq(<select name="c_type">\n);
-               for (@options)
-               {
-                       my ($field, $print) = @$_;
-                       my $sel = $field eq $selected ? ' selected="selected"' : '';
-                       print qq(\t\t\t\t<option value="$field"$sel>$print</option>\n);
-               }
-               print qq(\t\t\t</select>);
-       };
-
        print <<EOF;
-       <form action="$MySelf" method="post">
-       <input type="hidden" name="action" value="save" />
-       <input type="hidden" name="cn" value="$cn" />
-       <table class="edit">
-               <tr>
-                       <td>Lastname</td>
+               <form action="$MySelf" method="post">
+               <input type="hidden" name="action" value="save" />
+               <input type="hidden" name="cn" value="$cn" />
+               <table class="edit">
+                       <tr>
+                               <td>Lastname</td>
 EOF
        if ($UserID)
        {
-               print qq(\t\t\t<td><input type="text" name="lastname" value="$lastname" /></td>\n);
+               print qq(\t\t\t\t<td><input type="text" name="lastname" value="$lastname" /></td>\n);
        }
        else
        {
-               print qq(\t\t\t<td>$lastname</td>\n);
+               print qq(\t\t\t\t<td>$lastname</td>\n);
        }
        print <<EOF;
-               </tr>
-               <tr>
-                       <td>Firstname</td>
+                       </tr>
+                       <tr>
+                               <td>Firstname</td>
 EOF
        if ($UserID)
        {
-               print qq(\t\t\t<td><input type="text" name="firstname" value="$firstname" /></td>\n);
+               print qq(\t\t\t\t<td><input type="text" name="firstname" value="$firstname" /></td>\n);
        }
        else
        {
-               print qq(\t\t\t<td>$firstname</td>\n);
+               print qq(\t\t\t\t<td>$firstname</td>\n);
        }
        
-       print "\t\t</tr>\n";
+       print "\t\t\t</tr>\n";
 
        for (@MultiFields)
        {
                my $field = $_;
+               my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
                my @values = @{$contacts->{$field}};
 
-               @values = ('') unless (@values);
+               push (@values, '');
                
                for (@values)
                {
                        my $value = $_;
-                       print "\t\t<tr>\n",
-                       "\t\t\t<td>";
-                       $selector->($field);
-                       print "</td>\n", <<EOF;
-                       <td><input type="text" name="c_value" value="$value" /></td>
-               </tr>
+
+                       print <<EOF;
+                       <tr>
+                               <td>$print</td>
+                               <td><input type="text" name="$field" value="$value" /></td>
+                       </tr>
 EOF
                }
        }
 
-       print "\t\t<tr>\n",
-       "\t\t\t<td>";
-       $selector->();
-       print "</td>\n", <<EOF;
-                       <td><input type="text" name="c_value" value="" /></td>
-               </tr>
-               <tr>
-                       <td colspan="2"><input type="submit" name="button" value="Save" /></td>
-               </tr>
-       </table>
-       </form>
+       print <<EOF;
+                       <tr>
+                               <td colspan="2">
+EOF
+       print qq(\t\t\t\t\t<input type="submit" name="button" value="Update" />\n) if ($UserID);
+       print <<EOF;
+                                       <input type="submit" name="button" value="Save" />
+                               </td>
+                       </tr>
+               </table>
+               </form>
 EOF
 }
 
@@ -377,7 +411,13 @@ sub action_save
        my $lastname  = param ('lastname');
        my $firstname = param ('firstname');
 
-       my $contacts = get_contacts ();
+       my $contacts = {};
+       for (@MultiFields)
+       {
+               my $field = $_;
+               my @values = grep { $_ } (param ($field));
+               $contacts->{$field} = [@values] if (@values);
+       }
 
        my $person = Person->create (lastname => $lastname, firstname => $firstname, %$contacts);
 
@@ -389,7 +429,14 @@ sub action_save
        
        $cn = $person->name ();
 
-       action_edit (cn => $cn);
+       if (param ('button') eq 'Update')
+       {
+               action_edit (cn => $cn);
+       }
+       else
+       {
+               action_detail ($cn);
+       }
 }
 
 sub action_update
@@ -404,13 +451,19 @@ sub action_update
                my $lastname  = param ('lastname');
                my $firstname = param ('firstname');
 
-               $person->lastname  ($lastname)  if ($lastname);
-               $person->firstname ($firstname) if ($firstname);
+               $person->lastname  ($lastname)  if ($lastname  and $lastname  ne $person->lastname ());
+               $person->firstname ($firstname) if ($firstname and $firstname ne $person->firstname ());
 
                $cn = $person->name ();
        }
 
-       my $contacts = get_contacts ();
+       my $contacts = {};
+       for (@MultiFields)
+       {
+               my $field = $_;
+               my @values = grep { $_ } (param ($field));
+               $contacts->{$field} = [@values] if (@values);
+       }
 
        for (@MultiFields)
        {
@@ -427,20 +480,78 @@ sub action_update
                }
        }
 
-       action_edit (cn => $cn);
+       if (param ('button') eq 'Update' or !$UserID)
+       {
+               action_edit (cn => $cn);
+       }
+       else
+       {
+               action_detail ($cn);
+       }
+}
+
+sub action_vcard
+{
+       my $cn = param ('cn');
+       $cn = shift if (@_);
+       die unless ($cn);
+
+       my $person = Person->load ($cn);
+       die unless ($person);
+
+       my %vcard_types =
+       (
+               homephone       => 'TEL;TYPE=home,voice',
+               cellphone       => 'TEL;TYPE=cell',
+               officephone     => 'TEL;TYPE=work,voice',
+               fax             => 'TEL;TYPE=fax',
+               mail            => 'EMAIL',
+               uri             => 'URL',
+               group           => 'ORG'
+       );
+
+       my $sn = $person->lastname ();
+       my $gn = $person->firstname ();
+       my $cn_esc = uri_escape ($cn);
+
+       print <<EOF;
+Content-Type: text/x-vcard
+Content-Disposition: attachment; filename="$cn.vcf"
+
+BEGIN:VCARD
+VERSION:3.0
+FN: $cn
+N: $sn;$gn
+EOF
+
+       for (@MultiFields)
+       {
+               my $field = $_;
+               my $vc_fld = $vcard_types{$field};
+               my $values = $person->get ($field);
+
+               for (@$values)
+               {
+                       my $value = $_;
+                       print "$vc_fld:$value\n";
+               }
+       }
+       print "END:VCARD\n";
 }
 
-sub print_html_start
+sub html_start
 {
        my $title = shift;
-       $title = 'Search for names' unless ($title);
+       $title = q(octo's Lightweight Address Book) unless ($title);
 
        print <<EOF;
+Content-Type: text/html; charset=UTF-8
+
 <html>
-<head>
-<title>$title</title>
-<style type="text/css">
-<!--
+       <head>
+               <title>$title</title>
+               <style type="text/css">
+               <!--
 body
 {
        color: black;
@@ -499,35 +610,39 @@ th
        text-align: left;
        vertical-align: top;
 }
-//-->
-</style>
-</head>
+               //-->
+               </style>
+       </head>
 
-<body>
+       <body>
 EOF
        if ($UserID)
        {
                my $search = param ('search') || '';
                print <<EOF;
-       <div class="menu">
-               <form action="$MySelf" method="post">
-                       <input type="hidden" name="action" value="search" />
-                       <input type="text" name="search" value="$search" />
-                       <input type="submit" name="button" value="Search" />
-               </form>
-               <form action="$MySelf" method="post">
-                       <input type="hidden" name="action" value="edit" />
-                       <input type="hidden" name="dn" value="" />
-                       <input type="submit" name="button" value="Add New" />
-               </form>
-       </div>
-       <hr />
+               <div class="menu">
+                       <form action="$MySelf" method="post">
+                               <input type="hidden" name="action" value="browse" />
+                               <input type="submit" name="button" value="Browse" />
+                       </form>
+                       <form action="$MySelf" method="post">
+                               <input type="hidden" name="action" value="search" />
+                               <input type="text" name="search" value="$search" />
+                               <input type="submit" name="button" value="Search" />
+                       </form>
+                       <form action="$MySelf" method="post">
+                               <input type="hidden" name="action" value="edit" />
+                               <input type="hidden" name="dn" value="" />
+                               <input type="submit" name="button" value="Add New" />
+                       </form>
+               </div>
+               <hr />
 EOF
        }
-       print "\t<h1>octo's Lightweight Address Book</h1>\n";
+       print "\t\t<h1>octo's Lightweight Address Book</h1>\n";
 }
 
-sub print_html_end
+sub html_end
 {
        print <<EOF;
                <div class="foot">octo's Lightweight Address Book &lt;octo at verplant.org&gt;</div>
@@ -563,35 +678,3 @@ sub read_config
                die ("Not defined: $_") unless (defined ($Config{$_}));
        }
 }
-
-sub get_contacts
-{
-       my $contacts = @_ ? shift : {};
-
-       if (param ('c_value'))
-       {
-               my @c_values = param ('c_value');
-               my @c_types  = param ('c_type');
-
-               my %cts = ();
-
-               die if (scalar (@c_values) != scalar (@c_types));
-
-               for (my $i = 0; $i < scalar (@c_values); $i++)
-               {
-                       my $type  = $c_types[$i];
-                       my $value = $c_values[$i];
-
-                       $cts{$type} = [] unless (defined ($cts{$type}));
-                       push (@{$cts{$type}}, $value) if ($value);
-               }
-
-               for (@MultiFields)
-               {
-                       my $type = $_;
-                       @{$contacts->{$type}} = @{$cts{$type}} if (defined ($cts{$type}));
-               }
-       }
-
-       return ($contacts);
-}