<?php
/*******************************************************************
 * 版权协议：2009-2019
 * 版权所有: PHP-北京金南曦网络技术有限公司旗下PHPWMS，PHP商城系统技术支持中心http://www.php188.net，并保留所有权利。 
* PHP商城系统网址: http://www.php188.com
 * PHPWMS系统网址：http://www.phpwms.com
 * 销售电话：010-87875088
 * 销售电话：010-87875188 
*------------------------------------------------------------------
 * 这不是一个完全免费软件！您只能在不用于商业目的的前提下对程序代码进
 * 行修改和使用；不允许对程序代码以任何形式任何目的的再发布。
 *------------------------------------------------------------------
 * 文件名称：admin_link.php
 * 摘　　要：
 * 适用范围：PHPWMS 1.0
 * 版　　本：1.0
 *------------------------------------------------------------------
 * Script Written By phpwms项目组
 * 最后更新： 2009-09-24 phpwms 由PHP金南曦网络技术有限公司整理发布
 *******************************************************************/
if(!defined('IN_SYS'))exit('access die');
if (!getAdminPri('8103'))exit(NO_AUTH_LOGIN);
function delete_link_cache(){
	php188StaticCache::getInstance()->remove('phpwms_link');
}
if(is_post()){
	$task = $_POST['task'];
	switch ($task){
			case 'add' :
			case 'edit':
			$_SESSION['post_link_file'] = $_POST;
			$add = $task=='add'?TRUE:FALSE;
			$msg = "操作成功！";
            $data['add_time'] = date("Y-m-d H:i:s");
            $mail = $data['email'];
            if(!empty($mail) && !is_email($mail))exit($sp->bgmsg('邮箱格式错误'));
			if (!$add) {
				$id = to_int($data['id']);
				unset($data['id']);
				if(empty($id))die($sp -> bgmsg('参数错误！',$_SERVER["HTTP_REFERER"]));
				$c = $db->fetch("SELECT * FROM ##__link WHERE `id` = '$id';",TRUE);
				if(empty($c))exit('无可用数据');
			}
			if(empty($data['name'])) die($sp -> bgmsg('网站名称不能为空！',$_SERVER["HTTP_REFERER"]));
			if(empty($data['url']))die($sp -> bgmsg('网站网址不能为空！',$_SERVER["HTTP_REFERER"]));
			require SYS_ROOT.'/include/classes/upload.class.php';
			if($_FILES['upfile']['size']>0){
				$upload = new Upload();
				$upload->upload_maxsize = 1024*1024;
				$upload->upload_type = array('jpg','png','gif','jpeg');
				$upload->upload_save_path = 'files/link';
				$up = $upload->up('upfile');
				if(!$up){
					$msg = $upload->getMsg();
					exit($sp->bgmsg($msg['msg']));
				}else{
					$data['logo'] = $up['upload_part'];
					@unlink(SYS_ROOT.'/'.$c['logo']);
				}
			}
			if($data['logo'])$data['type'] = 'pic';
			$data['order_num'] = to_int($data['order_num']);
			if ($add) {
				$db->insert($data,'link');
			} else {
				$db->update($data,'link',array('id'=>$id));
			}
			$_SESSION['post_link_file'] = '';
			delete_link_cache();
			$sp -> bgmsg($msg,'admincp.php?action=link&do=list');
			break;
		default:dump($_POST);exit('invaild post data');
	}
}
$do = trim($_GET['do']);
$tpl -> assign("do",$do);
switch ($do) {
	case 'state':
			$msg = "操作成功！";
			$id = to_int($_GET['id']);
			if(empty($id))die($sp -> bgmsg('参数错误！',$_SERVER["HTTP_REFERER"]));
			$state = to_int($_GET['state']);
			$state == 0 ? $state = 1 : $state = 0;
			$sql = 'UPDATE `##__link` SET state = '.$state.' WHERE id = '.$id;
			$db->query($sql);
			delete_link_cache();
			$sp -> bgmsg($msg,$_SERVER["HTTP_REFERER"]);
		break;
	case 'delete':
		$id = to_int($_GET['id']);
		$check  = $db->fetch("SELECT * FROM ##__link WHERE `id` = '$id';",TRUE);
		if(empty($check))exit($sp->bgmsg('无可用数据'));
		$db->query("DELETE FROM ##__link WHERE `id` = '$id';");
		$check['type']=='pic'?@unlink(SYS_ROOT.$check['logo']):'';
		delete_link_cache();
		$sp->bgmsg('操作成功.');
		break;
	case 'add':
		$sql = 'SELECT * FROM ##__link_type';
		$tarr = $db -> fetch($sql);
		$tpl -> assign("tarr",$tarr);
		$pn  = '添加链接';
		$tpl->assign('farr',$_SESSION['post_link_file']['data']);
		break;
	case 'edit':
		$id = to_int($_GET['id']);
		if(empty($id))die($sp -> bgmsg('参数错误！'));
		$farr = $db->fetch("SELECT * FROM ##__link WHERE `id` = '$id'",TRUE);
		if($_SESSION['post_link_file']['data']){
			$farr = $_SESSION['post_link_file']['data'];
		}
		$tpl -> assign("farr",$farr);
		$sql = 'SELECT * FROM ##__link_type';
		$tarr = $db -> fetch($sql);
		$tpl -> assign("tarr",$tarr);
		$pn  = '编辑链接';
		break;
	default:
		$pn  = '链接列表';
		$sql = 'SELECT * FROM  ##__link ORDER BY order_num DESC,add_time DESC';
		$page = new ShowPage;
		$page->PageSize = 20;
		$page->Total = $db -> getCount($sql);
		$page->LinkAry = array(1,2);
		$re  = $db -> sql_query($sql." limit  ".$page->OffSet());
		$showpage = $page->ShowLink();
		$farr = $db -> sql_fetchrowset($re);
		$tpl -> assign("farr",$farr);
		$tpl -> assign("show_page",$showpage);
		$db -> sql_freeresult($re);
		$db -> sql_close();
}
$tpl -> assign("admin_nav",shownav($pn));
$sp  -> outPut('link.tpl',$pn);
?>