<?php

namespace app\models;

use Yii;
use yii\helpers\FileHelper;
use yii\web\UploadedFile;
use Zend\Validator\File\Upload;

/**
 * This is the model class for table "documents_to_the_user".
 *
 * @property integer $id
 * @property string $title
 * @property string $path
 */
class DocumentsToTheUser extends \yii\db\ActiveRecord
{

    public $files;


    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'documents_to_the_user';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title'], 'required'],
            [['title'], 'string', 'max' => 255],
            [['files'],'file'],
            [['path'], 'safe'],
            [['menu_to_the_user_id'],'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => Yii::t('app','Title Document User'),
            'files' => 'Files',
            'menu_to_the_user_id' => Yii::t('app','menuToTheUser ID')
        ];
    }

    public function upload()
    {
        if(false != ($file = UploadedFile::getInstance($this, 'files'))){
            $filename = time() . "_{$file->baseName}.{$file->extension}";
            $filename = self::transliteration($filename);
            $path = 'uploads/documents-to-the-user';
            FileHelper::createDirectory($path);
            if($this->save()){
                if($file->saveAs("{$path}/{$filename}")){
                    return $this->updateAttributes(['path' => "{$path}/{$filename}"]);
                }
                return false;
            }
        }
        return false;
    }

    public static function transliteration($str)
    {
        // ГОСТ 7.79B
        $transliteration = array(
            'А' => 'A', 'а' => 'a',
            'Б' => 'B', 'б' => 'b',
            'В' => 'V', 'в' => 'v',
            'Г' => 'G', 'г' => 'g',
            'Д' => 'D', 'д' => 'd',
            'Е' => 'E', 'е' => 'e',
            'Ё' => 'Yo', 'ё' => 'yo',
            'Ж' => 'Zh', 'ж' => 'zh',
            'З' => 'Z', 'з' => 'z',
            'И' => 'I', 'и' => 'i',
            'Й' => 'J', 'й' => 'j',
            'К' => 'K', 'к' => 'k',
            'Л' => 'L', 'л' => 'l',
            'М' => 'M', 'м' => 'm',
            'Н' => "N", 'н' => 'n',
            'О' => 'O', 'о' => 'o',
            'П' => 'P', 'п' => 'p',
            'Р' => 'R', 'р' => 'r',
            'С' => 'S', 'с' => 's',
            'Т' => 'T', 'т' => 't',
            'У' => 'U', 'у' => 'u',
            'Ф' => 'F', 'ф' => 'f',
            'Х' => 'H', 'х' => 'h',
            'Ц' => 'Cz', 'ц' => 'cz',
            'Ч' => 'Ch', 'ч' => 'ch',
            'Ш' => 'Sh', 'ш' => 'sh',
            'Щ' => 'Shh', 'щ' => 'shh',
            'Ъ' => 'ʺ', 'ъ' => 'ʺ',
            'Ы' => 'Y`', 'ы' => 'y`',
            'Ь' => '', 'ь' => '',
            'Э' => 'E`', 'э' => 'e`',
            'Ю' => 'Yu', 'ю' => 'yu',
            'Я' => 'Ya', 'я' => 'ya',
            '№' => '#', 'Ӏ' => '‡',
            '’' => '`', 'ˮ' => '¨',
        );

        $str = strtr($str, $transliteration);
        $str = mb_strtolower($str, 'UTF-8');
        $str = preg_replace('/[^0-9a-z.\-]/', '', $str);
        $str = preg_replace('|([-]+)|s', '-', $str);
        $str = trim($str, '-');

        //return time() . '_' . $str;
        return $str;
    }

    public function getMenu()
    {
        return $this->hasOne(MenuToTheUser::className(), ['id' => 'menu_to_the_user_id']);
    }

}
