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

sub ModifyCategory {
  my ($ERROR) = @_;

  (
    my ($DB) = &DB::Query (
      TABLE   => "Groups",
      COLUMNS => ["ID", "TITLE"]
    )
  ) || &HTML::Error("Categories Management / Modify category", "No group available for you to choose yet.<br>Sorry, you have to <a href=\"$SYSTEM::CP_URL/Panel.$SYSTEM::EXT?action=CreateGroup\" target=\"UBBODY\">create a new member group</a> first!");

  my $Groups;
  my $AllGroups;
  push (
    @{ $Groups },
    {
      KEY   => "*",
      VALUE => "All Groups"
    }
  );
  push (@{ $AllGroups }, "*");

  for (my $i = 0; $i <= $#{ $DB }; $i++) {
    push (
      @{ $Groups },
      {
        KEY   => $DB->[$i]{'ID'},
        VALUE => $DB->[$i]{'TITLE'}
      }
    );
    push (@{ $AllGroups }, $DB->[$i]{'ID'});
  }
  &Standard::PrintHTMLHeader();

  (
    my ($Category) = &DB::BinarySelect (
      TABLE   => "Categories",
      KEY     => $UB::QUERY{'CID'}
    )
  ) || &HTML::Error ("Internal Database Error", $DB::Error);

  my %FORM;
  $FORM{'FORM_ID'}           = $UB::QUERY{'CID'}              || $Category->{'ID'}          || "";
  $FORM{'FORM_TITLE'}        = $UB::QUERY{'FORM_TITLE'}       || $Category->{'TITLE'}       || "";
  $FORM{'FORM_DESCRIPTION'}  = $UB::QUERY{'FORM_DESCRIPTION'} || $Category->{'DESCRIPTION'} || "";

  &Standard::FileOpen(*DATA, "r", "$SYSTEM::DB_PATH/Categories/".$Category->{'ID'}."-LEVEL-00.dat");
    my $LEVEL_00 = join ("", <DATA>);
  close (DATA);

  $FORM{'FORM_LEVEL_00'}     = &HTML::Checkbox (
    NAME    => "FORM_LEVEL_00"                   ,
    VALUE   => "1"                               ,
    CHECKED => $UB::QUERY{'FORM_LEVEL_00'} || (($LEVEL_00 eq "*") ? 1 : 0) || "",
    EXTRA   => "onClick=\"Checked4Everyone()\""
  );

  &Standard::FileOpen(*DATA, "r", "$SYSTEM::DB_PATH/Categories/".$Category->{'ID'}."-LEVEL-30.dat");
    my $LEVEL_30 = join ("", <DATA>);
  close (DATA);

  $FORM{'FORM_LEVEL_30'}     = &HTML::Checkbox (
    NAME    => "FORM_LEVEL_30"                   ,
    VALUE   => "1"                               ,
    CHECKED => $UB::QUERY{'FORM_LEVEL_30'} || (($LEVEL_30 eq "*") ? 1 : 0) || "",
    EXTRA   => "onClick=\"Checked(document.form.FORM_LEVEL_30, document.form.FORM_GROUP_30)\""
  );

  &Standard::FileOpen(*DATA, "r", "$SYSTEM::DB_PATH/Categories/".$Category->{'ID'}."-LEVEL-50.dat");
    my $LEVEL_50 = join ("", <DATA>);
  close (DATA);

  $FORM{'FORM_LEVEL_50'}     = &HTML::Checkbox (
    NAME    => "FORM_LEVEL_50"                   ,
    VALUE   => "1"                               ,
    CHECKED => $UB::QUERY{'FORM_LEVEL_50'} || (($LEVEL_50 eq "*") ? 1 : 0) || "",
    EXTRA   => "onClick=\"Checked(document.form.FORM_LEVEL_50, document.form.FORM_GROUP_50)\""
  );

  $FORM{'FORM_GROUP_30'}     = &HTML::Select (
    NAME      => "FORM_GROUP_30"                   ,
    VALUE     => $Groups                           ,
    MULTIPLE  => 1                                 ,
    SIZE      => 3                                 ,
    SELECTED  => ($UB::QUERY{'FORM_GROUP_30'}) ? [$UB::CGI->param('FORM_GROUP_30')] : (($LEVEL_30 eq "*") ? $AllGroups : [split (/\n/, $LEVEL_30)]),
    EXTRA     => "style=\"width:100%\" onChange=\"Selected(document.form.FORM_GROUP_30, document.form.FORM_LEVEL_30)\""
  );
  $FORM{'FORM_GROUP_50'}     = &HTML::Select (
    NAME      => "FORM_GROUP_50"                   ,
    VALUE     => $Groups                           ,
    MULTIPLE  => 1                                 ,
    SIZE      => 3                                 ,
    SELECTED  => ($UB::QUERY{'FORM_GROUP_50'}) ? [$UB::CGI->param('FORM_GROUP_50')] : (($LEVEL_50 eq "*") ? $AllGroups : [split (/\n/, $LEVEL_50)]),
    EXTRA     => "style=\"width:100%\" onChange=\"Selected(document.form.FORM_GROUP_50, document.form.FORM_LEVEL_50)\""
  );

  &Standard::PrintHTMLHeader();
  print &HTML::Header;
  print<<HTML;
<script language="JavaScript">
<!--
function Checked4Everyone () {
  if (document.form.FORM_LEVEL_00.checked) {
    document.form.FORM_LEVEL_30.checked = true;
    document.form.FORM_LEVEL_50.checked = true;

    SelectAll (document.form.FORM_GROUP_30);
    SelectAll (document.form.FORM_GROUP_50);
  }
}

function Checked (CheckboxObject, SelectObject) {
  if (CheckboxObject.checked) {
    SelectAll  (SelectObject);
  } else {
    document.form.FORM_LEVEL_00.checked = false;
    UnselectAll(SelectObject);
  }
}

function Selected (SelectObject, CheckboxObject) {
  var selected1 = false;
  var selected2 = true ;

  if (SelectObject.options[0].selected) {
    SelectAll (SelectObject);
    CheckboxObject.checked = true ;
    return;
  }

  for (var i = 0; i < SelectObject.length; i++) {
    if (SelectObject.options[i].selected) {
      selected1 = true ;
    } else {
      selected2 = false;
    }
  }

  if (selected1) {
    CheckboxObject.checked  = true ;
  } else {
    CheckboxObject.checked  = false;
  }

  if (!selected2) {
    document.form.FORM_LEVEL_00.checked = false;
  }
}

function SelectAll (SelectObject) {
  for (var i = SelectObject.length - 1; i >= 0; i--) {
    SelectObject.options[i].selected = true ;
  }
}

function UnselectAll (SelectObject) {
  for (var i = SelectObject.length - 1; i >= 0; i--) {
    SelectObject.options[i].selected = false;
  }
}
// -->
</script>
<form action="$SYSTEM::CP_URL/Panel.$SYSTEM::EXT" method="post" name="form">
<input type="hidden" name="action" value="DoModifyCategory">
<input type="hidden" name="CID" value="$FORM{'FORM_ID'}">
<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> / Categories Management / Modify category
    </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>
HTML

  if ($ERROR ne "") {
    print<<HTML;
                        There were the following errors during modifying category.<br>
                        <table width="100%" border="0" cellspacing="2" cellpadding="2">
                          <tr>
                            <td><font color="#FF0000">$ERROR</font></td>
                          </tr>
                        </table>
                        Please make any changes and try again!<br>
HTML
  }

print<<HTML;
                      Please fill out all required (<font color="Red"><b>*</b></font>) fields.
                      </td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
            &nbsp;<br>
            &nbsp;&nbsp;General Information
            <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 width="100" valign="top" nowrap>Title <font color="Red"><b>*</b></font></td>
                      <td width="100%"><input type="text" name="FORM_TITLE" value="$FORM{'FORM_TITLE'}" size="45" style="width:100%"></td>
                      <td width="20" valign="top" nowrap><img src="$SYSTEM::IMAGE_URL/help.gif" width=20 height=20 border=0 alt="enter the name of category that you want to call. (128 chars. max.)"></td>
                    </tr>
                    <tr>
                      <td width="100" valign="top" nowrap>Description</td>
                      <td width="100%"><textarea cols="45" rows="3" name="FORM_DESCRIPTION" style="width:100%" wrap="virtual">$FORM{'FORM_DESCRIPTION'}</textarea></td>
                      <td width="20" valign="top" valign="top" nowrap><img src="$SYSTEM::IMAGE_URL/help.gif" width=20 height=20 border=0 alt="write some words about this category that you want to say to your visitors. (512 chars. max.)"></td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
            &nbsp;<br>
            &nbsp;&nbsp;Permission
            <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 colspan="2">$FORM{'FORM_LEVEL_00'}Display this category to anonymous members.</td>
                      <td rowspan="5" width="20" valign="top" nowrap><img src="$SYSTEM::IMAGE_URL/help.gif" width=20 height=20 border=0 alt="choose what level and member groups of members that this category will visable to them.\n\nthe level and member groups of members that you didn\'t choose, then this category will invisable to them.\n\ntip:\nif you choose \'all groups\', then all the member groups that you create in future will also have this permission to see this category."></td>
                    </tr>
                    <tr>
                      <td colspan="2">$FORM{'FORM_LEVEL_30'}Display this category to normal members that in</td>
                    </tr>
                    <tr>
                      <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                      <td width="100%">$FORM{'FORM_GROUP_30'}</td>
                    </tr>

                    <tr>
                      <td colspan="2">$FORM{'FORM_LEVEL_50'}Display this category to premium members that in</td>
                    </tr>
                    <tr>
                      <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                      <td width="100%">$FORM{'FORM_GROUP_50'}</td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
      <table width="100%" border="0" cellspacing="0" cellpadding="5">
        <tr>
          <td align="right"><input type="submit" value="Modify it">&nbsp;&nbsp;<input type="Reset" value="Cancel"></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</form>
</body>
</html>
HTML
}

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