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,71 +11,89 @@
|
||||
namespace IPS\Helpers\Form;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use DomainException;
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use IPS\Helpers\Form;
|
||||
use IPS\Member;
|
||||
use IPS\Request;
|
||||
use IPS\Settings;
|
||||
use IPS\Theme;
|
||||
use LogicException;
|
||||
use function array_merge;
|
||||
use function defined;
|
||||
use function is_array;
|
||||
use function is_null;
|
||||
use function is_object;
|
||||
use function is_string;
|
||||
use const IPS\CIC;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract Class for input types for Form Builder
|
||||
*/
|
||||
abstract class _FormAbstract
|
||||
abstract class FormAbstract
|
||||
{
|
||||
/**
|
||||
* @brief Name
|
||||
*/
|
||||
protected $_name = '';
|
||||
protected string $_name = '';
|
||||
|
||||
/**
|
||||
* @brief Label
|
||||
*/
|
||||
public $label = NULL;
|
||||
public ?string $label = NULL;
|
||||
|
||||
/**
|
||||
* @brief Description
|
||||
*/
|
||||
public $description = NULL;
|
||||
public ?string $description = NULL;
|
||||
|
||||
/**
|
||||
* @brief Default Value
|
||||
*/
|
||||
public $defaultValue = NULL;
|
||||
public mixed $defaultValue = NULL;
|
||||
|
||||
/**
|
||||
* @brief Value
|
||||
*/
|
||||
public $value = NULL;
|
||||
public mixed $value = NULL;
|
||||
|
||||
/**
|
||||
* @brief Unformatted Value
|
||||
*/
|
||||
public $unformatted = NULL;
|
||||
public mixed $unformatted = NULL;
|
||||
|
||||
/**
|
||||
* @brief Required?
|
||||
*/
|
||||
public $required = FALSE;
|
||||
public bool $required = FALSE;
|
||||
|
||||
/**
|
||||
* @brief Appears Required?
|
||||
*/
|
||||
public $appearRequired = FALSE;
|
||||
public bool $appearRequired = FALSE;
|
||||
|
||||
/**
|
||||
* @brief Additional CSS classnames to use on the row
|
||||
*/
|
||||
public $rowClasses = array();
|
||||
public array $rowClasses = array();
|
||||
|
||||
/**
|
||||
* @brief Type-Specific Options
|
||||
*/
|
||||
public $options = array();
|
||||
public array $options = array();
|
||||
|
||||
/**
|
||||
* @brief Default Options
|
||||
*/
|
||||
protected $defaultOptions = array(
|
||||
protected array $defaultOptions = array(
|
||||
'disabled' => FALSE,
|
||||
);
|
||||
|
||||
@@ -87,80 +105,99 @@ abstract class _FormAbstract
|
||||
/**
|
||||
* @brief Prefix (HTML that displays before the input box)
|
||||
*/
|
||||
public $prefix;
|
||||
public ?string $prefix = NULL;
|
||||
|
||||
/**
|
||||
* @brief Suffix (HTML that displays after the input box)
|
||||
*/
|
||||
public $suffix;
|
||||
public ?string $suffix = NULL;
|
||||
|
||||
/**
|
||||
* @brief HTML ID
|
||||
*/
|
||||
public $htmlId = NULL;
|
||||
public ?string $htmlId = NULL;
|
||||
|
||||
/**
|
||||
* @brief Validation Error
|
||||
*/
|
||||
public $error = NULL;
|
||||
public ?string $error = NULL;
|
||||
|
||||
/**
|
||||
* @brief Reload form flag (Can be used by JS disabled fall backs to alter form content on submit)
|
||||
*/
|
||||
public $reloadForm = FALSE;
|
||||
public bool $reloadForm = FALSE;
|
||||
|
||||
/**
|
||||
* @brief Warning
|
||||
*/
|
||||
public $warningBox = NULL;
|
||||
public ?string $warningBox = NULL;
|
||||
|
||||
/**
|
||||
* @brief Value has been set?
|
||||
*/
|
||||
public $valueSet = FALSE;
|
||||
public bool $valueSet = FALSE;
|
||||
|
||||
/**
|
||||
* @brief Used to position an element within a form
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
public ?string $afterElement = null;
|
||||
|
||||
/**
|
||||
* Specify the tab for this element
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
public ?string $tab = 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
|
||||
* @param string $name Name
|
||||
* @param mixed|null $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|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
|
||||
* @return void
|
||||
*/
|
||||
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 )
|
||||
{
|
||||
$this->_name = $name;
|
||||
$this->required = \is_null( $required ) ? FALSE : $required;
|
||||
$this->appearRequired = \is_null( $required ) ? TRUE : $required;
|
||||
$this->required = is_null( $required ) ? FALSE : $required;
|
||||
$this->appearRequired = is_null( $required ) ? TRUE : $required;
|
||||
$this->options = array_merge( $this->defaultOptions, $options );
|
||||
$this->customValidationCode = $customValidationCode;
|
||||
$this->prefix = $prefix;
|
||||
$this->suffix = $suffix;
|
||||
$this->defaultValue = $defaultValue;
|
||||
$this->htmlId = $id ? preg_replace( "/[^a-zA-Z0-9\-_]/", "_", $id ) : NULL;
|
||||
|
||||
$this->setValue( TRUE );
|
||||
|
||||
if ( isset( $this->options['rowClasses'] ) and is_array( $this->options['rowClasses'] ) and count( $this->options['rowClasses'] ) )
|
||||
{
|
||||
$this->rowClasses = array_merge( $this->rowClasses, $this->options['rowClasses'] );
|
||||
}
|
||||
|
||||
$this->setValue(TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of the element
|
||||
*
|
||||
* @param bool $initial Whether this is the initial call or not. Do not reset default values on subsequent calls.
|
||||
* @param bool $force Set the value even if one was not submitted (done on the final validation when getting values)?
|
||||
* @return void
|
||||
* @param bool $initial Whether this is the initial call or not. Do not reset default values on subsequent calls.
|
||||
* @param bool $force Set the value even if one was not submitted (done on the final validation when getting values)?
|
||||
* @return void
|
||||
*/
|
||||
public function setValue( $initial=FALSE, $force=FALSE )
|
||||
public function setValue( bool $initial=FALSE, bool $force=FALSE ): void
|
||||
{
|
||||
$name = $this->name;
|
||||
$unlimitedKey = "{$name}_unlimited";
|
||||
$nullKey = "{$name}_null";
|
||||
|
||||
if( $force or ( mb_substr( $name, 0, 8 ) !== '_new_[x]' and ( mb_strpos( $name, '[' ) ? \IPS\Request::i()->valueFromArray( $name ) !== NULL : ( isset( \IPS\Request::i()->$name ) OR isset( \IPS\Request::i()->$unlimitedKey ) OR isset( \IPS\Request::i()->$nullKey ) ) ) ) )
|
||||
if( $force or ( mb_substr( $name, 0, 8 ) !== '_new_[x]' and ( mb_strpos( $name, '[' ) ? Request::i()->valueFromArray( $name ) !== NULL : ( isset( Request::i()->$name ) OR isset( Request::i()->$unlimitedKey ) OR isset( Request::i()->$nullKey ) ) ) ) )
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -170,7 +207,7 @@ abstract class _FormAbstract
|
||||
$this->validate();
|
||||
$this->valueSet = TRUE;
|
||||
}
|
||||
catch ( \LogicException $e )
|
||||
catch ( LogicException $e )
|
||||
{
|
||||
$this->valueSet = TRUE;
|
||||
$this->error = $e->getMessage();
|
||||
@@ -185,7 +222,7 @@ abstract class _FormAbstract
|
||||
{
|
||||
$this->value = $this->formatValue();
|
||||
}
|
||||
catch ( \LogicException $e )
|
||||
catch ( LogicException $e )
|
||||
{
|
||||
$this->error = $e->getMessage();
|
||||
}
|
||||
@@ -196,10 +233,10 @@ abstract class _FormAbstract
|
||||
/**
|
||||
* Magic get method
|
||||
*
|
||||
* @param string $property Property requested
|
||||
* @param string $property Property requested
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get( $property )
|
||||
public function __get( string $property ) : mixed
|
||||
{
|
||||
if( $property === 'name' )
|
||||
{
|
||||
@@ -212,12 +249,12 @@ abstract class _FormAbstract
|
||||
/**
|
||||
* Magic set method
|
||||
*
|
||||
* @param string $property Property requested
|
||||
* @param string $property Property requested
|
||||
* @param mixed $value Value to set
|
||||
* @return void
|
||||
* @note We are operating on the 'name' property so that if an element's name is reset after the element is initialized we can reinitialize the value
|
||||
*/
|
||||
public function __set( $property, $value )
|
||||
public function __set( string $property, mixed $value ) : void
|
||||
{
|
||||
if( $property === 'name' )
|
||||
{
|
||||
@@ -239,10 +276,10 @@ abstract class _FormAbstract
|
||||
/**
|
||||
* Get HTML
|
||||
*
|
||||
* @param \IPS\Helpers\Form|null $form Form helper object
|
||||
* @param Form|null $form Form helper object
|
||||
* @return string
|
||||
*/
|
||||
public function rowHtml( $form=NULL )
|
||||
public function rowHtml( Form $form=NULL ): string
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -255,15 +292,15 @@ abstract class _FormAbstract
|
||||
$label = $this->name;
|
||||
if ( isset( $this->options['labelSprintf'] ) )
|
||||
{
|
||||
$label = \IPS\Member::loggedIn()->language()->addToStack( $label, FALSE, array( 'sprintf' => $this->options['labelSprintf'] ) );
|
||||
$label = Member::loggedIn()->language()->addToStack( $label, FALSE, array( 'sprintf' => $this->options['labelSprintf'] ) );
|
||||
}
|
||||
else if ( isset( $this->options['labelHtmlSprintf'] ) )
|
||||
{
|
||||
$label = \IPS\Member::loggedIn()->language()->addToStack( $label, FALSE, array( 'htmlsprintf' => $this->options['labelHtmlSprintf'] ) );
|
||||
$label = Member::loggedIn()->language()->addToStack( $label, FALSE, array( 'htmlsprintf' => $this->options['labelHtmlSprintf'] ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
$label = \IPS\Member::loggedIn()->language()->addToStack( $label );
|
||||
$label = Member::loggedIn()->language()->addToStack( $label );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -276,7 +313,7 @@ abstract class _FormAbstract
|
||||
else
|
||||
{
|
||||
$desc = $this->name . '_desc';
|
||||
$desc = \IPS\Member::loggedIn()->language()->addToStack( $desc, FALSE, array( 'returnBlank' => TRUE, 'returnInto' => \IPS\Theme::i()->getTemplate( 'forms', 'core', 'global' )->rowDesc( $label, $html, $this->appearRequired, $this->error, $this->prefix, $this->suffix, $this->htmlId ?: ( $form ? "{$form->id}_{$this->name}" : NULL ), $this, $form ) ) );
|
||||
$desc = Member::loggedIn()->language()->addToStack( $desc, FALSE, array( 'returnBlank' => TRUE, 'returnInto' => Theme::i()->getTemplate( 'forms', 'core', 'global' )->rowDesc( $label, $html, $this->appearRequired, $this->error, $this->prefix, $this->suffix, $this->htmlId ?: ( $form ? "{$form->id}_{$this->name}" : NULL ), $this, $form ) ) );
|
||||
}
|
||||
|
||||
if ( $this->warningBox )
|
||||
@@ -286,7 +323,7 @@ abstract class _FormAbstract
|
||||
else
|
||||
{
|
||||
$warning = $this->name . '_warning';
|
||||
$warning = \IPS\Member::loggedIn()->language()->addToStack( $warning, FALSE, array( 'returnBlank' => TRUE, 'returnInto' => \IPS\Theme::i()->getTemplate( 'forms', 'core', 'global' )->rowWarning( $label, $html, $this->appearRequired, $this->error, $this->prefix, $this->suffix, $this->htmlId ?: ( $form ? "{$form->id}_{$this->name}" : NULL ), $this, $form ) ) );
|
||||
$warning = Member::loggedIn()->language()->addToStack( $warning, FALSE, array( 'returnBlank' => TRUE, 'returnInto' => Theme::i()->getTemplate( 'forms', 'core', 'global' )->rowWarning( $label, $html, $this->appearRequired, $this->error, $this->prefix, $this->suffix, $this->htmlId ?: ( $form ? "{$form->id}_{$this->name}" : NULL ), $this, $form ) ) );
|
||||
}
|
||||
|
||||
if( array_key_exists( 'endSuffix', $this->options ) )
|
||||
@@ -297,14 +334,14 @@ abstract class _FormAbstract
|
||||
/* Some elements support an array for suffix, such as Number which supports preUnlimited and postUnlimited. We need to wipe out
|
||||
the suffix here before calling the row() template, however, which only supports a string and throws an Array to string conversion error.
|
||||
By this point, the element template has already ran and used the suffix if designed to */
|
||||
if( \is_array( $this->suffix ) )
|
||||
if( is_array( $this->suffix ) )
|
||||
{
|
||||
$this->suffix = '';
|
||||
}
|
||||
|
||||
return \IPS\Theme::i()->getTemplate( 'forms', 'core' )->row( $label, $html, $desc, $warning, $this->appearRequired, $this->error, $this->prefix, $this->suffix, $this->htmlId ?: ( $form ? "{$form->id}_{$this->name}" : NULL ), $this, $form, $this->rowClasses );
|
||||
return Theme::i()->getTemplate( 'forms', 'core' )->row( $label, $html, $desc, $warning, $this->appearRequired, $this->error, $this->prefix, $this->suffix, $this->htmlId ?: ( $form ? "{$form->id}_{$this->name}" : NULL ), $this, $form, $this->rowClasses );
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
catch ( Exception $e )
|
||||
{
|
||||
if ( \IPS\IN_DEV )
|
||||
{
|
||||
@@ -322,7 +359,7 @@ abstract class _FormAbstract
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getLabelForAttribute()
|
||||
public function getLabelForAttribute(): mixed
|
||||
{
|
||||
return $this->htmlId ?? $this->name;
|
||||
}
|
||||
@@ -332,10 +369,10 @@ abstract class _FormAbstract
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getValue()
|
||||
public function getValue(): mixed
|
||||
{
|
||||
$name = $this->name;
|
||||
$value = ( mb_strpos( $name, '[' ) OR ( isset( $this->options['multiple'] ) AND $this->options['multiple'] === TRUE ) ) ? \IPS\Request::i()->valueFromArray( $name ) : \IPS\Request::i()->$name;
|
||||
$value = ( mb_strpos( $name, '[' ) OR ( isset( $this->options['multiple'] ) AND $this->options['multiple'] === TRUE ) ) ? Request::i()->valueFromArray( $name ) : Request::i()->$name;
|
||||
|
||||
if( isset( $this->options['disabled'] ) AND $this->options['disabled'] === TRUE AND $value === NULL )
|
||||
{
|
||||
@@ -350,7 +387,7 @@ abstract class _FormAbstract
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function formatValue()
|
||||
public function formatValue(): mixed
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
@@ -358,21 +395,21 @@ abstract class _FormAbstract
|
||||
/**
|
||||
* Validate
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* @throws InvalidArgumentException
|
||||
* @return TRUE
|
||||
*/
|
||||
public function validate()
|
||||
public function validate(): bool
|
||||
{
|
||||
if( ( $this->value === '' OR ( \is_array( $this->value ) AND empty( $this->value ) ) ) and $this->required )
|
||||
if( ( $this->value === '' OR ( is_array( $this->value ) AND empty( $this->value ) ) ) and $this->required )
|
||||
{
|
||||
throw new \InvalidArgumentException('form_required');
|
||||
throw new InvalidArgumentException('form_required');
|
||||
}
|
||||
|
||||
if ( \IPS\Settings::i()->getFromConfGlobal('sql_utf8mb4') !== TRUE )
|
||||
if ( Settings::i()->getFromConfGlobal('sql_utf8mb4') !== TRUE )
|
||||
{
|
||||
if ( !static::utf8mb4Check( $this->value ) )
|
||||
{
|
||||
throw new \DomainException( \IPS\Member::loggedIn()->isAdmin() ? ( \IPS\CIC ? 'form_multibyte_unicode_admin_cic' : 'form_multibyte_unicode_admin' ) : 'form_multibyte_unicode' );
|
||||
throw new DomainException( Member::loggedIn()->isAdmin() ? ( CIC ? 'form_multibyte_unicode_admin_cic' : 'form_multibyte_unicode_admin' ) : 'form_multibyte_unicode' );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -391,9 +428,9 @@ abstract class _FormAbstract
|
||||
* @param mixed $value The value
|
||||
* @return bool
|
||||
*/
|
||||
public static function utf8mb4Check( $value )
|
||||
public static function utf8mb4Check( mixed $value ): bool
|
||||
{
|
||||
if ( \is_array( $value ) )
|
||||
if ( is_array( $value ) )
|
||||
{
|
||||
foreach ( $value as $_value )
|
||||
{
|
||||
@@ -403,9 +440,9 @@ abstract class _FormAbstract
|
||||
}
|
||||
}
|
||||
}
|
||||
elseif ( \is_string( $value ) )
|
||||
elseif ( is_string( $value ) )
|
||||
{
|
||||
return (bool) !preg_match( '/[\x{10000}-\x{10FFFF}]/u', $value );
|
||||
return !preg_match( '/[\x{10000}-\x{10FFFF}]/u', $value );
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
@@ -414,15 +451,15 @@ abstract class _FormAbstract
|
||||
* String Value
|
||||
*
|
||||
* @param mixed $value The value
|
||||
* @return string
|
||||
* @return string|int|null
|
||||
*/
|
||||
public static function stringValue( $value )
|
||||
public static function stringValue( mixed $value ): string|int|null
|
||||
{
|
||||
if ( \is_array( $value ) )
|
||||
if ( is_array( $value ) )
|
||||
{
|
||||
return implode( ',', array_map( function( $v )
|
||||
{
|
||||
if ( \is_object( $v ) )
|
||||
if ( is_object( $v ) )
|
||||
{
|
||||
return (string) $v;
|
||||
}
|
||||
@@ -432,4 +469,26 @@ abstract class _FormAbstract
|
||||
|
||||
return (string) $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the position of the element within the form
|
||||
*
|
||||
* @param string|null $afterElement
|
||||
* @param string|null $tab
|
||||
* @return $this
|
||||
*/
|
||||
public function setPosition( ?string $afterElement=null, ?string $tab=null ) : static
|
||||
{
|
||||
if( $afterElement )
|
||||
{
|
||||
$this->afterElement = $afterElement;
|
||||
}
|
||||
if( $tab )
|
||||
{
|
||||
$this->tab = $tab;
|
||||
}
|
||||
|
||||
/* Daisy chaining */
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user