Implemented checks for certain fields (telephone numbers and uris).
authorocto <octo>
Sun, 24 Apr 2005 12:13:08 +0000 (12:13 +0000)
committerocto <octo>
Sun, 24 Apr 2005 12:13:08 +0000 (12:13 +0000)
book.cgi

index 4805dda..cf4653e 100755 (executable)
--- a/book.cgi
+++ b/book.cgi
@@ -100,11 +100,19 @@ sub action_browse
 {
        my $group = param ('group');
        $group = shift if (@_);
-       $group ||= '*';
+       $group ||= '';
 
-       my @all = Person->search ([[group => $group]]);
+       my @all;
+       if ($group)
+       {
+               @all = Person->search ([[group => $group]]);
+       }
+       else
+       {
+               @all = Person->search ();
+       }
 
-       if ($group eq '*')
+       if (!$group)
        {
                my %groups = ();
                for (@all)
@@ -131,17 +139,17 @@ sub action_browse
                print qq(\t\t</ul>\n\n);
        }
 
-       if ($group eq '*')
+       if ($group)
        {
-               print qq(\t\t<h2>All Contacts</h2>\n);
+               print qq(\t\t<h2>Contact Group &quot;$group&quot;</h2>\n);
        }
        else
        {
-               print qq(\t\t<h2>Contact Group &quot;$group&quot;</h2>\n);
+               print qq(\t\t<h2>All Contacts</h2>\n);
        }
 
        print qq(\t\t<ul class="results">\n);
-       for (@all)
+       for (sort { $a->name () cmp $b->name () } (@all))
        {
                my $person = $_;
                my $cn = $person->name ();
@@ -150,6 +158,8 @@ sub action_browse
                print qq(\t\t\t<li><a href="$MySelf?action=detail&cn=$cn_esc">$cn</a></li>\n);
        }
        print qq(\t\t</ul>\n\n);
+
+       print qq(\t\t<div class="menu">[<a href="$MySelf?action=browse">back</a>]</div>\n) if ($group);
 }
 
 sub action_detail
@@ -198,6 +208,23 @@ EOF
                for (my $i = 0; $i < $num; $i++)
                {
                        my $val = $values->[$i];
+
+                       if ($field eq 'group')
+                       {
+                               my $val_esc = uri_escape ($val);
+                               $val = qq(<a href="$MySelf?action=browse&group=$val_esc">$val</a>);
+                       }
+                       elsif ($field eq 'uri')
+                       {
+                               my $uri = $val;
+                               $uri = qq(http://$val) unless ($val =~ m#^[a-z]+://#);
+                               $val = qq(<a href="$uri" class="extern">$val</a>);
+                       }
+                       elsif ($field eq 'mail')
+                       {
+                               $val = qq(<a href="mailto:$val" class="mail">$val</a>);
+                       }
+                       
                        print "\t\t<tr>\n" if ($i);
                        print "\t\t\t<td>$val</td>\n",
                        "\t\t</tr>\n";
@@ -308,12 +335,7 @@ sub action_edit
        $lastname    = param ('lastname')    if (param ('lastname')  and $UserID);
        $firstname   = param ('firstname')   if (param ('firstname') and $UserID);
 
-       for (@MultiFields)
-       {
-               my $field = $_;
-               my @values = grep { $_ } (param ($field));
-               $contacts->{$field} = [@values] if (@values);
-       }
+       get_contacts ($contacts);
        
        $lastname    =   $opts{'lastname'}     if (defined ($opts{'lastname'}));
        $firstname   =   $opts{'firstname'}    if (defined ($opts{'firstname'}));
@@ -409,6 +431,12 @@ sub action_save
 {
        my $cn = $UserID ? param ('cn') : $UserCN;
 
+       if (verify_fields ())
+       {
+               action_edit (cn => $cn);
+               return;
+       }
+
        if ($cn)
        {
                action_update ();
@@ -436,13 +464,7 @@ sub action_save
        my $lastname  = param ('lastname');
        my $firstname = param ('firstname');
 
-       my $contacts = {};
-       for (@MultiFields)
-       {
-               my $field = $_;
-               my @values = grep { $_ } (param ($field));
-               $contacts->{$field} = [@values] if (@values);
-       }
+       my $contacts = get_contacts ();
 
        my $person = Person->create (lastname => $lastname, firstname => $firstname, %$contacts);
 
@@ -491,13 +513,7 @@ sub action_update
                $cn = $person->name ();
        }
 
-       my $contacts = {};
-       for (@MultiFields)
-       {
-               my $field = $_;
-               my @values = grep { $_ } (param ($field));
-               $contacts->{$field} = [@values] if (@values);
-       }
+       my $contacts = get_contacts ();
 
        for (@MultiFields)
        {
@@ -722,7 +738,7 @@ div.foot
        color: gray;
        background-color: white;
 
-       position: absolute;
+       position: fixed;
        top: auto;
        right: 0px;
        bottom: 0px;
@@ -777,7 +793,12 @@ th.menu
        text-align: right;
 }
 
-th.menu a
+.menu
+{
+       font-weight: bold;
+}
+
+.menu a
 {
        color: blue;
        background-color: transparent;
@@ -894,3 +915,69 @@ sub pwgen
 
        return ($retval);
 }
+
+sub verify_fields
+{
+       my @errors = ();
+       for (param ('uri'))
+       {
+               my $val = $_;
+               next unless ($val);
+
+               if ($val !~ m#^[a-zA-Z]+://#)
+               {
+                       push (@errors, 'URIs have to begin with a protocol, e.g. &quot;http://&quot;, &quot;ftp://&quot; etc.');
+                       last;
+               }
+       }
+
+       for (param ('homephone'), param ('cellphone'), param ('officephone'), param ('fax'))
+       {
+               my $number = $_;
+               next unless ($number);
+
+               if ($number !~ m/^\+/)
+               {
+                       push (@errors, 'Telephone numbers have to begin with the country code, e.g. &quot;+49 911 123456&quot;');
+                       last;
+               }
+       }
+
+       print qq(\t\t<div class="error">\n) if (@errors);
+       for (my $i = 0; $i < scalar (@errors); $i++)
+       {
+               my $e = $errors[$i];
+
+               print "<br />\n" if ($i);
+               print "\t\t\t$e";
+       }
+       print qq(\n\t\t</div>\n\n) if (@errors);
+
+       return (scalar (@errors));
+}
+
+sub get_contacts
+{
+       my $contacts = @_ ? shift : {};
+
+       for (@MultiFields)
+       {
+               my $field = $_;
+               my @values = grep { $_ } (param ($field));
+
+               next unless (@values);
+
+               if ($field eq 'homephone' or $field eq 'cellphone' or $field eq 'officephone' or $field eq 'fax')
+               {
+                       for (@values)
+                       {
+                               $_ =~ s/\D//g;
+                               $_ = '+' . $_;
+                       }
+               }
+               
+               $contacts->{$field} = [@values] if (@values);
+       }
+
+       return ($contacts);
+}