<?php

namespace app\controllers\mikrotik\ip\dns;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use yii\helpers\Url;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\mikrotik\MikrotikSideMenu;
use app\models\mikrotik\ip\dns\Dnscache;
use yii\data\ArrayDataProvider;

class DnscacheController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        ///access controll//////////////////////////////////////////////////////////
                        'actions' => ['index'], //what should be controlled - actionPing 
                        'allow' => true, //allow or deny this action   
                        'roles' => ['mikrotik_ipViewDNSCache'], //"controller's name"_"rule name"
                    ],
                    [
                        '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 Dnscache();
        $provider = $model->getAll();

        $provide = new ArrayDataProvider([
            'allModels' => $provider,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'type',
                    'data',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        return $this->render('/mikrotik/ip/dns/cache', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'provider' => $provide,
        ]);
    }

    public function actionFlush() {

        $model = new Dnscache();
        $model->flush();
        return $this->redirect(Url::toRoute('/mikrotik/ip/dns/dnscache/index'));
    }

}
