文章添加,删除,修改,分页列表
Art:
<?php /** * @name UserController * @author pangee * @desc 用户控制器 */ class ArtController extends Yaf_Controller_Abstract { public function indexAction() { return $this->listAction(); } public function addAction($artId=0){ if(!$this->_isAdmin()) { echo json_encode( array("errno"=>-2000, "errmsg"=>"需要管理员权限才可以操作") ); return FALSE; } $submit = $this->getRequest()->getQuery( "submit", "0" ); if($submit!="1"){ echo json_encode(array("errno"=>-2001,"errmsg"=>"请通过正确渠道提交")); return false; } $title = $this->getRequest()->getPost( "title", false ); $contents = $this->getRequest()->getPost( "contents", false ); $author = $this->getRequest()->getPost( "author", false ); $cate = $this->getRequest()->getPost( "cate", false ); if(!$title||!$contents||!$author||!$cate){ echo json_encode( array("errno"=>-2002, "errmsg"=>"标题、内容、作者、分类信息为空,不能为空") ); return FALSE; } $model = new ArtModel(); if ( $lastId = $model->add( trim($title), trim($contents), trim($author), trim($cate), $artId) ) { echo json_encode( array( "errno"=>0, "errmsg"=>"", "data"=>array("lastId"=>$lastId), )); } else { echo json_encode( array( "errno"=>$model->errno, "errmsg"=>$model->errmsg, )); } return TRUE; } public function editAction(){ if(!$this->_isAdmin()){ echo json_encode( array("errno"=>-2000, "errmsg"=>"需要管理员权限才可以操作") ); return FALSE; } $artId = $this->getRequest()->getQuery( "artId", "0" ); if( is_numeric($artId) && $artId ) { return $this->addAction( $artId ); } else { echo json_encode( array("errno"=>-2003, "errmsg"=>"缺少必要的文章ID参数") ); } return TRUE; } public function delAction(){ if(!$this->_isAdmin()){ echo json_encode( array("errno"=>-2000, "errmsg"=>"需要管理员权限才可以操作") ); return FALSE; } $artId=$this->getRequest()->getQuery('artId',"0"); if(is_numeric($artId)&&$artId){ $model = new ArtModel(); if( $model->del( $artId ) ) { echo json_encode( array( "errno"=>0, "errmsg"=>"", )); } else { echo json_encode( array( "errno"=>$model->errno, "errmsg"=>$model->errmsg, )); } } else { echo json_encode( array("errno"=>-2003, "errmsg"=>"缺少必要的文章标题参数") ); } return TRUE; } public function statusAction(){ if(!$this->_isAdmin()){ echo json_encode( array("errno"=>-2000, "errmsg"=>"需要管理员权限才可以操作") ); return FALSE; } $artId=$this->getRequest()->getQuery('artId',"0"); $status = $this->getRequest()->getQuery( "status", "offline" ); if(is_numeric($artId)&&$artId){ $model = new ArtModel(); if( $model->status( $artId,$status) ) { echo json_encode( array( "errno"=>0, "errmsg"=>"", )); } else { echo json_encode( array( "errno"=>$model->errno, "errmsg"=>$model->errmsg, )); } } else { echo json_encode( array("errno"=>-2003, "errmsg"=>"缺少必要的文章标题参数") ); } return TRUE; } public function getAction(){ $artId = $this->getRequest()->getQuery( "artId", "0" ); if( is_numeric($artId) && $artId ) { $model = new ArtModel(); if( $data=$model->get( $artId ) ) { echo json_encode( array( "errno"=>0, "errmsg"=>"", "data"=>$data, )); } else { echo json_encode( array("errno"=>-2009, "errmsg"=>"获取文章信息失败") ); } } else { echo json_encode( array("errno"=>-2003, "errmsg"=>"缺少必要的文章标题参数") ); } return TRUE; } public function listAction(){ $pageNo = $this->getRequest()->getQuery( "pageNo", "0" ); $pageSize = $this->getRequest()->getQuery( "pageSize", "10" ); $cate = $this->getRequest()->getQuery( "cate", "0" ); $status = $this->getRequest()->getQuery( "status", "online" ); $model = new ArtModel(); if( $data=$model->list( $pageNo, $pageSize, $cate, $status ) ) { echo json_encode( array( "errno"=>0, "errmsg"=>"", "data"=>$data, )); } else { echo json_encode( array("errno"=>-2012, "errmsg"=>"获取文章列表失败") ); } return TRUE; } public function _isAdmin(){ return true; } }
Artmodel:
<?php /** * @name UserModel * @desc 用户操作Model类 * @author pangee */ class ArtModel { public $errno = 0; public $errmsg = ""; private $_db; public function __construct() { $this->_db = new PDO("mysql:host=127.0.0.1;dbname=imooc;", "root", ""); $this->_db->setAttribute( PDO::ATTR_EMULATE_PREPARES, false ); } public function add( $title, $contents, $author, $cate, $artId=0 ) { $isEdit=false; if($artId!=0&&is_numeric($artId)){ $query = $this->_db->prepare("select count(*) from `art` where `id`= ? "); $query->execute( array($artId) ); $ret = $query->fetchAll(); if ( !$ret || count($ret)!=1 ) { $this->errno = -2004; $this->errmsg = "找不到你要编辑的文章!"; return false; } $isEdit = true; }else{ $query=$this->_db->prepare("select count(*) from 'cate' where 'id' =?"); $query->execute(array($cate)); $ret=$query->fetchAll(); if ( !$ret || $ret[0][0]==0 ) { $this->errno = -2005; $this->errmsg = "找不到对应ID的分类信息,cate id:".$cate.", 请先创建该分类。"; return false; } } $data=array($title,$contents,$author,intval($cate)); if(!$isEdit){ $query = $this->_db->prepare("insert into `art` (`title`,`contents`,`author`,`cate`) VALUES ( ?, ?, ?, ? )"); }else { $query = $this->_db->prepare("update `art` set `title`=?, `contents`=?, `author`=?, `cate`=? where `id`= ?"); $data[] = $artId; } $ret = $query->execute( $data ); if ( !$ret ) { $this->errno = -2006; $this->errmsg = "操作文章数据表失败, ErrInfo:".end($query->errorInfo()); return false; } if( !$isEdit ) { return intval($this->_db->lastInsertId()); } else { return intval($artId); } } public function del( $artId ){ $query = $this->_db->prepare("delete from `art` where `id`=? "); $ret = $query->execute( array(intval($artId)) ); if( !$ret ) { $this->errno = -2007; $this->errmsg = "删除失败, ErrInfo:".end($query->errorInfo()); return false; } return true; } public function status( $artId, $status="offline" ){ $query = $this->_db->prepare("update `art` set `status`=? where `id`=? "); $ret = $query->execute( array( $status, intval($artId)) ); if( !$ret ) { $this->errno = -2008; $this->errmsg = "更新文章状态失败, ErrInfo:".end($query->errorInfo()); return false; } return true; } public function get( $artId ){ $query = $this->_db->prepare("select `title`,`contents`,`author`,`cate`,`ctime`,`mtime`,`status` from `art` where `id`=? "); $status = $query->execute( array( intval($artId)) ); $ret = $query->fetchAll(); if( !$status || !$ret ) { $this->errno = -2009; $this->errmsg = "查询失败, ErrInfo:".end($query->errorInfo()); return false; } $artInfo = $ret[0]; /** * 获取分类信息 */ $query = $this->_db->prepare("select `name` from `cate` where `id`=?"); $query->execute( array( $artInfo['cate']) ); $ret = $query->fetchAll(); if( !$ret ) { $this->errno = -2010; $this->errmsg = "获取分类信息失败, ErrInfo:".end($query->errorInfo()); return false; } $artInfo['cateName'] = $ret[0]['name']; $data = array( 'id' => intval($artId), 'title'=> $artInfo['title'], 'contents'=> $artInfo['contents'], 'author'=> $artInfo['author'], 'cateName'=> $artInfo['cateName'], 'cateId'=> intval($artInfo['cate']), 'ctime'=> $artInfo['ctime'], 'mtime'=> $artInfo['mtime'], 'status'=> $artInfo['status'], ); return $data; } public function list($pageNo=0, $pageSize=10, $cate=0, $status="online"){ $start=$pageNo*$pageSize+($pageNo==0?0:1); if( $cate == 0 ) { $filter = array( $status, intval($start), intval($pageSize) ); $query = $this->_db->prepare("select `id`, `title`,`contents`,`author`,`cate`,`ctime`,`mtime`,`status` from `art` where `status`=? order by `ctime` desc limit ?,? "); } else { $filter = array( intval($cate), $status, intval($start), intval($pageSize) ); $query = $this->_db->prepare("select `id`, `title`,`contents`,`author`,`cate`,`ctime`,`mtime`,`status` from `art` where `cate`=? and `status`=? order by `ctime` desc limit ?,? "); } $stat=$query->execute($filter); $ret=$stat->fetchAll(); if(!$ret){ $this->errno = -2011; $this->errmsg = "获取文章列表失败, ErrInfo:".end($query->errorInfo()); return false; } $data = array(); $cateInfo = array(); foreach( $ret as $item ) { /** * 获取分类信息 */ if( isset($cateInfo[$item['cate']]) ){ $cateName = $cateInfo[$item['cate']]; } else { $query = $this->_db->prepare("select `name` from `cate` where `id`=?"); $query->execute( array( $item['cate']) ); $retCate = $query->fetchAll(); if( !$retCate ) { $this->errno = -2010; $this->errmsg = "获取分类信息失败, ErrInfo:".end($query->errorInfo()); return false; } $cateName = $cateInfo[$item['cate']] = $retCate[0]['name']; } /** * 正文太长则剪切 */ $contents = mb_strlen($item['contents'])>30 ? mb_substr($item['contents'], 0, 30)."..." : $item['contents']; $data[] = array( 'id' => intval($item['id']), 'title'=> $item['title'], 'contents'=> $contents, 'author'=> $item['author'], 'cateName'=> $cateName, 'cateId'=> intval($item['cate']), 'ctime'=> $item['ctime'], 'mtime'=> $item['mtime'], 'status'=> $item['status'], ); } return $data; } }