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

