add Eloquent Tables
This commit is contained in:
@ -1,78 +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;
|
||||
}
|
||||
|
||||
}
|
||||
//
|
||||
//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;
|
||||
// }
|
||||
//
|
||||
//}
|
@ -1,40 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\IGTabel;
|
||||
|
||||
use app\helpers\Debug;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
//
|
||||
//namespace kernel\IGTabel;
|
||||
//
|
||||
//use app\helpers\Debug;
|
||||
//
|
||||
//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);
|
||||
// }
|
||||
//}
|
@ -1,42 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\IGTabel;
|
||||
|
||||
use app\helpers\Debug;
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
//
|
||||
//namespace kernel\IGTabel;
|
||||
//
|
||||
//use app\helpers\Debug;
|
||||
//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;
|
||||
// }
|
||||
//
|
||||
//}
|
@ -1,37 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\IGTabel;
|
||||
|
||||
use app\helpers\Debug;
|
||||
use app\models\User;
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
//
|
||||
//namespace kernel\IGTabel;
|
||||
//
|
||||
//use app\helpers\Debug;
|
||||
//use app\models\User;
|
||||
//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;
|
||||
// }
|
||||
//
|
||||
//}
|
Reference in New Issue
Block a user