<?php
/**
 * 文檔隨機模板
 *
 * @version        $Id: article_template_rand.php 1 14:31 2010年7月12日Z tianya $
 * @package        DedeCMS.Administrator
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license        http://help.dedecms.com/usersguide/license.html
 * @link           http://www.dedecms.com
 */
require_once(dirname(__FILE__).'/config.php');
require_once(DEDEINC.'/oxwindow.class.php');
CheckPurview('sys_StringMix');
if(empty($dopost)) $dopost = '';
$templates = empty($templates) ? '' : stripslashes($templates);
$m_file = DEDEDATA.'/template.rand.php';

//----------------------action
$okmsg = '';
//保存配置
if($dopost=='save')
{
    $fp = fopen($m_file,'w');
    flock($fp,3);
    fwrite($fp,$templates);
    fclose($fp);
    $okmsg = '成功保存配置信息 AT:('.MyDate('H:i:s', time()).')';
}
//對舊文檔進行隨機模板處理
else if($dopost=='makeold')
{
    set_time_limit(3600);
    if(!file_exists($m_file))
    {
        AjaxHead();
        echo "配置文件不存在！";
        exit();
    }
    require_once($m_file);
    if($cfg_tamplate_rand==0)
    {
        AjaxHead();
        echo "系統沒開啟允許隨機模板的選項！";
        exit();
    }
    $totalTmp = count($cfg_tamplate_arr) - 1;
    if( $totalTmp < 1 )
    {
        AjaxHead();
        echo "隨機模板的數量必須為2個或以上！";
        exit();
    }
    for($i=0; $i < 10; $i++)
    {
        $temp = $cfg_tamplate_arr[mt_rand(0, $totalTmp)];
        $dsql->ExecuteNoneQuery(" Update `#@__addonarticle` set templet='$temp' where RIGHT(aid, 1)='$i' ");
    }
    AjaxHead();
    echo "全部隨機操作成功！";
    exit();
}
//清除全部的指定模板
else if($dopost=='clearold')
{
    $dsql->ExecuteNoneQuery(" Update `#@__addonarticle` set templet='' ");
    $dsql->ExecuteNoneQuery(" OPTIMIZE TABLE `#@__addonarticle` ");
    AjaxHead();
    echo "全部清除操作成功！";
    exit();
}

//-------------------------read
//讀出
if(empty($templates) && filesize($m_file)>0)
{
    $fp = fopen($m_file,'r');
    $templates = fread($fp,filesize($m_file));
    fclose($fp);
}
$wintitle = "隨機模板防採集設置";
$wecome_info = "隨機模板防採集設置";

$msg = "
<link href='images/base.css' rel='stylesheet' type='text/css' />
<script language='javascript' src='js/main.js'></script>
<script language='javascript' src='../include/dedeajax2.js'></script>
<script language='javascript'>
function DoRand(jobname)
{
    ChangeFullDiv('show');
    \$DE('loading').style.display = 'block';
    var myajax = new DedeAjax(\$DE('tmpct'));
    myajax.SendGet2('article_template_rand.php?dopost='+jobname);
    \$DE('loading').style.display = 'none';
    ChangeFullDiv('hide');
}
</script>
<div id='loading' style='z-index:3000;top:160;left:300;position:absolute;display:none;'>
    <img src='images/loadinglit.gif' /> 請稍後，正在操作中...
</div>
<table width='98%' align='center'>
<tr>
    <td height='28'>
    如果你想對舊的文章應用隨機模板設置，請點擊此對舊文章進行處理(必須設置好模板項)！
    &nbsp; <a href='#' onclick='DoRand(\"makeold\")'>[<u>設置全部</u>]</a>
    &nbsp; <a href='#' onclick='DoRand(\"clearold\")'>[<u>取消全部</u>]</a>
    &nbsp; <span id='tmpct' style='color:red;font-weight:bold'>$okmsg</span>
    </td>
</tr>
<tr>
    <td bgcolor='#F9FCEF'><b>請按說明修改設置：</b></td>
</tr>
<tr>
    <td><textarea name='templates' id='templates' style='width:100%;height:250px'>$templates</textarea></td>
</tr>
</table>";

$win = new OxWindow();
$win->Init('article_template_rand.php','js/blank.js','POST');
$win->AddHidden('dopost','save');
$win->AddTitle("本設置僅適用於系統默認的文章模型，設置後發佈文章時會自動按指定的模板隨機獲取一個，如果不想使用此功能，把它設置為空即可！");
$win->AddMsgItem($msg);
$winform = $win->GetWindow('ok');
$win->Display();