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.

198 lines
5.0 KiB

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