
sub PreCreateForm ()
{
   my $cRecord;
   my $pDbHdl;
   my $pStmtHdl;
   my @lResult = ();
   my $cTable;
   my @lFields;
   my $cField;
   my $cFont;
   my $cFieldType;
   my @lUpdFields;
   my @lJumpValues;
   my $iInd;

   $cTable = $oSessionGL->getState("CreateAppRc,DbTable");

   &SetField("DbTable", $cTable);

   $oConfigFileTmp = ConfigFile->new
       ($oSessionGL->getState("CreateAppRc,ConfigFile"),
        ReadOnly => 0);

   $oConfigFile = ConfigFile->new
       (&GetAttr("BaseDir") . "/lib/WWWdb/tmp/" .
        $cTable . ".rc",
        ReadOnly => 0);

   # Should this field appear in query-mode
   $oConfigFileTmp->setValue("Layout Label Ruler1",
                             "Text", "<HR>");

   @lUpdFields = (split ", ",
                  $oConfigFile->getValue("Data", "UpdFields"));
   push @lUpdFields, (split ", ",
                      $oConfigFile->getValue("Data", "ExtraFields"));

   if(!&GetField("FieldId"))
   {
      &SetField("FieldId", "0");
   }

   $cField = $lUpdFields[&GetField("FieldId")];
   $cFieldType = $oConfigFile->getValue("Layout Field $cField", "Type", "");

   @lJumpValues = @lUpdFields;

   for ($iInd = 0;
        $iInd < @lJumpValues;
        $iInd ++)
   {
      $lJumpValues[$iInd] = $iInd . "\\," . $lJumpValues[$iInd];
   }

   &MySetVal("Layout Field JumpNext", "Values",
             (join "\n", @lJumpValues));

   # do we have all fields scanned?
   &Redirect(&GetAttr("SessionId"), "WWWdb:tmp:" . GetField("DbTable"))
       unless $cField;


   &SetField("FieldName", $lUpdFields[&GetField("FieldId")]);

   &SetField("JumpNext", &GetField("FieldId") + 1);

   &MySetVal("Header", "Title",
             &MyVal("Header", "Title", "") . " ($cTable.$cField)");

   $oConfigFileTmp->CreateSectionAndEntries
        (["Layout Label Hdr_Label",
          [["Text"   => i18n("Label-attributes")],
           ["Pos"    => "+1.0+5.0"],
           ["BgColor" => "#dddddd"],
           ["Bold"    => "True"],
           ["Big"     => "True"],
           ["Font"    => "Color=blue"]]],

         ["Layout Label Label_Font_Att",
          [["Pos"    => "+1.+0.0"],
           ["Bold"    => "True"],
           ["Text"   => i18n("Font Attributes")]]],

         ["Layout Field Label_Bold",
          [["Pos"    => "+0.1.0"],
           ["Type"   => "Checkbutton"],
           ["Text"   => i18n("Bold")],
           ["Bold"   => "Yes"]]],

         ["Layout Field Label_Italic",
          [["Pos"    => "+0.+0.+1"],
           ["Type"   => "Checkbutton"],
           ["Text"   => i18n("Italic")],
           ["Italic" => "Yes"]]],

         ["Layout Label Label_Big_Small",
          [["Pos"    => "+1.+0.0"],
           ["Bold"    => "True"],
           ["Text"   => i18n("Size-type")]]],

         ["Layout Field Label_Big_Small",
          [["Pos"    => "+0.+1.0"],
           ["Type"   => "Radio"],
           ["Values" => "Small\nBig"]]],

         ["Layout Label Label_Font_Face",
          [["Pos"    => "+1.+0.0"],
           ["Bold"    => "True"],
           ["Text"   => i18n("Font-face")]]],

         ["Layout Field Label_Font_Face",
          [["Pos"    => "+0.+1.0"],
           ["Type"   => "Radio"],
           ["Values" => "Helvetica\nTimes\nFixed\nCourier"]]],

         ["Layout Label Label_Font_Size",
          [["Pos"    => "+1.+0.0"],
           ["Bold"    => "True"],
           ["Text"   => i18n("Font-size")]]],

         ["Layout Field Label_Font_Size",
          [["Pos"    => "+0.+1.0"],
           ["Type"   => "Radio"],
           ["Values" => (join "\n", ("-4", "-3", "-2", "-1", "0",
                                     "+1", "+2", "+3", "+4"))]]],

         ["Layout Label Label_Color",
          [["Pos"    => "+1.+0.0"],
           ["Bold"    => "True"],
           ["Text"   => i18n("Color")]]],

         ["Layout Label Label_FgColor",
          [["Pos"    => "+0.+1.0"],
           ["Bold"    => "True"],
           ["Text"   => i18n("Foreground")]]],
         ["Layout Field Label_FgColor",
          [["Pos"    => "+0.+0.+1"],
           ["Type"   => "Select"],
           ["Values" => i18n("SELECT   key_value, label, sort_nr
                              FROM     wwwdb_lookup
                              WHERE    key_name = 'color' AND
                                       lang     = 'en'
                              ORDER BY sort_nr")]]],

         ["Layout Label Label_BgColor",
          [["Pos"    => "+0.+0.+1"],
           ["Bold"    => "True"],
           ["Text"   => i18n("Background")]]],

         ["Layout Field Label_BgColor",
          [["Pos"    => "+0.+0.+1"],
           ["Type"   => "Select"],
           ["Values" => i18n("SELECT   key_value, label, sort_nr
                              FROM     wwwdb_lookup
                              WHERE    key_name = 'color' AND
                                       lang     = 'en'
                              ORDER BY sort_nr")]]],

         ["Layout Label Label_No_Wrap",
          [["Pos"    => "+1.+0.0"],
           ["Bold"    => "True"],
           ["Text"   => i18n("Word-wrapping")]]],

         ["Layout Field Label_No_Wrap",
          [["Pos"    => "+0.+1.0"],
           ["Type"   => "Checkbutton"],
           ["Text"   => i18n("No wrap")]]],

         ["Layout Label Label_HorAlign",
          [["Pos"    => "+1.+0.0"],
           ["Bold"    => "True"],
           ["Text"   => i18n("Horizontal Alignment")]]],

         ["Layout Field Label_HorAlign",
          [["Pos"    => "+0.+1.0"],
           ["Type"   => "Radio"],
           ["Values" => (join "\n", ("Left", "Center", "Right"))]]],

         ["Layout Label Label_VerAlign",
          [["Pos"    => "+1.+0.0"],
           ["Bold"    => "True"],
           ["Text"   => i18n("Vertical Alignment")]]],

         ["Layout Field Label_VerAlign",
          [["Pos"    => "+0.+1.0"],
           ["Type"   => "Radio"],
           ["Values" => (join "\n", ("Top", "Center", "Bottom"))]]],

         ["Layout Label Hdr_Field",
          [["Text"   => i18n("Field-attributes")],
           ["Pos"    => "+2.0+5.0"],
           ["BgColor" => "#dddddd"],
           ["Bold"    => "True"],
           ["Big"    => "True"],
           ["Font"    => "Color=blue"]]],

         ["Layout Label Field_Type",
          [["Pos"    => "+1.+0.0"],
           ["Bold"    => "True"],
           ["Text"   => i18n("Field-Type")]]],

         ["Layout Field Field_Type",
          [["Pos"    => "+0.+1.0"],
           ["Type"   => "Select"],
           ["Values" => join "\n", ("Text",
                                    "Password",
                                    "Area",
                                    "Label",
                                    "Hidden",
                                    "Btn",
                                    "Ref",
                                    "Checkbutton",
                                    "Select",
                                    "Radio")]]]);

   $oConfigFileTmp->CreateSectionAndEntries
       (["Layout Label Field_Text_Size",
          [["Pos"    => "+1.+0.0"],
           ["Bold"    => "True"],
           ["Text"   => i18n("Display-size of field")]]],
         ["Layout Field Field_Text_Size",
          [["Pos"    => "+0.+1.0"],
           ["Type"   => "Text"],
           ["Size"   => "3"]]],

        ["Layout Label Field_Text_MaxLength",
          [["Pos"    => "+1.+0.0"],
           ["Bold"    => "True"],
           ["Text"   => i18n("Length of field")]]],
         ["Layout Field Field_Text_MaxLength",
          [["Pos"    => "+0.+1.0"],
           ["Type"   => "Text"],
           ["Size"   => "3"]]])
           if $cFieldType =~ /^(Text|Area|Password)$/;

   $oConfigFileTmp->CreateSectionAndEntries
       (["Layout Label Field_Check_Mandatory",
          [["Pos"    => "+1.+0.0"],
           ["Bold"    => "True"],
           ["Text"   => i18n("Is this field mandatory")]]],
        ["Layout Field Field_Check_Mandatory",
         [["Pos"    => "+0.+1.0"],
          ["Type"   => "Checkbutton"],
          ["Text"   => ""]]])
           if $cFieldType =~ /^(Text|Area|Password|Checkbutton|Select|Radio)$/;

   $oConfigFileTmp->CreateSectionAndEntries
       (["Layout Label Field_Check_MustMatch",
          [["Pos"    => "+1.+0.0"],
           ["Bold"    => "True"],
           ["Text"   => i18n("Regular expression, that field must match")]]],
        ["Layout Field Field_Check_MustMatch",
         [["Pos"    => "+0.+1.0"],
          ["Type"   => "Text"],
          ["Size"   => "60"]]],

        ["Layout Label Field_Check_ToLower",
          [["Pos"    => "+1.+0.0"],
           ["Bold"    => "True"],
           ["Text"   => i18n("Convert the field")]]],
        ["Layout Field Field_Check_ToLower",
         [["Pos"    => "+0.+1.0"],
          ["Type"   => "Checkbutton"],
          ["Text"   => "lowercase"]]],

        ["Layout Field Field_Check_ToUpper",
         [["Pos"    => "+0.+0.+1"],
          ["Type"   => "Checkbutton"],
          ["Text"   => "UPPERCASE"]]])
           if $cFieldType =~ /^(Text|Area|Password)$/;

   $oConfigFileTmp->CreateSectionAndEntries
       (["Layout Label Field_Area_Rows",
          [["Pos"    => "+1.+0.0"],
           ["Bold"    => "True"],
           ["Text"   => i18n("No. of lines")]]],

         ["Layout Field Field_Area_Rows",
          [["Pos"    => "+0.+1.0"],
           ["Type"   => "Text"],
           ["Size"   => "3"]]])
           if $cFieldType eq "Area";

   $oConfigFileTmp->CreateSectionAndEntries
       (["Layout Label Field_Label_SafeText",
          [["Pos"    => "+1.+0.0"],
           ["Bold"    => "True"],
           ["Text"   => i18n("Safe Text (for HTML-encoding)")]]],

         ["Layout Field Field_Label_SafeText",
          [["Pos"    => "+0.+1.0"],
           ["Type"   => "Checkbutton"],
           ["Text"   => i18n("")]]])
           if $cFieldType eq "Label";

   $oConfigFileTmp->CreateSectionAndEntries
       (["Layout Label Field_Btn_Label",
          [["Pos"    => "+1.+0.0"],
           ["Bold"    => "True"],
           ["Text"   => i18n("Label")]]],

         ["Layout Field Field_Btn_Label",
          [["Pos"    => "+0.+1.0"],
           ["Type"   => "Text"],
           ["Size"   => "30"]]])
           if $cFieldType =~ /^(Btn|Checkbutton)$/;

   $oConfigFileTmp->CreateSectionAndEntries
       (["Layout Label Field_Select_Rows",
          [["Pos"    => "+1.+0.0"],
           ["Bold"    => "True"],
           ["Text"   => i18n("Nr. of Entries")]]],
        ["Layout Field Field_Select_Rows",
         [["Pos"    => "+0.+1.0"],
          ["Type"   => "Text"],
          ["Size"   => "3"]]],

        ["Layout Label Field_Select_Multiple",
          [["Pos"    => "+1.+0.0"],
           ["Bold"    => "True"],
           ["Text"   => i18n("Multiple selections allowed")]]],
        ["Layout Field Field_Select_Multiple",
          [["Pos"    => "+0.+1.0"],
           ["Type"   => "Checkbutton"],
           ["Text"   => i18n("")]]])
           if $cFieldType eq "Select";

   # FIXME: NoUndef implementieren
   $oConfigFileTmp->CreateSectionAndEntries
       (["Layout Label Field_SelectFields_Values",
          [["Pos"    => "+1.+0.0"],
           ["Bold"   => "True"],
           ["VAlign" => "Top"],
           ["Text"   => i18n("Valid values for this field. ") . "<BR>"]]],
        ["Layout Label Field_SelectFields_Values1",
         [["Pos"    => "+0.+0.+1"],
          ["Italic" => "True"],
          ["Text"   => i18n("(Use the form Value[\\t+Label] " .
                             "for the entries)")]]],

         ["Layout Field Field_SelectFields_Values",
          [["Pos"    => "+0.+1.0"],
           ["Type"   => "Area"],
           ["Size"   => "50"],
           ["Rows"   => "5"]]])
           if $cFieldType =~ /^(Select|Radio)$/;

   &SetField("Label_Bold",
             $oConfigFile->getValue("Layout Label $cField",
                                    "Bold", ""));
   &SetField("Label_Italic",
             $oConfigFile->getValue("Layout Label $cField",
                                    "Italic", ""));
   &SetField("Label_Big_Small",
             $oConfigFile->getValue("Layout Label $cField",
                                    "Big", "") ||
             $oConfigFile->getValue("Layout Label $cField",
                                    "Small", ""));
   &SetField("Label_No_Wrap",
             $oConfigFile->getValue("Layout Label $cField",
                                    "NoWrap", ""));
   &SetField("Label_HorAlign",
             $oConfigFile->getValue("Layout Label $cField",
                                    "Align", ""));
   &SetField("Label_VerAlign",
             $oConfigFile->getValue("Layout Label $cField",
                                    "VAlign", ""));
   &SetField("Label_BgColor",
             $oConfigFile->getValue("Layout Label $cField",
                                    "BgColor", ""));

   $cFont = $oConfigFile->getValue("Layout Label $cField",
                                   "Font", "");

   &SetField("Label_Font_Face", "");
   &SetField("Label_FgColor", "");
   &SetField("Label_Font_Size", "");

   if($cFont =~ /face=\"(\w+)\"/i)
   {
        &SetField("Label_Font_Face", $1);
   }
   if($cFont =~ /color=\"(\S+)\"/i)
   {
        &SetField("Label_FgColor", $1);
   }
   if($cFont =~ /size=\"(\S+)\"/i)
   {
        &SetField("Label_Font_Size", $1);
   }

   &SetField("Field_Type",
             $oConfigFile->getValue("Layout Field $cField", "Type", ""));

   &SetField("Field_Text_Size",
             $oConfigFile->getValue("Layout Field $cField", "Size", ""));
   &SetField("Field_Text_MaxLength",
             $oConfigFile->getValue("Layout Field $cField",
                                    "MaxLength",
                                    $oConfigFile->getValue
                                    ("Layout Field $cField", "Size", "")));
   &SetField("Field_Check_Mandatory",
             $oConfigFile->getValue("Layout Field $cField",
                                    "Mandatory", ""));
   &SetField("Field_Check_MustMatch",
             $oConfigFile->getValue("Layout Field $cField",
                                    "MustMatch", ""));
   &SetField("Field_Check_ToLower",
             $oConfigFile->getValue("Layout Field $cField",
                                    "ToLower", ""));
   &SetField("Field_Check_ToUpper",
             $oConfigFile->getValue("Layout Field $cField",
                                    "ToUpper", ""));
   &SetField("Field_Label_SafeText",
             $oConfigFile->getValue("Layout Field $cField",
                                    "SafeText", ""));
   &SetField("Field_Area_Rows",
             $oConfigFile->getValue("Layout Field $cField",
                                    "Rows", "1"));
   &SetField("Field_Btn_Label",
             $oConfigFile->getValue("Layout Field $cField",
                                    "Label", $cField));
   &SetField("Field_Select_Rows",
             $oConfigFile->getValue("Layout Field $cField",
                                    "Rows", "1"));
   &SetField("Field_Select_Multiple",
             $oConfigFile->getValue("Layout Field $cField",
                                    "Multiple", ""));

   # the values may be in the form value\,label, but that
   # will confuse WWWdb, so we must replace it here
   {
      my $cValues = $oConfigFile->getValue("Layout Field $cField",
                                           "Values", "");

      $cValues =~ s/\\,/\t\t/g;

      &SetField("Field_SelectFields_Values", $cValues);
   }

   # $oConfigFileTmp->Dump();
   $oConfigFileTmp->WriteFile_Ini();

   $oConfPoolGL->addConfigFile ($oConfigFileTmp);

}

sub MyBtnFinish ()
{
   if(!&GetAttr("RecordOk"))
   {
      &GenPage();
   }
   else
   {

      &GenConfigFile();

      &Redirect(&GetAttr("SessionId"), "WWWdb:tmp:" . GetField("DbTable"));
   }
}



sub MyBtnReload ()
{

   if(!&GetAttr("RecordOk"))
   {
      &GenPage();
   }
   else
   {
      &GenConfigFile();
      &GenPage();
   }
}



sub MyBtnNextFld ()
{

   if(!&GetAttr("RecordOk"))
   {
      &GenPage();
   }
   else
   {
      &SetField("FieldId", &GetField("FieldId") + 1);

      &GenConfigFile();
      &GenPage();
   }
}


sub MyBtnJump ()
{

   if(!&GetAttr("RecordOk"))
   {
      &GenPage();
   }
   else
   {
      &SetField("FieldId", &GetField("JumpNext"));

      &GenConfigFile();
      &GenPage();
   }
}



sub MyBtnNext ()
{

   if(!&GetAttr("RecordOk"))
   {
      &GenPage();
   }
   else
   {
      &GenConfigFile();

      print &Redirect(&GetAttr("SessionId"), "WWWdb:Wiz:CreateAppRc_5");
   }
}


sub MyBtnPrev ()
{
   print &Redirect(&GetAttr("SessionId"), "WWWdb:Wiz:CreateAppRc_3");
}


sub GenConfigFile ()
{
   my $cFont = undef;
   my $cFieldType =  &GetField("Field_Type");

   $cFont .= ' FACE="' . &GetField("Label_Font_Face") . '"'
       if &GetField("Label_Font_Face");
   $cFont .= ' SIZE="' . &GetField("Label_Font_Size") . '"'
       if &GetField("Label_Font_Size");
   $cFont .= ' COLOR="' . &GetField("Label_FgColor") . '"'
       if &GetField("Label_FgColor");

   $oConfigFile = ConfigFile->new
       (&GetAttr("BaseDir") . "/lib/WWWdb/tmp/" . GetField("DbTable") . ".rc",
        ReadOnly => 0);

   $oConfigFile->CreateSectionAndEntries
       (["Layout Label " . &GetField("FieldName"),
         [["Bold"          => &GetField("Label_Bold")? "True": undef],
          ["Italic"        => &GetField("Label_Italic")? "True": undef],
          ["Big"           => (&GetField("Label_Big_Small") eq "Big" ?
                               "Big": undef)],
          ["Small"         => (&GetField("Label_Big_Small") eq "Small"?
                               "Small": undef)],
          ["Align"         => (&GetField("Label_HorAlign")?
                               &GetField("Label_HorAlign"): undef)],
          ["VAlign"        => (&GetField("Label_VerAlign")?
                               &GetField("Label_VerAlign"): undef)],
          ["BgColor"       => (&GetField("Label_BgColor")?
                               &GetField("Label_BgColor"): undef)],
          ["Font"          => $cFont]]],
        ["Layout Field " . &GetField("FieldName"),
         [["Type"          => &GetField("Field_Type")]]]);

   $oConfigFile->CreateSectionAndEntries
       (["Layout Field " . &GetField("FieldName"),
         [["Size"          => &GetField("Field_Text_Size")],
          ["MaxLength"     => &GetField("Field_Text_MaxLength")]]])
           if $cFieldType =~ /^(Text|Area|Password)$/;;


   $oConfigFile->CreateSectionAndEntries
       (["Layout Field " . &GetField("FieldName"),
         [["Mandatory"     => (&GetField("Field_Check_Mandatory")?
                               &GetField("Field_Check_Mandatory"):
                               undef)]]])
           if $cFieldType =~ /^(Text|Area|Password|Checkbutton|Select|Radio)$/;

   $oConfigFile->CreateSectionAndEntries
       (["Layout Field " . &GetField("FieldName"),
         [["MustMatch"   => (&GetField("Field_Check_MustMatch")?
                             &GetField("Field_Check_MustMatch"):
                             undef)],
          ["ToLower"     => (&GetField("Field_Check_ToLower")?
                             &GetField("Field_Check_ToLower"):
                             undef)],
          ["ToUpper"     => (&GetField("Field_Check_ToUpper")?
                             &GetField("Field_Check_ToUpper"):
                             undef)]]])
           if $cFieldType =~ /^(Text|Area|Password)$/;

   $oConfigFile->CreateSectionAndEntries
       (["Layout Field " . &GetField("FieldName"),
         [["Rows"     => &GetField("Field_Area_Rows")]]])
           if $cFieldType eq "Area";

   $oConfigFile->CreateSectionAndEntries
       (["Layout Field " . &GetField("FieldName"),
         [["SafeText"     => (&GetField("Field_Label_SafeText")?
                              &GetField("Field_Label_SafeText"):
                              undef)]]])
           if $cFieldType eq "Label";

   $oConfigFile->CreateSectionAndEntries
       (["Layout Field " . &GetField("FieldName"),
         [["Text"         => &GetField("Field_Btn_Label")]]])
           if $cFieldType =~ /^(Btn|Checkbutton)$/;

   $oConfigFile->CreateSectionAndEntries
       (["Layout Field " . &GetField("FieldName"),
         [["Rows"     => &GetField("Field_Select_Rows")],
          ["Multiple" => (&GetField("Field_Select_Multiple")?
                          &GetField("Field_Select_Multiple"):
                          undef)]]])
           if $cFieldType eq "Select";

   if ($cFieldType =~ /^(Select|Radio)$/)
   {
      my $cValues = &GetField("Field_SelectFields_Values");

      $cValues =~ s/\r//g;
      $cValues =~ s/\t+/\\,/g;

      $oConfigFile->CreateSectionAndEntries
          (["Layout Field " . &GetField("FieldName"),
            [["Values"     => $cValues]]]);
   }


   $oConfigFile->WriteFile_Ini();


   system("cd " . &GetAttr("BaseDir") . "/lib/WWWdb/tmp; " .
          "mv " . GetField("DbTable") . ".rc.new " . GetField("DbTable") . ".rc");

}



sub PostFirstColumn ()
{
   my $cResult = "";


   $cResult .= (br() x 2 .
                b(i18n("See also:")) .
                li(&ResolveRefField("wwwdb://WWWdb:Wiz:CreateAppRc_1",
                                    i18n("Select DB-table"))) .
                li(&ResolveRefField("wwwdb://WWWdb:Wiz:CreateAppRc_2",
                                    i18n("Select DB-fields"))) .
                li(&ResolveRefField("wwwdb://WWWdb:Wiz:CreateAppRc_2a",
                                    i18n("Field-order"))) .
                li(&ResolveRefField("wwwdb://WWWdb:Wiz:CreateAppRc_3",
                                    i18n("Mode-Information"))) .
                li(&ResolveRefField("wwwdb://WWWdb:Wiz:CreateAppRc_4",
                                    i18n("Field attributes")))
                );

   return small($cResult);

}



1;
