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

+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();