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
|
|
|
* This is the bind class for FileBehavior |
16
|
|
|
*/ |
17
|
|
|
class FileBind |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Bind files with owner |
21
|
|
|
* |
22
|
|
|
* @param Storage $storage |
23
|
|
|
* @param int $ownerId |
24
|
|
|
* @param int $ownerType |
25
|
|
|
* @param int|array $fileId |
26
|
|
|
* @return File|File[]|bool |
27
|
|
|
*/ |
28
|
26 |
|
public function setBind($storage, $ownerId, $ownerType, $fileId) |
29
|
|
|
{ |
30
|
26 |
|
if ($fileId === [] || $fileId === '') { |
31
|
3 |
|
(new File())->deleteByOwner($storage, $ownerId, $ownerType); |
32
|
3 |
|
return true; |
33
|
|
|
} |
34
|
|
|
|
35
|
26 |
|
if (is_array($fileId)) { |
36
|
9 |
|
return $this->bindMultiple($storage, $ownerId, $ownerType, $fileId); |
37
|
|
|
} else { |
38
|
17 |
|
return $this->bindSingle($storage, $ownerId, $ownerType, $fileId); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Bind the file to the with owner |
44
|
|
|
* |
45
|
|
|
* @param Storage $storage |
46
|
|
|
* @param int $ownerId |
47
|
|
|
* @param int $ownerType |
48
|
|
|
* @param int $fileId |
49
|
|
|
* @return rkit\filemanager\models\File|bool |
50
|
|
|
*/ |
51
|
17 |
|
private function bindSingle($storage, $ownerId, $ownerType, $fileId) |
52
|
|
|
{ |
53
|
17 |
|
$file = $fileId ? File::findOne($fileId) : false; |
54
|
|
|
|
55
|
17 |
|
if ($file && $file->isOwner($ownerId, $ownerType)) { |
|
|
|
|
56
|
17 |
|
$file->setStorage($storage); |
|
|
|
|
57
|
17 |
|
if ($file->tmp) { |
58
|
17 |
|
$file = $this->saveTmpDirToStorage($file, $ownerId); |
|
|
|
|
59
|
17 |
|
if ($file) { |
60
|
17 |
|
$this->deleteCurrentFiles($storage, $ownerId, $ownerType, $file); |
|
|
|
|
61
|
17 |
|
$file->updateAttributes($file->getDirtyAttributes()); |
62
|
17 |
|
} |
63
|
17 |
|
} |
64
|
17 |
|
return $file; |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Bind files to the with owner |
72
|
|
|
* |
73
|
|
|
* @param Storage $storage |
74
|
|
|
* @param int $ownerId |
75
|
|
|
* @param int $ownerType |
76
|
|
|
* @param array $files |
77
|
|
|
* @return rkit\filemanager\models\File[]|bool |
78
|
|
|
*/ |
79
|
9 |
|
private function bindMultiple($storage, $ownerId, $ownerType, $files) |
80
|
|
|
{ |
81
|
9 |
|
$files = ArrayHelper::getValue($files, 'files', []); |
82
|
9 |
|
$newFiles = ArrayHelper::index(File::findAll(array_keys($files)), 'id'); |
83
|
|
|
|
84
|
9 |
|
if (count($newFiles)) { |
85
|
9 |
|
foreach ($newFiles as $fileId => $file) { |
86
|
9 |
|
if ($file->isOwner($ownerId, $ownerType)) { |
87
|
9 |
|
$file->setStorage($storage); |
88
|
9 |
|
if ($file->tmp) { |
89
|
9 |
|
$file = $this->saveTmpDirToStorage($file, $ownerId); |
90
|
9 |
|
} |
91
|
9 |
|
if ($file) { |
92
|
9 |
|
$file->position = @array_search($file->id, array_keys($files)) + 1; |
93
|
9 |
|
$file->title = ArrayHelper::getValue($files, $file->id, $file->title); |
94
|
9 |
|
$file->updateAttributes($file->getDirtyAttributes()); |
95
|
9 |
|
continue; |
96
|
|
|
} |
97
|
1 |
|
} |
98
|
2 |
|
unset($newFiles[$fileId]); |
99
|
2 |
|
continue; |
100
|
9 |
|
} |
101
|
9 |
|
$this->deleteCurrentFiles($storage, $ownerId, $ownerType, null, $newFiles); |
102
|
9 |
|
} else { |
103
|
1 |
|
$this->deleteCurrentFiles($storage, $ownerId, $ownerType); |
104
|
|
|
} |
105
|
|
|
|
106
|
9 |
|
return count($newFiles) ? $newFiles : false; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Save temporary directory to the storage |
111
|
|
|
* |
112
|
|
|
* @param rkit\filemanager\models\File $file |
113
|
|
|
* @param int $ownerId |
114
|
|
|
* @return rkit\filemanager\models\File|bool |
115
|
|
|
*/ |
116
|
26 |
|
private function saveTmpDirToStorage(File $file, $ownerId) |
117
|
|
|
{ |
118
|
26 |
|
$file->owner_id = $ownerId; |
119
|
26 |
|
$file->tmp = false; |
120
|
26 |
|
if ($file->getStorage()->saveTmpDirToStorage()) { |
121
|
26 |
|
return $file; |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Delete current files |
129
|
|
|
* |
130
|
|
|
* @param Storage $storage |
131
|
|
|
* @param int $ownerId |
132
|
|
|
* @param int $ownerType |
133
|
|
|
* @param rkit\filemanager\models\File $exceptFile |
134
|
|
|
* @param rkit\filemanager\models\File[] $exceptFiles |
135
|
|
|
* @return void |
136
|
|
|
*/ |
137
|
26 |
|
private function deleteCurrentFiles($storage, $ownerId, $ownerType, $exceptFile = null, $exceptFiles = []) |
138
|
|
|
{ |
139
|
26 |
|
$currentFiles = File::findAllByOwner($ownerId, $ownerType); |
140
|
26 |
|
foreach ($currentFiles as $currFile) { |
141
|
11 |
|
$isExceptFile = $exceptFile !== null && $currFile->id === $exceptFile->id; |
142
|
11 |
|
$isExceptFiles = count($exceptFiles) && array_key_exists($currFile->id, $exceptFiles); |
143
|
11 |
|
if (!$isExceptFile && !$isExceptFiles) { |
144
|
6 |
|
$currFile->setStorage($storage); |
145
|
6 |
|
$currFile->delete(); |
146
|
6 |
|
} |
147
|
26 |
|
} |
148
|
26 |
|
} |
149
|
|
|
} |
150
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.