<?php
/**
 * 金戈企业建站系统：产品管理页面
 *
 *
 * @name 
 * @author 烟鬼 QQ:460874 Email:460874@qq.com
 * @version 0.1  $id  rootcp.php
 * @copyright Copyright (c) 2007 - 2008 烟鬼
 */
session_start();
require('../config.php');
require('rootfun/roothtml.php');
require('rootfun/rootfun.php');

//判断是否是管理员，该函数在myfun.php 中定义
if(!check_root())
	{
		echo '<meta http-equiv="Refresh" content="0; url=../error.php?error=nono" >';
		exit;
	}

//插入页头
rootheader();
?>
<div id="rootmain">
<?php
$action = $_GET['action'];

//取回所有的分类并做前期处理
if ($action == 'cpclass') 
{
     $cpclass = root_all(cp_class);
     if (!$cpclass) 
     {
          echo '没有分类...';
     }
     if ($cpclass) 
     {
          $cpclass = setarray($cpclass);
          foreach ($cpclass as $row)
		{
    		echo '分类名称：  '.$title = $row['cp_c_name'].'    '; 
    		echo '<a href=rootcp.php?action=delcpclass&cid='.$row['cp_c_id'].'>删除</a>   ';
    		echo '<a href=rootcp.php?action=endcpclass&cid='.$row['cp_c_id'].'>编辑</a><br />';
 		}
     }
 	cpclass_form();
}

//删除指定的分类
if ($action == 'delcpclass') 
{
     $cid = trim($_GET['cid']);
     $conn = db_connect();
     $result = $conn->query('SELECT cp_id FROM cp_cp where cp_c_id='.$cid);
     if ($result->num_rows > 0) 
     {
          echo '该分类下有产品,请先删除该分类下的所有产品';
          exit;
     }
     $result = $conn->query('DELETE FROM cp_class WHERE cp_c_id='.$cid);
     if (!$result) 
     {
          echo '数据库错误';
          exit;
     }
     else 
     {
     	echo '删除成功--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
     }
}

//编辑产品分类
if ($action == 'endcpclass') 
{
     $cid = trim($_GET['cid']);
     $endcpclass = endcpclass($cid);
     cpclass_form($endcpclass);
}

//更新产品分类
if ($action == 'upcpclass') 
{
     $cpclass = my_yz($_POST['cpclass']);
     $cid = trim($_POST['cid']);
     if (upcpclass($cid,$cpclass)) 
     {
          echo '更新成功--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
     }
     else 
     {
     	echo '更新失败--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
     }
}

//添加产品分类
if ($action == 'incpclass') 
{
     $cname = my_yz($_POST['cpclass']);
     if (incpclass($cname)) 
     {
          echo '添加成功--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
     }
     else 
     {
     	echo '添加失败--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
     }
}

//取回所有的产品并做分页等处理
if ($action == 'chanpin') 
{
     sechanpin();
}

//编辑指定的产品
if ($action == 'endcp') 
{
     $cpid = trim($_GET['cpid']);
     $endcp = root_alli(cp_cp,cp_id,$cpid);
     $endcp = setassoc($endcp);
     display_chanpin_form($endcp);
}

//更新指定的产品
if ($action == 'upchanpin') 
{
     $cpname = my_yz($_POST['cpname']);
     $cpxh = my_yz($_POST['cpxh']);
     $cid = trim($_POST['cid']);
     $cpid = $_POST['cpid'];
     $sm = my_yz($_POST['sm']);
     
     //取得是否主页推荐
     if ($_POST['cpkz'] == 'yes') 
     {
          $cpkz = 1;
          if (!indexcprows()) 
          {
				echo '首页推荐产品最多三条--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
				exit;
          }
     }
     else 
     {
     	$cpkz = 0;
     }
     if (!filled_out($_POST)) 
     {
          echo '完整填写表单--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
          exit;
     }
     
     if (strlen($cpname) > 255 || strlen($cpxh) > 255 || strlen($sm) > 255 || strlen($cpkz) > 1
		|| strlen($cid) > 5) 
     {
          echo '字数太多。请保持在100字以内--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
          exit;
     }
     
     //如果更新内容有图片信息
     if (isset($_FILES['userfile']['name'])&&is_uploaded_file($_FILES['userfile']['tmp_name'])) 
     {
		//严整上传内容是否有错误
     	if ($_FILES['userfile']['error'] > 0)
  		{
    		echo '警告: ';
    		switch ($_FILES['userfile']['error'])
    		{
      			case 1:  echo '文件大小超出系统设置的值--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a><br />';  break;
      			case 2:  echo '文件大小超出系统设置的值--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a><br />';  break;
      			case 3:  echo '上传文件残缺--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a><br />';  break;
      			case 4:  echo '您还没有选择任何需要上传的文件--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a><br />';  break;
    		}
		}
		//严整上传文件格式是否正确
		if ($_FILES['userfile']['type'] != 'image/jpeg' && $_FILES['userfile']['type'] != 'image/pjpeg')
  		{
    		echo '警告: 不符合系统要求的文件格式,请使用JPEG格式的图片--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a><br />';
    		exit;
  		}
  		//对上传文件的安全验证等
  		$upfile = '../up/image/'.$_FILES['userfile']['name'];
  		if (is_uploaded_file($_FILES['userfile']['tmp_name'])) 
  		{
     		if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile))
     		{
        		echo '警告: 无up/image文件夹或对该文件夹无写入权限！<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a><br />';
        		exit;
     		}
  		} 
  		else 
  		{
    		echo '警告: 文件名内包含敏感字符，请修改文件名: ';
    		echo $_FILES['userfile']['name'].'<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a><br />';
    		exit;
  		}
  		
  		$upfile = 'up/image/'.$_FILES['userfile']['name'];
  		if (upchanpin($upfile,$cpname,$cpxh,$cid,$sm,$cpkz,$cpid)) 
  		{
  	     	echo '更新成功--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
  		}
  		else 
  		{
  			echo '更新失败--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
  		}
     }
     //或者只有文字信息
     else 
     {
     	if (upchanpin_nr($cpname,$cpxh,$cid,$sm,$cpkz,$cpid)) 
     	{
     	     echo '更新成功--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
     	}
     	else 
     	{
     		echo '更新失败--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
     	}
     }
}

//删除选择的产品
if ($action == 'delcp') 
{
     $del = $_POST['del_me'];
     $img = $_POST['img'];
     if (count($del) > 0) 
     {
          foreach ($del as $nid)
          {
          	if (delchanpin($nid)) 
          	{
          		foreach ($img as $cpimg)
          		{
          			$cpimg = '../'.$cpimg;
          			if (file_exists($cpimg)) 
          			{
          			     if (unlink($cpimg)) 
          			     {
          			          echo '删除成功...<br />';
          			     }
          			     else 
          			     {
          			     	echo '该条产品删除成功.图片删除失败，请检查文件夹权限<br />';
          			     }
          			}
          			else 
          			{
          				echo '数据库内容删除成功.该产品没有图片或路径错误!';
          			}
          		}
          	}
          	else 
          	{
          		echo '删除失败....<br />';
          	}
          }
     }
     else 
     {
     	echo '您还没有选择需要删除的产品！';
     }
}

//添加产品
if ($action == 'inchanpin') 
{
     display_chanpin_form();
}

//处理添加的产品
if ($action == 'incp') 
{
	$cpname = my_yz($_POST['cpname']);
     $cpxh = my_yz($_POST['cpxh']);
     $cid = $_POST['cid'];
     $sm = my_yz($_POST['sm']);
     $select = $_POST['select'];
     if ($_POST['cpkz'] == 'yes') 
     {
          $cpkz = 1;
          if (!indexcprows()) 
          {
				echo '首页推荐产品最多三条--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
				exit;
          }
     }
     else 
     {
     	$cpkz = 0;
     }
     if (!filled_out($_POST)) 
     {
          echo '完整填写表单--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
          exit;
     }
     if ($select == 'no') 
     {
          echo '请先添加产品分类--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
          exit;
     }
     if (strlen($cpname) > 255 || strlen($cpxh) > 255 || strlen($sm) > 255 || strlen($cpkz) > 1
		|| strlen($cid) > 5) 
     {
          echo '字数太多。请保持在100字以内--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
          exit;
     }
     
     //严整上传内容是否有错误
     	if ($_FILES['userfile']['error'] > 0)
  		{
    		echo '警告: ';
    		switch ($_FILES['userfile']['error'])
    		{
      			case 1:  echo '文件大小超出系统设置的值--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a><br />';  break;
      			case 2:  echo '文件大小超出系统设置的值--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a><br />';  break;
      			case 3:  echo '上传文件残缺--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a><br />';  break;
      			case 4:  echo '请选择产品图片--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a><br />';  break;
    		}
		}
		//严整上传文件格式是否正确
		if ($_FILES['userfile']['type'] != 'image/jpeg' && $_FILES['userfile']['type'] != 'image/pjpeg')
  		{
    		echo '警告: 不符合系统要求的文件格式,请使用JPEG格式的图片--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a><br />';
    		exit;
  		}
  		//对上传文件的安全验证等
  		$upfile = '../up/image/'.$_FILES['userfile']['name'];
  		if (is_uploaded_file($_FILES['userfile']['tmp_name'])) 
  		{
     		if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile))
     		{
        		echo '警告: 无up/image文件夹或对该文件夹无写入权限！<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a><br />';
        		exit;
     		}
  		} 
  		else 
  		{
    		echo '警告: 文件名内包含敏感字符，请修改文件名: ';
    		echo $_FILES['userfile']['name'].'<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a><br />';
    		exit;
  		}
  		
  		$upfile = 'up/image/'.$_FILES['userfile']['name'];
  		if (inchanpin($cid,$cpkz,$cpname,$cpxh,$sm,$upfile)) 
  		{
  	     	echo '添加成功--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
  		}
  		else 
  		{
  			echo '添加失败--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
  		}
}
?>
</div>
<?php
//插入页脚
rootfooter();
?>