Blame view

app/modules/admin/controllers/CatalogController.php 1.96 KB
bf807468   Alex Savenko   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  <?php

  

  namespace app\modules\admin\controllers;

  

  use Yii;

  use yii\web\Controller;

  use yii\filters\AccessControl;

  use yii\filters\VerbFilter;

  use yii\data\ActiveDataProvider;

  use yii\data\ArrayDataProvider;

  use app\modules\admin\models\Catalog;

  

  

  class CatalogController extends Controller

  {

      public function behaviors()

      {

          return [

              'access' => [

                  'class' => AccessControl::className(),

                  //'only' => ['logout','index'],

                  'rules' => [

                      [

                          'actions' => ['index','save','delete'],

                          'allow' => true,

                          'roles' => ['admin'],

                      ],					

                  ],

              ],

              'verbs' => [

                  'class' => VerbFilter::className(),

                  'actions' => [

                      'logout' => ['post'],

                  ],

              ],

          ];

      }

  	

      public function actionIndex()

      {

  	$model = new Catalog;

          

          $dataCatalog = new ArrayDataProvider([

              'allModels' => $model->getDataTree(),

              'sort' => [

                  'attributes' => ['id'],

              ],

              'pagination' => [

                  'pageSize' => 100,

              ]

              ]);

                  

          return $this->render('index',['dataCatalog'=>$dataCatalog]);

      }

  	

      public function actionSave()

      {

  		$model = (!empty($_GET['id'])) ? Catalog::findOne($_GET['id']) : new Catalog;

  

  		if ($model->load(Yii::$app->request->post()) && $model->save()) {

  		

  			return Yii::$app->response->redirect(['/admin/catalog/index']);

  		}

          return $this->render('save',['model'=>$model]);

      }

  

  	public function actionDelete(){

  		$model = Catalog::findOne($_GET['id']);

  		$model->delete();

          Catalog::deleteAll(['parent_id' => $_GET['id']]);

  		return Yii::$app->response->redirect(['/admin/catalog/index']);

  	}	

  }