diff --git a/.env.example b/.env.example index 2e77d26..369616c 100644 --- a/.env.example +++ b/.env.example @@ -3,6 +3,9 @@ DB_USER=root DB_DRIVER=mysql DB_PASSWORD=123edsaqw DB_NAME=mfw +DB_CHARSET=utf8 +DB_COLLATION=utf8_unicode_ci +DB_PREFIX='' VIEWS_PATH=/views VIEWS_CACHE_PATH=/views_cache \ No newline at end of file diff --git a/app/controllers/UserController.php b/app/controllers/UserController.php index 8ceb36c..3470fac 100644 --- a/app/controllers/UserController.php +++ b/app/controllers/UserController.php @@ -12,6 +12,7 @@ use app\tables\columns\UserEditActionColumn; use app\tables\columns\UserViewActionColumn; use Exception; use http\Message; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\Facades\DB; use Itguild\Tables\ListJsonTable; use Itguild\Tables\ViewJsonTable; @@ -19,6 +20,7 @@ use JetBrains\PhpStorm\NoReturn; use kernel\App; use kernel\Controller; use kernel\IGTabel\btn\PrimaryBtn; +use kernel\IGTabel\EloquentDataProvider; use kernel\IGTabel\ListJsonTableEloquentCollection; use kernel\IGTabel\ViewJsonTableEloquentModel; use Twig\Error\LoaderError; @@ -60,11 +62,9 @@ class UserController extends Controller{ /** * @throws \Exception */ - public function actionIndex($page_id = 1): void + public function actionIndex($page_number = 1): void { - $users = User::all(); - - $this->cgView->render("user/index.php", ['users' => $users, 'page_id' => $page_id]); + $this->cgView->render("user/index.php", ['page_number' => $page_number]); } /** diff --git a/composer.lock b/composer.lock index 52bbb23..aad281a 100644 --- a/composer.lock +++ b/composer.lock @@ -337,7 +337,7 @@ }, { "name": "illuminate/collections", - "version": "v11.18.1", + "version": "v11.19.0", "source": { "type": "git", "url": "https://github.com/illuminate/collections.git", @@ -392,7 +392,7 @@ }, { "name": "illuminate/conditionable", - "version": "v11.18.1", + "version": "v11.19.0", "source": { "type": "git", "url": "https://github.com/illuminate/conditionable.git", @@ -438,7 +438,7 @@ }, { "name": "illuminate/container", - "version": "v11.18.1", + "version": "v11.19.0", "source": { "type": "git", "url": "https://github.com/illuminate/container.git", @@ -489,16 +489,16 @@ }, { "name": "illuminate/contracts", - "version": "v11.18.1", + "version": "v11.19.0", "source": { "type": "git", "url": "https://github.com/illuminate/contracts.git", - "reference": "be935e9d9115a57be74d20176f43fa8a207029f3" + "reference": "ebe2b8d69b8fb1c07111e3500d464e77dfab3202" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/contracts/zipball/be935e9d9115a57be74d20176f43fa8a207029f3", - "reference": "be935e9d9115a57be74d20176f43fa8a207029f3", + "url": "https://api.github.com/repos/illuminate/contracts/zipball/ebe2b8d69b8fb1c07111e3500d464e77dfab3202", + "reference": "ebe2b8d69b8fb1c07111e3500d464e77dfab3202", "shasum": "" }, "require": { @@ -533,20 +533,20 @@ "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, - "time": "2024-07-09T13:57:38+00:00" + "time": "2024-07-29T06:48:51+00:00" }, { "name": "illuminate/database", - "version": "v11.18.1", + "version": "v11.19.0", "source": { "type": "git", "url": "https://github.com/illuminate/database.git", - "reference": "3785e0d14d8e427b72db08a9c969d4afdcf21cba" + "reference": "db151e9a3221705cb4149c39dce2c51799708637" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/database/zipball/3785e0d14d8e427b72db08a9c969d4afdcf21cba", - "reference": "3785e0d14d8e427b72db08a9c969d4afdcf21cba", + "url": "https://api.github.com/repos/illuminate/database/zipball/db151e9a3221705cb4149c39dce2c51799708637", + "reference": "db151e9a3221705cb4149c39dce2c51799708637", "shasum": "" }, "require": { @@ -601,11 +601,11 @@ "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, - "time": "2024-07-26T06:18:25+00:00" + "time": "2024-07-30T07:00:12+00:00" }, { "name": "illuminate/filesystem", - "version": "v11.18.1", + "version": "v11.19.0", "source": { "type": "git", "url": "https://github.com/illuminate/filesystem.git", @@ -672,7 +672,7 @@ }, { "name": "illuminate/macroable", - "version": "v11.18.1", + "version": "v11.19.0", "source": { "type": "git", "url": "https://github.com/illuminate/macroable.git", @@ -718,16 +718,16 @@ }, { "name": "illuminate/support", - "version": "v11.18.1", + "version": "v11.19.0", "source": { "type": "git", "url": "https://github.com/illuminate/support.git", - "reference": "135bfb40087df34e7eae3ed699e753442f349020" + "reference": "f3c19ee61d875dca1045df2d90000ae97f1645f9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/support/zipball/135bfb40087df34e7eae3ed699e753442f349020", - "reference": "135bfb40087df34e7eae3ed699e753442f349020", + "url": "https://api.github.com/repos/illuminate/support/zipball/f3c19ee61d875dca1045df2d90000ae97f1645f9", + "reference": "f3c19ee61d875dca1045df2d90000ae97f1645f9", "shasum": "" }, "require": { @@ -788,7 +788,7 @@ "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, - "time": "2024-07-26T05:48:25+00:00" + "time": "2024-07-30T06:57:25+00:00" }, { "name": "itguild/forms", @@ -851,11 +851,11 @@ }, { "name": "itguild/tables", - "version": "0.1.7", + "version": "0.1.8", "source": { "type": "git", "url": "https://git.itguild.info/ItGuild/tables", - "reference": "b7fea4122c25d8ba9eafd58c07d65e829ea4c383" + "reference": "d6d703aaa6ea26005b029d1110b96b268ea30fe7" }, "type": "library", "autoload": { @@ -873,7 +873,7 @@ "email": "apuc06@mail.ru" } ], - "time": "2024-07-30T09:20:16+00:00" + "time": "2024-07-31T09:48:26+00:00" }, { "name": "madesimple/php-arrays", diff --git a/console/CgMigrationCreator.php b/console/CgMigrationCreator.php deleted file mode 100644 index 0ad2709..0000000 --- a/console/CgMigrationCreator.php +++ /dev/null @@ -1,22 +0,0 @@ -setMods(); - if(!$rout = $this->getRout()){ - echo "Not found \n"; - exit(); - } - App::$db = new Database(); - $dispatcher = new Dispatcher(App::$collector->getData()); - $response = $dispatcher->dispatch('GET', $rout); - echo $response; - } - - public function setArgv($argv) - { - $this->argv = $argv; - return $this; - } - - private function getRout() - { - if(isset($this->argv[1])){ - return $this->argv[1]; - } - return null; - } - - public static function start() - { - return new self(); - } - - -} \ No newline at end of file diff --git a/console/ConsoleController.php b/console/ConsoleController.php deleted file mode 100755 index 6e3a0a2..0000000 --- a/console/ConsoleController.php +++ /dev/null @@ -1,30 +0,0 @@ -out = new Out(); - $argv = $_SERVER['argv']; - unset($argv[0]); - unset($argv[1]); - if(!empty($argv)){ - $argvParser = new PHP_ArgvParser(); - $tmp = implode(" ", $argv); - $this->argv = $argvParser->parseConfigs($tmp); - } - } - -} \ No newline at end of file diff --git a/console/Out.php b/console/Out.php deleted file mode 100755 index 088ad68..0000000 --- a/console/Out.php +++ /dev/null @@ -1,78 +0,0 @@ -foreground_colors['black'] = '0;30'; - $this->foreground_colors['dark_gray'] = '1;30'; - $this->foreground_colors['blue'] = '0;34'; - $this->foreground_colors['light_blue'] = '1;34'; - $this->foreground_colors['green'] = '0;32'; - $this->foreground_colors['light_green'] = '1;32'; - $this->foreground_colors['cyan'] = '0;36'; - $this->foreground_colors['light_cyan'] = '1;36'; - $this->foreground_colors['red'] = '0;31'; - $this->foreground_colors['light_red'] = '1;31'; - $this->foreground_colors['purple'] = '0;35'; - $this->foreground_colors['light_purple'] = '1;35'; - $this->foreground_colors['brown'] = '0;33'; - $this->foreground_colors['yellow'] = '1;33'; - $this->foreground_colors['light_gray'] = '0;37'; - $this->foreground_colors['white'] = '1;37'; - - $this->background_colors['black'] = '40'; - $this->background_colors['red'] = '41'; - $this->background_colors['green'] = '42'; - $this->background_colors['yellow'] = '43'; - $this->background_colors['blue'] = '44'; - $this->background_colors['magenta'] = '45'; - $this->background_colors['cyan'] = '46'; - $this->background_colors['light_gray'] = '47'; - } - - // Returns colored string - public function get($string, $foreground_color = null, $background_color = null) - { - $colored_string = ""; - - // Check if given foreground color found - if (isset($this->foreground_colors[$foreground_color])) { - $colored_string .= "\033[" . $this->foreground_colors[$foreground_color] . "m"; - } - // Check if given background color found - if (isset($this->background_colors[$background_color])) { - $colored_string .= "\033[" . $this->background_colors[$background_color] . "m"; - } - - // Add string and end coloring - $colored_string .= $string . "\033[0m"; - - return $colored_string; - } - - public function r($string, $foreground_color = null, $background_color = null) - { - echo $this->get($string, $foreground_color, $background_color) . "\n"; - } - - // Returns all foreground color names - public function getForegroundColors() - { - return array_keys($this->foreground_colors); - } - - // Returns all background color names - public function getBackgroundColors() - { - return array_keys($this->background_colors); - } -} \ No newline at end of file diff --git a/kernel/Database.php b/kernel/Database.php index c7c0f16..3d1dec0 100755 --- a/kernel/Database.php +++ b/kernel/Database.php @@ -42,4 +42,11 @@ class Database $this->schema = $this->capsule->schema(); } + + public function createBuilder(string $table_name): \Illuminate\Database\Query\Builder + { + $builder = new \Illuminate\Database\Query\Builder($this->schema->getConnection()); + $builder->from($table_name); + return $builder; + } } \ No newline at end of file diff --git a/kernel/IGTabel/EloquentDataProvider.php b/kernel/IGTabel/EloquentDataProvider.php new file mode 100644 index 0000000..db0d53f --- /dev/null +++ b/kernel/IGTabel/EloquentDataProvider.php @@ -0,0 +1,78 @@ +queryBuilder = $source::query(); + $model = new $source(); + } elseif (is_object($source)) { + $this->queryBuilder = $source; + $model = $source->getModel(); + } else { + throw new Exception(message: "source is not valid"); + } + $this->options = $options; + $this->currentPage = $this->options['currentPage'] ?? 1; + $this->perPage = $this->options['perPage'] ?? 10; + $this->meta['total'] = $model->count(); + $this->meta['totalWithFilters'] = $this->queryBuilder->count(); + $this->meta['columns'] = $options['columns'] ?? $model->labels(); + $this->meta['perPage'] = $options['perPage'] ?? 10; + $this->meta['currentPage'] = $options['currentPage'] ?? 1; + $this->meta['baseUrl'] = $options['baseUrl'] ?? $model->table; + $this->meta['params'] = $options['params'] ?? []; + $this->meta['actions'] = $options['actions'] ?? []; + $this->createQuery(); + + $this->jsonStr = (new JSONCreator($this->meta, $this->getCollection()->toArray()))->getJson(); + + } + + public function createQuery(): void + { + if ($this->currentPage > 1) { + $this->queryBuilder->skip(($this->currentPage - 1) * $this->perPage)->take($this->perPage); + } else { + $this->queryBuilder->take($this->perPage); + } + } + + public function getCollection() + { + return $this->queryBuilder->get(); + } + + /** + * @return string|null + */ + public function getJson(): string|null + { + return $this->jsonStr; + } + +} \ No newline at end of file diff --git a/kernel/IGTabel/JSONCreator.php b/kernel/IGTabel/JSONCreator.php index 4d96b6c..0f28248 100644 --- a/kernel/IGTabel/JSONCreator.php +++ b/kernel/IGTabel/JSONCreator.php @@ -13,16 +13,7 @@ class JSONCreator $params = empty($meta['params']) ? ["class" => "table table-bordered", "border" => "1"] : $meta['params']; if ($meta) { $this->informationArray = [ - "meta" => [ - "columns" => $meta['columns'] ?? [], - "rows" => $meta['rows'] ?? [], - "perPage" => $meta['perPage'] ?? 10, - "currentPage" => $meta['currentPage'] ?? 1, - "baseUrl" => $meta['baseUrl'] ?? '', - "actions" => $meta['actions'] ?? '', - "params" => $params - - ], + "meta" => $meta, "data" => $data ?? [] ]; } diff --git a/kernel/console/migrations/stubs/migration.create.stub b/kernel/console/migrations/stubs/migration.create.stub index 168c622..58d4fed 100755 --- a/kernel/console/migrations/stubs/migration.create.stub +++ b/kernel/console/migrations/stubs/migration.create.stub @@ -11,7 +11,7 @@ return new class extends Migration */ public function up(): void { - Schema::create('{{ table }}', function (Blueprint $table) { + \kernel\App::$db->schema->create('{{ table }}', function (Blueprint $table) { $table->id(); $table->timestamps(); }); @@ -22,6 +22,6 @@ return new class extends Migration */ public function down(): void { - Schema::dropIfExists('{{ table }}'); + \kernel\App::$db->schema->dropIfExists('{{ table }}'); } }; diff --git a/kernel/console/migrations/stubs/migration.update.stub b/kernel/console/migrations/stubs/migration.update.stub index c9ff2c4..3327a65 100755 --- a/kernel/console/migrations/stubs/migration.update.stub +++ b/kernel/console/migrations/stubs/migration.update.stub @@ -11,7 +11,7 @@ return new class extends Migration */ public function up(): void { - Schema::table('{{ table }}', function (Blueprint $table) { + \kernel\App::$db->schema->table('{{ table }}', function (Blueprint $table) { // }); } @@ -21,7 +21,7 @@ return new class extends Migration */ public function down(): void { - Schema::table('{{ table }}', function (Blueprint $table) { + \kernel\App::$db->schema->table('{{ table }}', function (Blueprint $table) { // }); } diff --git a/migrations/2024_07_28_171433_some.php b/migrations/2024_07_31_083125_add_status_column_at_user_table.php similarity index 58% rename from migrations/2024_07_28_171433_some.php rename to migrations/2024_07_31_083125_add_status_column_at_user_table.php index b6e4805..e401141 100644 --- a/migrations/2024_07_28_171433_some.php +++ b/migrations/2024_07_31_083125_add_status_column_at_user_table.php @@ -11,9 +11,8 @@ return new class extends Migration */ public function up(): void { - Schema::create('some', function (Blueprint $table) { - $table->id(); - $table->timestamps(); + \kernel\App::$db->schema->table('user', function (Blueprint $table) { + $table->integer('status')->default(1); }); } @@ -22,6 +21,8 @@ return new class extends Migration */ public function down(): void { - Schema::dropIfExists('some'); + \kernel\App::$db->schema->table('user', function (Blueprint $table) { + $table->dropColumn(['status']); + }); } }; diff --git a/rout.php b/rout.php index 3a59606..08b57b7 100644 --- a/rout.php +++ b/rout.php @@ -10,7 +10,7 @@ App::$collector->get('/example', [MainController::class, 'actionExample']); App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){ App::$collector->group(["prefix" => "user"], callback: function (RouteCollector $router){ App::$collector->get('/', [\app\controllers\UserController::class, 'actionIndex']); - App::$collector->get('/page/{page_id}', [\app\controllers\UserController::class, 'actionIndex']); + App::$collector->get('/page/{page_number}', [\app\controllers\UserController::class, 'actionIndex']); App::$collector->get('/create', [\app\controllers\UserController::class, 'actionCreate']); App::$collector->post("/", [\app\controllers\UserController::class, 'actionAdd']); App::$collector->get('/{id}', [\app\controllers\UserController::class, 'actionView']); diff --git a/views/admin/user/index.php b/views/admin/user/index.php index ec28b90..9074016 100644 --- a/views/admin/user/index.php +++ b/views/admin/user/index.php @@ -1,23 +1,22 @@ forPage($page_id, $perPage); -$dataProvider = new ListJsonTableEloquentCollection($chunks, [ - 'model' => User::class, - 'perPage' => 5, + +$dataProvider = new \kernel\IGTabel\EloquentDataProvider(User::where(['status' => 2]), [ + 'currentPage' => $page_number, + 'perPage' => 3, 'params' => ["class" => "table table-bordered", "border" => "2"], 'baseUrl' => "/admin/user", ]); @@ -26,8 +25,8 @@ $table->beforePrint(function () { return PrimaryBtn::create("Создать", "/admin/user/create")->fetch(); //return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch(); }); -$table->afterPrint(function () use ($page_id, $users, $perPage) { - $pagination = new \Itguild\Tables\Pagination($users->count(), $perPage, $page_id, "/admin/user/page"); +$table->afterPrint(function ($meta) { + $pagination = new \Itguild\Tables\Pagination($meta['totalWithFilters'], $meta['perPage'], $meta['currentPage'], $meta['baseUrl'] . "/page"); $pagination->create(); return $pagination->fetch(); });