-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.php
124 lines (109 loc) · 3.01 KB
/
api.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
<html>
<head>
<meta charset="utf-8">
<title>API</title>
</head>
<body>
<?php
require_once("db.php");
$query = mysqli_query($bd, "SELECT * FROM names WHERE nkey='name' LIMIT 1");
$result = mysqli_fetch_assoc($query);
mysqli_free_result($query);
$name = $result["value"];
echo "<style>
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
color: #ffffff;
font-size: 180%;
}
textarea {
width: 100%;
height: 100%;
}
</style>";
//The string template for api with placeholders
$str = "<?php
require_once('Simpla.php');
class %class% extends Simpla {
public function get_%name%s(\$filter = array()) {
%filters%
\$query = \$this->db->placehold(\"SELECT DISTINCT * FROM __%name%s x
WHERE 1%filters_sql% ORDER BY x.name\");
\$this->db->query(\$query);
return \$this->db->results();
}
public function get_%name%(\$id) {
\$filter = \$this->db->placehold('x.id = ?', intval(\$id));
\$query = \"SELECT * FROM __%name%s x WHERE \$filter LIMIT 1\";
\$this->db->query(\$query);
return \$this->db->result();
}
public function add_%name%(\$%name%) {
\$%name% = (array) \$%name%;
\$query = \$this->db->query(\"INSERT INTO __%name%s SET ?%\", \$%name%);
return \$this->db->insert_id();
}
public function update_%name%(\$id, \$%name%) {
\$query = \$this->db->placehold(\"UPDATE __%name%s SET ?% WHERE id=? LIMIT 1\", \$%name%, intval(\$id));
\$this->db->query(\$query);
return \$id;
}
public function delete_%name%(\$id) {
if (!empty(\$id)) {
\$query = \$this->db->placehold(\"DELETE FROM __%name%s WHERE id=? LIMIT 1\", \$id);
\$this->db->query(\$query);
}
}
}";
function ecraniseBraces($str) {
$str = str_replace("<", "<", $str);
$str = str_replace(">", ">", $str);
return $str;
}
/**
* Makes filter declarations for function get_%name%s of template
*/
function getFilters() {
global $bd, $name;
$query = mysqli_query($bd, "SELECT name FROM vars WHERE type='0'");
$ret = '';
while ($row = mysqli_fetch_assoc($query)) {
$n = $row["name"];
$ret .= "\t\t\${$n}_filter = '';\n\t\tif (isset(\$filter['{$n}'])) \${$n}_filter = \$this->db->placehold('AND x.{$n}=?', intval(\$filter['{$n}']));\n";
}
mysqli_free_result($query);
return ecraniseBraces($ret);
}
/**
* Makes sql for filters in function get_%name%s of template
*/
function getFiltersSql() {
global $bd, $name;
$query = mysqli_query($bd, "SELECT name FROM vars WHERE type='0'");
$ret = '';
while ($row = mysqli_fetch_assoc($query)) {
$n = $row["name"];
$ret .= " \${$n}_filter";
}
mysqli_free_result($query);
return ecraniseBraces($ret);
}
//Inserting variables into a template
$str = str_replace("<", "<", $str);
$str = str_replace(">", ">", $str);
$str = str_replace("%class%", ucfirst($name)."s", $str);
$str = str_replace("%name%", $name, $str);
$str = str_replace("%filters%", getFilters(), $str);
$str = str_replace("%filters_sql%", getFiltersSql(), $str);
echo "<textarea>";
echo $str;
echo "</textarea>";
?>
</body>
</html>