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
|
19 |
|
public function bindSingle($storage, $ownerId, $ownerType, $fileId) |
30
|
|
|
{ |
31
|
19 |
|
$file = File::findOne($fileId); |
32
|
19 |
|
if ($file && $file->isOwner($ownerId, $ownerType)) { |
|
|
|
|
33
|
15 |
|
$file->setStorage($storage); |
|
|
|
|
34
|
15 |
|
if ($file->tmp) { |
35
|
15 |
|
$file = $this->saveTmpDirToStorage($file, $ownerId); |
|
|
|
|
36
|
15 |
|
if ($file) { |
37
|
15 |
|
$this->deleteCurrentFiles($storage, $ownerId, $ownerType, [$file->id => $file]); |
|
|
|
|
38
|
15 |
|
$file->updateAttributes($file->getDirtyAttributes()); |
39
|
15 |
|
$file->setStorage($storage); |
40
|
15 |
|
} |
41
|
15 |
|
} |
42
|
15 |
|
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
|
|
|
*/ |
57
|
3 |
|
public function bindMultiple($storage, $ownerId, $ownerType, $files) |
58
|
|
|
{ |
59
|
3 |
|
$newFiles = ArrayHelper::index(File::findAll(array_keys($files)), 'id'); |
60
|
|
|
|
61
|
3 |
|
if (count($newFiles)) { |
62
|
3 |
|
foreach ($newFiles as $fileId => $file) { |
63
|
3 |
|
if ($file->isOwner($ownerId, $ownerType)) { |
64
|
3 |
|
$file->setStorage($storage); |
65
|
3 |
|
if ($file->tmp) { |
66
|
3 |
|
$file = $this->saveTmpDirToStorage($file, $ownerId); |
67
|
3 |
|
} |
68
|
3 |
|
if ($file) { |
69
|
3 |
|
$file->position = @array_search($file->id, array_keys($files)) + 1; |
70
|
3 |
|
$file->title = ArrayHelper::getValue($files, $file->id, $file->title); |
71
|
3 |
|
$file->updateAttributes($file->getDirtyAttributes()); |
72
|
3 |
|
continue; |
73
|
|
|
} |
74
|
1 |
|
} |
75
|
1 |
|
unset($newFiles[$fileId]); |
76
|
1 |
|
continue; |
77
|
3 |
|
} |
78
|
3 |
|
$this->deleteCurrentFiles($storage, $ownerId, $ownerType, $newFiles); |
79
|
3 |
|
} else { |
80
|
1 |
|
$this->deleteCurrentFiles($storage, $ownerId, $ownerType); |
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
return count($newFiles) ? $newFiles : false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Save the temporary file to the storage |
88
|
|
|
* |
89
|
|
|
* @param File $file |
90
|
|
|
* @param int $ownerId The id of the owner |
91
|
|
|
* @return rkit\filemanager\models\File|bool |
92
|
|
|
*/ |
93
|
18 |
|
private function saveTmpDirToStorage(File $file, $ownerId) |
94
|
|
|
{ |
95
|
18 |
|
$file->owner_id = $ownerId; |
96
|
18 |
|
$file->tmp = false; |
97
|
18 |
|
if ($file->getStorage()->saveTemporaryFileToStorage()) { |
98
|
18 |
|
return $file; |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Delete current files |
106
|
|
|
* |
107
|
|
|
* @param Storage $storage |
108
|
|
|
* @param int $ownerId The id of the owner |
109
|
|
|
* @param int $ownerType The type of the owner |
110
|
|
|
* @param rkit\filemanager\models\File[] $exceptFiles |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
18 |
|
private function deleteCurrentFiles($storage, $ownerId, $ownerType, $exceptFiles = []) |
114
|
|
|
{ |
115
|
18 |
|
$currentFiles = File::findAllByOwner($ownerId, $ownerType); |
116
|
18 |
|
foreach ($currentFiles as $currFile) { |
117
|
4 |
|
$isExceptFiles = count($exceptFiles) && array_key_exists($currFile->id, $exceptFiles); |
118
|
4 |
|
if (!$isExceptFiles) { |
119
|
3 |
|
$currFile->setStorage($storage); |
120
|
3 |
|
$currFile->delete(); |
121
|
3 |
|
} |
122
|
18 |
|
} |
123
|
18 |
|
} |
124
|
|
|
} |
125
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.