@@ -34,27 +34,31 @@ discard block |
||
| 34 | 34 | |
| 35 | 35 | $this->_onlyUploaded = $onlyUploaded; |
| 36 | 36 | |
| 37 | - if (!isset($data['attribute']) || !$data['attribute'] || !isset($data['modelClass']) || !$data['modelClass']) |
|
| 38 | - throw new BadRequestHttpException("Attribute or class name not set."); |
|
| 37 | + if (!isset($data['attribute']) || !$data['attribute'] || !isset($data['modelClass']) || !$data['modelClass']) { |
|
| 38 | + throw new BadRequestHttpException("Attribute or class name not set."); |
|
| 39 | + } |
|
| 39 | 40 | |
| 40 | 41 | // Загружаем полученные данные |
| 41 | 42 | $this->_instance = $file; |
| 42 | 43 | $this->_attribute = $data['attribute']; |
| 43 | 44 | |
| 44 | - if (!file_exists($this->_instance->tempName)) |
|
| 45 | - throw new ErrorException("Tmp file not found on disk."); |
|
| 45 | + if (!file_exists($this->_instance->tempName)) { |
|
| 46 | + throw new ErrorException("Tmp file not found on disk."); |
|
| 47 | + } |
|
| 46 | 48 | |
| 47 | 49 | // Инициализируем класс владельца файла для валидаций и ставим сценарий |
| 48 | 50 | $this->_owner = new $data['modelClass']; |
| 49 | 51 | |
| 50 | - if (isset($data['scenario'])) |
|
| 51 | - $this->_owner->setScenario($data['scenario']); |
|
| 52 | + if (isset($data['scenario'])) { |
|
| 53 | + $this->_owner->setScenario($data['scenario']); |
|
| 54 | + } |
|
| 52 | 55 | |
| 53 | 56 | |
| 54 | 57 | if (isset($this->_owner->behaviors['files']->attributes[$this->_attribute]['validator'])) { |
| 55 | 58 | foreach ($this->_owner->behaviors['files']->attributes[$this->_attribute]['validator'] as $validator) { |
| 56 | - if (!$validator->validate($this->_instance, $error)) |
|
| 57 | - throw new BadRequestHttpException($error); |
|
| 59 | + if (!$validator->validate($this->_instance, $error)) { |
|
| 60 | + throw new BadRequestHttpException($error); |
|
| 61 | + } |
|
| 58 | 62 | } |
| 59 | 63 | |
| 60 | 64 | } |
@@ -70,10 +74,12 @@ discard block |
||
| 70 | 74 | $this->_model->content_type = $this->_instance->type; |
| 71 | 75 | $this->_model->size = $this->_instance->size; |
| 72 | 76 | $this->_model->type = $this->detectType(); |
| 73 | - if ($identity) |
|
| 74 | - $this->_model->user_id = $identity->id; |
|
| 75 | - if ($this->_model->type == FileType::VIDEO) |
|
| 76 | - $this->_model->video_status = 0; |
|
| 77 | + if ($identity) { |
|
| 78 | + $this->_model->user_id = $identity->id; |
|
| 79 | + } |
|
| 80 | + if ($this->_model->type == FileType::VIDEO) { |
|
| 81 | + $this->_model->video_status = 0; |
|
| 82 | + } |
|
| 77 | 83 | |
| 78 | 84 | //Генерируем полный новый адрес сохранения файла |
| 79 | 85 | $this->_fullPath = Yii::$app->getModule('files')->storageFullPath . DIRECTORY_SEPARATOR . $this->_model->filename; |
@@ -85,10 +91,12 @@ discard block |
||
| 85 | 91 | public function detectType() |
| 86 | 92 | { |
| 87 | 93 | $contentTypeArray = explode('/', $this->_model->content_type); |
| 88 | - if ($contentTypeArray[0] == 'image') |
|
| 89 | - return FileType::IMAGE; |
|
| 90 | - if ($contentTypeArray[0] == 'video') |
|
| 91 | - return FileType::VIDEO; |
|
| 94 | + if ($contentTypeArray[0] == 'image') { |
|
| 95 | + return FileType::IMAGE; |
|
| 96 | + } |
|
| 97 | + if ($contentTypeArray[0] == 'video') { |
|
| 98 | + return FileType::VIDEO; |
|
| 99 | + } |
|
| 92 | 100 | return FileType::FILE; |
| 93 | 101 | } |
| 94 | 102 | |
@@ -101,10 +109,11 @@ discard block |
||
| 101 | 109 | $path = Yii::$app->getModule('files')->storageFullPath . $this->_model->filename; |
| 102 | 110 | |
| 103 | 111 | if ($this->_model->save()) { |
| 104 | - if (!$this->_onlyUploaded) |
|
| 105 | - copy($this->_instance->tempName, $this->_fullPath); |
|
| 106 | - else |
|
| 107 | - $this->_instance->saveAs($this->_fullPath, false); |
|
| 112 | + if (!$this->_onlyUploaded) { |
|
| 113 | + copy($this->_instance->tempName, $this->_fullPath); |
|
| 114 | + } else { |
|
| 115 | + $this->_instance->saveAs($this->_fullPath, false); |
|
| 116 | + } |
|
| 108 | 117 | } |
| 109 | 118 | |
| 110 | 119 | if ($this->_model->type == FileType::IMAGE) { |