first
This commit is contained in:
		
							
								
								
									
										109
									
								
								kernel/app_modules/event/controllers/EventController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										109
									
								
								kernel/app_modules/event/controllers/EventController.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,109 @@ | ||||
| <?php | ||||
|  | ||||
| namespace kernel\app_modules\event\controllers; | ||||
|  | ||||
| use Exception; | ||||
| use JetBrains\PhpStorm\NoReturn; | ||||
| use kernel\AdminController; | ||||
| use kernel\app_modules\event\models\forms\CreateEventForm; | ||||
| use kernel\app_modules\event\models\Event; | ||||
| use kernel\app_modules\event\services\EventService; | ||||
| use kernel\EntityRelation; | ||||
| use kernel\helpers\Debug; | ||||
| use kernel\Request; | ||||
|  | ||||
| class EventController extends AdminController | ||||
| { | ||||
|     private EventService $eventService; | ||||
|     protected function init(): void | ||||
|     { | ||||
|         parent::init(); | ||||
|         $this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/event/views/"; | ||||
|         $this->eventService = new EventService(); | ||||
|     } | ||||
|  | ||||
|     public function actionCreate(): void | ||||
|     { | ||||
|         $this->cgView->render("form.php"); | ||||
|     } | ||||
|  | ||||
|     #[NoReturn] public function actionAdd(): void | ||||
|     { | ||||
|         $eventForm = new CreateEventForm(); | ||||
|         $eventForm->load($_REQUEST); | ||||
|         if ($eventForm->validate()){ | ||||
|             $event = $this->eventService->create($eventForm); | ||||
|  | ||||
|             $entityRelation = new EntityRelation(); | ||||
|             $entityRelation->saveEntityRelation(entity: "event", model: $event, request: new Request()); | ||||
|  | ||||
|             if ($event){ | ||||
|                 $this->redirect("/admin/event/view/" . $event->id); | ||||
|             } | ||||
|         } | ||||
|         $this->redirect("/admin/event/create"); | ||||
|     } | ||||
|  | ||||
|     public function actionIndex($page_number = 1): void | ||||
|     { | ||||
|         $this->cgView->render("index.php", ['page_number' => $page_number]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @throws Exception | ||||
|      */ | ||||
|     public function actionView($id): void | ||||
|     { | ||||
|         $event = Event::find($id); | ||||
|  | ||||
|         if (!$event){ | ||||
|             throw new Exception(message: "The event not found"); | ||||
|         } | ||||
|         $this->cgView->render("view.php", ['event' => $event]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @throws Exception | ||||
|      */ | ||||
|     public function actionUpdate($id): void | ||||
|     { | ||||
|         $model = Event::find($id); | ||||
|         if (!$model){ | ||||
|             throw new Exception(message: "The event not found"); | ||||
|         } | ||||
|  | ||||
|         $this->cgView->render("form.php", ['model' => $model]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @throws Exception | ||||
|      */ | ||||
|     public function actionEdit($id): void | ||||
|     { | ||||
|         $event = Event::find($id); | ||||
|         if (!$event){ | ||||
|             throw new Exception(message: "The event not found"); | ||||
|         } | ||||
|         $eventForm = new CreateEventForm(); | ||||
|         $eventService = new EventService(); | ||||
|         $eventForm->load($_REQUEST); | ||||
|         if ($eventForm->validate()) { | ||||
|             $event = $eventService->update($eventForm, $event); | ||||
|  | ||||
|             $entityRelation = new EntityRelation(); | ||||
|             $entityRelation->saveEntityRelation(entity: "event", model: $event, request: new Request()); | ||||
|  | ||||
|             if ($event) { | ||||
|                 $this->redirect("/admin/event/view/" . $event->id); | ||||
|             } | ||||
|         } | ||||
|         $this->redirect("/admin/event/update/" . $id); | ||||
|     } | ||||
|  | ||||
|     #[NoReturn] public function actionDelete($id): void | ||||
|     { | ||||
|         $event = Event::find($id)->first(); | ||||
|         $event->delete(); | ||||
|         $this->redirect("/admin/event/"); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user