123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- <?php
- namespace backend\controllers;
- use backend\models\search\ArticleSearch;
- use common\components\Controller;
- use common\models\Article;
- use common\models\ArticleModule;
- use common\models\Category;
- use Yii;
- use yii\base\Exception;
- use yii\data\ActiveDataProvider;
- use yii\filters\VerbFilter;
- use yii\helpers\Url;
- use yii\web\NotFoundHttpException;
- use yii\web\Response;
- /**
- * ArticleController implements the CRUD actions for Article model.
- */
- class ArticleController extends Controller
- {
- public function behaviors()
- {
- return [
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'delete' => ['post'],
- ],
- ],
- ];
- }
- public function actions()
- {
- return [
- 'ajax-update-field' => [
- 'class' => 'common\\actions\\AjaxUpdateFieldAction',
- 'allowFields' => ['status', 'is_top', 'is_hot', 'is_best'],
- 'findModel' => [$this, 'findModel']
- ],
- 'switcher' => [
- 'class' => 'backend\widgets\grid\SwitcherAction'
- ]
- ];
- }
- /**
- * Lists all Article models.
- *
- * @return mixed
- */
- public function actionIndex()
- {
- Url::remember();
- $searchModel = new ArticleSearch();
- $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
- return $this->render('index', [
- 'searchModel' => $searchModel,
- 'dataProvider' => $dataProvider,
- ]);
- }
- /**
- * 回收站列表
- *
- * @return mixed
- */
- public function actionTrash()
- {
- $query = \common\models\Article::find()->onlyTrashed();
- $dataProvider = new ActiveDataProvider([
- 'query' => $query,
- 'sort' => [
- 'defaultOrder' => [
- 'id' => SORT_DESC
- ]
- ]
- ]);
- return $this->render('trash',[
- 'dataProvider' => $dataProvider
- ]);
- }
- /**
- * 还原
- * @return array
- * @throws NotFoundHttpException
- */
- public function actionReduction()
- {
- Yii::$app->response->format = Response::FORMAT_JSON;
- $id = Yii::$app->request->post('id');
- $model = Article::find()->where(['id' => $id])->onlyTrashed()->one();
- if(!$model) {
- throw new NotFoundHttpException('人才不存在!');
- }
- $model->restore();
- return [
- 'message' => '操作成功'
- ];
- }
- /**
- * 彻底删除
- * @return array
- * @throws NotFoundHttpException
- * @throws \Exception
- */
- public function actionHardDelete()
- {
- Yii::$app->response->format = Response::FORMAT_JSON;
- $id = Yii::$app->request->post('id');
- $model = Article::find()->where(['id' => $id])->onlyTrashed()->one();
- if(!$model) {
- throw new NotFoundHttpException('人才不存在!');
- }
- $model->delete();
- return [
- 'message' => '操作成功'
- ];
- }
- public function actionClear()
- {
- if (Article::deleteAll(['>', 'deleted_at', 0]) !== false) {
- Yii::$app->session->setFlash('success', '操作成功');
- return $this->redirect('trash');
- }
- throw new Exception('操作失败');
- }
- /**
- * Displays a single Article model.
- *
- * @param int $id
- *
- * @return mixed
- */
- public function actionView($id)
- {
- return $this->render('view', [
- 'model' => $this->findModel($id),
- ]);
- }
- /**
- * Creates a new Article model.
- * If creation is successful, the browser will be redirected to the 'view' page.
- *
- * @param string $module 人才类型
- * @return mixed
- */
- public function actionCreate($module = 'talent')
- {
- $model = new Article();
- $model->status = Article::STATUS_ACTIVE;
- $model->module = $module;
- $moduleModelClass = $model->findModuleClass($module);
- $moduleModel = new $moduleModelClass;
- $this->performAjaxValidation($model);
- // $this->performAjaxValidation($moduleModel);
- if (Yii::$app->request->isPost) {
- $transaction = Yii::$app->db->beginTransaction();
- try{
- $model->load(Yii::$app->request->post());
- $model->save();
- if($model->hasErrors()) {
- throw new Exception('操作失败');
- }
- // $moduleModel->load(Yii::$app->request->post());
- // $moduleModel->id = $model->id;
- // $moduleModel->save();
- // if($moduleModel->hasErrors()) {
- // throw new Exception('操作失败');
- // }
- $transaction->commit();
- Yii::$app->session->setFlash('success', '发布成功');
- } catch (\Exception $e) {
- $transaction->rollBack();
- Yii::$app->session->setFlash('error', $e->getMessage());
- }
- return $this->goBack();
- }
- $articleModules = ArticleModule::find()->all();
- $articleModuleItems = [];
- foreach($articleModules as $articleModule) {
- $articleModuleItem = [];
- $articleModuleItem['label'] = $articleModule->title;
- $articleModuleItem['url'] = ['/article/create', 'module' => $articleModule->name];
- $articleModuleItem['active'] = $module == $articleModule->name;
- $articleModuleItems[] = $articleModuleItem;
- }
- return $this->render('create', [
- 'model' => $model,
- 'moduleModel' => $moduleModel,
- 'module' => $module,
- 'articleModuleItems' => $articleModuleItems
- ]);
- }
- /**
- * Updates an existing Article model.
- * If update is successful, the browser will be redirected to the 'view' page.
- *
- * @param int $id
- *
- * @return mixed
- */
- public function actionUpdate($id, $module = null)
- {
-
- $model = Article::find()->where(['id' => $id])->one();
- if ($module) {
- $model->module = $module;
- }
- // $moduleModel = $model->data;
- $this->performAjaxValidation($model);
- // $this->performAjaxValidation($moduleModel);
- if (Yii::$app->request->isPost) {
- $transaction = Yii::$app->db->beginTransaction();
- try {
- $model->load(Yii::$app->request->post());
- $model->save();
- if($model->hasErrors()) {
- throw new Exception('操作失败');
- }
- // if ($moduleModel) {
- // $moduleModel->load(Yii::$app->request->post());
- // $moduleModel->save();
- // if($moduleModel->hasErrors()) {
- // throw new Exception('操作失败');
- // }
- // }
- $transaction->commit();
- Yii::$app->session->setFlash('success', '操作成功');
- } catch (\Exception $e) {
- $transaction->rollBack();
- Yii::$app->session->setFlash('error', $e->getMessage());
- }
- return $this->goBack();
- }
- $moduleModelClass = $model->findModuleClass($model->module);
- $moduleModel = new $moduleModelClass;
- $articleModules = ArticleModule::find()->all();
- $articleModuleItems = [];
- foreach($articleModules as $articleModule) {
- $articleModuleItem = [];
- $articleModuleItem['label'] = $articleModule->title;
- $articleModuleItem['url'] = ['/article/update', 'id' => $id, 'module' => $articleModule->name];
- $articleModuleItem['active'] = $model->module == $articleModule->name;
- $articleModuleItems[] = $articleModuleItem;
- }
- //渲染产业链
- $model->chain = explode(',', $model->chain);
- return $this->render('update', [
- 'model' => $model,
- 'moduleModel' => $moduleModel,
- 'module' => $model->module,
- 'articleModuleItems' => $articleModuleItems
- ]);
- }
- /**
- * Deletes an existing Article model.
- * If deletion is successful, the browser will be redirected to the 'index' page.
- *
- * @param int $id
- *
- * @return mixed
- */
- public function actionDelete($id)
- {
- $this->findModel($id)->softDelete();
- Yii::$app->session->setFlash('success', '操作成功');
- return $this->goBack();
- }
- /**
- * Finds the Article model based on its primary key value.
- * If the model is not found, a 404 HTTP exception will be thrown.
- *
- * @param int $id
- *
- * @return Article the loaded model
- *
- * @throws NotFoundHttpException if the model cannot be found
- */
- public function findModel($id)
- {
- if (($model = Article::find()->where(['id' => $id])->notTrashed()->one()) !== null) {
- return $model;
- } else {
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
- }
|