From ccf2f506f0650d4bb515f0755bef7c6a3d980f84 Mon Sep 17 00:00:00 2001 From: versustunez Date: Fri, 24 Jul 2020 11:08:07 +0200 Subject: [PATCH] - added SEO-URL-Mode - added Language support --- config.base.php | 1 + install/db.sql | 18 ++++++++ lang.php | 3 ++ lang/de.php | 7 +++ public/index.php | 1 + src/Venom/ArgumentHandler.php | 5 +++ src/Venom/Config.php | 9 ++++ src/Venom/Database/DatabaseHandler.php | 2 +- src/Venom/Language.php | 61 ++++++++++++++++++++++++++ src/Venom/Registry.php | 28 +++++++++++- src/Venom/SEO/SeoController.php | 54 +++++++++++++++++++++++ src/Venom/Venom.php | 8 +++- tpl/default/base.php | 11 +++-- 13 files changed, 201 insertions(+), 7 deletions(-) create mode 100644 install/db.sql create mode 100644 lang.php create mode 100644 lang/de.php create mode 100644 src/Venom/Language.php create mode 100644 src/Venom/SEO/SeoController.php diff --git a/config.base.php b/config.base.php index 9a07a54..7e9d883 100644 --- a/config.base.php +++ b/config.base.php @@ -56,5 +56,6 @@ $config->setRender([ $config->setMaintainMode(false); $config->setDevMode(true); $config->setBaseUrl(''); // can changed to something like that: https://example.com !not enter a / after the url! this will break all +$config->setSeoMode(true); //seo mode is to lookup the complete entered url in the database! without query-parameters and load the get parameters from it $config->close(); \ No newline at end of file diff --git a/install/db.sql b/install/db.sql new file mode 100644 index 0000000..5d53092 --- /dev/null +++ b/install/db.sql @@ -0,0 +1,18 @@ +create table if not exists seoData +( + id int(255) auto_increment not null unique primary key, + seo varchar(255) not null, + raw varchar(255) not null, + isActive tinyint(1) default 1 null +) + comment 'seo url mapping'; + +create table if not exists language +( + id int(255) auto_increment not null unique primary key, + language varchar(255) not null, + shortTag varchar(255) not null, + isActive tinyint(1) default 1 null, + isDefault tinyint(1) default 0 null +) + comment 'Language File'; diff --git a/lang.php b/lang.php new file mode 100644 index 0000000..e3972ad --- /dev/null +++ b/lang.php @@ -0,0 +1,3 @@ +getLang()->registerLang('de', [ + 'HEADLINE' => 'VenomCMS', + 'TEST_TRANSLATION' => 'Das ist ein Test :)', +]); \ No newline at end of file diff --git a/public/index.php b/public/index.php index d20bbb8..85b7f07 100755 --- a/public/index.php +++ b/public/index.php @@ -16,6 +16,7 @@ if (!file_exists('../module.inc.php')) { } require_once '../config.inc.php'; require_once '../module.inc.php'; +require_once '../lang.php'; $config = Config::getInstance(); if ($config->isMaintenance()) { diff --git a/src/Venom/ArgumentHandler.php b/src/Venom/ArgumentHandler.php index aaf88ee..48e5534 100644 --- a/src/Venom/ArgumentHandler.php +++ b/src/Venom/ArgumentHandler.php @@ -34,4 +34,9 @@ class ArgumentHandler } return $default; } + + public function setItem(string $key, $item) + { + $this->arguments[$key] = $item; + } } \ No newline at end of file diff --git a/src/Venom/Config.php b/src/Venom/Config.php index 5582f4d..c4f9733 100644 --- a/src/Venom/Config.php +++ b/src/Venom/Config.php @@ -16,6 +16,7 @@ class Config private bool $devMode = false; private bool $isAdmin = false; private string $baseUrl = ''; + private bool $seoMode = false; private function __construct() { @@ -118,6 +119,14 @@ class Config $this->set('baseUrl', $url); } + public function setSeoMode(bool $mode) { + $this->set('seoMode', $mode); + } + + public function getSeoEnabled() { + return $this->seoMode; + } + /** * @return bool */ diff --git a/src/Venom/Database/DatabaseHandler.php b/src/Venom/Database/DatabaseHandler.php index e98046d..2bc3f4b 100644 --- a/src/Venom/Database/DatabaseHandler.php +++ b/src/Venom/Database/DatabaseHandler.php @@ -48,7 +48,7 @@ class DatabaseHandler } } - public function getOne(string $query, array $args): ?array + public function getOne(string $query, array $args): ?DatabaseObject { $data = $this->getAll($query, $args); if (count($data) > 0) { diff --git a/src/Venom/Language.php b/src/Venom/Language.php new file mode 100644 index 0000000..20d446c --- /dev/null +++ b/src/Venom/Language.php @@ -0,0 +1,61 @@ +defaultLang = DatabaseHandler::get()->getOne("select language, shortTag from language WHERE isActive = 1 and isDefault = 1", []); + } + + public function initLang() + { + $lang = ArgumentHandler::get()->getItem("lang", $this->defaultLang->shortTag); + //check if language exists + $data = DatabaseHandler::get()->getOne("select id from language where shortTag = :shortTag", [ + ':shortTag' => $lang + ]); + + if (isset($data->id)) { + $this->language = $lang; + } else { + throw new \RuntimeException("Language \"$lang\" not found"); + } + } + + public function registerLang(string $key, array $values) + { + $this->languages[$key] = $values; + } + + public function getCurrentLang() + { + return $this->language; + } + + public function getTranslations() + { + return $this->languages[$this->language] ?: []; + } + + public function getTranslation($key) + { + if (!isset($this->languages[$this->language])) { + return $key; + } + if (isset($this->languages[$this->language][$key])) { + return $this->languages[$this->language][$key]; + } + return $key; + } +} \ No newline at end of file diff --git a/src/Venom/Registry.php b/src/Venom/Registry.php index 91adef5..06a8a43 100644 --- a/src/Venom/Registry.php +++ b/src/Venom/Registry.php @@ -4,13 +4,39 @@ namespace Venom; +use Venom\SEO\SeoController; + /** * Singleton Class... hold current URL => can * @package Venom */ class Registry { - public function __construct() + private static ?Registry $instance = null; + private SeoController $seo; + private Language $lang; + + private function __construct() { + $this->seo = new SeoController(); + $this->lang = new Language(); + } + + public static function getInstance(): Registry + { + if (self::$instance === null) { + self::$instance = new Registry(); + } + return self::$instance; + } + + public function getSeo(): SeoController + { + return $this->seo; + } + + public function getLang(): Language + { + return $this->lang; } } \ No newline at end of file diff --git a/src/Venom/SEO/SeoController.php b/src/Venom/SEO/SeoController.php new file mode 100644 index 0000000..a8387db --- /dev/null +++ b/src/Venom/SEO/SeoController.php @@ -0,0 +1,54 @@ +shouldUse = Config::getInstance()->getSeoEnabled(); + } + + public function loadSite() + { + if (!$this->shouldUse) { + return; + } + $url = htmlspecialchars(parse_url($_SERVER['REQUEST_URI'])['path']); + $data = DatabaseHandler::get()->getOne("SELECT * FROM seoData WHERE seo = :url", [ + ':url' => $url, + ]); + $this->data = $data; + if($this->data != null) { + parse_str(parse_url($this->data->raw)['query'], $queryItems); + foreach($queryItems as $key => $item) { + ArgumentHandler::get()->setItem($key, $item); + } + } + } + + public function addSite() + { + + } + + public function deleteSite() + { + + } + + public function getData() + { + return $this->data; + } +} \ No newline at end of file diff --git a/src/Venom/Venom.php b/src/Venom/Venom.php index b368ce2..d105968 100644 --- a/src/Venom/Venom.php +++ b/src/Venom/Venom.php @@ -24,14 +24,18 @@ class Venom { // we need to load the current controller and the current start template. // after this we can start the renderer + $registry = Registry::getInstance(); + $registry->getSeo()->loadSite(); + $registry->getLang()->initLang(); $this->renderer->init($this->findController()); $this->renderer->render(); } private function findController(): ?RenderController { - if (isset($_GET['cl'], $this->controllers[$_GET['cl']])) { - return $this->controllers[$_GET['cl']]; + $cl = ArgumentHandler::get()->getItem('cl'); + if ($cl != null && isset($this->controllers[$cl])) { + return $this->controllers[$cl]; } return null; } diff --git a/tpl/default/base.php b/tpl/default/base.php index 3354cf6..712d120 100644 --- a/tpl/default/base.php +++ b/tpl/default/base.php @@ -1,8 +1,10 @@ getLang(); ?> @@ -18,9 +20,12 @@ use Venom\Views\Asset;
- VenomCMS + getTranslation("HEADLINE");?>
-templateData ?> +getTranslation("TEST_TRANSLATION"); + echo $this->templateData; +?> renderJS(); ?>