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.

172 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. /**
  40. * @author iron
  41. * 文件导出
  42. */
  43. public function actionExport()
  44. {
  45. $searchModel = new CategorySearch();
  46. $params = Yii::$app->request->queryParams;
  47. if ($params['page-type'] == 'all') {
  48. $dataProvider = $searchModel->allData($params);
  49. } else {
  50. $dataProvider = $searchModel->search($params);
  51. }
  52. \iron\widget\Excel::export([
  53. 'models' => $dataProvider->getModels(),
  54. 'format' => 'Xlsx',
  55. 'asAttachment' => true,
  56. 'fileName' => "category" . "-" . date('Y-m-d H/i/s', time()),
  57. 'columns' => Category::columns()
  58. ]);
  59. }
  60. /**
  61. * Lists all Category models.
  62. * @return mixed
  63. */
  64. public function actionIndex()
  65. {
  66. $searchModel = new CategorySearch();
  67. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  68. return $this->render('index', [
  69. 'searchModel' => $searchModel,
  70. 'dataProvider' => $dataProvider,
  71. 'columns' => Category::columns()
  72. ]);
  73. }
  74. /**
  75. * Displays a single Category model.
  76. * @param integer $id
  77. * @return mixed
  78. * @throws NotFoundHttpException if the model cannot be found
  79. */
  80. public function actionView($id)
  81. {
  82. return $this->render('view', [
  83. 'model' => $this->findModel($id),
  84. ]);
  85. }
  86. /**
  87. * Creates a new Category model.
  88. * If creation is successful, the browser will be redirected to the 'view' page.
  89. * @return mixed
  90. */
  91. public function actionCreate()
  92. {
  93. $model = new Category();
  94. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  95. return $this->redirect(['view', 'id' => $model->id]);
  96. }
  97. return $this->render('create', [
  98. 'model' => $model,
  99. ]);
  100. }
  101. /**
  102. * Updates an existing Category model.
  103. * If update is successful, the browser will be redirected to the 'view' page.
  104. * @param integer $id
  105. * @return mixed
  106. * @throws NotFoundHttpException if the model cannot be found
  107. */
  108. public function actionUpdate($id)
  109. {
  110. $model = $this->findModel($id);
  111. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  112. return $this->redirect(['view', 'id' => $model->id]);
  113. }
  114. return $this->render('update', [
  115. 'model' => $model,
  116. ]);
  117. }
  118. /**
  119. * Deletes an existing Category model.
  120. * If deletion is successful, the browser will be redirected to the 'index' page.
  121. * @param integer $id
  122. * @return mixed
  123. * @throws NotFoundHttpException if the model cannot be found
  124. */
  125. public function actionDelete($id)
  126. {
  127. $this->findModel($id)->delete();
  128. return $this->redirect(['index']);
  129. }
  130. /**
  131. *
  132. */
  133. public function actionDeletes()
  134. {
  135. //获取前端post的记录id
  136. $ids = Yii::$app->request->post('ids');
  137. }
  138. /**
  139. * Finds the Category model based on its primary key value.
  140. * If the model is not found, a 404 HTTP exception will be thrown.
  141. * @param integer $id
  142. * @return Category the loaded model
  143. * @throws NotFoundHttpException if the model cannot be found
  144. */
  145. protected function findModel($id)
  146. {
  147. if (($model = Category::findOne($id)) !== null) {
  148. return $model;
  149. }
  150. throw new NotFoundHttpException('The requested page does not exist.');
  151. }
  152. }