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,39 +11,50 @@
|
||||
namespace IPS\Poll\Api\GraphQL;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use IPS\Api\GraphQL\SafeException;
|
||||
use IPS\Member;
|
||||
use IPS\Poll;
|
||||
use IPS\Poll\Vote;
|
||||
use function count;
|
||||
use function defined;
|
||||
use function intval;
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Base mutator class for polls
|
||||
*/
|
||||
class _PollMutator
|
||||
class PollMutator
|
||||
{
|
||||
/**
|
||||
* Vote in a poll
|
||||
*
|
||||
* @param \IPS\Poll $poll Poll reference
|
||||
* @return void
|
||||
* @param Poll $poll Poll reference
|
||||
* @param array|null $votes
|
||||
* @return void
|
||||
*/
|
||||
protected function _vote( $poll, $votes )
|
||||
protected function _vote( Poll $poll, ?array $votes ) : void
|
||||
{
|
||||
if( !$poll instanceof \IPS\Poll )
|
||||
if( !$poll instanceof Poll )
|
||||
{
|
||||
throw new \IPS\Api\GraphQL\SafeException( 'INVALID_POLL', 'GQL/0007/1', 403 );
|
||||
throw new SafeException( 'INVALID_POLL', 'GQL/0007/1', 403 );
|
||||
}
|
||||
|
||||
if( !$poll->canVote() )
|
||||
{
|
||||
throw new \IPS\Api\GraphQL\SafeException( 'CANNOT_VOTE', 'GQL/0007/2', 403 );
|
||||
throw new SafeException( 'CANNOT_VOTE', 'GQL/0007/2', 403 );
|
||||
}
|
||||
|
||||
// A null vote is viewing the results and forfeiting the ability to vote again
|
||||
if( $votes === NULL || !\count( $votes ) )
|
||||
if( $votes === NULL || !count( $votes ) )
|
||||
{
|
||||
$vote = \IPS\Poll\Vote::fromForm( NULL );
|
||||
$vote = Vote::fromForm( NULL );
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -52,12 +63,12 @@ class _PollMutator
|
||||
// Rebuild the vote array to match form input
|
||||
foreach( $votes as $vote )
|
||||
{
|
||||
if( \is_array( $vote['choices'] ) )
|
||||
if( is_array( $vote['choices'] ) )
|
||||
{
|
||||
if( \count( $vote['choices'] ) === 1 )
|
||||
if( count( $vote['choices'] ) === 1 )
|
||||
{
|
||||
// When coming from a form we save this as a string without typecasting first, so replicating here for consistency
|
||||
$choice = (string) \intval( $vote['choices'][0] );
|
||||
$choice = (string) intval( $vote['choices'][0] );
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -67,15 +78,15 @@ class _PollMutator
|
||||
else
|
||||
{
|
||||
// When coming from a form we save this as a string without typecasting first, so replicating here for consistency
|
||||
$choice = (string) \intval( $vote['choices'] );
|
||||
$choice = (string) intval( $vote['choices'] );
|
||||
}
|
||||
|
||||
$voteAsForm[ $vote['id'] ] = $choice;
|
||||
}
|
||||
|
||||
$vote = \IPS\Poll\Vote::fromForm( $voteAsForm );
|
||||
$vote = Vote::fromForm( $voteAsForm );
|
||||
}
|
||||
|
||||
$poll->addVote( $vote, \IPS\Member::loggedIn() );
|
||||
$poll->addVote( $vote, Member::loggedIn() );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user