diff --git a/AppConfig_model.php b/AppConfig_model.php index 2233d1cf..c4dc2910 100644 --- a/AppConfig_model.php +++ b/AppConfig_model.php @@ -22,33 +22,42 @@ 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; } } @@ -56,20 +65,20 @@ public function set($userid,$json) // 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; @@ -79,7 +88,7 @@ public function get($userid) } else { return false; } - + } } diff --git a/README.md b/README.md new file mode 100644 index 00000000..101bdc34 --- /dev/null +++ b/README.md @@ -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. diff --git a/app.js b/app.js index d227df3c..2b06c86f 100644 --- a/app.js +++ b/app.js @@ -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(''); - $("#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(''); + $("#" + 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[i].innerHTML= key + } else { + flds[i].innerHTML= i18ntext[key] + } + } + out = '

Missing keys in i18n file

' + out + '
'; + 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) {} + }); } }; diff --git a/app_controller.php b/app_controller.php index 8ff875d8..fa598859 100644 --- a/app_controller.php +++ b/app_controller.php @@ -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); } - diff --git a/app_menu.php b/app_menu.php index af939f14..5b681ae3 100644 --- a/app_menu.php +++ b/app_menu.php @@ -1,26 +1,31 @@ get_lang($session['userid']),0,2); +/* - global $session, $user; - - - $apikey = ""; - if ($session['write']) $apikey = "?apikey=".$user->get_apikey_write($session['userid']); - +Check if language file exist;, otherwise force to use en + +*/ +if (!file_exists( dirname(__FILE__)."/locale_js/app_".$lang.'.js')) { + $lang= 'en'; +} + +if ($session['write']) $apikey = "?lang=".$lang."&apikey=".$user->get_apikey_write($session['userid']); $menu_left[] = array( - 'name'=>"Apps", - 'path'=>"app/mysolarpv" , - 'session'=>"write", + 'name'=>dgettext($domain,"Apps"), + 'path'=>"app/mysolarpv" , + 'session'=>"write", 'order' => 5, 'icon'=>'icon-leaf icon-white', 'dropdown'=>array( - - array('name' => 'My Electric', 'icon' => '', 'path' => "app$apikey#myelectric", 'session' => 'read', 'order' => 1), - array('name' => 'My Solar', 'icon' => '', 'path' => "app$apikey#mysolarpv", 'session' => 'read', 'order' => 2), - array('name' => 'My Heatpump', 'icon' => '', 'path' => "app$apikey#myheatpump", 'session' => 'read', 'order' => 3), - array('name' => 'My Solar&Wind', 'icon' => '','path' => "app$apikey#myenergy", 'session' => 'read', 'order' => 4) - + + array('name' => dgettext($domain,'My Electric'), 'icon' => '', 'path' => "app$apikey#myelectric", 'session' => 'read', 'order' => 1), + array('name' => dgettext($domain,'My Solar'), 'icon' => '', 'path' => "app$apikey#mysolarpv", 'session' => 'read', 'order' => 2), + array('name' => dgettext($domain,'My Heatpump'), 'icon' => '', 'path' => "app$apikey#myheatpump", 'session' => 'read', 'order' => 3), + array('name' => dgettext($domain,'My Solar&Wind'), 'icon' => '','path' => "app$apikey#myenergy", 'session' => 'read', 'order' => 4) + ) ); - - - diff --git a/client.php b/client.php index c1b2ffc0..8d9df155 100644 --- a/client.php +++ b/client.php @@ -1,33 +1,35 @@ - - - + + +
+ + + + +
+

+ + + + + + +
+