2011-09-23 03:49:10 +00:00
< ? php
class Admin_NewsController extends Zend_Controller_Action
{
function preDispatch ()
{
$this -> db = Zend_Registry :: get ( 'db' );
$this -> view -> config = Zend_Registry :: get ( 'config' );
$this -> messenger = $this -> _helper -> getHelper ( 'FlashMessenger' );
$this -> view -> messages = $this -> messenger -> getMessages ();
}
function postDispatch ()
{
$this -> view -> messages = $this -> messenger -> getMessages ();
}
function indexAction ()
{
2012-03-27 07:02:36 +00:00
$sql = " select n.*,c.title as catlog from news_archives n left join news_catlog c on n.typeid=c.id order by n.ts_created desc limit 10 " ;
$rs = $this -> db -> query ( $sql );
$rows = $rs -> fetchAll ();
$this -> view -> news = $rows ;
$sql = " SELECT count(id) as c FROM news_archives " ;
$rs = $this -> db -> query ( $sql );
$row = $rs -> fetch ();
$this -> view -> totle = $row ;
$sql = " SELECT count(id) as c FROM news_catlog " ;
$rs = $this -> db -> query ( $sql );
$row = $rs -> fetch ();
$this -> view -> typec = $row ;
2011-09-23 03:49:10 +00:00
2011-10-21 08:12:04 +00:00
} //indexAction 首页
function newslistAction (){
2011-09-23 03:49:10 +00:00
2012-03-27 07:02:36 +00:00
$type = $this -> _request -> getParam ( 'type' );
if ( ! empty ( $type ))
{
$sql = " select n.*,c.title as catlog,c.url from news_archives n left join news_catlog c on n.typeid=c.id WHERE n.typeid=' $type ' order by n.ts_created desc " ;
} else
{
$sql = " select n.*,c.title as catlog,c.url from news_archives n left join news_catlog c on n.typeid=c.id order by n.ts_created desc " ;
}
2011-10-21 08:12:04 +00:00
$rs = $this -> db -> query ( $sql );
$rows = $rs -> fetchAll ();
2011-09-23 03:49:10 +00:00
2011-10-21 08:12:04 +00:00
$paginator = Zend_Paginator :: factory ( $rows );
$paginator -> setCurrentPageNumber ( $this -> _getParam ( 'page' ));
$paginator -> setItemCountPerPage ( $this -> view -> config -> page -> max );
$paginator -> setView ( $this -> view );
Zend_View_Helper_PaginationControl :: setDefaultViewPartial ( 'pagination_param.phtml' );
$this -> view -> paginator = $paginator ;
2011-09-23 03:49:10 +00:00
2012-03-27 07:02:36 +00:00
$sql = " select * from news_catlog order by displayorder desc " ;
$re = $this -> db -> query ( $sql );
$types = $re -> fetchAll ();
$this -> view -> types = $types ;
$this -> view -> type = $type ;
2011-10-21 08:12:04 +00:00
} //newslistAction 新闻列表
2011-09-23 03:49:10 +00:00
function catlogAction ()
{
$add = $this -> _request -> getParam ( 'add' );
$submit = $this -> _request -> getParam ( 'submit' );
$delete = $this -> _request -> getParam ( 'delete' );
$edit = $this -> _request -> getParam ( 'edit' );
if ( $add )
{
if ( empty ( $submit ))
$this -> _helper -> viewRenderer ( 'catlogadd' );
else {
$title = $this -> _request -> getParam ( 'ctitle' );
2012-03-27 07:02:36 +00:00
$url = $this -> _request -> getParam ( 'url' );
2011-09-23 03:49:10 +00:00
$keyword = $this -> _request -> getParam ( 'keyword' );
2011-09-28 08:29:01 +00:00
$description = $this -> _request -> getParam ( 'description' );
2011-09-23 03:49:10 +00:00
2012-03-27 07:02:36 +00:00
$sql = " insert into news_catlog (title,keyword,description,url) values (' $title ',' $keyword ',' $description ',' $url ') " ;
2011-09-23 03:49:10 +00:00
if ( $this -> db -> exec ( $sql ) > 0 )
{
$this -> messenger -> addMessage ( '提示信息:栏目添加成功!' );
$this -> _redirect ( '/admin/news/catlog' );
}
}
} //栏目添加
if ( $delete > 0 )
{
$sql = " delete from news_catlog where id=' $delete ' " ;
if ( $this -> db -> exec ( $sql ) > 0 )
{
$this -> messenger -> addMessage ( '提示信息:栏目删除成功!' );
$this -> _redirect ( '/admin/news/catlog' );
}
} //栏目删除
if ( $edit > 0 )
{
$title = $this -> _request -> getParam ( 'ctitle' );
2012-03-27 07:02:36 +00:00
$url = $this -> _request -> getParam ( 'url' );
2011-09-23 03:49:10 +00:00
$keyword = $this -> _request -> getParam ( 'keyword' );
2011-09-28 08:29:01 +00:00
$description = $this -> _request -> getParam ( 'description' );
$displayorder = $this -> _request -> getParam ( 'displayorder' );
2011-09-23 03:49:10 +00:00
2012-03-27 07:02:36 +00:00
$sql = " update news_catlog set title=' $title ',keyword=' $keyword ',description=' $description ',displayorder=' $displayorder ',url=' $url ' where id=' $edit ' " ;
2011-09-23 03:49:10 +00:00
if ( $this -> db -> exec ( $sql ) > 0 )
{
$this -> messenger -> addMessage ( '提示信息:栏目编辑成功!' );
$this -> _redirect ( '/admin/news/catlog' );
}
} //栏目编辑
else
{
2012-03-27 07:02:36 +00:00
$sql = " select * from news_catlog order by displayorder asc " ;
2011-09-23 03:49:10 +00:00
$re = $this -> db -> query ( $sql );
$catlogs = $re -> fetchAll ();
$this -> view -> catlogs = $catlogs ;
} //栏目列表
} //栏目管理
function newsaddAction ()
{
2011-10-10 06:30:51 +00:00
$id = $this -> _request -> getParam ( 'id' );
if ( $id > 0 )
{
2012-03-27 07:02:36 +00:00
$sql = " SELECT arc.id,arc.title,arc.ts_published,arc.typeid,cat.url,cat.id as typeid from news_archives arc
LEFT JOIN news_catlog cat ON arc . typeid = cat . id
WHERE arc . id = $id " ;
2011-10-10 06:30:51 +00:00
$rs = $this -> db -> query ( $sql );
$rows = $rs -> fetch ();
2012-03-27 07:02:36 +00:00
if ( $rows [ 'ts_published' ] > time ())
2011-10-10 06:30:51 +00:00
{
2012-03-27 07:02:36 +00:00
$title = " 《 { $rows [ 'title' ] } 》将在 " . date ( 'Y-m-d H:i' , $rows [ 'ts_published' ]) . " 发布 " ;
2011-10-10 06:30:51 +00:00
}
else
{
$title = " 《 { $rows [ 'title' ] } 》发布成功! " ;
}
$this -> view -> title = $title ;
$this -> view -> infos = $rows ;
$this -> _helper -> viewRenderer ( 'newsaddok' );
}
2011-09-23 03:49:10 +00:00
2011-09-28 08:29:01 +00:00
$sql = " select * from news_catlog order by displayorder desc " ;
2011-09-23 03:49:10 +00:00
$re = $this -> db -> query ( $sql );
$types = $re -> fetchAll ();
2011-10-10 06:30:51 +00:00
$this -> view -> types = $types ;
2011-09-23 03:49:10 +00:00
} //newsadd 新闻添加
2012-03-27 07:02:36 +00:00
function replacehtml ( $html )
{
$newString = htmlentities ( $html , ENT_QUOTES , " UTF-8 " );
return $newString ;
}
2011-10-09 10:15:50 +00:00
function archivesaddAction ()
{
$this -> _helper -> layout -> disableLayout ();
$this -> _helper -> viewRenderer -> setNoRender ();
$datavar = array (
'title' , 'writer' , 'source' , 'image' , 'body' , 'typeid' , 'pub' , 'pubtimer' , 'pubtime' , 'description' , 'keyword'
);
foreach ( $_POST as $k => $v )
{
if ( in_array ( $k , $datavar ))
{
$$k = $v ;
}
}
$msg = array ();
if ( empty ( $title )) $msg [] = " 标题不能为空 " ;
2012-03-27 07:02:36 +00:00
if ( strlen ( $title ) > 200 ) $msg [] = " 标题长度不能超过200个字符 " ;
2011-10-09 10:15:50 +00:00
if ( strlen ( $writer ) > 50 ) $msg [] = " 作者长度不能超过50个字符 " ;
if ( strlen ( $keyword ) > 200 ) $msg [] = " 关键词长度不能超过200个字符, 现在输入了 " . strlen ( $keyword ) . " 个字符 " ;
2012-03-27 07:02:36 +00:00
if ( strlen ( $description ) > 500 ) $msg [] = " 内容描述不能超过200个字符, 现在输入了 " . strlen ( $description ) . " 个字符 " ;
2011-10-09 10:15:50 +00:00
if ( empty ( $body )) $msg [] = " 请填写内容 " ;
if ( $typeid == 0 ) $msg [] = " 请选择栏目 " ;
if ( count ( $msg ) > 0 )
{
echo ' < div class = " box box-error " > 发布失败 :</ div >
< div class = " box box-error-msg " >
< ol > ' ;
foreach ( $msg as $v )
{
echo '<li>' . $v . '</li>' ;
}
echo ' </ ol >
</ div >
' ;
}
else
{
if ( ! empty ( $pubtimer ))
{
2012-03-27 07:02:36 +00:00
$pubtime = date ( " Y-m-d H:i:s " , strtotime ( $pubtime ));
2011-10-10 06:30:51 +00:00
}
else
{
2012-03-27 07:02:36 +00:00
$pubtime = date ( " Y-m-d H:i:s " , time ());
2011-10-09 10:15:50 +00:00
}
$date = array (
2012-03-27 07:02:36 +00:00
'title' => $this -> db -> quote ( $this -> replacehtml ( $title )),
'writer' => $this -> db -> quote ( $this -> replacehtml ( $writer )),
'keyword' => $this -> db -> quote ( $this -> replacehtml ( $keyword )),
'description' => $this -> db -> quote ( $this -> replacehtml ( $description )),
'image' => $image ,
2011-10-09 10:15:50 +00:00
'pubtime' => $pubtime ,
'source' => $source ,
'typeid' => $typeid ,
2011-10-10 06:30:51 +00:00
'pub' => $pub ,
2012-03-27 07:02:36 +00:00
'body' => $this -> db -> quote ( $body )
2011-10-09 10:15:50 +00:00
);
2011-10-10 06:30:51 +00:00
2012-03-27 07:02:36 +00:00
$sql = " INSERT INTO news_archives (title,author,keyword,description,image,ts_published,source,typeid,pub)
VALUES (
" . $date['title'] . " ,
" . $date['writer'] . " ,
" . $date['keyword'] . " ,
" . $date['description'] . " ,
2011-10-10 06:30:51 +00:00
'{$date[' image ']}' ,
'{$date[' pubtime ']}' ,
'{$date[' source ']}' ,
'{$date[' typeid ']}' ,
'{$date[' pub ']}' )
RETURNING id
" ;
try {
$sth = $this -> db -> prepare ( $sql );
if ( $sth -> execute ())
2011-10-09 10:15:50 +00:00
{
2011-10-10 06:30:51 +00:00
$temp = $sth -> fetch ( PDO :: FETCH_ASSOC );
2012-03-27 07:02:36 +00:00
$sql = " INSERT INTO news_archivesaddon (id,body) values (' { $temp [ 'id' ] } ', { $date [ 'body' ] } ) " ;
2011-10-10 06:30:51 +00:00
if ( $this -> db -> exec ( $sql ) > 0 )
{
echo ' < div class = " box box-success " > 发布成功 !</ div >< script >
setTimeout ( " self.location= \ '/admin/news/newsadd/id/'. $temp['id'] .' \ ' " , 500 );
</ script > ' ;
}
else
{
$sql = " delete from news_archives where id= { $temp [ 'id' ] } " ;
$this -> db -> exec ( $sql );
echo '<div class="box box-success">发布失败!写入附加表出错,请联系管理员</div>' ;
}
2011-10-09 10:15:50 +00:00
}
2011-10-10 06:30:51 +00:00
} catch ( Exception $e ){
echo ' < div class = " box box-error " > 文章发布失败: </ div >
< div class = " box box-error-msg " >
< ol >
< li > '.$e->getMessage().' </ li >
</ ol >
</ div > ' ;
2012-03-27 07:02:36 +00:00
}
}
} // 文章发布
function newseditAction ()
{
$id = $this -> _request -> getParam ( 'id' );
$sql = " select * from news_archives arc
left join news_archivesaddon addon on addon . id = arc . id
where arc . id = $id
" ;
$rs = $this -> db -> query ( $sql );
$row = $rs -> fetch ();
$this -> view -> ev = $row ;
$sql = " select * from news_catlog order by displayorder desc " ;
$re = $this -> db -> query ( $sql );
$types = $re -> fetchAll ();
$this -> view -> types = $types ;
}
function archiveseditAction ()
{
$this -> _helper -> layout -> disableLayout ();
$this -> _helper -> viewRenderer -> setNoRender ();
$id = $this -> _request -> getParam ( 'id' );
if ( empty ( $id ))
{
echo ' < div class = " box box-error " > 发布失败 :</ div >
< div class = " box box-error-msg " >
< ol > ' ;
echo '<li>参数错误</li>' .
' </ ol >
</ div >
' ;
}
$datavar = array (
'title' , 'writer' , 'source' , 'image' , 'body' , 'typeid' , 'pub' , 'pubtimer' , 'pubtime' , 'description' , 'keyword'
);
foreach ( $_POST as $k => $v )
{
if ( in_array ( $k , $datavar ))
{
$$k = $v ;
2011-10-09 10:15:50 +00:00
}
2012-03-27 07:02:36 +00:00
}
$msg = array ();
if ( empty ( $title )) $msg [] = " 标题不能为空 " ;
if ( strlen ( $title ) > 200 ) $msg [] = " 标题长度不能超过200个字符 " ;
if ( strlen ( $writer ) > 50 ) $msg [] = " 作者长度不能超过50个字符 " ;
if ( strlen ( $keyword ) > 200 ) $msg [] = " 关键词长度不能超过200个字符, 现在输入了 " . strlen ( $keyword ) . " 个字符 " ;
if ( strlen ( $description ) > 500 ) $msg [] = " 内容描述不能超过500个字符, 现在输入了 " . strlen ( $description ) . " 个字符 " ;
if ( empty ( $body )) $msg [] = " 请填写内容 " ;
if ( $typeid == 0 ) $msg [] = " 请选择栏目 " ;
if ( count ( $msg ) > 0 )
{
echo ' < div class = " box box-error " > 发布失败 :</ div >
< div class = " box box-error-msg " >
< ol > ' ;
foreach ( $msg as $v )
{
echo '<li>' . $v . '</li>' ;
}
echo ' </ ol >
</ div >
' ;
}
else
{
$pubtime = date ( " Y-m-d H:i:s " , strtotime ( $pubtime ));
$date = array (
'title' => $this -> replacehtml ( $title ),
'writer' => $this -> replacehtml ( $writer ),
'keyword' => $this -> replacehtml ( $keyword ),
'description' => $this -> replacehtml ( $description ),
'image' => $image ,
'pubtime' => $pubtime ,
'source' => $this -> replacehtml ( $source ),
'typeid' => $typeid ,
'pub' => $pub ,
'body' => $this -> db -> quote ( $body )
);
2011-10-10 06:30:51 +00:00
2012-03-27 07:02:36 +00:00
$sql = " UPDATE news_archives SET
title = '{$date[' title ']}' ,
author = '{$date[' writer ']}' ,
keyword = '{$date[' keyword ']}' ,
description = '{$date[' description ']}' ,
image = '{$date[' image ']}' ,
ts_published = '{$date[' pubtime ']}' ,
source = '{$date[' source ']}' ,
typeid = '{$date[' typeid ']}' ,
pub = '{$date[' pub ']}'
WHERE id = '$id'
" ;
2011-10-10 06:30:51 +00:00
2012-03-27 07:02:36 +00:00
try {
$sth = $this -> db -> prepare ( $sql );
if ( $sth -> execute ())
{
$sql = " UPDATE news_archivesaddon SET body= " . $date [ 'body' ] . " WHERE id=' $id ' " ;
if ( $this -> db -> exec ( $sql ) > 0 )
{
echo ' < div class = " box box-success " > 发布成功 !</ div >< script >
setTimeout ( " self.location= \ '/admin/news/newsadd/id/'. $id .' \ ' " , 500 );
</ script > ' ;
}
else
{
$sql = " delete from news_archives where id=' $id ' " ;
$this -> db -> exec ( $sql );
echo '<div class="box box-success">发布失败!写入附加表出错,请联系管理员</div>' ;
}
}
} catch ( Exception $e ){
echo ' < div class = " box box-error " > 文章发布失败: </ div >
2011-10-09 10:15:50 +00:00
< div class = " box box-error-msg " >
< ol >
2012-03-27 07:02:36 +00:00
< li > '.$e->getMessage().' </ li >
2011-10-09 10:15:50 +00:00
</ ol >
2012-03-27 07:02:36 +00:00
</ div > ' ;
}
}
} // 文章编辑
function deleteAction (){
2011-10-09 10:15:50 +00:00
2012-03-27 07:02:36 +00:00
$id = $this -> _request -> getParam ( 'id' );
$sql = " DELETE FROM news_archives WHERE id=' $id ' " ;
$sql2 = " DELETE FROM news_archivesaddon WHERE id=' $id ' " ;
if ( $this -> db -> exec ( $sql ))
$this -> db -> exec ( $sql2 );
$this -> messenger -> addMessage ( '提示信息:文章删除成功!' );
$this -> _redirect ( '/admin/news/newslist' );
} //文章删除
2011-09-23 03:49:10 +00:00
}