ctms/ctms-admin/controllers/NewsController.php
2025-04-10 23:19:13 +08:00

456 lines
13 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
#
# @Author: fm453
# @Date: 2024/7/16
# @updated: 下午3:55
# @Email: 1280880631@qq.com
namespace backend\controllers;
use Yii;
use yii\data\Pagination;
use yii\helpers\Url;
use addons\models\AcNewsCat;
use addons\models\AcNews;
class NewsController extends Common{
public $enableCsrfValidation = false;
public function actionIndex(){
}
public function actionList(){
$options = $this->_tableOptions();
$columns = $options['columns'];
$operator = $options['operator'];
$tpl = 'list';
return $this->render($tpl, [
'columns' => json_encode($columns,JSON_UNESCAPED_UNICODE),
'operator' => json_encode($operator,JSON_UNESCAPED_UNICODE)
]);
}
public function actionSelect()
{
$pid = $this->pid;
$id = Yii::$app->request->get('id');
$callback = !empty(Yii::$app->request->get('callback')) ? Yii::$app->request->get('callback') : 'news';
if($id) $callback .= '-'.$id;
$options = $this->_tableOptions();
$columns = $options['columns'];
$operator = $options['operator'];
//不使用模板框架
// $this->layout = '@app/views/layouts/table';
$tpl = 'select';
return $this->render($tpl, [
'id' => $id,
'callback'=>$callback,
'columns' => json_encode($columns,JSON_UNESCAPED_UNICODE),
'operator' => json_encode($operator,JSON_UNESCAPED_UNICODE)
]);
}
public function actionNew()
{
$pid = $this->pid;
$tpl = 'modify';
return $this->render(
$tpl,
[
'detail'=>[],
'id'=>0
]
);
}
public function actionEdit()
{
$pid = $this->pid;
$post = $this->postdata;
$id = Yii::$app->request->get('id');
$AcNews = new AcNews();
$news = $AcNews->findOne($id);
$detail = $news->toArray();
if($detail['cid']){
$AcNewsCat = new AcNewsCat();
$cat = $AcNewsCat->findOne($detail['cid']);
if($cat){
$detail['cat_title'] = $cat->title;
}
}
$tpl = 'modify';
return $this->render(
$tpl,
[
'detail'=>$detail,
'id'=>$id
]
);
}
/*复制文章*/
public function actionCopy()
{
$pid = $this->pid;
$post = $this->postdata;
$id = Yii::$app->request->get('id');
$AcNews = new AcNews();
$news = $AcNews->findOne($id);
$detail = $news->toArray();
$detail['id'] = 0;
$tpl = 'modify';
return $this->render(
$tpl,
[
'detail'=>$detail
]
);
}
public function actionSave(){
$post = $this->postdata;
$id = isset($post['id']) ? (int)$post['id'] : 0;
$pid = Yii::$app->session->get('pid');
//格式化数据
//目标键=》POST键
$cols = ['title'=>'title','des'=>'des'];
foreach($cols as $col=>$key){
if(isset($post[$key])){
$data[$col] = trim($post[$key]);
}elseif(!$id){
$data[$col] = '';
}
}
$cols = ['content'=>'editor'];
foreach($cols as $col=>$key){
if(isset($post[$key])){
$data[$col] = htmlspecialchars($post[$key]);
}
}
$cols = ['orderby'=>'orderby','deleted'=>'deleted','cid'=>'cid','liked'=>'liked','reading'=>'reading','viewed'=>'viewed'];
foreach($cols as $col=>$key){
if(isset($post[$key])){
$data[$col] = (int)$post[$key];
}
}
$cols = ['is_show'=>'is_show'];
foreach($cols as $col=>$key){
if(!$id){
$data[$col] = isset($post[$key]) ? (int)$post[$key] : 1;
}else{
if(isset($post[$col])){
$data[$col] = (int)$post[$key];
}
}
}
if(!$id){
$data['create_at'] = time();
}
$data['update_at'] = time();
$data['pid'] = $pid;
//保存
$msg = '数据保存失败';
$res = false;
$cpde = 0;
$model = new AcNews();
if($id){
$_rec = $model::findOne($id);
if($_rec){
foreach($data as $key=>$val){
$_rec->$key = $val;
}
$res = $_rec->save();
if($res){
$msg = '数据保存成功';
$code = 200;
}
}
}else{
foreach($data as $key=>$val){
$model->$key = $val;
}
$res = $model->save();
if($res){
$msg = '数据新增成功';
$id = $model->attributes['id']; //获取插入后id
$code = 200;
}
}
$data['id'] = $id;
$op = Yii::$app->request->get('op');
if($op){
return $this->redirect(['edit','id'=>$id]);
}
$this->result($msg,$data,200);
}
public function actionAjax(){
$op = $_GET['op']; //操作标识只允许客户端GET传入
$post = $this->postdata;
$msg = '删除失败';
if(!isset($post['id'])){
$this->result($msg,$post,200);
}
$id = $post['id'];
$model = new AcNews();
$data = $model->findOne($id);
if(!$data){
$msg = '数据不存在,操作失败';
$this->result($msg,$post,200);
}
if($data->pid != $this->pid){
$msg = '归属平台不匹配,禁止操作';
$this->result($msg,$post,200);
}
switch($op){
case 'del':
if($data->deleted >0){
$msg = '数据已被删除过,操作无效';
$this->result($msg,$post,200);
}
$data->deleted += 1;
$_res = $data->save();
$msg = $_res ? '删除成功' : '操作失败,数据更新没有成功!';
$res = $data;
break;
default:
break;
}
$this->result($msg,$res,200);
}
public function actionJson()
{
$res = $this->search();
$list = $res['list'];
$ids = $res['ids'];
$pcats = $res['pcats'];
$cats = $res['cats'];
$newList = [];
foreach($ids as $id){
$row = $list[$id];
$row['cat_title'] = '';
if($row['cid']>0){
if(isset($cats[$row['cid']])){
$row['cat_title'] = $cats[$row['cid']]['title'];
}
}
$newList[] = $row;
}
$data = [];
$data['total'] = $res['total'];
$data['totalNotFiltered'] = $res['total'];
$data['rows'] = $newList;
header('Content-Type:application/json');
exit(json_encode($data,JSON_UNESCAPED_UNICODE)); //注意不要加强制object参数
}
private function search(){
$pid = $this->pid;
$session = Yii::$app->session;
$model = new AcNews();
$where = [];
$where[]='and';
$where[] = ['=','pid',$pid];
$post = $this->postdata;
$get = Yii::$app->request->get();
$searchSession = 'news::search';
if (isset($_GET['reset']) && $_GET['reset']==1) {
$post['search'] = [];
}
if (isset($post['search'])) {
$search = $post['search'];
$session->set($searchSession, $search);
} elseif ($session->get($searchSession)) {
$search = $session->get($searchSession);
}
$search['title'] = isset($search['title']) ? trim($search['title']) : '';
if (!empty($search['title'])) {
$where[] = ['LIKE','title',$search['title']];
}
$deleted = 0;
if (isset($_GET['deleted'])) {
$deleted = isset($_GET['deleted']) ? ((int)$_GET['deleted']>=0 ? 1 : 0) : 0;
}
$where[]=['=','deleted',$deleted];
$data = $model->find()->where($where);
$totalCount = $data->count();
$_orderby = 'orderby DESC';
$res = $data->orderby($_orderby)->all();
$list = $ids = $cids = $pcids = [];
$cats = $pcats = [];
if ($res) {
foreach ($res as $r) {
if($r->cid){
$cids[] = $r->cid;
}
$ids[] = $r->id;
$list[$r->id] = $r->toArray(); //内容按ID列出以便后用
}
}
$CatModel = new AcNewsCat();
if($cids){
$res = $CatModel->find()->where(['IN','id',$cids])->all();
foreach ($res as $r) {
if($r->pcat_id){
$pcids[]= $r->pcat_id;
}
$cats[$r->id] = $r;
}
if($pcids){
$res = $CatModel->find()->where(['IN','id',$pcids])->all();
foreach ($res as $r) {
$pcats[$r->id] = $r;
}
}
}
$data = [];
$data['total'] = $totalCount;
$data['totalNotFiltered'] = $totalCount;
$data['cats'] = $cats;
$data['pcats'] = $pcats;
$data['list'] = $list;
$data['ids'] = $ids;
return $data;
}
private function _tableOptions(){
$columns = [];
$columns['id'] = [
'field'=>'id',
'title'=>'ID',
'footerFormatter'=>'CountTotalFooter',
'searchable'=>false
];
$columns['cat'] = [
'field'=>'cid',
'title'=>'分类',
'class'=>"resize_able",
'formatter'=>'catFormatter',
'filterControl'=>"select",
'sortable'=>true,
'searchable'=>true
];
$columns['title'] = [
'field'=>'title',
'title'=>'标题',
'class'=>"resize_able",
'editable'=>'textEditor',
'formatter'=>'textFormatter',
'searchFormatter'=>true,
'searchHighlightFormatter'=>'customSearchFmt',
'filterControl'=>"input",
'searchable'=>true
];
$columns['des'] = [
'field'=>'des',
'title'=>'简述',
'editable'=>'textEditor',
'formatter'=>'textFormatter',
'searchFormatter'=>true,
'searchHighlightFormatter'=>'customSearchFmt',
'searchable'=>true
];
$columns['orderby'] = [
'field'=>'orderby',
'title'=>'排序',
'editable'=>'textEditor',
'formatter'=>'numFormatter',
'sortable'=>true,
'searchable'=>true
];
$columns['viewCount'] = [
'field'=>'viewed',
'title'=>'浏览量',
'formatter'=>'numFormatter',
'footerFormatter'=>'NumCountFooter',
'sortable'=>true,
'searchable'=>false
];
$columns['likeCount'] = [
'field'=>'liked',
'title'=>'收藏量',
'formatter'=>'numFormatter',
'footerFormatter'=>'NumCountFooter',
'sortable'=>true,
'searchable'=>false
];
$columns['readingCount'] = [
'field'=>'reading',
'title'=>'在看',
'formatter'=>'numFormatter',
'footerFormatter'=>'NumCountFooter',
'sortable'=>true,
'searchable'=>false
];
$columns['isShow'] = [
'field'=>'is_show',
'title'=>'显示',
'editable'=>'textEditor',
'formatter'=>'yesnoFormatter',
'filterControl'=>"select",
'sortable'=>true,
'searchable'=>false
];
$columns['create_at'] = [
'field'=>'create_at',
'title'=>'创建日期',
'formatter'=>'dateFormatter',
'sortable'=>true,
'searchable'=>false
];
$columns['update_at'] = [
'field'=>'update_at',
'title'=>'更新时间',
'formatter'=>'timeFormatter',
'sortable'=>true,
'searchable'=>false
];
$operator = [];
$operator['showDetail'] = [
'class'=>'view',
'title'=>'概览',
'icon'=>'eye',
'func'=>false
];
$operator['save'] = [
'class'=>'save',
'title'=>'保存',
'icon'=>'save',
'func'=>false
];
$operator['remove'] = [
'class'=>'remove',
'title'=>'移除',
'icon'=>'trash',
'func'=>false
];
$operator['edit'] = [
'class'=>'href',
'title'=>'编辑',
'icon'=>'pencil',
'func'=>false
];
return [
'columns' => $columns,
'operator' => $operator
];
}
}