| 30 |
var $_debug=0; |
var $_debug=0; |
| 31 |
/** @var array A log of queries */ |
/** @var array A log of queries */ |
| 32 |
var $_log=array(); |
var $_log=array(); |
| 33 |
|
/** @var string Null date */ |
| 34 |
|
var $_null_date='0000-00-00 00:00:00'; |
| 35 |
|
|
| 36 |
/** |
/** |
| 37 |
* Database object constructor |
* Database object constructor |
| 56 |
include $basePath . '/../offline.php'; |
include $basePath . '/../offline.php'; |
| 57 |
exit(); |
exit(); |
| 58 |
} |
} |
| 59 |
|
|
| 60 |
|
function getNullDate () { |
| 61 |
|
return $this->_null_date; |
| 62 |
|
} |
| 63 |
/** |
/** |
| 64 |
* @param int |
* @param int |
| 65 |
*/ |
*/ |
| 223 |
return "<div style=\"background-color:#FFFFCC\" align=\"left\">$buf</div>"; |
return "<div style=\"background-color:#FFFFCC\" align=\"left\">$buf</div>"; |
| 224 |
} |
} |
| 225 |
/** |
/** |
| 226 |
* @return int The number of rows returned from the most recent query. |
* @return int The number of rows returned from the most recent query - SELECT only |
| 227 |
*/ |
*/ |
| 228 |
function getNumRows( $cur=null ) { |
function getNumRows( $cur=null ) { |
| 229 |
return mysql_num_rows( $cur ? $cur : $this->_cursor ); |
return mysql_num_rows( $cur ? $cur : $this->_cursor ); |
| 230 |
} |
} |
| 231 |
|
|
| 232 |
/** |
/** |
| 233 |
|
* @return int The number of rows affected by the most recent query - INSERT, UPDATE, DELETE |
| 234 |
|
*/ |
| 235 |
|
function getAffectedRows( ) { |
| 236 |
|
return mysql_affected_rows( $this->_resource ); |
| 237 |
|
} |
| 238 |
|
|
| 239 |
|
/** |
| 240 |
* Load an array of retrieved database objects or values |
* Load an array of retrieved database objects or values |
| 241 |
* @param int Database cursor |
* @param int Database cursor |
| 242 |
* @param string The field name of a primary key |
* @param string The field name of a primary key |
| 560 |
} |
} |
| 561 |
} |
} |
| 562 |
|
|
|
|
|
| 563 |
/** |
/** |
| 564 |
* mosDBTable Abstract Class. |
* mosDBAbstractRow Abstract Class. |
| 565 |
* @abstract |
* @abstract |
| 566 |
* @package Mambo |
* @package Mambo |
| 567 |
* @subpackage Database |
* @subpackage Database |
| 569 |
* Parent classes to all database derived objects. Customisation will generally |
* Parent classes to all database derived objects. Customisation will generally |
| 570 |
* not involve tampering with this object. |
* not involve tampering with this object. |
| 571 |
* @package Mambo |
* @package Mambo |
| 572 |
* @author Andrew Eddie <eddieajau@users.sourceforge.net |
* @author Martin Brampton counterpoint@mambo-foundation.org |
| 573 |
*/ |
*/ |
| 574 |
class mosDBTable { |
class mosDBAbstractRow { |
| 575 |
/** @var string Name of the table in the db schema relating to child class */ |
/** @var string Name of the table in the db schema relating to child class */ |
| 576 |
var $_tbl = ''; |
var $_tbl = ''; |
| 577 |
/** @var string Name of the primary key field in the table */ |
/** @var string Name of the primary key field in the table */ |
| 578 |
var $_tbl_key = ''; |
var $_tbl_key = ''; |
| 579 |
/** @var string Error message */ |
/** @var string Error message */ |
| 580 |
var $_error = ''; |
var $_error = ''; |
|
/** @var mosDatabase Database connector */ |
|
|
var $_db = null; |
|
| 581 |
|
|
| 582 |
/** |
/** |
| 583 |
* Object constructor to set table and key field |
* Object constructor to set table and key field |
| 586 |
* @param string $table name of the table in the db schema relating to child class |
* @param string $table name of the table in the db schema relating to child class |
| 587 |
* @param string $key name of the primary key field in the table |
* @param string $key name of the primary key field in the table |
| 588 |
*/ |
*/ |
| 589 |
function mosDBTable( $table, $key, &$db ) { |
function mosDBAbstractRow ($table='', $keyname='id', $db='') { |
| 590 |
$this->_tbl = $table; |
if ($table) $this->_tbl = $table; |
| 591 |
$this->_tbl_key = $key; |
else $this->_tbl = $this->tableName(); |
| 592 |
$this->_db =& $db; |
$this->_tbl_key = $keyname; |
| 593 |
|
if (is_object($db)) $this->_db = $db; |
| 594 |
|
} |
| 595 |
|
|
| 596 |
|
/** |
| 597 |
|
* generic check method |
| 598 |
|
* |
| 599 |
|
* can be overloaded/supplemented by the child class |
| 600 |
|
* @return boolean True if the object is ok |
| 601 |
|
*/ |
| 602 |
|
function check() { |
| 603 |
|
return true; |
| 604 |
} |
} |
| 605 |
|
|
| 606 |
/** |
/** |
| 607 |
|
* Checks if this object lacks the property given by the parameter |
| 608 |
|
* @param string The name of the property |
| 609 |
|
* @return bool |
| 610 |
|
*/ |
| 611 |
|
function lacks( $property ) { |
| 612 |
|
$thisclass = get_class($this); |
| 613 |
|
if (array_key_exists( $property, get_class_vars($thisclass) )) return false; |
| 614 |
|
$this->_error = T_(sprintf('WARNING: %s does not support %s.', $thisclass, $property)); |
| 615 |
|
return true; |
| 616 |
|
} |
| 617 |
|
|
| 618 |
|
/** |
| 619 |
|
/* Move a database row object up or down through the ordering |
| 620 |
|
/* @param int positive to move up, negative to move down |
| 621 |
|
/* @param string Additional conditions on the WHERE clause to limit the effect |
| 622 |
|
*/ |
| 623 |
|
function move( $direction, $where='' ) { |
| 624 |
|
$compops = array (-1 => '<', 0 => '=', 1 => '>'); |
| 625 |
|
$relation = $compops[($direction>0)-($direction<0)]; |
| 626 |
|
$ordering = ($relation == '<' ? 'DESC' : 'ASC'); |
| 627 |
|
$k = $this->_tbl_key; |
| 628 |
|
$o1 = $this->ordering; |
| 629 |
|
$k1 = $this->$k; |
| 630 |
|
$database = isset($this->_db) ? $this->_db : mamboDatabase::getInstance(); |
| 631 |
|
$sql = "SELECT $k, ordering FROM $this->_tbl WHERE ordering $relation $o1"; |
| 632 |
|
$sql .= ($where ? "\n AND $where" : '').' ORDER BY ordering '.$ordering.' LIMIT 1'; |
| 633 |
|
$database->setQuery( $sql ); |
| 634 |
|
if ($database->loadObject($row)) { |
| 635 |
|
$o2 = $row->ordering; |
| 636 |
|
$k2 = $row->$k; |
| 637 |
|
$sql = "UPDATE $this->_tbl SET ordering = (ordering=$o1)*$o2 + (ordering=$o2)*$o1 WHERE $k = $k1 OR $k = $k2"; |
| 638 |
|
$database->doSQL($sql); |
| 639 |
|
} |
| 640 |
|
} |
| 641 |
|
/** |
| 642 |
|
* Compacts the ordering sequence of the selected records |
| 643 |
|
* @param string Additional conditions on WHERE clause to limit ordering to a particular subset of records |
| 644 |
|
*/ |
| 645 |
|
function updateOrder( $where='', $cfid=null, $order=null ) { |
| 646 |
|
if ($this->lacks('ordering')) return false; |
| 647 |
|
$k = $this->_tbl_key; |
| 648 |
|
if ($this->_tbl == "#__content_frontpage") $order2 = ", content_id DESC"; |
| 649 |
|
else $order2 = ""; |
| 650 |
|
|
| 651 |
|
$database = isset($this->_db) ? $this->_db : mamboDatabase::getInstance(); |
| 652 |
|
|
| 653 |
|
if (!is_null($cfid) AND !is_null($order)) { |
| 654 |
|
foreach ($cfid as $i=>$id) { |
| 655 |
|
$o = intval($order[$i]); |
| 656 |
|
$set[] = "(id=$id)*$o"; |
| 657 |
|
} |
| 658 |
|
$sql = "UPDATE $this->_tbl SET ordering = ".implode(' + ', $set).' WHERE id IN ('.implode(',', $cfid).')'; |
| 659 |
|
$database->doSQL($sql); |
| 660 |
|
} |
| 661 |
|
|
| 662 |
|
$sql = "SELECT $k, ordering FROM $this->_tbl " |
| 663 |
|
. ($where ? "\nWHERE $where" : '') |
| 664 |
|
. "\nORDER BY ordering$order2"; |
| 665 |
|
$database->setQuery($sql); |
| 666 |
|
if (!$rows = $database->loadObjectList()) { |
| 667 |
|
$this->_error = $database->getErrorMsg(); |
| 668 |
|
return false; |
| 669 |
|
} |
| 670 |
|
$i = 1; |
| 671 |
|
foreach ($rows as $row) { |
| 672 |
|
$sql = "UPDATE $this->_tbl SET ordering=$i WHERE $k = ".$row->$k; |
| 673 |
|
$database->doSQL($sql); |
| 674 |
|
$i++; |
| 675 |
|
} |
| 676 |
|
return true; |
| 677 |
|
} |
| 678 |
|
|
| 679 |
|
} |
| 680 |
|
|
| 681 |
|
|
| 682 |
|
/** |
| 683 |
|
* mosDBTable Abstract Class. |
| 684 |
|
* @abstract |
| 685 |
|
* @package Mambo |
| 686 |
|
* @subpackage Database |
| 687 |
|
* |
| 688 |
|
* Parent classes to all database derived objects. Customisation will generally |
| 689 |
|
* not involve tampering with this object. |
| 690 |
|
* @package Mambo |
| 691 |
|
* @author Andrew Eddie <eddieajau@users.sourceforge.net |
| 692 |
|
*/ |
| 693 |
|
class mosDBTable extends mosDBAbstractRow { |
| 694 |
|
/** @var mosDatabase Database connector */ |
| 695 |
|
var $_db = null; |
| 696 |
|
|
| 697 |
|
/** |
| 698 |
* @return bool True if DB query failed. Sets the error message |
* @return bool True if DB query failed. Sets the error message |
| 699 |
*/ |
*/ |
| 700 |
function queryTestFailure () { |
function queryTestFailure () { |
| 749 |
return $cache; |
return $cache; |
| 750 |
} |
} |
| 751 |
/** |
/** |
|
* Checks if this object lacks the property given by the parameter |
|
|
* @param string The name of the property |
|
|
* @return bool |
|
|
*/ |
|
|
function lacks( $property ) { |
|
|
$thisclass = strtolower(get_class($this)); |
|
|
if (!array_key_exists( $property, get_class_vars($thisclass) )) { |
|
|
$this->_error = "WARNING: $thisclass does not support $property."; |
|
|
return true; |
|
|
} |
|
|
return false; |
|
|
} |
|
|
/** |
|
| 752 |
* Set the value of the class variable |
* Set the value of the class variable |
| 753 |
* @param string The name of the class variable |
* @param string The name of the class variable |
| 754 |
* @param mixed The value to assign to the variable |
* @param mixed The value to assign to the variable |
| 784 |
} |
} |
| 785 |
|
|
| 786 |
/** |
/** |
|
* generic check method |
|
|
* |
|
|
* can be overloaded/supplemented by the child class |
|
|
* @return boolean True if the object is ok |
|
|
*/ |
|
|
function check() { |
|
|
return true; |
|
|
} |
|
|
|
|
|
/** |
|
| 787 |
* Inserts a new row if id is zero or updates an existing row in the database table |
* Inserts a new row if id is zero or updates an existing row in the database table |
| 788 |
* |
* |
| 789 |
* Can be overloaded/supplemented by the child class |
* Can be overloaded/supplemented by the child class |
| 802 |
} |
} |
| 803 |
|
|
| 804 |
/** |
/** |
|
*/ |
|
|
function move( $direction, $where='' ) { |
|
|
$compops = array (-1 => '<', 0 => '=', 1 => '>'); |
|
|
$relation = $compops[($direction>0)-($direction<0)]; |
|
|
$ordering = $relation == '<' ? 'DESC' : 'ASC'; |
|
|
$k = $this->_tbl_key; |
|
|
$o1 = $this->ordering; |
|
|
$k1 = $this->$k; |
|
|
$sql = "SELECT $k, ordering FROM $this->_tbl WHERE ordering $relation $o1"; |
|
|
$sql .= ($where ? "\n AND $where" : '').' ORDER BY ordering '.$ordering.' LIMIT 1'; |
|
|
$this->_db->setQuery( $sql ); |
|
|
if ($this->_db->loadObject($row)) { |
|
|
$o2 = $row->ordering; |
|
|
$k2 = $row->$k; |
|
|
$sql = "UPDATE $this->_tbl SET ordering = (ordering=$o1)*$o2 + (ordering=$o2)*$o1 WHERE $k = $k1 OR $k = $k2"; |
|
|
$this->_db->doSQL($sql); |
|
|
} |
|
|
} |
|
|
/** |
|
|
* Compacts the ordering sequence of the selected records |
|
|
* @param string Additional where query to limit ordering to a particular subset of records |
|
|
*/ |
|
|
function updateOrder( $where='' ) { |
|
|
if ($this->lacks('ordering')) return false; |
|
|
$k = $this->_tbl_key; |
|
|
if ($this->_tbl == "#__content_frontpage") $order2 = ", content_id DESC"; |
|
|
else $order2 = ""; |
|
|
|
|
|
$sql = "SELECT $k, ordering FROM $this->_tbl " |
|
|
. ($where ? "\nWHERE $where" : '') |
|
|
. "\nORDER BY ordering$order2"; |
|
|
$this->_db->setQuery($sql); |
|
|
if (!$rows = $this->_db->loadObjectList()) { |
|
|
$this->_error = $this->_db->getErrorMsg(); |
|
|
return false; |
|
|
} |
|
|
$i = 1; |
|
|
foreach ($rows as $row) { |
|
|
$sql = "UPDATE $this->_tbl SET ordering=$i WHERE $k = ".$row->$k; |
|
|
$this->_db->doSQL($sql); |
|
|
$i++; |
|
|
} |
|
|
return true; |
|
|
} |
|
|
|
|
|
/** |
|
| 805 |
* Default delete method |
* Default delete method |
| 806 |
* |
* |
| 807 |
* can be overloaded/supplemented by the child class |
* can be overloaded/supplemented by the child class |
| 937 |
* of a field that will have a timestamp placed in it whenever the DB is written. |
* of a field that will have a timestamp placed in it whenever the DB is written. |
| 938 |
*/ |
*/ |
| 939 |
|
|
| 940 |
class mosDBTableEntry { |
class mosTableEntry extends mosDBAbstractRow { |
|
|
|
|
function mosDBTableEntry () { |
|
|
die ('Cannot instantiate mosDBTableEntry'); |
|
|
} |
|
| 941 |
|
|
| 942 |
/* Stores all POST data where the name matches an object variable name */ |
/* Stores all POST data where the name matches an object variable name */ |
| 943 |
function addPostData () { |
function addPostData () { |
| 944 |
foreach (get_class_vars(get_class($this)) as $field=>$value) { |
foreach (get_class_vars(get_class($this)) as $field=>$value) { |
| 945 |
if ($field!='id' AND isset($_POST[$field])) { |
if ($field!='id' AND $field[1] != '_' AND isset($_POST[$field])) { |
| 946 |
$this->$field = trim($_POST[$field]); |
$this->$field = trim($_POST[$field]); |
| 947 |
} |
} |
| 948 |
} |
} |
| 981 |
$sql = "UPDATE $tabname SET %s WHERE id=$this->id"; |
$sql = "UPDATE $tabname SET %s WHERE id=$this->id"; |
| 982 |
$exclude = $this->notSQL(); |
$exclude = $this->notSQL(); |
| 983 |
foreach (get_class_vars(get_class($this)) as $field=>$value) { |
foreach (get_class_vars(get_class($this)) as $field=>$value) { |
| 984 |
if (!in_array($field,$exclude)) $setter[] = $field."='".$this->$field."'"; |
if (!in_array($field,$exclude) AND $field[0] != '_') $setter[] = $field."='".$this->$field."'"; |
| 985 |
} |
} |
| 986 |
$timestamp = $this->timeStampField(); |
$timestamp = $this->timeStampField(); |
| 987 |
if ($timestamp) $setter[] = $timestamp."='".date('Y-m-d H:i:s')."'"; |
if ($timestamp) $setter[] = $timestamp."='".date('Y-m-d H:i:s')."'"; |
| 1000 |
$sql = "INSERT INTO $tabname (%s) VALUES (%s)"; |
$sql = "INSERT INTO $tabname (%s) VALUES (%s)"; |
| 1001 |
$exclude = $this->notSQL(); |
$exclude = $this->notSQL(); |
| 1002 |
foreach (get_class_vars(get_class($this)) as $field=>$value) { |
foreach (get_class_vars(get_class($this)) as $field=>$value) { |
| 1003 |
if (!in_array($field,$exclude)) { |
if (!in_array($field,$exclude) AND $field[0] != '_') { |
| 1004 |
$infields[] = $field; |
$infields[] = $field; |
| 1005 |
$values[] = "'".$this->$field."'"; |
$values[] = "'".$this->$field."'"; |
| 1006 |
} |
} |