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 InvalidArgumentException;
|
||||
use IPS\Member as SystemMember;
|
||||
use IPS\Theme;
|
||||
use function array_slice;
|
||||
use function count;
|
||||
use function defined;
|
||||
use function is_array;
|
||||
use function is_null;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Member input class for Form Builder
|
||||
*/
|
||||
class _Member extends Text
|
||||
class Member extends Text
|
||||
{
|
||||
/**
|
||||
* @brief Default Options
|
||||
@@ -30,7 +40,7 @@ class _Member extends Text
|
||||
);
|
||||
* @endcode
|
||||
*/
|
||||
public $childDefaultOptions = array(
|
||||
public array $childDefaultOptions = array(
|
||||
'multiple' => 1,
|
||||
);
|
||||
|
||||
@@ -38,41 +48,43 @@ class _Member extends Text
|
||||
/**
|
||||
* 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 )
|
||||
{
|
||||
$this->defaultOptions['autocomplete'] = array(
|
||||
'source' => 'app=core&module=system&controller=ajax&do=findMember',
|
||||
'resultItemTemplate' => 'core.autocomplete.memberItem',
|
||||
'commaTrigger' => false,
|
||||
'unique' => true,
|
||||
'minAjaxLength' => 3,
|
||||
'disallowedCharacters' => array(),
|
||||
'lang' => 'mem_optional',
|
||||
'source' => 'app=core&module=system&controller=ajax&do=findMember',
|
||||
'resultItemTemplate' => 'core.autocomplete.memberItem',
|
||||
'commaTrigger' => false,
|
||||
'unique' => true,
|
||||
'minAjaxLength' => 3,
|
||||
'disallowedCharacters' => array(),
|
||||
'lang' => 'mem_optional',
|
||||
'suggestionsOnly' => true
|
||||
);
|
||||
if (\count($options) and array_key_exists('multiple', $options) and $options['multiple'] > 0)
|
||||
if( count( $options ) and array_key_exists( 'multiple', $options ) and $options['multiple'] > 0 )
|
||||
{
|
||||
$this->defaultOptions['autocomplete']['maxItems'] = $options['multiple'];
|
||||
} elseif (!array_key_exists('multiple', $options))
|
||||
}
|
||||
elseif ( !array_key_exists( 'multiple', $options ) )
|
||||
{
|
||||
$this->defaultOptions['autocomplete']['maxItems'] = $this->childDefaultOptions['multiple'];
|
||||
}
|
||||
|
||||
/* Explicitly merge autocomplete options */
|
||||
if( array_key_exists('autocomplete', $options ) )
|
||||
if ( array_key_exists( 'autocomplete', $options ) )
|
||||
{
|
||||
$options['autocomplete'] = array_merge( $options['autocomplete'], $this->defaultOptions['autocomplete'] );
|
||||
$options['autocomplete'] = array_merge( $this->defaultOptions['autocomplete'], $options['autocomplete'] );
|
||||
$options['suggestionsOnly'] = true;
|
||||
}
|
||||
|
||||
|
||||
parent::__construct( $name, $defaultValue, $required, $options, $customValidationCode, $prefix, $suffix, $id );
|
||||
}
|
||||
|
||||
@@ -81,41 +93,41 @@ class _Member extends Text
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function html()
|
||||
public function html(): string
|
||||
{
|
||||
$value = $this->value;
|
||||
if ( \is_array( $this->value ) )
|
||||
if ( is_array( $this->value ) )
|
||||
{
|
||||
$value = array();
|
||||
foreach ( $this->value as $v )
|
||||
{
|
||||
$value[] = ( $v instanceof \IPS\Member ) ? $v->name : $v;
|
||||
$value[] = ( $v instanceof SystemMember ) ? $v->name : $v;
|
||||
}
|
||||
$value = implode( "\n", $value );
|
||||
}
|
||||
elseif ( $value instanceof \IPS\Member )
|
||||
elseif ( $value instanceof SystemMember )
|
||||
{
|
||||
$value = $value->name;
|
||||
}
|
||||
|
||||
/* This value is decoded by the JS widget before use. */
|
||||
return \IPS\Theme::i()->getTemplate( 'forms', 'core', 'global' )->text( $this->name, 'text', ( $this->options['autocomplete'] AND !\is_null( $value ) ) ? htmlspecialchars( $value, ENT_QUOTES | ENT_DISALLOWED, 'UTF-8', FALSE ) : $value, $this->required, $this->options['maxLength'], $this->options['size'], $this->options['disabled'], $this->options['autocomplete'], $this->options['placeholder'], NULL, $this->options['nullLang'] );
|
||||
return Theme::i()->getTemplate( 'forms', 'core', 'global' )->text( $this->name, 'text', ( $this->options['autocomplete'] AND !is_null( $value ) ) ? htmlspecialchars( $value, ENT_QUOTES | ENT_DISALLOWED, 'UTF-8', FALSE ) : $value, $this->required, $this->options['maxLength'], $this->options['size'], $this->options['disabled'], $this->options['autocomplete'], $this->options['placeholder'], NULL, $this->options['nullLang'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Format Value
|
||||
*
|
||||
* @return \IPS\Member|array|NULL|FALSE
|
||||
* @return mixed
|
||||
*/
|
||||
public function formatValue()
|
||||
public function formatValue(): mixed
|
||||
{
|
||||
if ( $this->value !== '' and !( $this->value instanceof \IPS\Member ) )
|
||||
if ( $this->value !== '' and !( $this->value instanceof SystemMember ) )
|
||||
{
|
||||
$return = array();
|
||||
|
||||
foreach ( \is_array( $this->value ) ? $this->value : explode( "\n", $this->value ) as $v )
|
||||
foreach ( is_array( $this->value ) ? $this->value : explode( "\n", $this->value ) as $v )
|
||||
{
|
||||
if ( $v instanceof \IPS\Member )
|
||||
if ( $v instanceof SystemMember )
|
||||
{
|
||||
$return[ $v->member_id ] = $v;
|
||||
}
|
||||
@@ -123,7 +135,7 @@ class _Member extends Text
|
||||
{
|
||||
$v = html_entity_decode( $v, ENT_QUOTES, 'UTF-8' );
|
||||
|
||||
$member = \IPS\Member::load( $v, 'name' );
|
||||
$member = SystemMember::load( $v, 'name' );
|
||||
if ( $member->member_id )
|
||||
{
|
||||
if ( $this->options['multiple'] === 1 )
|
||||
@@ -137,7 +149,7 @@ class _Member extends Text
|
||||
|
||||
if ( !empty( $return ) )
|
||||
{
|
||||
return ( $this->options['multiple'] === NULL or $this->options['multiple'] == 0 ) ? $return : \array_slice( $return, 0, $this->options['multiple'] );
|
||||
return ( $this->options['multiple'] === NULL or $this->options['multiple'] == 0 ) ? $return : array_slice( $return, 0, $this->options['multiple'] );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,27 +159,29 @@ class _Member extends Text
|
||||
/**
|
||||
* Validate
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* @throws InvalidArgumentException
|
||||
* @return TRUE
|
||||
*/
|
||||
public function validate()
|
||||
public function validate(): bool
|
||||
{
|
||||
parent::validate();
|
||||
|
||||
if ( $this->value !== '' and !( $this->value instanceof \IPS\Member ) and !\is_array( $this->value ) )
|
||||
if ( $this->value !== '' and !( $this->value instanceof SystemMember ) and !is_array( $this->value ) )
|
||||
{
|
||||
throw new \InvalidArgumentException('form_member_bad');
|
||||
throw new InvalidArgumentException('form_member_bad');
|
||||
}
|
||||
else if ( \is_array( $this->value ) )
|
||||
else if ( is_array( $this->value ) )
|
||||
{
|
||||
foreach( $this->value AS $value )
|
||||
{
|
||||
if ( $value !== '' AND !( $value instanceof \IPS\Member ) )
|
||||
if ( $value !== '' AND !( $value instanceof SystemMember ) )
|
||||
{
|
||||
throw new \InvalidArgumentException( \IPS\Member::loggedIn()->language()->addToStack( 'form_member_bad_multiple', FALSE, array( 'sprintf' => array( $value ) ) ) );
|
||||
throw new InvalidArgumentException( SystemMember::loggedIn()->language()->addToStack( 'form_member_bad_multiple', FALSE, array( 'sprintf' => array( $value ) ) ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
@@ -175,13 +189,13 @@ class _Member extends Text
|
||||
* String Value
|
||||
*
|
||||
* @param mixed $value The value
|
||||
* @return string|null
|
||||
* @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 ) )
|
||||
{
|
||||
if( $value instanceof \IPS\Member )
|
||||
if( $value instanceof SystemMember )
|
||||
{
|
||||
$value = array( $value );
|
||||
}
|
||||
@@ -195,7 +209,7 @@ class _Member extends Text
|
||||
}
|
||||
}
|
||||
|
||||
if ( !\count( $value ) )
|
||||
if ( !count( $value ) )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user