upload files
This commit is contained in:
parent
09d8427293
commit
fa8fb69f19
@ -4,6 +4,14 @@ namespace app\models\forms;
|
||||
|
||||
use kernel\FormModel;
|
||||
|
||||
/**
|
||||
* @property $parent_id
|
||||
* @property $icon_file
|
||||
* @property $icon_font
|
||||
* @property $label
|
||||
* @property $url
|
||||
* @property $status
|
||||
*/
|
||||
class CreateMenuForm extends FormModel
|
||||
{
|
||||
public function rules(): array
|
||||
|
@ -6,16 +6,21 @@ use app\helpers\Debug;
|
||||
|
||||
class FileUpload
|
||||
{
|
||||
public string $fileTmpPath;
|
||||
public string $fileName;
|
||||
public string $fileSize;
|
||||
public string $fileType;
|
||||
public array $fileNameCmps;
|
||||
public string $fileExtension;
|
||||
protected string $fileTmpPath;
|
||||
|
||||
protected string $destPath;
|
||||
|
||||
protected string $uploadDir;
|
||||
protected string $uploadFile;
|
||||
protected string $fileName;
|
||||
protected string $fileSize;
|
||||
protected string $fileType;
|
||||
protected array $fileNameCmps;
|
||||
protected string $fileExtension;
|
||||
// public string $newFileName;
|
||||
public array $allowedFileExtensions;
|
||||
|
||||
public function __construct(array $file, array $extensions)
|
||||
public function __construct(array $file, array $extensions, $hashing = true)
|
||||
{
|
||||
$this->fileTmpPath = $file['tmp_name'];
|
||||
$this->fileName = $file['name'];
|
||||
@ -39,17 +44,36 @@ class FileUpload
|
||||
// return false;
|
||||
// }
|
||||
|
||||
public function upload()
|
||||
public function upload($uploadDir = "/resources/upload/"): bool
|
||||
{
|
||||
$newFileName = md5(time() . $this->fileName) . '.' . $this->fileExtension;
|
||||
if (in_array($this->fileExtension, $this->allowedFileExtensions)) {
|
||||
mkdir('./resources/upload/' . mb_substr($newFileName, 0, 2) . '/' . mb_substr($newFileName, 2, 2) . '/', 0777, true);
|
||||
$uploadFileDir = './resources/upload/' . mb_substr($newFileName, 0, 2) . '/' . mb_substr($newFileName, 2, 2) . '/';
|
||||
$dest_path = $uploadFileDir . $newFileName;
|
||||
move_uploaded_file($this->fileTmpPath, $dest_path);
|
||||
$this->uploadDir = $uploadDir . mb_substr($newFileName, 0, 2) . '/' . mb_substr($newFileName, 2, 2) . '/';
|
||||
mkdir(ROOT_DIR . $this->uploadDir, 0777, true);
|
||||
$uploadFileDir = ROOT_DIR . $this->uploadDir;
|
||||
$this->destPath = $uploadFileDir . $newFileName;
|
||||
$this->uploadFile = $this->uploadDir . $newFileName;
|
||||
move_uploaded_file($this->fileTmpPath, $this->destPath);
|
||||
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function getFullUploadedPath(): string
|
||||
{
|
||||
return $this->destPath;
|
||||
}
|
||||
|
||||
public function getUploadDir(): string
|
||||
{
|
||||
return $this->uploadDir;
|
||||
}
|
||||
|
||||
public function getUploadFile(): string
|
||||
{
|
||||
return $this->uploadFile;
|
||||
}
|
||||
|
||||
}
|
@ -36,6 +36,11 @@ class FormModel
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
public function setItem(string $name, string|int $value): void
|
||||
{
|
||||
$this->data[$name] = $value;
|
||||
}
|
||||
|
||||
public function getItem(string $name)
|
||||
{
|
||||
if (isset($this->data[$name])){
|
||||
|
@ -33,20 +33,22 @@ class MenuController extends AdminController
|
||||
|
||||
#[NoReturn] public function actionAdd(): void
|
||||
{
|
||||
$menuForm = new CreateMenuForm();
|
||||
$menuForm->load($_REQUEST);
|
||||
|
||||
if (isset($_FILES['icon_file']) && $_FILES['icon_file']['error'] === UPLOAD_ERR_OK) {
|
||||
$file = new FileUpload($_FILES['icon_file'], ['jpg', 'jpeg', 'png']);
|
||||
$file->upload();
|
||||
$menuForm->setItem('icon_file', $file->getUploadFile());
|
||||
}
|
||||
|
||||
$menuForm = new CreateMenuForm();
|
||||
$menuForm->load($_REQUEST);
|
||||
if ($menuForm->validate()){
|
||||
$menuItem = $this->menuService->create($menuForm);
|
||||
if ($menuItem){
|
||||
$this->redirect("/admin/settings/menu/" . $menuItem->id);
|
||||
$this->redirect("/admin/settings/menu/" . $menuItem->id, code: 302);
|
||||
}
|
||||
}
|
||||
$this->redirect("/admin/settings/menu/create");
|
||||
$this->redirect("/admin/settings/menu/create", code: 302);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -96,19 +98,23 @@ class MenuController extends AdminController
|
||||
if (!$menuItem){
|
||||
throw new Exception(message: "The menu item not found");
|
||||
}
|
||||
|
||||
$menuForm = new CreateMenuForm();
|
||||
$menuForm->load($_REQUEST);
|
||||
|
||||
if (isset($_FILES['icon_file']) && $_FILES['icon_file']['error'] === UPLOAD_ERR_OK) {
|
||||
$file = new FileUpload($_FILES['icon_file'], ['jpg', 'jpeg', 'png']);
|
||||
$file->upload();
|
||||
$menuForm->setItem('icon_file', $file->getUploadFile());
|
||||
}
|
||||
$menuForm = new CreateMenuForm();
|
||||
$menuForm->load($_REQUEST);
|
||||
|
||||
if ($menuForm->validate()){
|
||||
$menuItem = $this->menuService->update($menuForm, $menuItem);
|
||||
if ($menuItem){
|
||||
$this->redirect("/admin/settings/menu/" . $menuItem->id);
|
||||
$this->redirect("/admin/settings/menu/" . $menuItem->id, code: 302);
|
||||
}
|
||||
}
|
||||
$this->redirect("/admin/settings/menu/update/" . $id);
|
||||
$this->redirect("/admin/settings/menu/update/" . $id, code: 302);
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionDelete($id): void
|
||||
|
@ -17,6 +17,7 @@ class PostController extends AdminController
|
||||
|
||||
protected function init(): void
|
||||
{
|
||||
parent::init();
|
||||
$this->cgView->viewPath = KERNEL_MODULES_DIR . "/post/views/";
|
||||
$this->postService = new PostService();
|
||||
}
|
||||
|
20
kernel/modules/user/routs/user.php
Normal file
20
kernel/modules/user/routs/user.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
use app\controllers\MainController;
|
||||
use kernel\App;
|
||||
use kernel\modules\admin_themes\controllers\AdminThemeController;
|
||||
use Phroute\Phroute\RouteCollector;
|
||||
|
||||
|
||||
App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){
|
||||
App::$collector->group(["prefix" => "user"], callback: function (RouteCollector $router){
|
||||
App::$collector->get('/', [\kernel\modules\user\controllers\UserController::class, 'actionIndex']);
|
||||
App::$collector->get('/page/{page_number}', [\kernel\modules\user\controllers\UserController::class, 'actionIndex']);
|
||||
App::$collector->get('/create', [\kernel\modules\user\controllers\UserController::class, 'actionCreate']);
|
||||
App::$collector->post("/", [\kernel\modules\user\controllers\UserController::class, 'actionAdd']);
|
||||
App::$collector->get('/{id}', [\kernel\modules\user\controllers\UserController::class, 'actionView']);
|
||||
App::$collector->any('/update/{id}', [\kernel\modules\user\controllers\UserController::class, 'actionUpdate']);
|
||||
App::$collector->any("/edit/{id}", [\kernel\modules\user\controllers\UserController::class, 'actionEdit']);
|
||||
App::$collector->get('/delete/{id}', [\kernel\modules\user\controllers\UserController::class, 'actionDelete']);
|
||||
});
|
||||
});
|
@ -7,16 +7,6 @@ use Phroute\Phroute\RouteCollector;
|
||||
|
||||
|
||||
App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){
|
||||
App::$collector->group(["prefix" => "user"], callback: function (RouteCollector $router){
|
||||
App::$collector->get('/', [\kernel\modules\user\controllers\UserController::class, 'actionIndex']);
|
||||
App::$collector->get('/page/{page_number}', [\kernel\modules\user\controllers\UserController::class, 'actionIndex']);
|
||||
App::$collector->get('/create', [\kernel\modules\user\controllers\UserController::class, 'actionCreate']);
|
||||
App::$collector->post("/", [\kernel\modules\user\controllers\UserController::class, 'actionAdd']);
|
||||
App::$collector->get('/{id}', [\kernel\modules\user\controllers\UserController::class, 'actionView']);
|
||||
App::$collector->any('/update/{id}', [\kernel\modules\user\controllers\UserController::class, 'actionUpdate']);
|
||||
App::$collector->any("/edit/{id}", [\kernel\modules\user\controllers\UserController::class, 'actionEdit']);
|
||||
App::$collector->get('/delete/{id}', [\kernel\modules\user\controllers\UserController::class, 'actionDelete']);
|
||||
});
|
||||
App::$collector->group(["prefix" => "post"], function (RouteCollector $router){
|
||||
App::$collector->get('/', [\kernel\modules\post\controllers\PostController::class, 'actionIndex']);
|
||||
App::$collector->get('/page/{page_number}', [\kernel\modules\post\controllers\PostController::class, 'actionIndex']);
|
||||
|
Loading…
Reference in New Issue
Block a user