forked from pedro3005/whube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbobj.php
127 lines (111 loc) · 3.21 KB
/
dbobj.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
<?php
/**
* Database Object stuff, handles all database mapping.
*
* Base database interface class
* @author Paul Tagliamonte <paultag@whube.com>
* @version 1.0
* @license: AGPLv3
*/
if ( ! class_exists ( "dbobj" ) ) {
$model_root = dirname( __FILE__ ) . "/";
if ( ! class_exists( "sql" ) ) {
// last ditch...
include( $model_root . "sql.php" );
}
include( $model_root . "../conf/sql.php" );
include( $model_root . "events.php" );
class dbobj {
var $sql;
var $table;
var $pk_field;
function dbobj( $table, $pk_field ) {
global $TABLE_PREFIX;
$this->table = $TABLE_PREFIX.$table;
$this->pk_field = $pk_field;
$this->sql = new sql();
}
function getAll() {
$this->sql->query( "SELECT * FROM " . $this->table . " ORDER BY " . $this->pk_field . " DESC" );
}
function createNew( $items ) {
// key => value
$keys = "";
$values = "";
foreach ( $items as $key => $value ) {
if ( ! is_numeric( $value ) ) {
$value = "'$value'";
}
if ( $keys != "" ) { $keys .= ", "; }
$keys .= $key;
if ( $values != "" ) { $values .= ", "; }
$values .= $value;
}
$this->sql->query( "INSERT INTO " . $this->table . " ( " . $keys . " ) VALUES ( " . $values . " );" );
$ID = $this->sql->getLastID();
$this->sql->query( "UPDATE " . $this->table . " SET startstamp=" . time() . " WHERE " . $this->pk_field . "=" . $ID . " ;" );
$this->updateRoutine( "new", $ID );
return $ID;
}
function updateStamp( $ID ) {
$this->sql->query( "UPDATE " . $this->table . " SET trampstamp=" . time() . " WHERE " . $this->pk_field . "=" . $ID . " ;" );
}
function updateEvent( $STATUS, $ID ) {
$u = new events();
$this->getAllByPK( $ID );
$row = $this->getNext();
$ret[$this->table] = $row;
$ret['status'] = $STATUS;
$u->broadcast( json_encode($ret) );
}
function updateRoutine( $action, $id ) {
$this->updateStamp( $id );
$this->updateEvent( $action, $id );
}
function updateByPK( $PK, $tables ) {
$QUERY = "UPDATE " . $this->table . " SET ";
$nipflipflip = false;
foreach ( $tables as $key => $value ) {
if ( ! is_numeric( $value ) ) {
$value = "'$value'";
}
if ( $nipflipflip ) {
$QUERY .= ", ";
} else {
$nipflipflip = true;
}
$QUERY .= $key . " = " . $value;
}
$QUERY .= " WHERE " . $this->pk_field . " = '" . $PK . "';";
$this->sql->query( $QUERY );
$this->updateRoutine( "update", $PK );
}
function specialSelect($query ) {
$this->sql->query( "SELECT * FROM " . $this->table . " WHERE " . $query . ";" );
return $this->numrows();
}
function getAllByPK( $pk ) {
$this->sql->query( "SELECT * FROM " . $this->table . " WHERE " . $this->pk_field . " = '" . $pk . "'; " );
}
function getByCol( $cID, $id ) {
$this->sql->query( "SELECT * FROM " . $this->table . " WHERE " . $cID . " = '" . $id . "'; " );
}
function searchByKey( $cID, $id ) {
$this->sql->query( "SELECT * FROM " . $this->table . " WHERE " . $cID . " LIKE '%" . $id . "%'; " );
}
function numRows() {
return $this->sql->numrows();
}
function getNext() {
return $this->sql->getNextRow();
}
function toArray() {
$ret = array();
while ( $row = $this->sql->getNextRow() ) {
array_push( $ret, $row );
}
return $ret;
}
}
}
?>