308 lines
9.6 KiB
PHP
308 lines
9.6 KiB
PHP
<?php
|
||
|
||
# @Author: fm453
|
||
# @Date: 2024/7/16
|
||
# @updated: 下午3:55
|
||
# @Email: 1280880631@qq.com
|
||
|
||
namespace backend\controllers;
|
||
|
||
use addons\models\AcNewsCat;use Yii;
|
||
|
||
class NewscatController extends Common{
|
||
public $enableCsrfValidation = false;
|
||
public function actionIndex(){}
|
||
|
||
public function actionList()
|
||
{
|
||
global $_HI,$_FM;
|
||
$pid = $this->pid;
|
||
$session = Yii::$app->session;
|
||
$model = new AcNewsCat();
|
||
$where = [];
|
||
$where[]='and';
|
||
$where[] = ['=','pid',$pid];
|
||
$post = Yii::$app->request->post();
|
||
$get = Yii::$app->request->get();
|
||
$searchSession = 'newscat::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);
|
||
|
||
$_orderby = 'orderby DESC';
|
||
$res = $data->orderby($_orderby)->all();
|
||
|
||
$list = $cids = $pcids = [];
|
||
$cats = $pcats = [];
|
||
if ($res) {
|
||
foreach ($res as $r) {
|
||
if($r->pcat_id){
|
||
//重排数组,保留查询结果的排序
|
||
$pcids[] = $r->pcat_id; //父类
|
||
$cids[$r->pcat_id][] = $r->id; //子分类
|
||
}
|
||
$list[$r->id] = $r->toArray(); //内容按ID列出,以便后用
|
||
}
|
||
}
|
||
if($pcids){
|
||
foreach ($pcids as $pcid) {
|
||
$pcats[] = $list[$pcid];
|
||
}
|
||
foreach($cids as $pcid=>$ids){
|
||
foreach($ids as $id){
|
||
$cats[$pcid][] = $list[$id];
|
||
}
|
||
}
|
||
}
|
||
//不使用模板框架
|
||
// $this->layout = '@app/views/layouts/table';
|
||
$tpl = 'datatable';
|
||
$tpl = 'table';
|
||
return $this->render($tpl, [
|
||
'cats' => $cats,
|
||
'pcats' => $pcats,
|
||
'search'=>$search
|
||
]);
|
||
}
|
||
|
||
public function actionSelect()
|
||
{
|
||
$pid = $this->pid;
|
||
$id = Yii::$app->request->get('id');
|
||
$callback = !empty(Yii::$app->request->get('callback')) ? Yii::$app->request->get('callback') : 'newsCat';
|
||
if($id) $callback .= '-'.$id;
|
||
//不使用模板框架
|
||
// $this->layout = '@app/views/layouts/table';
|
||
$tpl = 'select';
|
||
return $this->render($tpl, [
|
||
'id' => $id,
|
||
'callback'=>$callback
|
||
]);
|
||
}
|
||
|
||
public function actionPcat(){
|
||
/**
|
||
* 选择上级分类
|
||
* @id [int] 当前来源子类ID
|
||
*/
|
||
$pid = $this->pid;
|
||
$id = Yii::$app->request->get('id');
|
||
$callback = !empty(Yii::$app->request->get('callback')) ? Yii::$app->request->get('callback') : 'newsCat';
|
||
if($id) $callback .= '-'.$id;
|
||
//不使用模板框架
|
||
// $this->layout = '@app/views/layouts/table';
|
||
$tpl = 'select';
|
||
return $this->render($tpl, [
|
||
'id' => $id,
|
||
'callback'=>$callback
|
||
]);
|
||
}
|
||
|
||
public function actionJson(){
|
||
$pid = $this->pid;
|
||
$session = Yii::$app->session;
|
||
$model = new AcNewsCat();
|
||
$where = [];
|
||
$where[]='and';
|
||
$where[] = ['=','pid',$pid];
|
||
$post = $this->postdata;
|
||
$get = Yii::$app->request->get();
|
||
$searchSession = 'newscat::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 = $newList = [];
|
||
if ($res) {
|
||
foreach ($res as $r) {
|
||
if($r->pcat_id){
|
||
//重排数组,保留查询结果的排序
|
||
$pcids[] = $r->pcat_id; //父类
|
||
$cids[$r->pcat_id][] = $r->id; //子分类,按父类分组
|
||
}else{
|
||
$cids['0'][] = $r->id; //无父类的
|
||
}
|
||
$ids[] = $r->id;
|
||
$list[$r->id] = $r->toArray(); //内容按ID列出,以便后用
|
||
}
|
||
}
|
||
|
||
if($pcids){
|
||
foreach ($pcids as $pcid) {
|
||
$pcats[] = $list[$pcid];
|
||
foreach($cids[$pcid] as $k=>$id){
|
||
$cats[$pcid][] = $list[$id];
|
||
}
|
||
}
|
||
foreach($cids as $pcid=>$ccids){
|
||
foreach($ccids as $k=>$id){
|
||
$cats[$pcid][] = $list[$id];
|
||
}
|
||
}
|
||
}
|
||
|
||
if(isset($post['id']) && (int)$post['id']){
|
||
$id = (int)$post['id'];
|
||
if($list[$id]){
|
||
$list[$id]['no_edit'] = $list[$id]['_isUnselectable'] = 1;
|
||
if($list[$id]['pcat_id']){
|
||
$list[$id]['_isSelected'] = 1;
|
||
}
|
||
}
|
||
}
|
||
foreach($ids as $id){
|
||
$row = $list[$id];
|
||
if($row['pcat_id']){
|
||
$row['pcat_title'] = $list[$row['pcat_id']]['title'];
|
||
}
|
||
$newList[] = $row;
|
||
}
|
||
|
||
//客户端表格类型 bootstrapTable 或 dataTable;
|
||
// $table_type = "dataTable";
|
||
$table_type = "bootstrapTable";
|
||
$data = [];
|
||
if($table_type=="dataTable"){
|
||
$data['draw'] = 1; //客户端表是否需要重绘
|
||
$data['recordsTotal'] = $totalCount;
|
||
$data['recordsFiltered'] = $totalCount;
|
||
$data['pcats'] = $pcats;
|
||
$data['cats'] = $cats;
|
||
$data['data'] = $newList;
|
||
} elseif($table_type=="bootstrapTable"){
|
||
$data['total'] = $totalCount;
|
||
$data['totalNotFiltered'] = $totalCount;
|
||
$data['pcats'] = $pcats;
|
||
$data['cats'] = $cats;
|
||
$data['rows'] = $newList;
|
||
}
|
||
|
||
header('Content-Type:application/json');
|
||
exit(json_encode($data,JSON_UNESCAPED_UNICODE)); //注意不要加强制object参数
|
||
}
|
||
|
||
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 = ['orderby'=>'orderby','deleted'=>'deleted','pcat_id'=>'pcat_id'];
|
||
foreach($cols as $col=>$key){
|
||
$data[$col] = isset($post[$key]) ? (int)$post[$key] : 0;
|
||
}
|
||
|
||
$cols = ['is_show'=>'is_show'];
|
||
foreach($cols as $col=>$key){
|
||
if(!$id){
|
||
$data[$col] = isset($post[$key]) ? (int)$post[$key] : 1;
|
||
}elseif(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 AcNewsCat();
|
||
if($id){
|
||
$_rec = $model::findOne($id);
|
||
if($_rec){
|
||
foreach($data as $key=>$val){
|
||
$_rec->$key = $val;
|
||
}
|
||
$_rec->id = $id;
|
||
$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;
|
||
|
||
$this->result($msg,$data,200);
|
||
}
|
||
|
||
public function actionAjax(){
|
||
$op = $_GET['op']; //操作标识,只允许客户端GET传入
|
||
$post = $this->postdata;
|
||
$res = [];
|
||
switch($op){
|
||
case 'del':
|
||
$msg = '软删除,假定成功';
|
||
$res = $post;
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
$this->result($msg,$res,200);
|
||
}
|
||
} |