Location: PHPKode > projects > Pygalle > admin/tables/table.php
<?php
/**
 * @package		Pygalle
 * @copyright	Copyright (C) 2009 Erik Finnegan. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * @author 		hide@address.com
 * Joomla! and Pygalle EVE Corporate Intranet are free software.
 * This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * See COPYRIGHT.php for copyright notices and details.
 */

// Check to ensure this file is within the rest of the framework
defined( '_JEXEC' ) or die( 'Restricted access' );

require_once( JPATH_SITE.DS.'components'.DS.'com_pygalle'.DS.'utilities'.DS.'import.php');
pyimport( 'log');
jimport( 'joomla.database.table');

/**
 * This class wraps the standard joomla table model.
 */
/**
 * @author hendrik.scheider
 *
 */
class PyTable extends JTable {
	
	protected $_log = null;
	
	protected $_foreignKey = null;
	
	private $_config = array();
	
	static $TABLE_ROW_SEPARATOR_ID = '__SEPARATOR__';
	static $TABLE_ROW_SEPARATOR_VISUAL = '==============';
	
	/**
	* @access 	protected
	*/
	function __construct( $table, $key, &$db ) {
		parent::__construct( $table, $key, $db);
		$this->_log = PyLog::getInstance();
	}
	
	/**
	 * Will preload a row from the database if configured with either 
	 * primary key or foreign key.
	 * 
	 * @see administrator/components/com_pygalle/tables/PyTable#configure()
	 */
	protected function configure( $config = array() ) {
		$this->_log->trace();
		$this->_config = $config;
		if ( is_numeric( $config[ $this->_tbl_key])) {
			return $this->load( $config[ $this->_tbl_key]);
		} elseif ( is_numeric( $config[ $this->_foreignKey])) {
			return $this->loadForeign( $config[ $this->_foreignKey]);
		}
	}
	
	/**
	 * @static (legacy)
	 * @see libraries/joomla/database/JTable#getInstance($type, $prefix, $config)
	 */
	function &getInstance( $type, $config = array() ) {
		static $instances;
		$hashSig = '';
		
		if ( !isset( $instances)) {
			$instances = array();
		}
		if ( count( $config) > 0) {
			$hashString = '';
			foreach ( $config as $k => $v) {
				$hashString .= ( $k . $v);
			}
			$hashSig = md5( $hashString);
			if ( isset( $instances[ $hashSig])) {
				$instance = $instances[ $hashSig];
				$instance->_log->trace( 'found existing instance with ' . $hashString);
				return $instances[ $hashSig];
			}
		}
		
		// only create a new instance if not previously stored
		$instance =& parent::getInstance( $type, 'PyTable', $config);
		if ( $instance->_log instanceof PyLog) {
			$instance->_log->trace( '"'.$type. '" type, with config:', $config);
		}
		$instance->configure( $config);
		if ( $hashSig) {
			$instances[ $hashSig] =& $instance;
		}
		
		return $instance;
	}
	
	function getEntriesWithColumnNotEmpty( $columnName) {
		
		$query = 'SELECT '. $this->tableObject->_tbl_key .' FROM ' . $this->tableObject->_tbl. ' AS t'
		 . ' WHERE '. $columnName .' <> ""'; 
		
		$foundEntries = $this->loadResultArray( $query);
		
		$objectArray = array();
		$callerClass = get_class( $this->tableObject);
		
		foreach ( $foundEntries as $v) {
			$object = new $callerClass( $this->db);
			$object->load( $v);
			
			$objectArray[] = $object; 
		}
		return $objectArray;
	}
	
	protected function setForeignKey( $columnName) {
		$this->_foreignKey = $columnName;
	}
	
	function loadObject( $query) {
		return $this->_loadFromDB( 'loadObject', $query);
	}
	
	function loadObjectList( $query, $key = null) {
		return $this->_loadFromDB( 'loadObjectList', $query, $key = null);
	}
	
	function loadResultArray( $query) {
		return $this->_loadFromDB( 'loadResultArray', $query);
	}
	
	function loadAssoc( $query) {
		return $this->_loadFromDB( 'loadAssoc', $query);
	}
	
	function loadAssocList( $query, $key = null) {
		return $this->_loadFromDB( 'loadAssocList', $query, $key);
	}
	
	function _loadFromDB( $retrievalMethod, $query, $key = null) {
		$_db =& $this->getDBO();
		$_db->setQuery($query);
		$_db->query();
		$this->_log->trace( $_db);
		
		if ( $_db->ErrorNo() > 0) {
			$this->tableObject->setError( $_db->getErrorMsg() . '('. $_db->getErrorNum() .')');
			return false;
		}
		
		if ( $key) {
			$result = $_db->$retrievalMethod( $key);
		} else {
			$result = $_db->$retrievalMethod();
		}
		
		return $result;
		
	}
	
	function loadForeign( $oid=null )
	{
		$column = $this->_foreignKey;

		if ($oid !== null) {
			$this->$column = $oid;
		}
		$oid = $this->$column;

		if ($oid === null) {
			return false;
		}
		$this->reset();
		
		$query = 'SELECT *'
		. ' FROM '.$this->_tbl
		. ' WHERE '.$column.' = '.$this->getDBO()->Quote($oid);
		
		if ($result = $this->loadAssoc( $query)) {
			return $this->bind($result);
		} else {
			return false;
		}
	}
	
	function store( $updateNulls = false)
	{
		$result = parent::store( $updateNulls);
		$this->_log->trace( $this->getDBO());
		return $result;
	}

	
	/**
	 * Overrides parent function and nullifies all properties for a clean reload from DB
	 * 
	 * @see libraries/joomla/database/JTable#reset()
	 */
	function reset() {

		$k = $this->_tbl_key;
		$fk = $this->_foreignKey;
		foreach ($this->getProperties() as $name => $value)
		{
			if($name != $k && $name != $fk)
			{
				$this->$name	= null;
			}
		}
	}
}

?>
Return current item: Pygalle