|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Canvas\Traits; |
|
6
|
|
|
|
|
7
|
|
|
use Canvas\Dto\Files; |
|
8
|
|
|
use Canvas\Mapper\FileMapper; |
|
9
|
|
|
use Canvas\Models\FileSystem; |
|
10
|
|
|
use Canvas\Models\FileSystemEntities; |
|
11
|
|
|
use Canvas\Models\FileSystemSettings; |
|
12
|
|
|
use Canvas\Models\SystemModules; |
|
13
|
|
|
use Phalcon\Mvc\Model\ResultsetInterface; |
|
|
|
|
|
|
14
|
|
|
use RuntimeException; |
|
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
|
|
|
if (!isset($file['filesystem_id'])) { |
|
45
|
|
|
continue; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if ($fileSystem = FileSystem::getById($file['filesystem_id'])) { |
|
49
|
|
|
$this->attach([[ |
|
50
|
|
|
'id' => $file['id'] ?: 0, |
|
51
|
|
|
'file' => $fileSystem, |
|
52
|
|
|
'field_name' => $file['field_name'] ?? '' |
|
53
|
|
|
]]); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return true; |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Over write, because of the phalcon events. |
|
63
|
|
|
* |
|
64
|
|
|
* @param array data |
|
65
|
|
|
* @param array whiteList |
|
|
|
|
|
|
66
|
|
|
* |
|
67
|
|
|
* @return boolean |
|
68
|
|
|
*/ |
|
69
|
|
|
public function update($data = null, $whiteList = null) : bool |
|
70
|
|
|
{ |
|
71
|
|
|
//associate uploaded files |
|
72
|
|
|
if (isset($data['files'])) { |
|
73
|
|
|
if (!empty($data['files'])) { |
|
74
|
|
|
/** |
|
75
|
|
|
* @todo for now lets delete them all and updated them |
|
76
|
|
|
* look for a better solution later, this can since we are not using transactions |
|
77
|
|
|
*/ |
|
78
|
|
|
$this->deleteFiles(); |
|
79
|
|
|
|
|
80
|
|
|
$this->uploadedFiles = $data['files']; |
|
81
|
|
|
} else { |
|
82
|
|
|
$this->deleteFiles(); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return parent::update($data, $whiteList); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Inserts or updates a model instance. Returning true on success or false otherwise. |
|
91
|
|
|
* |
|
92
|
|
|
*<code> |
|
93
|
|
|
* // Creating a new robot |
|
94
|
|
|
* $robot = new Robots(); |
|
95
|
|
|
* |
|
96
|
|
|
* $robot->type = "mechanical"; |
|
97
|
|
|
* $robot->name = "Astro Boy"; |
|
98
|
|
|
* $robot->year = 1952; |
|
99
|
|
|
* |
|
100
|
|
|
* $robot->save(); |
|
101
|
|
|
* |
|
102
|
|
|
* // Updating a robot name |
|
103
|
|
|
* $robot = Robots::findFirst("id = 100"); |
|
104
|
|
|
* |
|
105
|
|
|
* $robot->name = "Biomass"; |
|
106
|
|
|
* |
|
107
|
|
|
* $robot->save(); |
|
108
|
|
|
*</code> |
|
109
|
|
|
* |
|
110
|
|
|
* @param array data |
|
111
|
|
|
* @param array whiteList |
|
112
|
|
|
* |
|
113
|
|
|
* @return boolean |
|
114
|
|
|
*/ |
|
115
|
|
|
public function save($data = null, $whiteList = null) : bool |
|
116
|
|
|
{ |
|
117
|
|
|
//associate uploaded files |
|
118
|
|
|
if (isset($data['files'])) { |
|
119
|
|
|
if (!empty($data['files'])) { |
|
120
|
|
|
$this->uploadedFiles = $data['files']; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return parent::save($data, $whiteList); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Delete all the files from a module. |
|
129
|
|
|
* |
|
130
|
|
|
* @return bool |
|
131
|
|
|
*/ |
|
132
|
|
|
public function deleteFiles() : bool |
|
133
|
|
|
{ |
|
134
|
|
|
$systemModule = SystemModules::getSystemModuleByModelName(self::class); |
|
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
if ($files = FileSystemEntities::getAllByEntityId($this->getId(), $systemModule)) { |
|
|
|
|
|
|
137
|
|
|
$files->update([], function ($file) { |
|
138
|
|
|
$file->softDelete(); |
|
139
|
|
|
}); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return true; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Given the ID delete the file from this entity. |
|
147
|
|
|
* |
|
148
|
|
|
* @param int $id |
|
149
|
|
|
* |
|
150
|
|
|
* @return bool |
|
151
|
|
|
*/ |
|
152
|
|
|
public function deleteFile(int $id) |
|
153
|
|
|
{ |
|
154
|
|
|
$systemModule = SystemModules::getSystemModuleByModelName(self::class); |
|
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
$file = FileSystemEntities::findFirstOrFail([ |
|
157
|
|
|
'contidions' => 'id = ?0 AND entity_id = ?1 AND system_modules_id = ?2 AND is_deleted = ?3', |
|
158
|
|
|
'bind' => [$id, $this->getId(), $systemModule->getId(), 0] |
|
159
|
|
|
]); |
|
160
|
|
|
|
|
161
|
|
|
if ($file) { |
|
162
|
|
|
$file->softDelete(); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return false; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Given the array of files we will attch this files to the files. |
|
170
|
|
|
* [ |
|
171
|
|
|
* 'file' => $file, |
|
172
|
|
|
* 'file_name' => 'dfadfa' |
|
173
|
|
|
* ];. |
|
174
|
|
|
* |
|
175
|
|
|
* @param array $files |
|
176
|
|
|
* |
|
177
|
|
|
* @return void |
|
178
|
|
|
*/ |
|
179
|
|
|
public function attach(array $files) : bool |
|
180
|
|
|
{ |
|
181
|
|
|
$systemModule = SystemModules::getSystemModuleByModelName(self::class); |
|
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
foreach ($files as $file) { |
|
184
|
|
|
//im looking for the file inside an array |
|
185
|
|
|
if (!isset($file['file'])) { |
|
186
|
|
|
continue; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
if (!$file['file'] instanceof FileSystem) { |
|
190
|
|
|
throw new RuntimeException('Cant attach a none Filesytem to this entity'); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
$fileSystemEntities = null; |
|
194
|
|
|
//check if we are updating the attachment |
|
195
|
|
|
if ($id = (int) $file['id']) { |
|
196
|
|
|
$fileSystemEntities = FileSystemEntities::getByIdWithSystemModule($id, $systemModule); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
//new attachment |
|
200
|
|
|
if (!is_object($fileSystemEntities)) { |
|
201
|
|
|
$fileSystemEntities = new FileSystemEntities(); |
|
202
|
|
|
$fileSystemEntities->system_modules_id = $systemModule->getId(); |
|
203
|
|
|
$fileSystemEntities->companies_id = $file['file']->companies_id; |
|
204
|
|
|
$fileSystemEntities->entity_id = $this->getId(); |
|
205
|
|
|
$fileSystemEntities->created_at = $file['file']->created_at; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
$fileSystemEntities->filesystem_id = $file['file']->getId(); |
|
209
|
|
|
$fileSystemEntities->field_name = $file['field_name'] ?? null; |
|
210
|
|
|
$fileSystemEntities->is_deleted = 0; |
|
211
|
|
|
$fileSystemEntities->saveOrFail(); |
|
212
|
|
|
|
|
213
|
|
|
if (!is_null($this->filesNewAttachedPath())) { |
|
|
|
|
|
|
214
|
|
|
$file['file']->move($this->filesNewAttachedPath()); |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
return true; |
|
|
|
|
|
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Get all the files attach for the given module. |
|
223
|
|
|
* |
|
224
|
|
|
* @param string $fileType filter the files by their type |
|
225
|
|
|
* |
|
226
|
|
|
* @return array |
|
227
|
|
|
*/ |
|
228
|
|
|
public function getAttachments(string $fileType = null) : ResultsetInterface |
|
229
|
|
|
{ |
|
230
|
|
|
$systemModule = SystemModules::getSystemModuleByModelName(self::class); |
|
|
|
|
|
|
231
|
|
|
$appPublicImages = (bool) $this->di->getApp()->get('public_images'); |
|
232
|
|
|
|
|
233
|
|
|
$bindParams = [ |
|
234
|
|
|
'system_module_id' => $systemModule->getId(), |
|
235
|
|
|
'entity_id' => $this->getId(), |
|
236
|
|
|
'is_deleted' => 0, |
|
237
|
|
|
]; |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* We can also filter the attachments by its file type. |
|
241
|
|
|
*/ |
|
242
|
|
|
$fileTypeSql = null; |
|
243
|
|
|
if ($fileType) { |
|
244
|
|
|
$fileTypeSql = !is_null($fileType) ? 'AND f.file_type = :file_type:' : null; |
|
|
|
|
|
|
245
|
|
|
$bindParams['file_type'] = $fileType; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
//do we allow images by entity to be public to anybody accessing it directly by the entity? |
|
249
|
|
|
if ($appPublicImages) { |
|
250
|
|
|
/** |
|
251
|
|
|
* @todo optimize this queries to slow |
|
252
|
|
|
*/ |
|
253
|
|
|
$condition = 'system_modules_id = :system_module_id: AND entity_id = :entity_id: AND is_deleted = :is_deleted: |
|
254
|
|
|
AND filesystem_id IN (SELECT f.id from \Canvas\Models\FileSystem f WHERE |
|
255
|
|
|
f.is_deleted = :is_deleted: ' . $fileTypeSql . ' |
|
256
|
|
|
)'; |
|
257
|
|
|
} else { |
|
258
|
|
|
$bindParams['company_id'] = $this->di->getUserData()->currentCompanyId(); |
|
259
|
|
|
|
|
260
|
|
|
$condition = 'system_modules_id = :system_module_id: AND entity_id = :entity_id: AND is_deleted = :is_deleted: and companies_id = :company_id: |
|
261
|
|
|
AND filesystem_id IN (SELECT f.id from \Canvas\Models\FileSystem f WHERE |
|
262
|
|
|
f.is_deleted = :is_deleted: AND f.companies_id = :company_id: ' . $fileTypeSql . ' |
|
263
|
|
|
)'; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
return FileSystemEntities::find([ |
|
267
|
|
|
'conditions' => $condition, |
|
268
|
|
|
'order' => 'id desc', |
|
269
|
|
|
'bind' => $bindParams |
|
270
|
|
|
]); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Overwrite the relationship of the filesystem to return the attachment structure |
|
275
|
|
|
* to the given user. |
|
276
|
|
|
* |
|
277
|
|
|
* @deprecated version 0.2 |
|
278
|
|
|
* |
|
279
|
|
|
* @return array |
|
280
|
|
|
*/ |
|
281
|
|
|
public function getFilesystem() : array |
|
282
|
|
|
{ |
|
283
|
|
|
return $this->getFiles(); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Overwrite the relationship of the filesystem to return the attachment structure |
|
288
|
|
|
* to the given user. |
|
289
|
|
|
* |
|
290
|
|
|
* @return array |
|
291
|
|
|
*/ |
|
292
|
|
|
public function getFiles(string $fileType = null) : array |
|
293
|
|
|
{ |
|
294
|
|
|
$systemModule = SystemModules::getSystemModuleByModelName(self::class); |
|
|
|
|
|
|
295
|
|
|
|
|
296
|
|
|
$attachments = $this->getAttachments($fileType); |
|
297
|
|
|
|
|
298
|
|
|
$fileMapper = new FileMapper($this->getId(), $systemModule->getId()); |
|
299
|
|
|
|
|
300
|
|
|
//add a mapper |
|
301
|
|
|
$this->di->getDtoConfig() |
|
302
|
|
|
->registerMapping(FileSystemEntities::class, Files::class) |
|
303
|
|
|
->useCustomMapper($fileMapper); |
|
304
|
|
|
|
|
305
|
|
|
return $this->di->getMapper()->mapMultiple($attachments, Files::class); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Get a file by its fieldname. |
|
310
|
|
|
* |
|
311
|
|
|
* @todo this will be a performance issue in the futur look for better ways to handle this |
|
312
|
|
|
* when a company has over 1k images |
|
313
|
|
|
* |
|
314
|
|
|
* @deprecated version 0.2 |
|
315
|
|
|
* |
|
316
|
|
|
* @param string $name |
|
317
|
|
|
* |
|
318
|
|
|
* @return void |
|
319
|
|
|
*/ |
|
320
|
|
|
public function getAttachmentByName(string $fieldName) |
|
321
|
|
|
{ |
|
322
|
|
|
$systemModule = SystemModules::getSystemModuleByModelName(self::class); |
|
|
|
|
|
|
323
|
|
|
$appPublicImages = (bool) $this->di->getApp()->get('public_images'); |
|
324
|
|
|
|
|
325
|
|
|
$bindParams = [ |
|
326
|
|
|
'system_module_id' => $systemModule->getId(), |
|
327
|
|
|
'entity_id' => $this->getId(), |
|
328
|
|
|
'is_deleted' => 0, |
|
329
|
|
|
'field_name' => $fieldName, |
|
330
|
|
|
]; |
|
331
|
|
|
|
|
332
|
|
|
//do we allow images by entity to be public to anybody accessing it directly by the entity? |
|
333
|
|
|
if ($appPublicImages) { |
|
334
|
|
|
$condition = 'system_modules_id = :system_module_id: AND entity_id = :entity_id: AND is_deleted = :is_deleted: and field_name = :field_name: '; |
|
335
|
|
|
} else { |
|
336
|
|
|
$bindParams['company_id'] = $this->di->getUserData()->currentCompanyId(); |
|
337
|
|
|
$condition = 'system_modules_id = :system_module_id: AND entity_id = :entity_id: AND is_deleted = :is_deleted: and field_name = :field_name: and companies_id = :company_id:'; |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
return FileSystemEntities::findFirst([ |
|
341
|
|
|
'conditions' => $condition, |
|
342
|
|
|
'order' => 'id desc', |
|
343
|
|
|
'bind' => $bindParams |
|
344
|
|
|
]); |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* Given the filename look for it version with |
|
349
|
|
|
* the key and value associated to the file. |
|
350
|
|
|
* |
|
351
|
|
|
* @param string $fieldName |
|
352
|
|
|
* @param string $key |
|
353
|
|
|
* @param string $value |
|
354
|
|
|
* |
|
355
|
|
|
* @return void |
|
356
|
|
|
*/ |
|
357
|
|
|
public function getAttachmentByNameAndAttributes(string $fieldName, string $key, string $value) |
|
358
|
|
|
{ |
|
359
|
|
|
$systemModule = SystemModules::getSystemModuleByModelName(self::class); |
|
|
|
|
|
|
360
|
|
|
$appPublicImages = (bool) $this->di->getApp()->get('public_images'); |
|
361
|
|
|
|
|
362
|
|
|
$bindParams = [ |
|
363
|
|
|
'system_module_id' => $systemModule->getId(), |
|
364
|
|
|
'entity_id' => $this->getId(), |
|
365
|
|
|
'is_deleted' => 0, |
|
366
|
|
|
'field_name' => $fieldName, |
|
367
|
|
|
'name' => $key, |
|
368
|
|
|
'value' => $value |
|
369
|
|
|
]; |
|
370
|
|
|
|
|
371
|
|
|
//do we allow images by entity to be public to anybody accessing it directly by the entity? |
|
372
|
|
|
if ($appPublicImages) { |
|
373
|
|
|
$condition = 'system_modules_id = :system_module_id: AND entity_id = :entity_id: AND is_deleted = :is_deleted: and field_name = :field_name: |
|
374
|
|
|
AND filesystem_id IN ( |
|
375
|
|
|
SELECT s.filesystem_id FROM |
|
376
|
|
|
' . FileSystemSettings::class . ' s |
|
377
|
|
|
WHERE name = :name: AND value = :value: |
|
378
|
|
|
)'; |
|
379
|
|
|
} else { |
|
380
|
|
|
$bindParams['company_id'] = $this->di->getUserData()->currentCompanyId(); |
|
381
|
|
|
$condition = 'system_modules_id = :system_module_id: AND entity_id = :entity_id: AND is_deleted = :is_deleted: and field_name = :field_name: and companies_id = :company_id: |
|
382
|
|
|
AND filesystem_id IN ( |
|
383
|
|
|
SELECT s.filesystem_id FROM |
|
384
|
|
|
' . FileSystemSettings::class . ' s |
|
385
|
|
|
WHERE name = :name: AND value = :value: |
|
386
|
|
|
)'; |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
|
|
return FileSystemEntities::findFirst([ |
|
390
|
|
|
'conditions' => $condition, |
|
391
|
|
|
'order' => 'id desc', |
|
392
|
|
|
'bind' => $bindParams |
|
393
|
|
|
]); |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
/** |
|
397
|
|
|
* Get the file byt it's name. |
|
398
|
|
|
* |
|
399
|
|
|
* @param string $fieldName |
|
400
|
|
|
* |
|
401
|
|
|
* @return string|null |
|
402
|
|
|
*/ |
|
403
|
|
|
public function getFileByName(string $fieldName) : ?object |
|
404
|
|
|
{ |
|
405
|
|
|
return $this->fileMapper($this->getAttachmentByName($fieldName)); |
|
|
|
|
|
|
406
|
|
|
} |
|
407
|
|
|
|
|
408
|
|
|
/** |
|
409
|
|
|
* Get file by name and attributes. |
|
410
|
|
|
* |
|
411
|
|
|
* @param string $fieldName |
|
412
|
|
|
* @param string $key |
|
413
|
|
|
* @param string $value |
|
414
|
|
|
* |
|
415
|
|
|
* @return object|null |
|
416
|
|
|
*/ |
|
417
|
|
|
public function getFileByNameWithAttributes(string $fieldName, string $key, string $value) : ?object |
|
418
|
|
|
{ |
|
419
|
|
|
return $this->fileMapper($this->getAttachmentByNameAndAttributes($fieldName, $key, $value)); |
|
|
|
|
|
|
420
|
|
|
} |
|
421
|
|
|
|
|
422
|
|
|
/** |
|
423
|
|
|
* Convert identity to mapper. |
|
424
|
|
|
* |
|
425
|
|
|
* @param FileSystemEntities|null $fileEntity |
|
426
|
|
|
* |
|
427
|
|
|
* @return object|null |
|
428
|
|
|
*/ |
|
429
|
|
|
protected function fileMapper($fileEntity) : ?object |
|
430
|
|
|
{ |
|
431
|
|
|
$systemModule = SystemModules::getSystemModuleByModelName(self::class); |
|
|
|
|
|
|
432
|
|
|
|
|
433
|
|
|
if ($fileEntity instanceof FileSystemEntities) { |
|
434
|
|
|
$fileMapper = new FileMapper($this->getId(), $systemModule->getId()); |
|
435
|
|
|
|
|
436
|
|
|
//add a mapper |
|
437
|
|
|
$this->di->getDtoConfig() |
|
438
|
|
|
->registerMapping(FileSystemEntities::class, Files::class) |
|
439
|
|
|
->useCustomMapper($fileMapper); |
|
440
|
|
|
|
|
441
|
|
|
/** |
|
442
|
|
|
* @todo create a mapper for entity so we dont have to look for the relationship? |
|
443
|
|
|
*/ |
|
444
|
|
|
return $this->di->getMapper()->map($fileEntity, Files::class); |
|
445
|
|
|
} |
|
446
|
|
|
|
|
447
|
|
|
return null; |
|
448
|
|
|
} |
|
449
|
|
|
|
|
450
|
|
|
/** |
|
451
|
|
|
* Given this entity define a new path. |
|
452
|
|
|
* |
|
453
|
|
|
* @param string $path |
|
454
|
|
|
* |
|
455
|
|
|
* @return string |
|
456
|
|
|
*/ |
|
457
|
|
|
protected function filesNewAttachedPath() : ?string |
|
458
|
|
|
{ |
|
459
|
|
|
return null; |
|
460
|
|
|
} |
|
461
|
|
|
} |
|
462
|
|
|
|
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