-
Notifications
You must be signed in to change notification settings - Fork 517
/
Copy pathmultigraph_model.php
98 lines (83 loc) · 3.07 KB
/
multigraph_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
<?php
/*
All Emoncms code is released under the GNU Affero General Public License.
See COPYRIGHT.txt and LICENSE.txt.
---------------------------------------------------------------------
Emoncms - open source energy visualisation
Part of the OpenEnergyMonitor project:
http://openenergymonitor.org
*/
// no direct access
defined('EMONCMS_EXEC') or die('Restricted access');
class Multigraph
{
private $mysqli;
public function __construct($mysqli)
{
$this->mysqli = $mysqli;
}
public function create($userid)
{
$userid = (int) $userid;
$this->mysqli->query("INSERT INTO multigraph (`userid`,`feedlist`, `name`) VALUES ('$userid','', 'New Multigraph')");
return $this->mysqli->insert_id;
}
public function delete($id,$userid)
{
$id = (int) $id;
$userid = (int) $userid;
$stmt = $this->mysqli->prepare("DELETE FROM multigraph WHERE id=? AND userid=?");
$stmt->bind_param("ii", $id, $userid);
$stmt->execute();
$affected_rows = $stmt->affected_rows;
$stmt->close();
if ($affected_rows>0){
return array('success'=>true, 'message'=>'Multigraph deleted');
} else {
return array('success'=>false, 'message'=>'Multigraph was not deleted');
}
}
public function set($id, $userid, $feedlist, $name)
{
$id = (int) $id;
$userid = (int) $userid;
$feedlist = preg_replace('/[^\p{L}_\p{N}\s-.",:{}\[\]]/u','',$feedlist);
$name = preg_replace('/[^\p{L}_\p{N}\s-.]/u','',$name);
$stmt = $this->mysqli->prepare("UPDATE multigraph SET name=?, feedlist=? WHERE id=? AND userid=?");
$stmt->bind_param("ssii", $name, $feedlist, $id, $userid);
$stmt->execute();
$affected_rows = $stmt->affected_rows;
$stmt->close();
if ($affected_rows>0){
return array('success'=>true, 'message'=>'Multigraph updated');
} else {
return array('success'=>false, 'message'=>'Multigraph was not updated');
}
}
/*
userid not used
need to implement public multigraph feature, only return feedlist if multigraph is public or user session
*/
public function get($id, $userid)
{
$id = (int) $id;
$userid = (int) $userid;
$result = $this->mysqli->query("SELECT name, feedlist FROM multigraph WHERE `id`='$id'");
$result = $result->fetch_array();
if (!$result) return array('success'=>false, 'message'=>'Multigraph does not exist');
$row['name'] = $result['name'];
$row['feedlist'] = json_decode($result['feedlist']);
return $row;
}
public function getlist($userid)
{
$userid = (int) $userid;
$result = $this->mysqli->query("SELECT id,name,feedlist FROM multigraph WHERE `userid`='$userid'");
$multigraphs = array();
while ($row = $result->fetch_object())
{
$multigraphs[] = array('id'=>$row->id,'name'=>$row->name,'feedlist'=>$row->feedlist);
}
return $multigraphs;
}
}