1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @link https://github.com/rkit/filemanager-yii2 |
5
|
|
|
* @copyright Copyright (c) 2015 Igor Romanov |
6
|
|
|
* @license [MIT](http://opensource.org/licenses/MIT) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace rkit\filemanager\behaviors; |
10
|
|
|
|
11
|
|
|
use yii\helpers\ArrayHelper; |
12
|
|
|
use rkit\filemanager\models\File; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The bind class for FileBehavior |
16
|
|
|
*/ |
17
|
|
|
class FileBind |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Bind the file to the with owner |
21
|
|
|
* @internal |
22
|
|
|
* |
23
|
|
|
* @param Storage $storage The storage for the file |
24
|
|
|
* @param int $ownerId The id of the owner |
25
|
|
|
* @param int $ownerType The type of the owner |
26
|
|
|
* @param int $fileId The id of the file |
27
|
|
|
* @return rkit\filemanager\models\File|bool |
28
|
|
|
*/ |
29
|
20 |
|
public function bindSingle($storage, $ownerId, $ownerType, $fileId) |
30
|
|
|
{ |
31
|
20 |
|
$file = File::findOne($fileId); |
32
|
20 |
|
if ($file && $file->isOwner($ownerId, $ownerType)) { |
|
|
|
|
33
|
16 |
|
$file->setStorage($storage); |
|
|
|
|
34
|
16 |
|
if ($file->tmp) { |
35
|
16 |
|
$file = $this->saveTmpDirToStorage($file, $ownerId); |
|
|
|
|
36
|
16 |
|
if ($file) { |
37
|
16 |
|
$this->deleteCurrentFiles($storage, $ownerId, $ownerType, [$file->id => $file]); |
|
|
|
|
38
|
16 |
|
$file->updateAttributes($file->getDirtyAttributes()); |
39
|
16 |
|
$file->setStorage($storage); |
40
|
16 |
|
} |
41
|
16 |
|
} |
42
|
16 |
|
return $file; |
43
|
|
|
} |
44
|
|
|
|
45
|
6 |
|
return false; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Bind files to the with owner |
50
|
|
|
* |
51
|
|
|
* @param Storage $storage The storage for the files |
52
|
|
|
* @param int $ownerId The id of the owner |
53
|
|
|
* @param int $ownerType The type of the owner |
54
|
|
|
* @param array $files Array of ids |
55
|
|
|
* @return rkit\filemanager\models\File[]|bool |
56
|
|
|
* @SuppressWarnings(PHPMD.ElseExpression) |
57
|
|
|
*/ |
58
|
3 |
|
public function bindMultiple($storage, $ownerId, $ownerType, $files) |
59
|
|
|
{ |
60
|
3 |
|
$newFiles = ArrayHelper::index(File::findAll(array_keys($files)), 'id'); |
61
|
|
|
|
62
|
3 |
|
if (count($newFiles)) { |
63
|
3 |
|
foreach ($newFiles as $fileId => $file) { |
64
|
3 |
|
if ($file->isOwner($ownerId, $ownerType)) { |
65
|
3 |
|
$file->setStorage($storage); |
66
|
3 |
|
if ($file->tmp) { |
67
|
3 |
|
$file = $this->saveTmpDirToStorage($file, $ownerId); |
68
|
3 |
|
} |
69
|
3 |
|
if ($file) { |
70
|
3 |
|
$file->position = @array_search($file->id, array_keys($files)) + 1; |
71
|
3 |
|
$file->title = ArrayHelper::getValue($files, $file->id, $file->title); |
72
|
3 |
|
$file->updateAttributes($file->getDirtyAttributes()); |
73
|
3 |
|
continue; |
74
|
|
|
} |
75
|
1 |
|
} |
76
|
1 |
|
unset($newFiles[$fileId]); |
77
|
1 |
|
continue; |
78
|
3 |
|
} |
79
|
3 |
|
$this->deleteCurrentFiles($storage, $ownerId, $ownerType, $newFiles); |
80
|
3 |
|
} else { |
81
|
1 |
|
$this->deleteCurrentFiles($storage, $ownerId, $ownerType); |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
return count($newFiles) ? $newFiles : false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Save the temporary file to the storage |
89
|
|
|
* |
90
|
|
|
* @param File $file |
91
|
|
|
* @param int $ownerId The id of the owner |
92
|
|
|
* @return rkit\filemanager\models\File|bool |
93
|
|
|
*/ |
94
|
19 |
|
private function saveTmpDirToStorage(File $file, $ownerId) |
95
|
|
|
{ |
96
|
19 |
|
$file->owner_id = $ownerId; |
97
|
19 |
|
$file->tmp = false; |
98
|
19 |
|
if ($file->getStorage()->saveTemporaryFileToStorage()) { |
99
|
19 |
|
return $file; |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Delete current files |
107
|
|
|
* |
108
|
|
|
* @param Storage $storage |
109
|
|
|
* @param int $ownerId The id of the owner |
110
|
|
|
* @param int $ownerType The type of the owner |
111
|
|
|
* @param rkit\filemanager\models\File[] $exceptFiles |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
19 |
|
private function deleteCurrentFiles($storage, $ownerId, $ownerType, $exceptFiles = []) |
115
|
|
|
{ |
116
|
19 |
|
$currentFiles = File::findAllByOwner($ownerId, $ownerType); |
117
|
19 |
|
foreach ($currentFiles as $currFile) { |
118
|
5 |
|
$isExceptFiles = count($exceptFiles) && array_key_exists($currFile->id, $exceptFiles); |
119
|
5 |
|
if (!$isExceptFiles) { |
120
|
4 |
|
$currFile->setStorage($storage); |
121
|
4 |
|
$currFile->delete(); |
122
|
4 |
|
} |
123
|
19 |
|
} |
124
|
19 |
|
} |
125
|
|
|
} |
126
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.