<?php 
/*
　* @$Id
　* @
　* @Ver: 2.0
　* @Create: 2009-11-18 8:10
　* @Modify: 
　* @Author: CPWEB http://www.cpweb.net.cn
　* @Copyright (C) 2008 www.cpweb.net.cn. All Rights Reserved.
　* @License CPWEB is free software and use is subject to license terms
　*/
class ChannelAction extends GlobalAction 
{
	public function index()
	{
		$ChannelView=D("ChannelView");
		$channelview=$ChannelView->where($map)->order("displayorder")->findall();
		$this->assign('channel',$channelview);
		$this->assign('title','栏目管理');
		$this->display();
	}
	public function add()
	{
		$map['t_c_values']='channel';
		$TypeView=D("TypeView");
		$type=$TypeView->where($map)->findall();
		$this->assign('type',$type);
		$this->assign('title','添加栏目');
		$this->display();
	}
	public function adde()
	{
		$type_values=substr($_POST['type_values'], 8);
		if($type_values!='external'){
			$map['type_values']=$type_values;
			$ModelView=D("ModelView");
			$model=$ModelView->where($map)->findall();
			$this->assign('model',$model);
		}else{
			$map['address']=$type_values;
			$ModelView=D("ModelView");
			$model=$ModelView->where($map)->find();
			$this->assign('model',$model);
		}
		$this->assign('type_values',$type_values);
		$this->assign('title','添加栏目');
		$this->display();
	}
	public function adds()
	{
		$Channel=M("Channel");
		if(!$Channel->create()){
			$this->error($Channel->getError());
		}
		$map['type_values']='channel_'.$_POST['type_values'];
		$type=M('Type')->where($map)->find();
		$Channel->type_id=$type['id'];
		if($Channel->add()){
			$this->assign('jumpUrl',__URL__);
			$this->success(L('CPWEB_SUCCESS_ADD'));
		}else{
			$this->error(L('CPWEB_ERROR_ADD')); 
		}
	}
	public function edit()
	{
		$ChannelView=D("ChannelView");
		$map['Channel.id'] = $_GET['id'];
		$channelview=$ChannelView->where($map)->find();
		$Model=M("Model");
		$model_map['type_id']=$channelview['model_type_id'];
		$model=$Model->where($model_map)->findall();
		$this->assign('model',$model);
		$this->assign('channel',$channelview);
		$this->assign('title','编辑栏目');
		$this->display();
	}
	public function edits()
	{
		$Channel=D("Channel");
		$map['id']=$_POST['id'];
		$channel=$Channel->where($map)->find();
		if($channel['model_id'] != $_POST['model_id']){
			$Category=M("Category");
			$mep['channels']=$_POST['id'];
			$category=$Category->where($mep)->findall();
			if($category){
				$this->error(L('CPWEB_ERROR_CHANNEL_EDIT'));
			}
		}
		if($channel['folder'] != $_POST['folder']){
			$mep['folder']=$_POST['folder'];
			$category=$Channel->where($mep)->findall();
			if($category){
				$this->error(L('CPWEB_ERROR_CHANNEL_F'));
			}
		}
		if(!$Channel->create()){
			$this->error($Channel->getError());
		}
		if($Channel->save()){
			$this->assign('jumpUrl',__URL__);
			$this->success(L('CPWEB_SUCCESS_EDIT'));
		}else{
			$this->error(L('CPWEB_ERROR_EDIT')); 
		}
	}
	public function delete(){
		$ChannelView=D("ChannelView");
		$map['Channel.id'] = $_GET['id'];
		$channelview=$ChannelView->where($map)->find();
		if($channelview['type_values']=='model'){
			$Category=M("Category");
			$mep['channel_id']=$_GET['id'];
			$category=$Category->where($mep)->findall();
			if($category){
				$this->error(L('CPWEB_ERROR_CHANNEL_EDIT'));
			}
		}
		$this->cpweb_delete();
	}
}
?>