MatertypeController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace backend\controllers;
  3. use common\models\MaterType;
  4. use yii;
  5. use yii\filters\AccessControl;
  6. /**
  7. * 建材商分类
  8. * @package backend\controllers
  9. */
  10. class MatertypeController extends BaseController
  11. {
  12. public $layout = 'iframe';
  13. const PAGESIZE = 20;//分页数
  14. public function behaviors()
  15. {
  16. return [
  17. 'access' => [
  18. 'class' => AccessControl::className(),
  19. 'rules' => [
  20. [
  21. 'actions' => [],
  22. 'allow' => true,
  23. 'roles' => ['@'],
  24. ],
  25. ],
  26. ],
  27. ];
  28. }
  29. /**
  30. *列表
  31. * @return string
  32. */
  33. public function actionIndex()
  34. {
  35. $models = MaterType::find();
  36. $pages = new yii\data\Pagination(["totalCount" => $models->count(), "pageSize" => self::PAGESIZE]);
  37. $models = $models->offset($pages->offset)->limit($pages->limit)->orderBy("sort ASC,id DESC")->all();
  38. return $this->render('index', ['models' => $models, 'pages' => $pages]);
  39. }
  40. /**
  41. * 添加,修改
  42. * @return string|yii\web\Response
  43. */
  44. public function actionAdd()
  45. {
  46. if(Yii::$app->request->isPost)
  47. {
  48. $id = Yii::$app->request->post('id');
  49. if(!empty($id)){
  50. $model = MaterType::find()->where(['id'=>$id])->one();
  51. $model->u_time =time();
  52. }
  53. else{
  54. $model = new MaterType();
  55. }
  56. $model->load(Yii::$app->request->post());
  57. if(!empty($model->status))
  58. $model->status = MaterType::STATUS_YES;
  59. else
  60. $model->status = MaterType::STATUS_NO;
  61. if($model->save()&&$model->validate())
  62. {
  63. Yii::$app->getSession()->setFlash('success', '操作成功');
  64. return $this->redirect(['matertype/index']);
  65. }else{
  66. Yii::$app->getSession()->setFlash('error', '操作失败');
  67. return $this->redirect(Yii::$app->request->referrer);
  68. }
  69. }
  70. $id = Yii::$app->request->get('id');
  71. if(!empty($id))
  72. {
  73. $model= MaterType::find()->where(['id'=>$id])->one();
  74. $name = "编辑";
  75. }else{
  76. $model =new MaterType();
  77. $name = "添加";
  78. }
  79. return $this->render('add',['model'=>$model,'name'=>$name]);
  80. }
  81. /**
  82. * 更新状态
  83. * @return string
  84. */
  85. public function actionUpdateStatus()
  86. {
  87. $id = Yii::$app->request->post('id');
  88. $model = MaterType::find()->where(['id'=>$id])->one();
  89. if($model->status==MaterType::STATUS_YES)
  90. $model->status = MaterType::STATUS_NO;
  91. else
  92. $model->status = MaterType::STATUS_YES;
  93. if($model->save())
  94. $result = ['sign'=>1,'msg'=>'修改状态成功'];
  95. else
  96. $result = ['sign'=>0,'msg'=>'修改状态失败'];
  97. return json_encode($result);
  98. }
  99. }