<?php
/*
\* ===========================================
\* 项目: CTB文本论坛
\* 版本: 1.3
\* 团队: 实易数码
\* 小组: CTB文本论坛小组
\* 作者: cx
\* 功能: 后台系统-风格管理
\* ===========================================
\* Copyright (c) 2003
\* 团队主页: http://www.11cn.org
\* 技术论坛: http://ctb.11cn.org
\* 创建日期: 2003/4/27
\* 修改日期: ------
\* 修改说明: ------
\* 版权声明: 这个程序是免费软件.
\* ===========================================
*/

//=============================================================================
class Module extends CommonClass
//系统管理模块子类
{
    function execute() {
        $this->stylesFile = "../{$this->set[dataPath]}/styles/list.php";
        require_once $this->stylesFile;
        $this->styles = $styles;
        if (isset($this->input["styleId"]) and !$this->checkStyleId($this->input["styleId"])) {
            $this->adminShowMsg("main.php?mod=systemstyle&action=showAllStyles", "您指定的风格不存在");
        }
        switch($this->input['action']) {
            case "showAllStyles":
                $this->showAllStyles();
                break;
            case "setDefault":
                $this->setDefault();
                break;
            case "delStyle":
                $this->delStyle();
                break;
            case "showStyle":
                $this->showStyle();
                break;
            case "showStyleDetail":
                $this->showStyleDetail();
                break;
            case "newStyle":
                $this->showStyle();
                break;
            case "modifyStyle":
                $this->modifyStyle();
                break;
        }
        exit();
    } // end func

//1=============================================================================
//显示所有风格
function showAllStyles()
{
    foreach ($this->styles as $k => $v) {
        if ($k != "idDefault") {
            $styleId = $k;
            $styleTitle = $v["title"];
            $styleTplPath = $v["tplPath"];
            $styleImgPath = $v["imgPath"];
            if ($k == $this->styles["idDefault"]) {
                $styleSetDefault = "默认风格";
                $styleDelete = "保留";
            } else {
                $styleSetDefault = "<a href=\"./main.php?mod=systemstyle&action=setDefault&styleId=$styleId\">设为默认</a>";
                $styleDelete = "<a href=\"./main.php?mod=systemstyle&action=delStyle&styleId=$styleId\">删除</a>";
            }
            eval ("\$stylesList .= \"".$this->getTemplate("systemStyleList")."\";");
        }
    }
    $defaultTplPath = $this->styles[$this->styles["idDefault"]]["tplPath"];
    $defaultImgPath = $this->styles[$this->styles["idDefault"]]["imgPath"];
    eval("\$this->outPut(\"".$this->getTemplate("systemStyle")."\");");
} // end func

//2=============================================================================
//设置默认风格
function setDefault()
{
    if ($this->input["styleId"] == $this->styles["idDefault"]) {
        $this->adminShowMsg("main.php?mod=systemstyle&action=showAllStyles", "您已经成功设置了默认风格");
    } else {
        $newStyleFile = "<?php\n\n\$styles = array(\n";
        foreach ($this->styles as $k => $v) {
            if ($k == "idDefault") {
                $newStyleFile .= "    \"$k\" => \"{$this->input[styleId]}\",\n";
            } else {
                $newStyleFile .= "    $k => array(\n";
                foreach ($v as $kk => $vv) {
                    $newStyleFile .= "        \"$kk\" => \"$vv\",\n";
                }
                $newStyleFile .= "        ),\n";
            }
        }
        $newStyleFile .= "    );\n\n?>";
        $f = fopen($this->stylesFile, "w");
        flock($f, LOCK_EX);
        fputs($f, $newStyleFile);
        fclose($f);
        $this->adminShowMsg("main.php?mod=systemstyle&action=showAllStyles", "您已经成功设置了默认风格");
    }
} // end func

//3=============================================================================
//删除一个风格
function delStyle()
{
    if ($this->input["styleId"] == $this->styles["idDefault"]) {
        $this->adminShowMsg("main.php?mod=systemstyle&action=showAllStyles", "删除默认风格之前，请指定另一个风格作为替代");
    } else {
        @unlink("../{$this->set[dataPath]}/styles/{$this->input[styleId]}.css");
        @unlink("../{$this->set[dataPath]}/styles/{$this->input[styleId]}.php");
        unset($this->styles[$this->input["styleId"]]);
        $newStyleFile = "<?php\n\n\$styles = array(\n";
        foreach ($this->styles as $k => $v) {
            if ($k == "idDefault") {
                $newStyleFile .= "    \"$k\" => \"$v\",\n";
            } else {
                $newStyleFile .= "    $k => array(\n";
                foreach ($v as $kk => $vv) {
                    $newStyleFile .= "        \"$kk\" => \"$vv\",\n";
                }
                $newStyleFile .= "        ),\n";
            }
        }
        $newStyleFile .= "    );\n\n?>";
        $f = fopen($this->stylesFile, "w");
        flock($f, LOCK_EX);
        fputs($f, $newStyleFile);
        fclose($f);
        $this->adminShowMsg("main.php?mod=systemstyle&action=showAllStyles", "您已经成功删除了指定风格");
    }
} // end func

//4=============================================================================
//显示一个原有的或即将添加的风格
function showStyle()
{
    if ($this->input["action"] == "showStyle") {
        require_once "../{$this->set[dataPath]}/styles/{$this->input[styleId]}.php";
        $this->styleDetail = $style;
        $modifyType = "old";
    } else {
        if (!$this->input["newStyleId"] or !$this->input["newStyleTitle"] or !$this->input["newStyleTplPath"] or !$this->input["newStyleImgPath"]) {
            $this->adminShowMsg("main.php?mod=systemstyle&action=showAllStyles", "请填写新风格的所有项目");
        } elseif (!$this->input["newStyleId"] = $this->checkStyleId($this->input["newStyleId"], "new")) {
            $this->adminShowMsg("main.php?mod=systemstyle&action=showAllStyles", "新风格的编号无效或已经存在");
        } else {
            require_once "../{$this->set[dataPath]}/styles/{$this->styles[idDefault]}.php";
            $this->styleDetail = $style;
            $this->styleDetail["id"]      = $this->input["newStyleId"];
            $this->styleDetail["title"]   = $this->input["newStyleTitle"];
            $this->styleDetail["tplPath"] = $this->input["newStyleTplPath"];
            $this->styleDetail["imgPath"] = $this->input["newStyleImgPath"];
            $modifyType = "new";
        }
    }
    require_once "../{$this->set[dataPath]}/styles/vars.php";
    foreach ($styleVars as $k => $v) {
        if (isset($v["isCate"])) {
            $styleDetail .= $this->mkCateRow($k, $v);
        } else {
            $styleDetail .= $this->mkContentRow($k, $v);
        }
    }
    eval("\$this->outPut(\"".$this->getTemplate("systemStyleDetail")."\");");
} // end func

//5=============================================================================
//显示一个原有风格的详情，用于拷贝出来方便共享
function showStyleDetail()
{
    require_once "../{$this->set[dataPath]}/styles/vars.php";
    require_once "../{$this->set[dataPath]}/styles/{$this->input[styleId]}.php";
    unset($styleDetail);
    foreach ($styleVars as $k => $v) {
        if (isset($v["isCate"])) {
            $styleDetail .= "\n\n$k\n--------------------------------------------------------------------------------\n";
        } else {
            if ($v['type'] == 'textarea') {
               $styleDetail .= $k."：\n".str_replace("\r", "", $style[$v['name']])."\n";
            } else {
               $styleDetail .= $k.'：                '.$style[$v['name']]."\n";
            }
        }
    }
    eval("\$this->outPut(\"".$this->getTemplate("systemStyleDetail2")."\");");
} // end func

//6=============================================================================
//保存修改或添加一个新风格
function modifyStyle()
{
    if ($this->input["modifyType"] != "old" and $this->input["modifyType"] != "new") {
        $this->adminShowMsg("main.php?mod=systemstyle&action=showAllStyles", "无效的操作");
    } elseif (!$this->input["id"] or !$this->input["title"] or !$this->input["tplPath"] or !$this->input["imgPath"]) {
        $this->adminShowMsg("main.php?mod=systemstyle&action=showAllStyles", "请填写风格的所有必填项目");
    }

    // 更新 list.php
    if ($this->input["modifyType"] == "old") {
        $modifyStyleString = "您已经成功更新了指定风格";
        if (!$this->checkStyleId($this->input["id"])) {
            $this->adminShowMsg("main.php?mod=systemstyle&action=showAllStyles", "风格的编号不存在");
        }
    } elseif ($this->input["modifyType"] == "new") {
        $modifyStyleString = "您已经成功保存了新的风格";
        if (!$this->input["id"] = $this->checkStyleId($this->input["id"], "new")) {
            $this->adminShowMsg("main.php?mod=systemstyle&action=showAllStyles", "风格的编号无效");
        }
    }
    $this->styles[$this->input["id"]]["title"] = $this->input["title"];
    $this->styles[$this->input["id"]]["tplPath"] = $this->input["tplPath"];
    $this->styles[$this->input["id"]]["imgPath"] = $this->input["imgPath"];
    $newStyleFile = "<?php\n\n\$styles = array(\n";
    foreach ($this->styles as $k => $v) {
        if ($k == "idDefault") {
            $newStyleFile .= "    \"$k\" => \"$v\",\n";
        } else {
            $newStyleFile .= "    $k => array(\n";
            foreach ($v as $kk => $vv) {
                $newStyleFile .= "        \"$kk\" => \"$vv\",\n";
            }
            $newStyleFile .= "        ),\n";
        }
    }
    $newStyleFile .= "    );\n\n?>";
    $f = fopen($this->stylesFile, "w");
    flock($f, LOCK_EX);
    fputs($f, $newStyleFile);
    fclose($f);

    // 更新风格 PHP
    $newStylePhpFile = "<?php\n\n";
    foreach ($this->input as $k => $v) {
        if ($k != "mod" and $k != "action" and $k != "modifyType") {
            $newStylePhpFile .= "\$style[\"$k\"] = \"".addslashes($v)."\";\n";
        }
    }
    $newStylePhpFile .= "\n?>";
    $f = fopen("../{$this->set[dataPath]}/styles/{$this->input[id]}.php", "w");
    flock($f, LOCK_EX);
    fputs($f, $newStylePhpFile);
    fclose($f);

    // 更新风格 CSS
    $newStyleCssFile = "body {
    margin: {$this->input[topMargin]} {$this->input[rightMargin]} {$this->input[bottomMargin]} {$this->input[leftMargin]};
    background: {$this->input[pageBgColor]} url({$this->input[pageBgImg]});
    color: {$this->input[textColor]};
    font: {$this->input[textSize]} {$this->input[textFont]};
}
select {
    background: {$this->input[pageBgColor]};
    color: {$this->input[textColor]};
    font: {$this->input[textSize]} {$this->input[textFont]};
}

img              {border: 0;}
hr               {color: {$this->input[tInnerBgColor]}; height: 1px;}
td, p, div, span {color: {$this->input[textColor]}; font-size: {$this->input[textSize]};}
input, textarea  {color: {$this->input[textColor]}; font: {$this->input[textSize]} {$this->input[textFont]}; background: {$this->input[pageBgColor]};}
textarea         {width: 70%;}
.highLight       {color: {$this->input[textHighLight]};}

a:link    {color: {$this->input[linkColor]}; text-decoration: {$this->input[linkLine]};}
a:visited {color: {$this->input[visitedColor]}; text-decoration: {$this->input[visitedLine]};}
a:hover   {color: {$this->input[hoverColor]}; text-decoration: {$this->input[hoverLine]};}
a:active  {color: {$this->input[activeColor]}; text-decoration: {$this->input[activeLine]};}

.tOuter  {background: {$this->input[tOuterBgColor]}; width: {$this->input[tOuterWidth]};}
.tOuter2 {width: {$this->input[tOuterWidth]};}
.tOuter3 {background: {$this->input[tOuterBgColor]}; width: 60%;}
.tInner  {background: {$this->input[tInnerBgColor]}; width: 100%;}
.tHead   {background: {$this->input[tHeadBgColor]} url({$this->input[tHeadBgImg]});}
.tCate   {background: {$this->input[tCateBgColor]} url({$this->input[tCateBgImg]});}
.tBody1  {background: {$this->input[tBody1BgColor]};}
.tBody2  {background: {$this->input[tBody2BgColor]};}

.tHead td, .tHead a:link, .tHead a:visited {color: {$this->input[tHeadTextColor]}; text-decoration: none;}
.tHead a:hover, .tHead a:active            {color: {$this->input[tHeadTextColor]}; text-decoration: underline;}
.tCate td, .tCate a:link, .tCate a:visited {color: {$this->input[tCateTextColor]}; text-decoration: none;}
.tCate a:hover, .tCate a:active            {color: {$this->input[tCateTextColor]}; text-decoration: underline;}

{$this->input[otherCss]}";
    $f = fopen("../{$this->set[dataPath]}/styles/{$this->input[id]}.css", "w");
    flock($f, LOCK_EX);
    fputs($f, $newStyleCssFile);
    fclose($f);

    $this->adminShowMsg("main.php?mod=systemstyle&action=showAllStyles", $modifyStyleString);
} // end func

//7=============================================================================
//产生分类行
function mkCateRow($cateTitle, $cateArray)
{
    return "    <tr bgcolor=\"#dee7ef\">
        <td colspan=\"3\">$cateTitle</td>
    </tr>\n\n";
} // end func

//8=============================================================================
//产生内容行
function mkContentRow($contentTitle, $contentArray)
{
    if ($contentArray["type"] == "inputText") {
		if(eregi("#", $this->styleDetail[$contentArray[name]])){
            $getColorShow = "<td><input type=\"text\" name=\"$contentArray[name]\" value=\"{$this->styleDetail[$contentArray[name]]}\" size=\"40\" /></td><td bgcolor=\"{$this->styleDetail[$contentArray[name]]}\">color</td>";
		}
		else {
			$getColorShow = "<td colspan=2><input type=\"text\" name=\"$contentArray[name]\" value=\"{$this->styleDetail[$contentArray[name]]}\" size=\"40\" /></td>";
		}
        return "    <tr bgcolor=\"#ffffff\">
        <td>$contentTitle</td>$getColorShow
		</tr>\n\n";
    } elseif ($contentArray["type"] == "textarea") {
        $textareaContent = str_replace("\r", "", $this->styleDetail[$contentArray[name]]);
        return "    <tr bgcolor=\"#ffffff\">
        <td>$contentTitle</td>
        <td colspan=\"2\"><textarea name=\"$contentArray[name]\" rows=\"10\"  cols=\"70\">$textareaContent</textarea></td>
    </tr>\n\n";
    }
} // end func

//=============================================================================
//检查风格编号是否有效
function checkStyleId($id, $type="")
{
    if ($id == "idDefault") {
        return 0;
    } elseif ($type == "") {
        if (!isset($this->styles[$id])) {
            return 0;
        } else {
            return 1;
        }
    } elseif ($type == "new") {
        $id = intval($id);
        if ($id < 1) {
            return 0;
        } elseif (isset($this->styles[$id])) {
            return 0;
        } else {
            return $id;
        }
    }
} // end func
//=============================================================================

} //end class