This repository has been archived by the owner on Mar 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EWatcherConfig_model.php
103 lines (90 loc) · 2.81 KB
/
EWatcherConfig_model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
// No direct access
defined("EMONCMS_EXEC") or die("Restricted access");
// EWatcher module configuration
class EWatcherConfig
{
// Database connection
private $mysqli;
// User id
private $userid;
// Number of panels
public $numPanels;
// Name of the panels
public $panelsNames;
// Panels active
public $panels;
// Class constructor
public function __construct($mysqli, $userid)
{
$this->mysqli = $mysqli;
$this->userid = (int) $userid;
// Set numeric format (dots instead of commas)
setlocale(LC_NUMERIC, 'C');
// Set configuration
$this->numPanels = 5;
$this->panelsNames = array(
"P1"=>_("Consumption"),
"P2"=>_("Consumption - Queries"),
"P3"=>_("PV"),
"P4"=>_("PV - Queries"),
"P5"=>_("PV - Daily values")
);
// Asign panel actives
$result = $mysqli->query("SELECT * FROM ewatcher WHERE userid=$this->userid;");
if(($result === FALSE) || (empty($result)) || ($result->num_rows == 0)) {
// Create ewatcher user config if it does not exist
if($mysqli->query("INSERT INTO ewatcher (userid) VALUES ($userid);") === FALSE) {
return;
}
$result = $mysqli->query("SELECT * FROM ewatcher WHERE userid=$this->userid;");
}
$result = $result->fetch_object();
for($i = 1; $i <= $this->numPanels; $i++) {
$panelName = "P" . $i;
$this->panels[$panelName] = ($result->$panelName == 1) ? true : false;
}
}
// Get output cost
public function getcout() {
return $this->getProperty("cOut");
}
// Get income cost
public function getcin() {
return $this->getProperty("cIn");
}
// Get units
public function getunits() {
$this->mysqli->set_charset("utf8");
return $this->getProperty("units");
}
// Get generic property
private function getProperty($name) {
$result = $this->mysqli->query("SELECT * FROM ewatcher WHERE userid=$this->userid;");
if($result->num_rows == 0) {
return false;
}
return $result->fetch_object()->$name;
}
// Set output cost
public function setcout($value) {
return $this->setProperty("cOut", (float) $value);
}
// Set income cost
public function setcin($value) {
return $this->setProperty("cIn", (float) $value);
}
// Set units
public function setunits($value) {
$this->mysqli->set_charset("utf8");
return $this->setProperty("units", "'" . $this->mysqli->real_escape_string($value) . "'");
}
// Set generic property
private function setProperty($name, $value) {
if($this->mysqli->query("UPDATE ewatcher SET $name=$value WHERE userid=$this->userid;") === FALSE) {
return false;
}
return $value;
}
}
?>