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,26 @@
|
||||
namespace IPS\Helpers\Form;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use DomainException;
|
||||
use IPS\Member;
|
||||
use IPS\Request;
|
||||
use IPS\Theme;
|
||||
use OutOfRangeException;
|
||||
use function defined;
|
||||
use function in_array;
|
||||
use function is_array;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Select-box class for Form Builder
|
||||
*/
|
||||
class _Select extends FormAbstract
|
||||
class Select extends FormAbstract
|
||||
{
|
||||
/**
|
||||
* @brief Default Options
|
||||
@@ -45,7 +55,7 @@ class _Select extends FormAbstract
|
||||
);
|
||||
* @endcode
|
||||
*/
|
||||
protected $defaultOptions = array(
|
||||
protected array $defaultOptions = array(
|
||||
'options' => array(),
|
||||
'toggles' => array(),
|
||||
'multiple' => FALSE,
|
||||
@@ -63,32 +73,31 @@ class _Select extends FormAbstract
|
||||
'sort' => FALSE,
|
||||
'impliedUnlimited' => FALSE,
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* 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 )
|
||||
{
|
||||
/* Set the default value to the first option if one isn't provided */
|
||||
if ( $defaultValue === NULL and ( !isset( $options['noDefault'] ) OR !$options['noDefault'] ) and \is_array( $options['options'] ) )
|
||||
if ( $defaultValue === NULL and ( !isset( $options['noDefault'] ) OR !$options['noDefault'] ) and is_array( $options['options'] ) )
|
||||
{
|
||||
foreach ( $options['options'] as $k => $v )
|
||||
{
|
||||
if ( \is_array( $v ) )
|
||||
if ( is_array( $v ) )
|
||||
{
|
||||
foreach ( $v as $_k => $_v )
|
||||
{
|
||||
if ( !isset( $options['disabled'] ) OR ( \is_array( $options['disabled'] ) AND !\in_array( $_k, $options['disabled'] ) ) )
|
||||
if ( !isset( $options['disabled'] ) OR ( is_array( $options['disabled'] ) AND !in_array( $_k, $options['disabled'] ) ) )
|
||||
{
|
||||
$defaultValue = $_k;
|
||||
break;
|
||||
@@ -97,7 +106,7 @@ class _Select extends FormAbstract
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( !isset( $options['disabled'] ) OR ( \is_array( $options['disabled'] ) AND !\in_array( $k, $options['disabled'] ) ) )
|
||||
if ( !isset( $options['disabled'] ) OR ( is_array( $options['disabled'] ) AND !in_array( $k, $options['disabled'] ) ) )
|
||||
{
|
||||
$defaultValue = $k;
|
||||
break;
|
||||
@@ -120,7 +129,7 @@ class _Select extends FormAbstract
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function html()
|
||||
public function html(): string
|
||||
{
|
||||
/* Add [] to the name if this is a multi-select */
|
||||
$name = $this->name;
|
||||
@@ -133,7 +142,7 @@ class _Select extends FormAbstract
|
||||
if ( $this->options['returnLabels'] and ( $this->options['unlimited'] === NULL or $this->value !== $this->options['unlimited'] ) )
|
||||
{
|
||||
$value = array();
|
||||
if ( \is_array( $this->value ) )
|
||||
if ( is_array( $this->value ) )
|
||||
{
|
||||
foreach ( $this->value as $v )
|
||||
{
|
||||
@@ -150,7 +159,7 @@ class _Select extends FormAbstract
|
||||
$value = $this->value;
|
||||
}
|
||||
|
||||
return \IPS\Theme::i()->getTemplate( 'forms', 'core' )->select( $name, $value, $this->required, $this->parseOptions(), $this->options['multiple'], $this->options['class'], $this->options['disabled'], $this->options['toggles'], $this->htmlId, $this->options['unlimited'], $this->options['unlimitedLang'], $this->options['unlimitedToggles'], $this->options['unlimitedToggleOn'], $this->options['userSuppliedInput'], $this->options['sort'], $this->options['parse'] );
|
||||
return Theme::i()->getTemplate( 'forms', 'core' )->select( $name, $value, $this->required, $this->parseOptions(), $this->options['multiple'], $this->options['class'], $this->options['disabled'], $this->options['toggles'], $this->htmlId, $this->options['unlimited'], $this->options['unlimitedLang'], $this->options['unlimitedToggles'], $this->options['unlimitedToggleOn'], $this->options['userSuppliedInput'], $this->options['sort'], $this->options['parse'] );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,7 +167,7 @@ class _Select extends FormAbstract
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function parseOptions()
|
||||
protected function parseOptions(): array
|
||||
{
|
||||
$options = $this->options['options'];
|
||||
switch ( $this->options['parse'] )
|
||||
@@ -166,16 +175,16 @@ class _Select extends FormAbstract
|
||||
case 'lang':
|
||||
foreach ( $this->options['options'] as $k => $v )
|
||||
{
|
||||
if ( \is_array( $v ) )
|
||||
if ( is_array( $v ) )
|
||||
{
|
||||
foreach ( $v as $x => $y )
|
||||
{
|
||||
$options[ $k ][ $x ] = \IPS\Member::loggedIn()->language()->addToStack( $y );
|
||||
$options[ $k ][ $x ] = Member::loggedIn()->language()->addToStack( $y );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$options[ $k ] = \IPS\Member::loggedIn()->language()->addToStack( $v );
|
||||
$options[ $k ] = Member::loggedIn()->language()->addToStack( $v );
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -183,7 +192,7 @@ class _Select extends FormAbstract
|
||||
case 'normal':
|
||||
foreach ( $this->options['options'] as $k => $v )
|
||||
{
|
||||
if ( \is_array( $v ) )
|
||||
if ( is_array( $v ) )
|
||||
{
|
||||
foreach ( $v as $x => $y )
|
||||
{
|
||||
@@ -206,11 +215,11 @@ class _Select extends FormAbstract
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getValue()
|
||||
public function getValue(): mixed
|
||||
{
|
||||
/* Unlimited? */
|
||||
$unlimitedName = "{$this->name}_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'];
|
||||
}
|
||||
@@ -220,21 +229,35 @@ class _Select extends FormAbstract
|
||||
|
||||
if( isset( $this->options['userSuppliedInput'] ) )
|
||||
{
|
||||
if( $value == $this->options['userSuppliedInput'] )
|
||||
$name = $this->options['userSuppliedInput'] . '_' . $this->name;
|
||||
$userInputValue = Request::i()->$name;
|
||||
if( is_array( $userInputValue ) )
|
||||
{
|
||||
$name = $this->options['userSuppliedInput'] . '_' . $this->name;
|
||||
$value = mb_strpos( $name, '[' ) ? \IPS\Request::i()->valueFromArray( $name ) : \IPS\Request::i()->$name;
|
||||
$userInputValue = $userInputValue[0];
|
||||
}
|
||||
|
||||
if( is_array( $value ) )
|
||||
{
|
||||
if( in_array( $this->options['userSuppliedInput'], $value ) )
|
||||
{
|
||||
unset( $value[ array_search( $this->options['userSuppliedInput'], $value ) ] );
|
||||
$value[] = $userInputValue;
|
||||
}
|
||||
}
|
||||
elseif( $value == $this->options['userSuppliedInput'] )
|
||||
{
|
||||
$value = $userInputValue;
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $value ) AND \is_array( $value ) AND $this->options['multiple'] AND array_search( '__EMPTY', $value ) !== FALSE )
|
||||
if ( isset( $value ) AND is_array( $value ) AND $this->options['multiple'] AND in_array( '__EMPTY', $value ) )
|
||||
{
|
||||
unset( $value[ array_search( '__EMPTY', $value ) ] );
|
||||
}
|
||||
|
||||
if ( $this->options['returnLabels'] )
|
||||
{
|
||||
if ( \is_array( $value ) )
|
||||
if ( is_array( $value ) )
|
||||
{
|
||||
$return = array();
|
||||
foreach ( $value as $k => $v )
|
||||
@@ -255,10 +278,10 @@ class _Select extends FormAbstract
|
||||
/**
|
||||
* Validate
|
||||
*
|
||||
* @throws \OutOfRangeException
|
||||
* @throws OutOfRangeException
|
||||
* @return TRUE
|
||||
*/
|
||||
public function validate()
|
||||
public function validate(): bool
|
||||
{
|
||||
if( $this->options['userSuppliedInput'] )
|
||||
{
|
||||
@@ -280,7 +303,7 @@ class _Select extends FormAbstract
|
||||
}
|
||||
foreach ( $this->options['options'] as $k => $v )
|
||||
{
|
||||
if ( \is_array( $v ) )
|
||||
if ( is_array( $v ) )
|
||||
{
|
||||
$acceptableValues = array_merge( $acceptableValues, $this->options['returnLabels'] ? $v : array_keys( $v ) );
|
||||
}
|
||||
@@ -290,10 +313,10 @@ class _Select extends FormAbstract
|
||||
}
|
||||
}
|
||||
|
||||
$disabledValues = ( \is_array( $this->options['disabled'] ) ) ? $this->options['disabled'] : ( $this->options['disabled'] ? array( $this->options['disabled'] ) : array() );
|
||||
$disabledValues = ( is_array( $this->options['disabled'] ) ) ? $this->options['disabled'] : ( $this->options['disabled'] ? array( $this->options['disabled'] ) : array() );
|
||||
$acceptableValues = array_diff( $acceptableValues, $disabledValues );
|
||||
|
||||
$diff = array_diff( ( \is_array( $this->value ) ? $this->value : array( $this->value ) ), $acceptableValues );
|
||||
$diff = array_diff( ( is_array( $this->value ) ? $this->value : array( $this->value ) ), $acceptableValues );
|
||||
if ( !empty( $diff ) )
|
||||
{
|
||||
/* Is the entire thing disabled? If so, just return any default value set (if one has been set)*/
|
||||
@@ -302,12 +325,12 @@ class _Select extends FormAbstract
|
||||
return $this->defaultValue;
|
||||
}
|
||||
|
||||
throw new \OutOfRangeException( 'form_bad_value' );
|
||||
throw new OutOfRangeException( 'form_bad_value' );
|
||||
}
|
||||
|
||||
if ( $this->options['multiple'] and $this->required and empty( $this->value ) )
|
||||
{
|
||||
throw new \DomainException( 'form_required' );
|
||||
throw new DomainException( 'form_required' );
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
|
||||
Reference in new issue
Block a user