Skip to content
This repository was archived by the owner on Dec 8, 2022. It is now read-only.

Commit e89bbcb

Browse files
committed
Add deletefiles and update functions to trait
1 parent e6df3cb commit e89bbcb

File tree

2 files changed

+77
-13
lines changed

2 files changed

+77
-13
lines changed

src/FileSystemEntities.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Kanvas\Sdk\Filesystem;
1414
use Kanvas\Sdk\Util\Util;
1515
use Kanvas\Sdk\KanvasObject;
16+
use Kanvas\Sdk\Apps;
1617

1718
/**
1819
* Filesystem Resource
@@ -35,24 +36,35 @@ class FileSystemEntities extends Resource
3536
* @param bool $isDeleted
3637
* @return FileSystemEntities
3738
*/
38-
public static function getByIdWithSystemModule(int $id, int $systemModulesId)
39+
public static function getByIdWithSystemModule(int $id, int $systemModulesId, int $appId, int $currentCompanyId)
3940
{
40-
// $app = Di::getDefault()->getApp();
41-
// $companyId = Di::getDefault()->getUserData()->currentCompanyId();
41+
$filesystem = Filesystem::all([], ['conditions'=> ["apps_id:{$appId}","is_deleted:0"]]);
4242

43-
$app = 1;
44-
$companyId = 3;
43+
foreach ($filesystem as $file) {
44+
$filesystemEntity = current(self::all([],["conditions"=>["id:{$id}","system_modules_id:{$systemModulesId}","companies_id:{$currentCompanyId}","filesystem_id:{$file->id}","is_deleted:0"]]));
45+
if ($filesystemEntity instanceof KanvasObject) {
46+
return $filesystemEntity;
47+
}
48+
}
49+
}
4550

46-
//Search system modules
47-
// $filesystem = current(Filesystem::all([], ['conditions'=> ["apps_id:{$app->id}","is_deleted:0"]]));
48-
// $filesystem = current(Filesystem::all([], ['conditions'=> ["apps_id:{$app}","is_deleted:0"]]));
49-
return $filesystem = Filesystem::all([], ['conditions'=> ["apps_id:{$app}","is_deleted:0"]]);
51+
/**
52+
* Get all filesystem entities by entity_id
53+
*
54+
* @return array
55+
*/
56+
public static function getAllByEntityId(int $id, int $appId, int $currentCompanyId)
57+
{
58+
$entitiesArray = [];
59+
$filesystem = Filesystem::all([], ['conditions'=> ["apps_id:{$appId}","is_deleted:0"]]);
5060

5161
foreach ($filesystem as $file) {
52-
$filesystemEntity = current(self::all([],["conditions"=>["id:{$id}","system_modules_id:{$systemModulesId}","companies_id:{$companyId}","filesystem_id:{$file->id}","is_deleted:0"]]));
62+
$filesystemEntity = current(self::all([],["conditions"=>["entity_id:{$id}","companies_id:{$currentCompanyId}","filesystem_id:{$file->id}","is_deleted:0"]]));
5363
if ($filesystemEntity instanceof KanvasObject) {
54-
return $filesystemEntity;
64+
$entitiesArray[] = $filesystemEntity;
5565
}
5666
}
67+
68+
return $entitiesArray;
5769
}
5870
}

src/Traits/FileSystemModelTrait.php

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
namespace Kanvas\Sdk\Traits;
66

7+
use Exception;
78
use Kanvas\Sdk\SystemModules;
89
use Kanvas\Sdk\FileSystem;
10+
use Kanvas\Sdk\Apps;
11+
use Kanvas\Sdk\Users as KanvasUsers;
912
use Kanvas\Sdk\FileSystemEntities;
1013
use 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

Comments
 (0)