Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add i18n support to app/myelectric in emoncms 9.x #11

Open
wants to merge 15 commits into
base: 9.0
Choose a base branch
from
Open
35 changes: 22 additions & 13 deletions AppConfig_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,54 +22,63 @@ public function __construct($mysqli)
{
$this->mysqli = $mysqli;
}


public function checktable(){
$sql = 'SHOW TABLES LIKE "app_config"';
$result = $this->mysqli->query($sql);
if ($result->num_rows>0) return true;
return false;
}

public function set($userid,$json)
{
$userid = (int) $userid;

//var_dump ($json);

$data = json_decode($json);
if (!$data) return array('success'=>false);

// Input sanitisation
$outdata = array();
$filter = "/[^A-Za-z0-9&£€$.,]/";
foreach ($data as $appname=>$properties)
{
$appname = preg_replace("/[^A-Za-z0-9]/",'',$appname);
$appname = preg_replace($filter,'',$appname);

if (gettype($properties)=="object") {
$outdata[$appname] = array();
foreach ($properties as $property=>$value)
{
$property = preg_replace("/[^A-Za-z0-9]/",'',$property);
$property = preg_replace($filter,'',$property);
if (gettype($value)=="array") {
$tmp = array();
foreach ($value as $val) $tmp[] = (int) $val;
$value = $tmp;
} else {
$value = preg_replace("/[^A-Za-z0-9,;£$.]/",'',$value);
$value = preg_replace($filter,'',$value);
}

$outdata[$appname][$property] = $value;
}
}
}

// Re-encode for storage in db text field
$json = json_encode($outdata);

$result = $this->mysqli->query("SELECT `userid` FROM app_config WHERE `userid`='$userid'");
if ($result->num_rows) {
$this->mysqli->query("UPDATE app_config SET `data`='$json' WHERE `userid`='$userid'");
return true;

} else {
$this->mysqli->query("INSERT INTO app_config (`userid`,`data`) VALUES ('$userid','$json')");
return true;
}

return array('success'=>false);
}

public function get($userid)
{
$userid = (int) $userid;
Expand All @@ -79,7 +88,7 @@ public function get($userid)
} else {
return false;
}

}

}
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#App module
App, is an EMONCMS complementary module made to display technical information on theoretically any type of device. The screen is preformated to show predefined type of feeds.

Example: My electric is able to show the house electrical need, consumption of the day, month, year, with values and graphs.

how to use it:
copy or clone this repository into the module directory of your EMONCMS installation.
Visit admin page and update database. This module uses a specific table, not present in previous releases.
Go to the config page (wrench positionned top right of screen) to configure to your needs
NB:

rem : A conversion to bootstrap3 and font awesome icons will probably make the presentation more responsive.
148 changes: 97 additions & 51 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,131 @@
var app = {

basepath: path+"Modules/app/",

basepath: path + "Modules/app/",
include: {},

loaded: {},

load: function(appname)
{
load: function (appname) {
app.loaded[appname] = true;
// We load here the html of the app into an dedicated element for that app
// when an app is loaded its html remains in the dom even when viewing another app
// an app is just hidden and shown depending on the visibility settings.

// we check here if the app has been loaded to the dom, if not we load it

var appdom = $("#app_"+appname);

if (appdom.length) return true;


var html = "";
$.ajax({url: app.basepath+appname+"/"+appname+".html", async: false, cache: false, success: function(data) {html = data;} });

$("#content").append('<div class="apps" id="app_'+appname+'" style="display:none"></div>');
$("#app_"+appname).html(html);
var dash = "app_" + appname,
appdom = $("#" + dash),
html = '',
include='',
file,
i;

if (appdom.length) {return true; }
$.ajax({
url: app.basepath + appname + "/" + appname + ".html",
async: false,
cache: false,
success: function (data) {html = data; }
});

$("#content").append('<div class="apps" id="' + dash + '" style="display:none"></div>');
$("#" + dash).html(html);

$.ajax({
url: app.basepath+appname+"/"+appname+".js",
url: app.basepath + appname + "/" + appname + ".js",
dataType: 'script',
async: false
});

// include the translation file required by asked language
// check if answer is 200 or create a blank i18ntext variable
$.ajax({
url: app.basepath + "locale_js/app_" + 'en' + '.js',
dataType: 'script',
async: false,
error: function () {
}
});
$.ajax({
url: app.basepath + "locale_js/app_" + lang + '.js',
dataType: 'script',
async: false,
error: function () {
}
});
// ----------------------------------------------------------
// Included javascript loader
// ----------------------------------------------------------
var include = window["app_"+appname].include;
for (i in include) {
var file = include[i];
if (app.include[file]==undefined)
{
include = window[dash].include;
for (i = 0; i < include.length; i++) {
file = include[i];
if (app.include[file] === undefined) {
//file = file.replace('%lang%', lang);
app.include[file] = true;
$.ajax({
url: path+file,
url: path + file,
dataType: 'script',
async: false
});
}
}

window["app_"+appname].init();

window[dash].init();
var result= {
"ref":{
"test":"test"
}
};
// the DOM will be available here

var flds = document.getElementsByClassName("i18n");
var out = '';
for(var i=0; i<flds.length;i++){
var key = flds[i].innerHTML;
result['ref'][key]=key
if ( i18ntext[key] === undefined ){
out +=("'" + key + "'" + " : '"+key + "',<br />");
flds[i].innerHTML= key
} else {
flds[i].innerHTML= i18ntext[key]
}
}
out = '<h3 class="i18n">Missing keys in i18n file</h3><div>' + out + '</div>';
document.getElementById("obj").innerHTML= out

return true;
},
show: function(appname)
{
if (app.loaded[appname]==undefined) app.load(appname);

show: function (appname) {
var dash = "app_" + appname;
if (app.loaded[appname] === undefined) {app.load(appname); }
$(".apps").hide();
$("#app_"+appname).show();
if (window["app_"+appname]!=undefined) window["app_"+appname].show();
$("#" + dash).show();
if (window[dash] !== undefined) {window[dash].show(); }
},
hide: function(appname)
{
$("#app_"+appname).hide();
if (window["app_"+appname]!=undefined) window["app_"+appname].hide();

hide: function (appname) {
var dash = "app_" + appname;
$("#" + dash).hide();
if (window[dash] !== undefined) {window[dash].hide(); }
},

getconfig: function()
{
var config = {};
var apikeystr = "";
if (window.apikey!=undefined) apikeystr = "?apikey="+apikey;
$.ajax({ url: path+"app/getconfig.json"+apikeystr, dataType: 'json', async: false, success: function(data) {config = data;} });

getconfig: function () {
var config = {},
apikeystr = (window.apikey !== undefined) ? "?apikey=" + apikey : "";

//if (window.apikey !== undefined) {apikeystr = "?apikey=" + apikey; }
$.ajax({
url: path + "app/getconfig.json" + apikeystr,
dataType: 'json',
async: false,
success: function (data) {config = data; }
});
app.config = config;
return config;
},

setconfig: function(config)
{
$.ajax({ url: path+"app/setconfig.json", data: "data="+JSON.stringify(config), async: false, success: function(data){} });

setconfig: function (config) {
$.ajax({
url: path + "app/setconfig.json",
data: "data=" + JSON.stringify(config),
async: false,
success: function (data) {}
});
}
};
62 changes: 33 additions & 29 deletions app_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,46 @@ function app_controller()
global $session,$route,$mysqli;

$result = false;

include "Modules/app/AppConfig_model.php";
$appconfig = new AppConfig($mysqli);

if ($route->format == 'html')
{
if ($route->action == "" && $session['write']) {
$result = view("Modules/app/client.php",array());
}
}

if ($route->format == 'json')
{
if ($route->action == "setconfig" && $session['write'])
$result = $appconfig->set($session['userid'],get('data'));

if ($route->action == "getconfig" && $session['read'])
$result = $appconfig->get($session['userid']);

if ($route->action == "dataremote")
if ($appconfig->checktable() == false){
$result = view("Modules/app/tblerr.php",array());
} else {
if ($route->format == 'html')
{
$id = (int) get("id");
$start = (float) get("start");
$end = (float) get("end");
$interval = (int) get("interval");

$result = json_decode(file_get_contents("http://emoncms.org/feed/data.json?id=$id&start=$start&end=$end&interval=$interval&skipmissing=0&limitinterval=0"));
if ($route->action == "" && $session['write']) {
$result = view("Modules/app/client.php",array());
}
}
if ($route->action == "valueremote")

if ($route->format == 'json')
{
$id = (int) get("id");
$result = (float) json_decode(file_get_contents("http://emoncms.org/feed/value.json?id=$id"));
if ($route->action == "setconfig" && $session['write'])
$result = $appconfig->set($session['userid'],get('data'));

if ($route->action == "getconfig" && $session['read'])
$result = $appconfig->get($session['userid']);

if ($route->action == "dataremote")
{
$id = (int) get("id");
$start = (float) get("start");
$end = (float) get("end");
$interval = (int) get("interval");

$result = json_decode(file_get_contents("http://emoncms.org/feed/data.json?id=$id&start=$start&end=$end&interval=$interval&skipmissing=0&limitinterval=0"));
}

if ($route->action == "valueremote")
{
$id = (int) get("id");
$result = (float) json_decode(file_get_contents("http://emoncms.org/feed/value.json?id=$id"));
}
}

}


return array('content'=>$result, 'fullwidth'=>true);
}

Loading