<?php
// +---------------------------------------------+
// |     Copyright  2003 - 2005 weenCompany      |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+

define('IN_ADMIN', true);
define('IN_WEENCOMPANY', true);

$rootpath = "./../";

include($rootpath . 'includes/core.php');

PrintHeader('群組設置');

// get the value of $action
$action = isset($_POST['action']) ? $_POST['action'] : (isset($_GET['action']) ? $_GET['action'] : 'displaydefault');

// module bitfield data
$modulebitfield = array('canview'     => 1,
                        'cansubmit'   => 2,
                        'candownload' => 4,
                        'cancomment'  => 8,
                        'canadmin'    => 16);



// ############################## DELETE USERGROUP #############################

if($action == 'deleteusergroup')
{
  $usergroupid   = isset($_GET['usergroupid']) ? $_GET['usergroupid'] : $_POST['usergroupid'];
  $confirmdelete = isset($_POST['confirmdelete']) ? $_POST['confirmdelete'] : '0';

  if($confirmdelete == ' 取消 ')
  {
    $action = 'displaydefault';
  }
  else if($confirmdelete == '0')
  {
    // get the usergroupname
    $usergroup = $DB->query_first("SELECT name FROM " . TABLE_PREFIX . "usergroups WHERE usergroupid = $usergroupid");

    $description = '<b>刪除群組: "' . $usergroup['name'] . '"?</b><br /><br />
                    注: 刪除群組後, 屬於此群組的所有用戶將轉至"註冊用戶"組.';

    $hiddenvalues = '<input type="hidden" name="usergroupid" value="' . $usergroupid . '" />
                     <input type="hidden" name="action" value="deleteusergroup" />';

    // arguments: description, hidden input values, form redirect page
    ConfirmDelete($description, $hiddenvalues, 'usergroups.php');
  }
  else if($confirmdelete == ' 確定 ')
  {
    $DB->query("DELETE FROM " . TABLE_PREFIX . "usergroups WHERE usergroupid = '" . $usergroupid . "'");
    $DB->query("UPDATE " . TABLE_PREFIX . "users SET usergroupid = 3 WHERE usergroupid = '" . $usergroupid . "'");
    // redirect
    PrintRedirect('usergroups.php', 1);
  }
}



// ############################# INSERT USERGROUP ##############################

if($action == 'insertusergroup')
{
  $name                  = trim($_POST['name']);
  if ($name == '')
  {
   Errmsg('請輸入群組名稱!');
  }
  
  $usergroupid           = $_POST['usergroupid'];  // set to 0
  $forumusergroupid      = isset($_POST['forumusergroupid']) ? $_POST['forumusergroupid'] : 0;

  $adminaccess           = $_POST['adminaccess'];
  $commentaccess         = $_POST['commentaccess'];
  $offlinecategoryaccess = $_POST['offlinecategoryaccess'];

  $categoryviewids       = @implode(',', $_POST['categoryviewids']);
  $categorymenuids       = @implode(',', $_POST['categorymenuids']);

  $moduleviewids         = @implode(',', $_POST['moduleviewids']);
  $modulesubmitids       = @implode(',', $_POST['modulesubmitids']);
  $moduledownloadids     = @implode(',', $_POST['moduledownloadids']);
  $modulecommentids      = @implode(',', $_POST['modulecommentids']);
  $moduleadminids        = @implode(',', $_POST['moduleadminids']);

  $custommoduleviewids   = @implode(',', $_POST['custommoduleviewids']);
  $custommoduleadminids  = @implode(',', $_POST['custommoduleadminids']);

  $DB->query("INSERT INTO " . TABLE_PREFIX . "usergroups
              VALUES ('$usergroupid', '$forumusergroupid', '$name', '$adminaccess', '$commentaccess', '$offlinecategoryaccess', '$categoryviewids', '$categorymenuids',
                      '$moduleviewids', '$modulesubmitids', '$moduledownloadids', '$modulecommentids', '$moduleadminids', '$custommoduleviewids', '$custommoduleadminids') ");

  PrintRedirect('./usergroups.php?action=editusergroup&usergroupid=' . $DB->insert_id(), 1);
}



// ############################# UPDATE USERGROUP ##############################

if($action == 'updateusergroup')
{
  $name                  = trim($_POST['name']);
  if ($name == '')
  {
	   Errmsg('群組名稱不能為空!');
  }
  $usergroupid           = $_POST['usergroupid'];
  $forumusergroupid      = isset($_POST['forumusergroupid']) ? $_POST['forumusergroupid'] : 0;

  $adminaccess           = $_POST['adminaccess'];
  $commentaccess         = $_POST['commentaccess'];
  $offlinecategoryaccess = $_POST['offlinecategoryaccess'];

  $categoryviewids       = @implode(',', $_POST['categoryviewids']);
  $categorymenuids       = @implode(',', $_POST['categorymenuids']);

  $moduleviewids         = @implode(',', $_POST['moduleviewids']);
  $modulesubmitids       = @implode(',', $_POST['modulesubmitids']);
  $moduledownloadids     = @implode(',', $_POST['moduledownloadids']);
  $modulecommentids      = @implode(',', $_POST['modulecommentids']);
  $moduleadminids        = @implode(',', $_POST['moduleadminids']);

  $custommoduleviewids   = @implode(',', $_POST['custommoduleviewids']);
  $custommoduleadminids  = @implode(',', $_POST['custommoduleadminids']);

  $DB->query("UPDATE " . TABLE_PREFIX . "usergroups SET forumusergroupid= '$forumusergroupid',
                                                        name= '$name',
                                                        adminaccess           = '$adminaccess',
                                                        commentaccess         = '$commentaccess',
                                                        offlinecategoryaccess = '$offlinecategoryaccess',
                                                        categoryviewids       = '$categoryviewids',
                                                        categorymenuids       = '$categorymenuids',
                                                        moduleviewids         = '$moduleviewids',
                                                        modulesubmitids       = '$modulesubmitids',
                                                        moduledownloadids     = '$moduledownloadids',
                                                        modulecommentids      = '$modulecommentids',
                                                        moduleadminids        = '$moduleadminids',
                                                        custommoduleviewids   = '$custommoduleviewids',
                                                        custommoduleadminids  = '$custommoduleadminids'
                                                  WHERE usergroupid           = '$usergroupid'");

  echo '<BODY onLoad="parent.leftFrame.location.reload(true)">';
  PrintRedirect('./usergroups.php?action=editusergroup&usergroupid=' . $usergroupid, 1);
}



// ############################## DISPLAY DEFAULT ##############################

if($action == 'displaydefault')
{
  global $stylepath;

  // get the current usergroups
  $getusergroups = $DB->query("SELECT usergroupid, forumusergroupid, name FROM " . TABLE_PREFIX . "usergroups ORDER BY usergroupid");
  $usergrouprows = $DB->get_num_rows($getusergroups);

  PrintSection('創建用戶組');
  echo '<table width="100%" border="0" cellpadding="5" cellspacing="0">
        <tr>
          <td class="tdrow1" colspan="2">創建用戶組</td>
        </tr>
        <tr>
          <td class="tdrow2" width="70%">創建新的用戶群組:</td>
          <td class="tdrow3">
            <form method="post" action="usergroups.php">
            <input type="hidden" name="action" value="createusergroup" />
            <input type="submit" value=" 創建群組 " />
            </form>
          </td>
        </tr>
        </table>';
  EndSection();

  PrintSection('群組權限設置');
  if($usergrouprows)
  {
    if($forum = $DB->query_first("SELECT name FROM " . TABLE_PREFIX . "usersystems WHERE activated = '1' AND name != 'weenCompany'"))
    {
      $forumintegration = 1;
    }
    else
    {
      $forumintegration = 0;
    }

    echo '<table width="100%" border="0" cellpadding="5" cellspacing="0">
		  <tr>
            <td class="tdrow1" colspan="3">群組權限說明</td>
          </tr>
		  <tr>
            <td class="tdrow3" colspan="3">默認狀態下, 群組權限說明:<br/>1. 系統管理員: 所有網站權限.<br/>2. 信息管理員: 所有模塊設置及內容管理權限.<br/>3. 註冊用戶: 網站前臺發表評論, 文章, 圖片等權限.<br/>4. 遊客: 權限等同於未註冊用戶.</td>
          </tr>
		  <tr>
            <td class="tdrow1">群組名稱</td>
            ' . iif(!$forumintegration, '<td class="tdrow1">用戶數量</td>') . '
            <td class="tdrow1">刪除</td>
          </tr>';

    // usergroupids that can't be deleted
    $dontdeleteids = array(1, 2, 3, 4);

    while($usergroup = $DB->fetch_array($getusergroups))
    {
      if($forumintegration == 0)
      {
        $totalusergroupusers = $DB->query_first("SELECT COUNT(*) AS value FROM " . TABLE_PREFIX . "users WHERE usergroupid = '" . $usergroup['usergroupid'] . "'");
      }

      echo '<tr>
              <td class="tdrow2"><a href="./usergroups.php?action=editusergroup&usergroupid=' . $usergroup['usergroupid'] . '">' . iif(strlen($usergroup['name']), $usergroup['name'], '未命名') . '</a>&nbsp;(ID: '.$usergroup['usergroupid'].')</td>
              ' . iif(!$forumintegration, '<td class="tdrow3">' . $totalusergroupusers['value'] . '</td>') . '
              <td class="tdrow2">' . iif(in_array($usergroup['usergroupid'], $dontdeleteids), '<img src="' . $stylepath . 'images/notrash.gif" />', '<a href="usergroups.php?action=deleteusergroup&usergroupid=' . $usergroup['usergroupid'] . '"><img src="' . $stylepath . 'images/trash.gif" /></a>') . '</td>
            </tr>';
    }
  }

  echo '</table>';
  EndSection();
}



// ######################## DISPLAY MODULES PERMISSIONS ########################

function DisplayModulesPermissions($modules, $modulestype, $formgroup)
{
  global $DB, $modulebitfield, $moduleviewids, $modulesubmitids, $moduledownloadids,
         $modulecommentids, $moduleadminids, $custommoduleviewids, $custommoduleadminids;

  // return if there are no rows to print
  if($DB->get_num_rows($modules) == 0)
  {
    return;
  }

  // print label columns
    echo '<tr>
            <td class="tdrow1" width="12.5%">' . $modulestype . '</td>
            <td class="tdrow1" width="12.5%"><input type="checkbox" checkall="view_' . $formgroup . '" onclick="javascript: return select_deselectAll (\'usergroups\', this, \'view_' . $formgroup . '\');"> 允許查看</td>
            <td class="tdrow1" width="12.5%"><input type="checkbox" checkall="submit_' . $formgroup . '" onclick="javascript: return select_deselectAll (\'usergroups\', this, \'submit_' . $formgroup . '\');"> 允許發表</td>
            <td class="tdrow1" width="12.5%"><input type="checkbox" checkall="download_' . $formgroup . '" onclick="javascript: return select_deselectAll (\'usergroups\', this, \'download_' . $formgroup . '\');"> 允許下載</td>
            <td class="tdrow1" width="12.5%"><input type="checkbox" checkall="comment_' . $formgroup . '" onclick="javascript: return select_deselectAll (\'usergroups\', this, \'comment_' . $formgroup . '\');"> 允許評論</td>
            <td class="tdrow1" width="12.5%" style="background: #FF7D7D; color: #000000; border: 1px solid #000000;"><input type="checkbox" checkall="admin_' . $formgroup . '" onclick="javascript: return select_deselectAll (\'usergroups\', this, \'admin_' . $formgroup . '\');"> 允許管理</td>
          </tr>';


  // start printing the modules
  if($modulestype == '自定義模塊')
  {
    while($module = $DB->fetch_array($modules))
    {
      $canviewstatus     = in_array($module['custommoduleid'], $custommoduleviewids)     ? 'CHECKED' : '';
      $canadminstatus    = in_array($module['custommoduleid'], $custommoduleadminids)    ? 'CHECKED' : '';

      echo '<tr>
              <td class="tdrow2">' . $module['name'] . '</td>
              <td class="tdrow3">' . iif($module['settings'] & $modulebitfield['canview'],     '<input type="checkbox" name="custommoduleviewids[]"     value="' . $module['custommoduleid'] . '" ' . $canviewstatus     . '  checkme="view_' . $formgroup . '" />', '<input type="checkbox" DISABLED/>') . '</td>
              <td class="tdrow2"><input type="checkbox" DISABLED/></td>
              <td class="tdrow3"><input type="checkbox" DISABLED/></td>
              <td class="tdrow2"><input type="checkbox" DISABLED/></td>
              <td class="tdrow3">' . iif($module['settings'] & $modulebitfield['canadmin'],    '<input type="checkbox" name="custommoduleadminids[]"    value="' . $module['custommoduleid'] . '" ' . $canadminstatus    . ' checkme="admin_' . $formgroup . '" />', '<input type="checkbox" DISABLED/>') . '</td>
            </tr>';
    }
  }
  else
  {
    while($module = $DB->fetch_array($modules))
    {
      $canviewstatus     = in_array($module['moduleid'], $moduleviewids)     ? 'CHECKED' : '';
      $cansubmitstatus   = in_array($module['moduleid'], $modulesubmitids)   ? 'CHECKED' : '';
      $candownloadstatus = in_array($module['moduleid'], $moduledownloadids) ? 'CHECKED' : '';
      $cancommentstatus  = in_array($module['moduleid'], $modulecommentids)  ? 'CHECKED' : '';
      $canadminstatus    = in_array($module['moduleid'], $moduleadminids)    ? 'CHECKED' : '';

      echo '<tr>
              <td class="tdrow2">' . $module['name'] . '</td>
              <td class="tdrow3">' . iif($module['settings'] & $modulebitfield['canview'],     '<input type="checkbox" name="moduleviewids[]"     value="' . $module['moduleid'] . '" ' . $canviewstatus     . ' checkme="view_' . $formgroup . '"  />', '<input type="checkbox" DISABLED/>') . '</td>
              <td class="tdrow2">' . iif($module['settings'] & $modulebitfield['cansubmit'],   '<input type="checkbox" name="modulesubmitids[]"   value="' . $module['moduleid'] . '" ' . $cansubmitstatus   . ' checkme="submit_' . $formgroup . '" />', '<input type="checkbox" DISABLED/>') . '</td>
              <td class="tdrow3">' . iif($module['settings'] & $modulebitfield['candownload'], '<input type="checkbox" name="moduledownloadids[]" value="' . $module['moduleid'] . '" ' . $candownloadstatus . ' checkme="download_' . $formgroup . '" />', '<input type="checkbox" DISABLED/>') . '</td>
              <td class="tdrow2">' . iif($module['settings'] & $modulebitfield['cancomment'],  '<input type="checkbox" name="modulecommentids[]"  value="' . $module['moduleid'] . '" ' . $cancommentstatus  . ' checkme="comment_' . $formgroup . '" />', '<input type="checkbox" DISABLED/>') . '</td>
              <td class="tdrow3">' . iif($module['settings'] & $modulebitfield['canadmin'],    '<input type="checkbox" name="moduleadminids[]"    value="' . $module['moduleid'] . '" ' . $canadminstatus    . ' checkme="admin_' . $formgroup . '" />', '<input type="checkbox" DISABLED/>') . '</td>
            </tr>';
    }
  }
}


// ############################ DISPLAY CATEGORIES #############################
function DisplayCategories($categorymenuids, $categoryviewids, $parentid = 0, $sublevelmarker = '')
{
  global $DB;
  global $listedcategories; 

  // start table
  if($parentid != 0)
  {
    $sublevelmarker .= '- ';
  }

  $getcategories = $DB->query("SELECT categoryid, name, parentid FROM " . TABLE_PREFIX . "categories WHERE parentid = $parentid ORDER BY displayorder");

  while($category = $DB->fetch_array($getcategories))
  {
    $listedcategories[] = $category['name'];
    $menuitemstatus  = in_array($category['categoryid'], $categorymenuids) ? 'CHECKED' : '';
    $canaccessstatus = in_array($category['categoryid'], $categoryviewids) ? 'CHECKED' : '';

    echo '<tr>
            <td class="tdrow2">' . $sublevelmarker . iif(strlen($category['name']), $category['name'], '<b>未命名</b>') . '</td>
            <td class="tdrow3"><input type="checkbox" name="categorymenuids[]" value="' . $category['categoryid'] . '" ' . $menuitemstatus  . ' checkme="display" /></td>
            <td class="tdrow2"><input type="checkbox" name="categoryviewids[]" value="' . $category['categoryid'] . '" ' . $canaccessstatus . ' checkme="view" /></td>
          </tr>';

    DisplayCategories($categorymenuids, $categoryviewids, $category['categoryid'], $sublevelmarker);
  }
}

// ########################## DISPLAY USERGROUP FORM ###########################

if($action == 'createusergroup' OR $action == 'editusergroup')
{
  if($action == 'createusergroup')
  {
    $usergroup = array('usergroupid'           => 0,
                       'name'                  => '',
                       'forumusergroupid'      => 0,
                       'adminaccess'           => 0,
                       'commentaccess'         => 0,
                       'offlinecategoryaccess' => 0);

    $categoryviewids   = array();
    $categorymenuids   = array();

    $moduleviewids     = array();
    $modulesubmitids   = array();
    $moduledownloadids = array();
    $modulecommentids  = array();
    $moduleadminids    = array();

    $custommoduleviewids  = array();
    $custommoduleadminids = array();

    PrintSection('創建群組');
  }
  else if($action == 'editusergroup')
  {
    $usergroupid = isset($_POST['usergroupid']) ? $_POST['usergroupid'] : $_GET['usergroupid'];
    $usergroup = $DB->query_first("SELECT * FROM " . TABLE_PREFIX . "usergroups WHERE usergroupid = '$usergroupid'");

    $categoryviewids   = @explode(',', $usergroup['categoryviewids']);
    $categorymenuids   = @explode(',', $usergroup['categorymenuids']);

    $moduleviewids     = @explode(',', $usergroup['moduleviewids']);
    $modulesubmitids   = @explode(',', $usergroup['modulesubmitids']);
    $moduledownloadids = @explode(',', $usergroup['moduledownloadids']);
    $modulecommentids  = @explode(',', $usergroup['modulecommentids']);
    $moduleadminids    = @explode(',', $usergroup['moduleadminids']);

    $custommoduleviewids  = @explode(',', $usergroup['custommoduleviewids']);
    $custommoduleadminids = @explode(',', $usergroup['custommoduleadminids']);

    PrintSection('編輯群組: ' . $usergroup['name']);
  }

  echo '<form method="post" action="usergroups.php" name="usergroups">
        <input type="hidden" name="action" value="' . iif($action == 'createusergroup', 'insertusergroup', 'updateusergroup') . '" />
        <input type="hidden" name="usergroupid" value="' . $usergroup['usergroupid'] . '" />

        <table width="100%" border="0" cellpadding="5" cellspacing="0">
        <tr>
          <td class="tdrow1" colspan="2">群組名稱</td>
        </tr>
        <tr>
          <td class="tdrow2" width="70%">輸入用戶群組的名稱:</td>
          <td class="tdrow3"><input type="text" name="name" value="' . $usergroup['name'] . '" /></td>
        </tr>';

  if($forum = $DB->query_first("SELECT name, dbname, tblprefix FROM " . TABLE_PREFIX . "usersystems WHERE activated = '1' AND name != 'weenCompany'"))
  {
    switch($forum['name'])
    {
      case 'Discuz5':
        $forumusergroup_sql = "SELECT groupid AS usergroupid, grouptitle AS usergroupname FROM " . $forum['tblprefix'] . "usergroups ORDER BY groupid";
        break;

      case 'vBulletin 2':
        $forumusergroup_sql = "SELECT usergroupid AS usergroupid, title AS usergroupname FROM " . $forum['tblprefix'] . "usergroup ORDER BY usergroupid";
      break;
	  
      case 'vBulletin 3':
        $forumusergroup_sql = "SELECT usergroupid AS usergroupid, title AS usergroupname FROM " . $forum['tblprefix'] . "usergroup ORDER BY usergroupid";
      break;

      case 'phpBB2':
        $forumusergroup_sql = "SELECT group_id AS usergroupid, group_name AS usergroupname FROM " . $forum['tblprefix'] . "groups WHERE group_name != '' ORDER BY group_id";
      break;

      case 'Invision Power Board 2':
        $forumusergroup_sql = "SELECT g_id AS usergroupid, g_title AS usergroupname FROM " . $forum['tblprefix'] . "groups ORDER BY g_id";
      break;

      case 'Simple Machines Forum 1':
        $forumusergroup_sql = "SELECT ID_GROUP AS usergroupid, groupName AS usergroupname FROM " . $forum['tblprefix'] . "membergroups ORDER BY ID_GROUP";
      break;
    }

    // switch database? $dbname is include from config.php
    if($forum['dbname'] != $dbname)
    {
      $DB->select_db($forum['dbname']);
    }

    $getforumusergroups = $DB->query($forumusergroup_sql);

    // switch back to weencompany database? $dbname is include from config.php
    if($forum['dbname'] != $dbname)
    {
      $DB->select_db($dbname);
    }

    echo '<tr>
            <td class="tdrow1" colspan="2">' . $forum['name'] . '論壇接口</td>
          </tr>
          <tr>
            <td class="tdrow2" width="70%">選擇一個論壇用戶群組與系統群組進行關聯:</td>
            <td class="tdrow3">
              <select name="forumusergroupid">
                <option value=""></option>';

    if($forum['name'] == 'phpBB2')
    {
      echo '<option value="-3" ' . iif($usergroup['forumusergroupid'] == -3, 'SELECTED', '') . '>註冊用戶</option>';
    }

    while($forumusergroup = $DB->fetch_array($getforumusergroups))
    {
      echo '<option value="' . $forumusergroup['usergroupid'] . '" ' . iif($usergroup['forumusergroupid'] == $forumusergroup['usergroupid'], 'SELECTED', '') . '>' . $forumusergroup['usergroupname'] . '</option>';
    }

    echo '    </select>
            </td>
          </tr>';
  }

  echo '</table>';
  EndSection();


  PrintSection('管理員權限');
  echo '<table width="100%" border="0" cellpadding="5" cellspacing="0">
        <tr>
          <td class="tdrow1" colspan="2">完全管理權限</td>
        </tr>
        <tr>
          <td class="tdrow2" width="70%">是否擁有網站系統完全管理權限?<br/>注: 模塊權限可由下面"模塊權限"設置中的是否允許管理來決定.</td>
          <td class="tdrow3"><input type="radio" name="adminaccess" value="1" ' . iif($usergroup['adminaccess'] == 1, 'CHECKED', '') . ' /> 是 <input type="radio" name="adminaccess" value="0" ' . iif($usergroup['adminaccess'] == 0, 'CHECKED', '') . ' /> 否</td>
        </tr>
        <tr>
          <td class="tdrow1" colspan="2">前臺評論管理權限</td>
        <tr>
          <td class="tdrow2" width="70%">是否擁有在網站前臺管理用戶評論的權限?</td>
          <td class="tdrow3"><input type="radio" name="commentaccess" value="1" ' . iif($usergroup['commentaccess'] == 1, 'CHECKED', '') . ' /> 是 <input type="radio" name="commentaccess" value="0" ' . iif($usergroup['commentaccess'] == 0, 'CHECKED', '') . ' /> 否</td>
        </tr>
        <tr>
          <td class="tdrow1" colspan="2">網站關閉瀏覽權限</td>
        </tr>
        <tr>
          <td class="tdrow2" width="70%">當網站關閉後, 是否仍可以瀏覽網站?<br/>注: 僅當用戶以"記住我"的方式登陸登陸後, 設置才有效.</td>
          <td class="tdrow3"><input type="radio" name="offlinecategoryaccess" value="1" ' . iif($usergroup['offlinecategoryaccess'] == 1, 'CHECKED', '') . ' /> 是 <input type="radio" name="offlinecategoryaccess" value="0" ' . iif($usergroup['offlinecategoryaccess'] == 0, 'CHECKED', '') . ' /> 否</td>
        </tr>
        </table>';
  EndSection();


  PrintSection('模塊權限');
  echo '<table width="100%" border="0" cellpadding="5" cellspacing="0">';

  // WEENCOMPANY MODULES
  $modules = $DB->query("SELECT moduleid, name, settings FROM " . TABLE_PREFIX . "modules WHERE moduleid != 1 AND authorname = 'weencompany' ORDER BY name");
  DisplayModulesPermissions($modules, '系統模塊', 'main');

  // CLONED MODULES
  $modules = $DB->query("SELECT moduleid, name, settings FROM " . TABLE_PREFIX . "modules WHERE moduleid != 1 AND authorname = 'weencompany_cloner' ORDER BY name");
  DisplayModulesPermissions($modules, '複製的模塊', 'clone');

  // CUSTOM MODULES
  $modules = $DB->query("SELECT custommoduleid, name, settings FROM " . TABLE_PREFIX . "custommodules ORDER BY name");
  DisplayModulesPermissions($modules, '自定義模塊', 'custom');

  // DOWNLOADED MODULES
  $modules = $DB->query("SELECT moduleid, name, settings FROM " . TABLE_PREFIX . "modules WHERE moduleid != 1 AND authorname != 'weencompany' AND authorname != 'weencompany_cloner' ORDER BY name");
  DisplayModulesPermissions($modules, '下載的模塊', 'downloaded');

  echo '</table>';
  EndSection();


  PrintSection('菜單權限');
  echo '<table width="100%" border="0" cellpadding="5" cellspacing="0">
        <tr>
          <td class="tdrow1" width="200">菜單名稱</td>
          <td class="tdrow1" width="200"><input type="checkbox" checkall="display" onclick="javascript: return select_deselectAll (\'usergroups\', this, \'display\');"> 顯示在菜單欄</td>
          <td class="tdrow1"><input type="checkbox" checkall="view" onclick="javascript: return select_deselectAll (\'usergroups\', this, \'view\');"> 允許查看</td>
        </tr>';

  // Category Permissions, get all categories
  DisplayCategories($categorymenuids,$categoryviewids);

  echo '</table>';
  EndSection();

  echo '<table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td align="center"><input type="submit" name="" value="' . iif($action == 'createusergroup', ' 創建群組 ', ' 保存更新 ') . '" />
        </tr>
        </table>

        </form>';
  
  // INITIALISE CHECKBOX GROUPS
  echo' <script type="text/javascript">

        function initGroups()
        {
          initCheckboxGroup("usergroups", "view_main");
          initCheckboxGroup("usergroups", "submit_main");
          initCheckboxGroup("usergroups", "download_main");
          initCheckboxGroup("usergroups", "comment_main");
          initCheckboxGroup("usergroups", "admin_main");
          
          initCheckboxGroup("usergroups", "view_clone");
          initCheckboxGroup("usergroups", "submit_clone");
          initCheckboxGroup("usergroups", "download_clone");
          initCheckboxGroup("usergroups", "comment_clone");
          initCheckboxGroup("usergroups", "admin_clone");
          
          initCheckboxGroup("usergroups", "view_custom");
          initCheckboxGroup("usergroups", "submit_custom");
          initCheckboxGroup("usergroups", "download_custom");
          initCheckboxGroup("usergroups", "comment_custom");
          initCheckboxGroup("usergroups", "admin_custom");
          
          initCheckboxGroup("usergroups", "view_downloaded");
          initCheckboxGroup("usergroups", "submit_downloaded");
          initCheckboxGroup("usergroups", "download_downloaded");
          initCheckboxGroup("usergroups", "comment_downloaded");
          initCheckboxGroup("usergroups", "admin_downloaded");
          
          initCheckboxGroup("usergroups", "display");
          initCheckboxGroup("usergroups", "view");
        }
        
        window.onload = initGroups;
        
        </script>';

}



// ############################### PRINT FOOTER ################################

PrintFooter();

?>
