Files.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. use yii\helpers\FileHelper;
  5. use yii\web\UploadedFile;
  6. use zip\lib\createDirZip;
  7. use app\models\User;
  8. /**
  9. * This is the model class for table "files".
  10. *
  11. * @property integer $id
  12. * @property string $path
  13. */
  14. //Yii::$app->params['uploadPath'] = "../uploads/".Yii::$app->user->identity->id."/";
  15. class Files extends \yii\db\ActiveRecord
  16. {
  17. /**
  18. * @inheritdoc
  19. */
  20. public $file;
  21. public $uploads = '../uploads/';
  22. public static function tableName()
  23. {
  24. return 'files';
  25. }
  26. /**
  27. * @inheritdoc
  28. */
  29. public function rules()
  30. {
  31. return [
  32. [['path','name','user_id'], 'required'],
  33. ['file', 'required', 'message' => 'Необхiдно прикрiпити додаткову документацiю'],
  34. [['path','name'], 'string', 'max' => 255],
  35. [['date'],'date'],
  36. [['user_id','auction_id','lot_id'], 'integer', 'max' => 255],
  37. [['file'], 'file', 'maxFiles' => 0,
  38. 'maxSize'=> 50*(1024*1024),
  39. 'extensions' => ['doc','docx','pdf','png','jpg','tiff','zip','rar']],
  40. ];
  41. }
  42. /**
  43. * @inheritdoc
  44. */
  45. public function attributeLabels()
  46. {
  47. return [
  48. 'id' => Yii::t('app', 'ID'),
  49. 'path' => Yii::t('app', 'Path'),
  50. 'name' => Yii::t('app', 'file name'),
  51. 'user_id' => Yii::t('app', 'owner'),
  52. 'auction_id' => Yii::t('app', 'auction_id'),
  53. 'lot_id' => Yii::t('app', 'lot_id'),
  54. 'date'=> Yii::t('app','date'),
  55. ];
  56. }
  57. public function saveFile($insert)
  58. { if(empty($insert['type'])) { $insert['type'] = null; }
  59. //var_dump($saveFile); exit;
  60. Yii::$app->db->createCommand()->insert(
  61. 'files', [
  62. 'name' => $insert['name'],
  63. 'path' => $insert['path'],
  64. 'user_id' => $insert['user_id'],
  65. 'auction_id' => $insert['auction_id'],
  66. 'lot_id' => $insert['lot_id'],
  67. 'type' => $insert['type'],
  68. ])
  69. ->execute();
  70. return $this->getFileID($insert['user_id'],$insert['lot_id'],$insert['name']);
  71. }
  72. public function updateFile($update)
  73. {
  74. Yii::$app->db->createCommand("UPDATE `files` SET name='".$update['name']."' WHERE `id`=" . $update['file_id'])->execute();
  75. return true;
  76. }
  77. private function getFileID($user,$lot,$name)
  78. {
  79. $res = $this->find()->where(['user_id'=>$user,'lot_id'=>$lot,'name'=>$name])->one();
  80. return $res->id;
  81. }
  82. public static function transliteration($str)
  83. {
  84. // ГОСТ 7.79B
  85. $transliteration = array(
  86. 'А' => 'A', 'а' => 'a',
  87. 'Б' => 'B', 'б' => 'b',
  88. 'В' => 'V', 'в' => 'v',
  89. 'Г' => 'G', 'г' => 'g',
  90. 'Д' => 'D', 'д' => 'd',
  91. 'Е' => 'E', 'е' => 'e',
  92. 'Ё' => 'Yo', 'ё' => 'yo',
  93. 'Ж' => 'Zh', 'ж' => 'zh',
  94. 'З' => 'Z', 'з' => 'z',
  95. 'И' => 'I', 'и' => 'i',
  96. 'Й' => 'J', 'й' => 'j',
  97. 'К' => 'K', 'к' => 'k',
  98. 'Л' => 'L', 'л' => 'l',
  99. 'М' => 'M', 'м' => 'm',
  100. 'Н' => "N", 'н' => 'n',
  101. 'О' => 'O', 'о' => 'o',
  102. 'П' => 'P', 'п' => 'p',
  103. 'Р' => 'R', 'р' => 'r',
  104. 'С' => 'S', 'с' => 's',
  105. 'Т' => 'T', 'т' => 't',
  106. 'У' => 'U', 'у' => 'u',
  107. 'Ф' => 'F', 'ф' => 'f',
  108. 'Х' => 'H', 'х' => 'h',
  109. 'Ц' => 'Cz', 'ц' => 'cz',
  110. 'Ч' => 'Ch', 'ч' => 'ch',
  111. 'Ш' => 'Sh', 'ш' => 'sh',
  112. 'Щ' => 'Shh', 'щ' => 'shh',
  113. 'Ъ' => 'ʺ', 'ъ' => 'ʺ',
  114. 'Ы' => 'Y`', 'ы' => 'y`',
  115. 'Ь' => '', 'ь' => '',
  116. 'Э' => 'E`', 'э' => 'e`',
  117. 'Ю' => 'Yu', 'ю' => 'yu',
  118. 'Я' => 'Ya', 'я' => 'ya',
  119. '№' => '#', 'Ӏ' => '‡',
  120. '’' => '`', 'ˮ' => '¨',
  121. );
  122. $str = strtr($str, $transliteration);
  123. $str = mb_strtolower($str, 'UTF-8');
  124. $str = preg_replace('/[^0-9a-z.\-]/', '', $str);
  125. $str = preg_replace('|([-]+)|s', '-', $str);
  126. $str = trim($str, '-');
  127. //return time() . '_' . $str;
  128. return $str;
  129. }
  130. public function fileName()
  131. {
  132. return Yii::$app->user->identity->username.date('ymdhis'); //dir name
  133. }
  134. public function uploadFile()
  135. {
  136. $docs = UploadedFile::getInstances($this, 'file');
  137. $fileName = $this->fileName();
  138. $dir = FileHelper::createDirectory($this->uploads.$fileName); //create dir
  139. $saved = false;
  140. foreach ($docs as $k => $file){
  141. $file->name = $this->transliteration($file->name);
  142. $saved = $file->saveAs($this->uploads.$fileName.'/'. $file->name) || $saved ; // move upload file to dir
  143. }
  144. /**
  145. * CREATE ZIP
  146. */
  147. $createZip = new createDirZip();
  148. $createZip->get_files_from_folder($this->uploads . $fileName.'/','');
  149. /**
  150. * DElETE UPLOADS DIR
  151. */
  152. FileHelper::removeDirectory($this->uploads.$fileName);
  153. $fileName .= '.zip';
  154. $fd = fopen($this->uploads.$fileName, 'wb');
  155. $out = fwrite ($fd, $createZip->getZippedfile());
  156. fclose ($fd);
  157. //$createZip->forceDownload($fileName); // start download archive
  158. return $saved ? $fileName : false;
  159. }
  160. public function getUser()
  161. {
  162. return $this->hasOne(User::className(),['id' => 'user_id']);
  163. }
  164. public function copy($lot_id)
  165. {
  166. $path = str_replace($this->lot_id,$lot_id,$this->path.$this->name);
  167. //string(38) "../uploads/lots/neiron180223093830.zip"
  168. $directoryPath = explode('/', $path);
  169. //array(4) { [0]=> string(2) ".." [1]=> string(7) "uploads" [2]=> string(4) "lots" [3]=> string(22) "neiron180223093830.zip" }
  170. $name = end($directoryPath);
  171. //string(22) "neiron180223093830.zip"
  172. array_pop($directoryPath);
  173. //string(4) "lots"
  174. $directoryPath = implode('/', $directoryPath).'/clone/'.$this->lot_id.'_'.date('now').'/';
  175. //string(51) "../uploads/lots/clone/neiron180223093830.zip220185/"
  176. //var_dump($directoryPath);die();
  177. $copyFileName = $directoryPath.$name;
  178. FileHelper::createDirectory($directoryPath);
  179. copy($this->path.$this->name,$copyFileName);
  180. $model = new Files();
  181. $data = $this->attributes;
  182. $data['path'] = $directoryPath;
  183. $data['name'] = $name;
  184. $data['lot_id'] = $lot_id;
  185. $model->load($data, '');
  186. return $model->save(false) ? $model->id : false;
  187. }
  188. }