From d75bc6defe3981c4d4bc4d0f215528fa1c4a3da1 Mon Sep 17 00:00:00 2001
From: Kavalar <apuc06@mail.ru>
Date: Tue, 23 Jul 2024 15:22:33 +0300
Subject: [PATCH] user crud

---
 app/controllers/UserController.php            |  29 +++--
 composer.lock                                 | 104 +++++++++---------
 kernel/IGTabel/JSONCreator.php                |   3 +-
 kernel/IGTabel/ViewJsonTableEloquentModel.php |  29 +++++
 4 files changed, 104 insertions(+), 61 deletions(-)
 create mode 100644 kernel/IGTabel/ViewJsonTableEloquentModel.php

diff --git a/app/controllers/UserController.php b/app/controllers/UserController.php
index fb50beb..dadc708 100644
--- a/app/controllers/UserController.php
+++ b/app/controllers/UserController.php
@@ -7,9 +7,13 @@ use app\helpers\Debug;
 use app\models\Question;
 use app\models\User;
 use app\tables\columns\UserViewActionColumn;
+use Exception;
+use http\Message;
 use Itguild\Tables\ListJsonTable;
+use Itguild\Tables\ViewJsonTable;
 use kernel\Controller;
 use kernel\IGTabel\ListJsonTableEloquentCollection;
+use kernel\IGTabel\ViewJsonTableEloquentModel;
 use Twig\TwigFunction;
 
 class UserController extends Controller{
@@ -54,17 +58,26 @@ class UserController extends Controller{
         echo $this->twig->render('user_table.html.twig');
     }
 
+    /**
+     * @throws Exception
+     */
     public function actionView($id): void
     {
-        echo User::where('id', '=', $id)->get();
-        echo User::where('id', '=', $id)->first() . "<br><br>";
-
         $user = User::find($id);
-        echo $user->id . "<br>";
-        echo $user->username . "<br>";
-        echo $user->email . "<br>";
-        echo $user->created_at . "<br>";
-        echo $user->updated_at . "<br>";
+        if (!$user){
+            throw new Exception(message: "The user not found");
+        }
+        $this->twig->addFunction(new TwigFunction('table', function () use ($user){
+            $dataProvider = new ViewJsonTableEloquentModel($user, [
+                'params' => ["class" => "table table-bordered", "border" => "2"],
+                'baseUrl' => "/admin/user",
+            ]);
+            $table = new ViewJsonTable($dataProvider->getJson());
+            $table->create();
+            $table->render();
+        }));
+
+        echo $this->twig->render('user_table.html.twig');
     }
 
     public function actionUpdate(): void
diff --git a/composer.lock b/composer.lock
index 389f20a..c85851c 100644
--- a/composer.lock
+++ b/composer.lock
@@ -275,24 +275,24 @@
         },
         {
             "name": "graham-campbell/result-type",
-            "version": "v1.1.2",
+            "version": "v1.1.3",
             "source": {
                 "type": "git",
                 "url": "https://github.com/GrahamCampbell/Result-Type.git",
-                "reference": "fbd48bce38f73f8a4ec8583362e732e4095e5862"
+                "reference": "3ba905c11371512af9d9bdd27d99b782216b6945"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/GrahamCampbell/Result-Type/zipball/fbd48bce38f73f8a4ec8583362e732e4095e5862",
-                "reference": "fbd48bce38f73f8a4ec8583362e732e4095e5862",
+                "url": "https://api.github.com/repos/GrahamCampbell/Result-Type/zipball/3ba905c11371512af9d9bdd27d99b782216b6945",
+                "reference": "3ba905c11371512af9d9bdd27d99b782216b6945",
                 "shasum": ""
             },
             "require": {
                 "php": "^7.2.5 || ^8.0",
-                "phpoption/phpoption": "^1.9.2"
+                "phpoption/phpoption": "^1.9.3"
             },
             "require-dev": {
-                "phpunit/phpunit": "^8.5.34 || ^9.6.13 || ^10.4.2"
+                "phpunit/phpunit": "^8.5.39 || ^9.6.20 || ^10.5.28"
             },
             "type": "library",
             "autoload": {
@@ -321,7 +321,7 @@
             ],
             "support": {
                 "issues": "https://github.com/GrahamCampbell/Result-Type/issues",
-                "source": "https://github.com/GrahamCampbell/Result-Type/tree/v1.1.2"
+                "source": "https://github.com/GrahamCampbell/Result-Type/tree/v1.1.3"
             },
             "funding": [
                 {
@@ -333,20 +333,20 @@
                     "type": "tidelift"
                 }
             ],
-            "time": "2023-11-12T22:16:48+00:00"
+            "time": "2024-07-20T21:45:45+00:00"
         },
         {
             "name": "illuminate/collections",
-            "version": "v11.15.0",
+            "version": "v11.16.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/illuminate/collections.git",
-                "reference": "9bf68f03bbe05d38c9bd99bac8798c0de61f8478"
+                "reference": "ba2cf689f7d75315f483334b4efc8c6af1d5159c"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/illuminate/collections/zipball/9bf68f03bbe05d38c9bd99bac8798c0de61f8478",
-                "reference": "9bf68f03bbe05d38c9bd99bac8798c0de61f8478",
+                "url": "https://api.github.com/repos/illuminate/collections/zipball/ba2cf689f7d75315f483334b4efc8c6af1d5159c",
+                "reference": "ba2cf689f7d75315f483334b4efc8c6af1d5159c",
                 "shasum": ""
             },
             "require": {
@@ -388,11 +388,11 @@
                 "issues": "https://github.com/laravel/framework/issues",
                 "source": "https://github.com/laravel/framework"
             },
-            "time": "2024-07-02T20:54:37+00:00"
+            "time": "2024-07-15T21:44:45+00:00"
         },
         {
             "name": "illuminate/conditionable",
-            "version": "v11.15.0",
+            "version": "v11.16.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/illuminate/conditionable.git",
@@ -438,7 +438,7 @@
         },
         {
             "name": "illuminate/container",
-            "version": "v11.15.0",
+            "version": "v11.16.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/illuminate/container.git",
@@ -489,7 +489,7 @@
         },
         {
             "name": "illuminate/contracts",
-            "version": "v11.15.0",
+            "version": "v11.16.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/illuminate/contracts.git",
@@ -537,16 +537,16 @@
         },
         {
             "name": "illuminate/database",
-            "version": "v11.15.0",
+            "version": "v11.16.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/illuminate/database.git",
-                "reference": "9b7b13d2d9175ae9c07d4744d8f7d5b07e7264a7"
+                "reference": "cf816e7e0d08e2a75b233ad061eed85dd8b6b37f"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/illuminate/database/zipball/9b7b13d2d9175ae9c07d4744d8f7d5b07e7264a7",
-                "reference": "9b7b13d2d9175ae9c07d4744d8f7d5b07e7264a7",
+                "url": "https://api.github.com/repos/illuminate/database/zipball/cf816e7e0d08e2a75b233ad061eed85dd8b6b37f",
+                "reference": "cf816e7e0d08e2a75b233ad061eed85dd8b6b37f",
                 "shasum": ""
             },
             "require": {
@@ -601,11 +601,11 @@
                 "issues": "https://github.com/laravel/framework/issues",
                 "source": "https://github.com/laravel/framework"
             },
-            "time": "2024-07-08T15:08:27+00:00"
+            "time": "2024-07-15T22:28:28+00:00"
         },
         {
             "name": "illuminate/macroable",
-            "version": "v11.15.0",
+            "version": "v11.16.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/illuminate/macroable.git",
@@ -651,16 +651,16 @@
         },
         {
             "name": "illuminate/support",
-            "version": "v11.15.0",
+            "version": "v11.16.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/illuminate/support.git",
-                "reference": "b551f6cbecc607bcaf520eb3fc1933db9b1398f2"
+                "reference": "4fd85fffd9a4812386b6e10b2a18272ff9040dbe"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/illuminate/support/zipball/b551f6cbecc607bcaf520eb3fc1933db9b1398f2",
-                "reference": "b551f6cbecc607bcaf520eb3fc1933db9b1398f2",
+                "url": "https://api.github.com/repos/illuminate/support/zipball/4fd85fffd9a4812386b6e10b2a18272ff9040dbe",
+                "reference": "4fd85fffd9a4812386b6e10b2a18272ff9040dbe",
                 "shasum": ""
             },
             "require": {
@@ -721,15 +721,15 @@
                 "issues": "https://github.com/laravel/framework/issues",
                 "source": "https://github.com/laravel/framework"
             },
-            "time": "2024-07-08T14:46:56+00:00"
+            "time": "2024-07-16T13:48:58+00:00"
         },
         {
             "name": "itguild/tables",
-            "version": "0.1.5",
+            "version": "0.1.6",
             "source": {
                 "type": "git",
                 "url": "https://git.itguild.info/ItGuild/tables",
-                "reference": "85c4a850b190e20944dc5ee5e888b6ecd342f82f"
+                "reference": "48e6d645d97ace1eec258819da758f53db0b7d46"
             },
             "type": "library",
             "autoload": {
@@ -747,20 +747,20 @@
                     "email": "apuc06@mail.ru"
                 }
             ],
-            "time": "2024-07-16T10:26:29+00:00"
+            "time": "2024-07-23T12:17:16+00:00"
         },
         {
             "name": "nesbot/carbon",
-            "version": "3.6.0",
+            "version": "3.7.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/briannesbitt/Carbon.git",
-                "reference": "39c8ef752db6865717cc3fba63970c16f057982c"
+                "reference": "cb4374784c87d0a0294e8513a52eb63c0aff3139"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/39c8ef752db6865717cc3fba63970c16f057982c",
-                "reference": "39c8ef752db6865717cc3fba63970c16f057982c",
+                "url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/cb4374784c87d0a0294e8513a52eb63c0aff3139",
+                "reference": "cb4374784c87d0a0294e8513a52eb63c0aff3139",
                 "shasum": ""
             },
             "require": {
@@ -853,20 +853,20 @@
                     "type": "tidelift"
                 }
             ],
-            "time": "2024-06-20T15:52:59+00:00"
+            "time": "2024-07-16T22:29:20+00:00"
         },
         {
             "name": "phpoption/phpoption",
-            "version": "1.9.2",
+            "version": "1.9.3",
             "source": {
                 "type": "git",
                 "url": "https://github.com/schmittjoh/php-option.git",
-                "reference": "80735db690fe4fc5c76dfa7f9b770634285fa820"
+                "reference": "e3fac8b24f56113f7cb96af14958c0dd16330f54"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/schmittjoh/php-option/zipball/80735db690fe4fc5c76dfa7f9b770634285fa820",
-                "reference": "80735db690fe4fc5c76dfa7f9b770634285fa820",
+                "url": "https://api.github.com/repos/schmittjoh/php-option/zipball/e3fac8b24f56113f7cb96af14958c0dd16330f54",
+                "reference": "e3fac8b24f56113f7cb96af14958c0dd16330f54",
                 "shasum": ""
             },
             "require": {
@@ -874,13 +874,13 @@
             },
             "require-dev": {
                 "bamarni/composer-bin-plugin": "^1.8.2",
-                "phpunit/phpunit": "^8.5.34 || ^9.6.13 || ^10.4.2"
+                "phpunit/phpunit": "^8.5.39 || ^9.6.20 || ^10.5.28"
             },
             "type": "library",
             "extra": {
                 "bamarni-bin": {
                     "bin-links": true,
-                    "forward-command": true
+                    "forward-command": false
                 },
                 "branch-alias": {
                     "dev-master": "1.9-dev"
@@ -916,7 +916,7 @@
             ],
             "support": {
                 "issues": "https://github.com/schmittjoh/php-option/issues",
-                "source": "https://github.com/schmittjoh/php-option/tree/1.9.2"
+                "source": "https://github.com/schmittjoh/php-option/tree/1.9.3"
             },
             "funding": [
                 {
@@ -928,7 +928,7 @@
                     "type": "tidelift"
                 }
             ],
-            "time": "2023-11-12T21:59:55+00:00"
+            "time": "2024-07-20T21:41:07+00:00"
         },
         {
             "name": "psr/clock",
@@ -1841,23 +1841,23 @@
         },
         {
             "name": "vlucas/phpdotenv",
-            "version": "v5.6.0",
+            "version": "v5.6.1",
             "source": {
                 "type": "git",
                 "url": "https://github.com/vlucas/phpdotenv.git",
-                "reference": "2cf9fb6054c2bb1d59d1f3817706ecdb9d2934c4"
+                "reference": "a59a13791077fe3d44f90e7133eb68e7d22eaff2"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/vlucas/phpdotenv/zipball/2cf9fb6054c2bb1d59d1f3817706ecdb9d2934c4",
-                "reference": "2cf9fb6054c2bb1d59d1f3817706ecdb9d2934c4",
+                "url": "https://api.github.com/repos/vlucas/phpdotenv/zipball/a59a13791077fe3d44f90e7133eb68e7d22eaff2",
+                "reference": "a59a13791077fe3d44f90e7133eb68e7d22eaff2",
                 "shasum": ""
             },
             "require": {
                 "ext-pcre": "*",
-                "graham-campbell/result-type": "^1.1.2",
+                "graham-campbell/result-type": "^1.1.3",
                 "php": "^7.2.5 || ^8.0",
-                "phpoption/phpoption": "^1.9.2",
+                "phpoption/phpoption": "^1.9.3",
                 "symfony/polyfill-ctype": "^1.24",
                 "symfony/polyfill-mbstring": "^1.24",
                 "symfony/polyfill-php80": "^1.24"
@@ -1874,7 +1874,7 @@
             "extra": {
                 "bamarni-bin": {
                     "bin-links": true,
-                    "forward-command": true
+                    "forward-command": false
                 },
                 "branch-alias": {
                     "dev-master": "5.6-dev"
@@ -1909,7 +1909,7 @@
             ],
             "support": {
                 "issues": "https://github.com/vlucas/phpdotenv/issues",
-                "source": "https://github.com/vlucas/phpdotenv/tree/v5.6.0"
+                "source": "https://github.com/vlucas/phpdotenv/tree/v5.6.1"
             },
             "funding": [
                 {
@@ -1921,7 +1921,7 @@
                     "type": "tidelift"
                 }
             ],
-            "time": "2023-11-12T22:43:29+00:00"
+            "time": "2024-07-20T21:52:34+00:00"
         },
         {
             "name": "voku/portable-ascii",
diff --git a/kernel/IGTabel/JSONCreator.php b/kernel/IGTabel/JSONCreator.php
index 7c846a6..4bcd5fe 100644
--- a/kernel/IGTabel/JSONCreator.php
+++ b/kernel/IGTabel/JSONCreator.php
@@ -15,7 +15,8 @@ class JSONCreator
         if ($meta && $data) {
             $this->informationArray = [
                 "meta" => [
-                    "columns" => $meta['columns'],
+                    "columns" => $meta['columns'] ?? [],
+                    "rows" => $meta['rows'] ?? [],
                     "perPage" => $meta['perPage'] ?? 10,
                     "currentPage" => $meta['currentPage'] ?? 1,
                     "baseUrl" => $meta['baseUrl'] ?? '',
diff --git a/kernel/IGTabel/ViewJsonTableEloquentModel.php b/kernel/IGTabel/ViewJsonTableEloquentModel.php
new file mode 100644
index 0000000..07a0a96
--- /dev/null
+++ b/kernel/IGTabel/ViewJsonTableEloquentModel.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace kernel\IGTabel;
+
+use app\helpers\Debug;
+use Illuminate\Database\Eloquent\Model;
+
+class ViewJsonTableEloquentModel
+{
+    private string|null $jsonStr;
+
+    private array $meta = [];
+
+    public function __construct(Model $model, array $params = [])
+    {
+        $this->meta['rows'] = $params['rows'] ?? $model->labels();
+        $this->meta['baseUrl'] = $params['baseUrl'] ?? $model->table;
+        $this->meta['params'] = $params['params'] ?? [];
+        $this->meta['actions'] = $params['actions'] ?? [];
+
+        $this->jsonStr = (new JSONCreator($this->meta, $model->toArray()))->getJson();
+    }
+
+    public function getJson(): ?string
+    {
+        return $this->jsonStr;
+    }
+
+}
\ No newline at end of file