AuthorsController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace manager\controllers;
  3. use app\models\Authors;
  4. use app\models\AuthorsSearch;
  5. use yii\web\Controller;
  6. use yii\web\NotFoundHttpException;
  7. use yii\filters\VerbFilter;
  8. /**
  9. * AuthorsController implements the CRUD actions for Authors model.
  10. */
  11. class AuthorsController extends BaseController
  12. {
  13. /**
  14. * @inheritDoc
  15. */
  16. public function behaviors()
  17. {
  18. return array_merge(
  19. parent::behaviors(),
  20. [
  21. 'verbs' => [
  22. 'class' => VerbFilter::className(),
  23. 'actions' => [
  24. 'delete' => ['POST'],
  25. ],
  26. ],
  27. ]
  28. );
  29. }
  30. /**
  31. * Lists all Authors models.
  32. *
  33. * @return string
  34. */
  35. public function actionIndex()
  36. {
  37. $searchModel = new AuthorsSearch();
  38. $dataProvider = $searchModel->search($this->request->queryParams);
  39. $dataProvider->query->orderby('active,name');
  40. return $this->render('index', [
  41. 'searchModel' => $searchModel,
  42. 'dataProvider' => $dataProvider,
  43. ]);
  44. }
  45. /**
  46. * Displays a single Authors model.
  47. * @param int $id id
  48. * @return string
  49. * @throws NotFoundHttpException if the model cannot be found
  50. */
  51. public function actionView($id)
  52. {
  53. return $this->render('view', [
  54. 'model' => $this->findModel($id),
  55. ]);
  56. }
  57. /**
  58. * Creates a new Authors model.
  59. * If creation is successful, the browser will be redirected to the 'view' page.
  60. * @return string|\yii\web\Response
  61. */
  62. public function actionCreate()
  63. {
  64. $model = new Authors();
  65. if ($this->request->isPost) {
  66. if ($model->load($this->request->post()) && $model->save()) {
  67. if (is_uploaded_file($_FILES['Authors']['tmp_name']['photo'])) {
  68. $model->saveImg($model->id, $_FILES['Authors']['tmp_name']['photo'], $_FILES['Authors']['type']['photo']);
  69. }
  70. return $this->redirect(['view', 'id' => $model->id]);
  71. }
  72. } else {
  73. $model->loadDefaultValues();
  74. }
  75. return $this->render('create', [
  76. 'model' => $model,
  77. ]);
  78. }
  79. /**
  80. * Updates an existing Authors model.
  81. * If update is successful, the browser will be redirected to the 'view' page.
  82. * @param int $id id
  83. * @return string|\yii\web\Response
  84. * @throws NotFoundHttpException if the model cannot be found
  85. */
  86. public function actionUpdate($id)
  87. {
  88. $model = $this->findModel($id);
  89. if ($this->request->isPost && $model->load($this->request->post()) && $model->save()) {
  90. if (is_uploaded_file($_FILES['Authors']['tmp_name']['photo'])) {
  91. $model->saveImg($model->id, $_FILES['Authors']['tmp_name']['photo'], $_FILES['Authors']['type']['photo']);
  92. }
  93. return $this->redirect(['view', 'id' => $model->id]);
  94. }
  95. return $this->render('update', [
  96. 'model' => $model,
  97. ]);
  98. }
  99. /**
  100. * Deletes an existing Authors model.
  101. * If deletion is successful, the browser will be redirected to the 'index' page.
  102. * @param int $id id
  103. * @return \yii\web\Response
  104. * @throws NotFoundHttpException if the model cannot be found
  105. */
  106. public function actionDelete($id)
  107. {
  108. $this->findModel($id)->delete();
  109. return $this->redirect(['index']);
  110. }
  111. /**
  112. * Finds the Authors model based on its primary key value.
  113. * If the model is not found, a 404 HTTP exception will be thrown.
  114. * @param int $id id
  115. * @return Authors the loaded model
  116. * @throws NotFoundHttpException if the model cannot be found
  117. */
  118. protected function findModel($id)
  119. {
  120. if (($model = Authors::findOne(['id' => $id])) !== null) {
  121. return $model;
  122. }
  123. throw new NotFoundHttpException('The requested page does not exist.');
  124. }
  125. }