Changed the year `LiCoM' was written in to `2005-2006'.
[licom.git] / licom.cgi
index 59eddd6..c26ff3f 100755 (executable)
--- a/licom.cgi
+++ b/licom.cgi
@@ -9,13 +9,14 @@ use CGI::Carp (qw(fatalsToBrowser));
 use URI::Escape;
 use Data::Dumper;
 
-use LiCoM::Config (qw(get_config));
-use LiCoM::Person;
+use LiCoM::Config (qw(get_config set_config read_config));
+use LiCoM::Connection ();
+use LiCoM::Group ();
+use LiCoM::Person ();
 
 our $Debug = 0;
-our $Config = {};
 
-our @MultiFields = (qw(address homephone cellphone officephone fax mail uri group));
+our @MultiFields = (qw(address homephone cellphone officephone fax mail uri));
 
 our %FieldNames = 
 (
@@ -49,24 +50,25 @@ our %Actions =
        vcard   => \&action_vcard
 );
 
-$Config = get_config ();
+read_config ();
 
 # make sure AuthLDAPRemoteUserIsDN is enabled.
 die unless ($ENV{'REMOTE_USER'});
-$Config->{'base_dn'} = $ENV{'REMOTE_USER'};
+set_config ('base_dn', $ENV{'REMOTE_USER'});
 
-die unless (defined ($Config->{'uri'}) and defined ($Config->{'base_dn'})
-       and defined ($Config->{'bind_dn'}) and defined ($Config->{'password'}));
+die unless (defined (get_config ('uri'))
+       and defined (get_config ('base_dn'))
+       and defined (get_config ('bind_dn'))
+       and defined (get_config ('password')));
 
-LiCoM::Person->connect
+LiCoM::Connection->connect
 (
-       uri     => $Config->{'uri'},
-       base_dn => $Config->{'base_dn'},
-       bind_dn => $Config->{'bind_dn'},
-       password => $Config->{'password'}
+       uri      => get_config ('uri'),
+       bind_dn  => get_config ('bind_dn'),
+       password => get_config ('password')
 ) or die;
 
-our ($UserCN, $UserID) = LiCoM::Person->get_user ($Config->{'base_dn'});
+our ($UserCN, $UserID) = LiCoM::Person->get_user ($ENV{'REMOTE_USER'});
 
 if (!$UserID and $Action ne 'save')
 {
@@ -95,7 +97,7 @@ elsif (ref ($Actions{$Action}) eq 'ARRAY')
        }
 }
 
-LiCoM::Person->disconnect ();
+LiCoM::Connection->disconnect ();
 
 exit (0);
 
@@ -107,83 +109,60 @@ sub action_browse
        $group = shift if (@_);
        $group ||= '';
 
-       my @all;
-       if ($group)
-       {
-               @all = LiCoM::Person->search ([[group => $group]]);
-       }
-       else
-       {
-               @all = LiCoM::Person->search ();
-       }
-
        if (!$group)
        {
-               my @nogroup = ();
-               my %groups = ();
-               for (@all)
-               {
-                       my $person = $_;
-                       my @g = $person->get ('group');
-
-                       $groups{$_} = (defined ($groups{$_}) ? $groups{$_} + 1 : 1) for (@g);
-
-                       push (@nogroup, $person) if (!@g);
-               }
-               @all = @nogroup;
+               my @groups = LiCoM::Group->all ();
 
                print qq(\t\t<h2>Contact Groups</h2>\n\t\t<ul class="groups">\n);
-               for (sort (keys (%groups)))
+               for (@groups)
                {
                        my $group = $_;
-                       my $group_esc = uri_escape ($group);
-                       my $num = $groups{$group};
-
-                       print qq(\t\t\t<li><a href="$MySelf?action=browse&group=$group_esc">$group</a> ($num)</li>\n);
+                       my @members = $group->get_members ();
+                       my $members = scalar (@members);
+                       my $group_name = $group->name ();
+                       my $group_esc  = uri_escape ($group_name);
+                       my $desc = $group->description ();
+
+                       print qq#\t\t\t<li><a href="$MySelf?action=browse&group=$group_esc">$group_name</a> ($members Member#, ($members == 1 ? ')' : 's)');
+                       print qq(<br />\n\t\t\t\t<span class="description">$desc</span>) if ($desc);
+                       print "</li>\n";
                }
-               if (!%groups)
+               if (!@groups)
                {
                        print qq(\t\t\t<li class="empty">There are no groups yet.</li>\n);
                }
-               print qq(\t\t</ul>\n\n);
-       }
-
-       if ($group)
-       {
-               print qq(\t\t<h2>Contact Group &quot;$group&quot;</h2>\n);
+               print <<EOF;
+               </ul>
+               <div class="menu">
+                       [<a href="$MySelf?action=list">List&nbsp;all</a>]
+               </div>
+EOF
        }
        else
        {
-               print qq(\t\t<h2>Contacts without a group</h2>\n);
-       }
-
-       print qq(\t\t<ul class="results">\n);
-       for (sort { $a->name () cmp $b->name () } (@all))
-       {
-               my $person = $_;
-               my $cn = $person->name ();
-               my $cn_esc = uri_escape ($cn);
-
-               print qq(\t\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
-       }
-       if (!@all)
-       {
-               print "\t\t\t<li>There are no matching entries.</li>\n";
-       }
-       print qq(\t\t</ul>\n\n);
+               my $group_obj = LiCoM::Group->load ($group);
+               my $group_esc = uri_escape ($group_obj->name ());
+               my @member_names = $group_obj->get_members ();
+               
+               print qq(\t\t<h2>Contact Group &quot;$group&quot;</h2>\n),
+               qq(\t\t<ul class="results">\n);
+               for (sort (@member_names))
+               {
+                       my $cn = $_;
+                       my $cn_esc = uri_escape ($cn);
 
-       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\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
+               }
+               
+               print <<EOF;
+               </ul>
+               <div class="menu">
+                       [<a href="$MySelf?action=list&group=$group_esc">List</a>]
+                       [<a href="$MySelf?action=browse">Back</a>]
+                       [Edit]
+               </div>
+EOF
        }
-       print qq(\t\t</div>\n);
 }
 
 sub action_list
@@ -317,6 +296,25 @@ EOF
                        "\t\t\t</tr>\n";
                }
        }
+
+       my @groups = LiCoM::Group->load_by_member ($cn);
+       if (@groups)
+       {
+               my $num = scalar (@groups);
+               print "\t\t\t<tr>\n",
+               "\t\t\t\t<th", ($num == 1 ? '' : qq( rowspan="$num")), ">Group", ($num == 1 ? '' : 's'), "</th>\n";
+               for (my $i = 0; $i < $num; $i++)
+               {
+                       my $group = $groups[$i];
+                       my $group_name = $group->name ();
+                       my $group_esc = uri_escape ($group_name);
+
+                       print "\t\t\t<tr>\n" if ($i != 0);
+                       print qq(\t\t\t\t<td><a href="$MySelf?action=browse&group=$group_esc">$group_name</a></td>\n),
+                       "\t\t\t</tr>\n";
+               }
+       }
+       
        print <<EOF;
                </table>
 
@@ -501,26 +499,34 @@ EOF
 
        if ($UserID)
        {
-               my %c_groups = map { $_ => 1 } (@{$contacts->{'group'}});
-               my %a_groups = ();
-               my @a_persons = LiCoM::Person->search ();
+               my @all_groups = LiCoM::Group->all ();
 
-               for (@a_persons)
+               if (@all_groups)
                {
-                       $a_groups{$_} = 1 for ($_->get ('group'));
-               }
+                       print "\t\t\t<tr>\n",
+                       "\t\t\t\t<th>Group(s)</th>\n",
+                       qq(\t\t\t\t<td><select name="group" multiple="multiple" size="5">\n);
 
-               print "\t\t\t<tr>\n",
-               "\t\t\t\t<th>", $FieldNames{'group'}, "</th>\n",
-               qq(\t\t\t\t<td><select name="group" multiple="multiple">\n);
-               for (sort (keys %a_groups))
-               {
-                       my $group = $_;
-                       my $selec = defined ($c_groups{$group}) ? ' selected="selected"' : '';
+                       for (@all_groups)
+                       {
+                               my $group = $_;
+                               my $group_name = $group->name ();
+                               my $selected = '';
+
+                               if (grep { $cn eq $_ } ($group->get_members ()))
+                               {
+                                       $selected = ' selected="selected"';
+                               }
 
-                       print qq(\t\t\t\t\t<option value="$group"$selec>$group</option>\n);
+                               print qq(\t\t\t\t\t<option value="$group_name"$selected>$group_name</option>\n);
+                       }
+                       print "\t\t\t\t</select></td>\n",
+                       "\t\t\t</tr>\n";
                }
-               print "\t\t\t\t</select></td>\n",
+                       
+               print "\t\t\t<tr>\n",
+               "\t\t\t\t<th>New Group</th>\n",
+               qq(\t\t\t\t<td><input type="text" name="newgroup" value="" /></td>\n),
                "\t\t\t</tr>\n";
        }
 
@@ -593,6 +599,28 @@ sub action_save
        
        $cn = $person->name ();
 
+       for (param ('group'))
+       {
+               my $group_name = $_;
+               my $group = LiCoM::Group->load ($group_name);
+
+               if ($group)
+               {
+                       $group->add_members ($cn);
+               }
+               else
+               {
+                       print qq(\t<div class="error">Group &quot;$group_name&quot; does not exist or could not be loaded.</div>\n);
+               }
+       }
+
+       if (param ('newgroup'))
+       {
+               # FIXME add error handling
+               my $group_name = param ('newgroup');
+               LiCoM::Group->create ($group_name, '', $cn);
+       }
+
        if ($button eq 'apply')
        {
                action_edit (cn => $cn);
@@ -628,6 +656,7 @@ sub action_update
                $person->firstname ($firstname) if ($firstname and $firstname ne $person->firstname ());
 
                $cn = $person->name ();
+               # FIXME Fix groups
        }
 
        my $contacts = get_contacts ();
@@ -649,6 +678,38 @@ sub action_update
                }
        }
 
+       my %changed_groups = map { $_ => 1 } (param ('group'));
+       my @current_groups = LiCoM::Group->load_by_member ($cn);
+
+       for (@current_groups)
+       {
+               my $group_obj = $_;
+               my $group_name = $group_obj->name ();
+
+               if (!defined ($changed_groups{$group_name}))
+               {
+                       $group_obj->del_members ($cn);
+               }
+               else
+               {
+                       delete ($changed_groups{$group_name});
+               }
+       }
+       for (keys %changed_groups)
+       {
+               my $group_name = $_;
+               my $group_obj = LiCoM::Group->load ($group_name) or die;
+
+               $group_obj->add_members ($cn);
+       }
+
+       if (param ('newgroup'))
+       {
+               # FIXME add error handling
+               my $group_name = param ('newgroup');
+               LiCoM::Group->create ($group_name, '', $cn);
+       }
+
        if ($button eq 'apply' or !$UserID)
        {
                action_edit (cn => $cn);
@@ -1110,7 +1171,7 @@ sub html_end
        print <<EOF;
                <div class="foot">
                        &quot;Lightweight Contact Manager&quot;,
-                       written 2005 by <a href="http://verplant.org/">Florian octo Forster</a>
+                       written 2005-2006 by <a href="http://verplant.org/">Florian octo Forster</a>
                        &lt;octo at verplant.org&gt;
                </div>
        </body>