<?php

define('MOD_ACCESS', true);

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

$rootpath = "./../";

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

$divNameList = '';

// ############################ SETUP PAGE CHARSET #############################

SetPageCharacterSet();

function StartMenu()
{
        echo '<table cellpadding="0" cellspacing="0" border="0" width="100%">
        <tr>
          <td bgcolor="#FFFFFF" style="padding-left: 2px; padding-top: 2px; padding-right: 2px; padding-bottom: 0px;">';
}


function PrintMenuTitle($name, $open)
{
        global $stylepath, $divNameList;

        // Save a list of Block Names
        if(strlen($divNameList) > 0)
        $divNameList .= ',';

        $divNameList .= '"' . $name . '"';

        echo '<div class="menutitle" onclick="ToggleDiv(\'' . $name . '\');"><img id="img_' . $name . '" src="' . $stylepath . 'images/'. iif($open, 'closed.gif', 'open.gif') . '" border="0" align="absmiddle">&nbsp;&nbsp;' . $name . '</div>
        <div id="div_' . $name . '" style="display:' . iif($open, 'block', 'none') . ';">';
}

function PrintMenuRow($url, $name)
{
        global $stylepath;

        echo '<div class="menulink-normal" onclick="nav_goto(\'' . $url . '\');" onmouseover="this.className=\'menulink-hover\';" onmouseout="this.className=\'menulink-normal\'"><img src="' . $stylepath . 'images/menu_1.gif" border="0" align="absmiddle">&nbsp;<a href="' . $url . '" target="mainFrame">' . $name . '</a></div>';
}


function EndBlock()
{
        echo '</div>';
}


function EndMenu()
{
        echo '  </td>
        </tr>
        </table>

        <div style="margin-bottom:24px"></div>';
}

function PrintCategories($parentid, $indent)
{
        global $DB;
        //'categories.php?action=displaycategories',

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

        while($cat = $DB->fetch_array($getcategories))
        {
                PrintMenuRow('mycategories.php?categoryid=' . $cat['categoryid'], $indent . $cat['name']);
                PrintCategories($cat['categoryid'], $indent . '--');
        }
}

?>


<html>
<head>
  <link rel=stylesheet href='<?php echo $stylepath; ?>menu.css' />
  <script type="text/javascript">
  var cookieName = 'cwsaMenuCookie';

  if(top.location == self.location)
  {
          self.location.replace("index.php")
  }

  function setCookie(Cookie, value, expiredays)
  {
          var ExpireDate = new Date ();
          ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
          document.cookie = Cookie + "=" + escape(value) +
          ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
  }

  function getCookie(Cookie)
  {
          if (document.cookie.length > 0)
          {
                  begin = document.cookie.indexOf(Cookie+"=");
                  if (begin != -1)
                  {
                          begin += Cookie.length+1;
                          end = document.cookie.indexOf(";", begin);
                          if (end == -1) end = document.cookie.length;
                          return unescape(document.cookie.substring(begin, end));
                  }
          }
          return null;
  }

  function LoadMenu()
  {
          cookieMenu = getCookie(cookieName);
          if(cookieMenu != null)
          {
                  for(i = 0; i < divNames.length; i++)
                  {
                          if(cookieMenu.indexOf(divNames[i]) >= 0)
                          {
                                  document.getElementById('div_' + divNames[i]).style.display = 'block';
                                  document.getElementById('img_' + divNames[i]).src = '<?php echo $stylepath; ?>images/closed.gif';
                          }
                          else
                          {
                                  document.getElementById('div_' + divNames[i]).style.display = 'none';
                                  document.getElementById('img_' + divNames[i]).src = '<?php echo $stylepath; ?>images/open.gif';
                          }
                  }
          }
  }

  function SaveMenu()
  {
          var cookiestring = '';

          for(i = 0; i < divNames.length; i++)
          {
                  var block = document.getElementById('div_' + divNames[i]);

                  if(block.style.display != 'none')
                  {
                          cookiestring += divNames[i] + '|';
                  }
          }

          setCookie(cookieName,cookiestring,1);
  }

  function ToggleDiv(className)
  {
          var img = document.getElementById('img_' + className);
          var div = document.getElementById('div_' + className);

          if(div.style.display == 'none')
          {
                  img.src = '<?php echo $stylepath; ?>images/closed.gif';
                  div.style.display = 'block';
          }
          else
          {
                  img.src = '<?php echo $stylepath; ?>images/open.gif';
                  div.style.display = 'none';
          }
  }

  function nav_goto(targeturl)
  {
          parent.frames.mainFrame.location = targeturl;
  }
  </script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>

<body>



<table width="100%" border="0" cellpadding="0" cellspacing="0" align="center">
<tr>
  <td align="center"><a href="mphome.php" target="mainFrame" class="normal" onfocus="this.blur()" title="后台首页"><img src="<?php echo $stylepath; ?>images/logo.gif" border="0" /></a></td>
</tr>
<tr>
  <td align="center" style="padding-top: 8px; padding-bottom: 12px;">
    <a href="mphome.php" target="mainFrame" class="normal">後臺首頁</a>&nbsp;|&nbsp;
    <a href="../" target="_blank" class="normal">網站</a>&nbsp;|&nbsp;

    <?php

            echo '<a href="index.php?logout=1" class="normal" target="_parent" onclick="return confirm(\'確定退出weenCompany聞名企業網站系統嗎?\');"><img src="' . $stylepath . 'images/logout.gif" border="0" /> 退出</a>';

    ?>
  </td>
</tr>
</table>

<table width="150" border="0" cellpadding="0" cellspacing="0" align="center">
<tr>
  <td valign="top" align="center">

  <?php

  // admin menus
  if($userinfo['adminaccess'])
  {

          // ########################## Menu_Settings  ##########################

          StartMenu();
          PrintMenuTitle('系統設置', true);
          PrintMenuRow('mainsettings.php', '基本設置');
          PrintMenuRow('templates.php', '網站模板');
          PrintMenuRow('modules.php', '模塊管理');
          PrintMenuRow('comments.php', '評論管理');
          PrintMenuRow('forumintegration.php', '論壇接口');

          PrintMenuRow('languages.php', '前臺語言');
          EndBlock();
          PrintMenuTitle('系統維護', false);
          PrintMenuRow('sysupgrade.php', '系統升級');
          PrintMenuRow('template_editor.php?action=displayimages', '模板編輯器');
          PrintMenuRow('database.php', '數據庫維護');
          PrintMenuRow('info.php', '服務器信息');
          EndBlock();
          EndMenu();


          // ########################## Menu_Categories  ##########################

          StartMenu();
          PrintMenuTitle('菜單管理', true);
          PrintMenuRow('categories.php?action=createcategory', '新建菜單');
          PrintMenuRow('categories.php?action=displaycategories', '菜單管理');
          EndBlock();
          PrintMenuTitle('所有菜單', false);
          PrintCategories(0, '');
          EndBlock();
          EndMenu();


          // ########################## Menu_Image Manager  ##########################

          StartMenu();
          PrintMenuTitle('圖片管理', true);
          PrintMenuRow('imagemanager.php?action=displayimages', '網站圖片');
          PrintMenuRow('imagemanager.php?action=displaysmilies', '表情圖標');
          EndBlock();
          EndMenu();


          // ########################## Menu_Users  ##########################

          StartMenu();
          PrintMenuTitle('用戶管理', true);

          PrintMenuRow('users.php?action=displayuserform', '添加用戶');
          PrintMenuRow('users.php?action=displayusers', '查找用戶');
          PrintMenuRow('usergroups.php', '群組設置');
          EndBlock();
          EndMenu();

  }


  // ########################## Menu_Modules  ##########################

  StartMenu();

  // weencompany modules
  $getsdmodules = $DB->query("SELECT moduleid, name FROM " . TABLE_PREFIX . "modules WHERE authorname = 'weencompany' AND moduleid != '1' ORDER BY moduleid ASC");
  $showmenu = true;
  if($DB->get_num_rows($getsdmodules) > 0)
  {
          while($sdmodule = $DB->fetch_array($getsdmodules))
          {
                  if(@in_array($sdmodule['moduleid'], $userinfo['moduleadminids']))
                  {
                          if($showmenu)
                          {
                                  PrintMenuTitle('系統模塊', false);
                                  $showmenu = false;
                          }

                          PrintMenuRow('mymodules.php?moduleid='.$sdmodule['moduleid'].'', $sdmodule['name']);
                  }
          }
  }

  EndBlock();


  // cloned modules
  $getclonedmodules = $DB->query("SELECT moduleid, name FROM " . TABLE_PREFIX . "modules WHERE authorname = 'weencompany_cloner' ORDER BY moduleid ASC");
  $showmenu = true;
  if($DB->get_num_rows($getclonedmodules) > 0)
  {
          while($clonedmodule = $DB->fetch_array($getclonedmodules))
          {
                  if(@in_array($clonedmodule['moduleid'], $userinfo['moduleadminids']))
                  {
                          if($showmenu)
                          {
                                  PrintMenuTitle('複製的模塊', false);
                                  $showmenu = false;
                          }

                          PrintMenuRow('mymodules.php?moduleid=' . $clonedmodule['moduleid'] . '', $clonedmodule['name']);
                  }
          }
  }
  EndBlock();


  // custom modules
  $getcustommodules = $DB->query("SELECT custommoduleid, name FROM " . TABLE_PREFIX . "custommodules ORDER BY custommoduleid ASC");
  $showmenu = true;
  if($DB->get_num_rows($getcustommodules) > 0)
  {
          while($custommodule = $DB->fetch_array($getcustommodules))
          {
                  if(@in_array($custommodule['custommoduleid'], $userinfo['custommoduleadminids']))
                  {
                          if($showmenu)
                          {
                                  PrintMenuTitle('自定義模塊', false);
                                  $showmenu = false;
                          }

                          PrintMenuRow('custommodules.php?custommoduleid='.$custommodule['custommoduleid'].'', $custommodule['name']);
                  }
          }
  }
  EndBlock();

  // downloaded modules
  $getdwmodules = $DB->query("SELECT moduleid, name FROM " . TABLE_PREFIX . "modules WHERE authorname != 'weencompany' AND authorname != 'weencompany_cloner' ORDER BY moduleid ASC");
  $showmenu = true;
  if($DB->get_num_rows($getdwmodules) > 0)
  {
          while($dwmodule = $DB->fetch_array($getdwmodules))
          {
                  if(@in_array($dwmodule['moduleid'], $userinfo['moduleadminids']))
                  {
                          if($showmenu)
                          {
                                  PrintMenuTitle('下載的模塊', false);
                                  $showmenu = false;
                          }

                          PrintMenuRow('mymodules.php?moduleid='.$dwmodule['moduleid'].'', $dwmodule['name']);
                  }
          }
  }

  EndBlock();
  EndMenu();

  ?>

  </td>
</tr>
</table>
<script type="text/javascript">
var divNames = new Array(<?php echo $divNameList; ?>);
window.onload = LoadMenu;
window.onbeforeunload = SaveMenu;
</script>
</body>
</html>
