Annotation of /mambo/branches/4.6/includes/database.php
Parent Directory
|
Revision Log
Revision 287 - (view) (download)
| 1 : | csouza | 151 | <?php |
| 2 : | |||
| 3 : | /** | ||
| 4 : | * Database connector class | ||
| 5 : | * @subpackage Database | ||
| 6 : | * @package Mambo | ||
| 7 : | */ | ||
| 8 : | class database { | ||
| 9 : | /** @var string Internal variable to hold the query sql */ | ||
| 10 : | var $_sql=''; | ||
| 11 : | /** @var int Internal variable to hold the database error number */ | ||
| 12 : | var $_errorNum=0; | ||
| 13 : | /** @var string Internal variable to hold the database error message */ | ||
| 14 : | var $_errorMsg=''; | ||
| 15 : | /** @var string Internal variable to hold the prefix used on all database tables */ | ||
| 16 : | var $_table_prefix=''; | ||
| 17 : | /** @var Internal variable to hold the connector resource */ | ||
| 18 : | var $_resource=''; | ||
| 19 : | /** @var Internal variable to hold the last query cursor */ | ||
| 20 : | var $_cursor=null; | ||
| 21 : | /** @var boolean Debug option */ | ||
| 22 : | var $_debug=0; | ||
| 23 : | /** @var array A log of queries */ | ||
| 24 : | var $_log=array(); | ||
| 25 : | |||
| 26 : | /** | ||
| 27 : | * Database object constructor | ||
| 28 : | * @param string Database host | ||
| 29 : | * @param string Database user name | ||
| 30 : | * @param string Database user password | ||
| 31 : | * @param string Database name | ||
| 32 : | * @param string Common prefix for all tables | ||
| 33 : | */ | ||
| 34 : | function database( $host='localhost', $user, $pass, $db, $table_prefix ) { | ||
| 35 : | // perform a number of fatality checks, then die gracefully | ||
| 36 : | if (!function_exists( 'mysql_connect' )) $this->forceOffline(1); | ||
| 37 : | if (!($this->_resource = @mysql_connect( $host, $user, $pass ))) $this->forceOffline(2); | ||
| 38 : | if (!mysql_select_db($db)) $this->forceOffline(3); | ||
| 39 : | $this->_table_prefix = $table_prefix; | ||
| 40 : | } | ||
| 41 : | |||
| 42 : | function forceOffline ($error_number) { | ||
| 43 : | $mosSystemError = $error_number; | ||
| 44 : | $basePath = dirname( __FILE__ ); | ||
| 45 : | counterpoi | 182 | t include $basePath . '/../configuration.php'; |
| 46 : | t include $basePath . '/../offline.php'; | ||
| 47 : | csouza | 151 | exit(); |
| 48 : | } | ||
| 49 : | /** | ||
| 50 : | * @param int | ||
| 51 : | */ | ||
| 52 : | function debug( $level ) { | ||
| 53 : | $this->_debug = intval( $level ); | ||
| 54 : | } | ||
| 55 : | |||
| 56 : | function debug_trace () { | ||
| 57 : | trigger_error( $this->_errorNum, E_USER_NOTICE ); | ||
| 58 : | //echo "<pre>" . $this->_sql . "</pre>\n"; | ||
| 59 : | if (function_exists('debug_backtrace')) { | ||
| 60 : | foreach(debug_backtrace() as $back) { | ||
| 61 : | if (@$back['file']) { | ||
| 62 : | echo '<br />'.$back['file'].':'.$back['line']; | ||
| 63 : | } | ||
| 64 : | } | ||
| 65 : | } | ||
| 66 : | } | ||
| 67 : | /** | ||
| 68 : | * @return int The error number for the most recent query | ||
| 69 : | */ | ||
| 70 : | function getErrorNum() { | ||
| 71 : | return $this->_errorNum; | ||
| 72 : | } | ||
| 73 : | /** | ||
| 74 : | * @return string The error message for the most recent query | ||
| 75 : | */ | ||
| 76 : | function getErrorMsg() { | ||
| 77 : | return str_replace( array( "\n", "'" ), array( '\n', "\'" ), $this->_errorMsg ); | ||
| 78 : | } | ||
| 79 : | /** | ||
| 80 : | * Get a database escaped string | ||
| 81 : | * @return string | ||
| 82 : | */ | ||
| 83 : | function getEscaped( $text ) { | ||
| 84 : | return mysql_escape_string( $text ); | ||
| 85 : | } | ||
| 86 : | /** | ||
| 87 : | * Get a quoted database escaped string | ||
| 88 : | * @return string | ||
| 89 : | */ | ||
| 90 : | function Quote( $text ) { | ||
| 91 : | return '\'' . mysql_escape_string( $text ) . '\''; | ||
| 92 : | } | ||
| 93 : | /** | ||
| 94 : | * Sets the SQL query string for later execution. | ||
| 95 : | * | ||
| 96 : | * This function replaces a string identifier <var>$prefix</var> with the | ||
| 97 : | * string held is the <var>_table_prefix</var> class variable. | ||
| 98 : | * | ||
| 99 : | * @param string The SQL query | ||
| 100 : | * @param string The common table prefix | ||
| 101 : | */ | ||
| 102 : | function setQuery( $sql, $prefix='#__' ) { | ||
| 103 : | $this->_sql = $this->replacePrefix($sql, $prefix); | ||
| 104 : | } | ||
| 105 : | |||
| 106 : | /** | ||
| 107 : | * This function replaces a string identifier <var>$prefix</var> with the | ||
| 108 : | * string held is the <var>_table_prefix</var> class variable. | ||
| 109 : | * | ||
| 110 : | * @param string The SQL query | ||
| 111 : | * @param string The common table prefix | ||
| 112 : | * @author thede, David McKinnis | ||
| 113 : | */ | ||
| 114 : | function replacePrefix ($sql, $prefix='#__') { | ||
| 115 : | $done = ''; | ||
| 116 : | while (strlen($sql)) { | ||
| 117 : | counterpoi | 281 | t $single = preg_match("/\'([^\\\']|\\.)*'/", $sql,$matches_single,PREG_OFFSET_CAPTURE); |
| 118 : | t if ($double = preg_match('/\"([^\\\"]|\\.)*"/', $sql,$matches_double,PREG_OFFSET_CAPTURE) OR $single) { | ||
| 119 : | csouza | 151 | if ($single == 0 OR ($double AND $matches_double[0][1] < $matches_single[0][1])) { |
| 120 : | $done .= str_replace($prefix, $this->_table_prefix, substr($sql,0,$matches_double[0][1])).$matches_double[0][0]; | ||
| 121 : | $sql = substr($sql,$matches_double[0][1]+strlen($matches_double[0][0])); | ||
| 122 : | } | ||
| 123 : | else { | ||
| 124 : | $done .= str_replace($prefix, $this->_table_prefix, substr($sql,0,$matches_single[0][1])).$matches_single[0][0]; | ||
| 125 : | $sql = substr($sql,$matches_single[0][1]+strlen($matches_single[0][0])); | ||
| 126 : | } | ||
| 127 : | } | ||
| 128 : | else return $done.str_replace($prefix, $this->_table_prefix,$sql); | ||
| 129 : | } | ||
| 130 : | return $done; | ||
| 131 : | } | ||
| 132 : | /** | ||
| 133 : | * @return string The current value of the internal SQL vairable | ||
| 134 : | */ | ||
| 135 : | function getQuery($sql='') { | ||
| 136 : | if ($sql == '') $sql = $this->_sql; | ||
| 137 : | return "<pre>" . htmlspecialchars( $sql ) . "</pre>"; | ||
| 138 : | } | ||
| 139 : | /** | ||
| 140 : | * Execute the query | ||
| 141 : | * @return mixed A database resource if successful, FALSE if not. | ||
| 142 : | */ | ||
| 143 : | function query($sql = '') { | ||
| 144 : | global $mosConfig_debug; | ||
| 145 : | if ($sql == '') $sql = $this->_sql; | ||
| 146 : | if ($this->_debug) $this->_log[] = $sql; | ||
| 147 : | if ($this->_cursor = mysql_query($sql, $this->_resource)) { | ||
| 148 : | $this->_errorNum = 0; | ||
| 149 : | $this->_errorMsg = ''; | ||
| 150 : | return $this->_cursor; | ||
| 151 : | } | ||
| 152 : | else { | ||
| 153 : | $this->_errorNum = mysql_errno( $this->_resource ); | ||
| 154 : | $this->_errorMsg = mysql_error( $this->_resource )." SQL=$sql"; | ||
| 155 : | if ($this->_debug) $this->debug_trace(); | ||
| 156 : | return false; | ||
| 157 : | } | ||
| 158 : | } | ||
| 159 : | |||
| 160 : | function query_batch( $abort_on_error=true, $p_transaction_safe = false) { | ||
| 161 : | $this->_errorNum = 0; | ||
| 162 : | $this->_errorMsg = ''; | ||
| 163 : | if ($p_transaction_safe) { | ||
| 164 : | $si = mysql_get_server_info(); | ||
| 165 : | preg_match_all( "/(\d+)\.(\d+)\.(\d+)/i", $si, $m ); | ||
| 166 : | $prefix = ''; | ||
| 167 : | if ($m[1] >= 4) $prefix = 'START TRANSACTION; '; | ||
| 168 : | elseif ($m[2] >= 23) { | ||
| 169 : | if ($m[3] >= 19) $prefix = 'BEGIN WORK; '; | ||
| 170 : | elseif ($m[3] >= 17) $prefix = 'BEGIN; '; | ||
| 171 : | } | ||
| 172 : | if ($prefix) $this->_sql = $prefix.$this->_sql.'; COMMIT;'; | ||
| 173 : | } | ||
| 174 : | $query_split = preg_split ("/[;]+/", $this->_sql); | ||
| 175 : | $error = 0; | ||
| 176 : | foreach ($query_split as $command_line) { | ||
| 177 : | $command_line = trim( $command_line ); | ||
| 178 : | if ($command_line != '') { | ||
| 179 : | if (!$this->query($command_line)) { | ||
| 180 : | $error = 1; echo 'xxx '; | ||
| 181 : | if ($abort_on_error) { | ||
| 182 : | return $this->_cursor; | ||
| 183 : | } | ||
| 184 : | } | ||
| 185 : | } | ||
| 186 : | } | ||
| 187 : | return $error ? false : true; | ||
| 188 : | } | ||
| 189 : | |||
| 190 : | /** | ||
| 191 : | * Diagnostic function | ||
| 192 : | */ | ||
| 193 : | function explain() { | ||
| 194 : | if (!($cur = $this->query("EXPLAIN ".$this->_sql))) return null; | ||
| 195 : | $headline = $header = $body = ''; | ||
| 196 : | $buf = '<table cellspacing="1" cellpadding="2" border="0" bgcolor="#000000" align="center">'; | ||
| 197 : | $buf .= $this->getQuery("EXPLAIN ".$this->_sql); | ||
| 198 : | while ($row = mysql_fetch_assoc($cur)) { | ||
| 199 : | $body .= "<tr>"; | ||
| 200 : | foreach ($row as $k=>$v) { | ||
| 201 : | if ($headline == '') $header .= "<th bgcolor=\"#ffffff\">$k</th>"; | ||
| 202 : | $body .= "<td bgcolor=\"#ffffff\">$v</td>"; | ||
| 203 : | } | ||
| 204 : | $headline = $header; | ||
| 205 : | $body .= "</tr>"; | ||
| 206 : | } | ||
| 207 : | $buf .= "<tr>$headline</tr>$body</table><br /> "; | ||
| 208 : | mysql_free_result( $cur ); | ||
| 209 : | return "<div style=\"background-color:#FFFFCC\" align=\"left\">$buf</div>"; | ||
| 210 : | } | ||
| 211 : | /** | ||
| 212 : | * @return int The number of rows returned from the most recent query. | ||
| 213 : | */ | ||
| 214 : | function getNumRows( $cur=null ) { | ||
| 215 : | return mysql_num_rows( $cur ? $cur : $this->_cursor ); | ||
| 216 : | } | ||
| 217 : | |||
| 218 : | /** | ||
| 219 : | * Load an array of retrieved database objects or values | ||
| 220 : | * @param int Database cursor | ||
| 221 : | * @param string The field name of a primary key | ||
| 222 : | * @return array If <var>key</var> is empty as sequential list of returned records. | ||
| 223 : | * If <var>key</var> is not empty then the returned array is indexed by the value | ||
| 224 : | * the database key. Returns <var>null</var> if the query fails. | ||
| 225 : | */ | ||
| 226 : | function &retrieveResults ($key='', $max=0, $result_type='row') { | ||
| 227 : | $results = array(); | ||
| 228 : | $sql_function = 'mysql_fetch_'.$result_type; | ||
| 229 : | if ($cur = $this->query()) { | ||
| 230 : | while ($row = $sql_function($cur)) { | ||
| 231 : | if ($key != '') $results[$row->$key] = $row; | ||
| 232 : | else $results[] = $row; | ||
| 233 : | if ($max AND count($results) >= $max) break; | ||
| 234 : | } | ||
| 235 : | mysql_free_result($cur); | ||
| 236 : | } | ||
| 237 : | return $results; | ||
| 238 : | } | ||
| 239 : | /** | ||
| 240 : | * This method loads the first field of the first row returned by the query. | ||
| 241 : | * | ||
| 242 : | * @return The value returned in the query or null if the query failed. | ||
| 243 : | */ | ||
| 244 : | function loadResult() { | ||
| 245 : | $results =& $this->retrieveResults('', 1, 'row'); | ||
| 246 : | if (count($results)) return $results[0][0]; | ||
| 247 : | else return null; | ||
| 248 : | } | ||
| 249 : | |||
| 250 : | /** | ||
| 251 : | * Load an array of single field results into an array | ||
| 252 : | */ | ||
| 253 : | function loadResultArray($numinarray = 0) { | ||
| 254 : | $results =& $this->retrieveResults('', 0, 'row'); | ||
| 255 : | $values = array(); | ||
| 256 : | foreach ($results as $result) $values[] = $result[$numinarray]; | ||
| 257 : | if (count($values)) return $values; | ||
| 258 : | else return null; | ||
| 259 : | } | ||
| 260 : | /** | ||
| 261 : | * Load a assoc list of database rows | ||
| 262 : | * @param string The field name of a primary key | ||
| 263 : | * @return array If <var>key</var> is empty as sequential list of returned records. | ||
| 264 : | */ | ||
| 265 : | function loadAssocList( $key='' ) { | ||
| 266 : | $results =& $this->retrieveResults($key, 0, 'assoc'); | ||
| 267 : | if (count($results)) return $results[0][0]; | ||
| 268 : | else return null; | ||
| 269 : | } | ||
| 270 : | /** | ||
| 271 : | * Copy the named array content into the object as properties | ||
| 272 : | * only existing properties of object are filled. when undefined in hash, properties wont be deleted | ||
| 273 : | * @param array the input array | ||
| 274 : | * @param obj byref the object to fill of any class | ||
| 275 : | * @param string | ||
| 276 : | * @param boolean | ||
| 277 : | */ | ||
| 278 : | function mosBindArrayToObject( $array, &$obj, $ignore='', $prefix=NULL, $checkSlashes=true ) { | ||
| 279 : | if (!is_array($array) OR !is_object($obj)) return false; | ||
| 280 : | if ($prefix == null) $prefix = ''; | ||
| 281 : | foreach (get_object_vars($obj) as $k => $v) { | ||
| 282 : | if( substr( $k, 0, 1 ) != '_' AND strpos($ignore, $k) === false) { | ||
| 283 : | if (isset($array[$prefix.$k])) { | ||
| 284 : | $obj->$k = ($checkSlashes AND get_magic_quotes_gpc()) ? $this->mosStripslashes( $array[$prefix.$k] ) : $array[$prefix.$k]; | ||
| 285 : | } | ||
| 286 : | } | ||
| 287 : | } | ||
| 288 : | return true; | ||
| 289 : | } | ||
| 290 : | |||
| 291 : | /** | ||
| 292 : | * Strip slashes from strings or arrays of strings | ||
| 293 : | * @param value the input string or array | ||
| 294 : | */ | ||
| 295 : | function mosStripslashes(&$value) { | ||
| 296 : | if (is_string($value)) $ret = stripslashes($value); | ||
| 297 : | else { | ||
| 298 : | if (is_array($value)) { | ||
| 299 : | $ret = array(); | ||
| 300 : | while (list($key,$val) = each($value)) { | ||
| 301 : | $ret[$key] = $this->mosStripslashes($val); | ||
| 302 : | } // while | ||
| 303 : | } else $ret = $value; | ||
| 304 : | } // if | ||
| 305 : | return $ret; | ||
| 306 : | } // mosStripSlashes | ||
| 307 : | |||
| 308 : | /** | ||
| 309 : | * This global function loads the first row of a query into an object | ||
| 310 : | * | ||
| 311 : | * If an object is passed to this function, the returned row is bound to the existing elements of <var>object</var>. | ||
| 312 : | * If <var>object</var> has a value of null, then all of the returned query fields returned in the object. | ||
| 313 : | * @param string The SQL query | ||
| 314 : | * @param object The address of variable | ||
| 315 : | */ | ||
| 316 : | function loadObject( &$object ) { | ||
| 317 : | if ($object != null) { | ||
| 318 : | $results =& $this->retrieveResults('', 1, 'assoc'); | ||
| 319 : | if (count($results)) { | ||
| 320 : | $this->mosBindArrayToObject($results[0], $object, null, null, false); | ||
| 321 : | return true; | ||
| 322 : | } | ||
| 323 : | } | ||
| 324 : | else { | ||
| 325 : | $results =& $this->retrieveResults('', 1, 'object'); | ||
| 326 : | if (count($results)) { | ||
| 327 : | $object = $results[0]; | ||
| 328 : | return true; | ||
| 329 : | } | ||
| 330 : | else $object = null; | ||
| 331 : | } | ||
| 332 : | return false; | ||
| 333 : | } | ||
| 334 : | /** | ||
| 335 : | * Load a list of database objects | ||
| 336 : | * @param string The field name of a primary key | ||
| 337 : | * @return array If <var>key</var> is empty as sequential list of returned records. | ||
| 338 : | * If <var>key</var> is not empty then the returned array is indexed by the value | ||
| 339 : | * the database key. Returns <var>null</var> if the query fails. | ||
| 340 : | */ | ||
| 341 : | function loadObjectList( $key='' ) { | ||
| 342 : | $results =& $this->retrieveResults($key, 0, 'object'); | ||
| 343 : | if (count($results)) return $results; | ||
| 344 : | else return null; | ||
| 345 : | } | ||
| 346 : | /** | ||
| 347 : | * @return The first row of the query. | ||
| 348 : | */ | ||
| 349 : | function loadRow() { | ||
| 350 : | $results =& $this->retrieveResults('', 1, 'row'); | ||
| 351 : | if (count($results)) return $results[0]; | ||
| 352 : | else return null; | ||
| 353 : | } | ||
| 354 : | /** | ||
| 355 : | * Load a list of database rows (numeric column indexing) | ||
| 356 : | * @param string The field name of a primary key | ||
| 357 : | * @return array If <var>key</var> is empty as sequential list of returned records. | ||
| 358 : | * If <var>key</var> is not empty then the returned array is indexed by the value | ||
| 359 : | * the database key. Returns <var>null</var> if the query fails. | ||
| 360 : | */ | ||
| 361 : | function loadRowList( $key='' ) { | ||
| 362 : | $results =& $this->retrieveResults('', 0, 'row'); | ||
| 363 : | if (count($results)) return $results; | ||
| 364 : | else return null; | ||
| 365 : | } | ||
| 366 : | |||
| 367 : | /** | ||
| 368 : | * Document::db_insertObject() | ||
| 369 : | * | ||
| 370 : | * { Description } | ||
| 371 : | * | ||
| 372 : | * @param [type] $keyName | ||
| 373 : | * @param [type] $verbose | ||
| 374 : | */ | ||
| 375 : | function insertObject( $table, &$object, $keyName = NULL, $verbose=false ) { | ||
| 376 : | $fmtsql = "INSERT INTO $table ( %s ) VALUES ( %s ) "; | ||
| 377 : | $fields = array(); | ||
| 378 : | foreach (get_object_vars( $object ) as $k => $v) { | ||
| 379 : | if (is_array($v) OR is_object($v) OR $v === NULL OR $k[0] == '_') continue; | ||
| 380 : | $fields[] = "`$k`"; | ||
| 381 : | $values[] = "'" . $this->getEscaped( $v ) . "'"; | ||
| 382 : | } | ||
| 383 : | if (!isset($fields)) die ('class database method insertObject - no fields'); | ||
| 384 : | $this->setQuery( sprintf( $fmtsql, implode( ",", $fields ), implode( ",", $values ) ) ); | ||
| 385 : | ($verbose) && print "$sql<br />\n"; | ||
| 386 : | if (!$this->query()) return false; | ||
| 387 : | $id = mysql_insert_id(); | ||
| 388 : | ($verbose) && print "id=[$id]<br />\n"; | ||
| 389 : | if ($keyName && $id) $object->$keyName = $id; | ||
| 390 : | return true; | ||
| 391 : | } | ||
| 392 : | |||
| 393 : | /** | ||
| 394 : | * Document::db_updateObject() | ||
| 395 : | * | ||
| 396 : | * { Description } | ||
| 397 : | * | ||
| 398 : | * @param [type] $updateNulls | ||
| 399 : | */ | ||
| 400 : | function updateObject( $table, &$object, $keyName, $updateNulls=true ) { | ||
| 401 : | $fmtsql = "UPDATE $table SET %s WHERE %s"; | ||
| 402 : | $tmp = array(); | ||
| 403 : | foreach (get_object_vars( $object ) as $k => $v) { | ||
| 404 : | if (is_array($v) OR is_object($v) OR $k[0] == '_' OR ($v === null AND !$updateNulls)) continue; | ||
| 405 : | if( $k == $keyName ) { // PK not to be updated | ||
| 406 : | $where = "$keyName='" . $this->getEscaped( $v ) . "'"; | ||
| 407 : | continue; | ||
| 408 : | } | ||
| 409 : | if ($v) $v = $this->getEscaped($v); | ||
| 410 : | $tmp[] = "`$k`='$v'"; | ||
| 411 : | } | ||
| 412 : | if (!isset($tmp)) return true; | ||
| 413 : | if (!isset($where)) die ('database class updateObject method - no key value'); | ||
| 414 : | $this->setQuery( sprintf( $fmtsql, implode( ",", $tmp ) , $where ) ); | ||
| 415 : | return $this->query(); | ||
| 416 : | } | ||
| 417 : | |||
| 418 : | /** | ||
| 419 : | * @param boolean If TRUE, displays the last SQL statement sent to the database | ||
| 420 : | * @return string A standised error message | ||
| 421 : | */ | ||
| 422 : | function stderr( $showSQL = false ) { | ||
| 423 : | return "DB function failed with error number $this->_errorNum" | ||
| 424 : | ."<br /><font color=\"red\">$this->_errorMsg</font>" | ||
| 425 : | .($showSQL ? "<br />SQL = <pre>$this->_sql</pre>" : ''); | ||
| 426 : | } | ||
| 427 : | |||
| 428 : | function insertid() | ||
| 429 : | { | ||
| 430 : | return mysql_insert_id(); | ||
| 431 : | } | ||
| 432 : | |||
| 433 : | function getVersion() | ||
| 434 : | { | ||
| 435 : | return mysql_get_server_info(); | ||
| 436 : | } | ||
| 437 : | |||
| 438 : | /** | ||
| 439 : | * Fudge method for ADOdb compatibility | ||
| 440 : | */ | ||
| 441 : | function GenID( $foo1=null, $foo2=null ) { | ||
| 442 : | return '0'; | ||
| 443 : | } | ||
| 444 : | /** | ||
| 445 : | * @return array A list of all the tables in the database | ||
| 446 : | */ | ||
| 447 : | function getTableList() { | ||
| 448 : | $this->setQuery( 'SHOW tables' ); | ||
| 449 : | $this->query(); | ||
| 450 : | return $this->loadResultArray(); | ||
| 451 : | } | ||
| 452 : | /** | ||
| 453 : | * @param array A list of table names | ||
| 454 : | * @return array A list the create SQL for the tables | ||
| 455 : | */ | ||
| 456 : | function getTableCreate( $tables ) { | ||
| 457 : | $result = array(); | ||
| 458 : | |||
| 459 : | foreach ($tables as $tblval) { | ||
| 460 : | $this->setQuery( 'SHOW CREATE table ' . $tblval ); | ||
| 461 : | $this->query(); | ||
| 462 : | $result[$tblval] = $this->loadResultArray( 1 ); | ||
| 463 : | } | ||
| 464 : | |||
| 465 : | return $result; | ||
| 466 : | } | ||
| 467 : | /** | ||
| 468 : | * @param array A list of table names | ||
| 469 : | * @return array An array of fields by table | ||
| 470 : | */ | ||
| 471 : | function getTableFields( $tables ) { | ||
| 472 : | $result = array(); | ||
| 473 : | |||
| 474 : | foreach ($tables as $tblval) { | ||
| 475 : | $this->setQuery( 'SHOW FIELDS FROM ' . $tblval ); | ||
| 476 : | $this->query(); | ||
| 477 : | $fields = $this->loadObjectList(); | ||
| 478 : | foreach ($fields as $field) { | ||
| 479 : | $result[$tblval][$field->Field] = preg_replace("/[(0-9)]/",'', $field->Type ); | ||
| 480 : | } | ||
| 481 : | } | ||
| 482 : | |||
| 483 : | return $result; | ||
| 484 : | } | ||
| 485 : | |||
| 486 : | function displayLogged () { | ||
| 487 : | echo count($this->_log).' queries executed'; | ||
| 488 : | echo '<pre>'; | ||
| 489 : | foreach ($this->_log as $k=>$sql) { | ||
| 490 : | echo $k+1 . "\n" . $sql . '<hr />'; | ||
| 491 : | } | ||
| 492 : | } | ||
| 493 : | |||
| 494 : | /* Helper method - maybe should go into database itself */ | ||
| 495 : | function doSQL ($sql) { | ||
| 496 : | $this->setQuery($sql); | ||
| 497 : | if (!$this->query()) { | ||
| 498 : | echo "<script> alert('".$this->getErrorMsg()."'); window.history.go(-1); </script>\n"; | ||
| 499 : | exit(); | ||
| 500 : | } | ||
| 501 : | } | ||
| 502 : | |||
| 503 : | /* Helper method - maybe could go into database itself */ | ||
| 504 : | function &doSQLget ($sql, $classname) { | ||
| 505 : | $this->setQuery($sql); | ||
| 506 : | $rows = $this->loadObjectList(); | ||
| 507 : | $target = get_class_vars($classname); | ||
| 508 : | if ($rows) { | ||
| 509 : | foreach ($rows as $row) { | ||
| 510 : | counterpoi | 213 | t $next = new $classname(0); |
| 511 : | csouza | 151 | foreach ($target as $field=>$value) { |
| 512 : | if (isset($row->$field)) $next->$field = $row->$field; | ||
| 513 : | } | ||
| 514 : | $result[] = $next; | ||
| 515 : | } | ||
| 516 : | } | ||
| 517 : | else $result = array(); | ||
| 518 : | return $result; | ||
| 519 : | } | ||
| 520 : | |||
| 521 : | |||
| 522 : | } | ||
| 523 : | |||
| 524 : | class mamboDatabase extends database { | ||
| 525 : | |||
| 526 : | function mamboDatabase () { | ||
| 527 : | $host = mamboCore::get('mosConfig_host'); | ||
| 528 : | $user = mamboCore::get('mosConfig_user'); | ||
| 529 : | $pw = mamboCore::get('mosConfig_password'); | ||
| 530 : | $db = mamboCore::get('mosConfig_db'); | ||
| 531 : | $prefix = mamboCore::get('mosConfig_dbprefix'); | ||
| 532 : | parent::database($host, $user, $pw, $db, $prefix); | ||
| 533 : | } | ||
| 534 : | |||
| 535 : | function &getInstance () { | ||
| 536 : | static $instance; | ||
| 537 : | if (!is_object($instance)) $instance = new mamboDatabase(); | ||
| 538 : | return $instance; | ||
| 539 : | } | ||
| 540 : | } | ||
| 541 : | |||
| 542 : | |||
| 543 : | /** | ||
| 544 : | * mosDBTable Abstract Class. | ||
| 545 : | * @abstract | ||
| 546 : | * @package Mambo | ||
| 547 : | * @subpackage Database | ||
| 548 : | * | ||
| 549 : | * Parent classes to all database derived objects. Customisation will generally | ||
| 550 : | * not involve tampering with this object. | ||
| 551 : | * @package Mambo | ||
| 552 : | * @author Andrew Eddie <eddieajau@users.sourceforge.net | ||
| 553 : | */ | ||
| 554 : | class mosDBTable { | ||
| 555 : | /** @var string Name of the table in the db schema relating to child class */ | ||
| 556 : | var $_tbl = ''; | ||
| 557 : | /** @var string Name of the primary key field in the table */ | ||
| 558 : | var $_tbl_key = ''; | ||
| 559 : | /** @var string Error message */ | ||
| 560 : | var $_error = ''; | ||
| 561 : | /** @var mosDatabase Database connector */ | ||
| 562 : | var $_db = null; | ||
| 563 : | |||
| 564 : | /** | ||
| 565 : | * Object constructor to set table and key field | ||
| 566 : | * | ||
| 567 : | * Can be overloaded/supplemented by the child class | ||
| 568 : | * @param string $table name of the table in the db schema relating to child class | ||
| 569 : | * @param string $key name of the primary key field in the table | ||
| 570 : | */ | ||
| 571 : | function mosDBTable( $table, $key, &$db ) { | ||
| 572 : | $this->_tbl = $table; | ||
| 573 : | $this->_tbl_key = $key; | ||
| 574 : | $this->_db =& $db; | ||
| 575 : | } | ||
| 576 : | |||
| 577 : | /** | ||
| 578 : | * @return bool True if DB query failed. Sets the error message | ||
| 579 : | */ | ||
| 580 : | function queryTestFailure () { | ||
| 581 : | if ($this->_db->query()) return false; | ||
| 582 : | $this->_error = $this->_db->getErrorMsg(); | ||
| 583 : | return true; | ||
| 584 : | } | ||
| 585 : | /** | ||
| 586 : | * Filters public properties | ||
| 587 : | * @access protected | ||
| 588 : | * @param array List of fields to ignore | ||
| 589 : | */ | ||
| 590 : | function filter( $ignoreList=null ) { | ||
| 591 : | $callcheck = array('InputFilter', 'process'); | ||
| 592 : | csouza | 232 | if (!is_callable($callcheck)) require_once(mamboCore::get('mosConfig_absolute_path').'/includes/phpInputFilter/class.inputfilter.php'); |
| 593 : | csouza | 151 | // specific filters |
| 594 : | $iFilter =& new InputFilter(); | ||
| 595 : | if (is_array($ignoreList)) foreach ($this->getPublicProperties() as $k) { | ||
| 596 : | if (!in_array($k, $ignoreList)) $this->$k = $iFilter->process($this->$k); | ||
| 597 : | } | ||
| 598 : | else foreach ($this->getPublicProperties() as $k) $this->$k = $iFilter->process($this->$k); | ||
| 599 : | } | ||
| 600 : | /** | ||
| 601 : | * @return string Returns the error message | ||
| 602 : | */ | ||
| 603 : | function getError() { | ||
| 604 : | return $this->_error; | ||
| 605 : | } | ||
| 606 : | /** | ||
| 607 : | * Gets the value of the class variable | ||
| 608 : | * @param string The name of the class variable | ||
| 609 : | * @return mixed The value of the class var (or null if no var of that name exists) | ||
| 610 : | */ | ||
| 611 : | function get( $_property ) { | ||
| 612 : | if(isset( $this->$_property )) return $this->$_property; | ||
| 613 : | else return null; | ||
| 614 : | } | ||
| 615 : | /** | ||
| 616 : | * Returns an array of public properties | ||
| 617 : | * @return array | ||
| 618 : | */ | ||
| 619 : | function getPublicProperties() { | ||
| 620 : | static $cache = null; | ||
| 621 : | if (is_null( $cache )) { | ||
| 622 : | $cache = array(); | ||
| 623 : | foreach (get_class_vars( get_class( $this ) ) as $key=>$val) { | ||
| 624 : | if (substr( $key, 0, 1 ) != '_') { | ||
| 625 : | $cache[] = $key; | ||
| 626 : | } | ||
| 627 : | } | ||
| 628 : | } | ||
| 629 : | return $cache; | ||
| 630 : | } | ||
| 631 : | /** | ||
| 632 : | * Checks if this object lacks the property given by the parameter | ||
| 633 : | * @param string The name of the property | ||
| 634 : | * @return bool | ||
| 635 : | */ | ||
| 636 : | function lacks( $property ) { | ||
| 637 : | $thisclass = strtolower(get_class($this)); | ||
| 638 : | if (!array_key_exists( $property, get_class_vars($thisclass) )) { | ||
| 639 : | $this->_error = "WARNING: $thisclass does not support $property."; | ||
| 640 : | return true; | ||
| 641 : | } | ||
| 642 : | return false; | ||
| 643 : | } | ||
| 644 : | /** | ||
| 645 : | * Set the value of the class variable | ||
| 646 : | * @param string The name of the class variable | ||
| 647 : | * @param mixed The value to assign to the variable | ||
| 648 : | */ | ||
| 649 : | function set( $_property, $_value ) { | ||
| 650 : | $this->$_property = $_value; | ||
| 651 : | } | ||
| 652 : | /** | ||
| 653 : | * binds a named array/hash to this object | ||
| 654 : | * | ||
| 655 : | * can be overloaded/supplemented by the child class | ||
| 656 : | * @param array $hash named array | ||
| 657 : | * @return null|string null is operation was satisfactory, otherwise returns an error | ||
| 658 : | */ | ||
| 659 : | function bind( $array, $ignore="" ) { | ||
| 660 : | counterpoi | 238 | t $database =& mamboDatabase::getInstance(); |
| 661 : | csouza | 151 | if (is_array($array)) return $database->mosBindArrayToObject($array, $this, $ignore); |
| 662 : | $this->_error = strtolower(get_class( $this ))."::bind failed."; | ||
| 663 : | return false; | ||
| 664 : | } | ||
| 665 : | |||
| 666 : | /** | ||
| 667 : | * binds an array/hash to this object | ||
| 668 : | * @param int $oid optional argument, if not specifed then the value of current key is used | ||
| 669 : | * @return any result from the database operation | ||
| 670 : | */ | ||
| 671 : | function load( $oid=null ) { | ||
| 672 : | $k = $this->_tbl_key; | ||
| 673 : | if ($oid !== null) $this->$k = $oid; | ||
| 674 : | if ($this->$k === null) return false; | ||
| 675 : | $this->_db->setQuery("SELECT * FROM $this->_tbl WHERE $this->_tbl_key='".$this->$k."'" ); | ||
| 676 : | return $this->_db->loadObject($this); | ||
| 677 : | } | ||
| 678 : | |||
| 679 : | /** | ||
| 680 : | * generic check method | ||
| 681 : | * | ||
| 682 : | * can be overloaded/supplemented by the child class | ||
| 683 : | * @return boolean True if the object is ok | ||
| 684 : | */ | ||
| 685 : | function check() { | ||
| 686 : | return true; | ||
| 687 : | } | ||
| 688 : | |||
| 689 : | /** | ||
| 690 : | * Inserts a new row if id is zero or updates an existing row in the database table | ||
| 691 : | * | ||
| 692 : | * Can be overloaded/supplemented by the child class | ||
| 693 : | * @param boolean If false, null object variables are not updated | ||
| 694 : | * @return null|string null if successful otherwise returns and error message | ||
| 695 : | */ | ||
| 696 : | function store( $updateNulls=false ) { | ||
| 697 : | $k = $this->_tbl_key; | ||
| 698 : | global $migrate; | ||
| 699 : | if( $this->$k && !$migrate) $ret = $this->_db->updateObject( $this->_tbl, $this, $this->_tbl_key, $updateNulls ); | ||
| 700 : | else $ret = $this->_db->insertObject( $this->_tbl, $this, $this->_tbl_key ); | ||
| 701 : | if( !$ret ) { | ||
| 702 : | $this->_error = strtolower(get_class( $this ))."::store failed <br />" . $this->_db->getErrorMsg(); | ||
| 703 : | return false; | ||
| 704 : | } else return true; | ||
| 705 : | } | ||
| 706 : | |||
| 707 : | /** | ||
| 708 : | * Determine from the direction parameter which operator and order to use in SQL | ||
| 709 : | */ | ||
| 710 : | function orderCodes ($direction) { | ||
| 711 : | if ($direction < 0) { | ||
| 712 : | $relation = '<'; | ||
| 713 : | $ordering = ' ORDER BY ordering DESC'; | ||
| 714 : | } | ||
| 715 : | elseif ($direction > 0) { | ||
| 716 : | $relation = '>'; | ||
| 717 : | $ordering = ' ORDER BY ordering ASC'; | ||
| 718 : | } | ||
| 719 : | else { | ||
| 720 : | $relation = '='; | ||
| 721 : | $ordering = ''; | ||
| 722 : | } | ||
| 723 : | return array ($relation, $ordering); | ||
| 724 : | } | ||
| 725 : | /** | ||
| 726 : | */ | ||
| 727 : | function move( $direction, $where='' ) { | ||
| 728 : | $k = $this->_tbl_key; | ||
| 729 : | list ($relation, $ordering) = $this->orderCodes($direction); | ||
| 730 : | $sql = "SELECT $this->_tbl_key, ordering FROM $this->_tbl WHERE ordering $relation $this->ordering"; | ||
| 731 : | $sql .= ($where ? "\n AND $where" : '').$ordering.' LIMIT 1'; | ||
| 732 : | $this->_db->setQuery( $sql ); | ||
| 733 : | $row = null; | ||
| 734 : | if ($this->_db->loadObject($row)) { | ||
| 735 : | $sql = "UPDATE $this->_tbl SET ordering='$row->ordering' WHERE $this->_tbl_key='".$this->$k."'"; | ||
| 736 : | $this->_db->doSQL($sql); | ||
| 737 : | } | ||
| 738 : | $sql = "UPDATE $this->_tbl SET ordering='$this->ordering' WHERE $this->_tbl_key='".$row->$k."'"; | ||
| 739 : | if ($row) $this->ordering = $row->ordering; | ||
| 740 : | $this->_db->doSQL($sql); | ||
| 741 : | } | ||
| 742 : | /** | ||
| 743 : | * Compacts the ordering sequence of the selected records | ||
| 744 : | * @param string Additional where query to limit ordering to a particular subset of records | ||
| 745 : | */ | ||
| 746 : | function updateOrder( $where='' ) { | ||
| 747 : | if ($this->lacks('ordering')) return false; | ||
| 748 : | $k = $this->_tbl_key; | ||
| 749 : | if ($this->_tbl == "#__content_frontpage") $order2 = ", content_id DESC"; | ||
| 750 : | else $order2 = ""; | ||
| 751 : | |||
| 752 : | $sql = "SELECT $k, ordering FROM $this->_tbl " | ||
| 753 : | . ($where ? "\nWHERE $where" : '') | ||
| 754 : | . "\nORDER BY ordering$order2"; | ||
| 755 : | $this->_db->setQuery($sql); | ||
| 756 : | if (!$rows = $this->_db->loadObjectList()) { | ||
| 757 : | $this->_error = $this->_db->getErrorMsg(); | ||
| 758 : | return false; | ||
| 759 : | } | ||
| 760 : | $i = 1; | ||
| 761 : | foreach ($rows as $row) { | ||
| 762 : | counterpoi | 287 | t $sql = "UPDATE $this->_tbl SET ordering=$i WHERE $k = ".$row->id; |
| 763 : | csouza | 151 | $this->_db->doSQL($sql); |
| 764 : | $i++; | ||
| 765 : | } | ||
| 766 : | return true; | ||
| 767 : | } | ||
| 768 : | |||
| 769 : | /** | ||
| 770 : | * Default delete method | ||
| 771 : | * | ||
| 772 : | * can be overloaded/supplemented by the child class | ||
| 773 : | * @return true if successful otherwise returns and error message | ||
| 774 : | */ | ||
| 775 : | function delete( $oid=null ) { | ||
| 776 : | $k = $this->_tbl_key; | ||
| 777 : | if ($oid) $this->$k = intval( $oid ); | ||
| 778 : | $this->_db->setQuery( "DELETE FROM $this->_tbl WHERE $this->_tbl_key = '".$this->$k."'" ); | ||
| 779 : | if ($this->queryTestFailure()) return false; | ||
| 780 : | return true; | ||
| 781 : | } | ||
| 782 : | |||
| 783 : | function checkout( $who, $oid=null ) { | ||
| 784 : | if ($this->lacks('checked_out')) return false; | ||
| 785 : | $k = $this->_tbl_key; | ||
| 786 : | if ($oid !== null) $this->$k = $oid; | ||
| 787 : | $time = date( "%Y-%m-%d H:i:s" ); | ||
| 788 : | if (intval( $who )) { | ||
| 789 : | // new way of storing editor, by id | ||
| 790 : | $this->_db->setQuery( "UPDATE $this->_tbl" | ||
| 791 : | . "\nSET checked_out='$who', checked_out_time='$time'" | ||
| 792 : | . "\nWHERE $this->_tbl_key='".$this->$k."'" | ||
| 793 : | ); | ||
| 794 : | } else { | ||
| 795 : | // old way of storing editor, by name | ||
| 796 : | $this->_db->setQuery( "UPDATE $this->_tbl" | ||
| 797 : | . "\nSET checked_out='1', checked_out_time='$time', editor='".$who."' " | ||
| 798 : | . "\nWHERE $this->_tbl_key='".$this->$k."'" | ||
| 799 : | ); | ||
| 800 : | } | ||
| 801 : | return $this->_db->query(); | ||
| 802 : | } | ||
| 803 : | |||
| 804 : | function checkin( $oid=null ) { | ||
| 805 : | if ($this->lacks('checked_out')) return false; | ||
| 806 : | $k = $this->_tbl_key; | ||
| 807 : | if ($oid !== null) $this->$k = $oid; | ||
| 808 : | $time = date("H:i:s"); | ||
| 809 : | $this->_db->setQuery( "UPDATE $this->_tbl" | ||
| 810 : | . "\nSET checked_out='0', checked_out_time='0000-00-00 00:00:00'" | ||
| 811 : | . "\nWHERE $this->_tbl_key='".$this->$k."'" | ||
| 812 : | ); | ||
| 813 : | return $this->_db->query(); | ||
| 814 : | } | ||
| 815 : | |||
| 816 : | function hit( $oid=null ) { | ||
| 817 : | $k = $this->_tbl_key; | ||
| 818 : | if ($oid !== null) $this->$k = intval( $oid ); | ||
| 819 : | $key = $this->$k; | ||
| 820 : | $this->_db->setQuery( "UPDATE $this->_tbl SET hits=(hits+1) WHERE $this->_tbl_key='$key'" ); | ||
| 821 : | $this->_db->query(); | ||
| 822 : | |||
| 823 : | if (mamboCore::get('mosConfig_enable_log_items')) { | ||
| 824 : | $now = date( "Y-m-d" ); | ||
| 825 : | $this->_db->setQuery( "SELECT hits" | ||
| 826 : | . "\nFROM #__core_log_items" | ||
| 827 : | . "\nWHERE time_stamp='$now' AND item_table='$this->_tbl' AND item_id='$key'" | ||
| 828 : | ); | ||
| 829 : | $hits = intval( $this->_db->loadResult() ); | ||
| 830 : | if ($hits) $this->_db->setQuery( "UPDATE #__core_log_items SET hits=(hits+1)" | ||
| 831 : | . "\nWHERE time_stamp='$now' AND item_table='$this->_tbl' AND item_id='".$this->$k."'" | ||
| 832 : | ); | ||
| 833 : | else $this->_db->setQuery( "INSERT INTO #__core_log_items VALUES" | ||
| 834 : | . "\n('$now','$this->_tbl','".$this->$k."','1')" | ||
| 835 : | ); | ||
| 836 : | $this->_db->query(); | ||
| 837 : | } | ||
| 838 : | } | ||
| 839 : | |||
| 840 : | /** | ||
| 841 : | * Generic save function | ||
| 842 : | * @param array Source array for binding to class vars | ||
| 843 : | * @param string Filter for the order updating | ||
| 844 : | * @returns TRUE if completely successful, FALSE if partially or not succesful. | ||
| 845 : | */ | ||
| 846 : | function save( $source, $order_filter ) { | ||
| 847 : | if (!$this->bind($_POST) OR !$this->check() OR !$this->store()OR !$this->checkin()) return false; | ||
| 848 : | $filter_value = $this->$order_filter; | ||
| 849 : | $this->updateOrder( $order_filter ? "`$order_filter`='$filter_value'" : "" ); | ||
| 850 : | $this->_error = ''; | ||
| 851 : | return true; | ||
| 852 : | } | ||
| 853 : | |||
| 854 : | /** | ||
| 855 : | * Generic Publish/Unpublish function | ||
| 856 : | * @param array An array of id numbers | ||
| 857 : | * @param integer 0 if unpublishing, 1 if publishing | ||
| 858 : | * @param integer The id of the user performnig the operation | ||
| 859 : | */ | ||
| 860 : | function publish_array( $cid=null, $publish=1, $myid=0 ) { | ||
| 861 : | if (!is_array( $cid ) OR count( $cid ) < 1) { | ||
| 862 : | $this->_error = "No items selected."; | ||
| 863 : | return false; | ||
| 864 : | } | ||
| 865 : | $cids = implode( ',', $cid ); | ||
| 866 : | $this->_db->setQuery( "UPDATE $this->_tbl SET published='$publish'" | ||
| 867 : | . "\nWHERE $this->_tbl_key IN ($cids) AND (checked_out=0 OR checked_out='$myid')" | ||
| 868 : | ); | ||
| 869 : | if ($this->queryTestFailure()) return false; | ||
| 870 : | if (count( $cid ) == 1) $this->checkin( $cid[0] ); | ||
| 871 : | return true; | ||
| 872 : | } | ||
| 873 : | |||
| 874 : | /** | ||
| 875 : | * Export item list to xml | ||
| 876 : | * @param boolean Map foreign keys to text values | ||
| 877 : | */ | ||
| 878 : | function toXML( $mapKeysToText=false ) { | ||
| 879 : | $xml = '<record table="' . $this->_tbl . '"'; | ||
| 880 : | if ($mapKeysToText) $xml .= ' mapkeystotext="true"'; | ||
| 881 : | $xml .= '>'; | ||
| 882 : | foreach (get_object_vars($this) as $k => $v) { | ||
| 883 : | if ($v === null OR is_array($v) OR is_object($v)) continue; | ||
| 884 : | if ($k[0] == '_') continue; // internal field | ||
| 885 : | $xml .= '<' . $k . '><![CDATA[' . $v . ']]></' . $k . '>'; | ||
| 886 : | } | ||
| 887 : | $xml .= '</record>'; | ||
| 888 : | return $xml; | ||
| 889 : | } | ||
| 890 : | } | ||
| 891 : | |||
| 892 : | /** | ||
| 893 : | * Abstract class for classes where the objects of the class can be relatively easily | ||
| 894 : | * stored in a single database table. Can usually be adapted to more complex cases. | ||
| 895 : | * Requires child classes to implement: tableName(), notSQL(). | ||
| 896 : | * tableName() must return the name of the database table, using #__ in the usual Mambo way | ||
| 897 : | * notSQL() must return an array of strings, where each string is the name of a | ||
| 898 : | * variable that is NOT in the database table, or is not written explicitly, | ||
| 899 : | * e.g. the auto-increment key. If this is the ONLY non-SQL field, then the | ||
| 900 : | * child class need not implement it, as that it is already in the abstract class. | ||
| 901 : | * Child classes may implement timeStampField, in which case it must return the name | ||
| 902 : | * of a field that will have a timestamp placed in it whenever the DB is written. | ||
| 903 : | */ | ||
| 904 : | |||
| 905 : | class mosDBTableEntry { | ||
| 906 : | /** @var int ID for file record in database */ | ||
| 907 : | var $id = 0; | ||
| 908 : | var $database = ''; | ||
| 909 : | |||
| 910 : | function mosDBTableEntry () { | ||
| 911 : | die ('Cannot instantiate mosDBTableEntry'); | ||
| 912 : | } | ||
| 913 : | |||
| 914 : | function getDatabase () { | ||
| 915 : | counterpoi | 238 | t if (!is_object($this->database)) $this->database =& mamboDatabase::getInstance(); |
| 916 : | csouza | 151 | return $this->database; |
| 917 : | } | ||
| 918 : | |||
| 919 : | /* Stores all POST data where the name matches an object variable name */ | ||
| 920 : | function addPostData () { | ||
| 921 : | foreach (get_class_vars(get_class($this)) as $field=>$value) { | ||
| 922 : | if ($field!='id' AND isset($_POST[$field])) { | ||
| 923 : | $this->$field = trim($_POST[$field]); | ||
| 924 : | } | ||
| 925 : | } | ||
| 926 : | $this->forceBools(); | ||
| 927 : | } | ||
| 928 : | |||
| 929 : | /* Provided in case child class does not implement it. Can force any values */ | ||
| 930 : | /* within some limited range. In particular, can force bools to be 0 or 1 */ | ||
| 931 : | function forceBools () { | ||
| 932 : | return; | ||
| 933 : | } | ||
| 934 : | |||
| 935 : | /* Updates an existing DB entry with the object's current values */ | ||
| 936 : | function updateObjectDB () { | ||
| 937 : | $this->prepareValues(); | ||
| 938 : | $database = $this->getDatabase(); | ||
| 939 : | $database->doSQL($this->updateSQL()); | ||
| 940 : | } | ||
| 941 : | |||
| 942 : | /* Deletes the current object from the DB */ | ||
| 943 : | function delete () { | ||
| 944 : | $table = $this->tableName(); | ||
| 945 : | $sql = "DELETE FROM $table WHERE id=$this->id"; | ||
| 946 : | $database = $this->getDatabase(); | ||
| 947 : | $database->doSQL($sql); | ||
| 948 : | } | ||
| 949 : | |||
| 950 : | /* Provided in case the child class does not provide a method for timeStampField */ | ||
| 951 : | function timeStampField () { | ||
| 952 : | return ''; | ||
| 953 : | } | ||
| 954 : | |||
| 955 : | /* Provides SQL for updating the DB with the contents of the current object */ | ||
| 956 : | function updateSQL () { | ||
| 957 : | $tabname = $this->tableName(); | ||
| 958 : | $sql = "UPDATE $tabname SET %s WHERE id=$this->id"; | ||
| 959 : | $exclude = $this->notSQL(); | ||
| 960 : | foreach (get_class_vars(get_class($this)) as $field=>$value) { | ||
| 961 : | if (!in_array($field,$exclude)) $setter[] = $field."='".$this->$field."'"; | ||
| 962 : | } | ||
| 963 : | $timestamp = $this->timeStampField(); | ||
| 964 : | if ($timestamp) $setter[] = $timestamp."='".date('Y-m-d H:i:s')."'"; | ||
| 965 : | return sprintf($sql,implode(',', $setter)); | ||
| 966 : | } | ||
| 967 : | |||
| 968 : | /* Default method for identifying fields not to be written to the DB */ | ||
| 969 : | /* The child classes may override this and return more items in the array */ | ||
| 970 : | function notSQL () { | ||
| 971 : | return array ('id'); | ||
| 972 : | } | ||
| 973 : | |||
| 974 : | /* Provides SQL to insert the current object into the DB */ | ||
| 975 : | function insertSQL () { | ||
| 976 : | $tabname = $this->tableName(); | ||
| 977 : | $sql = "INSERT INTO $tabname (%s) VALUES (%s)"; | ||
| 978 : | $exclude = $this->notSQL(); | ||
| 979 : | foreach (get_class_vars(get_class($this)) as $field=>$value) { | ||
| 980 : | if (!in_array($field,$exclude)) { | ||
| 981 : | $infields[] = $field; | ||
| 982 : | $values[] = "'".$this->$field."'"; | ||
| 983 : | } | ||
| 984 : | } | ||
| 985 : | $timestamp = $this->timeStampField(); | ||
| 986 : | if ($timestamp) { | ||
| 987 : | $infields[] = $timestamp; | ||
| 988 : | $values[] = "'".date('Y-m-d H:i:s')."'"; | ||
| 989 : | } | ||
| 990 : | return sprintf($sql, implode(',', $infields), implode(',', $values)); | ||
| 991 : | } | ||
| 992 : | |||
| 993 : | /* Copies any matching fields from some arbitrary object into the current object */ | ||
| 994 : | function setValues (&$anObject) { | ||
| 995 : | foreach (get_class_vars(get_class($this)) as $field=>$value) { | ||
| 996 : | if ($field != 'id' AND isset($anObject->$field)) $this->$field = $anObject->$field; | ||
| 997 : | } | ||
| 998 : | } | ||
| 999 : | |||
| 1000 : | /* Ensures values can safely be written to DB; assumes magic quotes forced off */ | ||
| 1001 : | function prepareValues () { | ||
| 1002 : | $database = $this->getDatabase(); | ||
| 1003 : | foreach (get_class_vars(get_class($this)) as $field=>$value) { | ||
| 1004 : | if (!is_numeric($this->$field)) $this->$field = $database->getEscaped($this->$field); | ||
| 1005 : | } | ||
| 1006 : | } | ||
| 1007 : | |||
| 1008 : | /* Takes some arbitrary SELECT type SQL and places the first or only result into the current object */ | ||
| 1009 : | function readDataBase($sql) { | ||
| 1010 : | $database = $this->getDatabase(); | ||
| 1011 : | $database->setQuery( $sql ); | ||
| 1012 : | if (!$database->loadObject($this)) $this->id = 0; | ||
| 1013 : | } | ||
| 1014 : | |||
| 1015 : | } | ||
| 1016 : | |||
| 1017 : | ?> |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |
Web Hosting provided by Network Redux.

