-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblueprint.php
executable file
·149 lines (133 loc) · 3.86 KB
/
blueprint.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
<?
class blueprint {
var $keywords;
var $props;
var $db;
function blueprint($args = ''){
$this->db = new db;
if($args){
while(list($key,$value) = each($args)){
$this->props[$key] = $value;
}
}
}
function display($array = ''){
while(list($key,$value) = each($array)){
echo $key . " ::: " . $value . "\n<br>";
if(is_array($value)){
echo " ------------------------- \n <br>";
$this->display($value);
}
}
}
function get_all_elements($params = array()) {
//pass this guy an array of conditions for returning elements
$table = $this->props[table];
//echo " in get_all_elements db is $db->database ";
$st = "select * from $table ";
if(count($params)){
$st .= $this->sql_from_params($params);
}
//echo $st ."<br>";
$elements = array();
$rows = $this->db->dbh->query($st);
if(!$rows){
return false;
}
foreach($this->db->dbh->query($st) as $row){
$elements[] = new element($this,$row[id]);
}
return $elements;
}
function get_all_elements_xml($params = array()){
$earray = $this->get_all_elements($params);
$bpname = get_class($this);
$out = "<bp name=\"$bpname\">\n";
for($i=0;$i<count($earray);$i++){
$e = $earray[$i];
$out .= $e->get_as_xml();
}
$out .= "</bp>\n";
return $out;
}
function count_elements($params){
$db = $this->props[local]->props[db];
$table = $this->props[table];
$st = "select count(*) from $table ";
$st .= $this->sql_from_params($params);
$row = $this->db->dbh->query($st);
$count = $row["count(*)"];
return $count;
}
function sql_from_params($params){
if(!is_array($params)){
$params = array();
}
$cnt = 0;
while(list($key,$value) = each($params)){
if(preg_match("/%like%/",$key)){
$key = preg_replace("/%like%/","",$key);
$s .= " $key like \"%$value%\" and";
}else if(strstr('%not like%',$key)){
$s .= " $key not like \"%$value%\" and";
}else if(preg_match("/%regexp%/",$key)){
$key = preg_replace("/%regexp%/","",$key);
$s .= " $key regexp \"$value\" and";
}else if(preg_match("/%starts_with%/",$key)){
$key = preg_replace("/%starts_with%/","",$key);
$s .= " $key like \"$value%\" and";
}else if(preg_match("/%between%/",$key)){
$key = preg_replace("/%between%/","",$key);
$varray = explode(",",$value);
$s .= " $key between " . $varray[0] . " and " . $varray[1] . " and";
}else if(strstr($value,"%or%")){
$varray = explode("%or%",$value);
for($cnt = 0;$cnt<count($varray);$cnt++){
$s .= " $key = \"" . $varray[$cnt] . "\" or";
}
$s = preg_replace("/or$/","",$s);
$s .= " and";
}else if(preg_match("/%>%/",$key)){
$key = preg_replace("/%>%/","",$key);
$s .= " $key > $value and";
}else if(preg_match("/%<%/",$key)){
$key = preg_replace("/%<%/","",$key);
$s .= " $key < $value and";
}else if(preg_match("/%>=%/",$key)){
$key = preg_replace("/%>=%/","",$key);
$s .= " $key >= $value and";
}else if(preg_match("/%<=%/",$key)){
$key = preg_replace("/%<=%/","",$key);
$s .= " $key <= $value and";
}else if(strstr($key,"%!=%")){
$key = str_replace("%!=%","",$key);
$s .= " $key != $value and";
}else if($key != "order_by" && $key != "limit"){
if(preg_match("/^!/",$value)){
$value = preg_replace("/!/","",$value);
$s .= " $key != $value and";
}else{
$s .= " $key = \"$value\" and";
}
}
if($key != "order_by" && $key != "limit"){
$cnt++;
}
}
$s = preg_replace("/and$/","",$s);
$s = preg_replace("/where$/","",$s);
$st = $cnt? " where $s" : "";
if($o = $params[order_by]){
$st .= " order by $o";
}
if($l = $params[limit]){
$st .= " limit $l";
}
//echo "st is $st\n<br>";
return $st;
}
function get_element($id){
return new element($this,$id);
}
}
?>