@@ -17,8 +17,9 @@ discard block |
||
| 17 | 17 | use yii\helpers\Html; |
| 18 | 18 | use yii\web\View; |
| 19 | 19 | |
| 20 | -if (is_array($model)) |
|
| 20 | +if (is_array($model)) { |
|
| 21 | 21 | $model = $model[0]; |
| 22 | +} |
|
| 22 | 23 | |
| 23 | 24 | $doc_contents = [ |
| 24 | 25 | 'application/msword', |
@@ -38,7 +39,8 @@ discard block |
||
| 38 | 39 | <?= Html::hiddenInput((new ReflectionClass($model->class))->getShortName() . "[{$model->field}_ids][]", $model->id) ?> |
| 39 | 40 | </div> |
| 40 | 41 | |
| 41 | - <?php else: ?> |
|
| 42 | + <?php else { |
|
| 43 | + : ?> |
|
| 42 | 44 | |
| 43 | 45 | <div data-title="<?= $model->title ?>" |
| 44 | 46 | id="yii2-file-object-<?= $model->id ?>" |
@@ -52,7 +54,9 @@ discard block |
||
| 52 | 54 | <?php if ($model->type != FileType::IMAGE): ?> |
| 53 | 55 | <?= $model->icon ?> |
| 54 | 56 | <?= $model->title ?> |
| 55 | - <?php endif; ?> |
|
| 57 | + <?php endif; |
|
| 58 | +} |
|
| 59 | +?> |
|
| 56 | 60 | </div> |
| 57 | 61 | <?php endif; ?> |
| 58 | 62 | |
@@ -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) { |
@@ -33,11 +33,13 @@ discard block |
||
| 33 | 33 | { |
| 34 | 34 | $this->_file = $file; |
| 35 | 35 | |
| 36 | - if ($this->_file->type != FileType::IMAGE) |
|
| 37 | - throw new ErrorException('This file is not an image.'); |
|
| 36 | + if ($this->_file->type != FileType::IMAGE) { |
|
| 37 | + throw new ErrorException('This file is not an image.'); |
|
| 38 | + } |
|
| 38 | 39 | |
| 39 | - if (!file_exists($this->_file->rootPath)) |
|
| 40 | - throw new ErrorException('File not found on disk'); |
|
| 40 | + if (!file_exists($this->_file->rootPath)) { |
|
| 41 | + throw new ErrorException('File not found on disk'); |
|
| 42 | + } |
|
| 41 | 43 | |
| 42 | 44 | $this->_maxHeight = $maxHeight; |
| 43 | 45 | $this->_maxWidth = $maxWidth; |
@@ -51,16 +53,18 @@ discard block |
||
| 51 | 53 | */ |
| 52 | 54 | public function execute(): bool |
| 53 | 55 | { |
| 54 | - if ($this->_file->content_type == 'image/svg+xml') |
|
| 55 | - return true; |
|
| 56 | + if ($this->_file->content_type == 'image/svg+xml') { |
|
| 57 | + return true; |
|
| 58 | + } |
|
| 56 | 59 | |
| 57 | 60 | $image = new SimpleImage(); |
| 58 | 61 | $image->load($this->_file->rootPath); |
| 59 | 62 | |
| 60 | 63 | if ($image->getWidth() > $this->_maxWidth || $image->getHeight() > $this->_maxHeight) { |
| 61 | 64 | $image->resizeToWidth($this->_maxWidth); |
| 62 | - if ($this->_file->content_type == 'image/png') |
|
| 63 | - $this->_imageType = IMAGETYPE_PNG; |
|
| 65 | + if ($this->_file->content_type == 'image/png') { |
|
| 66 | + $this->_imageType = IMAGETYPE_PNG; |
|
| 67 | + } |
|
| 64 | 68 | $image->save($this->_file->rootPath, $this->_imageType, $this->_compression); |
| 65 | 69 | $this->_file->size = filesize($this->_file->rootPath); |
| 66 | 70 | return $this->_file->save(false, ['size']); |
@@ -66,9 +66,11 @@ discard block |
||
| 66 | 66 | ] |
| 67 | 67 | )->execute(); |
| 68 | 68 | |
| 69 | - if ($ids) foreach ($ids as $id) { |
|
| 69 | + if ($ids) { |
|
| 70 | + foreach ($ids as $id) { |
|
| 70 | 71 | if (empty($id)) |
| 71 | 72 | continue; |
| 73 | + } |
|
| 72 | 74 | $file = File::findOne($id); |
| 73 | 75 | if ($file) { |
| 74 | 76 | $file->object_id = $this->owner->id; |
@@ -103,8 +105,9 @@ discard block |
||
| 103 | 105 | in_array($this->owner->scenario, $params['requiredOn']) && |
| 104 | 106 | !in_array($this->owner->scenario, $params['requiredExcept']) && |
| 105 | 107 | !isset($this->_values[$attributeIds][1]) |
| 106 | - ) |
|
| 107 | - $this->owner->addError($attributeName, $params['requiredMessage']); |
|
| 108 | + ) { |
|
| 109 | + $this->owner->addError($attributeName, $params['requiredMessage']); |
|
| 110 | + } |
|
| 108 | 111 | } |
| 109 | 112 | } |
| 110 | 113 | |
@@ -121,8 +124,8 @@ discard block |
||
| 121 | 124 | $validators = $owner->validators; |
| 122 | 125 | |
| 123 | 126 | // Пробегаемся по валидаторам и вычисляем, какие из них касаются наших файл-полей |
| 124 | - if ($validators) |
|
| 125 | - foreach ($validators as $key => $validator) { |
|
| 127 | + if ($validators) { |
|
| 128 | + foreach ($validators as $key => $validator) { |
|
| 126 | 129 | |
| 127 | 130 | // Сначала пробегаемся по файловым валидаторам |
| 128 | 131 | if ($validator::className() == 'yii\validators\FileValidator' || $validator::className() == 'floor12\files\components\ReformatValidator') { |
@@ -130,6 +133,7 @@ discard block |
||
| 130 | 133 | |
| 131 | 134 | if (is_string($params)) { |
| 132 | 135 | $field = $params; |
| 136 | + } |
|
| 133 | 137 | $params = []; |
| 134 | 138 | } |
| 135 | 139 | |
@@ -165,8 +169,10 @@ discard block |
||
| 165 | 169 | } |
| 166 | 170 | |
| 167 | 171 | // Добавляем дефолтный валидатор для прилетающих айдишников |
| 168 | - if ($this->attributes) foreach ($this->attributes as $fieldName => $fieldParams) { |
|
| 172 | + if ($this->attributes) { |
|
| 173 | + foreach ($this->attributes as $fieldName => $fieldParams) { |
|
| 169 | 174 | $validator = Validator::createValidator('safe', $owner, ["{$fieldName}_ids"]); |
| 175 | + } |
|
| 170 | 176 | $validators->append($validator); |
| 171 | 177 | } |
| 172 | 178 | } |
@@ -187,8 +193,9 @@ discard block |
||
| 187 | 193 | */ |
| 188 | 194 | public function canSetProperty($name, $checkVars = true) |
| 189 | 195 | { |
| 190 | - if (array_key_exists($this->getRealAttributeName($name), $this->attributes)) |
|
| 191 | - return true; |
|
| 196 | + if (array_key_exists($this->getRealAttributeName($name), $this->attributes)) { |
|
| 197 | + return true; |
|
| 198 | + } |
|
| 192 | 199 | |
| 193 | 200 | return parent::canSetProperty($name, $checkVars = true); |
| 194 | 201 | } |
@@ -213,8 +220,8 @@ discard block |
||
| 213 | 220 | isset($this->attributes[$att_name]['validator']) && |
| 214 | 221 | isset($this->attributes[$att_name]['validator']['yii\validators\FileValidator']) && |
| 215 | 222 | $this->attributes[$att_name]['validator']['yii\validators\FileValidator']->maxFiles > 1 |
| 216 | - ) |
|
| 217 | - return File::find() |
|
| 223 | + ) { |
|
| 224 | + return File::find() |
|
| 218 | 225 | ->where( |
| 219 | 226 | [ |
| 220 | 227 | 'object_id' => $this->owner->id, |
@@ -223,7 +230,8 @@ discard block |
||
| 223 | 230 | ]) |
| 224 | 231 | ->orderBy('ordering ASC') |
| 225 | 232 | ->all(); |
| 226 | - else return File::find() |
|
| 233 | + } else { |
|
| 234 | + return File::find() |
|
| 227 | 235 | ->where( |
| 228 | 236 | [ |
| 229 | 237 | 'object_id' => $this->owner->id, |
@@ -232,6 +240,7 @@ discard block |
||
| 232 | 240 | ]) |
| 233 | 241 | ->orderBy('ordering ASC') |
| 234 | 242 | ->one(); |
| 243 | + } |
|
| 235 | 244 | } |
| 236 | 245 | } |
| 237 | 246 | |
@@ -244,8 +253,9 @@ discard block |
||
| 244 | 253 | { |
| 245 | 254 | $attribute = $this->getRealAttributeName($name); |
| 246 | 255 | |
| 247 | - if (array_key_exists($attribute, $this->attributes)) |
|
| 248 | - $this->_values[$attribute] = $value; |
|
| 256 | + if (array_key_exists($attribute, $this->attributes)) { |
|
| 257 | + $this->_values[$attribute] = $value; |
|
| 258 | + } |
|
| 249 | 259 | } |
| 250 | 260 | |
| 251 | 261 | |
@@ -31,8 +31,9 @@ discard block |
||
| 31 | 31 | $this->width = $width; |
| 32 | 32 | $this->webp = $webp; |
| 33 | 33 | |
| 34 | - if ($this->model->type != FileType::IMAGE) |
|
| 35 | - throw new ErrorException('File is not an image.'); |
|
| 34 | + if ($this->model->type != FileType::IMAGE) { |
|
| 35 | + throw new ErrorException('File is not an image.'); |
|
| 36 | + } |
|
| 36 | 37 | } |
| 37 | 38 | |
| 38 | 39 | /** |
@@ -40,8 +41,9 @@ discard block |
||
| 40 | 41 | */ |
| 41 | 42 | public function getUrl() |
| 42 | 43 | { |
| 43 | - if ($this->model->isSvg()) |
|
| 44 | - return $this->model->getRootPath(); |
|
| 44 | + if ($this->model->isSvg()) { |
|
| 45 | + return $this->model->getRootPath(); |
|
| 46 | + } |
|
| 45 | 47 | |
| 46 | 48 | $this->fileName = Yii::$app->getModule('files')->cacheFullPath . DIRECTORY_SEPARATOR . $this->model->makeNameWithSize($this->model->filename, |
| 47 | 49 | $this->width, 0); |
@@ -50,14 +52,17 @@ discard block |
||
| 50 | 52 | |
| 51 | 53 | $this->prepareFolder(); |
| 52 | 54 | |
| 53 | - if (!file_exists($this->fileName) || filesize($this->fileName) == 0) |
|
| 54 | - $this->createPreview(); |
|
| 55 | + if (!file_exists($this->fileName) || filesize($this->fileName) == 0) { |
|
| 56 | + $this->createPreview(); |
|
| 57 | + } |
|
| 55 | 58 | |
| 56 | - if ($this->webp && !file_exists($this->fileNameWebp)) |
|
| 57 | - $this->createPreviewWebp(); |
|
| 59 | + if ($this->webp && !file_exists($this->fileNameWebp)) { |
|
| 60 | + $this->createPreviewWebp(); |
|
| 61 | + } |
|
| 58 | 62 | |
| 59 | - if ($this->webp) |
|
| 60 | - return $this->fileNameWebp; |
|
| 63 | + if ($this->webp) { |
|
| 64 | + return $this->fileNameWebp; |
|
| 65 | + } |
|
| 61 | 66 | |
| 62 | 67 | return $this->fileName; |
| 63 | 68 | } |
@@ -79,8 +84,9 @@ discard block |
||
| 79 | 84 | } |
| 80 | 85 | |
| 81 | 86 | $saveType = $img->image_type; |
| 82 | - if ($saveType == IMG_WEBP || $saveType == IMG_QUADRATIC) |
|
| 83 | - $saveType = IMG_JPEG; |
|
| 87 | + if ($saveType == IMG_WEBP || $saveType == IMG_QUADRATIC) { |
|
| 88 | + $saveType = IMG_JPEG; |
|
| 89 | + } |
|
| 84 | 90 | $img->save($this->fileName, $saveType); |
| 85 | 91 | } |
| 86 | 92 | |
@@ -99,20 +105,24 @@ discard block |
||
| 99 | 105 | */ |
| 100 | 106 | protected function prepareFolder() |
| 101 | 107 | { |
| 102 | - if (!file_exists(Yii::$app->getModule('files')->cacheFullPath)) |
|
| 103 | - mkdir(Yii::$app->getModule('files')->cacheFullPath); |
|
| 108 | + if (!file_exists(Yii::$app->getModule('files')->cacheFullPath)) { |
|
| 109 | + mkdir(Yii::$app->getModule('files')->cacheFullPath); |
|
| 110 | + } |
|
| 104 | 111 | $folders = []; |
| 105 | 112 | $lastFolder = '/'; |
| 106 | 113 | $explodes = explode('/', $this->fileName); |
| 107 | 114 | array_pop($explodes); |
| 108 | - if (empty($explodes)) |
|
| 109 | - return; |
|
| 115 | + if (empty($explodes)) { |
|
| 116 | + return; |
|
| 117 | + } |
|
| 110 | 118 | foreach ($explodes as $folder) { |
| 111 | - if (empty($folder)) |
|
| 112 | - continue; |
|
| 119 | + if (empty($folder)) { |
|
| 120 | + continue; |
|
| 121 | + } |
|
| 113 | 122 | $lastFolder = $lastFolder . $folder . '/'; |
| 114 | - if (!file_exists($lastFolder)) |
|
| 115 | - mkdir($lastFolder); |
|
| 123 | + if (!file_exists($lastFolder)) { |
|
| 124 | + mkdir($lastFolder); |
|
| 125 | + } |
|
| 116 | 126 | $folders[] = $lastFolder; |
| 117 | 127 | } |
| 118 | 128 | } |