<?php
require_once(dirname(__FILE__)."/admin_inc.php");
require_once(dirname(__FILE__)."/../cron/function.cron.php");
/*******************************************************************************************
' Software name: DJCMS Content Management System
' Version:3.0
' Web: http://www.djcms.net
' Author: 心情(qq:1130731)
' Copyright (C) 2009-2010 DJCMS官方 版权所有
' 法律申明：DJCMS程序所有代码100%原创、未引入任何网上代码,对一切抄袭行为、坚决严肃追究法律责任
'*******************************************************************************************/
checkPower();
$cronid = getForm("cronid","both");$op = getForm("op","both");

if (getForm("do","post")=="submit"){
	$name=getForm("name","post");
	$filename=getForm("filename","post");
	$paramets=getForm("paramets","post");
	$months=getForm("months","post");
	$days=getForm("days","post");
	$hours=getForm("hours","post");
	$minutes=getForm("minutes","post");
	$available=getForm("available","post");
	$des=getForm("des","post");
	if (!isExistFile(DJ_ROOT."/cron/execute/".$filename)){
		alertMsg("指定的脚本文件不存在","javascript:history.go(-1);");die("");
	}
	if ($months=="" || $days=="" || $hours=="" || $minutes==""){
		alertMsg("设定的脚本执行周期不正确","javascript:history.go(-1);");die("");
	}
	if(empty($cronid)) {
		$feedarr = array(
		    'name' => $name,
		    'filename' => $filename,
		    'available' => $available,
		    'months' => dimplode($months),
		    'days' => dimplode($days),
		    'hours' => dimplode($hours),
		    'minutes' => dimplode($minutes),
		    'type' => 'user',
		    'nextrun' => time(),
		    'paramets' => $paramets,
		    'des' => $des,
	    );
		$cronid = DB::insert('crons', $feedarr, 1);//返回
	}else{
		$feedarr = array(
		    'name' => $name,
		    'filename' => $filename,
		    'available' => $available,
		    'months' => dimplode($months),
		    'days' => dimplode($days),
		    'hours' => dimplode($hours),
		    'minutes' => dimplode($minutes),
		    'nextrun' => time(),
		    'paramets' => $paramets,
		    'des' => $des,
	    );
		DB::update('crons', $feedarr, array('cronid' => $cronid));
	}
	computenext($cronid);nextConfig();
	alertMsg("进行的操作完成了","?ac=cron");die("");
}

if ($op=="delete") {
	DB::delete('crons', array('cronid' => $cronid,'type' => 'user'));
	nextConfig();
	alertMsg("删除成功","?ac=cron");die();
} else if ($op == "run"){
	runcron($cronid);
	alertMsg("任务已开始执行","?ac=cron");die();
}

$availables=array("","","");

Headera();
echoHtml();
viewFoot();

function echoHtml(){
	global $availables,$cronid,$op;
    $ipttype=ifthen($availables[1]<>"","radio","checkbox");
?>
<div class="container" id="cpcontainer">
<table class="tb">
	<thead>
		<tr class="thead"><th>&nbsp;<font color="#000000">操作选项： </font><a href="?ac=cron">浏览</a> | <a href="?ac=cron&op=add">添加</a></th></tr>
	</thead>
</table>
<?php
if ($op=="add"){
?>
<form method="post" action="?ac=cron&cronid=<?=$cronid?>">
	<table class="tb2">
	<tr>
		<td class="label">任务名</td>
		<td><input size="25" name="name" value="" type="text">
		</td></tr>
	<tr>
		<td class="label">有效性</td>
		<td><input type="radio" class="radio" name="available" value="2" onClick="change2CheckBox(this.form)">循环执行&nbsp;&nbsp;<input type="radio" class="radio" name="available" value="1" onClick="change2Radio(this.form)">一次&nbsp;&nbsp;<input type="radio" class="radio" name="available" value="0" checked>无效</td></tr>
	<tr>
		<td class="label">任务脚本</td>
		<td><input size="25" name="filename" value="" type="text">
			<br>不能包含路径，程序脚本必须存放于 ./cron/execute/ 目录中
		</td></tr>
	<tr>
		<td class="label">传递参数</td>
		<td><input size="25" name="paramets" value="" type="text" maxlength="255">
			<br>可以留空，格式:<font color="blue">action=daysview&days=1</font>
		</td></tr>
	<tr>
		<td class="label">在每年的(月)</td>
		<td id="months"><?php
				$t=",1,2,3,4,5,6,7,8,9,10,11,12,";
				if ($t<>",,"){
					for ($i = 1;$i <= 12;$i++) {
						if (strpos(" ".$t,",".$i.",")>0) $a[$i]="checked";
					}
				}
				for ($i = 1;$i <= 9;$i++) {
					echo "<input type=\"".$ipttype."\" class=\"".$ipttype."\" name=\"months[]\" value=\"".$i."\" ".$a[$i]."/>0".$i;
				}
				for ($i = 10;$i <= 12;$i++) {
					echo "<input type=\"".$ipttype."\" class=\"".$ipttype."\" name=\"months[]\" value=\"".$i."\" ".$a[$i]."/>".$i;
               }
			?><br /><span class="red">*</span> 最少选取一个月份</td></tr>
	<tr>
		<td class="label">在每月的(日)</td>
		<td id="days"><?
		        $t=",1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,";
				if ($t<>",,"){
					for ($i = 1;$i <= 31;$i++) {
						if (strpos(" ".$t,",".$i.",")>0) $a[$i]="checked";
					}
				}
				for ($i = 1;$i <= 9;$i++) {
					echo "<input type=\"".$ipttype."\" class=\"".$ipttype."\" name=\"days[]\" value=\"".$i."\" ".$a[$i]."/>0".$i;
				}
				for ($i = 10;$i <= 31;$i++) {
					echo "<input type=\"".$ipttype."\" class=\"".$ipttype."\" name=\"days[]\" value=\"".$i."\" ".$a[$i]."/>".$i;
					if ($i==12 || $i==24) echo "<br />";
                }
			?><br /><span class="red">*</span> 最少选取其中一天</td></tr>
	<tr>
		<td class="label">在每天的(时)</td>
		<td id="hours"><?php
				for ($i = 0;$i <= 9;$i++) {
					echo "<input type=\"".$ipttype."\" class=\"".$ipttype."\" name=\"hours[]\" value=\"".$i."\"/>0".$i;
				}
				for ($i = 10;$i <= 23;$i++) {
					echo "<input type=\"".$ipttype."\" class=\"".$ipttype."\" name=\"hours[]\" value=\"".$i."\"/>".$i;
					if ($i==11) echo "<br />";
                }
	?><br /><span class="red">*</span> 最少选取其中一个时段</td></tr>
	<tr>
		<td class="label">在每时的(分)</td>
		<td id="minutes"><?php
				for ($i = 0;$i <= 9;$i++) {
					echo "<input type=\"".$ipttype."\" class=\"".$ipttype."\" name=\"minutes[]\" value=\"".$i."\"/>0".$i;
				}
				for ($i = 10;$i <= 59;$i++) {
					echo "<input type=\"".$ipttype."\" class=\"".$ipttype."\" name=\"minutes[]\" value=\"".$i."\"/>".$i;
					if ($i==11 || $i==23 || $i==35 || $i==47) echo "<br />";
                }
			?><br /><span class="red">*</span> 最少选取其中一个分钟段</td></tr>
	<tr>
		<td class="label">任务描述</td>
		<td><textarea name="des" rows="4" cols="80"></textarea></td></tr>
	</table>
	</div>
	<div class="footactions">
		<input type="hidden" name="do" value="submit" />
		<input type="submit" name="cronsubmit" value="提交" class="btn">
	</div>
</form>
<?php
}elseif ($op=="edit"){
	$thevalue = DB::fetch_first("SELECT * FROM ".DB::table('crons')." WHERE cronid='$cronid'");
	if (!is_array($thevalue)) { die("没找到记录");}
?>
<form method="post" action="?ac=cron&cronid=<?=$cronid?>">
	<table class="tb2">
	<tr>
		<td class="label">任务名</td>
		<td><input size="25" name="name" value="<?=$thevalue["name"]?>" type="text">
		</td></tr>
	<tr>
		<td class="label">有效性</td>
		<td><input type="radio" class="radio" name="available" value="2" onClick="change2CheckBox(this.form)" <?php if ($thevalue["available"]==2) echo " checked";?>>循环执行&nbsp;&nbsp;<input type="radio" class="radio" name="available" value="1" onClick="change2Radio(this.form)"<?php if ($thevalue["available"]==1) echo " checked";?>>一次&nbsp;&nbsp;<input type="radio" class="radio" name="available" value="0"<?php if ($thevalue["available"]==0) echo " checked";?>>无效</td></tr>
	<tr>
		<td class="label">任务脚本</td>
		<td><input size="25" name="filename" value="<?=$thevalue["filename"]?>" type="text">
			<br>不能包含路径，程序脚本必须存放于 ./cron/execute/ 目录中
		</td></tr>
	<tr>
		<td class="label">传递参数</td>
		<td><input size="25" name="paramets" value="<?=$thevalue["paramets"]?>" type="text" maxlength="255">
			<br>可以留空，格式:<font color="blue">action=daysview&days=1</font>
		</td></tr>
	<tr>
		<td class="label">在每年的(月)</td>
		<td id="months"><?php
				$tl=",".replaceStr($thevalue["months"]," ","").",";
				if ($tl<>",,"){
					for ($i = 1;$i <= 12;$i++) {
						if (isExistStr(" ".$tl,",".$i.",")>0) $a[$i]="checked";
					}
				}
				for ($i = 1;$i <= 9;$i++) {
					echo "<input type=\"".$ipttype."\" class=\"".$ipttype."\" name=\"months[]\" value=\"".$i."\" ".$a[$i]."/>0".$i;
				}
				for ($i = 10;$i <= 12;$i++) {
					echo "<input type=\"".$ipttype."\" class=\"".$ipttype."\" name=\"months[]\" value=\"".$i."\" ".$a[$i]."/>".$i;
               }
			?><br /><span class="red">*</span> 最少选取一个月份</td></tr>
	<tr>
		<td class="label">在每月的(日)</td>
		<td id="days"><?
		        $ta=",".replaceStr($thevalue["days"]," ","").",";
				if ($ta<>",,"){
					for ($i = 1;$i <= 31;$i++) {
						if (isExistStr(" ".$ta,",".$i.",")>0) $aa[$i]="checked";
					}
				}
				for ($i = 1;$i <= 9;$i++) {
					echo "<input type=\"".$ipttype."\" class=\"".$ipttype."\" name=\"days[]\" value=\"".$i."\" ".$aa[$i]."/>0".$i;
				}
				for ($i = 10;$i <= 31;$i++) {
					echo "<input type=\"".$ipttype."\" class=\"".$ipttype."\" name=\"days[]\" value=\"".$i."\" ".$aa[$i]."/>".$i;
					if ($i==12 || $i==24) echo "<br />";
                }
			?><br /><span class="red">*</span> 最少选取其中一天</td></tr>
	<tr>
		<td class="label">在每天的(时)</td>
		<td id="hours"><?php
		        $tb=",".replaceStr($thevalue["hours"]," ","").",";
				if ($tb<>",,"){
					for ($i = 0;$i <= 23;$i++) {
						if (isExistStr(" ".$tb,",".$i.",")>0) $ab[$i]="checked";
					}
				}
				for ($i = 0;$i <= 9;$i++) {
					echo "<input type=\"".$ipttype."\" class=\"".$ipttype."\" name=\"hours[]\" value=\"".$i."\" ".$ab[$i]."/>0".$i;
				}
				for ($i = 10;$i <= 23;$i++) {
					echo "<input type=\"".$ipttype."\" class=\"".$ipttype."\" name=\"hours[]\" value=\"".$i."\" ".$ab[$i]."/>".$i;
					if ($i==11) echo "<br />";
                }
	?><br /><span class="red">*</span> 最少选取其中一个时段</td></tr>
	<tr>
		<td class="label">在每时的(分)</td>
		<td id="minutes"><?php
		        $tc=",".replaceStr($thevalue["minutes"]," ","").",";
				if ($tc<>",,"){
					for ($i = 0;$i <= 59;$i++) {
						if (isExistStr(" ".$tc,",".$i.",")>0) $ac[$i]="checked";
					}
				}
				for ($i = 0;$i <= 9;$i++) {
					echo "<input type=\"".$ipttype."\" class=\"".$ipttype."\" name=\"minutes[]\" value=\"".$i."\" ".$ac[$i]."/>0".$i;
				}
				for ($i = 10;$i <= 59;$i++) {
					echo "<input type=\"".$ipttype."\" class=\"".$ipttype."\" name=\"minutes[]\" value=\"".$i."\" ".$ac[$i]."/>".$i;
					if ($i==11 || $i==23 || $i==35 || $i==47) echo "<br />";
                }
			?><br /><span class="red">*</span> 最少选取其中一个分钟段</td></tr>
	<tr>
		<td class="label">任务描述</td>
		<td><textarea name="des" rows="4" cols="80"><?=$thevalue["des"]?></textarea></td></tr>
	</table>
	</div>
	<div class="footactions">
		<input type="hidden" name="do" value="submit" />
		<input type="submit" name="cronsubmit" value="提交" class="btn">
	</div>
</form>
<?php
}else{
?>
	<table class="tb2">
	<tr class="thead">
		<th width="150">任务名</th>
		<th>描述</th>
		<th width="150">上次执行/下次执行</th>
		<th width="120">操作</th>
	</tr>
<?php
	$t="";
	$query = DB::query("SELECT * FROM ".DB::table('crons')." ORDER BY nextrun ASC,type DESC");
	while($cron = DB::fetch($query)) {
		$cron['lastrun'] = $cron['lastrun'] ? date("Y-m-d H:i:s", $cron['lastrun']) : 'N/A';
		$cron['nextrun'] = $cron['available'] && $cron['nextrun'] ? date("Y-m-d H:i:s", $cron['nextrun']) : 'N/A';
	?>
		<tr>
		<td><b><?=$cron['name']?></b><br /><?=$cron['filename']?></td>
		<td><?=$cron['des']?></td>
		<td><?=$cron['lastrun']?><br /><?=$cron['nextrun']?></td>
		<td><a href="?ac=cron&op=edit&cronid=<?=$cron[cronid]?>">编辑</a>
        <?php if ($cron['available']>0){?>&nbsp;|&nbsp;<a href="?op=run&cronid=<?=$cron[cronid]?>">执行</a><?php } ?>
        <?php if ($cron['type']!='system'){?>&nbsp;|&nbsp;<a href="?ac=cron&op=delete&cronid=<?=$cron[cronid]?>">删除</a><?php } ?>
	</td>
	</tr>
	<?php
    }
?>
	</table>
	</div>
</div>
<?php
    }
}

function Headera(){
	global $menuList;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>系统计划任务</title>
<link href="imgs/admin.css" rel="stylesheet" type="text/css"/>
<script src="../js/common.js" type="text/javascript"></script>
<script src="imgs/main.js" type="text/javascript"></script>
<script>if(parent.$('admincpnav')) parent.$('admincpnav').innerHTML='后台首页&nbsp;&raquo;&nbsp;<?=$menuList[4][0]?>&nbsp;&raquo;&nbsp;DjCms3.0 系统计划任务';</script>
<style type="text/css">
input{height:12px;}
.txt{width:250px;}
.tb2 td{padding:2px 5px 2px 5px;height:25px}
.tb2 .thc{text-align:center;line-height:30px; background-color: #F5F7F8;font-size:18px;font-weight:bold;color:#000}
.tb2 .thr{text-align:center;height:20px}
.label{padding:0;width:150px;text-align:right;border-right:1px solid #DEEFFA;}
.btn{height:22px}
.red{color:red}
.blue{color:blue}
</style>
<script type="text/javascript">
function change2CheckBox(form)
{
	var ids="months,days,hours,minutes".split(",");
	for(var i=0;i<ids.length;i++)
	{
		var tmp=$(ids[i]).innerHTML;
		if(/radio/i.test(tmp))
		{
			$(ids[i]).innerHTML=tmp.replace(/radio/ig,"checkbox");
		}
	}
}

function change2Radio(form)
{
	var ids="months,days,hours,minutes".split(",");
	for(var i=0;i<ids.length;i++)
	{
		var tmp=$(ids[i]).innerHTML;
		if(/checkbox/i.test(tmp))
		{
			$(ids[i]).innerHTML=tmp.replace(/checkbox/ig,"radio");
		}
	}
}
</script>
</head>
<body bgcolor="#F7FBFF">
<div class="container" id="cpcontainer">
<?php
}

function Footer(){
	global $t1;
?></div>
<div align=center><?=getRunTime($t1)?></div>
<div align=center>Copyright 2005-2009 All rights reserved. <a target="_blank" href="http://www.djcms.net/">djCms3.0</a></div>
</body>
</html>
<?php
}
?>