From fa8fb69f195c0ccd97ea5e261a76363592e64501 Mon Sep 17 00:00:00 2001 From: Kavalar Date: Tue, 10 Sep 2024 12:55:41 +0300 Subject: [PATCH] upload files --- app/models/forms/CreateMenuForm.php | 8 ++++ kernel/FileUpload.php | 48 ++++++++++++++----- kernel/FormModel.php | 5 ++ .../menu/controllers/MenuController.php | 22 +++++---- .../post/controllers/PostController.php | 1 + kernel/modules/user/routs/user.php | 20 ++++++++ kernel/routs/admin.php | 10 ---- 7 files changed, 84 insertions(+), 30 deletions(-) create mode 100644 kernel/modules/user/routs/user.php diff --git a/app/models/forms/CreateMenuForm.php b/app/models/forms/CreateMenuForm.php index df81fc4..36f1031 100644 --- a/app/models/forms/CreateMenuForm.php +++ b/app/models/forms/CreateMenuForm.php @@ -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 diff --git a/kernel/FileUpload.php b/kernel/FileUpload.php index a33302e..27d2369 100644 --- a/kernel/FileUpload.php +++ b/kernel/FileUpload.php @@ -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; + } + } \ No newline at end of file diff --git a/kernel/FormModel.php b/kernel/FormModel.php index 6954ad2..341c04b 100644 --- a/kernel/FormModel.php +++ b/kernel/FormModel.php @@ -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])){ diff --git a/kernel/modules/menu/controllers/MenuController.php b/kernel/modules/menu/controllers/MenuController.php index 2417014..919ab60 100644 --- a/kernel/modules/menu/controllers/MenuController.php +++ b/kernel/modules/menu/controllers/MenuController.php @@ -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 diff --git a/kernel/modules/post/controllers/PostController.php b/kernel/modules/post/controllers/PostController.php index 10c10c7..0968b35 100644 --- a/kernel/modules/post/controllers/PostController.php +++ b/kernel/modules/post/controllers/PostController.php @@ -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(); } diff --git a/kernel/modules/user/routs/user.php b/kernel/modules/user/routs/user.php new file mode 100644 index 0000000..5f5c74c --- /dev/null +++ b/kernel/modules/user/routs/user.php @@ -0,0 +1,20 @@ +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']); + }); +}); \ No newline at end of file diff --git a/kernel/routs/admin.php b/kernel/routs/admin.php index 9c8c18a..f55996f 100644 --- a/kernel/routs/admin.php +++ b/kernel/routs/admin.php @@ -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']);