123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?php
- namespace backend\controllers;
- use common\models\MaterType;
- use yii;
- use yii\filters\AccessControl;
- /**
- * 建材商分类
- * @package backend\controllers
- */
- class MatertypeController extends BaseController
- {
- public $layout = 'iframe';
- const PAGESIZE = 20;//分页数
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::className(),
- 'rules' => [
- [
- 'actions' => [],
- 'allow' => true,
- 'roles' => ['@'],
- ],
- ],
- ],
- ];
- }
- /**
- *列表
- * @return string
- */
- public function actionIndex()
- {
- $models = MaterType::find();
- $pages = new yii\data\Pagination(["totalCount" => $models->count(), "pageSize" => self::PAGESIZE]);
- $models = $models->offset($pages->offset)->limit($pages->limit)->orderBy("sort ASC,id DESC")->all();
- return $this->render('index', ['models' => $models, 'pages' => $pages]);
- }
- /**
- * 添加,修改
- * @return string|yii\web\Response
- */
- public function actionAdd()
- {
- if(Yii::$app->request->isPost)
- {
- $id = Yii::$app->request->post('id');
- if(!empty($id)){
- $model = MaterType::find()->where(['id'=>$id])->one();
- $model->u_time =time();
- }
- else{
- $model = new MaterType();
- }
- $model->load(Yii::$app->request->post());
- if(!empty($model->status))
- $model->status = MaterType::STATUS_YES;
- else
- $model->status = MaterType::STATUS_NO;
- if($model->save()&&$model->validate())
- {
- Yii::$app->getSession()->setFlash('success', '操作成功');
- return $this->redirect(['matertype/index']);
- }else{
- Yii::$app->getSession()->setFlash('error', '操作失败');
- return $this->redirect(Yii::$app->request->referrer);
- }
- }
- $id = Yii::$app->request->get('id');
- if(!empty($id))
- {
- $model= MaterType::find()->where(['id'=>$id])->one();
- $name = "编辑";
- }else{
- $model =new MaterType();
- $name = "添加";
- }
- return $this->render('add',['model'=>$model,'name'=>$name]);
- }
- /**
- * 更新状态
- * @return string
- */
- public function actionUpdateStatus()
- {
- $id = Yii::$app->request->post('id');
- $model = MaterType::find()->where(['id'=>$id])->one();
- if($model->status==MaterType::STATUS_YES)
- $model->status = MaterType::STATUS_NO;
- else
- $model->status = MaterType::STATUS_YES;
- if($model->save())
- $result = ['sign'=>1,'msg'=>'修改状态成功'];
- else
- $result = ['sign'=>0,'msg'=>'修改状态失败'];
- return json_encode($result);
- }
- }
|