You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.3 KiB

  1. <?php
  2. namespace kcadmin\controllers;
  3. use common\models\Cat;
  4. use Yii;
  5. use common\models\Category;
  6. use common\models\CategorySearch;
  7. use yii\helpers\Html;
  8. use yii\web\Controller;
  9. use yii\web\NotFoundHttpException;
  10. use yii\filters\VerbFilter;
  11. /**
  12. * CategoryController implements the CRUD actions for Category model.
  13. */
  14. class CategoryController extends Controller
  15. {
  16. public $enableCsrfValidation = false;
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public function behaviors()
  21. {
  22. return [
  23. 'verbs' => [
  24. 'class' => VerbFilter::className(),
  25. 'actions' => [
  26. 'delete' => ['POST'],
  27. ],
  28. ],
  29. ];
  30. }
  31. public function actions()
  32. {
  33. return [
  34. 'test' => [
  35. 'class' => 'iron\actions\UploadAction',
  36. ]
  37. ];
  38. }
  39. public function actionExport()
  40. {
  41. $searchModel = new CategorySearch();
  42. $params = Yii::$app->request->queryParams;
  43. if ($params['page-type'] == 'all') {
  44. $dataProvider = $searchModel->allData($params);
  45. } else {
  46. $dataProvider = $searchModel->search($params);
  47. }
  48. \iron\widget\Excel::export([
  49. 'models' => $dataProvider->getModels(),
  50. 'format' => 'Xlsx',
  51. 'asAttachment' => true,
  52. 'fileName' => "category" . "-" . date('Y-m-d H/i/s', time()),
  53. 'columns' => Category::columns()
  54. ]);
  55. }
  56. /**
  57. * Lists all Category models.
  58. * @return mixed
  59. */
  60. public function actionIndex()
  61. {
  62. $searchModel = new CategorySearch();
  63. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  64. return $this->render('index', [
  65. 'searchModel' => $searchModel,
  66. 'dataProvider' => $dataProvider,
  67. 'columns' => Category::columns()
  68. ]);
  69. }
  70. /**
  71. * Displays a single Category model.
  72. * @param integer $id
  73. * @return mixed
  74. * @throws NotFoundHttpException if the model cannot be found
  75. */
  76. public function actionView($id)
  77. {
  78. return $this->render('view', [
  79. 'model' => $this->findModel($id),
  80. ]);
  81. }
  82. /**
  83. * Creates a new Category model.
  84. * If creation is successful, the browser will be redirected to the 'view' page.
  85. * @return mixed
  86. */
  87. public function actionCreate()
  88. {
  89. $model = new Category();
  90. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  91. return $this->redirect(['view', 'id' => $model->id]);
  92. }
  93. return $this->render('create', [
  94. 'model' => $model,
  95. ]);
  96. }
  97. /**
  98. * Updates an existing Category model.
  99. * If update is successful, the browser will be redirected to the 'view' page.
  100. * @param integer $id
  101. * @return mixed
  102. * @throws NotFoundHttpException if the model cannot be found
  103. */
  104. public function actionUpdate($id)
  105. {
  106. $model = $this->findModel($id);
  107. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  108. return $this->redirect(['view', 'id' => $model->id]);
  109. }
  110. return $this->render('update', [
  111. 'model' => $model,
  112. ]);
  113. }
  114. /**
  115. * Deletes an existing Category model.
  116. * If deletion is successful, the browser will be redirected to the 'index' page.
  117. * @param integer $id
  118. * @return mixed
  119. * @throws NotFoundHttpException if the model cannot be found
  120. */
  121. public function actionDelete($id)
  122. {
  123. $this->findModel($id)->delete();
  124. return $this->redirect(['index']);
  125. }
  126. /**
  127. *
  128. */
  129. public function actionDeletes()
  130. {
  131. //获取前端post的记录id
  132. $ids = Yii::$app->request->post('ids');
  133. }
  134. /**
  135. * Finds the Category model based on its primary key value.
  136. * If the model is not found, a 404 HTTP exception will be thrown.
  137. * @param integer $id
  138. * @return Category the loaded model
  139. * @throws NotFoundHttpException if the model cannot be found
  140. */
  141. protected function findModel($id)
  142. {
  143. if (($model = Category::findOne($id)) !== null) {
  144. return $model;
  145. }
  146. throw new NotFoundHttpException('The requested page does not exist.');
  147. }
  148. }