DocumentsToTheUser.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. use yii\helpers\FileHelper;
  5. use yii\web\UploadedFile;
  6. use Zend\Validator\File\Upload;
  7. /**
  8. * This is the model class for table "documents_to_the_user".
  9. *
  10. * @property integer $id
  11. * @property string $title
  12. * @property string $path
  13. */
  14. class DocumentsToTheUser extends \yii\db\ActiveRecord
  15. {
  16. public $files;
  17. /**
  18. * @inheritdoc
  19. */
  20. public static function tableName()
  21. {
  22. return 'documents_to_the_user';
  23. }
  24. /**
  25. * @inheritdoc
  26. */
  27. public function rules()
  28. {
  29. return [
  30. [['title'], 'required'],
  31. [['title'], 'string', 'max' => 255],
  32. [['files'],'file'],
  33. [['path'], 'safe'],
  34. [['menu_to_the_user_id'],'integer'],
  35. ];
  36. }
  37. /**
  38. * @inheritdoc
  39. */
  40. public function attributeLabels()
  41. {
  42. return [
  43. 'id' => 'ID',
  44. 'title' => Yii::t('app','Title Document User'),
  45. 'files' => 'Files',
  46. 'menu_to_the_user_id' => Yii::t('app','menuToTheUser ID')
  47. ];
  48. }
  49. public function upload()
  50. {
  51. if(false != ($file = UploadedFile::getInstance($this, 'files'))){
  52. $filename = time() . "_{$file->baseName}.{$file->extension}";
  53. $filename = self::transliteration($filename);
  54. $path = 'uploads/documents-to-the-user';
  55. FileHelper::createDirectory($path);
  56. if($this->save()){
  57. if($file->saveAs("{$path}/{$filename}")){
  58. return $this->updateAttributes(['path' => "{$path}/{$filename}"]);
  59. }
  60. return false;
  61. }
  62. }
  63. return false;
  64. }
  65. public static function transliteration($str)
  66. {
  67. // ГОСТ 7.79B
  68. $transliteration = array(
  69. 'А' => 'A', 'а' => 'a',
  70. 'Б' => 'B', 'б' => 'b',
  71. 'В' => 'V', 'в' => 'v',
  72. 'Г' => 'G', 'г' => 'g',
  73. 'Д' => 'D', 'д' => 'd',
  74. 'Е' => 'E', 'е' => 'e',
  75. 'Ё' => 'Yo', 'ё' => 'yo',
  76. 'Ж' => 'Zh', 'ж' => 'zh',
  77. 'З' => 'Z', 'з' => 'z',
  78. 'И' => 'I', 'и' => 'i',
  79. 'Й' => 'J', 'й' => 'j',
  80. 'К' => 'K', 'к' => 'k',
  81. 'Л' => 'L', 'л' => 'l',
  82. 'М' => 'M', 'м' => 'm',
  83. 'Н' => "N", 'н' => 'n',
  84. 'О' => 'O', 'о' => 'o',
  85. 'П' => 'P', 'п' => 'p',
  86. 'Р' => 'R', 'р' => 'r',
  87. 'С' => 'S', 'с' => 's',
  88. 'Т' => 'T', 'т' => 't',
  89. 'У' => 'U', 'у' => 'u',
  90. 'Ф' => 'F', 'ф' => 'f',
  91. 'Х' => 'H', 'х' => 'h',
  92. 'Ц' => 'Cz', 'ц' => 'cz',
  93. 'Ч' => 'Ch', 'ч' => 'ch',
  94. 'Ш' => 'Sh', 'ш' => 'sh',
  95. 'Щ' => 'Shh', 'щ' => 'shh',
  96. 'Ъ' => 'ʺ', 'ъ' => 'ʺ',
  97. 'Ы' => 'Y`', 'ы' => 'y`',
  98. 'Ь' => '', 'ь' => '',
  99. 'Э' => 'E`', 'э' => 'e`',
  100. 'Ю' => 'Yu', 'ю' => 'yu',
  101. 'Я' => 'Ya', 'я' => 'ya',
  102. '№' => '#', 'Ӏ' => '‡',
  103. '’' => '`', 'ˮ' => '¨',
  104. );
  105. $str = strtr($str, $transliteration);
  106. $str = mb_strtolower($str, 'UTF-8');
  107. $str = preg_replace('/[^0-9a-z.\-]/', '', $str);
  108. $str = preg_replace('|([-]+)|s', '-', $str);
  109. $str = trim($str, '-');
  110. //return time() . '_' . $str;
  111. return $str;
  112. }
  113. public function getMenu()
  114. {
  115. return $this->hasOne(MenuToTheUser::className(), ['id' => 'menu_to_the_user_id']);
  116. }
  117. }