44
55namespace Kanvas \Sdk \Traits ;
66
7+ use Exception ;
78use Kanvas \Sdk \SystemModules ;
89use Kanvas \Sdk \FileSystem ;
10+ use Kanvas \Sdk \Apps ;
11+ use Kanvas \Sdk \Users as KanvasUsers ;
912use Kanvas \Sdk \FileSystemEntities ;
1013use Phalcon \Di ;
1114
@@ -68,6 +71,7 @@ public function attach(array $files): bool
6871 {
6972 $ appId = Apps::getIdByKey (getenv ('GEWAER_APP_ID ' ))->id ;
7073 $ systemModule = SystemModules::getSystemModuleByModelName (self ::class, $ appId );
74+ $ currentCompanyId = KanvasUsers::getSelf ()->default_company ;
7175
7276 foreach ($ files as $ file ) {
7377 //im looking for the file inside an array
@@ -76,13 +80,13 @@ public function attach(array $files): bool
7680 }
7781
7882 if (!$ file ['file ' ] instanceof FileSystem) {
79- throw new RuntimeException ('Cant attach a none Filesytem to this entity ' );
83+ throw new Exception ('Cant attach a none Filesytem to this entity ' );
8084 }
8185
8286 $ fileSystemEntities = null ;
8387 //check if we are updating the attachment
8488 if ($ id = (int ) $ file ['id ' ]) {
85- $ fileSystemEntities = FileSystemEntities::getByIdWithSystemModule ($ id , $ appId );
89+ $ fileSystemEntities = FileSystemEntities::getByIdWithSystemModule ($ id , $ systemModule -> id , $ appId , $ currentCompanyId );
8690 }
8791
8892 //new attachment
@@ -132,4 +136,52 @@ protected function filesNewAttachedPath(): ?string
132136 {
133137 return null ;
134138 }
139+
140+ /**
141+ * Over write, because of the phalcon events.
142+ *
143+ * @param array data
144+ * @param array whiteList
145+ * @return boolean
146+ */
147+ public function update ($ data = null , $ whiteList = null ): bool
148+ {
149+ //associate uploaded files
150+ if (isset ($ data ['files ' ])) {
151+ if (!empty ($ data ['files ' ])) {
152+ /**
153+ * @todo for now lets delete them all and updated them
154+ * look for a better solution later, this can since we are not using transactions
155+ */
156+ $ this ->deleteFiles ();
157+
158+ $ this ->uploadedFiles = $ data ['files ' ];
159+ } else {
160+ $ this ->deleteFiles ();
161+ }
162+ }
163+
164+ return parent ::update ($ data , $ whiteList );
165+ }
166+
167+ /**
168+ * Delete all the files from a module.
169+ *
170+ * @return bool
171+ */
172+ public function deleteFiles (): bool
173+ {
174+ $ appId = Apps::getIdByKey (getenv ('GEWAER_APP_ID ' ))->id ;
175+ $ currentCompanyId = KanvasUsers::getSelf ()->default_company ;
176+
177+ if ($ files = FileSystemEntities::getAllByEntityId ($ this ->getId (), $ appId , $ currentCompanyId )) {
178+ foreach ($ files as $ file ) {
179+ FileSystemEntities::update ($ file ->id , [
180+ "is_deleted " => 1
181+ ]);
182+ }
183+ }
184+
185+ return true ;
186+ }
135187}
0 commit comments