diff --git a/src/Venom/Core/Database/Entity.php b/src/Venom/Core/Database/Entity.php index 30c1bde..053f5ca 100755 --- a/src/Venom/Core/Database/Entity.php +++ b/src/Venom/Core/Database/Entity.php @@ -20,6 +20,15 @@ abstract class Entity implements JsonSerializable // Override this if you want special fields :) + public function getLoadedFieldValues(): array + { + $keyValue = []; + foreach ($this->loadedFields as $var) { + $keyValue[$var] = $this->$var; + } + return $keyValue; + } + public function getFieldsToWrite(): array { if ($this->fields !== null) { @@ -33,9 +42,10 @@ abstract class Entity implements JsonSerializable unset($vars[$key]); } } + if (!$allLoaded) { foreach ($vars as $key => $var) { - if (!in_array($var, $this->loadedFields)) { + if (!in_array($key, $this->loadedFields)) { unset($vars[$key]); } } @@ -104,7 +114,7 @@ abstract class Entity implements JsonSerializable public function jsonSerialize(): array { - return $this->getFieldsToWrite(); + return $this->getLoadedFieldValues(); } public function preSave() diff --git a/src/Venom/Core/Database/EntityManager.php b/src/Venom/Core/Database/EntityManager.php index 24d6d4f..9aef7d5 100755 --- a/src/Venom/Core/Database/EntityManager.php +++ b/src/Venom/Core/Database/EntityManager.php @@ -50,10 +50,10 @@ class EntityManager return null; } - public function saveAll() + public function saveAll(): bool { if (count($this->entities) === 0) { - return; + return true; } try { $this->db->start(); @@ -64,10 +64,12 @@ class EntityManager } catch (Exception $ex) { trigger_error($ex->getMessage()); $this->db->rollBack(); + return false; } + return true; } - public function deleteEntities() + public function deleteEntities(): bool { try { $this->db->start(); @@ -78,7 +80,9 @@ class EntityManager } catch (Exception $ex) { trigger_error($ex->getMessage()); $this->db->rollBack(); + return false; } + return true; } public function clearAll() @@ -118,6 +122,11 @@ class EntityManager return $this->entities; } + public function getFirst(): ?Entity + { + return $this->entities[0] ?? null; + } + private function addEntities(array $entities) { foreach ($entities as $entity) { diff --git a/src/modules/User/Controller/UserAPIController.php b/src/modules/User/Controller/UserAPIController.php index 8addaee..a4c7000 100755 --- a/src/modules/User/Controller/UserAPIController.php +++ b/src/modules/User/Controller/UserAPIController.php @@ -22,32 +22,27 @@ class UserAPIController public function getById($id) { - $d = UserController::getById($id, ["id", "username", "firstname", "lastname", "email", "isActive"]); - AdminHelper::sendResponse($d); + $entityManager = EntityManager::create(User::class); + $easyQuery = new EasyQuery(User::$tableName, ["id", "username", "firstname", "lastname", "email", "isActive"]); + $easyQuery->where("id", $id); + $entityManager->load($easyQuery); + AdminHelper::sendResponse($entityManager->getFirst()); } public function update($id) { - $original = UserController::getById($id); + $entityManager = EntityManager::create(User::class); + $easyQuery = new EasyQuery(User::$tableName, ["id", "username", "firstname", "lastname", "email", "isActive"]); + $easyQuery->where("id", $id); + $entityManager->load($easyQuery); + /** @var User|null $original */ + $original = $entityManager->getFirst(); if ($original == null) { AdminHelper::sendStatus(false, "User not Found"); } - $args = ArgumentHandler::get(); - $data = []; - $d = $original->getData(); - foreach ($d as $key => $item) { - if ($args->hasPostItem($key)) { - $val = $args->getPostItem($key); - if ($val != $item) { - $data[$key] = $val; - } - } - } parse_str(file_get_contents('php://input'), $_PUT); - var_dump(array_keys($_PUT)); - //var_dump($data, $d, $_POST); - // $args->getPostItem("username")//UPDATE users SET lastname='Doe', firstname='' WHERE id=2 - AdminHelper::sendStatus(UserController::update($id, $data)); +/* var_dump(array_keys($_PUT));*/ + AdminHelper::sendStatus($entityManager->saveAll()); } public function delete($id) diff --git a/src/modules/User/Controller/UserController.php b/src/modules/User/Controller/UserController.php deleted file mode 100755 index dba2e69..0000000 --- a/src/modules/User/Controller/UserController.php +++ /dev/null @@ -1,36 +0,0 @@ -getOne($sel, [ - ':id' => $id - ]); - } - - public static function get(array $fields = ["*"]): array - { - return DatabaseHandler::get()->getAll(DatabaseHandler::createEasySelect($fields, "users")); - } - - public static function update($id, array $values = []): bool - { - if (count($values) === 0) { - return false; - } - return DatabaseHandler::get()->execute(...DatabaseHandler::getUpdateString($values, "users", "WHERE id = :id")); - } -} \ No newline at end of file diff --git a/src/modules/User/module.php b/src/modules/User/module.php index c060b78..3f8d42a 100755 --- a/src/modules/User/module.php +++ b/src/modules/User/module.php @@ -23,7 +23,7 @@ $venom->registerModule([ ], "1" => [ Route::GET => 'getById', - Route::POST => 'insert', + Route::POST => 'update', Route::PUT => 'update', Route::DELETE => 'delete' ] diff --git a/tpl/admin/jsTemplates/userEdit.tpl b/tpl/admin/jsTemplates/userEdit.tpl index ca7b8e0..7f4a1f0 100755 --- a/tpl/admin/jsTemplates/userEdit.tpl +++ b/tpl/admin/jsTemplates/userEdit.tpl @@ -1,65 +1,34 @@
-

User: engineertrooper

+

User: ${username}

{include(includes/svg;class=back-arrow;icon=vt-arrow-back)}
-

User Data

-
- {include(includes/input;class=input-group;label=Username;name=newUserName;error=New User Name is required;default=EngineerTrooper)} - {include(includes/input;class=input-group;label=Author Name;name=newAuthorName;error=New Author Name is required;default=Dominic Seela;classes=spacer)} - {include(includes/input;class=input-group;label=E-Mail;name=newEMailAddress;error=E-Mail Address is required;default=kontakt@engineertrooper.com;classes=spacer)} - {include(includes/input;class=input-group;label=Password;name=newPassword;type=password;error=Password is required;classes=spacer)} - {include(includes/input;class=input-group;label=Password (Repeat);name=newPasswordRepeat;type=password;error=Password (Repeat) is required;classes=spacer)} -
- -

Privileges

- - - Module - Edit - View - - - Meta-Data - {include(includes/switch;id=${switch.id};name=permissionEditMetaData)} - {include(includes/switch;id=${switch.id};name=permissionViewMetaData)} - - - Pages - {include(includes/switch;id=${switch.id};name=permissionEditPages)} - {include(includes/switch;id=${switch.id};name=permissionViewPages)} - - - Roles - {include(includes/switch;id=${switch.id};name=permissionEditRoles)} - {include(includes/switch;id=${switch.id};name=permissionViewRoles)} - - - SEO-URL - {include(includes/switch;id=${switch.id};name=permissionEditSeoUrl)} - {include(includes/switch;id=${switch.id};name=permissionViewSeoUrl)} - - - Users - {include(includes/switch;id=${switch.id};name=permissionEditUsers)} - {include(includes/switch;id=${switch.id};name=permissionViewUsers)} - - - VENOM-Status - {include(includes/switch;id=${switch.id};name=permissionEditVenomStatus)} - {include(includes/switch;id=${switch.id};name=permissionViewVenomStatus)} - - -
-
-
+ +

User-Data

+
+
+ {include(includes/input;class=input-group;label=Username;name=username;error=Username is required;default=$username)} + {include(includes/input;class=input-group;label=Firstname;name=firstname;error=Firstname is required;default=$firstname;classes=spacer)} + {include(includes/input;class=input-group;label=Lastname;name=lastname;error=Lastname is required;default=$lastname;classes=spacer)} + {include(includes/input;class=input-group;label=E-Mail;name=newEMailAddress;error=E-Mail Address is required;default=$email;classes=spacer)} + +
+
{include(includes/btn;type=valid;content=Save)} - {include(includes/btn;type=primary;content=Reset)} {include(includes/btn;type=warn;content=Delete User)}
-
+ + +

Change Password

+
+ {include(includes/input;class=input-group;label=Password;name=password;type=password;classes=spacer)} + {include(includes/input;class=input-group;label=Password (Repeat);name=passwordRepeat;type=password;classes=spacer)} +
+ {include(includes/btn;type=valid;content=Save)} +
+
\ No newline at end of file diff --git a/tpl/admin/jsTemplates/usersList.tpl b/tpl/admin/jsTemplates/usersList.tpl index 202e621..1781db8 100755 --- a/tpl/admin/jsTemplates/usersList.tpl +++ b/tpl/admin/jsTemplates/usersList.tpl @@ -17,9 +17,7 @@ {/for}
-

Add new User

- {include(includes/input;label=New User Name;name=newUserName;error=New User Name is required;type=text)} - {include(includes/btn;type=primary;content=Add)} + {include(includes/btn;type=primary;content=Add User)}
\ No newline at end of file