<?php

Class AboutusAction extends PublicAction {

	public function index() {
		
            $Aboutus = D("AboutusView");
            $count = $Aboutus->count();
            $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 =  $Aboutus->order("cTime desc")->limit($p->firstRow.','.$p->listRows)->select();
            $page = $p->show();
            $this->assign("list", $list);
            $this->assign("count", $count);
            $this->assign("page", $page);

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

            $this->display();
	}

          public function tag() {
            $Tag = M("Tag");
            if(!empty($_GET['name'])) {
                $name = trim($_REQUEST['name']);
                $list = $Tag->where("module='Aboutus' and name='$name'")->field('id,count')->find();
				$tagId = $list['id'];
				$count = $list['count'];
				import("@.ORG.Page");
				$listRows = 5;
				$fields	= 'a.id,a.userId,a.categoryId,a.cTime,a.readCount,a.commentCount,a.title,c.title as category';
				$p = new Page($count,$listRows);
				$p->setConfig('header' ,'篇文章 ');
				$dao = D("Aboutus");
				$list = $dao->query("select ".$fields." from ".C('DB_PREFIX').'aboutus as a,'.C('DB_PREFIX').'tagged as b, '.C('DB_PREFIX').'category as c where b.tagId  in ('.$tagId.') and a.categoryId= c.id and a.status=1  and a.id=b.recordId order by a.id desc limit '.$p->firstRow.','.$p->listRows);
				if($list) {
                    $page = $p->show();
                    $this->assign("page", $page);
                    $this->assign('list',$list);
                }
                $this->assign('tag',$name);
                $this->assign("count",$count);
            }else {
                $list = $Tag->where("module='Aboutus'")->findAll();
                //dump($list);
                $this->assign('tags',$list);
            }
            $this->display();
        }

       
        public function show() {
            $this->getAttach();
            $id = $_REQUEST["id"];
            if (!empty($id)) {
               $Aboutus = D("AboutusView");
               $result = $Aboutus->where('Aboutus.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() {
            $Aboutus = D("Aboutus");
            $aboutus = $this->get('vo');
            // 阅读计数
            $id	= $aboutus["id"];
            if(!isset($_SESSION['aboutus_read_count_'.$id])) {
		$Aboutus->setInc('readCount',"id=".$id);
		$_SESSION['aboutus_read_count_'.$id] = true;
            }
        }
        
}
?>