#!/usr/bin/perl
# -----------------------------------------------------------------------------
# TWC
# Treuwert-Computer GmbH
# Schloßbergring 9
# 79098 Freiburg
#
# -----------------------------------------------------------------------------
# Programm-Daten
# -----------------------------------------------------------------------------
# Projekt  :
# System   :
# Programm :
# Modul    : $RCSfile: 000048_en.html,v $
# Version  : $Revision: 1.1 $
# Stand von: $Date: 2000/11/03 11:17:15 $
# Status   : $State: Exp $
#
# Kurzbeschreibung:
# Dieses Modul enthält die benötigten-Routinen zur Pflege der
# Kontakt-Datenbank
# -----------------------------------------------------------------------------
# Aenderungen:
# $Log: 000048_en.html,v $
# Revision 1.1  2000/11/03 11:17:15  k_reger
# - Too large for db
#
# --- 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";



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 /;
      }
      else {
         # lokale Nummern
         $cKommAdr = "+49 761 " . $cKommAdr;
      }

      # 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;