first
This commit is contained in:
commit
1316f68469
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
.idea
|
||||
.env
|
||||
vendor
|
24
composer.json
Normal file
24
composer.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "itguild/eloquent_table",
|
||||
"type": "library",
|
||||
"require": {
|
||||
"itguild/tables": "dev-master",
|
||||
"illuminate/database": "^12.0@dev",
|
||||
"illuminate/filesystem": "^12.0@dev",
|
||||
"vlucas/phpdotenv": "^5.6@dev",
|
||||
"twbs/bootstrap": "dev-main"
|
||||
},
|
||||
"license": "MIT",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Itguild\\EloquentTable\\": "src/"
|
||||
}
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
"name": "Kavalar",
|
||||
"email": "apuc06@mail.ru"
|
||||
}
|
||||
],
|
||||
"minimum-stability": "dev"
|
||||
}
|
1975
composer.lock
generated
Normal file
1975
composer.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
47
index.php
Normal file
47
index.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use Illuminate\Database\Schema\Builder;
|
||||
use Illuminate\Container\Container;
|
||||
use Itguild\EloquentTable\EloquentDataProvider;
|
||||
use Itguild\Tables\ListJsonTable;
|
||||
|
||||
ini_set("display_errors", true);
|
||||
error_reporting(-1);
|
||||
|
||||
require_once __DIR__ . "/vendor/autoload.php";
|
||||
|
||||
echo "<link rel='stylesheet' href='/vendor/twbs/bootstrap/dist/css/bootstrap.css'>";
|
||||
|
||||
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
|
||||
$dotenv->load();
|
||||
|
||||
$capsule = new Capsule;
|
||||
$capsule->addConnection([
|
||||
'driver' => $_ENV['DB_DRIVER'],
|
||||
'host' => $_ENV['DB_HOST'],
|
||||
'database' => $_ENV['DB_NAME'],
|
||||
'username' => $_ENV['DB_USER'],
|
||||
'password' => $_ENV['DB_PASSWORD'],
|
||||
'charset' => $_ENV['DB_CHARSET'],
|
||||
'collation' => $_ENV['DB_COLLATION'],
|
||||
'prefix' => $_ENV['DB_PREFIX'],
|
||||
]);
|
||||
// Setup the Eloquent ORM…
|
||||
|
||||
$capsule->setAsGlobal();
|
||||
|
||||
$capsule->bootEloquent();
|
||||
|
||||
$schema = $capsule->schema();
|
||||
|
||||
$dataProvider = new EloquentDataProvider(\Itguild\EloquentTable\models\User::class, [
|
||||
'currentPage' => 1,
|
||||
'perPage' => 3,
|
||||
'params' => ["class" => "table table-bordered", "border" => "2"],
|
||||
'baseUrl' => "/admin/user",
|
||||
]);
|
||||
$table = new ListJsonTable($dataProvider->getJson());
|
||||
|
||||
$table->addAction(\Itguild\Tables\ActionColumn\EditActionColumn::class);
|
||||
$table->create();
|
||||
$table->render();
|
79
src/EloquentDataProvider.php
Normal file
79
src/EloquentDataProvider.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\EloquentTable;
|
||||
|
||||
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)) {
|
||||
echo "<pre>";
|
||||
print_r($source);die();
|
||||
$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;
|
||||
}
|
||||
|
||||
}
|
39
src/JSONCreator.php
Normal file
39
src/JSONCreator.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\EloquentTable;
|
||||
|
||||
|
||||
class JSONCreator
|
||||
{
|
||||
protected array $informationArray = [];
|
||||
|
||||
public function __construct(array $meta, array $data)
|
||||
{
|
||||
$params = empty($meta['params']) ? ["class" => "table table-bordered", "border" => "1"] : $meta['params'];
|
||||
if ($meta) {
|
||||
$this->informationArray = [
|
||||
"meta" => $meta,
|
||||
"data" => $data ?? []
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $infArr
|
||||
* @return string|null
|
||||
*/
|
||||
protected function toJson(array $infArr): ?string
|
||||
{
|
||||
if ($infArr)
|
||||
return json_encode($infArr, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getJson(): ?string
|
||||
{
|
||||
return $this->toJson($this->informationArray);
|
||||
}
|
||||
}
|
41
src/ListJsonTableEloquentCollection.php
Normal file
41
src/ListJsonTableEloquentCollection.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\EloquentTable;
|
||||
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
class ListJsonTableEloquentCollection
|
||||
{
|
||||
private string|null $jsonStr;
|
||||
private array $meta = [];
|
||||
|
||||
/**
|
||||
* @param Collection $collection
|
||||
* @param array $params
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __construct(Collection $collection, array $params = [])
|
||||
{
|
||||
if (!isset($params['model'])) {
|
||||
throw new \Exception(message: "The parameter model must not be empty");
|
||||
}
|
||||
$model = new $params['model']();
|
||||
$this->meta['columns'] = $params['columns'] ?? $model->labels();
|
||||
$this->meta['perPage'] = $params['perPage'] ?? 10;
|
||||
$this->meta['currentPage'] = $params['currentPage'] ?? 1;
|
||||
$this->meta['baseUrl'] = $params['baseUrl'] ?? $model->table;
|
||||
$this->meta['params'] = $params['params'] ?? [];
|
||||
$this->meta['actions'] = $params['actions'] ?? [];
|
||||
|
||||
$this->jsonStr = (new JSONCreator($this->meta, $collection->toArray()))->getJson();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getJson(): string|null
|
||||
{
|
||||
return $this->jsonStr;
|
||||
}
|
||||
|
||||
}
|
35
src/ViewJsonTableEloquentModel.php
Normal file
35
src/ViewJsonTableEloquentModel.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Itguild\EloquentTable;
|
||||
|
||||
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();
|
||||
$model = $model->toArray();
|
||||
// if(isset($model['user_id']))
|
||||
// {
|
||||
// $model['user_id'] = User::find($model['user_id'])->username;
|
||||
// }
|
||||
|
||||
$this->jsonStr = (new JSONCreator($this->meta, $model))->getJson();
|
||||
}
|
||||
|
||||
public function getJson(): ?string
|
||||
{
|
||||
return $this->jsonStr;
|
||||
}
|
||||
|
||||
}
|
27
src/models/User.php
Normal file
27
src/models/User.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
namespace Itguild\EloquentTable\models;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $username
|
||||
* @property string $email
|
||||
* @property string $password_hash
|
||||
* @method static where(int[] $array)
|
||||
* @method static find($id)
|
||||
*/
|
||||
class User extends Model {
|
||||
protected $table = 'user';
|
||||
protected $fillable = ['username', 'email', 'password_hash', 'role'];
|
||||
protected array $dates = ['deleted at'];
|
||||
|
||||
public static function labels(): array
|
||||
{
|
||||
return [
|
||||
'username' => 'Логин',
|
||||
'email' => 'Email',
|
||||
'created_at' => 'Создан',
|
||||
'updated_at' => 'Обновлен'
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user