Commit fe1b91ea authored by M.Nowocyn's avatar M.Nowocyn

Changed class system.

parent e569e98c
<?php
/**
*
* This file is part of IkoBB Forum and belongs to the module <Language>.
* This file is part of IkoBB Forum and belongs to the module <System>.
*
* @copyright (c) IkoBB <https://www.ikobb.de>
* @license GNU General Public License, version 3 (GPL-3.0)
......
......@@ -20,21 +20,72 @@ namespace Iko\Language;
use Iko\Core;
use Iko\Event\Handler;
use Iko\Language;
use Iko\Lib\Multiton\Cache\Mixed;
use Iko\User;
use PDO;
class Key extends Mixed implements iKey
{
const TABLE = Language::TABLE;
const NAME = Language::NAME;
const TABLE = System::TABLE;
const NAME = System::NAME;
protected static $cache = array ();
protected static $cache_exist = array ();
public static function get ($id = 0, $reload = FALSE): Key
public static function get ( $id = 0, $reload = FALSE ): iKey
{
return parent::get($id, $reload);
$class = parent::get(
$id,
$reload
);
if ( $class == NULL ) {
$class = new class( $id ) implements iKey
{
public function __construct ( string $name )
{
$this->key = $name;
}
public function getKey (): string
{
return $this->key;
}
public function getLang ( $lang = "" ): string
{
return "";
}
public function getLangAll (): array
{
return array ();
}
/**
* @param $lang
* @param $value
*
* @return bool
* @permission iko.language.keys.set.lang
*
*/
public function setLang ( $lang, $value )
{
return FALSE;
}
public function setKey ( $name )
{
return FALSE;
}
public function __toString ()
{
return $this->getKey();
}
};
}
return $class;
}
/**
......@@ -63,8 +114,8 @@ class Key extends Mixed implements iKey
* @param $name
*
* @return bool
* @permission iko.Language.keys.delete
* Allows User to delete Language keys
* @permission iko.language.keys.delete
* Allows User to delete System keys
*/
public static function delete ($name)
{
......@@ -89,10 +140,10 @@ class Key extends Mixed implements iKey
* @param string $name
*
* @return bool
* @permission iko.Language.keys.create
* @permission iko.language.keys.create
* Allows User to create new Keys
*/
public static function create (string $name)
public static function create ( string $name ): bool
{
if ($name != "" && !self::exist($name)) {
if ( User::getSession()->hasPermission( "iko.language.keys.create" ) ) {
......@@ -108,9 +159,9 @@ class Key extends Mixed implements iKey
return FALSE;
}
private $key;
private $languages = array ();
private $all = FALSE;
protected $key;
private $languages = array ();
private $all = FALSE;
protected function __construct (string $name)
{
......@@ -119,22 +170,22 @@ class Key extends Mixed implements iKey
}
}
public function getKey ()
public function getKey (): string
{
return $this->key;
}
public function getLang (string $lang = "")
public function getLang ( $lang = NULL ): string
{
if ($lang == "") {
$lang = Language::getInstance()->getCurrent();
$lang = System::getInstance()->getCurrent();
}
if ($lang != "") {
if (!isset($this->languages[ $lang ])) {
if ( $lang !== null ) {
if ( !isset( $this->languages[ (string) $lang ] ) ) {
$this->loadLang($lang);
}
return $this->languages[ $lang ] ?? "";
return $this->languages[ (string) $lang ] ?? "";
}
return "";
......@@ -144,7 +195,7 @@ class Key extends Mixed implements iKey
{
if (!$this->all) {
$this->all = TRUE;
$languages = Language::getInstance()->getLanguages();
$languages = System::getInstance()->getLanguages();
foreach ($languages as $item) {
if (!isset($this->languages[ $item ])) {
$this->all = FALSE;
......@@ -174,13 +225,13 @@ class Key extends Mixed implements iKey
$this->all = TRUE;
}
private function loadLang (string $lang)
private function loadLang ( $lang )
{
if (Language::getInstance()->isSupportedLanguage($lang)) {
if ( System::getInstance()->isSupportedLanguage( $lang ) ) {
$sql = "SELECT " . $lang . " FROM " . self::TABLE . " WHERE " . self::NAME . " = '" . $this->getKey() . "'";
$statement = Core::PDO()->query($sql);
$fetch = $statement->fetch(PDO::FETCH_ASSOC);
$this->languages[ $lang ] = $fetch[ $lang ];
$this->languages[ (string) $lang ] = $fetch[ (string) $lang ];
}
}
......@@ -199,12 +250,12 @@ class Key extends Mixed implements iKey
* @param $value
*
* @return bool
* @permission iko.Language.keys.set.lang
* @permission iko.language.keys.set.lang
*
*/
public function setLang ($lang, $value)
{
if (Language::getInstance()->isSupportedLanguage($lang)) {
if ( System::getInstance()->isSupportedLanguage( $lang ) ) {
if ($value != "" && $value != $this->getLang($lang)) {
if ( User::getSession()->hasPermission( "iko.language.keys.set.lang" ) ) {
if ( Handler::event(
......
<?php
/**
*
* This file is part of IkoBB Forum and belongs to the module <Language>.
*
* @copyright (c) IkoBB <https://www.ikobb.de>
* @license GNU General Public License, version 3 (GPL-3.0)
*
* For full copyright and license information, please see
* the LICENSE file.
*
*/
/**
* Created by PhpStorm.
* User: Marcel
* Date: 11.01.2017
* Time: 23:29
* Date: 29.06.2017
* Time: 23:38
*/
namespace Iko;
namespace Iko\Language;
use iko\language\iLanguage;
use Iko\Lib\Singleton\Instance;
class Language extends Instance implements iLanguage
{
const DEFAULT = "english";
const TABLE = "{prefix}translation";
const NAME = "translation_key";
const FIXED = array (
"german",
"english");
protected static $instance = NULL;
public static function getInstance ($args = NULL): Language
{
return parent::getInstance($args);
}
use Iko\Core;
use Iko\Event\Handler;
use Iko\Lib\Multiton\Cache\Mixed;
use Iko\log;
use Iko\User;
use Iko\User\iUser;
use PDO;
private $current = NULL;
private $languages = array ();
class Language extends Mixed implements iLanguage
{
protected static $cache;
protected static $cache_exist;
private $status;
private $language;
protected function __construct ()
protected function __construct ( string $lang )
{
$lang = NULL;
if (Session::isLoggedIn()) {
$lang = User::getSession()->getLanguage();
if ( self::exist( $lang ) ) {
$statement = Core::PDO()->query(
"SELECT * FROM " . self::TABLE . " WHERE " . self::NAME . " = " . Core::PDO()->quote( $lang )
);
$fetch = $statement->fetch( PDO::FETCH_ASSOC );
if ( $fetch !== FALSE ) {
foreach ( $fetch as $key => $value ) {
$this->{$key} = $value;
}
}
else {
throw new \Exception( "Language does not exist" );
}
}
if ($lang == NULL || $lang == "") {
$config = Config::load("pdo", "Language");
$lang = $config->default_language ?? self::DEFAULT;
else {
throw new \Exception( "" );
}
$this->current = $lang;
}
$sql = "SHOW COLUMNS FROM " . self::TABLE;
$statement = Core::PDO()->query($sql);
$fetch_all = $statement->fetchAll();
foreach ($fetch_all as $item) {
if ($item["Field"] != self::NAME) {
array_push($this->languages, $item["Field"]);
}
}
public function getName (): string
{
return $this->language;
}
public function getCurrent (): string
public function getStatus (): bool
{
return $this->current;
return (bool) $this->status;
}
public function isSupportedLanguage (string $lang): bool
private function set ( $name, $value, $log = TRUE ): bool
{
if (array_search($lang, $this->languages) !== FALSE) {
return TRUE;
$pieces = preg_split(
'/(?=[A-Z])/',
$name
);
if ( count( $pieces ) > 2 ) {
$name = strtolower( $pieces[1] ) . "_" . strtolower( $pieces[2] );
}
else {
return FALSE;
$name = strtolower( $pieces[1] );
}
}
$handler_name = "iko.language.set." . $name;
if ( $handler_name == "name" ) {
$table_name = "language";
}
else {
$table_name = $name;
}
$class_name = $table_name;
if ( User::getSession() instanceof iUser ) {
$old = $this->{$class_name};
public function getLanguages (): array
{
return $this->languages;
}
if ( Handler::event(
$handler_name,
array (
"old" => $old,
"new" => $value,
"language" => $this ),
User::getSession()
)
) {
if ( $value !== NULL && $value != $this->{$class_name} ) {
Core::PDO()->beginTransaction();
$sql = "UPDATE " . self::TABLE . " Set " . $table_name . " = " . Core::PDO()->quote( $value );
private function change (string $func, $name): bool
{
$name = strtolower($name);
if (($func == "add" && !$this->isSupportedLanguage($name)) || ($func == "remove" && $this->isSupportedLanguage($name) && array_search($name,
self::FIXED, TRUE) === FALSE)
) {
if ( User::getSession()->hasPermission( "iko.language." . $func . ".lang" ) ) {
if ($func == "add") {
$sql = "ALTER TABLE " . self::TABLE . " ADD :lang TEXT";
}
else {
$sql = "ALTER TABLE " . self::TABLE . " DROP :lang";
}
$statement = Core::PDO()->prepare($sql);
$statement->bindParam(":lang", $name);
$statement->execute();
if ($statement > 0) {
if ($func == "add") {
array_push($this->languages, $name);
$sql .= " WHERE language = " . $this->getName();
$statement = Core::PDO()->exec( $sql );
if ( $statement > 0 ) {
$this->{$class_name} = $value;
Core::PDO()->commit();
if ( $log ) {
log::add(
"language",
"info",
2,
"Changed " . $class_name . " from " . $old . " to " . $value . "",
array (
"old" => $old,
"new" => $value,
"id" => $this->getName() )
);
}
Handler::eventFinal(
$handler_name,
array ( "language" => $this->getName() ),
User::getSession()->getID()
);
return TRUE;
}
else {
unset($this->languages[ array_search($name, $this->languages, TRUE) ]);
Core::PDO()->rollBack();
}
return TRUE;
}
}
}
......@@ -114,13 +126,24 @@ class Language extends Instance implements iLanguage
return FALSE;
}
public function add (string $name): bool
public function setStatus ( bool $status ): bool
{
return $this->set(
__FUNCTION__,
$status
);
}
public function setName ( string $name ): bool
{
return $this->change(__FUNCTION__, $name);
return $this->set(
__FUNCTION__,
$name
);
}
public function remove (string $name): bool
public function __toString ()
{
return $this->change(__FUNCTION__, $name);
return $this->getName();
}
}
\ No newline at end of file
<?php
/**
*
* This file is part of IkoBB Forum and belongs to the module <System>.
*
* @copyright (c) IkoBB <https://www.ikobb.de>
* @license GNU General Public License, version 3 (GPL-3.0)
*
* For full copyright and license information, please see
* the LICENSE file.
*
*/
/**
* Created by PhpStorm.
* User: Marcel
* Date: 11.01.2017
* Time: 23:29
*/
namespace Iko\Language;
use Iko\Config;
use Iko\Core;
use Iko\Lib\Singleton\Instance;
use Iko\Session;
use Iko\User;
class System extends Instance implements iSystem
{
const DEFAULT = "english";
const TABLE = "{prefix}translation";
const LANG_TABLE = "{prefix}languages";
const LANG_NAME = "language";
const NAME = "translation_key";
const FIXED = array (
"german",
"english" );
protected static $instance = NULL;
public static function getInstance ( $args = NULL ): System
{
return parent::getInstance( $args );
}
private $config = NULL;
private $current = NULL;
private $languages = array ();
protected function __construct ()
{
$lang = NULL;
if ( Session::isLoggedIn() ) {
$lang = User::getSession()->getLanguage();
}
if ( $lang == NULL || $lang == "" ) {
$this->config = Config::load(
"pdo",
"language"
);
$lang = $this->config->default_language ?? self::DEFAULT;
}
$this->current = Language::get( $lang );
$sql = "SHOW COLUMNS FROM " . self::TABLE;
$statement = Core::PDO()->query( $sql );
$fetch_all = $statement->fetchAll();
foreach ( $fetch_all as $item ) {
if ( $item["Field"] != self::NAME ) {
array_push(
$this->languages,
Language::get( $item["Field"] )
);
}
}
}
public function getCurrent (): iLanguage
{
return $this->current;
}
public function isSupportedLanguage ( $lang ): bool
{
if ( !( $lang instanceof iLanguage ) ) {
$lang = Language::get( $lang );
}
if ( array_search(
$lang,
$this->languages
) !== FALSE
) {
return TRUE;
}
else {
return FALSE;
}
}
public function getLanguages (): array
{
return $this->languages;
}
private function change ( string $func, $name ): bool
{
if ( $name instanceof iLanguage ) {
$name = $name->getName();
}
$name = strtolower( $name );
if ( ( $func == "add" && !$this->isSupportedLanguage(
$name
) ) || ( $func == "remove" && $this->isSupportedLanguage( $name ) && array_search(
$name,
self::FIXED,
TRUE
) === FALSE )
) {
if ( User::getSession()->hasPermission( "iko.language." . $func . ".lang" ) ) {
if ( $func == "add" ) {
$sql = "ALTER TABLE " . self::TABLE . " ADD :lang TEXT";
$sql_update = "INSERT INTO " . Language::TABLE . " ( " . Language::NAME . ",status) VALUE(:lang, '" . $this->config->default_status . "')";
}
else {
$sql_update = "DELETE FROM " . Language::TABLE . " WHERE " . Language::NAME . " = :lang";
$sql = "ALTER TABLE " . self::TABLE . " DROP :lang";
}
$statement = Core::PDO()->prepare( $sql );
$statement->bindParam(
":lang",
$name
);
$statement->execute();
if ( $statement > 0 ) {
$statement_update = Core::PDO()->prepare( $sql_update );
$statement_update->bindParam(
":lang",
$name
);
$statement_update->execute();
if ( $statement_update > 0 ) {
if ( $func == "add" ) {
array_push(
$this->languages,
Language::get(
$name,
TRUE
)
);
}
else {
unset(
$this->languages[ array_search(
$name,
$this->languages,
TRUE
) ]
);
}
return TRUE;
}
}
}
}
return FALSE;
}
public function add ( $name ): bool
{
return $this->change(
__FUNCTION__,
$name
);
}
public function remove ( $name ): bool
{
return $this->change(
__FUNCTION__,
$name
);
}
}
\ No newline at end of file
<?php
/**
*
* This file is part of IkoBB Forum and belongs to the module <Language>.
* This file is part of IkoBB Forum and belongs to the module <System>.
*
* @copyright (c) IkoBB <https://www.ikobb.de>
* @license GNU General Public License, version 3 (GPL-3.0)
......@@ -16,9 +16,26 @@
* Date: 11.01.2017
* Time: 23:29
*/
namespace iko\language;
namespace Iko\Language;
interface iKey
{
public function getKey (): string;
public function getLang ( $lang = "" ): string;
public function getLangAll (): array;
/**
* @param $lang
* @param $value
*
* @return bool
* @permission iko.language.keys.set.lang
*
*/
public function setLang ( $lang, $value );
public function setKey ( $name );
}
\ No newline at end of file
<?php
/**
*
* This file is part of IkoBB Forum and belongs to the module <Language>.
*
* @copyright (c) IkoBB <https://www.ikobb.de>
* @license GNU General Public License, version 3 (GPL-3.0)
*
* For full copyright and license information, please see
* the LICENSE file.
*
*/
/**
* Created by PhpStorm.
* User: Marcel
* Date: 11.01.2017
* Time: 23:28
* Date: 29.06.2017
* Time: 23:38
*/
namespace iko\language;
interface iLanguage
{
public function isSupportedLanguage (string $lang): bool;
public function getCurrent (): string;
namespace Iko\Language;
public function getLanguages (): array;
public function add (string $name): bool;
interface iLanguage
{
const TABLE = "{prefix}languages";
const NAME = "language";
const GET = "getName";
public function remove (string $name): bool;
public function getName (): string;
}
\ No newline at end of file
<?php
/**
*
* This file is part of IkoBB Forum and belongs to the module <System>.
*
* @copyright (c) IkoBB <https://www.ikobb.de>
* @license GNU General Public License, version 3 (GPL-3.0)
*
* For full copyright and license information, please see
* the LICENSE file.
*
*/
/**
* Created by PhpStorm.
* User: Marcel
* Date: 11.01.2017
* Time: 23:28
*/
namespace Iko\Language;
interface iSystem
{
public function isSupportedLanguage ( $lang ): bool;
public function getCurrent (): iLanguage;
public function getLanguages (): array;
public function add ( $name ): bool;
public function remove ( $name ): bool;
}
\ No newline at end of file
<?php
/**
*
* This file is part of IkoBB Forum and belongs to the module <Language>.
* This file is part of IkoBB Forum and belongs to the module <System>.
*
* @copyright (c) IkoBB <https://www.ikobb.de>
* @license GNU General Public License, version 3 (GPL-3.0)
......@@ -23,7 +23,7 @@ class Loader extends \Iko\Module\Loader
protected function preCheckPDOTables ()
{
//Add Tables like User and choosen Language assignment and more
//Add Tables like User and choosen System assignment and more
$tables = array (
"translation",
"users",);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment