<?php

namespace app\models\mikrotik;

use yii\base\Model;
use Yii;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use PEAR2\Net\RouterOS;
use app\models\mikrotik\MikrotikConnect;

class Interfaces extends Model {

    private $id;
    private $name;
    private $l2mtu;
    private $running;
    private $disabled;
    private $type;

    public function setInterface($id) {
        $connect = MikrotikConnect::Instance();
        $printRequest = new RouterOS\Request('/interface/getall');
        $printRequest->setQuery(RouterOS\Query::where('.id', $id));
        $response = $connect->getConnection()->sendSync($printRequest);

        $this->id = $response->getProperty('.id');
        $this->name = $response->getProperty('name');
        $this->l2mtu = $response->getProperty('l2mtu');
        $this->running = $response->getProperty('running');
        $this->disabled = $response->getProperty('disabled');
        $this->type = $response->getProperty('type');

        return;
    }

    public function getInterface() {
        $ret['.id'] = $this->id;
        $ret['name'] = $this->name;
        $ret['l2mtu'] = $this->l2mtu;
        $ret['running'] = $this->running;
        $ret['disabled'] = $this->disabled;
        $ret['type'] = $this->type;

        return $ret;
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/getall');
        //$getRequest->setArgument('.proplist', 'mac-address,comment'); //vrati pouze mac a comment
        $responses = $connect->getConnection()->sendSync($getRequest);
        foreach ($responses as $response) {

            if ($response->getProperty('.id') != null) {
                $ret['.id'] = $response->getProperty('.id');
                $ret['name'] = $response->getProperty('name');
                $ret['l2mtu'] = $response->getProperty('l2mtu');
                $ret['running'] = $response->getProperty('running');
                $ret['disabled'] = $response->getProperty('disabled');
                $ret['type'] = $response->getProperty('type');
                $type[] = $ret;
            }
        }

        return $type;
    }

    public function getNames() {

        $connect = MikrotikConnect::Instance();
        $printRequest = new RouterOS\Request('/interface/getall');
        $responses = $connect->getConnection()->sendSync($printRequest);
        foreach ($responses as $response) {

            if ($response->getProperty('.id') != null) {
                $ret[] = $response->getProperty('name');
            }
        }
        return $ret;
    }

    public function disable($id) {
        if ($this->disabled == 'true') {
            $action = 'enable';
        } else {
            $action = 'disable';
        }
        $connect = MikrotikConnect::Instance();
        $printRequest = new RouterOS\Request('/interface/' . $action);
        $printRequest->setArgument('numbers', $id);
        $response = $connect->getConnection()->sendSync($printRequest);

        return $response;
    }

    public function getType($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/getall');
        $getRequest->setArgument('.proplist', 'type');
        $getRequest->setQuery(RouterOS\Query::where('.id', $id));
        $response = $connect->getConnection()->sendSync($getRequest);
        return $response->getProperty('type');
    }

    public function getName($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/getall');
        $getRequest->setArgument('.proplist', 'name');
        $getRequest->setQuery(RouterOS\Query::where('.id', $id));
        $response = $connect->getConnection()->sendSync($getRequest);
        return $response->getProperty('name');
    }

    public function names() {

        $connect = MikrotikConnect::Instance();
        $printRequest = new RouterOS\Request('/interface/getall');
        $responses = $connect->getConnection()->sendSync($printRequest);
        foreach ($responses as $response) {

            if ($response->getProperty('.id') != null) {
                $ret[$response->getProperty('name')] = $response->getProperty('name');
            }
        }
        return $ret;
    }

}
?> 
