Files.php 7.5 KB

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