photo module
This commit is contained in:
104
kernel/app_modules/photo/controllers/PhotoController.php
Executable file
104
kernel/app_modules/photo/controllers/PhotoController.php
Executable file
@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\photo\controllers;
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use kernel\AdminController;
|
||||
use kernel\app_modules\photo\models\form\CreatePhotoForm;
|
||||
use kernel\app_modules\photo\models\Photo;
|
||||
use kernel\app_modules\photo\services\PhotoService;
|
||||
use kernel\helpers\Debug;
|
||||
class PhotoController extends AdminController
|
||||
{
|
||||
private PhotoService $photoService;
|
||||
protected function init(): void
|
||||
{
|
||||
parent::init();
|
||||
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/photo/views/";
|
||||
$this->photoService = new PhotoService();
|
||||
}
|
||||
|
||||
public function actionCreate(): void
|
||||
{
|
||||
$this->cgView->render("form.php");
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionAdd(): void
|
||||
{
|
||||
$photoForm = new CreatePhotoForm();
|
||||
$photoForm->load($_REQUEST);
|
||||
if ($photoForm->validate()){
|
||||
$photo = $this->photoService->create($photoForm);
|
||||
if ($photo){
|
||||
$this->redirect("/admin/photo/view/" . $photo->id);
|
||||
}
|
||||
}
|
||||
$this->redirect("/admin/photo/create");
|
||||
}
|
||||
|
||||
public function actionIndex($page_number = 1): void
|
||||
{
|
||||
$this->cgView->render("index.php", ['page_number' => $page_number]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionView($id): void
|
||||
{
|
||||
$photo = Photo::find($id);
|
||||
|
||||
if (!$photo){
|
||||
throw new Exception(message: "The photo not found");
|
||||
}
|
||||
$this->cgView->render("view.php", ['photo' => $photo]);
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @throws Exception
|
||||
// */
|
||||
// public function actionUpdate($id): void
|
||||
// {
|
||||
// $model = Tag::find($id);
|
||||
// if (!$model){
|
||||
// throw new Exception(message: "The tag not found");
|
||||
// }
|
||||
//
|
||||
// $this->cgView->render("form.php", ['model' => $model]);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * @throws Exception
|
||||
// */
|
||||
// public function actionEdit($id): void
|
||||
// {
|
||||
// $tag = Tag::find($id);
|
||||
// if (!$tag){
|
||||
// throw new Exception(message: "The tag not found");
|
||||
// }
|
||||
// $tagForm = new CreateTagForm();
|
||||
// $tagService = new TagService();
|
||||
// $tagForm->load($_REQUEST);
|
||||
// if ($tagForm->validate()) {
|
||||
// $tag = $tagService->update($tagForm, $tag);
|
||||
// if ($tag) {
|
||||
// $this->redirect("/admin/tag/view/" . $tag->id);
|
||||
// }
|
||||
// }
|
||||
// $this->redirect("/admin/tag/update/" . $id);
|
||||
// }
|
||||
//
|
||||
// #[NoReturn] public function actionDelete($id): void
|
||||
// {
|
||||
// $post = Tag::find($id)->first();
|
||||
// $post->delete();
|
||||
// $this->redirect("/admin/tag/");
|
||||
// }
|
||||
//
|
||||
// public function actionSettings(): void
|
||||
// {
|
||||
// $this->cgView->render('form.php');
|
||||
// }
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
\kernel\App::$db->schema->create('photo', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('image', 255)->nullable(false);
|
||||
$table->string('entity', 255)->nullable(false);
|
||||
$table->integer('entity_id')->default(1);
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
\kernel\App::$db->schema->dropIfExists('photo');
|
||||
}
|
||||
};
|
44
kernel/app_modules/photo/models/Photo.php
Executable file
44
kernel/app_modules/photo/models/Photo.php
Executable file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\photo\models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $image
|
||||
* @property string $entity
|
||||
* @property int $entity_id
|
||||
*/
|
||||
class Photo extends Model
|
||||
{
|
||||
protected $table = 'photo';
|
||||
|
||||
protected $fillable = ['image', 'entity', 'entity_id'];
|
||||
|
||||
public static function labels(): array
|
||||
{
|
||||
return [
|
||||
'image' => 'Фото',
|
||||
'entity' => 'Сущность',
|
||||
'entity_id' => 'Идентификатор сущности',
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPhotoListByEntity(string $entity): array
|
||||
{
|
||||
return self::where("entity", $entity)->get()->toArray();
|
||||
}
|
||||
|
||||
public static function getPhotoByEntity(string $entity): array
|
||||
{
|
||||
$result = [];
|
||||
$photos = self::getPhotoListByEntity($entity);
|
||||
foreach ($photos as $photo) {
|
||||
$result[$photo['id']] = $photo['label'];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
19
kernel/app_modules/photo/models/form/CreatePhotoForm.php
Executable file
19
kernel/app_modules/photo/models/form/CreatePhotoForm.php
Executable file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\photo\models\form;
|
||||
|
||||
use kernel\FormModel;
|
||||
|
||||
class CreatePhotoForm extends FormModel
|
||||
{
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'image' => 'required',
|
||||
'entity' => 'required|string',
|
||||
'entity_id' => 'required|integer|min:1',
|
||||
];
|
||||
}
|
||||
|
||||
}
|
20
kernel/app_modules/photo/routs/photo.php
Executable file
20
kernel/app_modules/photo/routs/photo.php
Executable file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
use kernel\App;
|
||||
use kernel\CgRouteCollector;
|
||||
use Phroute\Phroute\RouteCollector;
|
||||
|
||||
App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $router) {
|
||||
App::$collector->group(["before" => "auth"], function (RouteCollector $router) {
|
||||
App::$collector->group(["prefix" => "photo"], function (CGRouteCollector $router) {
|
||||
App::$collector->get('/', [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionIndex']);
|
||||
App::$collector->get('/page/{page_number}', [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionIndex']);
|
||||
App::$collector->get('/create', [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionCreate']);
|
||||
App::$collector->post("/", [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionAdd']);
|
||||
App::$collector->get('/view/{id}', [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionView']);
|
||||
App::$collector->any('/update/{id}', [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionUpdate']);
|
||||
App::$collector->any("/edit/{id}", [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionEdit']);
|
||||
App::$collector->get('/delete/{id}', [\kernel\app_modules\photo\controllers\PhotoController::class]);
|
||||
});
|
||||
});
|
||||
});
|
34
kernel/app_modules/photo/services/PhotoService.php
Executable file
34
kernel/app_modules/photo/services/PhotoService.php
Executable file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\photo\services;
|
||||
|
||||
use kernel\app_modules\photo\models\Photo;
|
||||
use kernel\FormModel;
|
||||
use kernel\helpers\Debug;
|
||||
|
||||
class PhotoService
|
||||
{
|
||||
public function create(FormModel $form_model): false|Photo
|
||||
{
|
||||
$model = new Photo();
|
||||
$model->image = $form_model->getItem('image');
|
||||
$model->entity = $form_model->getItem('entity');
|
||||
$model->entity_id = $form_model->getItem('entity_id');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function update(FormModel $form_model, Photo $photo): false|Photo
|
||||
{
|
||||
$photo->image = $form_model->getItem('image');
|
||||
$photo->entity = $form_model->getItem('entity');
|
||||
$photo->entity_id = $form_model->getItem('entity_id');
|
||||
|
||||
if ($photo->save()){
|
||||
return $photo;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
41
kernel/app_modules/photo/views/index.php
Executable file
41
kernel/app_modules/photo/views/index.php
Executable file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/**
|
||||
* @var int $page_number
|
||||
*/
|
||||
|
||||
use Itguild\EloquentTable\EloquentDataProvider;
|
||||
use Itguild\EloquentTable\ListEloquentTable;
|
||||
use kernel\app_modules\photo\models\Photo;
|
||||
use kernel\widgets\IconBtn\IconBtnCreateWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnDeleteWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnEditWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnViewWidget;
|
||||
|
||||
$table = new ListEloquentTable(new EloquentDataProvider(Photo::class, [
|
||||
'currentPage' => $page_number,
|
||||
'perPage' => 8,
|
||||
'params' => ["class" => "table table-bordered", "border" => "2"],
|
||||
'baseUrl' => "/admin/photo",
|
||||
]));
|
||||
|
||||
//$table->beforePrint(function () {
|
||||
// return IconBtnCreateWidget::create(['url' => '/admin/photo/create'])->run();
|
||||
//});
|
||||
|
||||
$table->columns([
|
||||
'image' => function ($data) {
|
||||
return $data ? "<img src='$data' width='150px'>" : "";
|
||||
}
|
||||
]);
|
||||
|
||||
$table->addAction(function($row) {
|
||||
return IconBtnViewWidget::create(['url' => '/admin/photo/view/' . $row['id']])->run();
|
||||
});
|
||||
//$table->addAction(function($row) {
|
||||
// return IconBtnEditWidget::create(['url' => '/admin/photo/update/' . $row['id']])->run();
|
||||
//});
|
||||
//$table->addAction(function($row) {
|
||||
// return IconBtnDeleteWidget::create(['url' => '/admin/photo/delete/' . $row['id']])->run();
|
||||
//});
|
||||
$table->create();
|
||||
$table->render();
|
34
kernel/app_modules/photo/views/view.php
Executable file
34
kernel/app_modules/photo/views/view.php
Executable file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Database\Eloquent\Collection $photo
|
||||
*/
|
||||
|
||||
use kernel\modules\user\models\User;
|
||||
use Itguild\EloquentTable\ViewEloquentTable;
|
||||
use Itguild\EloquentTable\ViewJsonTableEloquentModel;
|
||||
use kernel\IGTabel\btn\DangerBtn;
|
||||
use kernel\IGTabel\btn\PrimaryBtn;
|
||||
use kernel\IGTabel\btn\SuccessBtn;
|
||||
use kernel\widgets\IconBtn\IconBtnDeleteWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnEditWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnListWidget;
|
||||
|
||||
$table = new ViewEloquentTable(new ViewJsonTableEloquentModel($photo, [
|
||||
'params' => ["class" => "table table-bordered", "border" => "2"],
|
||||
'baseUrl' => "/admin/photo",
|
||||
]));
|
||||
$table->beforePrint(function () use ($photo) {
|
||||
$btn = IconBtnListWidget::create(['url' => '/admin/photo'])->run();
|
||||
// $btn .= IconBtnEditWidget::create(['url' => '/admin/photo/update/' . $photo->id])->run();
|
||||
// $btn .= IconBtnDeleteWidget::create(['url' => '/admin/photo/delete/' . $photo->id])->run();
|
||||
return $btn;
|
||||
});
|
||||
|
||||
$table->rows([
|
||||
'image' => function ($data) {
|
||||
return $data ? "<img src='$data' width='300px'>" : "";
|
||||
}
|
||||
]);
|
||||
$table->create();
|
||||
$table->render();
|
@ -15,32 +15,19 @@ use kernel\modules\menu\service\MenuService;
|
||||
|
||||
class TagEntityController extends AdminController
|
||||
{
|
||||
private TagEntityService $tagEntityService;
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function init(): void
|
||||
{
|
||||
parent::init();
|
||||
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/tag/views/tag_entity/";
|
||||
$this->tagEntityService = new TagEntityService();
|
||||
}
|
||||
|
||||
// public function actionCreate(): void
|
||||
// {
|
||||
// $this->cgView->render("form.php");
|
||||
// }
|
||||
//
|
||||
// #[NoReturn] public function actionAdd(): void
|
||||
// {
|
||||
// $tagForm = new CreateTagForm();
|
||||
// $tagForm->load($_REQUEST);
|
||||
// if ($tagForm->validate()){
|
||||
// $tag = $this->tagEntityService->create($tagForm);
|
||||
// if ($tag){
|
||||
// $this->redirect("/admin/tag/" . $tag->id);
|
||||
// }
|
||||
// }
|
||||
// $this->redirect("/admin/tag/create");
|
||||
// }
|
||||
|
||||
/**
|
||||
* @param $page_number
|
||||
* @return void
|
||||
*/
|
||||
public function actionIndex($page_number = 1): void
|
||||
{
|
||||
$this->cgView->render("index.php", ['page_number' => $page_number]);
|
||||
@ -59,45 +46,4 @@ class TagEntityController extends AdminController
|
||||
$this->cgView->render("view.php", ['tagEntity' => $tagEntity]);
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @throws Exception
|
||||
// */
|
||||
// public function actionUpdate($id): void
|
||||
// {
|
||||
// $model = Tag::find($id);
|
||||
// if (!$model){
|
||||
// throw new Exception(message: "The tag not found");
|
||||
// }
|
||||
//
|
||||
// $this->cgView->render("form.php", ['model' => $model]);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * @throws Exception
|
||||
// */
|
||||
// public function actionEdit($id): void
|
||||
// {
|
||||
// $tag = Tag::find($id);
|
||||
// if (!$tag){
|
||||
// throw new Exception(message: "The tag not found");
|
||||
// }
|
||||
// $tagForm = new CreateTagForm();
|
||||
// $tagService = new TagService();
|
||||
// $tagForm->load($_REQUEST);
|
||||
// if ($tagForm->validate()) {
|
||||
// $tag = $tagService->update($tagForm, $tag);
|
||||
// if ($tag) {
|
||||
// $this->redirect("/admin/tag/" . $tag->id);
|
||||
// }
|
||||
// }
|
||||
// $this->redirect("/admin/tag/update/" . $id);
|
||||
// }
|
||||
//
|
||||
// #[NoReturn] public function actionDelete($id): void
|
||||
// {
|
||||
// $post = Tag::find($id)->first();
|
||||
// $post->delete();
|
||||
// $this->redirect("/admin/tag/");
|
||||
// }
|
||||
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
<?php
|
||||
/**
|
||||
* @var \Illuminate\Database\Eloquent\Collection $menuItem
|
||||
* @var int $page_number
|
||||
*/
|
||||
|
||||
@ -22,7 +21,6 @@ $table = new ListEloquentTable(new EloquentDataProvider(Tag::class, [
|
||||
|
||||
$table->beforePrint(function () {
|
||||
return PrimaryBtn::create("Создать", "/admin/tag/create")->fetch();
|
||||
//return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch();
|
||||
});
|
||||
|
||||
$table->columns([
|
||||
|
Reference in New Issue
Block a user