00001 <?php
00002
00020
00021
00022
00023 class DAO {
00025 var $_dataSource;
00026
00031 function DAO($dataSource = null, $callHooks = true) {
00032 if ($callHooks === true && checkPhpVersion('4.3.0')) {
00033 $trace = debug_backtrace();
00034
00035
00036
00037 if (HookRegistry::call($trace[1]['class'] . '::_Constructor', array(&$this, &$dataSource))) {
00038 return;
00039 }
00040 }
00041
00042 if (!isset($dataSource)) {
00043 $this->_dataSource = &DBConnection::getConn();
00044 } else {
00045 $this->_dataSource = $dataSource;
00046 }
00047 }
00048
00055 function &retrieve($sql, $params = false, $callHooks = true) {
00056 if ($callHooks === true && checkPhpVersion('4.3.0')) {
00057 $trace = debug_backtrace();
00058
00059
00060
00061 $value = null;
00062 if (HookRegistry::call($trace[1]['class'] . '::_' . $trace[1]['function'], array(&$sql, &$params, &$value))) {
00063 return $value;
00064 }
00065 }
00066
00067 $result = &$this->_dataSource->execute($sql, $params !== false && !is_array($params) ? array($params) : $params);
00068 if ($this->_dataSource->errorNo()) {
00069
00070 fatalError('DB Error: ' . $this->_dataSource->errorMsg());
00071 }
00072 return $result;
00073 }
00074
00081 function &retrieveCached($sql, $params = false, $secsToCache = 3600, $callHooks = true) {
00082 if ($callHooks === true && checkPhpVersion('4.3.0')) {
00083 $trace = debug_backtrace();
00084
00085
00086
00087 $value = null;
00088 if (HookRegistry::call($trace[1]['class'] . '::_' . $trace[1]['function'], array(&$sql, &$params, &$secsToCache, &$value))) {
00089 return $value;
00090 }
00091 }
00092
00093 $this->setCacheDir();
00094
00095 $result = &$this->_dataSource->CacheExecute($secsToCache, $sql, $params !== false && !is_array($params) ? array($params) : $params);
00096 if ($this->_dataSource->errorNo()) {
00097
00098 fatalError('DB Error: ' . $this->_dataSource->errorMsg());
00099 }
00100 return $result;
00101 }
00102
00111 function &retrieveLimit($sql, $params = false, $numRows = false, $offset = false, $callHooks = true) {
00112 if ($callHooks === true && checkPhpVersion('4.3.0')) {
00113 $trace = debug_backtrace();
00114
00115
00116
00117 $value = null;
00118 if (HookRegistry::call($trace[1]['class'] . '::_' . $trace[1]['function'], array(&$sql, &$params, &$numRows, &$offset, &$value))) {
00119 return $value;
00120 }
00121 }
00122
00123 $result = &$this->_dataSource->selectLimit($sql, $numRows === false ? -1 : $numRows, $offset === false ? -1 : $offset, $params !== false && !is_array($params) ? array($params) : $params);
00124 if ($this->_dataSource->errorNo()) {
00125 fatalError('DB Error: ' . $this->_dataSource->errorMsg());
00126 }
00127 return $result;
00128 }
00129
00136 function &retrieveRange($sql, $params = false, $dbResultRange = null, $callHooks = true) {
00137 if ($callHooks === true && checkPhpVersion('4.3.0')) {
00138 $trace = debug_backtrace();
00139
00140
00141
00142 $value = null;
00143 if (HookRegistry::call($trace[1]['class'] . '::_' . $trace[1]['function'], array(&$sql, &$params, &$dbResultRange, &$value))) {
00144 return $value;
00145 }
00146 }
00147
00148 if (isset($dbResultRange) && $dbResultRange->isValid()) {
00149 $result = &$this->_dataSource->PageExecute($sql, $dbResultRange->getCount(), $dbResultRange->getPage(), $params);
00150 if ($this->_dataSource->errorNo()) {
00151 fatalError('DB Error: ' . $this->_dataSource->errorMsg());
00152 }
00153 }
00154 else {
00155 $result = &$this->retrieve($sql, $params, false);
00156 }
00157 return $result;
00158 }
00159
00168 function update($sql, $params = false, $callHooks = true, $dieOnError = true) {
00169 if ($callHooks === true && checkPhpVersion('4.3.0')) {
00170 $trace = debug_backtrace();
00171
00172
00173
00174 $value = null;
00175 if (HookRegistry::call($trace[1]['class'] . '::_' . $trace[1]['function'], array(&$sql, &$params, &$value))) {
00176 return $value;
00177 }
00178 }
00179
00180 $this->_dataSource->execute($sql, $params !== false && !is_array($params) ? array($params) : $params);
00181 if ($dieOnError && $this->_dataSource->errorNo()) {
00182 fatalError('DB Error: ' . $this->_dataSource->errorMsg());
00183 }
00184 return $this->_dataSource->errorNo() == 0 ? true : false;
00185 }
00186
00193 function replace($table, $arrFields, $keyCols) {
00194 $arrFields = array_map(array($this->_dataSource, 'qstr'), $arrFields);
00195 $this->_dataSource->Replace($table, $arrFields, $keyCols, false);
00196 }
00197
00204 function getInsertId($table = '', $id = '', $callHooks = true) {
00205 return $this->_dataSource->po_insert_id($table, $id);
00206 }
00207
00213 function setCacheDir() {
00214 static $cacheDir;
00215 if (!isset($cacheDir)) {
00216 global $ADODB_CACHE_DIR;
00217
00218 import('cache.CacheManager');
00219 $cacheDir = CacheManager::getFileCachePath() . '/_db';
00220
00221 $ADODB_CACHE_DIR = $cacheDir;
00222 }
00223 }
00224
00228 function flushCache() {
00229 $this->setCacheDir();
00230 $this->_dataSource->CacheFlush();
00231 }
00232
00238 function datetimeToDB($dt) {
00239 return $this->_dataSource->DBTimeStamp($dt);
00240 }
00241
00247 function dateToDB($d) {
00248 return $this->_dataSource->DBDate($d);
00249 }
00250
00256 function datetimeFromDB($dt) {
00257 if ($dt === null) return null;
00258 return $this->_dataSource->UserTimeStamp($dt, 'Y-m-d H:i:s');
00259 }
00265 function dateFromDB($d) {
00266 if ($d === null) return null;
00267 return $this->_dataSource->UserDate($d, 'Y-m-d');
00268 }
00269
00276 function convertFromDB($value, $type) {
00277 switch ($type) {
00278 case 'bool':
00279 $value = (bool) $value;
00280 break;
00281 case 'int':
00282 $value = (int) $value;
00283 break;
00284 case 'float':
00285 $value = (float) $value;
00286 break;
00287 case 'object':
00288 $value = unserialize($value);
00289 break;
00290 case 'date':
00291 if ($value !== null) $value = strtotime($value);
00292 break;
00293 case 'string':
00294 default:
00295
00296 break;
00297 }
00298 return $value;
00299 }
00300
00306 function getType($value) {
00307 switch (gettype($value)) {
00308 case 'boolean':
00309 case 'bool':
00310 return 'bool';
00311 case 'integer':
00312 case 'int':
00313 return 'int';
00314 case 'double':
00315 case 'float':
00316 return 'float';
00317 case 'array':
00318 case 'object':
00319 return 'object';
00320 case 'string':
00321 default:
00322 return 'string';
00323 }
00324 }
00325
00332 function convertToDB($value, &$type) {
00333 if ($type == null) {
00334 $type = $this->getType($value);
00335 }
00336
00337 switch ($type) {
00338 case 'object':
00339 $value = serialize($value);
00340 break;
00341 case 'bool':
00342 $value = $value ? 1 : 0;
00343 break;
00344 case 'date':
00345 if ($value !== null) {
00346 if (!is_numeric($value)) $value = strtotime($value);
00347 $value = strftime('%Y-%m-%d %H:%M:%S', $value);
00348 }
00349 break;
00350 default:
00351 }
00352
00353 return $value;
00354 }
00355
00356 function getLocaleFieldNames() {
00357 return array();
00358 }
00359
00360 function updateDataObjectSettings($tableName, &$dataObject, $idArray) {
00361 $idFields = array_keys($idArray);
00362 $idFields[] = 'locale';
00363 $idFields[] = 'setting_name';
00364
00365 foreach ($this->getLocaleFieldNames() as $field) {
00366 $values = $dataObject->getData($field);
00367 if (!is_array($values)) continue;
00368
00369 foreach ($values as $locale => $value) {
00370 $idArray['setting_type'] = null;
00371 $idArray['locale'] = $locale;
00372 $idArray['setting_name'] = $field;
00373 $idArray['setting_value'] = $this->convertToDB($value, $idArray['setting_type']);
00374
00375 $this->replace($tableName, $idArray, $idFields);
00376 }
00377 }
00378 }
00379
00380 function getDataObjectSettings($tableName, $idFieldName, $idFieldValue, &$dataObject) {
00381 if ($idFieldName !== null) {
00382 $sql = "SELECT * FROM $tableName WHERE $idFieldName = ?";
00383 $params = array($idFieldValue);
00384 } else {
00385 $sql = "SELECT * FROM $tableName";
00386 $params = false;
00387 }
00388 $result =& $this->retrieve($sql, $params);
00389
00390 while (!$result->EOF) {
00391 $row = &$result->getRowAssoc(false);
00392 $dataObject->setData($row['setting_name'], $this->convertFromDB($row['setting_value'], $row['setting_type']), $row['locale']);
00393 unset($row);
00394 $result->MoveNext();
00395 }
00396
00397 $result->Close();
00398 unset($result);
00399 }
00400 }
00401
00402 ?>