123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- /**
- * Created by PhpStorm.
- * User: NODELOG
- * Date: 2016/12/15
- * Time: 下午2:05
- */
- namespace common\modules\book\backend\controllers;
- use backend\actions\Position;
- use common\modules\book\models\Book;
- use common\modules\book\models\BookChapter;
- use Yii;
- use yii\data\ActiveDataProvider;
- use yii\web\Controller;
- class DefaultController extends Controller
- {
- public function actions()
- {
- return [
- 'delete' => [
- 'class' => 'backend\\actions\\Delete',
- 'findModel' => function($id) {
- return Book::findOne($id);
- }
- ],
- 'move-chapter' => [
- 'class' => Position::className(),
- 'returnUrl' => request()->referrer,
- 'findModel' => function($id) {
- return BookChapter::findOne($id);
- }
- ]
- ];
- }
- public function actionIndex()
- {
- $dataProvider = new ActiveDataProvider(['query' => Book::find()]);
- return $this->render('index', [
- 'dataProvider' => $dataProvider
- ]);
- }
- public function actionCreate()
- {
- $model = new Book();
- if ($model->load(request()->post()) && $model->save()) {
- Yii::$app->session->setFlash('success', '保存成功');
- return $this->redirect('index');
- }
- return $this->render('create', [
- 'model' => $model
- ]);
- }
- public function actionView($id)
- {
- $model = Book::findOne($id);
- return $this->render('view', [
- 'model' => $model
- ]);
- }
- public function actionChapter($id)
- {
- $model = BookChapter::findOne($id);
- return $this->render('chapter', [
- 'model' => $model
- ]);
- }
- public function actionUpdate($id)
- {
- $model = Book::findOne($id);
- if ($model->load(request()->post()) && $model->save()) {
- Yii::$app->session->setFlash('success', '保存成功');
- return $this->redirect('index');
- }
- return $this->render('update', [
- 'model' => $model
- ]);
- }
- public function actionCreateChapter($id, $chapter_id = 0)
- {
- $model = new BookChapter();
- $model->book_id = $id;
- if ($chapter_id > 0) {
- $model->pid = $chapter_id;
- }
- if ($model->load(request()->post()) && $model->save()) {
- Yii::$app->session->setFlash('success', '保存成功');
- return $this->redirect(['update-chapter', 'id' => $model->id]);
- }
- return $this->render('create-chapter', [
- 'model' => $model
- ]);
- }
- public function actionUpdateChapter($id)
- {
- $model = BookChapter::findOne($id);
- if ($model->load(request()->post()) && $model->save()) {
- Yii::$app->session->setFlash('success', '保存成功');
- return $this->refresh();
- }
- return $this->render('update-chapter', [
- 'model' => $model
- ]);
- }
- public function actionDeleteChapter($id)
- {
- $model = BookChapter::findOne($id);
- $model->delete();
- return $this->redirect('index');
- }
- }
|