#!/usr/bin/perl
# -----------------------------------------------------------------------------
# TWC
# Treuwert-Computer GmbH
# Schloßbergring 9
# 79098 Freiburg
#
# -----------------------------------------------------------------------------
# Programm-Daten
# -----------------------------------------------------------------------------
# Projekt  :
# System   :
# Programm :
# Modul    : $RCSfile: Contacts.pl,v $
# Version  : $Revision: 1.6 $
# Stand von: $Date: 2002/03/06 15:07:08 $
# Status   : $State: Exp $
#
# Kurzbeschreibung:
# Dieses Modul enthält die benötigten-Routinen zur Pflege der
# Kontakt-Datenbank
# -----------------------------------------------------------------------------
# Aenderungen:
# $Log: Contacts.pl,v $
# Revision 1.6  2002/03/06 15:07:08  k_reger
# - PDF-Reports added
#
# Revision 1.5  2000/09/21 08:28:20  k_reger
# - Get data to Palm-PDA
#
# Revision 1.4  2000/03/24 11:03:26  klaus
# Checkpoint
#
# Revision 1.3  2000/02/24 11:27:23  klaus
# - Internationalization
# - New OO-Classes
#
# Revision 1.2  2000/02/02 21:38:58  klaus
# Preparing WWWdb for 0.0.6
#
# Revision 1.1  1999/12/02 16:19:20  klaus
# new file-struct
#
# Revision 1.1  1999/11/23 15:39:13  klaus
# New lib-directory
#
# Revision 1.9  1999/11/22 10:33:27  klaus
# - Last corrections before 0.0.4
#
# Revision 1.8  1999/11/15 13:52:37  klaus
# - HTML-output buffered
# - WWWdb_(Pre|Post).(rc|pl)
# - own tmp-directory
# - little customizations  for qry-mode
# - enhancement of debug-trace-utilities
# - API: GetAttr()
#
# Revision 1.7  1999/11/11 13:22:49  klaus
# Snapshot for internal installation
#
# Revision 1.6  1999/11/02 12:01:59  klaus
# Check-in to store actual state
#
# Revision 1.5  1999/10/21 13:35:05  klaus
# - Select and Radio-Fields can now be filled with SQL
# - WWWdb is now "-w"-proof
# - Database-Errors are now shown uniquely
#
# Revision 1.4  1999/10/20 13:01:34  klaus
# Version 1.0.3
# - Getting field-type-information
# - Plugins to convert DB-data to Form-data and vice versa
# - New API-functions
#    $current_state = GetState();
#    $cust_type = GetFieldTypeName($field_name);
#
# BUGFIX:
# -kfm, lynx and w3m seem to work now
#
# Revision 1.3  1999/10/08 13:48:10  klaus
# - Selection-Boxes and radio-fields now can have labels
# - plugins: API-Documentation
# - Perl-makefile integrated
#
# Revision 1.2  1999/10/06 08:42:13  klaus
# Checkpoint before copying to WWW-server
#
# Revision 1.1  1999/10/06 06:31:45  klaus
# Creation
#
# Revision 1.1.1.1  1999/09/27 08:50:27  klaus
# Integration in CVS
#
# Revision 1.1.1.1  1999/09/27 08:09:51  klaus
# Imported using tkCVS
#
#
# --- FIX ----- FIX ----- FIX ----- FIX ----- FIX ----- FIX -------------------
# NOCH ZU ERLEDIGEN:      (-) offen           (+) teils erledigt
# - FIX-MEs in der Source bereinigen
#
# ----------------------------------------------------------------------------
# --- FIX ----- FIX ----- FIX -----FIX ----- FIX ----- FIX -------------------

# include Db2Pdb here
my $cBasePath = ($ENV{"WWWDB_BASE_PATH"}?
                 $ENV{"WWWDB_BASE_PATH"}:
                 "/usr/local/httpd/htdocs/WWWdb");

require "$cBasePath" . "/lib/WWWdb/Tools/Db2Pdb.pl";
require "$cBasePath" . "/lib/WWWdb/Tools/Db2Pdf.pl";



sub PreCreateForm ()
{
   if(!$oSessionGL->getState("ActualLogin"))
   {

      if("Button_deactivated")
      {
         &MySetVal("State Init", "BtnNew", "-");
         &MySetVal("State Work", "BtnUpd", "-");
         &MySetVal("State Work", "BtnDel", "-");
      }
      else
      {
         &OkForm("ERROR",
                 sprintf(i18n("Sorry, please %s first!"),
                         "<A HREF=" .
                         &CreateReference(&GetAttr("SessionId"),
                                          "Login") .
                         ">" .
                         i18n("login") .
                      "</A>"),
                 "BtnExit");
         &MyExit();
      }
   }
}

# --- Feld company prüfen --------------------------------------------------
sub Check_contacts_company()
{
   &AddError("company", i18n("No company (or last name), please enter!!"))
       if(!&GetField("company") && !&GetField("last_name"));
}


# --- Feld conttyp1 prüfen -----------------------------------------------
sub Check_contacts_conttyp1()
{
   &AddError("conttyp1", i18n("No contact-type (1), please select!!"))
       if(!&GetField("conttyp1"));
}


# --- Feld conttyp1 prüfen -----------------------------------------------
sub Check_contacts_contadr1()
{
   &AddError("contadr1", i18n("No contact-adress (1), please select!!"))
       if(!&GetField("contadr1"));
}

# --- Feld sex prüfen -----------------------------------------------
sub Check_contacts_sex()
{
   &AddError("sex",
             i18n("No sex selected, altough the first name is known. " .
                  "Please select!!"))
       if(!&GetField("sex") and &GetField("first_name"));
}




# --- Gesamtprüfung für Datenintegrität ---------------------------------
sub Check_contacts_EveryField($)
{
   my $cFieldNamePI = shift;

}



# --- Feld conttyp1 prüfen -----------------------------------------------
sub Check_contacts()
{
   my $iInd;

   for $iInd (1..5) {
      if((&GetField("contadr$iInd")) and
         (!&GetField("conttyp$iInd")))
      {
         &AddError("conttyp$iInd",
                   sprintf(i18n("For contact-address (%s), " .
                                "the contact-type is missing!!"),
                           $iInd));
      }

      if((!&GetField("contadr$iInd")) and
         (&GetField("conttyp$iInd")))
      {
         &AddError("contadr$iInd",
                   sprintf(i18n("For contact-type (%s), " .
                                "the contact-address is missing!!"),
                           $iInd));
      }

      &NormConttyp($iInd)
          if(&GetField("contadr$iInd"));
   }

   # Set time-stamp
   &SetField("last_change",
             $oDbSessionGL->DateToDb(scalar(localtime())));
}

sub PreBtnDb2Pdb ()
{
   &PreBtnQry();
}


sub PreBtnQry ()
{
   &SetField("title",
             &GetField("title") .
             &GetField("title1"));

   &SetField("state", "!= DEL");

}


sub PreBtnNew ()
{
   my $iMaxContactId;

   if (&GetField("title1"))
   {
      &SetField("title", &GetField("title1"));
   }

   &SetField("state", "");

   # generate a new object-id
   $iMaxContactId = $oDbSessionGL->getMaxId("id_contact", "contacts");

   &SetField("id_contact", $iMaxContactId);


}


sub PreBtnUpd ()
{

   if (&GetField("title1"))
   {
      &SetField("title", &GetField("title1"));
   }

}



sub MyBtnDel ()
{
    &SetField("state", "DEL");

    &BtnUpd();
}




# --- Dieses Modul normiert Telefonnummern, um ein einheitliches
#     Aussehen zu gewährleisten
sub NormConttyp ($)
{
   my $iIndPI = shift;

   my $cKommAdr = &GetField("contadr$iIndPI");
   my $cKommTyp = &GetField("conttyp$iIndPI");

   # Nur Adressen bearbeiten, die nur für Telefonnummern relevante
   # Zeichen beinhalten
   unless ($cKommAdr =~ /.*[^\/+.()0-9 -].*/)
   {

      # + durch int.Vorwahl ersetzen
      if($cKommAdr =~ /^\+\d\d ?/) {
         $cKommAdr =~ s/^\+(\d\d) ?/00$1 /;
      }

      # Sonderzeichen normieren
      $cKommAdr =~ s%[-/.()]% %g;

      # Leerzeichen trimmen (mehrfache, am Anfang, am Ende)
      $cKommAdr =~ s/\s+/ /g;
      $cKommAdr =~ s/^\s+//;
      $cKommAdr =~ s/\s+$//;

      # internationale Nummern
      if($cKommAdr =~ /^00\d\d ?/) {
         $cKommAdr =~ s/^00(\d\d) ?/+$1 /;
      }
      # nationale Nummern
      elsif($cKommAdr =~ /^0.*/) {

         # Handies herausfiltern
         if($cKommAdr =~ /^01[67][123].*/) {
            $cKommTyp = "Handy";
         }

         $cKommAdr =~ s/^0/+49 /;
      }

      # Vorwahl in Klammern setzen
      $cKommAdr =~ s/^(\+\d+ )(\d+)(.*)/$1($2)$3/;

      # Duchwahl mit - abtrennen
      $cKommAdr =~ s/(\d) (\d+)$/$1-$2/;

      if(length($cKommAdr) >= 40) {
         $cKommAdr = substr($cKommAdr, 0, 40);
      }

   } # unless ($cKommAdr =~ /[!/+.()0-9 -]/)

   &SetField("contadr$iIndPI", $cKommAdr);
   &SetField("conttyp$iIndPI", $cKommTyp);
}

1;
