<?php

namespace app\models\mikrotik\backup;

use Net_SFTP;
use yii\base\Model;
use yii\db\Query;
use yii\db\ActiveRecord;

//use app\models\mikrotik\Backupmikrotik;

class Backupdb extends Activerecord {

    public function rules() {
        return [
            [['name', 'type', 'content'], 'required'],
            [['name', 'type'], 'string'],
            [['size'], 'integer']
        ];
    }

    public static function tableName() {
        return 'mikrotik_backup';
    }

    public static function getDb() {
        return \Yii::$app->dbMikrotik;
    }

    public function doBackupdb($ip, $name, $type, $size, $content) {
        $backup = new Backupdb();
        $backup->ip = $ip;
        $backup->name = $name;
        $backup->type = $type;
        $backup->size = $size;
        $backup->content = file_get_contents($content);
        $backup->save();

        return;
    }

    public function getAll($ip = null) {
        if ($ip != null) {
            return $this::find()->select(['id', 'name', 'ip', 'size'])->where(['ip' => $ip])->indexBy('id')->all();
        } else {
            return $this::find()->select(['id', 'name', 'ip', 'size'])->indexBy('id')->all();
        }
    }

    public function get($id) {

        return $this::find()->where(['id' => $id])->all();
    }

    public function remove($id) {
        $c = BackupDB::findone($id); //->where(['id' => $id])->all();
        $c->delete();
        return;
    }

}
