###############################################################################
# CP_DoCfgStyle.src                                                           #
###############################################################################
# UltraBoard 2000 by UltraScripts.com, Inc.                                   #
# Scripts written by Jacky Wing Wa Yung, WebMaster@UltraScripts.com           #
# Available from http://www.UltraBoard.org/                                   #
# =========================================================================== #
# COPYRIGHT NOTICE :                                                          #
#                                                                             #
# Copyright (c) 1999-2000 UltraScripts.com, Inc. All Rights Reserved.         #
#                                                                             #
# This program is being distributed as shareware. It may be used and modified #
# free for personal, government or non-profit. Any commercial use must be     #
# registered. However, modified versions cannot be sold or distributed. You   #
# cannot alter the copyright and "powered by" notices throughout the scripts. #
# These notices must be clearly visible to the end users.                     #
#                                                                             #
# WARRANTY DISCLAIMER:                                                        #
#                                                                             #
# THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT #
# ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR       #
# FITNESS FOR A PARTICULAR PURPOSE.                                           #
###############################################################################
package Source;

use strict;

&Standard::Require("Cfg.lib", "$UB::LIBRARIES_PATH/Cfg.lib");

sub DoCfgStyle {

# =========================================================================== #
# Verify Variables

  my %FORM;

  &Standard::Require("ControlPanel.cfg", "$SYSTEM::THEME_PATH/$SYSTEM::DEFAULT_THEME/Style/ControlPanel.cfg");

  for (my $c = 0; $c <= $#UB::CONTROL_PANEL; $c++) {
    my $Category = $UB::CONTROL_PANEL[$c];

    for (my $o = 0; $o <= $#{ $Category->{'OPTIONS'} }; $o++) {
      my $Option = $Category->{'OPTIONS'}[$o];

      $FORM{ $Option->{'FORM'}{'NAME'} } = $UB::QUERY{ $Option->{'FORM'}{'NAME'} };
      $FORM{ $Option->{'FORM'}{'NAME'} } = &Encode ( $FORM{ $Option->{'FORM'}{'NAME'} } );

      if ($Option->{'FORM'}{'REQUIRED'} == 1) {
        if ($FORM{ $Option->{'FORM'}{'NAME'} } eq "") {
          &Error("You forgot to fill the \"".$Option->{'TITLE'}."\" field.");
        }
      }

    }
  }

# =========================================================================== #
# Update the Style.cfg

  &Standard::FileOpen(*CFG, "w", "$SYSTEM::THEME_PATH/$SYSTEM::DEFAULT_THEME/Style/Style.cfg");

    foreach my $KEY (sort keys %UB::STYLE) {
      if (ref ( $UB::STYLE{ $KEY } ) eq "HASH") {
        print CFG "\$UB::STYLE{\'$KEY\'} = {\n";
        foreach my $KEYKEY (sort keys %{ $UB::STYLE{$KEY} }) {
          $KEYKEY =~ s/\"/\\\"/g;
          $KEYKEY =~ s/\'/\\\'/g;
          $KEYKEY =~ s/\$/\\\$/g;
          $KEYKEY =~ s/\@/\\\@/g;
          $KEYKEY =~ s/\%/\\\%/g;
          $KEYKEY =~ s/\~/\\\~/g;
          print CFG "  \"$KEYKEY\" => \"".$UB::STYLE{$KEY}->{$KEYKEY}."\",\n";
        }
        print CFG "};\n";
      } else {
        print CFG "\$UB::STYLE{\'$KEY\'} = \"".$FORM{$KEY}."\";\n";
      }
    }

    print CFG "1;";
  close (CFG);

  &Success(\%FORM);
}

###############################################################################
# Error function
# - function of printing error message of updating style variables
sub Error {
  my ($message) = @_;

  require "$UB::SOURCES_PATH/CP_CfgStyle.src";
  &Source::CfgStyle($message);

  exit;
}

###############################################################################
# Success function
# - function of printing success message of updating style variables
sub Success {
  my ($FORM) = @_;

  &Standard::PrintHTMLHeader();
  print &HTML::Header;
  print<<HTML;
<table width="100%" border="0" cellspacing="0" cellpadding="5">
  <tr bgcolor="Gray">
    <td><font color="White"><b>
      <a href="$SYSTEM::CP_URL/Panel.$SYSTEM::EXT?action=body" target="UBBODY" class="address">Control Panel</a> / General Management / Configure style options
    </b></font></td>
  </tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr>
    <td>
      <table width="100%" border="0" cellspacing="0" cellpadding="2">
        <tr>
          <td>
            <table width="100%" border="1" cellspacing="0" cellpadding="3" bordercolordark="White" bordercolorlight="Gray">
              <tr>
                <td>
                  <table width="100%" border="0" cellspacing="0" cellpadding="2">
                    <tr>
                      <td>The style options has been updated, however you may have to rebuild/compile all the data to apply some of these setting.</td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</body>
</html>
HTML

  exit;
}

###############################################################################
1; # end of CP_DoCfgStyle.src file
###############################################################################