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
|
|
|
/** |
46
|
|
|
* @todo remove when all the frontend standardize our request |
47
|
|
|
*/ |
48
|
|
|
if (!isset($file['id']) && (int) $file > 0) { |
49
|
|
|
$file = ['id' => $file]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (!isset($file['id'])) { |
53
|
|
|
continue; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($fileSystem = FileSystem::getById($file['id'])) { |
57
|
|
|
$this->attach([[ |
58
|
|
|
'file' => $fileSystem, |
59
|
|
|
'field_name' => $file['field_name'] ?? '' |
60
|
|
|
]]); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return true; |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Over write, because of the phalcon events. |
70
|
|
|
* |
71
|
|
|
* @param array data |
72
|
|
|
* @param array whiteList |
|
|
|
|
73
|
|
|
* @return boolean |
74
|
|
|
*/ |
75
|
|
|
public function update($data = null, $whiteList = null): bool |
76
|
|
|
{ |
77
|
|
|
//associate uploaded files |
78
|
|
|
if (isset($data['files'])) { |
79
|
|
|
$this->uploadedFiles = $data['files']; |
80
|
|
|
} elseif (isset($data['filesystem_files'])) { |
81
|
|
|
$this->uploadedFiles = $data['filesystem_files']; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return parent::update($data, $whiteList); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Inserts or updates a model instance. Returning true on success or false otherwise. |
89
|
|
|
* |
90
|
|
|
*<code> |
91
|
|
|
* // Creating a new robot |
92
|
|
|
* $robot = new Robots(); |
93
|
|
|
* |
94
|
|
|
* $robot->type = "mechanical"; |
95
|
|
|
* $robot->name = "Astro Boy"; |
96
|
|
|
* $robot->year = 1952; |
97
|
|
|
* |
98
|
|
|
* $robot->save(); |
99
|
|
|
* |
100
|
|
|
* // Updating a robot name |
101
|
|
|
* $robot = Robots::findFirst("id = 100"); |
102
|
|
|
* |
103
|
|
|
* $robot->name = "Biomass"; |
104
|
|
|
* |
105
|
|
|
* $robot->save(); |
106
|
|
|
*</code> |
107
|
|
|
* |
108
|
|
|
* @param array data |
109
|
|
|
* @param array whiteList |
110
|
|
|
* @return boolean |
111
|
|
|
*/ |
112
|
|
|
public function save($data = null, $whiteList = null): bool |
113
|
|
|
{ |
114
|
|
|
//associate uploaded files |
115
|
|
|
if (isset($data['files'])) { |
116
|
|
|
$this->uploadedFiles = $data['files']; |
117
|
|
|
} elseif (isset($data['filesystem_files'])) { |
118
|
|
|
$this->uploadedFiles = $data['filesystem_files']; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return parent::save($data, $whiteList); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Delete all the files from a module. |
126
|
|
|
* |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
public function deleteFiles(): bool |
130
|
|
|
{ |
131
|
|
|
$systemModule = SystemModules::getSystemModuleByModelName(self::class); |
132
|
|
|
|
133
|
|
|
if ($files = FileSystem::getAllByEntityId($this->getId(), $systemModule)) { |
|
|
|
|
134
|
|
|
foreach ($files as $file) { |
135
|
|
|
$file->softDelete(); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return true; |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Given the array of files we will attch this files to the files. |
144
|
|
|
* [ |
145
|
|
|
* 'file' => $file, |
146
|
|
|
* 'file_name' => 'dfadfa' |
147
|
|
|
* ];. |
148
|
|
|
* |
149
|
|
|
* @param array $files |
150
|
|
|
* @return void |
151
|
|
|
*/ |
152
|
|
|
public function attach(array $files): bool |
153
|
|
|
{ |
154
|
|
|
$systemModule = SystemModules::getSystemModuleByModelName(self::class); |
155
|
|
|
|
156
|
|
|
foreach ($files as $file) { |
157
|
|
|
//im looking for the file inside an array |
158
|
|
|
if (!array_key_exists('file', $file)) { |
159
|
|
|
continue; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (!$file['file'] instanceof FileSystem) { |
163
|
|
|
throw new RuntimeException('Cant attach a one Filesytem to this entity'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
//attach to the entity |
167
|
|
|
$fileSystemEntities = new FileSystemEntities(); |
168
|
|
|
$fileSystemEntities->filesystem_id = $file['file']->getId(); |
169
|
|
|
$fileSystemEntities->entity_id = $this->getId(); |
170
|
|
|
$fileSystemEntities->system_modules_id = $systemModule->getId(); |
171
|
|
|
$fileSystemEntities->field_name = $file['field_name'] ?? null; |
172
|
|
|
$fileSystemEntities->created_at = $file['file']->created_at; |
173
|
|
|
$fileSystemEntities->is_deleted = 0 ; |
174
|
|
|
$fileSystemEntities->saveOrFail(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return true; |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get all the files attach for the given module. |
182
|
|
|
* |
183
|
|
|
* @param string $fileType filter the files by their type |
184
|
|
|
* @return array |
185
|
|
|
*/ |
186
|
|
|
public function getAttachments(string $fileType = null) : array |
187
|
|
|
{ |
188
|
|
|
$systemModule = SystemModules::getSystemModuleByModelName(self::class); |
189
|
|
|
$bindParams = [ |
190
|
|
|
0, |
191
|
|
|
Di::getDefault()->getUserData()->currentCompanyId(), |
192
|
|
|
$systemModule->getId(), |
193
|
|
|
$this->getId() |
194
|
|
|
]; |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* We can also filter the attachements by its file type. |
198
|
|
|
*/ |
199
|
|
|
$fileTypeSql = !is_null($fileType) ? 'AND file_type = ?3' : null; |
200
|
|
|
if ($fileTypeSql) { |
201
|
|
|
$bindParams[] = $fileType; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$attachments = FileSystem::find([ |
205
|
|
|
'conditions' => ' |
206
|
|
|
is_deleted = ?0 AND companies_id = ?1 AND id in |
207
|
|
|
(SELECT |
208
|
|
|
filesystem_id from \Canvas\Models\FileSystemEntities e |
209
|
|
|
WHERE e.system_modules_id = ?2 AND e.entity_id = ?3 |
210
|
|
|
)' . $fileTypeSql, |
211
|
|
|
'bind' => $bindParams |
212
|
|
|
]); |
213
|
|
|
|
214
|
|
|
$fileMapper = new FileMapper(); |
215
|
|
|
$fileMapper->systemModuleId = $systemModule->getId(); |
216
|
|
|
$fileMapper->entityId = $this->getId(); |
217
|
|
|
|
218
|
|
|
//add a mapper |
219
|
|
|
$this->di->getDtoConfig()->registerMapping(FileSystem::class, Files::class) |
220
|
|
|
->useCustomMapper($fileMapper); |
221
|
|
|
|
222
|
|
|
return $this->di->getMapper()->mapMultiple($attachments, Files::class); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Overwrite the relationship of the filesystem to return the attachment structure |
227
|
|
|
* to the given user. |
228
|
|
|
* |
229
|
|
|
* @return array |
230
|
|
|
*/ |
231
|
|
|
public function getFilesystem(): array |
232
|
|
|
{ |
233
|
|
|
return $this->getAttachments(); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
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