1 |
<? |
2 |
## ------------------------------------------------------------------------------------- |
3 |
## $Id: ProxyObject.class.php,v 1.11 2002/11/12 06:05:58 cvsjoko Exp $ |
4 |
## ------------------------------------------------------------------------------------- |
5 |
## $Log: ProxyObject.class.php,v $ |
6 |
## Revision 1.11 2002/11/12 06:05:58 cvsjoko |
7 |
## + renamed class: Text_Encode -> TextEncode |
8 |
## |
9 |
## Revision 1.10 2002/10/29 19:15:33 cvsjoko |
10 |
## - moved utf8/iso-conversion to lib/utils/Text_Encode.php |
11 |
## |
12 |
## Revision 1.9 2002/10/26 12:32:36 cvsjoko |
13 |
## - removed debugging via "print" |
14 |
## + added generic logging via PEAR::Log |
15 |
## |
16 |
## Revision 1.8 2002/10/22 09:51:38 cvsmax |
17 |
## + moved semi-method 'createBackend' to method $site->user->create at User.class.php |
18 |
## |
19 |
## Revision 1.7 2002/10/21 18:27:09 cvsjoko |
20 |
## - manually disabled any form of caching |
21 |
## |
22 |
## Revision 1.6 2002/10/17 03:48:47 cvsmax |
23 |
## + new |
24 |
## + function _createBackend create new Backend |
25 |
## + function _create api-create |
26 |
## |
27 |
## Revision 1.5 2002/10/16 03:37:54 cvsjoko |
28 |
## + bugfix: wrong comparison in restriction to save to array |
29 |
## |
30 |
## Revision 1.4 2002/10/10 03:04:23 cvsjoko |
31 |
## + no debug-output |
32 |
## |
33 |
## Revision 1.3 2002/10/10 03:03:27 cvsjoko |
34 |
## + bugfix: save object to cache only if payload from backend isn't empty |
35 |
## |
36 |
## Revision 1.2 2002/10/10 02:40:06 cvsjoko |
37 |
## + new level of data-caching (session and persistant) |
38 |
## + function _loadState loads data from session |
39 |
## + function _saveState saves data to session |
40 |
## + function save api-save (session & backend) |
41 |
## + function _commit saves data to backend |
42 |
## + handy utils |
43 |
## + function _setAttributes |
44 |
## + function flushState |
45 |
## |
46 |
## Revision 1.1 2002/10/09 00:51:39 cvsjoko |
47 |
## + new |
48 |
## ------------------------------------------------------------------------------------- |
49 |
|
50 |
class ProxyObject { |
51 |
|
52 |
var $objectId; |
53 |
// purpose ... |
54 |
var $meta; |
55 |
var $payload; |
56 |
var $attributes; |
57 |
var $backend; |
58 |
|
59 |
function ProxyObject($objectId = "") { |
60 |
session_register_safe("proxy"); |
61 |
$this->backend = new Remote; |
62 |
$this->_init($objectId); |
63 |
} |
64 |
|
65 |
function _init($objectId="") { |
66 |
if ($objectId) { |
67 |
$this->objectId = $objectId; |
68 |
$this->load(); |
69 |
//$this->_saveCache(); |
70 |
} |
71 |
} |
72 |
|
73 |
function load() { |
74 |
if (!$this->_loadState()) { |
75 |
if (!$this->_loadCache()) { |
76 |
$this->_loadBackend(); |
77 |
} |
78 |
} |
79 |
} |
80 |
|
81 |
function save($data, $type) { |
82 |
$this->_setAttributes($data); |
83 |
$this->_saveState(); |
84 |
if ($type == 'commit') { |
85 |
$this->_commit(); |
86 |
} |
87 |
if ($type == 'create') { |
88 |
$this->_create(); |
89 |
} |
90 |
} |
91 |
|
92 |
function flush() { |
93 |
$this->flushState(); |
94 |
$this->flushCache(); |
95 |
} |
96 |
|
97 |
function _commit() { |
98 |
$this->_saveBackend($this->attributes); |
99 |
$this->flushState(); |
100 |
$this->flushCache(); |
101 |
} |
102 |
|
103 |
|
104 |
// TODO: make this work |
105 |
/* |
106 |
function _create() { |
107 |
$this->_createBackend($this->attributes); |
108 |
$this->flushState(); |
109 |
$this->flushCache(); |
110 |
} |
111 |
*/ |
112 |
|
113 |
function getAttributes() { |
114 |
if (!$this->meta[decoded]) { |
115 |
$this->_decode(); |
116 |
$this->_saveState(); |
117 |
} |
118 |
return $this->attributes; |
119 |
} |
120 |
|
121 |
function _setAttributes($data) { |
122 |
$this->attributes = $data; |
123 |
} |
124 |
|
125 |
function flushCache() { |
126 |
connectdb(); |
127 |
$sql = "DELETE FROM f_proxy WHERE oid='$this->objectId'"; |
128 |
send_sql($sql); |
129 |
} |
130 |
|
131 |
function flushState() { |
132 |
global $proxy; |
133 |
unset($proxy[$this->objectId]); |
134 |
} |
135 |
|
136 |
function _loadState() { |
137 |
global $proxy; |
138 |
logp(get_class($this) . "->_loadState()", LOG_DEBUG); |
139 |
if ($this->attributes = $proxy[$this->objectId]) { |
140 |
//print "_loadState:" . dumpVar($this->attributes); |
141 |
$this->meta[decoded] = 1; |
142 |
// TODO: make a parameter from this (0 deactivates session-layer) |
143 |
return 0; |
144 |
} |
145 |
} |
146 |
|
147 |
function _saveState() { |
148 |
global $proxy; |
149 |
logp(get_class($this) . "->_saveState()", LOG_DEBUG); |
150 |
$proxy[$this->objectId] = $this->attributes; |
151 |
//print "_saveState: " . dumpVar($this->attributes); |
152 |
// TODO: throw exception-message back to user if operation fails |
153 |
} |
154 |
|
155 |
function _loadCache() { |
156 |
logp(get_class($this) . "->_loadCache()", LOG_DEBUG); |
157 |
connectdb(); |
158 |
$sql = "SELECT payload FROM f_proxy WHERE oid='$this->objectId'"; |
159 |
if ($res = send_sql($sql)) { |
160 |
$row = mysql_fetch_array($res, MYSQL_ASSOC); |
161 |
if ($row) { |
162 |
$this->payload = $row[payload]; |
163 |
// TODO: make a parameter from this (0 deactivates mysqldb-layer) |
164 |
return 0; |
165 |
} |
166 |
} |
167 |
} |
168 |
|
169 |
function _saveCache() { |
170 |
logp(get_class($this) . "->_saveCache()", LOG_DEBUG); |
171 |
connectdb(); |
172 |
if ($this->payload) { |
173 |
//$sql = "INSERT INTO f_proxy SET payload='$this->payload' WHERE oid='$this->objectId'"; |
174 |
$sql = "INSERT INTO f_proxy SET oid='$this->objectId', payload='$this->payload'"; |
175 |
if (!send_sql($sql)) { |
176 |
$sql = "UPDATE f_proxy SET payload='$this->payload' WHERE oid='$this->objectId'"; |
177 |
send_sql($sql); |
178 |
} |
179 |
} |
180 |
} |
181 |
|
182 |
function _loadBackend() { |
183 |
logp(get_class($this) . "->_loadBackend()", LOG_DEBUG); |
184 |
// TODO: test backend for reachability first (eventually cache this information and "reset" it by another party) |
185 |
if ($result = $this->backend->call('getObject', $this->objectId)) { |
186 |
//print dumpVar($result); |
187 |
if (count($result) == 0) { return; } |
188 |
$encoder = new TextEncode($result); |
189 |
$encoder->toISO(); |
190 |
if ($_GET[debug]) { |
191 |
print dumpVar($result); |
192 |
} |
193 |
$this->payload = serialize($result); |
194 |
$this->_saveCache(); |
195 |
} |
196 |
} |
197 |
|
198 |
function _saveBackend($result) { |
199 |
logp(get_class($this) . "->_saveBackend()", LOG_DEBUG); |
200 |
$encoder = new TextEncode($result); |
201 |
$encoder->toUTF8(); |
202 |
$response = $this->backend->send('saveObject', array('oid' => $this->objectId, 'data' => $result) ); |
203 |
} |
204 |
|
205 |
function _decode() { |
206 |
// fill attributes-hashtable from message-hashtable |
207 |
if (!$this->payload) { return; } |
208 |
//if ($this->attributes = $backend->decodeData($this->payload)) { |
209 |
if ($this->attributes = unserialize($this->payload)) { |
210 |
$this->meta[decoded] = 1; |
211 |
} |
212 |
} |
213 |
|
214 |
|
215 |
} |
216 |
|
217 |
?> |