DefaultController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 2016/12/15
  6. * Time: 下午2:05
  7. */
  8. namespace common\modules\book\backend\controllers;
  9. use backend\actions\Position;
  10. use common\modules\book\models\Book;
  11. use common\modules\book\models\BookChapter;
  12. use Yii;
  13. use yii\data\ActiveDataProvider;
  14. use yii\web\Controller;
  15. class DefaultController extends Controller
  16. {
  17. public function actions()
  18. {
  19. return [
  20. 'delete' => [
  21. 'class' => 'backend\\actions\\Delete',
  22. 'findModel' => function($id) {
  23. return Book::findOne($id);
  24. }
  25. ],
  26. 'move-chapter' => [
  27. 'class' => Position::className(),
  28. 'returnUrl' => request()->referrer,
  29. 'findModel' => function($id) {
  30. return BookChapter::findOne($id);
  31. }
  32. ]
  33. ];
  34. }
  35. public function actionIndex()
  36. {
  37. $dataProvider = new ActiveDataProvider(['query' => Book::find()]);
  38. return $this->render('index', [
  39. 'dataProvider' => $dataProvider
  40. ]);
  41. }
  42. public function actionCreate()
  43. {
  44. $model = new Book();
  45. if ($model->load(request()->post()) && $model->save()) {
  46. Yii::$app->session->setFlash('success', '保存成功');
  47. return $this->redirect('index');
  48. }
  49. return $this->render('create', [
  50. 'model' => $model
  51. ]);
  52. }
  53. public function actionView($id)
  54. {
  55. $model = Book::findOne($id);
  56. return $this->render('view', [
  57. 'model' => $model
  58. ]);
  59. }
  60. public function actionChapter($id)
  61. {
  62. $model = BookChapter::findOne($id);
  63. return $this->render('chapter', [
  64. 'model' => $model
  65. ]);
  66. }
  67. public function actionUpdate($id)
  68. {
  69. $model = Book::findOne($id);
  70. if ($model->load(request()->post()) && $model->save()) {
  71. Yii::$app->session->setFlash('success', '保存成功');
  72. return $this->redirect('index');
  73. }
  74. return $this->render('update', [
  75. 'model' => $model
  76. ]);
  77. }
  78. public function actionCreateChapter($id, $chapter_id = 0)
  79. {
  80. $model = new BookChapter();
  81. $model->book_id = $id;
  82. if ($chapter_id > 0) {
  83. $model->pid = $chapter_id;
  84. }
  85. if ($model->load(request()->post()) && $model->save()) {
  86. Yii::$app->session->setFlash('success', '保存成功');
  87. return $this->redirect(['update-chapter', 'id' => $model->id]);
  88. }
  89. return $this->render('create-chapter', [
  90. 'model' => $model
  91. ]);
  92. }
  93. public function actionUpdateChapter($id)
  94. {
  95. $model = BookChapter::findOne($id);
  96. if ($model->load(request()->post()) && $model->save()) {
  97. Yii::$app->session->setFlash('success', '保存成功');
  98. return $this->refresh();
  99. }
  100. return $this->render('update-chapter', [
  101. 'model' => $model
  102. ]);
  103. }
  104. public function actionDeleteChapter($id)
  105. {
  106. $model = BookChapter::findOne($id);
  107. $model->delete();
  108. return $this->redirect('index');
  109. }
  110. }