Skip to content

Commit c86a117

Browse files
committed
Fix multiple files
1 parent e7804d6 commit c86a117

File tree

5 files changed

+44
-12
lines changed

5 files changed

+44
-12
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Add the following in your config, in section `components`
105105
// save file id in this table
106106
// 'saveFileId' => true,
107107

108-
// protected a file
108+
// inaccessible from the web
109109
// 'protected' => true,
110110

111111
// @see http://www.yiiframework.com/doc-2.0/guide-tutorial-core-validators.html
@@ -199,7 +199,7 @@ Add the following in your config, in section `components`
199199
<a href="<?= $file->path()?>" target="_blank">
200200
<img src="<?= $model->thumb('gallery', '80x80', $file->path())?>">
201201
</a>
202-
<?= Html::textInput(Html::getInputName($model, $attribute) . '[' . $file->id .']', $file->title, [
202+
<?= Html::textInput(Html::getInputName($model, $attribute) . '[files][' . $file->id .']', $file->title, [
203203
'class' => 'form-control',
204204
])?>
205205
</li>

src/behaviors/FileBehavior.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ private function bindSingleFile($file, $ownerId)
340340
*/
341341
private function bindMultiple($ownerId, $ownerType, $files)
342342
{
343+
$files = ArrayHelper::getValue($files, 'files', []);
343344
$newFiles = ArrayHelper::index(File::findAll(array_keys($files)), 'id');
344345
$currentFiles = ArrayHelper::index(File::getByOwner($ownerId, $ownerType), 'id');
345346

@@ -350,7 +351,7 @@ private function bindMultiple($ownerId, $ownerType, $files)
350351
continue;
351352
}
352353
if (!$this->bindMultipleFile($file, $ownerId, $files)) {
353-
return false;
354+
continue;
354355
}
355356
}
356357

tests/BaseTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ protected function checkUploadFileResponse($response)
139139
*/
140140
protected function checkUploadGalleryResponse($response)
141141
{
142-
preg_match('/News\[(.*?)\]\[(.*?)\]/', $response, $matches);
142+
preg_match('/News\[(.*?)\]\[files\]\[(.*?)\]/', $response, $matches);
143143

144144
$this->assertTrue(is_string($response));
145145
$this->assertTrue(isset($matches[2]));
@@ -193,7 +193,7 @@ protected function uploadGallery($config)
193193
$response = $this->checkUploadGalleryResponse($response);
194194

195195
$file = File::findOne($response[1]);
196-
$model = new News(['title' => 'test', $config['attribute'] => [$file->id => 'test']]);
196+
$model = new News(['title' => 'test', $config['attribute'] => ['files' => [$file->id => 'test']]]);
197197
$ownerType = $model->getFileOwnerType($config['attribute']);
198198

199199
$this->checkTmpFile($file, -1, $ownerType);

tests/GalleryUploadTest.php

+37-6
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function testAnotherOwnerGallery()
8181
'saveAfterUpload' => true
8282
]);
8383

84-
$model->image_gallery = [$response['id'] => 'test'];
84+
$model->image_gallery = ['files' => [$response['id'] => 'test']];
8585
$model->save();
8686

8787
$file = File::findOne($response['id']);
@@ -108,6 +108,34 @@ public function testEmptyGallery()
108108
$this->assertCount(0, $model->getFiles('image_gallery'));
109109
}
110110

111+
public function testRemoveFileFromGallery()
112+
{
113+
extract($this->uploadGallery([
114+
'modelName' => News::className(),
115+
'attribute' => 'image_gallery',
116+
'inputName' => 'file-300',
117+
'multiple' => true,
118+
'template' => Yii::getAlias('@tests/data/views/gallery-item.php')
119+
]));
120+
121+
$this->assertCount(1, $model->getFiles('image_gallery'));
122+
123+
$response = $this->runAction([
124+
'modelName' => News::className(),
125+
'attribute' => 'image_gallery',
126+
'inputName' => 'file-500',
127+
'saveAfterUpload' => true,
128+
'ownerId' => $model->id
129+
]);
130+
131+
$this->assertCount(2, $model->getFiles('image_gallery'));
132+
133+
$model->image_gallery = ['files' => [$response['id'] => 'test']];
134+
$model->save();
135+
136+
$this->assertCount(1, $model->getFiles('image_gallery'));
137+
}
138+
111139
public function testWrongGallery()
112140
{
113141
extract($this->uploadGallery([
@@ -118,7 +146,7 @@ public function testWrongGallery()
118146
'template' => Yii::getAlias('@tests/data/views/gallery-item.php')
119147
]));
120148

121-
$model->image_gallery = ['1000' => 'test'];
149+
$model->image_gallery = ['files' => ['1000' => 'test']];
122150
$model->save();
123151

124152
$this->assertCount(0, $model->getFiles('image_gallery'));
@@ -144,7 +172,11 @@ public function testSaveNotTmpGallery()
144172
'saveAfterUpload' => true
145173
]);
146174

147-
$model->image_gallery = [$response['id'] => 'test'];
175+
$model->image_gallery = ['files' => [
176+
$files[0]->id => 'test2',
177+
$response['id'] => 'test'
178+
]];
179+
148180
$model->save();
149181

150182
$this->assertCount(2, $model->getFiles('image_gallery'));
@@ -171,12 +203,11 @@ public function testFailSaveGallery()
171203
$file = File::findOne($response['id']);
172204
unlink($file->path(true));
173205

174-
$model->image_gallery = [$response['id'] => 'test'];
206+
$model->image_gallery = ['files' => [$response['id'] => 'test']];
175207
$model->save();
176208

177209
$files = $model->getFiles('image_gallery');
178210

179-
$this->assertCount(1, $files);
180-
$this->assertTrue($files[0]->id === $oldFiles[0]->id);
211+
$this->assertCount(0, $files);
181212
}
182213
}

tests/data/views/gallery-item.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<img src="<?= $model->thumb('image_gallery', '80x80', $file->path())?>">
77
</a>
88
<a class="btn btn-lg"><span class="glyphicon glyphicon-remove remove-item" data-remove-item="li"></span></a>
9-
<?= Html::textInput(Html::getInputName($model, $attribute) . '[' . $file->id .']', $file->title, [
9+
<?= Html::textInput(Html::getInputName($model, $attribute) . '[files][' . $file->id .']', $file->title, [
1010
'class' => 'form-control',
1111
])?>
1212
</li>

0 commit comments

Comments
 (0)