Version 5.0.0 beta 1

This commit is contained in:
Neo committed 2025-12-19 16:27:35 -08:00
1 parent 25ddeb65d6
commit 15c7beabc5
6736 files changed
+627902 -497943

No files matched your search

+49 -36
View File
@@ -11,16 +11,29 @@
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\Member;
use IPS\Output;
use IPS\Poll as SystemPoll;
use IPS\Settings;
use IPS\Theme;
use LengthException;
use function count;
use function defined;
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;
}
/**
* Poll input class for Form Builder
*/
class _Poll extends FormAbstract
class Poll extends FormAbstract
{
/**
* @brief Default Options
@@ -31,30 +44,30 @@ class _Poll extends FormAbstract
);
* @endcode
*/
protected $defaultOptions = array(
protected array $defaultOptions = array(
'allowPollOnly' => FALSE,
);
/**
* @brief Have we output the js yet?
*/
protected $jsSent = FALSE;
protected bool $jsSent = FALSE;
/**
* Get HTML
*
* @return string
*/
public function html()
public function html(): string
{
$json = array();
if ( $this->value AND \is_array( $this->value->choices ) AND \count( $this->value->choices ) )
if ( $this->value AND is_array( $this->value->choices ) AND count( $this->value->choices ) )
{
foreach ( $this->value->choices as $k => $choice )
{
$thisQuestion['title'] = $choice['question'];
$thisQuestion['multiChoice'] = ( isset( $choice['multi'] ) and $choice['multi'] ) ? TRUE : FALSE;
$thisQuestion['multiChoice'] = ( isset( $choice['multi'] ) and $choice['multi'] );
$thisQuestion['choices'] = array();
foreach ( $choice['choice'] as $i => $question )
@@ -71,18 +84,18 @@ class _Poll extends FormAbstract
if( !$this->jsSent )
{
\IPS\Output::i()->endBodyCode .= <<<EOF
Output::i()->endBodyCode .= <<<EOF
<script type='text/javascript'>
ips.setSetting('pollData', {$json} );
ips.setSetting('pollData', {$json} )
</script>
EOF;
$this->jsSent = TRUE;
}
/* Ensure poll JS is loaded */
\IPS\Output::i()->jsFiles = array_merge( \IPS\Output::i()->jsFiles, \IPS\Output::i()->js( 'front_core.js', 'core' ) );
Output::i()->jsFiles = array_merge( Output::i()->jsFiles, Output::i()->js( 'front_core.js', 'core' ) );
return \IPS\Theme::i()->getTemplate( 'forms', 'core', 'global' )->poll( $this->name, $this->value, $json, $this->options['allowPollOnly'] );
return Theme::i()->getTemplate( 'forms', 'core', 'global' )->poll( $this->name, $this->value, $json, $this->options['allowPollOnly'] );
}
/**
@@ -90,20 +103,20 @@ EOF;
*
* @return mixed
*/
public function getValue()
public function getValue(): mixed
{
/* Check we provided a value */
$formValues = parent::getValue();
/* Init object */
if ( $this->defaultValue instanceof \IPS\Poll )
if ( $this->defaultValue instanceof SystemPoll )
{
$poll = $this->defaultValue;
}
else
{
$poll = new \IPS\Poll;
$poll->starter_id = \IPS\Member::loggedIn()->member_id;
$poll = new SystemPoll;
$poll->starter_id = Member::loggedIn()->member_id;
if( isset( $this->options['itemClass'] ) )
{
@@ -139,17 +152,17 @@ EOF;
/**
* Validate
*
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
* @return TRUE
*/
public function validate()
public function validate(): bool
{
/* Required? */
if ( $this->value === NULL )
{
if ( $this->required )
{
throw new \InvalidArgumentException('form_required');
throw new InvalidArgumentException('form_required');
}
else
{
@@ -159,17 +172,17 @@ EOF;
if( mb_strlen( $this->value->poll_question ) > 255 )
{
throw new \InvalidArgumentException( \IPS\Member::loggedIn()->language()->addToStack('form_maxlength', FALSE, array( 'pluralize' => array( 255 ) ) ) );
throw new InvalidArgumentException( Member::loggedIn()->language()->addToStack('form_maxlength', FALSE, array( 'pluralize' => array( 255 ) ) ) );
}
/* Do we have at least one question? */
if ( !\count( $this->value->choices ) )
if ( !count( $this->value->choices ) )
{
throw new \InvalidArgumentException('form_poll_no_questions');
throw new InvalidArgumentException('form_poll_no_questions');
}
if ( \count( $this->value->choices ) > \IPS\Settings::i()->max_poll_questions )
if ( count( $this->value->choices ) > Settings::i()->max_poll_questions )
{
throw new \InvalidArgumentException( \IPS\Member::loggedIn()->language()->addToStack('form_poll_too_many_questions', FALSE, array( 'pluralize' => array( \IPS\Settings::i()->max_poll_questions ) ) ) );
throw new InvalidArgumentException( Member::loggedIn()->language()->addToStack('form_poll_too_many_questions', FALSE, array( 'pluralize' => array( Settings::i()->max_poll_questions ) ) ) );
}
/* Do all the questions have at least 2 options? */
@@ -178,33 +191,33 @@ EOF;
/* Do we have a question title? */
if ( empty( trim( $question['question'] ) ) )
{
throw new \InvalidArgumentException('form_poll_no_question_title');
throw new InvalidArgumentException('form_poll_no_question_title');
}
if ( \count( $question['choice'] ) < 2 )
if ( count( $question['choice'] ) < 2 )
{
throw new \InvalidArgumentException('form_poll_too_few_answers');
throw new InvalidArgumentException('form_poll_too_few_answers');
}
if ( \count( $question['choice'] ) > \IPS\Settings::i()->max_poll_choices )
if ( count( $question['choice'] ) > Settings::i()->max_poll_choices )
{
throw new \InvalidArgumentException( \IPS\Member::loggedIn()->language()->addToStack('form_poll_too_many_answers', FALSE, array( 'sprintf' => array( \IPS\Settings::i()->max_poll_choices ) ) ) );
throw new InvalidArgumentException( Member::loggedIn()->language()->addToStack('form_poll_too_many_answers', FALSE, array( 'sprintf' => array( Settings::i()->max_poll_choices ) ) ) );
}
}
/* Only validate if the timestamp is greater than zero */
if( $this->value->poll_close_date instanceof \IPS\DateTime )
if( $this->value->poll_close_date instanceof DateTime )
{
/* Check maximum date */
$maximum = \IPS\DateTime::ts( 2147483647 );
$maximum = DateTime::ts( 2147483647 );
if ( $this->value->poll_close_date > $maximum )
{
throw new \LengthException( \IPS\Member::loggedIn()->language()->addToStack('form_date_max', FALSE, array( 'sprintf' => array( $maximum->localeDate() ) ) ) );
throw new LengthException( Member::loggedIn()->language()->addToStack('form_date_max', FALSE, array( 'sprintf' => array( $maximum->localeDate() ) ) ) );
}
/* Check minimum date and time */
$minimum = \IPS\DateTime::ts( time() );
$minimum = DateTime::ts( time() );
if ( $this->value->poll_close_date < $minimum )
{
throw new \LengthException( \IPS\Member::loggedIn()->language()->addToStack('form_date_min', FALSE, array( 'sprintf' => array( $minimum->localeDate() . ' ' . $minimum->localeTime( FALSE ) ) ) ) );
throw new LengthException( Member::loggedIn()->language()->addToStack('form_date_min', FALSE, array( 'sprintf' => array( $minimum->localeDate() . ' ' . $minimum->localeTime( FALSE ) ) ) ) );
}
}
@@ -215,9 +228,9 @@ EOF;
* 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
{
return $value ? $value->pid : NULL;
}