Skip to content

Commit 299f437

Browse files
authored
Merge pull request #69 from NetCommons3/bugfix_20250930
fix: 汎用データベースの編集画面で「登録済みのファイルを削除する」にチェックを付けても添付ファイルが削除されない
2 parents d292f5a + 49cd050 commit 299f437

3 files changed

Lines changed: 48 additions & 6 deletions

File tree

.github/workflows/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ jobs:
3030
runs-on: ubuntu-latest
3131
strategy:
3232
matrix:
33-
php: [ '7.1', '7.2', '7.3', '7.4' ]
34-
mysql: [ '5.7', '8.0' ]
33+
php: [ '7.4' ]
34+
mysql: [ '8.0' ]
3535

3636
env:
3737
NC3_BUILD_DIR: "/opt/nc3"

Model/MultidatabaseContent.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,14 @@ public function saveContent($data, $isUpdate) {
288288
}
289289
$data = $this->data;
290290

291+
// $this->dataには、クレンジングされたデータが保持されており、添付ファイルの削除フラグが消されてしまっている。
292+
// 後続の処理で、添付ファイルを削除する際に、`$data['value5__attach_del'] = 'on'`がないと削除してくれないため、
293+
// $dataを引数$dataにある値を戻す。
294+
foreach ($deleteFiles as $colNo) {
295+
$delColumn = 'value' . $colNo . '_attach';
296+
$data[$delColumn . '_del'] = 'on';
297+
}
298+
291299
$result = $this->MultidatabaseContentEdit->makeSaveData($data, $metadatas, $isUpdate);
292300

293301
return $this->__saveContent($result);
@@ -365,7 +373,7 @@ public function deleteContentByKey($key) {
365373
$this->deleteCommentsByContentKey($key);
366374

367375
// 添付ファイルの削除
368-
if (! $this->MultidatabaseContentFile->removeFileByContentKey($key)) {
376+
if (! $this->MultidatabaseContentFile->removeFilesByContentKey($key)) {
369377
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
370378
}
371379

@@ -470,14 +478,16 @@ private function __saveContent($data) {
470478
$attachPasswords
471479
);
472480

473-
$this->commit();
474-
475481
// ファイルを削除する
476482
if (! empty($removeAttachFields)) {
477483
$this->MultidatabaseContentFile->removeAttachFile(
478-
$removeAttachFields, $data['MultidatabaseContent']['key']);
484+
$removeAttachFields,
485+
$data['MultidatabaseContent']['key']
486+
);
479487
}
480488

489+
$this->commit();
490+
481491
} catch (Exception $e) {
482492
$this->rollback($e);
483493
}

Model/MultidatabaseContentFile.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,38 @@ public function removeFileByContentKey($key, $fieldName = '') {
241241
return $this->__removeFile($fileInfo, $fieldName);
242242
}
243243

244+
/**
245+
* Remove File(s)
246+
* ファイルを削除する(コンテンツKeyより)
247+
*
248+
* @param string $contentKey コンテンツKey
249+
* @return bool
250+
*/
251+
public function removeFilesByContentKey($contentKey) {
252+
$UploadFile = ClassRegistry::init('Files.UploadFile');
253+
$pluginKey = 'multidatabases';
254+
255+
$options = [
256+
'recursive' => -1,
257+
'fields' => [
258+
'UploadFile.id',
259+
],
260+
'conditions' => [
261+
'UploadFile.plugin_key' => $pluginKey,
262+
'UploadFile.content_key' => $contentKey,
263+
],
264+
'callbacks' => false,
265+
];
266+
267+
$files = $UploadFile->find('all', $options);
268+
269+
foreach ($files as $file) {
270+
$UploadFile->deleteUploadFile($file['UploadFile']['id']);
271+
}
272+
273+
return true;
274+
}
275+
244276
/**
245277
* RemoveFile(s) Base
246278
* ファイルを削除する

0 commit comments

Comments
 (0)