1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Canvas\Traits; |
6
|
|
|
|
7
|
|
|
use Canvas\Models\SystemModules; |
8
|
|
|
use Canvas\Models\FileSystem; |
9
|
|
|
use RuntimeException; |
10
|
|
|
use Phalcon\Mvc\Model\Resultset\Simple; |
|
|
|
|
11
|
|
|
use Canvas\Models\FileSystemEntities; |
12
|
|
|
use Canvas\Dto\Files; |
13
|
|
|
use Canvas\Mapper\FileMapper; |
14
|
|
|
use Phalcon\Di; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Trait ResponseTrait. |
18
|
|
|
* |
19
|
|
|
* @package Canvas\Traits |
20
|
|
|
* |
21
|
|
|
* @property Users $user |
22
|
|
|
* @property AppsPlans $appPlan |
23
|
|
|
* @property CompanyBranches $branches |
24
|
|
|
* @property Companies $company |
25
|
|
|
* @property UserCompanyApps $app |
26
|
|
|
* @property \Phalcon\Di $di |
27
|
|
|
* |
28
|
|
|
*/ |
29
|
|
|
trait FileSystemModelTrait |
30
|
|
|
{ |
31
|
|
|
public $uploadedFiles = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Associated the list of uploaded files to this entity. |
35
|
|
|
* |
36
|
|
|
* call on the after saves |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
protected function associateFileSystem(): bool |
41
|
|
|
{ |
42
|
|
|
if (!empty($this->uploadedFiles) && is_array($this->uploadedFiles)) { |
43
|
|
|
foreach ($this->uploadedFiles as $file) { |
44
|
|
|
/** |
45
|
|
|
* @todo remove when all the frontend standardize our request |
46
|
|
|
*/ |
47
|
|
|
if (!isset($file['id']) && (int) $file > 0) { |
48
|
|
|
$file = ['id' => $file]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (!isset($file['id'])) { |
52
|
|
|
continue; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($fileSystem = FileSystem::getById($file['id'])) { |
56
|
|
|
$this->attach([[ |
57
|
|
|
'file' => $fileSystem, |
58
|
|
|
'field_name' => $file['field_name'] ?? '' |
59
|
|
|
]]); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return true; |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Over write, because of the phalcon events. |
69
|
|
|
* |
70
|
|
|
* @param array data |
71
|
|
|
* @param array whiteList |
|
|
|
|
72
|
|
|
* @return boolean |
73
|
|
|
*/ |
74
|
|
|
public function update($data = null, $whiteList = null): bool |
75
|
|
|
{ |
76
|
|
|
//associate uploaded files |
77
|
|
|
if (isset($data['files'])) { |
78
|
|
|
$this->uploadedFiles = $data['files']; |
79
|
|
|
} elseif (isset($data['filesystem_files'])) { |
80
|
|
|
$this->uploadedFiles = $data['filesystem_files']; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return parent::update($data, $whiteList); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Inserts or updates a model instance. Returning true on success or false otherwise. |
88
|
|
|
* |
89
|
|
|
*<code> |
90
|
|
|
* // Creating a new robot |
91
|
|
|
* $robot = new Robots(); |
92
|
|
|
* |
93
|
|
|
* $robot->type = "mechanical"; |
94
|
|
|
* $robot->name = "Astro Boy"; |
95
|
|
|
* $robot->year = 1952; |
96
|
|
|
* |
97
|
|
|
* $robot->save(); |
98
|
|
|
* |
99
|
|
|
* // Updating a robot name |
100
|
|
|
* $robot = Robots::findFirst("id = 100"); |
101
|
|
|
* |
102
|
|
|
* $robot->name = "Biomass"; |
103
|
|
|
* |
104
|
|
|
* $robot->save(); |
105
|
|
|
*</code> |
106
|
|
|
* |
107
|
|
|
* @param array data |
108
|
|
|
* @param array whiteList |
109
|
|
|
* @return boolean |
110
|
|
|
*/ |
111
|
|
|
public function save($data = null, $whiteList = null): bool |
112
|
|
|
{ |
113
|
|
|
//associate uploaded files |
114
|
|
|
if (isset($data['files'])) { |
115
|
|
|
$this->uploadedFiles = $data['files']; |
116
|
|
|
} elseif (isset($data['filesystem_files'])) { |
117
|
|
|
$this->uploadedFiles = $data['filesystem_files']; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return parent::save($data, $whiteList); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Delete all the files from a module. |
125
|
|
|
* |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
|
public function deleteFiles(): bool |
129
|
|
|
{ |
130
|
|
|
$systemModule = SystemModules::getSystemModuleByModelName(self::class); |
131
|
|
|
|
132
|
|
|
if ($files = FileSystem::getAllByEntityId($this->getId(), $systemModule)) { |
|
|
|
|
133
|
|
|
foreach ($files as $file) { |
134
|
|
|
$file->softDelete(); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return true; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Given the ID delete the file from this entity. |
143
|
|
|
* |
144
|
|
|
* @param integer $id |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
public function deleteFile(int $id) |
148
|
|
|
{ |
149
|
|
|
$systemModule = SystemModules::getSystemModuleByModelName(self::class); |
150
|
|
|
|
151
|
|
|
$file = FileSystemEntities::findFirstOrFail([ |
152
|
|
|
'contidions' => 'filesystem_id = ?0 AND system_modules_id = ?1 AND entity_id = ?2 AND is_deleted = ?3', |
153
|
|
|
'bind' => [$id, $systemModule->getId(), $this->getId(), 0] |
154
|
|
|
]); |
155
|
|
|
|
156
|
|
|
return $file->softDelete(); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Given the array of files we will attch this files to the files. |
161
|
|
|
* [ |
162
|
|
|
* 'file' => $file, |
163
|
|
|
* 'file_name' => 'dfadfa' |
164
|
|
|
* ];. |
165
|
|
|
* |
166
|
|
|
* @param array $files |
167
|
|
|
* @return void |
168
|
|
|
*/ |
169
|
|
|
public function attach(array $files): bool |
170
|
|
|
{ |
171
|
|
|
$systemModule = SystemModules::getSystemModuleByModelName(self::class); |
172
|
|
|
|
173
|
|
|
foreach ($files as $file) { |
174
|
|
|
//im looking for the file inside an array |
175
|
|
|
if (!array_key_exists('file', $file)) { |
176
|
|
|
continue; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if (!$file['file'] instanceof FileSystem) { |
180
|
|
|
throw new RuntimeException('Cant attach a one Filesytem to this entity'); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
//attach to the entity |
184
|
|
|
$fileSystemEntities = new FileSystemEntities(); |
185
|
|
|
$fileSystemEntities->filesystem_id = $file['file']->getId(); |
186
|
|
|
$fileSystemEntities->entity_id = $this->getId(); |
187
|
|
|
$fileSystemEntities->system_modules_id = $systemModule->getId(); |
188
|
|
|
$fileSystemEntities->field_name = $file['field_name'] ?? null; |
189
|
|
|
$fileSystemEntities->created_at = $file['file']->created_at; |
190
|
|
|
$fileSystemEntities->is_deleted = 0 ; |
191
|
|
|
$fileSystemEntities->saveOrFail(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return true; |
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Get all the files attach for the given module. |
199
|
|
|
* |
200
|
|
|
* @param string $fileType filter the files by their type |
201
|
|
|
* @return array |
202
|
|
|
*/ |
203
|
|
|
public function getAttachments(string $fileType = null) : array |
204
|
|
|
{ |
205
|
|
|
$systemModule = SystemModules::getSystemModuleByModelName(self::class); |
206
|
|
|
$bindParams = [ |
207
|
|
|
0, |
208
|
|
|
Di::getDefault()->getUserData()->currentCompanyId(), |
209
|
|
|
$systemModule->getId(), |
210
|
|
|
$this->getId() |
211
|
|
|
]; |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* We can also filter the attachements by its file type. |
215
|
|
|
*/ |
216
|
|
|
$fileTypeSql = !is_null($fileType) ? 'AND file_type = ?3' : null; |
217
|
|
|
if ($fileTypeSql) { |
218
|
|
|
$bindParams[] = $fileType; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$attachments = FileSystem::find([ |
222
|
|
|
'conditions' => ' |
223
|
|
|
is_deleted = ?0 AND companies_id = ?1 AND id in |
224
|
|
|
(SELECT |
225
|
|
|
filesystem_id from \Canvas\Models\FileSystemEntities e |
226
|
|
|
WHERE e.system_modules_id = ?2 AND e.entity_id = ?3 AND e.is_deleted = ?0 |
227
|
|
|
)' . $fileTypeSql, |
228
|
|
|
'bind' => $bindParams |
229
|
|
|
]); |
230
|
|
|
|
231
|
|
|
$fileMapper = new FileMapper(); |
232
|
|
|
$fileMapper->systemModuleId = $systemModule->getId(); |
233
|
|
|
$fileMapper->entityId = $this->getId(); |
234
|
|
|
|
235
|
|
|
//add a mapper |
236
|
|
|
$this->di->getDtoConfig()->registerMapping(FileSystem::class, Files::class) |
237
|
|
|
->useCustomMapper($fileMapper); |
238
|
|
|
|
239
|
|
|
return $this->di->getMapper()->mapMultiple($attachments, Files::class); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Overwrite the relationship of the filesystem to return the attachment structure |
244
|
|
|
* to the given user. |
245
|
|
|
* |
246
|
|
|
* @return array |
247
|
|
|
*/ |
248
|
|
|
public function getFilesystem(): array |
249
|
|
|
{ |
250
|
|
|
return $this->getAttachments(); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Undocumented function. |
255
|
|
|
* |
256
|
|
|
* @todo this will be a performance issue in the futur look for better ways to handle this |
257
|
|
|
* when a company has over 1k images |
258
|
|
|
* |
259
|
|
|
* @param string $name |
260
|
|
|
* @return void |
261
|
|
|
*/ |
262
|
|
|
public function getAttachementByName(string $fieldName): ?string |
263
|
|
|
{ |
264
|
|
|
$systemModule = SystemModules::getSystemModuleByModelName(self::class); |
265
|
|
|
$companyId = Di::getDefault()->getUserData()->currentCompanyId(); |
266
|
|
|
|
267
|
|
|
$fileEntity = FileSystemEntities::findFirst([ |
268
|
|
|
'conditions' => 'system_modules_id = ?0 AND entity_id = ?1 AND is_deleted = ?2 and field_name = ?3 |
269
|
|
|
AND filesystem_id IN (SELECT id from \Canvas\Models\FileSystem f WHERE |
270
|
|
|
f.is_deleted = ?2 AND f.companies_id = ?4 |
271
|
|
|
)', |
272
|
|
|
'bind' => [$systemModule->getId(), $this->getId(), 0, $fieldName, $companyId] |
273
|
|
|
]); |
274
|
|
|
|
275
|
|
|
if ($fileEntity) { |
276
|
|
|
return $fileEntity->file->url; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
return null; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths