<?php

class Backup extends PhonalisaRecord
{

    public $compressedSize;
    public $datetime;
    public $fileName;
    public $sha256;
    public $version;
    public $comment;

    public static function myAttributes()
    {
        return array(
            'compressedSize' => 'compressedSize',
            'datetime' => 'datetime',
            'fileName' => 'fileName',
            'sha256' => 'sha256',
            'version' => 'version',
            'comment' => 'comment'
        );
    }

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Job the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'backup';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('datetime, compressedSize, fileName, sha256, version, comment', 'safe'),
            array('datetime, compressedSize, fileName, sha256, version, comment', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'datetime' => __('SYSTEM_BACKUP_RESTORE_DATETIME'),
            'compressedSize' => __('SYSTEM_BACKUP_RESTORE_SIZE'),
            'fileName' => __('SYSTEM_BACKUP_RESTORE_NAME'),
            'sha256' => __('SYSTEM_BACKUP_RESTORE_HASH'),
            'version' => __('SYSTEM_BACKUP_RESTORE_VERSION'),
            'comment' => __('SYSTEM_BACKUP_RESTORE_COMMENT')
        );
    }

    public function getBackupsFromDB()
    {
        $oldBackups = PhonalisaDBConnector::getBackups();
        $data = array();
        if ($oldBackups && $oldBackups->status === 'ok') {
            $data = (array) $oldBackups->data;
            $data = array_values($data);

            foreach ($data as $key => $oldBackup) {
                $data[$key]->id = $key;
            }
            $data = array_reverse($data);
        }
        return new CArrayDataProvider($data, array('pagination' => false));
    }

    public function getSizeHumanReadable($data, $row, $cell)
    {
        $size = $data->compressedSize;
        return $this->fileSizeConvert($size);
    }

    public function fileSizeConvert($bytes)
    {
        $bytes = floatval($bytes);
        $arBytes = array(
            0 => array(
                "UNIT" => "TB",
                "VALUE" => pow(1024, 4)
            ),
            1 => array(
                "UNIT" => "GB",
                "VALUE" => pow(1024, 3)
            ),
            2 => array(
                "UNIT" => "MB",
                "VALUE" => pow(1024, 2)
            ),
            3 => array(
                "UNIT" => "KB",
                "VALUE" => 1024
            ),
            4 => array(
                "UNIT" => "B",
                "VALUE" => 1
            ),
        );

        foreach ($arBytes as $arItem) {
            if ($bytes >= $arItem["VALUE"]) {
                $result = $bytes / $arItem["VALUE"];
                $result = str_replace(".", ",", strval(round($result, 2))) . " " . $arItem["UNIT"];
                break;
            }
        }
        return $result;
    }
}
