<?php

Class LinksAction extends CommonAction {

   public function insert() {
		$Links = D("Links");
		if ($vo = $Links->create()) {
			$list = $Links->add();
			if ($list) {
				//数据保存触发器
			
				$this->success("操作成功");
			}else {
				$this->error("操作失败");
			}
		}else {
			$this->error($Links->getError());
		}
	}
        
        public function _after_show() {
            $Links = D("Links");
            $links = $this->get('vo');
            // 阅读计数
            $id	= $links["id"];
            if(!isset($_SESSION['links_read_count_'.$id])) {
		$Links->setInc('readCount',"id=".$id);
		$_SESSION['links_read_count_'.$id] = true;
            }
        }

        public function add() {
            $this->display();
        }
	public function index() {
            $Links = M("Links");
            $count = $Links->count();
            $listRows = 12;       
            import("@.ORG.Page");
            $p = new Page($count, $listRows);
            $p->setConfig("header", "个链接");
            $this->assign("mode", $mode);
            $list =  $Links->limit($p->firstRow.','.$p->listRows)->select();
            $page = $p->show();
            $this->assign("list", $list);
            $this->assign("count", $count);
            $this->assign("page", $page);

            //统计数据
            $Links = M("Links");
            $stat = array();
            
            $stat["linksCount"] = $Links->where("status=1")->Count("url");
            $stat["readCount"] = $Links->where("status=1")->sum("readCount");
           
            $this->assign($stat);

            $this->display();
	}

        public function edit() {
            $Links = M("Links");
            $id = $_REQUEST['id'];
            $links = $Links->where('id='.$id)->find();
            //dump($links);
            if($links) {
                $this->assign('vo',$links);
                $this->display();
            }else{
                $this->redirect('Links/index');
            }
	}
        public function update() {
            $model = D("Links");
            $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) {

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

}
?>