<?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 included in Joomla!
defined('_JEXEC') or die('Restricted access');
pyimport( 'tables.table');
class PyTableMining extends PyTable {
/**
* @access protected
*/
function __construct( &$db ) {
parent::__construct( '#__py_mining_yield', 'id', $db);
}
function getMiningLogs($limit = 100, $ids = null) {
$querylimit = "";
$exactid = "";
if ( $ids === null) {
if ($limit !== null) {
/* this is calculating the last n rows. Not needed for now.
$query = 'SELECT count(*) FROM #__py_mining_yield WHERE `published` = 1';
$db->setQuery($query);
$numrows = $db->loadResult();
$this->_log->trace($db);
$limit = $numrows - $limit;
*/
$querylimit = " LIMIT $limit";
}
} else {
if ( !is_array($ids)) { $ids = array( $ids); }
$exactid = ' AND (';
$_first = true;
foreach ($ids as $oneId) {
if ( $_first) {
$_first = false;
} else {
$exactid .= ' OR ';
}
$exactid .= "y.`id` = $oneId";
}
$exactid .= ') ';
}
$query = 'SELECT y.*, COUNT(g.miningId) AS numpilots, u.`name` AS `createdByName`, o.`name` AS `ore`, b.`id` AS `locationid`, b.system, b.planet, b.belt AS beltnumber'
. ' FROM #__py_mining_yield AS y'
. ' INNER JOIN #__py_mining_pilots AS g ON y.`id` = g.`miningId`'
. ' LEFT OUTER JOIN #__users AS u ON y.`modifiedBy` = u.`id`'
. ' LEFT OUTER JOIN #__py_ores AS o ON y.`typeId` = o.`eveTypeId`'
. ' LEFT OUTER JOIN #__py_locations AS b ON y.`belt` = b.`id`'
. ' WHERE y.`published` = 1'
. $exactid
. ' GROUP BY y.`id`'
. ' ORDER BY y.`date` DESC '
. $querylimit;
return $this->loadObjectList( $query);
}
function getMiningLogsByDate( JDate $startDate, JDate $endDate) {
$query = 'SELECT `value`, `amount`, `date`'
. ' FROM #__py_mining_yield AS y'
. ' WHERE y.`date` >= "' . $startDate->toMySQL() . '" AND y.`date` <= "' . $endDate->toMySQL() . '"'
. ' ORDER BY y.`date` ASC';
return $this->loadAssocList( $query);
}
function getMiningLogsWithConfig( array $config = array()) {
$query = 'SELECT y.*';
$from = $this->buildFrom( $config);
$where = $this->buildWhere( $config);
$order = $this->buildOrder( $config);
$query = $query . $from . $where . $order;
return $this->loadAssocList( $query);
}
private function buildOrder( array $config = array()) {
$order = '';
if ( !empty( $config['order'])) {
$orderConfig = each( $config['order']);
$order = ' ORDER BY y.`'.$orderConfig['key'].'` '.$orderConfig['value'];
}
return $order;
}
private function buildFrom( array $config = array()) {
$from = ' FROM #__py_mining_yield AS y';
if ( isset( $config['belt']) && count( $config['belt'])) {
$from .= ' JOIN #__py_locations AS b ON y.`belt` = b.`id`';
}
return $from;
}
private function buildWhere( array $config = array()) {
$where = ' WHERE y.`published`= 1';
if ( isset( $config['startDate']) && $config['startDate'] instanceof JDate && isset( $config['endDate']) && $config['endDate'] instanceof JDate) {
$where .= ' AND y.`date` >= "' . $config['startDate']->toMySQL() . '" AND y.`date` <= "' . $config['endDate']->toMySQL() . '"';
}
if ( isset( $config['belt']) && count( $config['belt'])) {
$moreWhere = '';
settype( $config['belt'], 'array');
foreach( $config['belt'] as $belt) {
if ( $moreWhere) {
$moreWhere .= ' OR';
}
$moreWhere .= (' b.`id` = ' . $belt);
}
$where .= ( ' AND (' . $moreWhere . ')');
}
return $where;
}
}
?>