cxfoot/modules/admin/controllers/FaqController.php
2023-10-27 14:25:12 +08:00

315 lines
10 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2021年6月30日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\modules\admin\controllers;
use yii\helpers\VarDumper;
use app\modules\admin\behaviors\LoginBehavior;
use app\models\common\CommonFaqActionForm;
use app\models\common\CommonFaqEditForm;
use app\models\common\CommonFaqListForm;
use app\models\common\CommonCatActionForm;
use app\models\common\CommonCatEditForm;
use app\models\common\CommonCatListForm;
use app\models\Faq;
use app\models\FaqMeta;
use app\models\Cat;
class FaqController extends Controller
{
public function behaviors()
{
return array_merge(parent::behaviors(), [
'login' => [
'class' => LoginBehavior::className(),
],
]);
}
public function actionIndex()
{
if(\Yii::$app->request->isAjax){
$form = new CommonFaqListForm();
$form->attributes = \Yii::$app->request->get();
$form->cx_mch_id = $this->cx_mch_id;
$data = $form->search();
return $this->responseHandler($data);
}
$cat_list = Cat::find()
->where([
'cx_mch_id' => $this->cx_mch_id,
'is_delete' => 0,
'type' => Cat::TYPE_FAQ,
])
->orderBy(['sort' => SORT_ASC, 'created_at' => SORT_DESC])
->select('id,name')->asArray()->all();
return $this->render('index',[
'cat_list' => $cat_list
]);
}
public function actionEdit($id = 0)
{
$model = Faq::findOne([
'cx_mch_id' => $this->cx_mch_id,
'is_delete' => 0,
'id' => $id,
]);
if($model == null)
$model = new Faq();
if(\Yii::$app->request->isPost){
$form = new CommonFaqEditForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->model = $model;
$data = $form->save();
return $this->responseHandler($data);
}
$cat_list = Cat::find()
->where([
'cx_mch_id' => $this->cx_mch_id,
'is_delete' => 0,
'type' => Cat::TYPE_FAQ,
])
->select('id,name')->asArray()->all();
foreach($cat_list as $index => $item){
$checked = false;
$temp = FaqMeta::findOne([
'cx_mch_id' => $this->cx_mch_id,
'faq_id' => $model->isNewRecord ? 0 : $model->id,
'type' => FaqMeta::TYPE_CAT,
'value' => $item['id'],
'is_delete' => 0
]);
$checked = $temp == null ? false : true;
$item['checked'] = $checked;
$cat_list[$index] = $item;
}
$return_url = \Yii::$app->request->referrer;
return $this->render('edit', [
'model' => $model,
'return_url' => $return_url,
'cat_list' => $cat_list
]);
}
public function actionDelete()
{
if(!\Yii::$app->request->isPost){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new CommonFaqActionForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$data = $form->delete();
return $this->responseHandler($data);
}
public function actionPublish()
{
if(!\Yii::$app->request->isPost){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new CommonFaqActionForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$data = $form->publish();
return $this->responseHandler($data);
}
public function actionTrash()
{
if(!\Yii::$app->request->isPost){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new CommonFaqActionForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$data = $form->trash();
return $this->responseHandler($data);
}
public function actionCancelTrash()
{
if(!\Yii::$app->request->isPost){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new CommonFaqActionForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$data = $form->cancel_trash();
return $this->responseHandler($data);
}
public function actionCancelHot()
{
if(!\Yii::$app->request->isPost){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new CommonFaqActionForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$data = $form->cancel_hot();
return $this->responseHandler($data);
}
public function actionHot()
{
if(!\Yii::$app->request->isPost){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new CommonFaqActionForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$data = $form->hot();
return $this->responseHandler($data);
}
public function actionCat()
{
if(\Yii::$app->request->isAjax){
$form = new CommonCatListForm();
$form->attributes = \Yii::$app->request->get();
$form->cx_mch_id = $this->cx_mch_id;
$form->type = Cat::TYPE_FAQ;
$data = $form->search();
return $this->responseHandler($data);
}
return $this->render('cat');
}
public function actionCatEdit($id = 0)
{
$model = Cat::findOne([
'cx_mch_id' => $this->cx_mch_id,
'is_delete' => 0,
'id' => $id,
'type' => Cat::TYPE_FAQ,
]);
if($model == null){
$model = new Cat();
$model->status = 1;
}
if(\Yii::$app->request->isPost){
$form = new CommonCatEditForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->model = $model;
$form->type = Cat::TYPE_FAQ;
$data = $form->save();
return $this->responseHandler($data);
}
$cat_ids = [];
$cat_ids[] = $model->isNewRecord ? 0 : $model->id;
$parent_list = Cat::find()
->where([
'cx_mch_id' => $this->cx_mch_id,
'is_delete' => 0,
'type' => Cat::TYPE_FAQ,
])
->andWhere([
'NOT IN',
'id',
$cat_ids
])
->select('id,name')->asArray()->all();
$is_has_parent = false;//开启是否有父节点
$parent_list = $is_has_parent ? $parent_list : [];
array_unshift($parent_list,['id' => 0, 'name' => '无父节点']);
$return_url = \Yii::$app->request->referrer;
return $this->render('cat-edit', [
'model' => $model,
'return_url' => $return_url,
'parent_list' => $parent_list
]);
}
public function actionCatDelete()
{
if(!\Yii::$app->request->isPost){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new CommonCatActionForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->type = Cat::TYPE_FAQ;
$data = $form->delete();
return $this->responseHandler($data);
}
public function actionCatShow()
{
if(!\Yii::$app->request->isPost){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new CommonCatActionForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->type = Cat::TYPE_FAQ;
$data = $form->show();
return $this->responseHandler($data);
}
public function actionCatHide()
{
if(!\Yii::$app->request->isPost){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new CommonCatActionForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->type = Cat::TYPE_FAQ;
$data = $form->hide();
return $this->responseHandler($data);
}
public function actionCatOpen()
{
if(!\Yii::$app->request->isPost){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new CommonCatActionForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->type = Cat::TYPE_FAQ;
$data = $form->open();
return $this->responseHandler($data);
}
public function actionCatClose()
{
if(!\Yii::$app->request->isPost){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new CommonCatActionForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->type = Cat::TYPE_FAQ;
$data = $form->close();
return $this->responseHandler($data);
}
}