<?php
require "./FCKeditor/fckeditor.php";
class productAction extends CommonAction{
	private $oFCKeditor;
	private $product;
	function __construct(){
			parent::__construct();
			$this->product = new productModel();
			$this->oFCKeditor = new FCKeditor("content") ;          // 创建FCKeditor实例，可创建多个实例
			$this->oFCKeditor->BasePath = './FCKeditor/';
		}


	function category_add() {
    	$this->product= new productModel("_cate");
    	$select = $this->product->cate_select();
    	$this->assign('select',$select);
    	$this->assign('channelid',$_REQUEST['channelid']);
    	$this->display('admin/product/categroy.html');

    }


    function insert(){
    	$this->product = new productModel("_cate");
		if($this->product->validate()){
			$pinfo = $this->product->find($_POST['parentid']);
			$_POST['htmldir']=$this->product->get_htmldir($pinfo['htmldir']);
			$this->upload = new upload(array('filepath'=>ATTCHMENT_REAL_PATH.'product'));
			if($this->upload->uploadFile('picdir')){
			global $thumbSize;
			$picname =$this->upload->getNewFileName();
			$this->image = new image(ATTCHMENT_REAL_PATH.'product');
			$imagename = $this->image->thumb($picname,$thumbSize['width'],$thumbSize['height'],'');
			$_POST['catepic']='product/'.$imagename;
				if($this->product->add($_POST)){
					$this->showMsg("成功添加分类","admin.php?m=product&a=category_mod&channelid=$_POST[channelid]");
				}else{
					$this->showMsg("添加分类失败","admin.php?m=product&a=category_mod&channelid=$_POST[channelid]");
				}

			}else{
				$this->showMsg($this->upload->getErrorMsg(),"admin.php?m=product&a=category_add&channelid=$_POST[channelid]");
			}


		}else{
			$this->showMsg($this->product->getMessList(),"admin.php?m=product&a=category_add&channelid=$_POST[channelid]");
		}
    }



    function category_mod() {
    	$this->product = new productModel("_cate");
    	$list = $this->product->cate_list();
    	$this->assign('channelid',$_REQUEST['channelid']);
    	$this->assign('list',$list);
    	$this->display('admin/product/catemod.html');

    }


    function ordering(){
    	$this->product = new productModel("_cate");
		if(empty($_POST['order'])){
			$this->showMsg("没有需要排序的频道","admin.php?m=product&a=category_mod&channelid=$_POST[channelid]");
		}
		if($this->product->valorder()){
			if($this->product->ordering()){
				$this->showMsg("排序修改成功","admin.php?m=product&a=category_mod&channelid=$_POST[channelid]");
			}else{
				$this->showMsg("排序修改失败","admin.php?m=product&a=category_mod&channelid=$_POST[channelid]");
			}

		}else{
			$this->showMsg($this->product->getMessList(),"admin.php?m=product&a=category_mod&channelid=$_POST[channelid]");
		}
	}



		function cate_del(){
			$this->product = new productModel("_cate");
			if($this->product->checkcolumn()){
					if($this->product->del($_REQUEST["id"])){
						$this->showMsg("成功删除分类","admin.php?m=product&a=category_mod&channelid=$_REQUEST[channelid]");
					}else{
						$this->showMsg("删除分类失败","admin.php?m=product&a=category_mod&channelid=$_REQUEST[channelid]");;
					}
			}else{
				$this->showMsg("必须先删除下级分类","admin.php?m=product&a=category_mod&channelid=$_REQUEST[channelid]");
			}

		}


		function cate_edit(){
			$this->product = new productModel("_cate");
			$_POST=$this->product->find($_REQUEST["id"]);
			$this->assign('catepic',$_POST['catepic']);
			$_POST['catepic']=ATTCHMENT_PATH.$_POST['catepic'];
			$_POST['htmldir']=$this->product->path_dir($_POST['htmldir']);
			$select = $this->product->cate_select($_REQUEST["id"],'0');
    		$this->assign('select',$select);
			$this->assign("post",$_POST);
			$this->assign('channelid',$_REQUEST['channelid']);
    		$this->display('admin/product/cateedit.html');

		}

	function cate_update(){
		$this->product = new productModel("_cate");
		if($this->product->validate()){
			$pinfo = $this->product->find($_POST['parentid']);
			$_POST['htmldir']=$this->product->get_htmldir($pinfo['htmldir']);
			$this->upload = new upload(array('filepath'=>ATTCHMENT_REAL_PATH.'product'));
			if($this->upload->uploadFile('picdir')){
			global $thumbSize;
			$picname =$this->upload->getNewFileName();
			$this->image = new image(ATTCHMENT_REAL_PATH.'product');
			$imagename = $this->image->thumb($picname,$thumbSize['width'],$thumbSize['height'],'');
			@unlink(ATTCHMENT_REAL_PATH.$_POST['catepic']);
			$_POST['catepic']='product/'.$imagename;
			}else{
				unset ($_POST['catepic']);
			}

			if($_POST['parentid']==$_REQUEST['id']){
				$this->showMsg("不能以自己为父类","admin.php?m=product&a=category_mod&channelid=$_POST[channelid]");
			}
			$code = $this->product->check_parentid($_REQUEST['id']);
			if(is_array($code) && in_array($_REQUEST['parentid'],$code)){
				$this->showMsg("不能以自己的子类为父类","admin.php?m=product&a=category_mod&channelid=$_POST[channelid]");
			}
			if($this->product->mod($_POST)){
				$this->showMsg("成功修改分类","admin.php?m=product&a=category_mod&channelid=$_POST[channelid]");
			}else{
				$this->showMsg("修改分类失败","admin.php?m=product&a=category_mod&channelid=$_POST[channelid]");
			}

		}else{
			$this->showMsg($this->product->getMessList(),"admin.php?m=product&a=category_mod&channelid=$_POST[channelid]");
		}


	}

		//==================上面为分类==下面为产品=====================
		function product_add(){
			$this->product = new productModel("_cate");
    		$select = $this->product->cate_select();
			$this->assign('fieldform',$this->get_fieldset());
    		$this->assign('select',$select);
    		$this->assign('channelid',$_REQUEST['channelid']);
			$this->assign("FCKeditor",$this->oFCKeditor->Create());
			$this->display('admin/product/product.html');
		}


		function get_fieldset(){
			$this->manage = new manageModel();
			return $this->manage->getModelfieldset();

		}

		function product_insert(){
			if($this->product->chech_form()){
				if(empty($_FILES[spic][name])){
					$this->showMsg("产品图片必须上转","admin.php?m=product&a=product_add&channelid=$_REQUEST[channelid]");
				}
				$this->upload_pic();
				$_POST['publish']=time();
				foreach($_POST as $key =>$val){
					if(is_array($val)){
						$_POST[$key]=implode(',',$val);
					}
				}

				if($this->product->add($_POST)){
					$this->showMsg("成功添加产品","admin.php?m=product&a=product_add&channelid=$_REQUEST[channelid]");
				}else{
					$this->showMsg("添加产品失败","admin.php?m=product&a=product_add&channelid=$_REQUEST[channelid]");;
				}

			}else{
				$this->showMsg($this->product->getMessList(),"admin.php?m=product&a=product_add&channelid=$_REQUEST[channelid]");
			}

		}

		function upload_pic(){
			global $thumbSize,$pictureSize;
			$this->upload = new upload(array('filepath'=>ATTCHMENT_REAL_PATH.'product'));
			$this->image = new image(ATTCHMENT_REAL_PATH.'product');
			if(!empty($_FILES)){

				foreach($_FILES as $key=>$val){
					$empty = is_array($val[name])? array_filter($val[name]) : $val[name];

					if(empty($empty)){
						unset($_FILES[$key]);
						continue;
					}

					 if($this->upload->uploadFile($key)){
					 	$picname =$this->upload->getNewFileName();
					 	if(is_array($picname)){
					 		$imagename=array();
					 		foreach($picname as $k=> $v){
//					 			$srcimage = $this->image->thumb($v,$thumbSize['width'],$thumbSize['height'],'product_');
//								$imagename[] = 'product/'.$this->image->water($srcimage,WATER,POSITION,'product_');
					 			$srcimage = $this->image->thumb($v,$pictureSize['maxWidth'],$pictureSize['maxHeight'],'');
					 			$imagename[] = 'product/'.$this->image->water($srcimage,WATER,POSITION,'');
					 		}

					 		$_POST[$key]=implode("|",$imagename);
					 	}else{
					 			$imagename = 'product/'.$this->image->thumb($picname,$thumbSize['width'],$thumbSize['height'],'');
					 			$name="elincms".$key;

							if(!empty($_POST[$name])){
								@unlink(ATTCHMENT_REAL_PATH.$_POST[$name]);
							}
					 		$_POST[$key]=$imagename;
					 	}



					 }else{
					 	unset($_POST[$key]);
						$this->showMsg($this->upload->getErrorMsg(),"admin.php?m=product&a=product_add&channelid=$_POST[channelid]");


					 }
				}
			}


		}


		function product_mod(){
			$where=$and="";
			if(isset($_REQUEST[select_id])){
				$this->newcate = new productModel("_cate");
				$id = $this->newcate->check_parentid($_REQUEST[select_id]);

					if(!empty($id)){
						$id  = implode(",",$id);
						$id .=",".$_REQUEST[select_id];
						$con = "cateid in (".$id.")";
						$where ="where $con and channelid=$_REQUEST[channelid]";
						$and = "and $con";
					}else{
						$con = "cateid = ".$_REQUEST[select_id]."";
						$where ="where $con and channelid=$_REQUEST[channelid]";
						$and = "and $con";
					}
			}else{
				$where ="where channelid=$_REQUEST[channelid]";
			}
			$total = count( $this->product->total($where));
			$this->page = new page($total,10,'');

			$val2 = $this->product->findAll("",$this->page->getOffset(),10,"where channelid=$_REQUEST[channelid] $and ","id");
			$fpage =$this->page->fpage();
			$val=$this->get_cateinfo($val2);
			$this->assign("fpage",$fpage);
			$this->assign("product",$val['list']);
			$this->assign("select",$val['select']);
			$this->assign("channelid",$_REQUEST['channelid']);
			$this->display('admin/product/productmod.html');
		}



		function get_cateinfo($val2){
			$this->cateinfo = new productModel("_cate");
			$select = $this->cateinfo->cate_select();
			$val = $this->cateinfo->findAll();
			foreach ($val as $key => $v){
				$value[$v['id']]=$v['catename'];
			}
			foreach ($val2 as $key =>$v){
				$val2[$key]['catename']=$value[$v['cateid']];
				$val2[$key]['spic']=ATTCHMENT_PATH.$val2[$key]['spic'];
			}
			$value = array('select'=>$select,'list'=>$val2);
			return $value;


		}

		function product_del(){
			if($_REQUEST["id"]=="data"){
			$_REQUEST["id"]=$_REQUEST["xm"];
			}
			if($this->product->del($_REQUEST["id"])){
				$this->showMsg("成功删除内容","admin.php?m=product&a=product_mod&channelid=$_REQUEST[channelid]");
			}else{
				$this->showMsg("删除内容失败","admin.php?m=product&a=product_mod&channelid=$_REQUEST[channelid]");
			}
		}



		function product_edit(){
			$cate = new productModel("_cate");
			$_POST=$this->product->find($_REQUEST["id"]);
			$this->assign('spic',$_POST['spic']);
			$_POST['spic']=ATTCHMENT_PATH.$_POST['spic'];
			if(!empty($_POST[bigpic])){
				$this->assign("picfield",$this->picfield());
			}else{
				$this->assign("picfield","");
			}
    		$select = $cate->cate_select();
    		$this->assign('fieldform',$this->get_fieldset());
    		$this->assign('select',$select);
			$this->oFCKeditor->Value=$_POST["content"];
			$this->assign("post",$_POST);
			$this->assign("FCKeditor",$this->oFCKeditor->Create());
			$this->display('admin/product/productedit.html');


		}


		function picfield(){
			$picdir = explode("|",$_POST[bigpic]);
			$html="<div><input type=\"hidden\"  name=\"picUrl\" value=\"$_POST[bigpic]\" /><ul>";
			foreach($picdir as $key => $val){
				$picdir = ATTCHMENT_PATH.$val;
				$html.="<li style=\"float:left;width:150px; padding-right:5px;\">

						<img src=\"$picdir\" width=\"150\" height=\"100\" id=\"picview$key\" name=\"picview$key\" style=\"float:left\"/>

						<span class=\"del\"><a href=\"admin.php?m=product&a=product_pic_del&id=$_REQUEST[id]&pic=$key&channelid=$_REQUEST[channelid]&cateid=$_REQUEST[cateid]\">删除</a></span>
						</li>";
			}
			$html.="</div></ul>";
			return $html;
		}




		function change_cate(){
			if($_REQUEST['id']=='data' && $_REQUEST['change_cate']!=0){

				if($this->product->change_cate()){
					$this->showMsg("成功移动产品","admin.php?m=product&a=product_mod&channelid=$_REQUEST[channelid]");
				}else{
					$this->showMsg("移动产品失败","admin.php?m=product&a=product_mod&channelid=$_REQUEST[channelid]");
				}

			}else{
					$this->showMsg("请选择目标分类","admin.php?m=product&a=product_mod&channelid=$_REQUEST[channelid]");
			}
		}



		function product_pic_del(){
			$row = $this->product->find($_REQUEST[id]);
			$row = explode("|",$row[bigpic]);
			@unlink(ATTCHMENT_REAL_PATH.$row[$_REQUEST['pic']]);
			unset($row[$_REQUEST[pic]]);

			$row = implode("|",$row);
			$this->product->mod(array('id'=>$_REQUEST[id],'bigpic'=>$row));
			$this->showMsg("成功删除图片","admin.php?m=product&a=product_edit&id=$_REQUEST[id]&channelid=$_REQUEST[channelid]&cateid=$_REQUEST[cateid]");

		}

		function product_update(){
			if($this->product->chech_form()){
				$this->upload_pic();
				if(!empty($_POST[picUrl]) && !empty($_POST[bigpic])){
					$_POST[bigpic].="|".$_POST[picUrl];
				}
				$_POST['publish']=time();
				foreach($_POST as $key =>$val){
					if(is_array($val)){
						$_POST[$key]=implode(',',$val);
					}
				}
				if($this->product->mod($_POST)){
						$this->showMsg("修改产品成功","admin.php?m=product&a=product_edit&id=$_REQUEST[id]&channelid=$_REQUEST[channelid]&cateid=$_REQUEST[cateid]");
					}else{
						$this->showMsg("修改产品失败","admin.php?m=product&a=product_edit&id=$_REQUEST[id]&channelid=$_REQUEST[channelid]&cateid=$_REQUEST[cateid]");
					}

			}else{
				$this->showMsg($this->product->getMessList(),"-1");
			}
		}


}
?>
