<?php

Class ProductAction extends PublicAction {

	// 查看分类文章
        public function procategory() {
            $pid = $_GET["pid"];
        	$id = $_REQUEST["id"];
            if (!empty($id)) {
                $CateList = M("Procategory");
                if ($CateList) {
                    $procategoryName = $CateList->where('id='.$id and 'id='.$pid)->getField("title");
                    $this->assign('procategoryName', $procategoryName);
                    $dao = D('ProductView');
                    //导入分页类
                    import("@.ORG.Page");
                    //取得满足条件的记录数
                    $count = $dao->where('status=1')->where('procategoryId='.$id)->count();
                    $p = new Page($count, 5);
                    //分页查询数据
                    $voList = $dao->where('status=1')->where('procategoryId='.$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->getAttach();
            $id = $_REQUEST["id"];
            if (!empty($id)) {
               $Product = D("ProductView");
               $result = $Product->where('Product.id='.$id)->find();  // 这里为什么用select()就读不出来
               if ($result) {
                   $this->assign('voshow', $result);
               }else {
                   $this->redirect('index');
                   return ;
               }
            }else {
                $this->redirect('index');
            }
            $this->display();
            return ;
        }

        public function _after_show() {
            $Product = M("Product");
            $product = $this->get('voshow');
            // 阅读计数
            $id	= $product["id"];
            if(!isset($_SESSION['product_read_count_'.$id])) {
		$Product->setInc('readCount',"id=".$id);
		$_SESSION['product_read_count_'.$id] = true;
            }
        }

        
	public function index() {
		     
            $Product = D("ProductView");
            $count = $Product->count();
                      
            $listRows = 5;
            import("@.ORG.Page");
            $p = new Page($count, $listRows);
            $p->setConfig("header", "个产品");
            $this->assign("mode", $mode);
            $list =  $Product->order("cTime desc")->limit($p->firstRow.','.$p->listRows)->select();
            $page = $p->show();
            $this->assign("list", $list);
            $this->assign("count", $count);
            $this->assign("page", $page);
            
	    
            // 标签列表
           $List = M("Tag");
           $list  = $List->where("module='Product'")->field('id,name,count')->order('count desc')->limit('0,25')->findAll();
           $this->assign('tags',$list);
           $list = array();

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

            $this->display();
	}

        public function tag() {
            $Tag = M("Tag");
            if(!empty($_GET['name'])) {
                $name = trim($_REQUEST['name']);
                $list = $Tag->where("module='Product' and name='$name'")->field('id,count')->find();
				$tagId = $list['id'];
				$count = $list['count'];
				import("@.ORG.Page");
				$listRows = 5;
				$fields	= 'a.id,a.userId,a.procategoryId,a.cTime,a.readCount,a.commentCount,a.title,c.title as procategory';
				$p = new Page($count,$listRows);
				$p->setConfig('header' ,'个产品 ');
				$dao = D("Product");
				$list = $dao->query("select ".$fields." from ".C('DB_PREFIX').'product as a,'.C('DB_PREFIX').'tagged as b, '.C('DB_PREFIX').'procategory as c where b.tagId  in ('.$tagId.') and a.procategoryId= 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='Product'")->findAll();
                //dump($list);
                $this->assign('tags',$list);
            }
            $this->display();
        }
        
	   public function search(){
	   	 $product = D('Product');  	 
		 $search=$_POST[search];
		 $title=$_POST[title];
		 
		 $category=$_POST[category];
		 $count = $product->count();
         $listRows = 6;
         import("@.ORG.Page");
         $p = new Page($count, $listRows);
         $p->setConfig("header", "个产品");
		 
		 $info =$product->where("content like '%".$title."%'")->order('cTime desc')->limit($p->firstRow.','.$p->listRows)->select();
		

         $page = $p->show();        
         $this->assign("count", $count);
         $this->assign("page", $page);
		 $this->assign('info',$info);
		 $this->display();

	   }      

}
?>