-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathopenbem_model.php
172 lines (128 loc) · 5.63 KB
/
openbem_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
// no direct access
defined('EMONCMS_EXEC') or die('Restricted access');
class OpenBEM
{
private $mysqli;
private $dbv = "_v3";
public function __construct($mysqli)
{
$this->mysqli = $mysqli;
}
public function get_projects($userid)
{
$userid = (int) $userid;
$result = $this->mysqli->query("SELECT * FROM openbem_projects".$this->dbv." WHERE project_owner = '$userid'");
$projects = array();
while($row = $result->fetch_object())
{
$projects[] = $row;
}
return $projects;
}
public function get_project_details($userid, $project_id)
{
$userid = (int) $userid;
$project_id = (int) $project_id;
$result = $this->mysqli->query("SELECT * FROM openbem_projects".$this->dbv." WHERE project_owner = '$userid' AND project_id = '$project_id'");
return $result->fetch_object();
}
public function add_project($userid,$name,$description)
{
$userid = (int) $userid;
$name = preg_replace('/[^\w\s-]/','',$name);
$description = preg_replace('/[^\w\s-]/','',$description);
$project_mdate = time();
$result = $this->mysqli->query("INSERT INTO openbem_projects".$this->dbv." (`project_name`,`project_description`,`project_owner`,`project_mdate`) VALUES ('$name','$description','$userid','$project_mdate')");
$project_id = $this->mysqli->insert_id;
return $project_id;
}
public function delete_project($userid,$project_id)
{
$project_owner = (int) $userid;
$project_id = (int) $project_id;
$result = $this->mysqli->query("DELETE FROM openbem_projects".$this->dbv." WHERE `project_id`='$project_id' AND `project_owner`='$project_owner'");
if ($this->mysqli->affected_rows==1) {
$result = $this->mysqli->query("DELETE FROM openbem_scenarios".$this->dbv." WHERE `project_id`='$project_id'");
return true;
}
return false;
}
public function get_scenarios($project_id)
{
$project_id = (int) $project_id;
$result = $this->mysqli->query("SELECT `scenario_id`,`scenario_meta` FROM openbem_scenarios".$this->dbv." WHERE `project_id` = '$project_id' ORDER BY scenario_id ASC");
$scenarios = array();
while($row = $result->fetch_object())
{
$row->scenario_meta = json_decode($row->scenario_meta);
$scenarios[] = $row;
}
return $scenarios;
}
public function add_scenario($project_id,$meta)
{
$project_id = (int) $project_id;
$meta = preg_replace('/[^\w\s-.",:{}\[\]]/','',$meta);
$meta = json_decode($meta);
if ($meta==null) return false;
$meta = json_encode($meta);
$data = false;
$stmt = $this->mysqli->prepare("INSERT INTO openbem_scenarios".$this->dbv." (`project_id`,`scenario_meta`,`scenario_data`) VALUES (?,?,?)");
$stmt->bind_param("iss", $project_id, $meta, $data);
$stmt->execute();
$new_scenario_id = $this->mysqli->insert_id;
return $new_scenario_id;
}
public function clone_scenario($projectid,$scenario_id)
{
$project_id = (int) $project_id;
$scenario_id = (int) $scenario_id;
// 1) Get data from scenario to clone
$result = $this->mysqli->query("SELECT `scenario_data`, `scenario_meta` FROM openbem_scenarios".$this->dbv." WHERE `scenario_id` = '$scenario_id'");
$row = $result->fetch_array();
$data = $row['scenario_data'];
$meta = json_decode($row['scenario_meta']);
$meta->name = "Copy of ".$meta->name;
$meta = json_encode($meta);
// 2) Insert data in new scenario
$stmt = $this->mysqli->prepare("INSERT INTO openbem_scenarios".$this->dbv." (`project_id`,`scenario_meta`,`scenario_data`) VALUES (?,?,?)");
$stmt->bind_param("iss", $projectid, $meta, $data);
$stmt->execute();
$new_scenario_id = $this->mysqli->insert_id;
return $new_scenario_id;
}
public function delete_scenario($scenario_id)
{
$scenario_id = (int) $scenario_id;
$result = $this->mysqli->query("DELETE FROM openbem_scenarios".$this->dbv." WHERE `scenario_id` = '$scenario_id'");
return array("Deleted");
}
public function get_scenario($scenario_id)
{
$scenario_id = (int) $scenario_id;
$result = $this->mysqli->query("SELECT `scenario_meta`,`scenario_data` FROM openbem_scenarios".$this->dbv." WHERE `scenario_id` = '$scenario_id'");
$row = $result->fetch_object();
$row->scenario_meta = json_decode($row->scenario_meta);
$row->scenario_data = json_decode($row->scenario_data);
return $row;
}
public function save_scenario($scenario_id,$data)
{
$scenario_id = (int) $scenario_id;
$data = preg_replace('/[^\w\s-.",:{}\[\]]/','',$data);
$data = json_decode($data);
// Dont save if json_decode fails
if ($data!=null) {
$data = json_encode($data);
$stmt = $this->mysqli->prepare("UPDATE openbem_scenarios".$this->dbv." SET `scenario_data` = ? WHERE `scenario_id` = ?");
$stmt->bind_param("si", $data, $scenario_id);
$stmt->execute();
if ($this->mysqli->affected_rows==1) return true; else return false;
}
else
{
return false;
}
}
}