diff --git a/src/controller/AbstractController.php b/src/controller/AbstractController.php index f2de7fe..57d9af5 100644 --- a/src/controller/AbstractController.php +++ b/src/controller/AbstractController.php @@ -27,7 +27,14 @@ abstract class AbstractController } protected function render(string $template,array $variables=[]):void{ - echo $this->core->getTwig()->render($template,$variables); + echo $this->core->getTwig()->render($template,$this->addUser($variables)); + } + + private function addUser(array $variables):array{ + if(array_key_exists('user', $variables)){ + throw new \Exception('Key user isn\'t allowed!'); + } + $variables['user'] = $this->core->getUser(); + return $variables; } } - diff --git a/src/controller/AbstractDefaultController.php b/src/controller/AbstractDefaultController.php new file mode 100644 index 0000000..a1e5c2d --- /dev/null +++ b/src/controller/AbstractDefaultController.php @@ -0,0 +1,67 @@ +addMenuItems($variables)); + } + + private function addMenuItems(array $variables): array + { + if (array_key_exists('menu_items', $variables)) { + throw new \Exception('You aren\'t allowed to define this key!'); + } + $variables['menu_items'] = $this->getMenuItems(); + return $variables; + } + + private function getMenuItems(): array + { + return array_merge([ + new Link([], 'home'), + new Link([ + Router::CONTROLLER => 'product', + Router::ACTION => 'list' + ], 'product list'), + new Link([ + Router::CONTROLLER => 'order', + Router::ACTION => 'basket' + ], 'basket') + ], $this->getUserMenuItems()); + } + + private function getUserMenuItems(): array + { + if ($this->core->getUser()) { + return [ + new Link([ + Router::CONTROLLER => 'user', + Router::ACTION => 'logout' + ], 'logout') + ]; + } + return [ + new Link([ + Router::CONTROLLER => 'user', + Router::ACTION => 'login' + ], 'login'), + new Link([ + Router::CONTROLLER => 'user', + Router::ACTION => 'register' + ], 'register') + ]; + } +} + diff --git a/src/controller/product/Product.php b/src/controller/product/Product.php index 7493c9f..8410e71 100644 --- a/src/controller/product/Product.php +++ b/src/controller/product/Product.php @@ -1,17 +1,17 @@ productRepository = new ProductRepository($this->core->getDatabase()); + $this->productRepository = new ProductRepository($this->core); } public function list(?string $color = null): void diff --git a/src/controller/standart/Standart.php b/src/controller/standart/Standart.php index 65b3ca6..5e2dc63 100644 --- a/src/controller/standart/Standart.php +++ b/src/controller/standart/Standart.php @@ -1,43 +1,17 @@ render('standart/homepage.html.twig',['options'=>$this->getAllOptions()]); - } - - private function getAllOptions(){ - return [ - new Link([ - 'controller'=>'user', - 'action'=>'login' - ],'login'), - new Link([ - 'controller'=>'user', - 'action'=>'logout' - ],'logout'), - new Link([ - 'controller'=>'user', - 'action'=>'register' - ],'register'), - new Link([ - 'controller'=>'product', - 'action'=>'list' - ],'product list'), - new Link([ - 'controller'=>'order', - 'action'=>'basket' - ],'basket'), - ]; + $this->render('standart/homepage.html.twig'); } } diff --git a/src/repository/AbstractRepository.php b/src/repository/AbstractRepository.php index 4deca72..b21f0e0 100644 --- a/src/repository/AbstractRepository.php +++ b/src/repository/AbstractRepository.php @@ -1,8 +1,11 @@ database = $database; + /** + * @var UserInterface + */ + protected $user; + + public function __construct(CoreInterface $core){ + $this->database = $core->getDatabase(); + $this->user = $core->getUser(); } } diff --git a/src/router/Link.php b/src/router/Link.php deleted file mode 100644 index 4c3c544..0000000 --- a/src/router/Link.php +++ /dev/null @@ -1,52 +0,0 @@ -setParameters($parameters); - $this->setName($name); - } - - public function setParameters(array $parameters):void{ - $this->parameters = $parameters; - } - - public function setName(string $name):void{ - $this->name = $name; - } - - public function getName():string{ - return $this->name; - } - - public function getUrl():string{ - return "index.php".$this->getParameters(); - } - - private function getParameters():string{ - $parameters = '?'; - foreach ($this->parameters as $key=>$value){ - $parameters .= $key.'='.$value.'&'; - } - return $parameters; - } -} - diff --git a/src/router/Router.php b/src/router/Router.php index a4cb004..6ea62f8 100644 --- a/src/router/Router.php +++ b/src/router/Router.php @@ -14,7 +14,10 @@ use controller\order\Order; */ final class Router implements RouterInterface { - + const CONTROLLER='controller'; + + const ACTION = 'action'; + /** * * @var CoreInterface @@ -38,10 +41,10 @@ final class Router implements RouterInterface public function route() { if ($this->get) { - switch ($this->get['controller']) { + switch ($this->get[self::CONTROLLER]) { case 'user': $userController = new User(); - switch ($this->get['action']) { + switch ($this->get[self::ACTION]) { case 'login': return $userController->login(); case 'logout': @@ -51,13 +54,13 @@ final class Router implements RouterInterface } case 'product': $productController = new Product($this->core); - switch ($this->get['action']) { + switch ($this->get[self::ACTION]) { case 'list': return $productController->list(($this->get['color'])?$this->get['color']:null); } case 'order': $orderController = new Order($this->core); - switch ($this->get['action']){ + switch ($this->get[self::ACTION]){ case 'store': return $orderController->store(); case 'basket': diff --git a/src/router/Button.php b/src/router/link/Button.php similarity index 91% rename from src/router/Button.php rename to src/router/link/Button.php index 54cf207..f9e7e58 100644 --- a/src/router/Button.php +++ b/src/router/link/Button.php @@ -1,5 +1,5 @@ setParameters($parameters); + $this->setName($name); + } + + public function setParameters(array $parameters): void + { + $this->parameters = $parameters; + } + + public function setName(string $name): void + { + $this->name = $name; + } + + public function getName(): string + { + return $this->name; + } + + public function getUrl(): string + { + return "index.php" . $this->getParameters(); + } + + private function getParameters(): string + { + $parameters = '?'; + foreach ($this->parameters as $key => $value) { + $parameters .= $key . '=' . $value . '&'; + } + return $parameters; + } + + public function isActive(): bool + { + foreach ($this->parameters as $key => $parameter) { + if (! array_key_exists($key, $_GET) || (array_key_exists($key, $_GET) && $_GET[$key]!==$parameter)) { + return false; + } + } + return true; + } +} + diff --git a/src/router/link/LinkInterface.php b/src/router/link/LinkInterface.php new file mode 100644 index 0000000..eec6dee --- /dev/null +++ b/src/router/link/LinkInterface.php @@ -0,0 +1,17 @@ + - {% block content %} {% endblock %} diff --git a/src/template/frames/structure/navbar.html.twig b/src/template/frames/structure/navbar.html.twig new file mode 100644 index 0000000..b0687c2 --- /dev/null +++ b/src/template/frames/structure/navbar.html.twig @@ -0,0 +1,32 @@ + \ No newline at end of file diff --git a/src/template/product/list.html.twig b/src/template/product/list.html.twig index d820088..349c65c 100644 --- a/src/template/product/list.html.twig +++ b/src/template/product/list.html.twig @@ -24,7 +24,7 @@ price: Add to basket diff --git a/src/template/standart/homepage.html.twig b/src/template/standart/homepage.html.twig index fd5ce3c..9268337 100644 --- a/src/template/standart/homepage.html.twig +++ b/src/template/standart/homepage.html.twig @@ -4,7 +4,7 @@

Welcome to the online shop!

You have the following options: