<?php

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

$rootpath = "./../";

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

PrintHeader('前臺語言');


// ############################## UPDATE LANGUGAE ##############################

function TranslateLanguage()
{
  global $DB;

  $languagefile = $_POST['languagefile'];

  include('./languages/' . $languagefile);

  if($languagefile == 'default.php')
  {
    $DB->query("UPDATE " . TABLE_PREFIX . "phrases SET customphrase = ''");
  }
  else
  {
    foreach($sdphrases as $i => $x)
    {
      foreach($sdphrases[$i] as $k => $v)
      {
        $DB->query("UPDATE " . TABLE_PREFIX . "phrases SET customphrase = '" . addslashes($v) . "' WHERE varname = '$k' AND moduleid = '$i'");
      }
    }
  }

  $languagearray = array($languagefile, $version, $charset);
  $languageinfo  = implode('|', $languagearray);

  $DB->query("UPDATE " . TABLE_PREFIX . "mainsettings SET value = '$languageinfo' WHERE varname = 'language'");

  PrintRedirect('./languages.php', 1);

}


// ############################## UPDATE LANGUGAE ##############################

function UpdateLanguage()
{
  global $DB;

  $moduleid     = $_POST['moduleid'];
  $phraseid     = $_POST['phraseid'];
  $customphrase = $_POST['customphrase'];
  $font         = $_POST['font'];
  $color        = $_POST['color'];
  $size         = $_POST['size'];
  $bold         = $_POST['bold'];
  $italic       = $_POST['italic'];
  $underline    = $_POST['underline'];

  for($i = 0; $i < count($phraseid); $i++)
  {
    $DB->query("UPDATE " . TABLE_PREFIX . "phrases SET customphrase = '$customphrase[$i]',
                                                       font         = '$font[$i]',
                                                       color        = '$color[$i]',
                                                       size         = '$size[$i]'
                                                 WHERE phraseid     = '$phraseid[$i]'");
  }

  // checkboxes don't return unchecked values, thus we need to do this:

  // first we need to empty bold, italic, underline
  $DB->query("UPDATE " . TABLE_PREFIX . "phrases SET bold      = '0',
                                                     italic    = '0',
                                                     underline = '0'
                                               WHERE moduleid  = '$moduleid'");

  if($bold)
    foreach($bold as $key=>$value)
      $DB->query("UPDATE " . TABLE_PREFIX . "phrases SET bold = '$value' WHERE phraseid = '$key'");

  if($italic)
    foreach($italic as $key=>$value)
      $DB->query("UPDATE " . TABLE_PREFIX . "phrases SET italic = '$value' WHERE phraseid = '$key'");

  if($underline)
    foreach($underline as $key=>$value)
      $DB->query("UPDATE " . TABLE_PREFIX . "phrases SET underline = '$value' WHERE phraseid = '$key'");


  PrintRedirect('./languages.php', 1);

}


// ########################### PRINT INSTRUCTIONS ##############################

function PrintInstructions()
{
  PrintSection('關於前臺語言');
  echo '<table width="100%" border="0" cellpadding="5" cellspacing="0">
        <tr>
          <td class="tdrow2">
          weenCompany聞名企業網站系統允許您自定義網站前臺語言, 包括: 編輯, 刪除, 導入導出, 也可以調整前臺語言的字體, 顏色, 大小等. 顏色參考: <a href="./colors.php" target="_blank"><b>web顏色列表</b></a>.
		  <br>1. "字體"可參考Dreamweaver等網頁製作時添加的字體, 如: 仿宋_GB2312, 表示詞句顯示為仿宋體;
		  <br>2. "顏色"可參考 "web顏色列表", 如: #996600, 也可直接填寫red, blue, green等這些代表顏色的單詞;
		  <br>3. "大小"需填寫代表字體大小的px值, 如: 12, 它表示字體為12px.

          </td>
        </tr>
        </table>';
  EndSection();
}


// ############################## EDIT LANGUAGE ################################

function EditLanguage($moduleid)
{
  global $DB;

  PrintInstructions();

  $module      = $DB->query_first("SELECT name FROM " . TABLE_PREFIX . "modules WHERE moduleid = '$moduleid'");
  $getlanguage = $DB->query("SELECT * FROM " . TABLE_PREFIX . "phrases WHERE moduleid = '$moduleid'");

  // start form
  echo '<form action="./languages.php" method="post">
        <input type="hidden" name="action" value="updatelanguage" />
        <input type="hidden" name="moduleid" value="' . $moduleid . '" />';

  PrintSection(iif($moduleid == 1, '系統程序', $module['name']).'_前臺語言');
  echo '<table width="100%" border="0" cellpadding="5" cellspacing="0">
        <tr>
          <td class="tdrow1">默認詞句</td>
          <td class="tdrow1">自定義詞句</td>
          <td class="tdrow1">字體</td>
          <td class="tdrow1">顏色</td>
          <td class="tdrow1" width="50">大小</td>
          <td class="tdrow1" width="50">粗體</td>
          <td class="tdrow1" width="50">斜體</td>
          <td class="tdrow1" width="50">下劃線</td>
        </tr>';

  while($language = $DB->fetch_array($getlanguage))
  {
    echo '<input type="hidden" name="phraseid[]" value="' . $language['phraseid'] . '" />
          <tr>
            <td class="tdrow2">' . $language['defaultphrase'] . '&nbsp;</td>
            <td class="tdrow3"><input type="text"     name="customphrase[]"    value="' . CleanFormValue($language['customphrase'])    . '" size="25" />&nbsp;</td>
            <td class="tdrow2"><input type="text"     name="font[]"  value="' . CleanFormValue($language['font'])  . '" size="15" />&nbsp;</td>
            <td class="tdrow3"><input type="text"     name="color[]" value="' . CleanFormValue($language['color']) . '" size="8" />&nbsp;</td>
            <td class="tdrow2"><input type="text"     name="size[]"  value="' . CleanFormValue($language['size'])  . '" size="3" />&nbsp;</td>
            <td class="tdrow3"><input type="checkbox" name="bold['      . $language['phraseid'] . ']" value="1" ' . iif($language['bold'], 'checked', '') . ' />&nbsp;</td>
            <td class="tdrow2"><input type="checkbox" name="italic['    . $language['phraseid'] . ']" value="1" ' . iif($language['italic'], 'checked', '') . '/>&nbsp;</td>
            <td class="tdrow3"><input type="checkbox" name="underline[' . $language['phraseid'] . ']" value="1" ' . iif($language['underline'], 'checked', '') . '/>&nbsp;</td>
          </tr>';
  }

  echo '<tr>
          <td class="tdrow1" bgcolor="#FCFCFC" colspan="8" align="center"><input type="submit" value=" 保存更新 " /></td>
        </tr>
        </table>
        </form>';
  EndSection();

}



function DisplayPhraseRow($modules, $modulestype)
{
  global $DB;

  echo '<tr>
          <td class="tdrow1" width="250">' . $modulestype . '</td>
          <td class="tdrow1" width="100">默認詞句</td>
          <td class="tdrow1" width="225">自定義詞句</td>
          <td class="tdrow1">編輯語言</td>
        </tr>';

  while($module = $DB->fetch_array($modules))
  {
    $phraserows = $DB->query_first("SELECT COUNT(*) AS total FROM " . TABLE_PREFIX . "phrases
                                    WHERE moduleid = " . $module['moduleid']);

    $customrows = $DB->query_first("SELECT COUNT(*) AS total FROM " . TABLE_PREFIX . "phrases
                                    WHERE customphrase != '' AND moduleid = " . $module['moduleid']);

    echo '<tr>
            <td class="tdrow2">' . iif($module['moduleid'] == 1, '系統程序', $module['name']) . '</td>
            <td class="tdrow3">' . $phraserows['total'] . '</td>
            <td class="tdrow2"><span ' . iif($customrows['total'], 'class="ohred"') . '>'. $customrows['total'] . '</span></td>
            <td class="tdrow3">';

    if($phraserows['total'])
    {
      echo '  <form action="./languages.php" method="post">
              <input type="hidden" name="moduleid" value="' . $module['moduleid'] . '" />
              <input type="hidden" name="action"   value="editlanguage" />
              <input type="submit" style="width: 175px;" value="' . iif($module['moduleid'] == 1, '系統程序', $module['name']) . '" />
              </form>';
    }
    else
      echo '&nbsp;';

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


// ############################## PRINT Default ################################

function PrintDefault()
{
  global $DB, $userinfo;

  $getlanguageinfo = $DB->query_first("SELECT value FROM " . TABLE_PREFIX . "mainsettings WHERE varname = 'language'");
  $languageinfo    = explode('|', $getlanguageinfo['value']);

  PrintInstructions();

  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%">
		    1. 選擇導入"default", 即恢復為系統默認的前臺語言, 並刪除自定義的前臺語言.<br/>
            2. 導入語言需先導出語言或將下載編輯後的前臺語言文件上傳到目錄: '.ADMIN_DIR.'/languages/ 下.<br /><br />

            <td class="tdrow3" valign="top">
            <form action="./languages.php" method="post">
              <input type="hidden" name="action" value="translatelanguage" />
              <select name="languagefile">';

  // find language files
  $d = dir('./languages/');

  while($entry = $d->read())
  {
    if($entry != "." && $entry != ".." && filesize('languages/'.$entry))
    {
      echo '<option value="' . $entry . '" ' . iif($entry == $languageinfo[0], "selected", "") . '>' . substr($entry, 0, -4)  . ' ' . iif($entry == $languageinfo[0], $languageinfo[1], "") . '</option>';
    }
  }

  echo '      </select><br/><br/>
              <input type="submit" value=" 導入語言 " />
            </form>
          </td>
        </tr>
        </table>';
  EndSection();

  PrintSection('導出自定義語言');
  echo '<form action="./exportlanguage.php" method="post">
        <table width="100%" border="0" cellpadding="5" cellspacing="0">
        <tr>
          <td class="tdrow1" colspan="2">語言名稱</td>
        </tr>
        <tr>
          <td class="tdrow2" width="70%">
		  為要導出的自定義前臺語言設置一個名稱(可選):<br/><br/>
		    1. 用戶自定義前臺語言後, 可導出語言保存到語言文件, 供重裝系統後導入使用.<br/>
			2. 導出語言時, 自定義語言已自動保存到: '.ADMIN_DIR.'/languages/ 目錄下的custom_lang.php文件中, 同時提示下載.<br/>
		  </td>
          <td class="tdrow3"><input type="text" name="language" value="' . substr($languageinfo[0], 0, -4) . '" /></td>
        </tr>
        <tr>
          <td class="tdrow1" colspan="2">版本號</td>
        </tr>
        <tr>
          <td class="tdrow2" width="70%">為要導出的自定義前臺語言設置一個版本號(可選): </td>
          <td class="tdrow3"><input type="text" name="version" value="' . $languageinfo[1] . '" /></td>
        </tr>
        <tr>
          <td class="tdrow1" colspan="2">作者</td>
        </tr>
        <tr>
          <td class="tdrow2" width="70%">輸入自定義前臺語言的作者(可選):</td>
          <td class="tdrow3"><input type="text" name="author" value="' . $userinfo['username'] . '" /></td>
        </tr>
        <tr>
          <td class="tdrow1" colspan="2">包含默認語言</td>
        </tr>
        <tr>
          <td class="tdrow2" width="70%">選擇包含默認語言, 當導出自定義前臺語言時, 沒有自定義的地方將用默認語言填充. <br />不選擇此項, 則僅導出用戶自定義語言.</td></td>
          <td class="tdrow3"><input type="checkbox" name="includedefault" value="1" checked/> 包含默認語言</td>
        </tr>
        <tr>
          <td class="tdrow1" bgcolor="#FCFCFC" colspan="2" align="center"><input type="submit" value="導出自定義語言" /></td>
        </tr>
        </table>
        </form>';
  EndSection();

  PrintSection('自定義前臺語言');
  echo '<table width="100%" border="0" cellpadding="5" cellspacing="0">';

  // MAIN MODULE PHRASES
  $modules = $DB->query("SELECT moduleid, name FROM " . TABLE_PREFIX . "modules WHERE authorname = 'weencompany' ORDER BY moduleid");
  DisplayPhraseRow($modules, '系統模塊');

  // CLONED MODULE PHRASES
  $modules = $DB->query("SELECT moduleid, name FROM " . TABLE_PREFIX . "modules WHERE moduleid != 1 AND authorname = 'weencompany_cloner' ORDER BY moduleid");
  if($DB->get_num_rows($modules))
  {
    DisplayPhraseRow($modules, '複製的模塊');
  }

  // DOWNLOADED MODULE PHRASES
  $modules = $DB->query("SELECT moduleid, name FROM " . TABLE_PREFIX . "modules WHERE moduleid != 1 AND authorname != 'weencompany' AND authorname != 'weencompany_cloner' ORDER BY moduleid");
  if($DB->get_num_rows($modules))
  {
    DisplayPhraseRow($modules, '下載的模塊');
  }

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


// ############################## SELECT FUNCTION ##############################

switch($action)
{
  case 'editlanguage':
    EditLanguage($_POST['moduleid']);
  break;

  case 'updatelanguage':
    UpdateLanguage();
  break;

  case 'translatelanguage':
    TranslateLanguage();
  break;

  default:
    PrintDefault();
}


PrintFooter();

?>
