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

