<?php

namespace app\models\mikrotik\routing\filter;

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 Filter extends Model {

    public $id;
    public $action;
    public $address_family;
    public $append_bgp_communities;
    public $append_route_targets;
    public $bgp_as_path;
    public $bgp_as_path_length;
    public $bgp_atomic_aggregate;
    public $bgp_communities;
    public $bgp_local_pref;
    public $bgp_med;
    public $bgp_origin;
    public $bgp_weight;
    public $chain;
    public $distance;
    public $invert_math;
    public $jump_target;
    public $locally_originated_bgp;
    public $match_chain;
    public $ospf_type;
    public $pref_src;
    public $prefix;
    public $prefix_length;
    public $protocol;
    public $route_comment;
    public $route_tag;
    public $route_target;
    public $routing_mark;
    public $scope;
    public $set_bgp_communities;
    public $set_bgp_local_pref;
    public $set_bgp_med;
    public $set_bgp_prepend;
    public $set_bgp_prepend_path;
    public $set_bgp_weight;
    public $set_check_gateway;
    public $set_disabled;
    public $set_distance;
    public $set_in_nexthop;
    public $set_in_nexthop_direct;
    public $set_in_nexthop_ipv6;
    public $set_in_nexthop_linklocal;
    public $set_out_nexthop;
    public $set_out_nexthop_ipv6;
    public $set_out_nexthop_linklocal;
    public $set_pref_src;
    public $set_route_comment;
    public $set_route_tag;
    public $set_route_targets;
    public $set_routing_mark;
    public $set_scope;
    public $set_target_scope;
    public $set_type;
    public $set_use_te_nexthop;
    public $site_of_origin;
    public $set_site_of_origin;
    public $target_scope;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['action', 'string'],
            ['append_bgp_communities', 'string'],
            ['append_route_targets', 'string'],
            ['bgp_as_path', 'string'],
            ['bgp_as_path_length', 'string'],
            ['bgp_atomic_aggregate', 'string'],
            ['bgp_communities', 'string'],
            ['bgp_local_pref', 'string'],
            ['bgp_med', 'string'],
            ['bgp_weight', 'string'],
            ['chain', 'string'],
            ['distance', 'string'],
            ['invert_math', 'string'],
            ['jump_target', 'string'],
            ['locally_originated_bgp', 'string'],
            ['match_chain', 'string'],
            ['ospf_type', 'string'],
            ['pref_src', 'string'],
            ['prefix', 'string'],
            ['prefix_length', 'string'],
            ['route_comment', 'string'],
            ['route_tag', 'string'],
            ['route_target', 'string'],
            ['routing_mark', 'string'],
            ['scope', 'string'],
            ['set_bgp_communities', 'string'],
            ['set_bgp_local_pref', 'string'],
            ['set_bgp_med', 'string'],
            ['set_bgp_prepend', 'string'],
            ['set_bgp_prepend_path', 'string'],
            ['set_bgp_weight', 'string'],
            ['set_check_gateway', 'string'],
            ['set_disabled', 'string'],
            ['set_distance', 'string'],
            ['set_in_nexthop', 'string'],
            ['set_in_nexthop_direct', 'string'],
            ['set_in_nexthop_ipv6', 'string'],
            ['set_in_nexthop_linklocal', 'string'],
            ['set_out_nexthop', 'string'],
            ['set_out_nexthop_ipv6', 'string'],
            ['set_out_nexthop_linklocal', 'string'],
            ['set_pref_src', 'string'],
            ['set_route_comment', 'string'],
            ['set_route_tag', 'string'],
            ['set_route_targets', 'string'],
            ['set_routing_mark', 'string'],
            ['set_scope', 'string'],
            ['set_target_scope', 'string'],
            ['set_type', 'string'],
            ['set_use_te_nexthop', 'string'],
            ['site_of_origin', 'string'],
            ['set_site_of_origin', 'string'],
            ['target_scope', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/filter/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Filter();
                $c->id = $response->getProperty('.id');
                $c->action = $response->getProperty('action');
                $c->address_family = $response->getProperty('address-family');
                $c->append_bgp_communities = $response->getProperty('append-bgp-communities');
                $c->append_route_targets = $response->getProperty('append-route-targets');
                $c->bgp_as_path = $response->getProperty('bgp-as-path');
                $c->bgp_as_path_length = $response->getProperty('bgp-as-path-length');
                $c->bgp_atomic_aggregate = $response->getProperty('bgp-atomic-aggregate');
                $c->bgp_communities = $response->getProperty('bgp-communities');
                $c->bgp_local_pref = $response->getProperty('bgp-local-pref');
                $c->bgp_med = $response->getProperty('bgp-med');
                $c->bgp_origin = $response->getProperty('bgp-origin');
                $c->bgp_weight = $response->getProperty('bgp-weight');
                $c->chain = $response->getProperty('chain');
                $c->distance = $response->getProperty('distance');
                $c->invert_math = $response->getProperty('invert-math');
                $c->jump_target = $response->getProperty('jump-target');
                $c->locally_originated_bgp = $response->getProperty('locally-originated-bgp');
                $c->match_chain = $response->getProperty('match-chain');
                $c->ospf_type = $response->getProperty('ospf-type');
                $c->pref_src = $response->getProperty('pref-src');
                $c->prefix = $response->getProperty('prefix');
                $c->prefix_length = $response->getProperty('prefix-length');
                $c->protocol = $response->getProperty('protocol');
                $c->route_comment = $response->getProperty('route-comment');
                $c->route_tag = $response->getProperty('route-tag');
                $c->route_target = $response->getProperty('route-target');
                $c->routing_mark = $response->getProperty('routing-mark');
                $c->scope = $response->getProperty('scope');
                $c->set_bgp_communities = $response->getProperty('set-bgp-communities');
                $c->set_bgp_local_pref = $response->getProperty('set-bgp-local-pref');
                $c->set_bgp_med = $response->getProperty('set-bgp-med');
                $c->set_bgp_prepend = $response->getProperty('set-bgp-prepend');
                $c->set_bgp_prepend_path = $response->getProperty('set-bgp-prepend-path');
                $c->set_bgp_weight = $response->getProperty('set-bgp-weight');
                $c->set_check_gateway = $response->getProperty('set-check-gateway');
                $c->set_disabled = $response->getProperty('set-disabled');
                $c->set_distance = $response->getProperty('set-distance');
                $c->set_in_nexthop = $response->getProperty('set-in-nexthop');
                $c->set_in_nexthop_direct = $response->getProperty('set-in-nexthop-direct');
                $c->set_in_nexthop_ipv6 = $response->getProperty('set-in-nexthop-ipv6');
                $c->set_in_nexthop_linklocal = $response->getProperty('set-in-nexthop-linklocal');
                $c->set_out_nexthop = $response->getProperty('set-out-nexthop');
                $c->set_out_nexthop_ipv6 = $response->getProperty('set-out-nexthop-ipv6');
                $c->set_out_nexthop_linklocal = $response->getProperty('set-out-nexthop-linklocal');
                $c->set_pref_src = $response->getProperty('set-pref-src');
                $c->set_route_comment = $response->getProperty('set-route-comment');
                $c->set_route_tag = $response->getProperty('set-route-tag');
                $c->set_route_targets = $response->getProperty('set-route-targets');
                $c->set_routing_mark = $response->getProperty('set-routing-mark');
                $c->set_scope = $response->getProperty('set-scope');
                $c->set_target_scope = $response->getProperty('set-target-scope');
                $c->set_type = $response->getProperty('set-type');
                $c->set_use_te_nexthop = $response->getProperty('set-use-te-nexthop');
                $c->site_of_origin = $response->getProperty('site-of-origin');
                $c->set_site_of_origin = $response->getProperty('set-site-of-origin');
                $c->target_scope = $response->getProperty('target-scope');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/filter/print');
        $getRequest->setQuery(RouterOS\Query::where('.id', $id));
        $responses = $connect->getConnection()->sendSync($getRequest);
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $this->id = $response->getProperty('.id');
                $this->action = $response->getProperty('action');
                $this->address_family = $response->getProperty('address-family');
                $this->append_bgp_communities = $response->getProperty('append-bgp-communities');
                $this->append_route_targets = $response->getProperty('append-route-targets');
                $this->bgp_as_path = $response->getProperty('bgp-as-path');
                $this->bgp_as_path_length = $response->getProperty('bgp-as-path-length');
                $this->bgp_atomic_aggregate = $response->getProperty('bgp-atomic-aggregate');
                $this->bgp_communities = $response->getProperty('bgp-communities');
                $this->bgp_local_pref = $response->getProperty('bgp-local-pref');
                $this->bgp_med = $response->getProperty('bgp-med');
                $this->bgp_origin = $response->getProperty('bgp-origin');
                $this->bgp_weight = $response->getProperty('bgp-weight');
                $this->chain = $response->getProperty('chain');
                $this->distance = $response->getProperty('distance');
                $this->invert_math = $response->getProperty('invert-math');
                $this->jump_target = $response->getProperty('jump-target');
                $this->locally_originated_bgp = $response->getProperty('locally-originated-bgp');
                $this->match_chain = $response->getProperty('match-chain');
                $this->ospf_type = $response->getProperty('ospf-type');
                $this->pref_src = $response->getProperty('pref-src');
                $this->prefix = $response->getProperty('prefix');
                $this->prefix_length = $response->getProperty('prefix-length');
                $this->protocol = $response->getProperty('protocol');
                $this->route_comment = $response->getProperty('route-comment');
                $this->route_tag = $response->getProperty('route-tag');
                $this->route_target = $response->getProperty('route-target');
                $this->routing_mark = $response->getProperty('routing-mark');
                $this->scope = $response->getProperty('scope');
                $this->set_bgp_communities = $response->getProperty('set-bgp-communities');
                $this->set_bgp_local_pref = $response->getProperty('set-bgp-local-pref');
                $this->set_bgp_med = $response->getProperty('set-bgp-med');
                $this->set_bgp_prepend = $response->getProperty('set-bgp-prepend');
                $this->set_bgp_prepend_path = $response->getProperty('set-bgp-prepend-path');
                $this->set_bgp_weight = $response->getProperty('set-bgp-weight');
                $this->set_check_gateway = $response->getProperty('set-check-gateway');
                $this->set_disabled = $response->getProperty('set-disabled');
                $this->set_distance = $response->getProperty('set-distance');
                $this->set_in_nexthop = $response->getProperty('set-in-nexthop');
                $this->set_in_nexthop_direct = $response->getProperty('set-in-nexthop-direct');
                $this->set_in_nexthop_ipv6 = $response->getProperty('set-in-nexthop-ipv6');
                $this->set_in_nexthop_linklocal = $response->getProperty('set-in-nexthop-linklocal');
                $this->set_out_nexthop = $response->getProperty('set-out-nexthop');
                $this->set_out_nexthop_ipv6 = $response->getProperty('set-out-nexthop-ipv6');
                $this->set_out_nexthop_linklocal = $response->getProperty('set-out-nexthop-linklocal');
                $this->set_pref_src = $response->getProperty('set-pref-src');
                $this->set_route_comment = $response->getProperty('set-route-comment');
                $this->set_route_tag = $response->getProperty('set-route-tag');
                $this->set_route_targets = $response->getProperty('set-route-targets');
                $this->set_routing_mark = $response->getProperty('set-routing-mark');
                $this->set_scope = $response->getProperty('set-scope');
                $this->set_target_scope = $response->getProperty('set-target-scope');
                $this->set_type = $response->getProperty('set-type');
                $this->set_use_te_nexthop = $response->getProperty('set-use-te-nexthop');
                $this->site_of_origin = $response->getProperty('site-of-origin');
                $this->set_site_of_origin = $response->getProperty('set-site-of-origin');
                $this->target_scope = $response->getProperty('target-scope');
                $this->disabled = $response->getProperty('disabled');
            }
        }
        return;
    }

    public function update($id) {
        $c = new Filter();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/filter/set');
        $setRequest->setargument('.id', $id);
        if ($this->action != $c->action) {
            $setRequest->setargument('action', $this->action);
        }
        if ($this->address_family != $c->address_family) {
            if ($this->address_family == '') {
                $unset[] = 'address-family';
            } else {
                $setRequest->setargument('address-family', $this->address_family);
            }
        }
        if ($this->append_bgp_communities != $c->append_bgp_communities) {
            if ($this->append_bgp_communities == '') {
                $unset[] = 'append-bgp-communities';
            } else {
                $setRequest->setargument('append-bgp-communities', $this->append_bgp_communities);
            }
        }
        if ($this->append_route_targets != $c->append_route_targets) {
            if ($this->append_route_targets == '') {
                $unset[] = 'append-route-targets';
            } else {
                $setRequest->setargument('append-route-targets', $this->append_route_targets);
            }
        }
        if ($this->bgp_as_path != $c->bgp_as_path) {
            if ($this->bgp_as_path == '') {
                $unset[] = 'bgp-as-path';
            } else {
                $setRequest->setargument('bgp-as-path', $this->bgp_as_path);
            }
        }
        if ($this->bgp_as_path_length != $c->bgp_as_path_length) {
            if ($this->bgp_as_path_length == '') {
                $unset[] = 'bgp-as-path-length';
            } else {
                $setRequest->setargument('bgp-as-path-length', $this->bgp_as_path_length);
            }
        }
        if ($this->bgp_atomic_aggregate != $c->bgp_atomic_aggregate) {
            if ($this->bgp_atomic_aggregate == '') {
                $unset[] = 'bgp-atomic-aggregate';
            } else {
                $setRequest->setargument('bgp-atomic-aggregate', $this->bgp_atomic_aggregate);
            }
        }
        if ($this->bgp_communities != $c->bgp_communities) {
            if ($this->bgp_communities == '') {
                $unset[] = 'bgp-communities';
            } else {
                $setRequest->setargument('bgp-communities', $this->bgp_communities);
            }
        }
        if ($this->bgp_local_pref != $c->bgp_local_pref) {
            if ($this->bgp_local_pref == '') {
                $unset[] = 'bgp-local-pref';
            } else {
                $setRequest->setargument('bgp-local-pref', $this->bgp_local_pref);
            }
        }
        if ($this->bgp_med != $c->bgp_med) {
            if ($this->bgp_med == '') {
                $unset[] = 'bgp-med';
            } else {
                $setRequest->setargument('bgp-med', $this->bgp_med);
            }
        }
        if ($this->bgp_origin != $c->bgp_origin) {
            if ($this->bgp_origin == '') {
                $unset[] = 'bgp-origin';
            } else {
                $setRequest->setargument('bgp-origin', $this->bgp_origin);
            }
        }
        if ($this->bgp_weight != $c->bgp_weight) {
            if ($this->bgp_weight == '') {
                $unset[] = 'bgp-weight';
            } else {
                $setRequest->setargument('bgp-weight', $this->bgp_weight);
            }
        }
        if ($this->chain != $c->chain) {
            $setRequest->setargument('chain', $this->chain);
        }
        if ($this->distance != $c->distance) {
            if ($this->distance == '') {
                $unset[] = 'distance';
            } else {
                $setRequest->setargument('distance', $this->distance);
            }
        }
        if ($this->invert_math != $c->invert_math) {
            $setRequest->setargument('invert-math', $this->invert_math);
        }
        if ($this->jump_target != $c->jump_target) {
            $setRequest->setargument('jump-target', $this->jump_target);
        }
        if ($this->locally_originated_bgp != $c->locally_originated_bgp) {
            if ($this->locally_originated_bgp == '') {
                $unset[] = 'locally-originated-bgp';
            } else {
                $setRequest->setargument('locally-originated-bgp', $this->locally_originated_bgp);
            }
        }
        if ($this->match_chain != $c->match_chain) {
            if ($this->match_chain == '') {
                $unset[] = 'match-chain';
            } else {
                $setRequest->setargument('match-chain', $this->match_chain);
            }
        }
        if ($this->ospf_type != $c->ospf_type) {
            if ($this->ospf_type == '') {
                $unset[] = 'ospf-type';
            } else {
                $setRequest->setargument('ospf-type', $this->ospf_type);
            }
        }
        if ($this->pref_src != $c->pref_src) {
            if ($this->pref_src == '') {
                $unset[] = 'pref-src';
            } else {
                $setRequest->setargument('pref-src', $this->pref_src);
            }
        }
        if ($this->prefix != $c->prefix) {
            if ($this->prefix == '') {
                $unset[] = 'prefix';
            } else {
                $setRequest->setargument('prefix', $this->prefix);
            }
        }
        if ($this->prefix_length != $c->prefix_length) {
            if ($this->prefix_length == '') {
                $unset[] = 'prefix-length';
            } else {
                $setRequest->setargument('prefix-length', $this->prefix_length);
            }
        }
        if ($this->protocol != $c->protocol) {
            if ($this->protocol == '') {
                $unset[] = 'protocol';
            } else {
                $setRequest->setargument('protocol', $this->protocol);
            }
        }
        if ($this->route_comment != $c->route_comment) {
            if ($this->route_comment == '') {
                $unset[] = 'route-comment';
            } else {
                $setRequest->setargument('route-comment', $this->route_comment);
            }
        }
        if ($this->route_tag != $c->route_tag) {
            if ($this->route_tag == '') {
                $unset[] = 'route-tag';
            } else {
                $setRequest->setargument('route-tag', $this->route_tag);
            }
        }
        if ($this->route_target != $c->route_target) {
            if ($this->route_target == '') {
                $unset[] = 'route-target';
            } else {
                $setRequest->setargument('route-target', $this->route_target);
            }
        }
        if ($this->routing_mark != $c->routing_mark) {
            if ($this->routing_mark == '') {
                $unset[] = 'routing-mark';
            } else {
                $setRequest->setargument('routing-mark', $this->routing_mark);
            }
        }
        if ($this->scope != $c->scope) {
            if ($this->scope == '') {
                $unset[] = 'scope';
            } else {
                $setRequest->setargument('scope', $this->scope);
            }
        }
        if ($this->set_bgp_communities != $c->set_bgp_communities) {
            if ($this->set_bgp_communities == '') {
                $unset[] = 'set-bgp-communities';
            } else {
                $setRequest->setargument('set-bgp-communities', $this->set_bgp_communities);
            }
        }
        if ($this->set_bgp_local_pref != $c->set_bgp_local_pref) {
            if ($this->set_bgp_local_pref == '') {
                $unset[] = 'set-bgp-local-pref';
            } else {
                $setRequest->setargument('set-bgp-local-pref', $this->set_bgp_local_pref);
            }
        }
        if ($this->set_bgp_med != $c->set_bgp_med) {
            if ($this->set_bgp_med == '') {
                $unset[] = 'set-bgp-med';
            } else {
                $setRequest->setargument('set-bgp-med', $this->set_bgp_med);
            }
        }
        if ($this->set_bgp_prepend != $c->set_bgp_prepend) {
            if ($this->set_bgp_prepend == '') {
                $unset[] = 'set-bgp-prepend';
            } else {
                $setRequest->setargument('set-bgp-prepend', $this->set_bgp_prepend);
            }
        }
        if ($this->set_bgp_prepend_path != $c->set_bgp_prepend_path) {
            $setRequest->setargument('set-bgp-prepend-path', $this->set_bgp_prepend_path);
        }
        if ($this->set_bgp_weight != $c->set_bgp_weight) {
            if ($this->set_bgp_weight == '') {
                $unset[] = 'set-bgp-weight';
            } else {
                $setRequest->setargument('set-bgp-weight', $this->set_bgp_weight);
            }
        }
        if ($this->set_check_gateway != $c->set_check_gateway) {
            if ($this->set_check_gateway == '') {
                $unset[] = 'set-check-gateway';
            } else {
                $setRequest->setargument('set-check-gateway', $this->set_check_gateway);
            }
        }
        if ($this->set_disabled != $c->set_disabled) {
            if ($this->set_disabled == '') {
                $unset[] = 'set-disabled';
            } else {
                $setRequest->setargument('set-disabled', $this->set_disabled);
            }
        }
        if ($this->set_distance != $c->set_distance) {
            if ($this->set_distance == '') {
                $unset[] = 'set-distance';
            } else {
                $setRequest->setargument('set-distance', $this->set_distance);
            }
        }
        if ($this->set_in_nexthop != $c->set_in_nexthop) {
            if ($this->set_in_nexthop == '') {
                $unset[] = 'set-in-nexthop';
            } else {
                $setRequest->setargument('set-in-nexthop', $this->set_in_nexthop);
            }
        }
        if ($this->set_in_nexthop_direct != $c->set_in_nexthop_direct) {
            if ($this->set_in_nexthop_direct == '') {
                $unset[] = 'set-in-nexthop-direct';
            } else {
                $setRequest->setargument('set-in-nexthop-direct', $this->set_in_nexthop_direct);
            }
        }
        if ($this->set_in_nexthop_ipv6 != $c->set_in_nexthop_ipv6) {
            if ($this->set_in_nexthop_ipv6 == '') {
                $unset[] = 'set-in-nexthop-ipv6';
            } else {
                $setRequest->setargument('set-in-nexthop-ipv6', $this->set_in_nexthop_ipv6);
            }
        }
        if ($this->set_in_nexthop_linklocal != $c->set_in_nexthop_linklocal) {
            if ($this->set_in_nexthop_linklocal == '') {
                $unset[] = 'set-in-nexthop-linklocal';
            } else {
                $setRequest->setargument('set-in-nexthop-linklocal', $this->set_in_nexthop_linklocal);
            }
        }
        if ($this->set_out_nexthop != $c->set_out_nexthop) {
            if ($this->set_out_nexthop == '') {
                $unset[] = 'set-out-nexthop';
            } else {
                $setRequest->setargument('set-out-nexthop', $this->set_out_nexthop);
            }
        }
        if ($this->set_out_nexthop_ipv6 != $c->set_out_nexthop_ipv6) {
            if ($this->set_out_nexthop_ipv6 == '') {
                $unset[] = 'set-out-nexthop-ipv6';
            } else {
                $setRequest->setargument('set-out-nexthop-ipv6', $this->set_out_nexthop_ipv6);
            }
        }
        if ($this->set_out_nexthop_linklocal != $c->set_out_nexthop_linklocal) {
            if ($this->set_out_nexthop_linklocal == '') {
                $unset[] = 'set-out-nexthop-linklocal';
            } else {
                $setRequest->setargument('set-out-nexthop-linklocal', $this->set_out_nexthop_linklocal);
            }
        }
        if ($this->set_pref_src != $c->set_pref_src) {
            if ($this->set_pref_src == '') {
                $unset[] = 'set-pref-src';
            } else {
                $setRequest->setargument('set-pref-src', $this->set_pref_src);
            }
        }
        if ($this->set_route_comment != $c->set_route_comment) {
            if ($this->set_route_comment == '') {
                $unset[] = 'set-route-comment';
            } else {
                $setRequest->setargument('set-route-comment', $this->set_route_comment);
            }
        }
        if ($this->set_route_tag != $c->set_route_tag) {
            if ($this->set_route_tag == '') {
                $unset[] = 'set-route-tag';
            } else {
                $setRequest->setargument('set-route-tag', $this->set_route_tag);
            }
        }
        if ($this->set_route_targets != $c->set_route_targets) {
            if ($this->set_route_targets == '') {
                $unset[] = 'set-route-targets';
            } else {
                $setRequest->setargument('set-route-targets', $this->set_route_targets);
            }
        }
        if ($this->set_routing_mark != $c->set_routing_mark) {
            if ($this->set_routing_mark == '') {
                $unset[] = 'set-routing-mark';
            } else {
                $setRequest->setargument('set-routing-mark', $this->set_routing_mark);
            }
        }
        if ($this->set_scope != $c->set_scope) {
            if ($this->set_scope == '') {
                $unset[] = 'set-scope';
            } else {
                $setRequest->setargument('set-scope', $this->set_scope);
            }
        }
        if ($this->set_target_scope != $c->set_target_scope) {
            if ($this->set_target_scope == '') {
                $unset[] = 'set-target-scope';
            } else {
                $setRequest->setargument('set-target-scope', $this->set_target_scope);
            }
        }
        if ($this->set_type != $c->set_type) {
            if ($this->set_type == '') {
                $unset[] = 'set-type';
            } else {
                $setRequest->setargument('set-type', $this->set_type);
            }
        }
        if ($this->set_use_te_nexthop != $c->set_use_te_nexthop) {
            if ($this->set_use_te_nexthop == '') {
                $unset[] = 'set-use-te-nexthop';
            } else {
                $setRequest->setargument('set-use-te-nexthop', $this->set_use_te_nexthop);
            }
        }
        if ($this->site_of_origin != $c->site_of_origin) {
            if ($this->site_of_origin == '') {
                $unset[] = 'site-of-origin';
            } else {
                $setRequest->setargument('site-of-origin', $this->site_of_origin);
            }
        }
        if ($this->set_site_of_origin != $c->set_site_of_origin) {
            if ($this->set_site_of_origin == '') {
                $unset[] = 'set-site-of-origin';
            } else {
                $setRequest->setargument('set-site-of-origin', $this->set_site_of_origin);
            }
        }
        if ($this->target_scope != $c->target_scope) {
            if ($this->target_scope == '') {
                $unset[] = 'target-scope';
            } else {
                $setRequest->setargument('target-scope', $this->target_scope);
            }
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/routing/filter/unset');
            foreach ($unset as $value) {
                $unsetRequest->setArgument('.id', $id);
                $unsetRequest->setArgument('value-name', $value);
                $resp = $connect->getConnection()->sendSync($unsetRequest);
                if ($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
                    if (!isset($re)) {
                        $re = '';
                    } $re.= ' ' . $resp->getProperty('message');
                }
            }
        } $resp = $connect->getConnection()->sendSync($setRequest);
        if ($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
            if (isset($re)) {
                $re.= ' ' . $resp->getProperty('message');
            } else {
                $re = $resp->getProperty('message');
            }
        } if (isset($re)) {
            return $re;
        } else {
            return '1';
        }
    }

    public function disable($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/filter/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

    public function enable($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/filter/enable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/filter/add');
        if ($this->action != "") {
            $setRequest->setargument('action', $this->action);
        }
        if ($this->address_family != "") {
            $setRequest->setargument('address-family', $this->address_family);
        }
        if ($this->append_bgp_communities != "") {
            $setRequest->setargument('append-bgp-communities', $this->append_bgp_communities);
        }
        if ($this->append_route_targets != "") {
            $setRequest->setargument('append-route-targets', $this->append_route_targets);
        }
        if ($this->bgp_as_path != "") {
            $setRequest->setargument('bgp-as-path', $this->bgp_as_path);
        }
        if ($this->bgp_as_path_length != "") {
            $setRequest->setargument('bgp-as-path-length', $this->bgp_as_path_length);
        }
        if ($this->bgp_atomic_aggregate != "") {
            $setRequest->setargument('bgp-atomic-aggregate', $this->bgp_atomic_aggregate);
        }
        if ($this->bgp_communities != "") {
            $setRequest->setargument('bgp-communities', $this->bgp_communities);
        }
        if ($this->bgp_local_pref != "") {
            $setRequest->setargument('bgp-local-pref', $this->bgp_local_pref);
        }
        if ($this->bgp_med != "") {
            $setRequest->setargument('bgp-med', $this->bgp_med);
        }
        if ($this->bgp_origin != "") {
            $setRequest->setargument('bgp-origin', $this->bgp_origin);
        }
        if ($this->bgp_weight != "") {
            $setRequest->setargument('bgp-weight', $this->bgp_weight);
        }
        if ($this->chain != "") {
            $setRequest->setargument('chain', $this->chain);
        }
        if ($this->distance != "") {
            $setRequest->setargument('distance', $this->distance);
        }
        if ($this->invert_math != "") {
            $setRequest->setargument('invert-math', $this->invert_math);
        }
        if ($this->jump_target != "") {
            $setRequest->setargument('jump-target', $this->jump_target);
        }
        if ($this->locally_originated_bgp != "") {
            $setRequest->setargument('locally-originated-bgp', $this->locally_originated_bgp);
        }
        if ($this->match_chain != "") {
            $setRequest->setargument('match-chain', $this->match_chain);
        }
        if ($this->ospf_type != "") {
            $setRequest->setargument('ospf-type', $this->ospf_type);
        }
        if ($this->pref_src != "") {
            $setRequest->setargument('pref-src', $this->pref_src);
        }
        if ($this->prefix != "") {
            $setRequest->setargument('prefix', $this->prefix);
        }
        if ($this->prefix_length != "") {
            $setRequest->setargument('prefix-length', $this->prefix_length);
        }
        if ($this->protocol != "") {
            $setRequest->setargument('protocol', $this->protocol);
        }
        if ($this->route_comment != "") {
            $setRequest->setargument('route-comment', $this->route_comment);
        }
        if ($this->route_tag != "") {
            $setRequest->setargument('route-tag', $this->route_tag);
        }
        if ($this->route_target != "") {
            $setRequest->setargument('route-target', $this->route_target);
        }
        if ($this->routing_mark != "") {
            $setRequest->setargument('routing-mark', $this->routing_mark);
        }
        if ($this->scope != "") {
            $setRequest->setargument('scope', $this->scope);
        }
        if ($this->set_bgp_communities != "") {
            $setRequest->setargument('set-bgp-communities', $this->set_bgp_communities);
        }
        if ($this->set_bgp_local_pref != "") {
            $setRequest->setargument('set-bgp-local-pref', $this->set_bgp_local_pref);
        }
        if ($this->set_bgp_med != "") {
            $setRequest->setargument('set-bgp-med', $this->set_bgp_med);
        }
        if ($this->set_bgp_prepend != "") {
            $setRequest->setargument('set-bgp-prepend', $this->set_bgp_prepend);
        }
        if ($this->set_bgp_prepend_path != "") {
            $setRequest->setargument('set-bgp-prepend-path', $this->set_bgp_prepend_path);
        }
        if ($this->set_bgp_weight != "") {
            $setRequest->setargument('set-bgp-weight', $this->set_bgp_weight);
        }
        if ($this->set_check_gateway != "") {
            $setRequest->setargument('set-check-gateway', $this->set_check_gateway);
        }
        if ($this->set_disabled != "") {
            $setRequest->setargument('set-disabled', $this->set_disabled);
        }
        if ($this->set_distance != "") {
            $setRequest->setargument('set-distance', $this->set_distance);
        }
        if ($this->set_in_nexthop != "") {
            $setRequest->setargument('set-in-nexthop', $this->set_in_nexthop);
        }
        if ($this->set_in_nexthop_direct != "") {
            $setRequest->setargument('set-in-nexthop-direct', $this->set_in_nexthop_direct);
        }
        if ($this->set_in_nexthop_ipv6 != "") {
            $setRequest->setargument('set-in-nexthop-ipv6', $this->set_in_nexthop_ipv6);
        }
        if ($this->set_in_nexthop_linklocal != "") {
            $setRequest->setargument('set-in-nexthop-linklocal', $this->set_in_nexthop_linklocal);
        }
        if ($this->set_out_nexthop != "") {
            $setRequest->setargument('set-out-nexthop', $this->set_out_nexthop);
        }
        if ($this->set_out_nexthop_ipv6 != "") {
            $setRequest->setargument('set-out-nexthop-ipv6', $this->set_out_nexthop_ipv6);
        }
        if ($this->set_out_nexthop_linklocal != "") {
            $setRequest->setargument('set-out-nexthop-linklocal', $this->set_out_nexthop_linklocal);
        }
        if ($this->set_pref_src != "") {
            $setRequest->setargument('set-pref-src', $this->set_pref_src);
        }
        if ($this->set_route_comment != "") {
            $setRequest->setargument('set-route-comment', $this->set_route_comment);
        }
        if ($this->set_route_tag != "") {
            $setRequest->setargument('set-route-tag', $this->set_route_tag);
        }
        if ($this->set_route_targets != "") {
            $setRequest->setargument('set-route-targets', $this->set_route_targets);
        }
        if ($this->set_routing_mark != "") {
            $setRequest->setargument('set-routing-mark', $this->set_routing_mark);
        }
        if ($this->set_scope != "") {
            $setRequest->setargument('set-scope', $this->set_scope);
        }
        if ($this->set_target_scope != "") {
            $setRequest->setargument('set-target-scope', $this->set_target_scope);
        }
        if ($this->set_type != "") {
            $setRequest->setargument('set-type', $this->set_type);
        }
        if ($this->set_use_te_nexthop != "") {
            $setRequest->setargument('set-use-te-nexthop', $this->set_use_te_nexthop);
        }
        if ($this->site_of_origin != "") {
            $setRequest->setargument('site-of-origin', $this->site_of_origin);
        }
        if ($this->set_site_of_origin != "") {
            $setRequest->setargument('set-site-of-origin', $this->set_site_of_origin);
        }
        if ($this->target_scope != "") {
            $setRequest->setargument('target-scope', $this->target_scope);
        }
        $resp = $connect->getConnection()->sendSync($setRequest);
        if ($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
            return $resp->getProperty('message');
        } else {
            return '1';
        }
    }

    public function delete($id) {
        $connect = MikrotikConnect::Instance();
        $util = new RouterOS\Util($connect->getConnection());
        $util->setMenu('/routing/filter');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
