<?php

Class TranslateAction extends CommonAction {

   public function insert() {
		$Translate = D("Translate");
		if ($vo = $Translate->create()) {
			$list = $Translate->add();
			if ($list) {
				//数据保存触发器
				if (method_exists($this,'_trigger')) {
					$this->_trigger($vo,$list);
				}
				$this->success("操作成功");
			}else {
				$this->error("操作失败");
			}
		}else {
			$this->error($Translate->getError());
		}
	}
        
	// 查看分类文章
        public function category() {
            $pid = $_GET["pid"];
        	$id = $_REQUEST["id"];
            if (!empty($id)) {
                $CateList = M("Category");
                if ($CateList) {
                    $categoryName = $CateList->where('id='.$id and 'id='.$pid)->getField("title");
                    $this->assign('categoryName', $categoryName);
                    $dao = D('TranslateView');
                    //导入分页类
                    import("@.ORG.Page");
                    //取得满足条件的记录数
                    $count = $dao->where('status=1')->where('categoryId='.$id)->count();
                    $p = new Page($count, 5);
                    //分页查询数据
                    $voList = $dao->where('status=1')->where('categoryId='.$id)->limit($p->firstRow.','.$p->listRows)->select();
                    //分页显示
                    $page = $p->show();
                    //模板赋值显示
                    $this->assign('list', $voList);
                    $this->assign("count", $count);
                    $this->assign("page", $page);
                }else {
                   $this->redirect('index');
                   return ;
                }
            }else {
                $this->redirect('index');
            }
            
            $this->display();
            return ;
        }

        public function show() {
            $this->getComment();
            $this->getAttach();
            $id = $_REQUEST["id"];
            if (!empty($id)) {
               $Translate = D("TranslateView");
               $result = $Translate->where('Translate.id='.$id)->find();  // 这里为什么用select()就读不出来
               if ($result) {
                   $this->assign('vo', $result);
               }else {
                   $this->redirect('index');
                   return ;
               }
            }else {
                $this->redirect('index');
            }
            $this->display();
            return ;
        }

        public function _after_show() {
            $Translate = D("Translate");
            $translate = $this->get('vo');
            // 阅读计数
            $id	= $translate["id"];
            if(!isset($_SESSION['translate_read_count_'.$id])) {
		$Translate->setInc('readCount',"id=".$id);
		$_SESSION['translate_read_count_'.$id] = true;
            }
        }

        public function _before_add() {
                $verify = build_verify(8);
                $_SESSION['attach_verify'] = $verify;
                $this->assign('verify',$verify);
        }
        public function add() {
        				
		    $Cate = M("Category");
		    $cateList = $Cate->findAll();
		    $this->assign("category", $cateList);
            $this->display();
        }
	public function index() {
	       $Cate = M("Category");
		//$cateList = $Cate->findAll();
		//$cateList = list_to_tree();

		   $cateList=$Cate->field("id,title,pid,path,concat(path,'-',id) as bpath")->order('bpath')->select();
	          foreach($cateList as $key=>$value){
				$cateList[$key]['count']=count(explode('-',$value['bpath']));
			}
		    $this->assign("category", $cateList);
	    
        
                if (ACTION_NAME != "add") {
                    //切换到高级模型
                    $Translate = M("Translate", "AdvModel");
                    $Comment = M("Comment", "AdvModel");
                    // 需要将msubstr()从扩展库extend.php中拷贝到common.php中
                    $new = $Translate->where('status=1')->order("cTime desc")->top10();
                    $comment = $Comment->where('status=1')->order("id desc")->top8();
                    $this->assign("lastTranslates", $new);
                    $this->assign("lastComments", $comment);
                    // 标签列表
                    $List = M("Tag");
                    $list  = $List->where("module='Translate'")->field('id,name,count')->order('count desc')->limit('0,25')->findAll();
                    $this->assign('tags',$list);

                    $list = array();
                    // 获取归档文章
                    if(!isset($_SESSION["TranslateArchiveList2"])) {
                        $Translate = M("Translate");
                        $new = $Translate->max("cTime");
                        $old = strtotime("-1 year");
                        $time = $new;
                        while($time>=$old) {
                            $list[] = array('year'=>date('Y',$time),'month'=>date('m',$time),'show'=>$time);
                            $time = strtotime('-1 month',$time);
			}
                        $_SESSION['TranslateArchiveList2'] = $list;
                    }else {
                        $list = $_SESSION['TranslateArchiveList2'];
                    }
                    $this->assign('monthList',$list);
                }
		
            $Translate = D("TranslateView");
            $count = $Translate->count();
            $mode = "normal";
            if (isset($_REQUEST["mode"])) {
                $mode = $_REQUEST["mode"];
            }
            if ($mode == "list") {
                $listRows = 23;
                //$fields	= 'id,title,categoryId,category,cTime,readCount,commentCount';
            }else {
                $listRows = 3;
                //$fields	= 'id,title,categoryId,cTime,readCount,commentCount,content,tags';
            }
            import("@.ORG.Page");
            $p = new Page($count, $listRows);
            $p->setConfig("header", "篇文章");
            $this->assign("mode", $mode);
            $list =  $Translate->order("cTime desc")->limit($p->firstRow.','.$p->listRows)->select();
            $page = $p->show();
            $this->assign("list", $list);
            $this->assign("count", $count);
            $this->assign("page", $page);

            //统计数据
            $Translate = M("Translate");
            $stat = array();
            $stat["beginTime"] = $Translate->min('cTime');
            $stat["translateCount"] = $Translate->where("status=1")->count();
            $stat["readCount"] = $Translate->where("status=1")->sum("readCount");
            $stat["commentCount"] = $Translate->where("status=1")->sum("commentCount");
            $this->assign($stat);

            $this->display();
            
            
	}

      
        public function edit() {
            $Translate = M("Translate");
            $id = $_REQUEST['id'];
            $translate = $Translate->where('id='.$id)->find();
            //dump($translate);
            if($translate) {
                $this->getAttach();
                $this->assign('vo',$translate);
                $this->display();
            }else{
                $this->redirect('Translate/index');
            }
	}
        public function update() {
            $model = D("Translate");
            $vo = $model->create('','edit');
            if(!$vo) {
                $this->error($model->getError());
            }
            $id	= is_array($vo)?$vo[$model->getPk()]:$vo->{$model->getPk()};
            $result  = $model->save($vo);
            if($result) {

                //数据保存触发器
                if(method_exists($this,'_trigger')) {
                    $this->_trigger($vo, $result);
                }

                if(!empty($_FILES)) {//如果有文件上传
                    //执行默认上传操作
                    //保存附件信息到数据库
                   $this->_upload(MODULE_NAME,$result);
                }

                //成功提示
                $this->success("操作成功！");
            }else {
                //错误提示
                $this->error($model->getError());
            }
        }
        	  // 删除数据 
    public function delete() { 
        if(!empty($_POST['id'])) { 
            $Translate  =  M("Translate"); 
            $result    =    $Translate->delete($_POST['id']); 
            if(false !== $result) { 
                $this->ajaxReturn($_POST['id'],'删除成功！',1); 
            }else{ 
                $this->error('删除出错！'); 
            } 
        }else{ 
            $this->error('删除项不存在！'); 
        } 
    } 

}
?>
