From a739a66ae426af92205c379ac82579c26785bbb8 Mon Sep 17 00:00:00 2001 From: Cerbu Andrei Mihnea <100320074+andrei-mihnea-cerbu@users.noreply.github.com> Date: Wed, 10 May 2023 03:28:30 +0300 Subject: [PATCH] gata basic ul --- knowyourfood/controllers/AuthController.php | 1 - knowyourfood/controllers/siteController.php | 45 ++++++++++---- knowyourfood/core/Application.php | 6 +- knowyourfood/core/Controller.php | 2 +- knowyourfood/core/Router.php | 66 +++------------------ knowyourfood/core/Session.php | 10 ++-- knowyourfood/core/View.php | 45 ++++++++++++++ knowyourfood/core/form/BaseField.php | 35 +++++++++++ knowyourfood/core/form/Form.php | 4 +- knowyourfood/core/form/InputField.php | 36 +++++++++++ knowyourfood/core/form/TextareaField.php | 13 ++++ knowyourfood/index.php | 4 +- knowyourfood/models/ContactForm.php | 28 +++++++++ knowyourfood/views/contact.php | 27 ++++----- knowyourfood/views/home.php | 4 ++ knowyourfood/views/layouts/main.php | 12 +++- knowyourfood/views/login.php | 9 ++- knowyourfood/views/profile.php | 6 ++ knowyourfood/views/register.php | 12 ++-- 19 files changed, 255 insertions(+), 110 deletions(-) create mode 100644 knowyourfood/core/View.php create mode 100644 knowyourfood/core/form/BaseField.php create mode 100644 knowyourfood/core/form/InputField.php create mode 100644 knowyourfood/core/form/TextareaField.php create mode 100644 knowyourfood/models/ContactForm.php diff --git a/knowyourfood/controllers/AuthController.php b/knowyourfood/controllers/AuthController.php index 0277e13..052974c 100644 --- a/knowyourfood/controllers/AuthController.php +++ b/knowyourfood/controllers/AuthController.php @@ -43,7 +43,6 @@ class AuthController extends Controller{ $user->loadData($request->getBody()); if($user->validate() && $user->save()){ - Application::$app->session->setFlash('success', 'Thanks for registering'); $response->redirect('home'); exit; } diff --git a/knowyourfood/controllers/siteController.php b/knowyourfood/controllers/siteController.php index 39c8554..bc8dc36 100644 --- a/knowyourfood/controllers/siteController.php +++ b/knowyourfood/controllers/siteController.php @@ -1,20 +1,41 @@ getBody(); - return 'handling requested data'; +class SiteController extends Controller +{ + public function contact(Request $request, Response $response) + { + $contact = new ContactForm(); + + if ($request->isPost()) { + $contact->loadData($request->getBody()); + + if ($contact->validate() && $contact->send()) { + Application::$app->session->setFlash('success', 'The form has been succesfully sent!'); + var_dump($_SESSION['flash_messages']); + $response->redirect('contact'); + exit; + } + + return $this->render('contact', [ + 'model' => $contact + ]); + } + + return $this->render('contact', [ + 'model' => $contact + ]); } - public function contact(){ - return $this->render('contact'); - } - - public function home(){ + public function home() + { $params = [ 'name' => "TheCodeholic" ]; @@ -22,11 +43,13 @@ class SiteController extends Controller{ return $this->render('home', $params); } - public function profile(){ + public function profile() + { return $this->render('profile'); } - public function _404(){ + public function _404() + { return $this->render('_404'); } } \ No newline at end of file diff --git a/knowyourfood/core/Application.php b/knowyourfood/core/Application.php index 7206480..9f414fd 100644 --- a/knowyourfood/core/Application.php +++ b/knowyourfood/core/Application.php @@ -18,6 +18,7 @@ class Application public Database $db; public Session $session; public ?DbModel $user; + public View $view; public function __construct($rootPath, array $config) { @@ -28,6 +29,7 @@ class Application $this->request = new Request(); $this->response = new Response(); $this->router = new Router($this->request, $this->response); + $this->view = new View(); $this->db = new Database($config['db']); $this->session = new Session(); @@ -78,12 +80,12 @@ class Application echo $this->router->resolve(); }catch(ForbiddenException $e){ $this->response->setStatusCode(403); - echo $this->router->renderView('_error', [ + echo $this->view->renderView('_error', [ 'exception' => $e ]); }catch(NotFoundException $e){ $this->response->setStatusCode(404); - echo $this->router->renderView('_error', [ + echo $this->view->renderView('_error', [ 'exception' => $e ]); } diff --git a/knowyourfood/core/Controller.php b/knowyourfood/core/Controller.php index fcbb677..39577ca 100644 --- a/knowyourfood/core/Controller.php +++ b/knowyourfood/core/Controller.php @@ -9,7 +9,7 @@ class Controller{ protected array $middlewares = []; public function render($view, $params = []){ - return Application::$app->router->renderView($view, $params); + return Application::$app->view->renderView($view, $params); } public function setLayout($layout){ diff --git a/knowyourfood/core/Router.php b/knowyourfood/core/Router.php index cb7cadf..403471e 100644 --- a/knowyourfood/core/Router.php +++ b/knowyourfood/core/Router.php @@ -1,6 +1,7 @@ routes['post'][$path] = $callback; } - /* - in aceasta functie se identifica path ul catre fisier, metoda (GET/POST) si se identifica un "routes" callback ul (in general controller ul) pe care trebuie sa l returneze - astfel, in call_user_function, se ofera clasa de tip constructor si request ul de la user ca dupa controller ul sa se ocupe de restul - */ - public function resolve() { $path = $this->request->getPath(); @@ -40,61 +36,15 @@ class Router throw new NotFoundException(); } - if (is_string($callback)) { - return $this->renderView($callback); + $controller = new $callback[0](); + Application::$app->controller = $controller; + $controller->action = $callback[1]; + $callback[0] = $controller; + + foreach ($controller->getMiddlewares() as $middleware) { + $middleware->execute(); } - if (is_array($callback)) { - $controller = new $callback[0](); - Application::$app->controller = $controller; - $controller->action = $callback[1]; - $callback[0] = $controller; - - foreach($controller->getMiddlewares() as $middleware){ - $middleware->execute(); - } - } - - return call_user_func($callback, $this->request, $this->response); } - - //layout ne va ajuta sa identificam ce fel de layout trebuie sa aiba pagina pe care o trimitem (elemente extra in afara de continut: navbar, elemente din baza de date etc) - protected function layoutContent() - { - $layout = Application::$app->layout; - if (Application::$app->controller) { - $layout = Application::$app->controller->layout; - } - - ob_start(); - include_once Application::$ROOT_DIR . "/views/layouts/$layout.php"; - return ob_get_clean(); - } - - - public function renderView($view, $params = []) - { - $layoutContent = $this->layoutContent(); - $viewContent = $this->renderOnlyView($view, $params); - - return str_replace('{{content}}', $viewContent, $layoutContent); - } - - public function renderContent($viewContent) - { - $layoutContent = $this->layoutContent(); - return str_replace('{{content}}', $viewContent, $layoutContent); - } - - protected function renderOnlyView($view, $params) - { - foreach ($params as $key => $value) { - $$key = $value; - } - - ob_start(); - include_once Application::$ROOT_DIR . "/views/$view.php"; - return ob_get_clean(); - } } \ No newline at end of file diff --git a/knowyourfood/core/Session.php b/knowyourfood/core/Session.php index a4cf901..84c7679 100644 --- a/knowyourfood/core/Session.php +++ b/knowyourfood/core/Session.php @@ -4,14 +4,12 @@ namespace app\core; class Session{ protected const FLASH_KEY = 'flash_messages'; + public function __construct(){ session_start(); - $flashMessages = $_SESSION[self::FLASH_KEY] ?? []; - foreach($flashMessages as $key => &$flashMessage){ + foreach($_SESSION[self::FLASH_KEY] as $key => &$flashMessage){ $flashMessage['remove'] = 'true'; } - - $_SESSION[self::FLASH_KEY] = $flashMessages; } public function setFlash($key, $message){ @@ -30,7 +28,7 @@ class Session{ } public function get($key){ - return $_SESSION[$key] ?? false; + return $_SESSION[$key]; } public function remove($key){ @@ -40,7 +38,7 @@ class Session{ public function __destruct(){ $flashMessages = $_SESSION[self::FLASH_KEY] ?? []; foreach($flashMessages as $key => &$flashMessage){ - if($flashMessage['remove']){ + if($flashMessage['remove'] == 'true'){ unset($flashMessages[$key]); } } diff --git a/knowyourfood/core/View.php b/knowyourfood/core/View.php new file mode 100644 index 0000000..e4583d1 --- /dev/null +++ b/knowyourfood/core/View.php @@ -0,0 +1,45 @@ +layout; + if (Application::$app->controller) { + $layout = Application::$app->controller->layout; + } + + ob_start(); + include_once Application::$ROOT_DIR . "/views/layouts/$layout.php"; + return ob_get_clean(); + } + + + public function renderView($view, $params = []) + { + $viewContent = $this->renderOnlyView($view, $params); + $layoutContent = $this->layoutContent(); + + return str_replace('{{content}}', $viewContent, $layoutContent); + } + + public function renderContent($viewContent) + { + $layoutContent = $this->layoutContent(); + return str_replace('{{content}}', $viewContent, $layoutContent); + } + + protected function renderOnlyView($view, $params) + { + foreach ($params as $key => $value) { + $$key = $value; + } + + ob_start(); + include_once Application::$ROOT_DIR."/views/$view.php"; + return ob_get_clean(); + } +} \ No newline at end of file diff --git a/knowyourfood/core/form/BaseField.php b/knowyourfood/core/form/BaseField.php new file mode 100644 index 0000000..8f544c7 --- /dev/null +++ b/knowyourfood/core/form/BaseField.php @@ -0,0 +1,35 @@ +model = $model; + $this->attribute = $attribute; + } + + abstract public function renderInput(): string; + + public function __toString() + { + return sprintf(' +
+ + %s +
+ %s +
+
+ ', + $this->model->labels()[$this->attribute] ?? $this->attribute, + $this->renderInput(), + $this->model->getFirstError($this->attribute) + ); + } +} \ No newline at end of file diff --git a/knowyourfood/core/form/Form.php b/knowyourfood/core/form/Form.php index 329064f..bbadec2 100644 --- a/knowyourfood/core/form/Form.php +++ b/knowyourfood/core/form/Form.php @@ -13,8 +13,8 @@ class Form{ echo ''; } - public function field(Model $model, $attribute){ - return new Field($model, $attribute); + public function inputField(Model $model, $attribute){ + return new InputField($model, $attribute); } public function button(){ diff --git a/knowyourfood/core/form/InputField.php b/knowyourfood/core/form/InputField.php new file mode 100644 index 0000000..d370de7 --- /dev/null +++ b/knowyourfood/core/form/InputField.php @@ -0,0 +1,36 @@ +type = self::TYPE_TEXT; + parent::__construct($model, $attribute); + } + + public function passwordField() + { + $this->type = self::TYPE_PASSWORD; + return $this; + } + + public function renderInput(): string + { + return sprintf( + '', + $this->type, + $this->attribute, + $this->model->{$this->attribute}, + $this->model->hasError($this->attribute) ? ' is invalid' : '' + ); + } +} \ No newline at end of file diff --git a/knowyourfood/core/form/TextareaField.php b/knowyourfood/core/form/TextareaField.php new file mode 100644 index 0000000..43baa3d --- /dev/null +++ b/knowyourfood/core/form/TextareaField.php @@ -0,0 +1,13 @@ +%s', + $this->attribute, + $this->model->hasError($this->attribute) ? ' is invalid' : '', + $this->model->{$this->attribute} + ); + } +} \ No newline at end of file diff --git a/knowyourfood/index.php b/knowyourfood/index.php index a5335eb..2533f16 100644 --- a/knowyourfood/index.php +++ b/knowyourfood/index.php @@ -23,7 +23,7 @@ $app->router->get('/home', [SiteController::class, 'home']); $app->router->get('/_404', [SiteController::class, '_404']); $app->router->get('/contact', [SiteController::class, 'contact']); -$app->router->post('/contact', [SiteController::class, 'handleContact']); +$app->router->post('/contact', [SiteController::class, 'contact']); $app->router->get('/login', [AuthController::class, 'login']); $app->router->post('/login', [AuthController::class, 'login']); @@ -35,5 +35,7 @@ $app->router->get('/profile', [AuthController::class, 'profile']); $app->router->post('/profile', [AuthController::class, 'profile']); $app->router->get('/logout', [AuthController::class, 'logout']); + +$app->router->get('/login/{id}', [SiteController::class, 'login']); $app->run(); diff --git a/knowyourfood/models/ContactForm.php b/knowyourfood/models/ContactForm.php new file mode 100644 index 0000000..083c247 --- /dev/null +++ b/knowyourfood/models/ContactForm.php @@ -0,0 +1,28 @@ + [self::RULE_REQUIRED], + 'body' => [self::RULE_REQUIRED] + ]; + } + + public function labels(): array{ + return [ + 'subject' => 'Enter your subject', + 'body' => 'Enter the body' + ]; + } + + public function send(){ + return true; + } +} \ No newline at end of file diff --git a/knowyourfood/views/contact.php b/knowyourfood/views/contact.php index 46dd3fa..63fcce0 100644 --- a/knowyourfood/views/contact.php +++ b/knowyourfood/views/contact.php @@ -1,17 +1,12 @@ -

Contact

+title = 'Contact'; -
-
- - -
-
- - -
-
- - -
- -
\ No newline at end of file +$form = Form::begin('', 'post'); +echo $form->inputField($model, 'subject'); +echo new TextareaField($model, 'body'); +echo $form->button(); + +Form::end(); +?> \ No newline at end of file diff --git a/knowyourfood/views/home.php b/knowyourfood/views/home.php index 1a1f5b0..8717747 100644 --- a/knowyourfood/views/home.php +++ b/knowyourfood/views/home.php @@ -1,2 +1,6 @@ +title = 'Home'; +?> +

Home

Welcome to our site!

\ No newline at end of file diff --git a/knowyourfood/views/layouts/main.php b/knowyourfood/views/layouts/main.php index ea1273d..0c031b3 100644 --- a/knowyourfood/views/layouts/main.php +++ b/knowyourfood/views/layouts/main.php @@ -1,10 +1,14 @@ + + - Bootstrap demo + <?php echo $this->title; ?> @@ -42,6 +46,12 @@
+ session->getFlash('success'); + if($flashMessage){ + echo $flashMessage; + } + ?> {{content}}
diff --git a/knowyourfood/views/login.php b/knowyourfood/views/login.php index 9166582..21a886b 100644 --- a/knowyourfood/views/login.php +++ b/knowyourfood/views/login.php @@ -1,16 +1,15 @@ -

Login

- title='Login'; $form = Form::begin('', 'post'); -echo $form->field($model, 'email'); -echo $form->field($model, 'password')->passwordField(); +echo $form->InputField($model, 'email'); +echo $form->inputField($model, 'password')->passwordField(); echo $form->button(); Form::end() ?> +

Login

\ No newline at end of file diff --git a/knowyourfood/views/profile.php b/knowyourfood/views/profile.php index e241fdb..02e4a57 100644 --- a/knowyourfood/views/profile.php +++ b/knowyourfood/views/profile.php @@ -1 +1,7 @@ +title = 'Profile'; + +?> +

Profile

\ No newline at end of file diff --git a/knowyourfood/views/register.php b/knowyourfood/views/register.php index d63fcf7..f2b6088 100644 --- a/knowyourfood/views/register.php +++ b/knowyourfood/views/register.php @@ -1,17 +1,17 @@ -

Create An Account

title = 'Register'; $form = Form::begin('', 'post'); -echo $form->field($model, 'firstname'); -echo $form->field($model, 'lastname'); -echo $form->field($model, 'email'); -echo $form->field($model, 'password')->passwordField(); +echo $form->inputField($model, 'firstname'); +echo $form->inputField($model, 'lastname'); +echo $form->inputField($model, 'email'); +echo $form->inputField($model, 'password')->passwordField(); echo $form->button(); Form::end() ?> +

Create An Account