Skip to content

Commit

Permalink
Merge pull request #16 from peronczyk/dev
Browse files Browse the repository at this point in the history
New feature: language autodetection
  • Loading branch information
peronczyk authored Jul 10, 2017
2 parents 8750871 + 5f9303c commit cd4f173
Show file tree
Hide file tree
Showing 18 changed files with 208 additions and 210 deletions.
12 changes: 4 additions & 8 deletions app/libs/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ class Core {
// Check if script is run as request by AJAX
public static $ajax_loaded = false;

// Stores list of loaded libraries
private $loaded_libs = array();


/**
* Constructor
Expand All @@ -26,7 +23,6 @@ public function __construct() {
error_reporting(E_ERROR | E_WARNING | E_PARSE);

if (!function_exists('session_status') || session_status() == PHP_SESSION_NONE) session_start();
//ob_start();

if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') self::$ajax_loaded = true;
}
Expand All @@ -42,8 +38,6 @@ public function __construct() {
*/

public static function error($msg, $file = null, $line = null, $debug = null) {
$headers_sent = headers_sent();
if ($headers_sent) ob_clean();

// Hide server document root from file path
$document_root = str_replace('/', '\\', $_SERVER['DOCUMENT_ROOT']);
Expand Down Expand Up @@ -86,7 +80,9 @@ public static function error($msg, $file = null, $line = null, $debug = null) {

public function is_dev() {
$default_dev_ip = array('127.0.0.1', '0.0.0.0', '::1');
if ($_SERVER['REMOTE_ADDR'] === \Config::$DEV_IP || in_array($_SERVER['REMOTE_ADDR'], $default_dev_ip)) return true;
if ($_SERVER['REMOTE_ADDR'] === \Config::$DEV_IP || in_array($_SERVER['REMOTE_ADDR'], $default_dev_ip)) {
return true;
}
return false;
}

Expand All @@ -111,7 +107,7 @@ public static function print_arr($arr) {

public static function get_mtime() {
list($usec, $sec) = explode (' ', microtime());
return((float)$usec + (float)$sec);
return (float)$usec + (float)$sec;
}


Expand Down
126 changes: 99 additions & 27 deletions app/libs/language.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,45 @@

class Language {

private $lang_code;
public $translations = array();
private $_db; // Handle to database controller

private $lang_code;
private $lang_list;

private $_router; // Handle to router class
private $_db; // Handle to database class


/**
* Constructor & dependency injection
*
* @param object $router - Router class
* @param object $db - Database handling class
*/

public function __construct($db) {
// Check if variable passed to this class is database controller
if ($db && is_object($db) && is_a($db, __NAMESPACE__ . '\Database')) $this->_db = $db;
else Core::error('Variable passed to class "Language" is not correct "Database" object', __FILE__, __LINE__, debug_backtrace());
public function __construct($router, $db) {
$this->_router = $router;
$this->_db = $db;
}


/**
* SETTER : Active language
* GETTER : List of configured languages
*
* @return boolean - Return false if language was set to default
* @return array
*/

public function set($requested = null) {
if (!empty($requested)) {
public function get_list() {
if (!$this->lang_list) {
$result = $this->_db->query('SELECT * FROM `languages`', true);
$languages = $this->_db->fetch($result);

if (is_array($languages)) {
foreach($languages as $lang) {
if ($lang['code'] == $requested && (bool)$lang['active'] === true) {
$this->lang_code = $lang['code'];
return true;
}
}
}
$this->lang_list = $this->_db->fetch($result);
}

$this->lang_code = \Config::$DEFAULT_LANG;
return false;
return $this->lang_list;
}


/**
* Get active language code
* GETTER : Active language code
*
* @return string{2}
*/
Expand All @@ -64,18 +59,95 @@ public function get() {
}


/**
* Set active language
*
* @return boolean - Returns false if language was set to default
*/

public function set() {

// Check if first chunk of request is valid language definition
$lang_requested = (isset($this->_router->request[0]) && strlen($this->_router->request[0]) == 2);

// Lang detection mechanism
// This code checks if user language was not detected before
// and if language was not requested in url.

if (\Config::$DETECT_LANG && !$_SESSION['lang_detected'] && !$lang_requested) {
$user_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if ($user_lang != \Config::$DEFAULT_LANG && $this->exists($user_lang)) {
$_SESSION['lang_detected'] = $user_lang;
$this->_router->redirect($user_lang, true, true);
}
}

// Check if first request chunk is a existing and active language

if ($lang_requested && $this->exists($this->_router->request[0])) {

// Prevent accessing default language from two different URLs
if ($this->_router->request[0] == \Config::$DEFAULT_LANG) {
$_SESSION['lang_detected'] = \Config::$DEFAULT_LANG;
$this->_router->request_shift();
$this->_router->redirect('', true, true);
}

// Set requested language as active and shift requests
else {
$this->lang_code = $this->_router->request[0];
$this->_router->request_shift();
return true;
}
}

if ($this->exists(\Config::$DEFAULT_LANG)) {
$this->lang_code = \Config::$DEFAULT_LANG;
return false;
}
else {
\Core::error('Configured default page language does not exist in database or it is inactive.', __FILE__, __LINE__, debug_backtrace());
}
}


/**
* Check if provided lang code matches any configured language in database
*
* @return boolean
*/

public function exists($lang_code) {
$lang_list = $this->get_list();
if (count($lang_list) > 0) {
foreach($lang_list as $lang) {
if ($lang['code'] == $lang_code && (bool)$lang['active'] === true) {
return true;
}
}
}
return false;
}


/**
* Load theme translations
*
* @return boolean
*/

public function load_theme_translations() {
if (!$this->lang_code) return false;

$lang_file = \Config::$THEMES_DIR . \Config::$THEME_NAME . '/lang/' . $this->lang_code . '.php';
if (!file_exists($lang_file)) {
Core::error('Theme translations file not found at this location: ' . $lang_file, __FILE__, __LINE__, debug_backtrace());

if (file_exists($lang_file)) {
$this->translations = include $lang_file;
return true;
}
else {
Core::error('Theme translations file not found at location: ' . $lang_file, __FILE__, __LINE__, debug_backtrace());
}
$this->translations = include $lang_file;
}


Expand Down
8 changes: 4 additions & 4 deletions app/libs/mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ private function emails2string($emails) {

public function send($message) {
if (empty($message)) {
throw new Exception('Pusta wiadomość');
throw new \Exception('Pusta wiadomość');
}
if (empty($this->topic)) {
throw new Exception('Nie ustawiono tematu');
throw new \Exception('Nie ustawiono tematu');
}
if (count($this->recipients) < 1) {
throw new Exception('Brak odbiorców');
throw new \Exception('Brak odbiorców');
}

$topic = $this->topic;
Expand Down Expand Up @@ -174,7 +174,7 @@ public function send($message) {

if (@mail($recipients, $topic, $content, $headers)) return true;
else {
throw new Exception('Nie udało się wysłać wiadomości. Spróbuj ponownie lub skontaktuj się z nami telefonicznie.');
throw new \Exception('Nie udało się wysłać wiadomości. Spróbuj ponownie lub skontaktuj się z nami telefonicznie.');
}
}

Expand Down
28 changes: 23 additions & 5 deletions app/libs/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,34 @@ public function get_module_to_load() {

/**
* Request shift
* Moves requests array forward
* Remove first request from array
*/

public function request_shift() {
if (count($this->request) > 1) {
$this->request = array_shift($this->request);
array_shift($this->request);
return $this->request;
}


/**
* Redirect to specified module
*
* @param boolean $add_request
* @param boolean $add_query
*/

public function redirect($path, $add_request = false, $add_query = false) {
$redirect_url = $this->site_path . '/' . $path;

if ($add_request && count($this->request) > 0) {
if (substr($redirect_url, -1) != '/') $redirect_url .= '/';
$redirect_url .= implode('/', $this->request);
}
if ($add_query && !empty($_SERVER['QUERY_STRING'])) {
$redirect_url .= '?' . $_SERVER['QUERY_STRING'];
}
else $this->request = null;

return $this->request;
header('location: ' . $redirect_url);
}


Expand Down
4 changes: 2 additions & 2 deletions app/modules/admin/admin-history.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@

$date = explode(' ', $val);
switch($template_fields[$key]['category']) {
case 'text': $category = 'Zawartość'; break;
case 'setting': $category = 'Ustawienia'; break;
case 'text': $category = 'Content'; break;
case 'setting': $category = 'Setting'; break;
default: $category = null; break;
}
$display_str .= '<tr><td>' . $category . '</td><td>' . $template_fields[$key]['name'] . '</td><td>' . $date[0] . '</td><td>' . $date[1] . '</td></tr>';
Expand Down
2 changes: 1 addition & 1 deletion app/modules/mail/mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

// Validate sended form

$inputs_required = Config::$CF_REQUIRED_INPUTS;
$inputs_required = Config::$CONTACT_REQUIRED_INPUTS;
$inputs_with_errors = array();

foreach($inputs_required as $input_name) {
Expand Down
8 changes: 8 additions & 0 deletions config-app.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class Config {
// Language that will be set by default when entering the website.
static $DEFAULT_LANG = 'en';

// Decide whether the app should detect user system language or not.
// If set to true user will be redirected to url with language that matches his
// system/browser language - only if this language exists and is active.
static $DETECT_LANG = true;

// This is the name of direcory of your theme. Default: "default".
static $THEME_NAME = 'default';

Expand All @@ -36,6 +41,9 @@ class Config {
// Set this to true if you want to send messages to all other users as BCC.
static $CONTACT_ALL = true;

// Form fields that are required to send message
static $CONTACT_REQUIRED_INPUTS = array('message');


# ------------------------------------------------------------------------------
# DEV OPTIONS
Expand Down
8 changes: 4 additions & 4 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# ==================================================================================


define('VIZU_VERSION', '1.1.0');
define('VIZU_VERSION', '1.1.1');


/**
Expand Down Expand Up @@ -63,9 +63,9 @@
* Start language library and set active language
*/

$lang = new libs\Language($db);
$lang_set_by_request = $lang->set(@$router->request[0]);
if ($lang_set_by_request) $router->request_shift();

$lang = new libs\Language($router, $db);
$lang->set();
$lang->load_theme_translations();


Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# VIZU 1.1.0
# VIZU 1.1.1
Simple, dependency-free CMS system that allows for quick implementation of simple Web pages without having to configure anything in the administration panel.


Expand Down
2 changes: 2 additions & 0 deletions themes/admin/style/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ a.nav {

.hidden { display: none; }

.rotate-180 { transform: rotate(180deg); }


.box {
margin-bottom: 10px;
Expand Down
9 changes: 4 additions & 5 deletions themes/admin/templates/edit.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<header>
<h1>Edycja strony</h1>
<a href="" class="nav"><svg class="icon-16 rotate-180"><use xlink:href="#icon-16_arrowhead"></use></svg> Wróć</a>
<h1>Website content editor</h1>
</header>

<ul class="tabs">
<li class="label">Język:</li>
<li class="label">Language:</li>
{{ languages }}
</ul>

Expand All @@ -18,11 +17,11 @@ <h1>Edycja strony</h1>

<div class="buttons ending">
<button type="submit" class="accept">
Zapisz
Save
<svg class="icon-16"><use xlink:href="#icon-16_accept"></use></svg>
</button>
<button type="reset" class="cancel">
Resetuj
Reset
<svg class="icon-16"><use xlink:href="#icon-16_ban"></use></svg>
</button>
</div>
Expand Down
Loading

0 comments on commit cd4f173

Please sign in to comment.