Version 5.0.0 beta 1

This commit is contained in:
Neo committed 2025-12-19 16:27:35 -08:00
1 parent 25ddeb65d6
commit 15c7beabc5
6736 files changed
+627902 -497943

No files matched your search

+145 -131
View File
@@ -11,99 +11,114 @@
namespace IPS\Helpers\Chart;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use DateTimeZone;
use Exception;
use IPS\DateTime;
use IPS\Db;
use IPS\Helpers\Table\Db as TableDb;
use IPS\Http\Url;
use IPS\Member;
use IPS\Output;
use IPS\Request;
use function count;
use function defined;
use function header;
use function in_array;
use function is_array;
use function mb_strlen;
use function mb_substr;
use function md5;
use function substr;
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Dynamic Database Chart Helper
*/
class _Database extends \IPS\Helpers\Chart\Dynamic
class Database extends Dynamic
{
/**
* @brief Database Table
*/
protected $table;
protected ?string $table = null;
/**
* @brief Database column that contains date
*/
protected $dateField;
protected ?string $dateField = null;
/**
* @brief Where clauses
*/
public $where = array();
public array $where = array();
/**
* @brief Query joins
*/
public $joins = array();
public array $joins = array();
/**
* @brief Extra column to group by (useful for multi-data line charts)
*/
public $groupBy = '';
public string $groupBy = '';
/**
* @brief Group by keys for the series
*/
protected $groupByKeys = array();
protected array $groupByKeys = array();
/**
* @brief Table Columns
*/
public $tableInclude = array();
public array $tableInclude = array();
/**
* @brief Table Column Formatters
*/
public $tableParsers = array();
/**
* @brief Table Lang Prefix
*/
public $tableLangPrefix = NULL;
public array $tableParsers = array();
/**
* @brief Along with the other series, show one of the sum of all
*/
public $allRecordsSeries = FALSE;
public bool $allRecordsSeries = FALSE;
/**
* @brief The lang string used to label the 'all records' series
*/
public $allRecordsLang = 'database_chart_all_series';
public string $allRecordsLang = 'database_chart_all_series';
/**
* @brief Custom form
*/
public $customFiltersForm = FALSE;
public bool|array $customFiltersForm = FALSE;
/**
* Constructor
*
* @param \IPS\Http\Url $url The URL the chart will be displayed on
* @param string $table Database Table
* @param string $dateField Database column that contains date
* @param string $title Title
* @param array $options Options
* @param string $defaultType The default chart type
* @param string $defaultTimescale The default timescale to use
* @param array $defaultTimes The default start/end times to use
* @param array $tableInclude Table columns to include in results
* @param string $identifier If there will be more than one chart per page, provide a unique identifier
* @param \IPS\DateTime|NULL $minimumDate The earliest available date for this chart
* @see <a href='https://google-developers.appspot.com/chart/interactive/docs/gallery'>Charts Gallery - Google Charts - Google Developers</a>
* @param Url $url The URL the chart will be displayed on
* @param string $table Database Table
* @param string $dateField Database column that contains date
* @param string $title Title
* @param array $options Options
* @param string $defaultType The default chart type
* @param string $defaultTimescale The default timescale to use
* @param array $defaultTimes The default start/end times to use
* @param array $tableInclude Table columns to include in results
* @param string $identifier If there will be more than one chart per page, provide a unique identifier
* @param DateTime|null $minimumDate The earliest available date for this chart
* @return void
*@see <a href='https://google-developers.appspot.com/chart/interactive/docs/gallery'>Charts Gallery - Google Charts - Google Developers</a>
*/
public function __construct( \IPS\Http\Url $url, $table, $dateField, $title='', $options=array(), $defaultType='AreaChart', $defaultTimescale='monthly', $defaultTimes=array( 'start' => 0, 'end' => 0 ), $tableInclude=array(), $identifier='', $minimumDate=NULL )
public function __construct( Url $url, string $table, string $dateField, string $title='', array $options=array(), string $defaultType='AreaChart', string $defaultTimescale='monthly', array $defaultTimes=array( 'start' => 0, 'end' => 0 ), array $tableInclude=array(), string $identifier='', DateTime $minimumDate=null )
{
$this->table = $table;
$this->dateField = $dateField;
$this->identifier = \substr( md5( $table . $dateField ), 0, 6 ) . $identifier . ( \IPS\Request::i()->chartId ?: '_default' );
$this->identifier = substr( md5( $table . $dateField ), 0, 6 ) . $identifier . ( Request::i()->chartId ?: '_default' );
if ( !empty( $tableInclude ) )
{
@@ -112,24 +127,24 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
parent::__construct( $url, $title, $options, $defaultType, $defaultTimescale, $defaultTimes, $identifier, $minimumDate );
}
/**
* Add Series
*
* @param mixed $name Either a string with the series name or an array [ 'value' => "Series Name", 'key' => "XX" ]. Keys are used to give a country code for GeoCharts.
* @param string $type Type of value
* @param string $type Type of value
* @li string
* @li number
* @li boolean
* @li date
* @li datetime
* @li timeofday
* @param string $sql SQL expression to get value
* @param bool $filterable If TRUE, will show as a filter option to be toggled on/off
* @param string $groupByKey If $this->groupBy is set, the raw key value
* @param string $sql SQL expression to get value
* @param bool $filterable If TRUE, will show as a filter option to be toggled on/off
* @param string|null $groupByKey If $this->groupBy is set, the raw key value
* @return void
*/
public function addSeries( $name, $type, $sql, $filterable=TRUE, $groupByKey=NULL )
public function addSeries( mixed $name, string $type, string $sql, bool $filterable=TRUE, string $groupByKey=NULL ) : void
{
if ( $groupByKey !== NULL )
{
@@ -137,16 +152,16 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
}
else
{
$name = \is_array( $name ) ? $name['value'] : $name;
\IPS\Member::loggedIn()->language()->parseOutputForDisplay( $name );
$name = is_array( $name ) ? $name['value'] : $name;
Member::loggedIn()->language()->parseOutputForDisplay( $name );
$filterKey = $name;
}
if ( !$filterable or !isset( \IPS\Request::i()->filters[ $this->identifier ] ) or \in_array( $filterKey, \IPS\Request::i()->filters[ $this->identifier ] ) )
if ( !$filterable or !isset( Request::i()->filters[ $this->identifier ] ) or in_array( $filterKey, Request::i()->filters[ $this->identifier ] ) )
{
if ( $this->type !== 'PieChart' and $this->type !== 'GeoChart' )
{
$this->addHeader( \is_array( $name ) ? $name['value'] : $name, $type );
$this->addHeader( is_array( $name ) ? $name['value'] : $name, $type );
}
if( $this->groupBy )
@@ -155,68 +170,68 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
}
$this->series[ $filterKey ] = $sql;
if ( $filterable )
{
$this->currentFilters[] = $filterKey;
if ( isset( \IPS\Request::i()->filters[ $this->identifier ] ) )
if ( isset( Request::i()->filters[ $this->identifier ] ) )
{
$this->url = $this->url->setQueryString( 'filters', array( $this->identifier => $this->currentFilters ) );
}
}
}
if ( $filterable )
{
$this->availableFilters[ $filterKey ] = $name;
}
}
/**
* Init the data array
*
* @return array
*/
protected function initData()
protected function initData(): array
{
$data = parent::initData();
if ( $this->customFiltersForm )
{
if ( $values = $this->getCustomFiltersForm()->values() )
{
$whereFunction = $this->customFiltersForm['where'];
$seriesFunction = $this->customFiltersForm['series'];
foreach( $values as $k => $v )
{
unset( $values[ $k ] );
$values[ \mb_substr( $k, \mb_strlen( $this->identifier ) ) ] = $v;
$values[ mb_substr( $k, mb_strlen( $this->identifier ) ) ] = $v;
}
$this->groupBy = $this->customFiltersForm['groupBy'];
$whereFunctionResult = $whereFunction( $values );
$this->where[] = \is_array( $whereFunctionResult ) ? $whereFunctionResult : array( $whereFunctionResult );
$this->where[] = is_array( $whereFunctionResult ) ? $whereFunctionResult : array( $whereFunctionResult );
foreach( $seriesFunction( $values ) as $series )
{
$this->addSeries( $series[0], $series[1], $series[2], $series[3], $series[4] );
}
}
else if ( $this->savedCustomFilters and \count( $this->savedCustomFilters ) )
else if ( $this->savedCustomFilters and count( $this->savedCustomFilters ) )
{
$whereFunction = $this->customFiltersForm['where'];
$seriesFunction = $this->customFiltersForm['series'];
$this->groupBy = $this->customFiltersForm['groupBy'];
$whereFunctionResult = $whereFunction( $this->savedCustomFilters );
$this->where[] = \is_array( $whereFunctionResult ) ? $whereFunctionResult : array( $whereFunctionResult );
$this->where[] = is_array( $whereFunctionResult ) ? $whereFunctionResult : array( $whereFunctionResult );
foreach( $seriesFunction( $this->savedCustomFilters ) as $series )
{
$this->addSeries( $series[0], $series[1], $series[2], $series[3], $series[4] );
@@ -268,12 +283,12 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
}
$fromUnixTime = "FROM_UNIXTIME( IFNULL( {$this->dateField}, 0 ) )";
if ( !$this->timezoneError and \IPS\Member::loggedIn()->timezone and \in_array( \IPS\Member::loggedIn()->timezone, \DateTimeZone::listIdentifiers() ) )
if ( !$this->timezoneError and Member::loggedIn()->timezone and in_array( Member::loggedIn()->timezone, DateTimeZone::listIdentifiers() ) )
{
$fromUnixTime = "CONVERT_TZ( {$fromUnixTime}, @@session.time_zone, '" . \IPS\Db::i()->escape_string( \IPS\Member::loggedIn()->timezone ) . "' )";
$fromUnixTime = "CONVERT_TZ( {$fromUnixTime}, @@session.time_zone, '" . Db::i()->escape_string( Member::loggedIn()->timezone ) . "' )";
}
$allRecordsStatement = \IPS\Db::i()->select(
$allRecordsStatement = Db::i()->select(
"SQL_BIG_RESULT DATE_FORMAT( {$fromUnixTime}, '{$timescale}' ) AS time_as_alias, COUNT(*) as $this->allRecordsLang",
$this->table,
$where,
@@ -288,23 +303,22 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
$data[ $allRecordsRow['time_as_alias'] ][$this->allRecordsLang] = $allRecordsRow[$this->allRecordsLang];
}
$this->addSeries( \IPS\Member::loggedIn()->language()->addToStack($this->allRecordsLang), 'number', 'COUNT(*)', FALSE, $this->allRecordsLang );
$this->addSeries( Member::loggedIn()->language()->addToStack($this->allRecordsLang), 'number', 'COUNT(*)', FALSE, $this->allRecordsLang );
}
return $data;
}
/**
* Compile Data for Output
*
* @param array $data The data
* @return void
* @return void
*/
public function compileForOutput()
public function compileForOutput() : void
{
/* Init data */
$data = $this->initData();
/* Work out where clause */
$where = $this->where;
$where[] = array( "{$this->dateField}>?", 0 );
@@ -316,7 +330,7 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
{
$where[] = array( "{$this->dateField}<?", $this->end->getTimestamp() );
}
/* What's our SQL time? */
switch ( $this->timescale )
{
@@ -327,23 +341,23 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
case 'daily':
$timescale = '%Y-%c-%e';
break;
case 'weekly':
$timescale = '%x-%v';
break;
case 'monthly':
$timescale = '%Y-%c';
break;
}
/* Pie Chart */
if ( $this->type === 'PieChart' or $this->type === 'GeoChart' )
{
{
$keys = array_unique( $this->series );
$key = array_pop( $keys );
$stmt = \IPS\Db::i()->select(
$stmt = Db::i()->select(
"{$key}" . ( $this->groupBy ? ", {$this->groupBy}" : '' ),
$this->table,
$where,
@@ -352,11 +366,11 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
$this->groupBy
);
if( \count( $this->joins ) )
if( count( $this->joins ) )
{
foreach( $this->joins as $join )
{
$stmt = $stmt->join( $join[0], $join[1], ( isset( $join[2] ) ? $join[2] : 'LEFT' ), ( isset( $join[3] ) ? $join[3] : FALSE ) );
$stmt = $stmt->join( $join[0], $join[1], ( $join[2] ?? 'LEFT' ), ( $join[3] ?? FALSE ) );
}
}
@@ -365,26 +379,26 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
foreach ( $stmt as $k => $v )
{
if( \count( $this->availableFilters ) and !\in_array( $k, $this->currentFilters ) )
if( count( $this->availableFilters ) and !in_array( $k, $this->currentFilters ) )
{
continue;
}
$this->addRow( array( 'key' => $this->availableFilters[ $k ], 'value' => $v ) );
}
}
/* Graph */
else
{
/* Fetch */
$fromUnixTime = "FROM_UNIXTIME( IFNULL( {$this->dateField}, 0 ) )";
if ( !$this->timezoneError and \IPS\Member::loggedIn()->timezone and \in_array( \IPS\Member::loggedIn()->timezone, \DateTimeZone::listIdentifiers() ) )
if ( !$this->timezoneError and Member::loggedIn()->timezone and in_array( Member::loggedIn()->timezone, DateTimeZone::listIdentifiers() ) )
{
$fromUnixTime = "CONVERT_TZ( {$fromUnixTime}, @@session.time_zone, '" . \IPS\Db::i()->escape_string( \IPS\Member::loggedIn()->timezone ) . "' )";
$fromUnixTime = "CONVERT_TZ( {$fromUnixTime}, @@session.time_zone, '" . Db::i()->escape_string( Member::loggedIn()->timezone ) . "' )";
}
$stmt = \IPS\Db::i()->select(
"SQL_BIG_RESULT DATE_FORMAT( {$fromUnixTime}, '{$timescale}' ) AS time_as_alias" . ( \count( $this->series ) ? "," : "" ) . implode( ', ', array_unique( $this->series ) ) . ( $this->groupBy ? ", " . $this->groupBy : '' ),
$stmt = Db::i()->select(
"SQL_BIG_RESULT DATE_FORMAT( {$fromUnixTime}, '{$timescale}' ) AS time_as_alias" . ( count( $this->series ) ? "," : "" ) . implode( ', ', array_unique( $this->series ) ) . ( $this->groupBy ? ", " . $this->groupBy : '' ),
$this->table,
$where,
'time_as_alias ASC',
@@ -392,21 +406,21 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
$this->groupBy ? array( 'time_as_alias', $this->groupBy ) : 'time_as_alias'
);
if( \count( $this->joins ) )
if( count( $this->joins ) )
{
foreach( $this->joins as $join )
{
$stmt = $stmt->join( $join[0], $join[1], ( isset( $join[2] ) ? $join[2] : 'LEFT' ), ( isset( $join[3] ) ? $join[3] : FALSE ) );
$stmt = $stmt->join( $join[0], $join[1], ( $join[2] ?? 'LEFT' ), ( $join[3] ?? FALSE ) );
}
}
foreach ( $stmt as $row )
{
$result = array();
if( $this->groupBy )
{
if( \count( $this->availableFilters ) AND !\in_array( $row[ $this->groupBy ], $this->currentFilters ) )
if( count( $this->availableFilters ) AND !in_array( $row[ $this->groupBy ], $this->currentFilters ) )
{
continue;
}
@@ -431,29 +445,29 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
$result[ $column ] = $row[ $column ];
}
}
$data[ $row['time_as_alias'] ] = $result;
}
ksort( $data, SORT_NATURAL );
/* Add to graph */
$min = NULL;
$max = NULL;
foreach ( $data as $time => $d )
{
$datetime = new \IPS\DateTime;
$datetime = new DateTime;
if ( \IPS\Member::loggedIn()->timezone )
if ( Member::loggedIn()->timezone )
{
try
{
$datetime->setTimezone( new \DateTimeZone( \IPS\Member::loggedIn()->timezone ) );
$datetime->setTimezone( new DateTimeZone( Member::loggedIn()->timezone ) );
}
catch ( \Exception $e )
catch ( Exception $e )
{
\IPS\Member::loggedIn()->timezone = null;
\IPS\Member::loggedIn()->save();
Member::loggedIn()->timezone = null;
Member::loggedIn()->save();
}
}
@@ -470,26 +484,26 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
switch ( $this->timescale )
{
case 'none':
$datetime = \IPS\DateTime::ts( $time );
$datetime = DateTime::ts( $time );
break;
case 'daily':
$datetime->setDate( (float) $exploded[0], $exploded[1], $exploded[2] );
//$datetime = $datetime->localeDate();
break;
case 'weekly':
$datetime->setISODate( (float) $exploded[0], $exploded[1] );
//$datetime = $datetime->localeDate();
break;
case 'monthly':
$datetime->setDate( (float) $exploded[0], $exploded[1], 1 );
//$datetime = $datetime->format( 'F Y' );
break;
}
}
if ( empty( $d ) )
{
if ( empty( $this->series ) )
@@ -498,7 +512,7 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
}
else
{
$this->addRow( array_merge( array( $datetime ), array_fill( 0, \count( $this->series ), 0 ) ) );
$this->addRow( array_merge( array( $datetime ), array_fill( 0, count( $this->series ), 0 ) ) );
}
}
else
@@ -510,14 +524,14 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
{
$_values[ $id ] = ( isset( $d[ $id ] ) ) ? $d[ $id ] : ( $this->plotZeros ? 0 : NULL );
}
$this->addRow( array_merge( array( $datetime ), $_values ) );
}
else
{
if( \count($d) < \count($this->series) )
if( count($d) < count($this->series) )
{
$this->addRow( array_merge( array( $datetime ), $d, array_fill( 0, \count($this->series) - \count($d), 0 ) ) );
$this->addRow( array_merge( array( $datetime ), $d, array_fill( 0, count($this->series) - count($d), 0 ) ) );
}
else
{
@@ -526,20 +540,20 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
}
}
}
if ( \count( $data ) === 1 )
if ( count( $data ) === 1 )
{
$this->options['domainAxis']['type'] = 'category';
}
}
}
/**
* Get the chart output
*
* @return string
*/
public function getOutput()
public function getOutput(): string
{
/* Auto-support tables where appropriate */
if ( !empty( $this->tableInclude ) )
@@ -549,7 +563,7 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
$this->availableTypes[] = 'Table';
}
}
/* Work out where clause */
$where = $this->where;
$where[] = array( "{$this->dateField}>?", 0 );
@@ -561,31 +575,31 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
{
$where[] = array( "{$this->dateField}<?", $this->end->getTimestamp() );
}
/* Table... */
if ( $this->type === 'Table' )
{
/* Are we filtering? */
if( $this->groupBy )
{
if( \count( $this->availableFilters ) AND \count( $this->currentFilters ) )
if( count( $this->availableFilters ) AND count( $this->currentFilters ) )
{
$where[] = array( \IPS\Db::i()->in( $this->groupBy, $this->currentFilters ) );
$where[] = array( Db::i()->in( $this->groupBy, $this->currentFilters ) );
}
}
$table = new \IPS\Helpers\Table\Db( $this->table, $this->url, $where );
$table = new TableDb( $this->table, $this->url, $where );
/* Reformat chart join for the table helper */
if( \is_array( $this->joins ) )
if( is_array( $this->joins ) )
{
array_walk( $this->joins, function ( $item ) use ( $table )
{
$table->joins[] = array('from' => $item[0], 'where' => $item[1], 'type' => isset( $item[2] ) ? $item[2] : 'LEFT');
$table->joins[] = array('from' => $item[0], 'where' => $item[1], 'type' => $item[2] ?? 'LEFT' );
} );
}
if( \count( $this->tableInclude ) )
if( count( $this->tableInclude ) )
{
$table->include = $this->tableInclude;
}
@@ -595,8 +609,8 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
}
$table->parsers = $this->tableParsers;
$table->sortBy = $table->sortBy ?: $this->dateField;
if ( isset( \IPS\Request::i()->download ) )
if ( isset( Request::i()->download ) )
{
$headers = array();
foreach( $table->getHeaders( $table->getAdvancedSearchValues() ) AS $header )
@@ -605,13 +619,13 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
{
continue;
}
$headers[] = array(
'label' => \IPS\Member::loggedIn()->language()->addToStack( $header ),
'label' => Member::loggedIn()->language()->addToStack( $header ),
'type' => 'string'
);
}
$rows = array();
foreach( $table->getRows( $table->getAdvancedSearchValues() ) AS $k => $v )
{
@@ -621,20 +635,20 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
$rows[$k][] = trim( strip_tags( (string) $value ) );
}
}
return $this->download( $headers, $rows, \IPS\Output::i()->title );
}
$output = (string) $table;
return $output;
$this->download( $headers, $rows, Output::i()->title );
return '';
}
return (string) $table;
}
$this->compileForOutput();
if ( isset( \IPS\Request::i()->download ) )
if ( isset( Request::i()->download ) )
{
return $this->download();
$this->download();
return '';
}
else
{