Many more fixes and additions..
authorocto <octo>
Fri, 22 Apr 2005 22:26:44 +0000 (22:26 +0000)
committerocto <octo>
Fri, 22 Apr 2005 22:26:44 +0000 (22:26 +0000)
book.cgi
lib/Person.pm

index 8a616b1..bebf6d1 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,74 @@ 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);
+               }
+               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 +152,8 @@ sub action_detail
                return;
        }
 
+       print qq(\t<h2>Details for $cn</h2>\n);
+
        my $cn_esc = uri_escape ($cn);
 
        print <<EOF;
@@ -149,6 +194,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 +230,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)
        {
@@ -255,29 +309,25 @@ sub action_edit
 
        if ($cn)
        {
-               print "<h2>Edit contact $cn</h2>\n";
+               print "\t<h2>Edit contact $cn</h2>\n";
        }
        else
        {
-               print "<h2>Create new contact</h2>\n";
+               print "\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']
-               );
+               my @options = ([none => '-- Contact --']);
+
+               for (@MultiFields)
+               {
+                       my $field = $_;
+                       my $print = defined ($FieldNames{$field}) ? $FieldNames{$field} : $field;
+                       push (@options, [$field, $print]);
+               }
 
                print qq(<select name="c_type">\n);
                for (@options)
@@ -389,7 +439,7 @@ sub action_save
        
        $cn = $person->name ();
 
-       action_edit (cn => $cn);
+       action_detail ($cn);
 }
 
 sub action_update
@@ -404,8 +454,8 @@ 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 ();
        }
@@ -427,20 +477,78 @@ sub action_update
                }
        }
 
-       action_edit (cn => $cn);
+       if ($UserID)
+       {
+               action_detail ($cn);
+       }
+       else
+       {
+               action_edit (cn => $cn);
+       }
 }
 
-sub print_html_start
+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 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 +607,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>
index 96a5977..c692e96 100644 (file)
@@ -179,11 +179,11 @@ sub create
                {
                        if (ref ($val) eq 'ARRAY')
                        {
-                               $entry->add ($field => [@$val]);
+                               $entry->add ($field => [@$val]) if (@$val);
                        }
                        elsif (!ref ($val))
                        {
-                               $entry->add ($field => [$val]);
+                               $entry->add ($field => [$val]) if ($val);
                        }
                        else
                        {
@@ -364,8 +364,11 @@ sub _update_dn
 
        $obj->{'cn'} = $cn;
 
+       print STDERR "This is _update_dn, trying to set dn=$dn";
+
        $entry->changetype ('modify');
        $entry->replace (sn => $sn, givenName => $gn, cn => $cn);
+       $entry->update ($Ldap);
        $entry->dn ($dn);
        $entry->update ($Ldap);
 }
@@ -528,8 +531,6 @@ sub get_user
                my ($t_cn) = $e->get_value ('cn', asref => 0);
                my ($t_id) = $e->get_value ('uid', asref => 0);
 
-               print STDERR "LDAP result: $t_cn, $t_id";
-
                if (!$id or $t_id)
                {
                        $cn = $t_cn;