user crud

This commit is contained in:
Kavalar 2024-07-31 12:59:06 +03:00
parent 0459ed561c
commit be8fea187b
15 changed files with 134 additions and 233 deletions

View File

@ -3,6 +3,9 @@ DB_USER=root
DB_DRIVER=mysql DB_DRIVER=mysql
DB_PASSWORD=123edsaqw DB_PASSWORD=123edsaqw
DB_NAME=mfw DB_NAME=mfw
DB_CHARSET=utf8
DB_COLLATION=utf8_unicode_ci
DB_PREFIX=''
VIEWS_PATH=/views VIEWS_PATH=/views
VIEWS_CACHE_PATH=/views_cache VIEWS_CACHE_PATH=/views_cache

View File

@ -12,6 +12,7 @@ use app\tables\columns\UserEditActionColumn;
use app\tables\columns\UserViewActionColumn; use app\tables\columns\UserViewActionColumn;
use Exception; use Exception;
use http\Message; use http\Message;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Itguild\Tables\ListJsonTable; use Itguild\Tables\ListJsonTable;
use Itguild\Tables\ViewJsonTable; use Itguild\Tables\ViewJsonTable;
@ -19,6 +20,7 @@ use JetBrains\PhpStorm\NoReturn;
use kernel\App; use kernel\App;
use kernel\Controller; use kernel\Controller;
use kernel\IGTabel\btn\PrimaryBtn; use kernel\IGTabel\btn\PrimaryBtn;
use kernel\IGTabel\EloquentDataProvider;
use kernel\IGTabel\ListJsonTableEloquentCollection; use kernel\IGTabel\ListJsonTableEloquentCollection;
use kernel\IGTabel\ViewJsonTableEloquentModel; use kernel\IGTabel\ViewJsonTableEloquentModel;
use Twig\Error\LoaderError; use Twig\Error\LoaderError;
@ -60,11 +62,9 @@ class UserController extends Controller{
/** /**
* @throws \Exception * @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", ['page_number' => $page_number]);
$this->cgView->render("user/index.php", ['users' => $users, 'page_id' => $page_id]);
} }
/** /**

46
composer.lock generated
View File

@ -337,7 +337,7 @@
}, },
{ {
"name": "illuminate/collections", "name": "illuminate/collections",
"version": "v11.18.1", "version": "v11.19.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/illuminate/collections.git", "url": "https://github.com/illuminate/collections.git",
@ -392,7 +392,7 @@
}, },
{ {
"name": "illuminate/conditionable", "name": "illuminate/conditionable",
"version": "v11.18.1", "version": "v11.19.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/illuminate/conditionable.git", "url": "https://github.com/illuminate/conditionable.git",
@ -438,7 +438,7 @@
}, },
{ {
"name": "illuminate/container", "name": "illuminate/container",
"version": "v11.18.1", "version": "v11.19.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/illuminate/container.git", "url": "https://github.com/illuminate/container.git",
@ -489,16 +489,16 @@
}, },
{ {
"name": "illuminate/contracts", "name": "illuminate/contracts",
"version": "v11.18.1", "version": "v11.19.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/illuminate/contracts.git", "url": "https://github.com/illuminate/contracts.git",
"reference": "be935e9d9115a57be74d20176f43fa8a207029f3" "reference": "ebe2b8d69b8fb1c07111e3500d464e77dfab3202"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/illuminate/contracts/zipball/be935e9d9115a57be74d20176f43fa8a207029f3", "url": "https://api.github.com/repos/illuminate/contracts/zipball/ebe2b8d69b8fb1c07111e3500d464e77dfab3202",
"reference": "be935e9d9115a57be74d20176f43fa8a207029f3", "reference": "ebe2b8d69b8fb1c07111e3500d464e77dfab3202",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -533,20 +533,20 @@
"issues": "https://github.com/laravel/framework/issues", "issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework" "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", "name": "illuminate/database",
"version": "v11.18.1", "version": "v11.19.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/illuminate/database.git", "url": "https://github.com/illuminate/database.git",
"reference": "3785e0d14d8e427b72db08a9c969d4afdcf21cba" "reference": "db151e9a3221705cb4149c39dce2c51799708637"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/illuminate/database/zipball/3785e0d14d8e427b72db08a9c969d4afdcf21cba", "url": "https://api.github.com/repos/illuminate/database/zipball/db151e9a3221705cb4149c39dce2c51799708637",
"reference": "3785e0d14d8e427b72db08a9c969d4afdcf21cba", "reference": "db151e9a3221705cb4149c39dce2c51799708637",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -601,11 +601,11 @@
"issues": "https://github.com/laravel/framework/issues", "issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework" "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", "name": "illuminate/filesystem",
"version": "v11.18.1", "version": "v11.19.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/illuminate/filesystem.git", "url": "https://github.com/illuminate/filesystem.git",
@ -672,7 +672,7 @@
}, },
{ {
"name": "illuminate/macroable", "name": "illuminate/macroable",
"version": "v11.18.1", "version": "v11.19.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/illuminate/macroable.git", "url": "https://github.com/illuminate/macroable.git",
@ -718,16 +718,16 @@
}, },
{ {
"name": "illuminate/support", "name": "illuminate/support",
"version": "v11.18.1", "version": "v11.19.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/illuminate/support.git", "url": "https://github.com/illuminate/support.git",
"reference": "135bfb40087df34e7eae3ed699e753442f349020" "reference": "f3c19ee61d875dca1045df2d90000ae97f1645f9"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/illuminate/support/zipball/135bfb40087df34e7eae3ed699e753442f349020", "url": "https://api.github.com/repos/illuminate/support/zipball/f3c19ee61d875dca1045df2d90000ae97f1645f9",
"reference": "135bfb40087df34e7eae3ed699e753442f349020", "reference": "f3c19ee61d875dca1045df2d90000ae97f1645f9",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -788,7 +788,7 @@
"issues": "https://github.com/laravel/framework/issues", "issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework" "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", "name": "itguild/forms",
@ -851,11 +851,11 @@
}, },
{ {
"name": "itguild/tables", "name": "itguild/tables",
"version": "0.1.7", "version": "0.1.8",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://git.itguild.info/ItGuild/tables", "url": "https://git.itguild.info/ItGuild/tables",
"reference": "b7fea4122c25d8ba9eafd58c07d65e829ea4c383" "reference": "d6d703aaa6ea26005b029d1110b96b268ea30fe7"
}, },
"type": "library", "type": "library",
"autoload": { "autoload": {
@ -873,7 +873,7 @@
"email": "apuc06@mail.ru" "email": "apuc06@mail.ru"
} }
], ],
"time": "2024-07-30T09:20:16+00:00" "time": "2024-07-31T09:48:26+00:00"
}, },
{ {
"name": "madesimple/php-arrays", "name": "madesimple/php-arrays",

View File

@ -1,22 +0,0 @@
<?php
namespace core\console;
use Illuminate\Database\Migrations\MigrationCreator;
class CgMigrationCreator extends MigrationCreator
{
/**
* Get the path to the stubs.
*
* @return string
*/
public function stubPath()
{
return ROOT_DIR . '/core/console/migrations/stubs';
}
}

View File

@ -1,48 +0,0 @@
<?php
namespace core\console;
use core\App;
use core\Database;
use Phroute\Phroute\Dispatcher;
class ConsoleApp extends App
{
public $argv;
public function run()
{
$this->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();
}
}

View File

@ -1,30 +0,0 @@
<?php
namespace core\console;
use samejack\PHP\PHP_ArgvParser;
class ConsoleController
{
/**
* @var Out
*/
public $out;
protected $argv;
public function __construct()
{
$this->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);
}
}
}

View File

@ -1,78 +0,0 @@
<?php
namespace core\console;
class Out
{
private $foreground_colors = array();
private $background_colors = array();
public function __construct()
{
// Set up shell colors
$this->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);
}
}

View File

@ -42,4 +42,11 @@ class Database
$this->schema = $this->capsule->schema(); $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;
}
} }

View File

@ -0,0 +1,78 @@
<?php
namespace kernel\IGTabel;
use app\helpers\Debug;
use Exception;
use Illuminate\Database\Eloquent\Model;
class EloquentDataProvider
{
protected int $totalCount;
protected int $perPage = 10;
protected int $currentPage = 1;
protected Model $model;
protected $queryBuilder = false;
protected array $options = [];
protected array $meta = [];
protected string $jsonStr = '';
/**
* @throws Exception
*/
public function __construct($source, array $options)
{
if (is_string($source)) {
$this->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;
}
}

View File

@ -13,16 +13,7 @@ class JSONCreator
$params = empty($meta['params']) ? ["class" => "table table-bordered", "border" => "1"] : $meta['params']; $params = empty($meta['params']) ? ["class" => "table table-bordered", "border" => "1"] : $meta['params'];
if ($meta) { if ($meta) {
$this->informationArray = [ $this->informationArray = [
"meta" => [ "meta" => $meta,
"columns" => $meta['columns'] ?? [],
"rows" => $meta['rows'] ?? [],
"perPage" => $meta['perPage'] ?? 10,
"currentPage" => $meta['currentPage'] ?? 1,
"baseUrl" => $meta['baseUrl'] ?? '',
"actions" => $meta['actions'] ?? '',
"params" => $params
],
"data" => $data ?? [] "data" => $data ?? []
]; ];
} }

View File

@ -11,7 +11,7 @@ return new class extends Migration
*/ */
public function up(): void public function up(): void
{ {
Schema::create('{{ table }}', function (Blueprint $table) { \kernel\App::$db->schema->create('{{ table }}', function (Blueprint $table) {
$table->id(); $table->id();
$table->timestamps(); $table->timestamps();
}); });
@ -22,6 +22,6 @@ return new class extends Migration
*/ */
public function down(): void public function down(): void
{ {
Schema::dropIfExists('{{ table }}'); \kernel\App::$db->schema->dropIfExists('{{ table }}');
} }
}; };

View File

@ -11,7 +11,7 @@ return new class extends Migration
*/ */
public function up(): void 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 public function down(): void
{ {
Schema::table('{{ table }}', function (Blueprint $table) { \kernel\App::$db->schema->table('{{ table }}', function (Blueprint $table) {
// //
}); });
} }

View File

@ -11,9 +11,8 @@ return new class extends Migration
*/ */
public function up(): void public function up(): void
{ {
Schema::create('some', function (Blueprint $table) { \kernel\App::$db->schema->table('user', function (Blueprint $table) {
$table->id(); $table->integer('status')->default(1);
$table->timestamps();
}); });
} }
@ -22,6 +21,8 @@ return new class extends Migration
*/ */
public function down(): void public function down(): void
{ {
Schema::dropIfExists('some'); \kernel\App::$db->schema->table('user', function (Blueprint $table) {
$table->dropColumn(['status']);
});
} }
}; };

View File

@ -10,7 +10,7 @@ App::$collector->get('/example', [MainController::class, 'actionExample']);
App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){
App::$collector->group(["prefix" => "user"], callback: function (RouteCollector $router){ App::$collector->group(["prefix" => "user"], callback: function (RouteCollector $router){
App::$collector->get('/', [\app\controllers\UserController::class, 'actionIndex']); 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->get('/create', [\app\controllers\UserController::class, 'actionCreate']);
App::$collector->post("/", [\app\controllers\UserController::class, 'actionAdd']); App::$collector->post("/", [\app\controllers\UserController::class, 'actionAdd']);
App::$collector->get('/{id}', [\app\controllers\UserController::class, 'actionView']); App::$collector->get('/{id}', [\app\controllers\UserController::class, 'actionView']);

View File

@ -1,23 +1,22 @@
<?php <?php
/** /**
* @var \Illuminate\Database\Eloquent\Collection $users * @var \Illuminate\Database\Eloquent\Collection $users
* @var int $page_id * @var int $page_number
*/ */
use app\models\User; use app\models\User;
use app\tables\columns\UserDeleteActionColumn; use app\tables\columns\UserDeleteActionColumn;
use app\tables\columns\UserEditActionColumn; use app\tables\columns\UserEditActionColumn;
use app\tables\columns\UserViewActionColumn; use app\tables\columns\UserViewActionColumn;
use Illuminate\Support\Facades\DB;
use Itguild\Tables\ListJsonTable; use Itguild\Tables\ListJsonTable;
use kernel\IGTabel\btn\PrimaryBtn; use kernel\IGTabel\btn\PrimaryBtn;
use kernel\IGTabel\ListJsonTableEloquentCollection; use kernel\IGTabel\ListJsonTableEloquentCollection;
$perPage = 3;
$chunks = $users->forPage($page_id, $perPage); $dataProvider = new \kernel\IGTabel\EloquentDataProvider(User::where(['status' => 2]), [
$dataProvider = new ListJsonTableEloquentCollection($chunks, [ 'currentPage' => $page_number,
'model' => User::class, 'perPage' => 3,
'perPage' => 5,
'params' => ["class" => "table table-bordered", "border" => "2"], 'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/user", 'baseUrl' => "/admin/user",
]); ]);
@ -26,8 +25,8 @@ $table->beforePrint(function () {
return PrimaryBtn::create("Создать", "/admin/user/create")->fetch(); return PrimaryBtn::create("Создать", "/admin/user/create")->fetch();
//return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch(); //return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch();
}); });
$table->afterPrint(function () use ($page_id, $users, $perPage) { $table->afterPrint(function ($meta) {
$pagination = new \Itguild\Tables\Pagination($users->count(), $perPage, $page_id, "/admin/user/page"); $pagination = new \Itguild\Tables\Pagination($meta['totalWithFilters'], $meta['perPage'], $meta['currentPage'], $meta['baseUrl'] . "/page");
$pagination->create(); $pagination->create();
return $pagination->fetch(); return $pagination->fetch();
}); });