(
address => 'Address',
homephone => 'Home Phone',
- cellphone => 'Cellphone',
+ cellphone => 'Cell Phone',
officephone => 'Office Phone',
fax => 'FAX',
mail => 'E-Mail',
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 ();
$Action = 'edit';
}
-print <<HEADER;
-Content-Type: text/html; charset=UTF-8
-
-HEADER
-
-print_html_start ("octo's Address Book");
-
if (!$UserCN)
{
die;
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 "$group"</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);
return;
}
+ print qq(\t<h2>Details for $cn</h2>\n);
+
my $cn_esc = uri_escape ($cn);
print <<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
}
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)
{
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)
$cn = $person->name ();
- action_edit (cn => $cn);
+ action_detail ($cn);
}
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 ();
}
}
}
- 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;
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 <octo at verplant.org></div>