<?php

namespace app\models\mikrotik\ip\upnp;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use yii\base\Model;
use Yii;
use PEAR2\Net\RouterOS;
use app\models\mikrotik\MikrotikConnect;

class Upnp extends Model {

    public $enabled;
    public $allow_disable_external_interface;
    public $show_dummy_rule;

    public function rules() {
        return [
            ['enabled', 'string'],
            ['allow_disable_external_interface', 'string'],
            ['show_dummy_rule', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/upnp/getall');
        $response = $connect->getConnection()->sendSync($getRequest);
        $this->enabled = (($response->getProperty('enabled') == 'true') ? true : false);
        $this->allow_disable_external_interface = (($response->getProperty('allow-disable-external-interface') == 'true') ? true : false);
        $this->show_dummy_rule = (($response->getProperty('show-dummy-rule') == 'true') ? true : false);
        return $this;
    }

    public function update() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/upnp/set');
        if ($this->enabled != "") {
            $setRequest->setargument('enabled', (($this->enabled == true) ? "true" : "false"));
        }
        if ($this->allow_disable_external_interface != "") {
            $setRequest->setargument('allow-disable-external-interface', (($this->allow_disable_external_interface == true) ? "true" : "false"));
        }
        if ($this->show_dummy_rule != "") {
            $setRequest->setargument('show-dummy-rule', (($this->show_dummy_rule == true) ? "true" : "false"));
        }
        $connect->getConnection()->sendSync($setRequest);
    }

}
