More fixed/additions..
authorocto <octo>
Fri, 22 Apr 2005 22:56:45 +0000 (22:56 +0000)
committerocto <octo>
Fri, 22 Apr 2005 22:56:45 +0000 (22:56 +0000)
book.cgi

index bebf6d1..461815b 100755 (executable)
--- a/book.cgi
+++ b/book.cgi
@@ -122,6 +122,10 @@ sub action_browse
 
                        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
@@ -284,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'}));
@@ -309,99 +315,77 @@ sub action_edit
 
        if ($cn)
        {
-               print "\t<h2>Edit contact $cn</h2>\n";
+               print "\t\t<h2>Edit contact $cn</h2>\n";
        }
        else
        {
-               print "\t<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 --']);
-
-               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)
-               {
-                       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
 }
 
@@ -427,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);
 
@@ -439,7 +429,14 @@ sub action_save
        
        $cn = $person->name ();
 
-       action_detail ($cn);
+       if (param ('button') eq 'Update')
+       {
+               action_edit (cn => $cn);
+       }
+       else
+       {
+               action_detail ($cn);
+       }
 }
 
 sub action_update
@@ -460,7 +457,13 @@ sub action_update
                $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)
        {
@@ -477,13 +480,13 @@ sub action_update
                }
        }
 
-       if ($UserID)
+       if (param ('button') eq 'Update' or !$UserID)
        {
-               action_detail ($cn);
+               action_edit (cn => $cn);
        }
        else
        {
-               action_edit (cn => $cn);
+               action_detail ($cn);
        }
 }
 
@@ -675,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);
-}