Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
@@ -11,20 +11,28 @@
|
||||
namespace IPS\Helpers\Form;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use InvalidArgumentException;
|
||||
use IPS\DateTime;
|
||||
use IPS\Request;
|
||||
use IPS\Theme;
|
||||
use LengthException;
|
||||
use function defined;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Date range input class for Form Builder
|
||||
*/
|
||||
class _DateRange extends FormAbstract
|
||||
class DateRange extends FormAbstract
|
||||
{
|
||||
/**
|
||||
* @brief Default Options
|
||||
* @see \IPS\Helpers\Form\Date::$defaultOptions
|
||||
* @see Date
|
||||
* @code
|
||||
$defaultOptions = array(
|
||||
'start' => array( ... ),
|
||||
@@ -37,7 +45,7 @@ class _DateRange extends FormAbstract
|
||||
);
|
||||
* @endcode
|
||||
*/
|
||||
protected $defaultOptions = array(
|
||||
protected array $defaultOptions = array(
|
||||
'start' => array(
|
||||
'min' => NULL,
|
||||
'max' => NULL,
|
||||
@@ -68,38 +76,37 @@ class _DateRange extends FormAbstract
|
||||
/**
|
||||
* @brief Start Date Object
|
||||
*/
|
||||
public $start = NULL;
|
||||
public ?Date $start = NULL;
|
||||
|
||||
/**
|
||||
* @brief End Date Object
|
||||
*/
|
||||
public $end = NULL;
|
||||
public ?Date $end = NULL;
|
||||
|
||||
/**
|
||||
* @brief Name for unlimited checkbox
|
||||
*/
|
||||
public $unlimitedName = '';
|
||||
|
||||
public string $unlimitedName = '';
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $name Name
|
||||
* @param mixed $defaultValue Default value
|
||||
* @param bool|NULL $required Required? (NULL for not required, but appears to be so)
|
||||
* @param array $options Type-specific options
|
||||
* @param callback $customValidationCode Custom validation code
|
||||
* @param string $prefix HTML to show before input field
|
||||
* @param string $suffix HTML to show after input field
|
||||
* @param string $id The ID to add to the row
|
||||
* @return void
|
||||
* @param string $name Name
|
||||
* @param mixed $defaultValue Default value
|
||||
* @param bool|null $required Required? (NULL for not required, but appears to be so)
|
||||
* @param array $options Type-specific options
|
||||
* @param callable|null $customValidationCode Custom validation code
|
||||
* @param string|null $prefix HTML to show before input field
|
||||
* @param string|null $suffix HTML to show after input field
|
||||
* @param string|null $id The ID to add to the row
|
||||
*/
|
||||
public function __construct( $name, $defaultValue=NULL, $required=FALSE, $options=array(), $customValidationCode=NULL, $prefix=NULL, $suffix=NULL, $id=NULL )
|
||||
public function __construct( string $name, mixed $defaultValue=NULL, ?bool $required=FALSE, array $options=array(), callable $customValidationCode=NULL, string $prefix=NULL, string $suffix=NULL, string $id=NULL )
|
||||
{
|
||||
$startOptions = isset( $options['start'] ) ? array_merge( $this->defaultOptions['start'], $options['start'] ) : $this->defaultOptions['start'];
|
||||
$this->start = new \IPS\Helpers\Form\Date( "{$name}[start]", isset( $defaultValue['start'] ) ? $defaultValue['start'] : NULL, FALSE, $startOptions );
|
||||
$this->start = new Date( "{$name}[start]", $defaultValue['start'] ?? NULL, FALSE, $startOptions );
|
||||
|
||||
$endOptions = isset( $options['end'] ) ? array_merge( $this->defaultOptions['end'], $options['end'] ) : $this->defaultOptions['end'];
|
||||
$this->end = new \IPS\Helpers\Form\Date( "{$name}[end]", isset( $defaultValue['end'] ) ? $defaultValue['end'] : NULL, FALSE, $endOptions );
|
||||
$this->end = new Date( "{$name}[end]", $defaultValue['end'] ?? NULL, FALSE, $endOptions );
|
||||
$this->unlimitedName = "{$name}_unlimited";
|
||||
|
||||
parent::__construct( $name, $defaultValue, $required, $options, $customValidationCode, $prefix, $suffix, $id );
|
||||
@@ -110,7 +117,7 @@ class _DateRange extends FormAbstract
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getLabelForAttribute()
|
||||
public function getLabelForAttribute(): mixed
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
@@ -121,25 +128,25 @@ class _DateRange extends FormAbstract
|
||||
* @note Returns an array with keys 'start' and 'end' normally, but if using the unlimited checkbox option will return that value if checked
|
||||
* @return mixed
|
||||
*/
|
||||
public function formatValue()
|
||||
public function formatValue(): mixed
|
||||
{
|
||||
/* The start time may be offset a few hours depending on the users timezone, let's fix that now */
|
||||
$start = $this->start->formatValue();
|
||||
if ( $start instanceof \IPS\DateTime and $this->options['start']['time'] === FALSE )
|
||||
if ( $start instanceof DateTime and $this->options['start']['time'] === FALSE )
|
||||
{
|
||||
$start->setTime( 00, 00, 00 );
|
||||
}
|
||||
|
||||
/* The end date needs to be 23:59:59 rather than 00:00:00 as we need to go right up to the end of the day */
|
||||
$end = $this->end->formatValue();
|
||||
if ( $end instanceof \IPS\DateTime and $this->options['end']['time'] === FALSE )
|
||||
if ( $end instanceof DateTime and $this->options['end']['time'] === FALSE )
|
||||
{
|
||||
$end->setTime( 23, 59, 59 );
|
||||
}
|
||||
|
||||
/* Unlimited? */
|
||||
$unlimitedName = $this->unlimitedName;
|
||||
if ( $this->options['unlimited'] !== NULL and isset( \IPS\Request::i()->$unlimitedName ) )
|
||||
if ( $this->options['unlimited'] !== NULL and isset( Request::i()->$unlimitedName ) )
|
||||
{
|
||||
return $this->options['unlimited'];
|
||||
}
|
||||
@@ -156,9 +163,9 @@ class _DateRange extends FormAbstract
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function html()
|
||||
public function html(): string
|
||||
{
|
||||
return \IPS\Theme::i()->getTemplate( 'forms', 'core', 'global' )->dateRange(
|
||||
return Theme::i()->getTemplate( 'forms', 'core', 'global' )->dateRange(
|
||||
$this->start->html(),
|
||||
$this->end->html(),
|
||||
$this->options['unlimited'],
|
||||
@@ -173,18 +180,18 @@ class _DateRange extends FormAbstract
|
||||
/**
|
||||
* Validate
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* @throws \LengthException
|
||||
* @throws InvalidArgumentException
|
||||
* @throws LengthException
|
||||
* @return TRUE
|
||||
*/
|
||||
public function validate()
|
||||
public function validate(): bool
|
||||
{
|
||||
$this->start->validate();
|
||||
$this->end->validate();
|
||||
|
||||
if ( $this->required and $this->value['start'] === NULL and $this->value['end'] === NULL )
|
||||
{
|
||||
throw new \InvalidArgumentException('form_required');
|
||||
throw new InvalidArgumentException('form_required');
|
||||
}
|
||||
|
||||
if( $this->customValidationCode !== NULL )
|
||||
@@ -192,5 +199,7 @@ class _DateRange extends FormAbstract
|
||||
$validationFunction = $this->customValidationCode;
|
||||
$validationFunction( $this->value );
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user