101 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			101 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace kernel\app_modules\card\controllers;
 | |
| 
 | |
| use Exception;
 | |
| use JetBrains\PhpStorm\NoReturn;
 | |
| use kernel\AdminController;
 | |
| use kernel\app_modules\card\models\forms\CreateCardForm;
 | |
| use kernel\app_modules\card\models\Card;
 | |
| use kernel\app_modules\card\services\CardService;
 | |
| use kernel\Flash;
 | |
| use kernel\helpers\Debug;
 | |
| 
 | |
| class CardController extends AdminController
 | |
| {
 | |
|     private CardService $cardService;
 | |
|     protected function init(): void
 | |
|     {
 | |
|         parent::init();
 | |
|         $this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/card/views/";
 | |
|         $this->cardService = new CardService();
 | |
|     }
 | |
| 
 | |
|     public function actionCreate(): void
 | |
|     {
 | |
|         $this->cgView->render("form.php");
 | |
|     }
 | |
| 
 | |
|     #[NoReturn] public function actionAdd(): void
 | |
|     {
 | |
|         $cardForm = new CreateCardForm();
 | |
|         $cardForm->load($_REQUEST);
 | |
|         if ($cardForm->validate()){
 | |
|             $card = $this->cardService->create($cardForm);
 | |
|             if ($card){
 | |
|                 $this->redirect("/admin/card/" . $card->id);
 | |
|             }
 | |
|         }
 | |
|         Flash::setMessage("error", $cardForm->getErrorsStr());
 | |
|         $this->redirect("/admin/card/create");
 | |
|     }
 | |
| 
 | |
|     public function actionIndex($page_number = 1): void
 | |
|     {
 | |
|         $this->cgView->render("index.php", ['page_number' => $page_number]);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @throws Exception
 | |
|      */
 | |
|     public function actionView($id): void
 | |
|     {
 | |
|         $card = Card::find($id);
 | |
| 
 | |
|         if (!$card){
 | |
|             throw new Exception(message: "The card not found");
 | |
|         }
 | |
|         $this->cgView->render("view.php", ['card' => $card]);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @throws Exception
 | |
|      */
 | |
|     public function actionUpdate($id): void
 | |
|     {
 | |
|         $model = Card::find($id);
 | |
|         if (!$model){
 | |
|             throw new Exception(message: "The card not found");
 | |
|         }
 | |
| 
 | |
|         $this->cgView->render("form.php", ['model' => $model]);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @throws Exception
 | |
|      */
 | |
|     public function actionEdit($id): void
 | |
|     {
 | |
|         $card = Card::find($id);
 | |
|         if (!$card){
 | |
|             throw new Exception(message: "The card not found");
 | |
|         }
 | |
|         $cardForm = new CreateCardForm();
 | |
|         $cardService = new CardService();
 | |
|         $cardForm->load($_REQUEST);
 | |
|         if ($cardForm->validate()) {
 | |
|             $card = $cardService->update($cardForm, $card);
 | |
|             if ($card) {
 | |
|                 $this->redirect("/admin/card/" . $card->id);
 | |
|             }
 | |
|         }
 | |
|         $this->redirect("/admin/card/update/" . $id);
 | |
|     }
 | |
| 
 | |
|     #[NoReturn] public function actionDelete($id): void
 | |
|     {
 | |
|         $card = Card::find($id)->first();
 | |
|         $card->delete();
 | |
|         $this->redirect("/admin/card/");
 | |
|     }
 | |
| } |