Version 4.3.0

This commit is contained in:
Neo committed 2025-12-19 05:47:12 -08:00
1 parent 96997ddd8e
commit fe1acf984a
1349 files changed
+116159 -67711

No files matched your search

+28 -19
View File
@@ -166,26 +166,35 @@ class _Callback extends \IPS\Helpers\Chart\Dynamic
$datetime->setTime( 0, 0, 0 );
$exploded = explode( '-', $time );
switch ( $this->timescale )
{
case 'none':
$datetime = \IPS\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( $this->enableHourly === TRUE AND $this->timescale == 'hourly' )
{
$datetime->setDate( (float) $exploded[0], $exploded[1], $exploded[2] );
$datetime->setTime( $exploded[3], $exploded[4], $exploded[5] );
}
else
{
switch ( $this->timescale )
{
case 'none':
$datetime = \IPS\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 ) )
+80 -22
View File
@@ -36,6 +36,11 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
* @brief Where clauses
*/
public $where = array();
/**
* @brief Query joins
*/
public $joins = array();
/**
* @brief Extra column to group by (useful for multi-data line charts)
@@ -78,12 +83,11 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
{
$this->table = $table;
$this->dateField = $dateField;
$this->identifier = \substr( md5( $table . $dateField ), 0, 6 ) . $identifier;
$this->identifier = \substr( md5( $table . $dateField ), 0, 6 ) . ( $identifier ?: ( \IPS\Request::i()->chartId ?: '_default' ) );
if ( !empty( $tableInclude ) )
{
$this->tableInclude = $tableInclude;
$this->availableTypes[] = 'Table';
}
parent::__construct( $url, $title, $options, $defaultType, $defaultTimescale, $defaultTimes, $identifier, $minimumDate );
@@ -155,6 +159,15 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
*/
public function getOutput()
{
/* Auto-support tables where appropriate */
if ( !empty( $this->tableInclude ) )
{
if( !array_search( 'Table', $this->availableTypes ) )
{
$this->availableTypes[] = 'Table';
}
}
/* Init data */
$data = $this->initData();
@@ -173,6 +186,10 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
/* What's our SQL time? */
switch ( $this->timescale )
{
case 'hourly':
$timescale = '%Y-%c-%e-%H-%i-%s';
break;
case 'daily':
$timescale = '%Y-%c-%e';
break;
@@ -189,11 +206,27 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
/* Table... */
if ( $this->type === 'Table' )
{
/* Are we filtering? */
if( $this->groupBy )
{
if( count( $this->availableFilters ) AND count( $this->currentFilters ) )
{
$where[] = array( \IPS\Db::i()->in( $this->groupBy, $this->currentFilters ) );
}
}
$table = new \IPS\Helpers\Table\Db( $this->table, $this->url, $where );
$table->include = $this->tableInclude;
$table->joins = $this->joins;
if( count( $this->tableInclude ) )
{
$table->include = $this->tableInclude;
}
$table->parsers = $this->tableParsers;
$table->sortBy = $table->sortBy ?: $this->dateField;
$output = (string) $table;
return $output;
}
/* Pie Chart */
@@ -210,6 +243,14 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
NULL,
$this->groupBy
)->setKeyField( $this->groupBy )->setValueField( $key );
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 ) );
}
}
foreach ( $stmt as $k => $v )
{
@@ -241,6 +282,14 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
$this->groupBy ? array( 'time_as_alias', $this->groupBy ) : 'time_as_alias'
);
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 ) );
}
}
foreach ( $stmt as $row )
{
$result = array();
@@ -300,26 +349,35 @@ class _Database extends \IPS\Helpers\Chart\Dynamic
$datetime->setTime( 0, 0, 0 );
$exploded = explode( '-', $time );
switch ( $this->timescale )
{
case 'none':
$datetime = \IPS\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( $this->enableHourly === TRUE AND $this->timescale == 'hourly' )
{
$datetime->setDate( (float) $exploded[0], $exploded[1], $exploded[2] );
$datetime->setTime( $exploded[3], $exploded[4], $exploded[5] );
}
else
{
switch ( $this->timescale )
{
case 'none':
$datetime = \IPS\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 ) )
+179 -7
View File
@@ -107,6 +107,11 @@ abstract class _Dynamic extends \IPS\Helpers\Chart
*/
public $minimumDate = NULL;
/**
* @brief Enable hourly filtering. USE WITH CAUTION.
*/
public $enableHourly = FALSE;
/**
* @brief Error(s) to show on chart UI
*/
@@ -128,6 +133,13 @@ abstract class _Dynamic extends \IPS\Helpers\Chart
*/
public function __construct( \IPS\Http\Url $url, $title='', $options=array(), $defaultType='AreaChart', $defaultTimescale='monthly', $defaultTimes=array( 'start' => 0, 'end' => 0 ), $identifier='', $minimumDate=NULL )
{
/* If we are deleting a chart, just do that now and redirect */
if( isset( \IPS\Request::i()->deleteChart ) )
{
\IPS\Db::i()->delete( 'core_saved_charts', array( 'chart_id=? and chart_member=?', \IPS\Request::i()->deleteChart, \IPS\Member::loggedIn()->member_id ) );
\IPS\Output::i()->redirect( \IPS\Request::i()->url()->stripQueryString( array( 'deleteChart', 'chartId' ) ), 'chart_deleted' );
}
if ( !isset( $options['chartArea'] ) )
{
$options['chartArea'] = array(
@@ -135,6 +147,11 @@ abstract class _Dynamic extends \IPS\Helpers\Chart
'width' => '75%'
);
}
if( isset( \IPS\Request::i()->chartId ) AND \IPS\Request::i()->chartId != '_default' )
{
$url = $url->setQueryString( 'chartId', \IPS\Request::i()->chartId );
}
$this->baseURL = $url;
$this->title = $title;
@@ -154,10 +171,13 @@ abstract class _Dynamic extends \IPS\Helpers\Chart
$this->type = $defaultType;
}
if ( isset( \IPS\Request::i()->timescale[ $this->identifier ] ) and in_array( \IPS\Request::i()->timescale[ $this->identifier ], array( 'daily', 'weekly', 'monthly' ) ) )
if ( isset( \IPS\Request::i()->timescale[ $this->identifier ] ) and in_array( \IPS\Request::i()->timescale[ $this->identifier ], array( 'hourly', 'daily', 'weekly', 'monthly' ) ) )
{
$this->timescale = \IPS\Request::i()->timescale[ $this->identifier ];
$url = $url->setQueryString( 'timescale', array( $this->identifier => \IPS\Request::i()->timescale[ $this->identifier ] ) );
if( \IPS\Request::i()->timescale[ $this->identifier ] != 'hourly' OR ( \IPS\Request::i()->timescale[ $this->identifier ] == 'hourly' AND $this->enableHourly === TRUE ) )
{
$this->timescale = \IPS\Request::i()->timescale[ $this->identifier ];
$url = $url->setQueryString( 'timescale', array( $this->identifier => \IPS\Request::i()->timescale[ $this->identifier ] ) );
}
}
if ( $this->type === 'PieChart' or $this->type === 'GeoChart' )
@@ -167,7 +187,7 @@ abstract class _Dynamic extends \IPS\Helpers\Chart
}
else
{
$this->addHeader( \IPS\Member::loggedIn()->language()->addToStack('date'), ( $this->timescale == 'none' ) ? 'datetime' : 'date' );
$this->addHeader( \IPS\Member::loggedIn()->language()->addToStack('date'), ( $this->timescale == 'none' OR $this->timescale == 'hourly' ) ? 'datetime' : 'date' );
}
if ( isset( \IPS\Request::i()->start[ $this->identifier ] ) and \IPS\Request::i()->start[ $this->identifier ] )
@@ -182,7 +202,7 @@ abstract class _Dynamic extends \IPS\Helpers\Chart
}
else
{
$this->start = new \IPS\DateTime( \IPS\Request::i()->start[ $this->identifier ], new \DateTimeZone( \IPS\Member::loggedIn()->timezone ) );
$this->start = new \IPS\DateTime( \IPS\Helpers\Form\Date::_convertDateFormat( \IPS\Request::i()->start[ $this->identifier ] ), new \DateTimeZone( \IPS\Member::loggedIn()->timezone ) );
}
if( $this->minimumDate > $this->start )
@@ -213,7 +233,7 @@ abstract class _Dynamic extends \IPS\Helpers\Chart
}
else
{
$this->end = new \IPS\DateTime( \IPS\Request::i()->end[ $this->identifier ], new \DateTimeZone( \IPS\Member::loggedIn()->timezone ) );
$this->end = new \IPS\DateTime( \IPS\Helpers\Form\Date::_convertDateFormat( \IPS\Request::i()->end[ $this->identifier ] ), new \DateTimeZone( \IPS\Member::loggedIn()->timezone ) );
}
/* The end date should include items to the end of the day */
@@ -246,6 +266,18 @@ abstract class _Dynamic extends \IPS\Helpers\Chart
$this->timezoneError = TRUE;
}
}
/* If we have requested a saved chart, load its filters */
if( isset( \IPS\Request::i()->chartId ) AND \IPS\Request::i()->chartId != '_default' AND !isset( \IPS\Request::i()->filters ) )
{
foreach( $this->loadAvailableChartTabs() as $chart )
{
if( $chart['chart_id'] == \IPS\Request::i()->chartId )
{
\IPS\Request::i()->filters = array( $this->identifier => json_decode( $chart['chart_configuration'], true ) );
}
}
}
}
/**
@@ -255,6 +287,11 @@ abstract class _Dynamic extends \IPS\Helpers\Chart
*/
abstract public function getOutput();
/**
* @brief Form to save filters
*/
public $form = NULL;
/**
* HTML
*
@@ -264,6 +301,89 @@ abstract class _Dynamic extends \IPS\Helpers\Chart
{
try
{
/* If we have filters, we can save them */
if( count( $this->availableFilters ) > 0 )
{
/* We have an existing chart ID */
if( isset( \IPS\Request::i()->chartId ) AND \IPS\Request::i()->chartId != '_default' )
{
$title = '';
foreach( $this->loadAvailableChartTabs() as $chart )
{
if( $chart['chart_id'] == \IPS\Request::i()->chartId )
{
$title = $chart['chart_title'];
break;
}
}
/* Generate a form so we can save our filters as a new saved chart */
$this->form = new \IPS\Helpers\Form;
$this->form->class = 'ipsForm_vertical';
$this->form->add( new \IPS\Helpers\Form\Text( 'custom_chart_title', $title, TRUE ) );
if( $values = $this->form->values() )
{
\IPS\Db::i()->update( 'core_saved_charts', array( 'chart_title' => $values['custom_chart_title'] ), array( 'chart_id=? AND chart_member=?', \IPS\Request::i()->chartId, \IPS\Member::loggedIn()->member_id ) );
/* And then return the output we need */
\IPS\Output::i()->json( array(
'title' => $values['custom_chart_title']
) );
}
/* And we want to save our filter updates */
if( isset( \IPS\Request::i()->saveFilters ) )
{
\IPS\Db::i()->update( 'core_saved_charts', array( 'chart_configuration' => json_encode( \IPS\Request::i()->chartFilters ) ), array( 'chart_id=? AND chart_member=?', \IPS\Request::i()->chartId, \IPS\Member::loggedIn()->member_id ) );
}
}
/* We are not viewing a saved chart */
else
{
/* Generate a form so we can save our filters as a new saved chart */
$this->form = new \IPS\Helpers\Form;
$this->form->class = 'ipsForm_vertical';
$this->form->add( new \IPS\Helpers\Form\Text( 'custom_chart_title', NULL, TRUE ) );
if( $values = $this->form->values() )
{
/* Store the new chart */
$id = \IPS\Db::i()->insert( 'core_saved_charts', array(
'chart_member' => \IPS\Member::loggedIn()->member_id,
'chart_controller' => \IPS\Request::i()->app . '_' . \IPS\Request::i()->module . '_' . \IPS\Request::i()->controller . ( \IPS\Request::i()->tab ? '_' . \IPS\Request::i()->tab : '' ),
'chart_configuration' => json_encode( \IPS\Request::i()->chartFilters ),
'chart_title' => $values['custom_chart_title'],
) );
/* Set some input parameters */
$this->url = $this->url->setQueryString( 'chartId', $id );
\IPS\Request::i()->chartId = $id;
\IPS\Request::i()->filters = array( $this->identifier => \IPS\Request::i()->chartFilters );
$this->currentFilters = \IPS\Request::i()->filters;
if ( isset( \IPS\Request::i()->filters[ $this->identifier ] ) )
{
$this->url = $this->url->setQueryString( 'filters', array( $this->identifier => $this->currentFilters ) );
}
/* Reset form, since template looks for it and it should not be set for a saved chart */
$this->form = new \IPS\Helpers\Form;
$this->form->class = 'ipsForm_vertical';
$this->form->add( new \IPS\Helpers\Form\Text( 'custom_chart_title', $values['custom_chart_title'], TRUE ) );
/* And then return the output we need */
\IPS\Output::i()->json( array(
'tabHref' => $this->url->stripQueryString( 'filters' ),
'chartId' => $id,
'tabId' => md5( $this->url->acpQueryString() ),
) );
}
}
}
/* Get data */
$output = '';
if ( !empty( $this->series ) )
@@ -282,7 +402,16 @@ abstract class _Dynamic extends \IPS\Helpers\Chart
}
else
{
return \IPS\Theme::i()->getTemplate( 'global', 'core', 'global' )->dynamicChart( $this, $output );
$chartOutput = \IPS\Theme::i()->getTemplate( 'global', 'core', 'global' )->dynamicChart( $this, $output );
if( count( $this->availableFilters ) > 0 AND ( !\IPS\Request::i()->isAjax() OR ( \IPS\Request::i()->tab AND !\IPS\Request::i()->chartId ) ) )
{
return \IPS\Theme::i()->getTemplate( 'global', 'core' )->tabs( $this->getChartTabs(), ( isset( \IPS\Request::i()->chartId ) ) ? \IPS\Request::i()->chartId : NULL, $chartOutput, $this->url, 'chartId', 'ipsTabs_small ipsTabs_contained' );
}
else
{
return $chartOutput;
}
}
}
catch ( \Exception $e )
@@ -294,6 +423,43 @@ abstract class _Dynamic extends \IPS\Helpers\Chart
\IPS\IPS::exceptionHandler( $e );
}
}
/**
* @brief Cached tab data
*/
protected $availableChartTabs = NULL;
/**
* Retrieve tabs based on saved charts
*
* @return array
*/
protected function getChartTabs()
{
$tabs = array( '' => 'dynamic_chart_overview' );
foreach( $this->loadAvailableChartTabs() as $chart )
{
$tabs[ $chart['chart_id'] ] = $chart['chart_title'];
}
return $tabs;
}
/**
* Load and return available chart tabs
*
* @return array
*/
protected function loadAvailableChartTabs()
{
if( $this->availableChartTabs === NULL )
{
$this->availableChartTabs = iterator_to_array( \IPS\Db::i()->select( '*', 'core_saved_charts', array( 'chart_member=? AND chart_controller=?', \IPS\Member::loggedIn()->member_id, \IPS\Request::i()->app . '_' . \IPS\Request::i()->module . '_' . \IPS\Request::i()->controller . ( \IPS\Request::i()->tab ? '_' . \IPS\Request::i()->tab : '' ) ) ) );
}
return $this->availableChartTabs;
}
/**
* Flip URL Filter
@@ -333,6 +499,12 @@ abstract class _Dynamic extends \IPS\Helpers\Chart
{
switch ( $this->timescale )
{
case 'hourly':
$data[ $date->format( 'Y-n-j-h-i-s' ) ] = array();
$date->add( new \DateInterval( 'P1H' ) );
break;
case 'daily':
$data[ $date->format( 'Y-n-j' ) ] = array();