<?php

namespace app\controllers\mikrotik\ip\snmp;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use Yii;
use yii\helpers\Url;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\data\ArrayDataProvider;
use app\models\mikrotik\MikrotikSideMenu;
use app\models\mikrotik\ip\snmp\Communities;

class CommunitiesController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipViewSNMPCommunities'],
                    ],
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipCreateSNMPCommunities'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDeleteSNMPCommunities'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipUpdateSNMPCommunities'],
                    ],
                    [
                        'actions' => [],
                        'allow' => true,
                        'roles' => ['admin'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    public function actions() {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function actionIndex() {
        $menu = new MikrotikSideMenu();
        $model = new Communities();
        $provider = $model->getAll();

        $provide = new ArrayDataProvider([
            'allModels' => $provider,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'address',
                    'security',
                    'read_access',
                    'write_access',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);


        return $this->render('/mikrotik/ip/snmp/communities', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'provider' => $provide,
        ]);
    }

    public function actionDelete() {
        $id = Yii::$app->request->get('id');
        $model = new Communities();
        $model->delete($id);
        return $this->redirect(Url::toRoute('mikrotik/ip/snmp/communities/index'));
    }

    public function actionCreate() {
        $menu = new MikrotikSideMenu();
        $model = new Communities();

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

            $model->create();
            return $this->redirect(Url::toRoute('mikrotik/ip/snmp/communities/index'));
        }

        return $this->render('/mikrotik/ip/snmp/communitiesadd', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'create',
        ]);
    }

    public function actionUpdate() {
        $menu = new MikrotikSideMenu();
        $model = new Communities();
        $id = Yii::$app->request->get('id');
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $model->update($id);
            return $this->redirect(Url::toRoute('mikrotik/ip/snmp/communities/index'));
        }

        $model->get($id);

        return $this->render('/mikrotik/ip/snmp/communitiesadd', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'update',
        ]);
    }

}
