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