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,16 +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\Member;
|
||||
use IPS\Request;
|
||||
use IPS\Theme;
|
||||
use LengthException;
|
||||
use function defined;
|
||||
use function floatval;
|
||||
use function intval;
|
||||
use function is_int;
|
||||
use function is_numeric;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Text input class for Form Builder
|
||||
*/
|
||||
class _Number extends FormAbstract
|
||||
class Number extends FormAbstract
|
||||
{
|
||||
/**
|
||||
* @brief Default Options
|
||||
@@ -41,7 +53,7 @@ class _Number extends FormAbstract
|
||||
);
|
||||
* @endcode
|
||||
*/
|
||||
protected $defaultOptions = array(
|
||||
protected array $defaultOptions = array(
|
||||
'min' => 0,
|
||||
'max' => NULL,
|
||||
'unlimited' => NULL,
|
||||
@@ -55,27 +67,26 @@ class _Number extends FormAbstract
|
||||
'disabled' => FALSE,
|
||||
'endSuffix' => NULL,
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* 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 )
|
||||
{
|
||||
/* Specify there's a value if the unlimited element contains a value */
|
||||
$unlimitedName = "{$name}_unlimited";
|
||||
if( isset( $options['unlimited'] ) and $options['unlimited'] !== NULL and isset( \IPS\Request::i()->$unlimitedName ) )
|
||||
if( isset( $options['unlimited'] ) and $options['unlimited'] !== NULL and isset( Request::i()->$unlimitedName ) )
|
||||
{
|
||||
\IPS\Request::i()->$name = $options['unlimited'];
|
||||
Request::i()->$name = $options['unlimited'];
|
||||
}
|
||||
|
||||
/* Work out the step */
|
||||
@@ -98,19 +109,19 @@ class _Number extends FormAbstract
|
||||
/**
|
||||
* Generated HTML store
|
||||
*/
|
||||
protected static $generatedHtml = array();
|
||||
protected static array $generatedHtml = array();
|
||||
|
||||
/**
|
||||
* Get HTML
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function html()
|
||||
public function html(): string
|
||||
{
|
||||
/* Display */
|
||||
if ( ! isset( static::$generatedHtml[ $this->_name ] ) )
|
||||
{
|
||||
static::$generatedHtml[ $this->_name ] = \IPS\Theme::i()->getTemplate( 'forms', 'core', 'global' )->number( $this->name, $this->value, $this->required, $this->options['unlimited'], $this->options['range'], $this->options['min'], $this->options['max'], $this->options['step'], $this->options['decimals'], $this->options['unlimitedLang'], $this->options['disabled'], $this->suffix, $this->options['unlimitedToggles'], $this->options['unlimitedToggleOn'], $this->options['valueToggles'], NULL, $this->prefix );
|
||||
static::$generatedHtml[ $this->_name ] = Theme::i()->getTemplate( 'forms', 'core', 'global' )->number( $this->name, $this->value, $this->required, $this->options['unlimited'], $this->options['range'], $this->options['min'], $this->options['max'], $this->options['step'], $this->options['decimals'], $this->options['unlimitedLang'], $this->options['disabled'], $this->suffix, $this->options['unlimitedToggles'], $this->options['unlimitedToggleOn'], $this->options['valueToggles'], NULL, $this->prefix );
|
||||
}
|
||||
|
||||
/* Because we mess around with suffixes in the Number field, we store the HTML here otherwise when it is regenerated, the suffix is missing */
|
||||
@@ -122,14 +133,14 @@ class _Number extends FormAbstract
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getValue()
|
||||
public function getValue(): mixed
|
||||
{
|
||||
$name = $this->name;
|
||||
|
||||
if ( $pos = mb_strpos( $name, '[' ) )
|
||||
{
|
||||
$_name = mb_substr( preg_replace( '/\[(.+?)\]/', '[$1_unlimited]', $name, 1 ), 0, $pos );
|
||||
$val = \IPS\Request::i()->$_name;
|
||||
$val = Request::i()->$_name;
|
||||
preg_match_all( '/\[(.+?)\]/', $name, $matches );
|
||||
foreach ( $matches[1] as $_name )
|
||||
{
|
||||
@@ -149,17 +160,17 @@ class _Number extends FormAbstract
|
||||
else
|
||||
{
|
||||
$unlimitedName = "{$name}_unlimited";
|
||||
$unlimitedChecked = isset( \IPS\Request::i()->$unlimitedName );
|
||||
$unlimitedChecked = isset( Request::i()->$unlimitedName );
|
||||
}
|
||||
|
||||
/* Unlimited? */
|
||||
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'];
|
||||
}
|
||||
|
||||
/* Get value */
|
||||
return \IPS\Request::i()->$name;
|
||||
return Request::i()->$name;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -167,7 +178,7 @@ class _Number extends FormAbstract
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function formatValue()
|
||||
public function formatValue(): mixed
|
||||
{
|
||||
/* Get value */
|
||||
$value = $this->value;
|
||||
@@ -183,20 +194,20 @@ class _Number extends FormAbstract
|
||||
of the locale to . in an attempt to get us to the former from the latter, but we don't
|
||||
do the thousand seperator in case what we received was the former alredy, which would turn
|
||||
for example "10,00" into "1000" */
|
||||
$value = str_replace( trim( \IPS\Member::loggedIn()->language()->locale['decimal_point'] ), '.', $value );
|
||||
$value = str_replace( trim( Member::loggedIn()->language()->locale['decimal_point'] ), '.', $value );
|
||||
|
||||
/* Convert to int/float */
|
||||
if ( $this->options['decimals'] and $value != '' )
|
||||
{
|
||||
$value = \floatval( $value );
|
||||
if ( \is_int( $this->options['decimals'] ) )
|
||||
$value = floatval( $value );
|
||||
if ( is_int( $this->options['decimals'] ) )
|
||||
{
|
||||
$value = round( $value, $this->options['decimals'] );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$value = ( $value != '' ) ? \intval( $value ) : '';
|
||||
$value = ( $value != '' ) ? intval( $value ) : '';
|
||||
}
|
||||
|
||||
/* Return */
|
||||
@@ -206,29 +217,31 @@ class _Number extends FormAbstract
|
||||
/**
|
||||
* Validate
|
||||
*
|
||||
* @throws \LengthException
|
||||
* @throws LengthException
|
||||
* @return TRUE
|
||||
*/
|
||||
public function validate()
|
||||
public function validate(): bool
|
||||
{
|
||||
parent::validate();
|
||||
|
||||
if ( $this->options['unlimited'] === NULL or $this->value !== $this->options['unlimited'] )
|
||||
{
|
||||
/* If it's not numeric, throw an exception */
|
||||
if ( ( !\is_numeric( $this->value ) and $this->value !== '' and $this->required === FALSE ) or ( !\is_numeric( $this->value ) and $this->required === TRUE ) )
|
||||
if ( ( !is_numeric( $this->value ) and $this->value !== '' and $this->required === FALSE ) or ( !is_numeric( $this->value ) and $this->required === TRUE ) )
|
||||
{
|
||||
throw new \InvalidArgumentException( 'form_number_bad' );
|
||||
throw new InvalidArgumentException( 'form_number_bad' );
|
||||
}
|
||||
|
||||
if ( $this->options['min'] !== NULL and (int) $this->value < $this->options['min'] )
|
||||
{
|
||||
throw new \LengthException( \IPS\Member::loggedIn()->language()->addToStack('form_number_min', FALSE, array( 'sprintf' => array( $this->options['min'] ) ) ) );
|
||||
throw new LengthException( Member::loggedIn()->language()->addToStack('form_number_min', FALSE, array( 'sprintf' => array( $this->options['min'] ) ) ) );
|
||||
}
|
||||
if ( $this->options['max'] !== NULL and (int) $this->value > $this->options['max'] )
|
||||
{
|
||||
throw new \LengthException( \IPS\Member::loggedIn()->language()->addToStack('form_number_max', FALSE, array( 'sprintf' => array( $this->options['max'] ) ) ) );
|
||||
throw new LengthException( Member::loggedIn()->language()->addToStack('form_number_max', FALSE, array( 'sprintf' => array( $this->options['max'] ) ) ) );
|
||||
}
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user