Location: PHPKode > projects > Pygalle > admin/tables/supply.php
<?php
/**
 * @package		Pygalle
 * @copyright	Copyright (C) 2008 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 included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

jimport( 'joomla.database.table' );
pyimport( 'models.pygalle');
pyimport( 'tables.table');

class PyTableSupply extends PyTable {
	
	var $id;
	
	var $eveTypeId;
	
	var $name;
	
	/**
	 * Price stored in 1/100 * isk
	 * Converstion is done via and from UI. Internal representation is always in isk-Cents.
	 *
	 * @var int
	 */
	var $stdPrice;

	/**
	 * Price stored in 1/100 * isk
	 * Converstion is done via and from UI. Internal representation is always in isk-Cents.
	 *
	 * @var int
	 */
	var $priceUpdate;
	
	var $ordering;
	
	/**
	 * The type must match the table suffix in singular
	 * e.g. type = ore for the table ...py_ores 
	 *
	 * @var String
	 */
	var $_type = '';
	
	/**
	 * The max age for market price until update is required
	 *
	 * @var int
	 */
	var $_maxAge = 2419200; // four weeks
	
	/**
	 * shows whether this object refers to the table as a whole
	 *
	 * @var boolean
	 */
	var $_fullTableModel = false;

	/**
	 * @access 	protected
	 * @param $type string the supply type
	 * @param supplyId int the id of the supply for this object
	 */
	function __construct( &$db, $type) {
		parent::__construct( '#__py_'.$type.'s', 'id', $db);

		$this->_log->trace('instantiating ' . get_class($this));
		$this->_type = $type;
		$this->setForeignKey( 'eveTypeId');
		
		if ( is_numeric( $eveTypeId) && $eveTypeId > 0) {
			$this->loadForeign( $eveTypeId);
		}
	}

	/**
	 * Returns the price of the mineral which this objects refers to.
	 *
	 * @return int
	 */
	function getPrice() {
		
		$k = $this->_tbl_key;
		$foreign = $this->_foreignKey;
		if ( !$this->$k && !$this->$foreign) {
			die('logical error calling supply::getPrice');
		}
				
		return $this->stdPrice;
	}
		
	function setPrice($newPrice) {
		
		$k = $this->_tbl_key;
		$foreign = $this->_foreignKey;
		if ( !$this->$k && !$this->$foreign) {
			die('logical error calling supply::setPrice');
		}
		
		$nowDate = new JDate();
		$nowString = $nowDate->toMySQL();
		
		$this->stdPrice = $newPrice;
		$this->priceUpdate = $nowString;
		
		$returnVal = $this->store();

		return $returnVal;
	}
	
	/**
	 * Retrieve the data for the one supply or the whole supply table.
	 * Return result will always be an array.
	 *
	 * @return array An array of objects.
	 */
	function getData( $showSeparators = true) {
		
		$ordering = 'ordering';
		if ( $this->_type == 'item') {
			$ordering = 'name';
		}
		
		$k = $this->_tbl_key;
		if ( $this->$k) {
			$result = array($this->getProperties());
		} else {
			$query = 'SELECT ' . $this->_getQueryFieldList()
			 . ' FROM #__py_'.$this->_type.'s'
			 . (!$showSeparators ? ' WHERE `name` <> "'.self::$TABLE_ROW_SEPARATOR_ID.'"' : '')
			 . ' ORDER BY `'.$ordering.'`';
			$result = $this->loadObjectList( $query);
		}
		
		return $result;
	}
	
	function hasPrice() {
		
		$k = $this->_tbl_key;
		$foreign = $this->_foreignKey;
		if ( !$this->$k && !$this->$foreign) {
			die('logical error calling supply::hasPrice');
		}
		
		// no date at all
		if ( $this->priceUpdate == null || strlen($this->priceUpdate) == 0 ) return false;
		
		// price is zero (mostly after fresh install)
		if ( !$this->stdPrice) return false;
		
		$nowDate = new JDate();
		$nowUnix = $nowDate->toUnix();
		$priceUpdateDate = new JDate( $this->priceUpdate);
		$priceUpdateUnix = $priceUpdateDate->toUnix();
		
		if ( $nowUnix - $priceUpdateUnix > $this->_maxAge) return false;
		
		return true;
	}
	
	protected function _getQueryFieldList() {
		return 'IF(`name` ="'.self::$TABLE_ROW_SEPARATOR_ID.'" , "'.self::$TABLE_ROW_SEPARATOR_VISUAL.'" , `name` )AS `name`'
			 . ', IF(`name` ="'.self::$TABLE_ROW_SEPARATOR_ID.'" , -1 , `eveTypeId` ) AS `eveTypeId`, `ordering`, `stdPrice`, `priceUpdate`';
	}
}

?>
Return current item: Pygalle