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