post filters

This commit is contained in:
Билай Станислав 2024-12-24 16:38:28 +03:00
parent 04bd134e3e
commit f0bda2ee24
4 changed files with 77 additions and 51 deletions

View File

@ -0,0 +1,24 @@
<?php
namespace kernel\filters;
use Itguild\Tables\Filter\Filter;
use kernel\helpers\Debug;
class CustomSelectFilterForAssociativeArr extends Filter
{
public function fetch()
{
$this->html = "<td><select class='form-control' name='$this->name'>";
foreach ($this->param as $key => $value) {
if ($key == $this->value) {
$this->html .= "<option value='$key' selected>$value</option>";
} else {
$this->html .= "<option value='$key'>$value</option>";
}
}
$this->html .= "value='$this->value'</select></td>";
return $this->html;
}
}

View File

@ -45,30 +45,9 @@ class ModuleShopClientController extends AdminController
if ($this->moduleService->issetModuleShopToken()) { if ($this->moduleService->issetModuleShopToken()) {
if ($this->moduleService->isServerAvailable()) { if ($this->moduleService->isServerAvailable()) {
$request = new Request();
$filters = $request->get();
$per_page = 8; $per_page = 8;
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
// $this->moduleShopClientService->getModulesInfo($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug') $modules_info = json_decode($modules_info->getBody()->getContents(), true);
$modules = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
$modules = json_decode($modules->getBody()->getContents(), true);
if ($filters) {
$modules_info = [];
foreach ($modules as $module) {
foreach ($filters as $key => $value) {
if ($value === '') continue;
if ($module[$key] !== $value) {
break;
}
$modules_info[] = $module;
}
}
} else {
$modules_info = $modules;
}
$module_count = count($modules_info); $module_count = count($modules_info);
$modules_info = array_slice($modules_info, $per_page * ($page_number - 1), $per_page); $modules_info = array_slice($modules_info, $per_page * ($page_number - 1), $per_page);

View File

@ -6,17 +6,17 @@ use kernel\helpers\RESTClient;
class ModuleShopClientService class ModuleShopClientService
{ {
public function getModulesInfo(string $url, int $perPage, int $pageNumber): \Psr\Http\Message\ResponseInterface // public function getModulesInfo(string $url, int $perPage, int $pageNumber): \Psr\Http\Message\ResponseInterface
{ // {
$modules_info = RESTClient::request($url); // $modules_info = RESTClient::request($url);
$modules_info = json_decode($modules_info->getBody()->getContents(), true); // $modules_info = json_decode($modules_info->getBody()->getContents(), true);
return array_slice($modules_info, $perPage * ($pageNumber - 1), $perPage); // return array_slice($modules_info, $perPage * ($pageNumber - 1), $perPage);
} // }
//
public function getModulesInfoWithFilters(string $url, int $perPage, int $pageNumber): \Psr\Http\Message\ResponseInterface // public function getModulesInfoWithFilters(string $url, int $perPage, int $pageNumber): \Psr\Http\Message\ResponseInterface
{ // {
$modules_info = RESTClient::request($url); // $modules_info = RESTClient::request($url);
$modules_info = json_decode($modules_info->getBody()->getContents(), true); // $modules_info = json_decode($modules_info->getBody()->getContents(), true);
return array_slice($modules_info, $perPage * ($pageNumber - 1), $perPage); // return array_slice($modules_info, $perPage * ($pageNumber - 1), $perPage);
} // }
} }

View File

@ -15,14 +15,19 @@ use kernel\widgets\IconBtn\IconBtnDeleteWidget;
use kernel\widgets\IconBtn\IconBtnEditWidget; use kernel\widgets\IconBtn\IconBtnEditWidget;
use kernel\widgets\IconBtn\IconBtnViewWidget; use kernel\widgets\IconBtn\IconBtnViewWidget;
$request = (new \kernel\Request())->get();
$table = new ListEloquentTable(new EloquentDataProvider(Post::class, [ $table = new ListEloquentTable(new EloquentDataProvider(Post::class, [
'currentPage' => $page_number, 'currentPage' => $page_number,
'perPage' => 3, 'perPage' => 3,
'params' => ["class" => "table table-bordered", "border" => "2"], 'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/post", 'baseUrl' => "/admin/post",
'showFiltersRow' => false, 'searchPrefix' => "",
'searchParams' => (new \kernel\Request())->get()
])); ]));
//\kernel\helpers\Debug::dd((new \kernel\Request())->get());
$view->setTitle("Список постов"); $view->setTitle("Список постов");
$view->setMeta([ $view->setMeta([
'description' => 'Список постов системы' 'description' => 'Список постов системы'
@ -37,14 +42,24 @@ foreach ($additionals as $additional) {
}); });
} }
$table->columns([ //\kernel\helpers\Debug::dd($request);
'created_at' => function ($data) {
if (!$data){
return null;
}
return (new DateTimeImmutable($data))->format("d-m-Y"); $table->columns([
}, 'title' => [
'filter' => [
'class' => \Itguild\Tables\Filter\InputTextFilter::class,
'value' => $request['title'] ?? ''
]
],
'content' => [
'filter' => [
'class' => \Itguild\Tables\Filter\InputTextFilter::class,
'value' => $request['content'] ?? ''
]
],
'created_at' => [
'format' => 'date:d-m-Y',
],
'updated_at' => function ($data) { 'updated_at' => function ($data) {
if (!$data) { if (!$data) {
return null; return null;
@ -52,11 +67,19 @@ $table->columns([
return (new DateTimeImmutable($data))->format("d-m-Y"); return (new DateTimeImmutable($data))->format("d-m-Y");
}, },
'user_id' => (function ($data) { 'user_id' => [
'value' => function ($data) {
return User::find($data)->username; return User::find($data)->username;
}) },
'filter' => [
'class' => \kernel\filters\CustomSelectFilterForAssociativeArr::class,
'param' => \kernel\modules\user\service\UserService::createUsernameArr(),
'value' => $request['user_id'] ?? ''
]
]
]); ]);
$table->beforePrint(function () { $table->beforePrint(function () {
return IconBtnCreateWidget::create(['url' => '/admin/post/create'])->run(); return IconBtnCreateWidget::create(['url' => '/admin/post/create'])->run();
}); });