Location: PHPKode > projects > Pygalle > admin/tables/user.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 within the rest of the framework
defined( '_JEXEC' ) or die( 'Restricted access' );

// need to use basic path because pygalle plugin needs this class and operates in com_user namespace
pyimport( 'log');
pyimport( 'tables.table');

/**
 * User class which basically represents the pyuser table.
 * Handles all application interaction with a user, including EVE cluster information.
 * 
 * Interaction with the EVE cluster is delegated to other classes. E.g. Basic account
 * information is collected by PyCharAccounts
 *
 */
class PyTableUser extends PyTable
{
	/**
	 * Primary id
	 * @var int
	 */
	var $id				= null;

	/**
	 * Unique id
	 * @var int
	 */
	var $josId				= null;

	/**
	 * The user's EVE API Key hash
	 * @var string
	 */
	var $eveKey			= null;

	/**
	 * The EVE account number
	 * @var string
	 */
	var $eveUserId      = null;
	
	/**
	 * The EVE character ID
	 * @var int
	 */
	var $characterId      = null;
	var $corpId = null;
	var $employeeCheckDate = null;
	var $allowForeign = null;
	var $lastRedAlert = null;
	var $redAlertOptOut = null;
	
	/**
	 * Constructor
	 *  
	 * @access 	protected
	 * @param dbo JDatabase The database connection
	 */
	function __construct( &$dbo)
	{
		parent::__construct( '#__py_users', 'id', $dbo );
		$this->setForeignKey( 'josId');
	}
	
	static function &getCurrentUser() {
		return PyTable::getInstance( 'user', array( 'josId' => JFactory::getUser()->id));
	}
	
	function &getInstance( array $config) {
		return PyTable::getInstance( 'user', $config);
	}
	
	function configure( array $config) {
		if (!parent::configure( $config)) {
			if ( !empty( $config['name'])) {
				$query = 'SELECT *'
				. ' FROM '.$this->_tbl . ' AS pu'
				. ' JOIN #__users AS ju ON ju.`id` = pu.`josId`'
				. ' WHERE `name` = '.$this->getDBO()->Quote($config['name']);
				
				if ($result = $this->loadAssoc( $query)) {
					return $this->bind($result);
				} else {
					return false;
				}
				
			}
		}
	}
	
	/**
	 * This will create a new entry in the pyuser table with the current
	 * object values. Given that the primary
	 * key is 'id' while all internal references are done reusing the 'josId'
	 * from joomla we need to tweak in the new table row and cannot use
	 * the JTable method.
	 * 
	 * @return boolean true, if the database request was successful.
	 */
	function createNew() {
		
		$result = $this->_db->insertObject( $this->_tbl, $this, 'id' );
		$this->_log->trace( $this->_db);
		if ( $this->_db->getErrorNum() > 0) {
			$this->_log->debug( 'Error while creating a new pyuser table entry.', $db);
			return false;
		}
		return true;
	}
	
	/**
	 * Is the user an employee?
	 *
	 * @return boolean true, if user is employee; zero, if last check is long ago; false, if last check failed
	 */
	function getIsEmployee() {
		
		if ( $this->employeeCheckDate != null) {
			$lastCheck = new JDate( $this->employeeCheckDate);
			$now = new JDate();
			if ( $lastCheck > $now - 4 * 7 * 24 * 60 * 60) {
				return true;
			}
			return 0;
		}		
		return false;
	}
	
	/**
	 * Allow login to user?
	 * Respects a foreign employee login override in the database.
	 *
	 * @return boolean true, if user is employee; zero, if last check is long ago; false, if last check failed
	 */
	function getIsLoginAllowed() {
		return $this->getIsEmployee() || $this->allowForeign;
	}
	
	function getPilots() {
		$query = 'SELECT u.`name`, p.`josId`'
		 . ' FROM #__py_users as p ' 
		 . ' JOIN #__users as u ON u.`id` = p.`josId`' 
		 . ' WHERE p.`employeeCheckDate` IS NOT NULL AND u.`block` <> 1'
		 . ' ORDER BY u.`name`';

		return $this->loadObjectList( $query);
	}
	
	function getMiningPilots() {
		$query = 'SELECT u.`name`, p.`josId`'
		 . ' FROM #__py_mining_pilots as p ' 
		 . ' JOIN #__users as u ON u.`id` = p.`josId`' 
		 . ' GROUP BY u.`name`'
		 . ' ORDER BY u.`name`';
		return $this->loadObjectList( $query);
	}
	
	/**
	 * User always updates the nulls
	 * 
	 * @see administrator/components/com_pygalle/tables/PyTable#store($updateNulls)
	 */
	function store( $updateNulls = true) {
		return parent::store( $updateNulls);
	}
}
Return current item: Pygalle