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,46 +11,69 @@
|
||||
namespace IPS\core\Achievements;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use ArrayIterator;
|
||||
use DomainException;
|
||||
use Exception;
|
||||
use IPS\core\CustomBadge;
|
||||
use IPS\Data\Store;
|
||||
use IPS\Db;
|
||||
use IPS\File;
|
||||
use IPS\Helpers\Form;
|
||||
use IPS\Helpers\Form\Number;
|
||||
use IPS\Helpers\Form\Translatable;
|
||||
use IPS\Helpers\Form\Upload;
|
||||
use IPS\Image;
|
||||
use IPS\Lang;
|
||||
use IPS\Member;
|
||||
use IPS\Node\Model;
|
||||
use IPS\Patterns\ActiveRecordIterator;
|
||||
use IPS\Settings;
|
||||
use IPS\Theme;
|
||||
use XMLReader;
|
||||
use function count;
|
||||
use function defined;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rank Model
|
||||
*/
|
||||
class _Rank extends \IPS\Node\Model
|
||||
class Rank extends Model
|
||||
{
|
||||
/**
|
||||
* @brief Database Table
|
||||
*/
|
||||
public static $databaseTable = 'core_member_ranks';
|
||||
public static ?string $databaseTable = 'core_member_ranks';
|
||||
|
||||
/**
|
||||
* @brief [Node] Order Database Column
|
||||
*/
|
||||
public static $databaseColumnOrder = 'points';
|
||||
public static ?string $databaseColumnOrder = 'points';
|
||||
|
||||
/**
|
||||
* @brief [Node] Sortable?
|
||||
*/
|
||||
public static $nodeSortable = FALSE;
|
||||
public static bool $nodeSortable = FALSE;
|
||||
|
||||
/**
|
||||
* @brief Multiton Store
|
||||
*/
|
||||
protected static $multitons;
|
||||
protected static array $multitons;
|
||||
|
||||
/**
|
||||
* @brief [Node] Node Title
|
||||
*/
|
||||
public static $nodeTitle = 'menu__core_achievements_ranks';
|
||||
public static string $nodeTitle = 'menu__core_achievements_ranks';
|
||||
|
||||
/**
|
||||
* @brief [Node] Title prefix. If specified, will look for a language key with "{$key}_title" as the key
|
||||
*/
|
||||
public static $titleLangPrefix = 'core_member_rank_';
|
||||
public static ?string $titleLangPrefix = 'core_member_rank_';
|
||||
|
||||
/**
|
||||
* @brief [Node] ACP Restrictions
|
||||
@@ -68,7 +91,7 @@ class _Rank extends \IPS\Node\Model
|
||||
'prefix' => 'foo_', // [Optional] Rather than specifying each key in the map, you can specify a prefix, and it will automatically look for restrictions with the key "[prefix]_add/edit/permissions/delete"
|
||||
* @endcode
|
||||
*/
|
||||
protected static $restrictions = array(
|
||||
protected static ?array $restrictions = array(
|
||||
'app' => 'core',
|
||||
'module' => 'achievements',
|
||||
'prefix' => 'ranks_',
|
||||
@@ -78,7 +101,14 @@ class _Rank extends \IPS\Node\Model
|
||||
* @brief [ActiveRecord] Caches
|
||||
* @note Defined cache keys will be cleared automatically as needed
|
||||
*/
|
||||
protected $caches = ['achievementRanks'];
|
||||
protected array $caches = ['achievementRanks'];
|
||||
|
||||
/**
|
||||
* Determines if this class can be extended via UI Extension
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public static bool $canBeExtended = true;
|
||||
|
||||
/**
|
||||
* [ActiveRecord] Get cached rules
|
||||
@@ -87,21 +117,21 @@ class _Rank extends \IPS\Node\Model
|
||||
*/
|
||||
public static function getStore(): array
|
||||
{
|
||||
if ( !isset( \IPS\Data\Store::i()->achievementRanks ) )
|
||||
if ( !isset( Store::i()->achievementRanks ) )
|
||||
{
|
||||
\IPS\Data\Store::i()->achievementRanks = iterator_to_array( \IPS\Db::i()->select( '*', static::$databaseTable, NULL, 'points' )->setKeyField( static::$databasePrefix . static::$databaseColumnId ) );
|
||||
Store::i()->achievementRanks = iterator_to_array( Db::i()->select( '*', static::$databaseTable, NULL, 'points' )->setKeyField( static::$databasePrefix . static::$databaseColumnId ) );
|
||||
}
|
||||
|
||||
return iterator_to_array( new \IPS\Patterns\ActiveRecordIterator( new \ArrayIterator( \IPS\Data\Store::i()->achievementRanks ), 'IPS\core\Achievements\Rank' ) );
|
||||
return iterator_to_array( new ActiveRecordIterator( new ArrayIterator( Store::i()->achievementRanks ), 'IPS\core\Achievements\Rank' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Work out the rank for a given number of points
|
||||
*
|
||||
* @param int $points Number of points
|
||||
* @return \IPS\core\Achievements\Rank|NULL
|
||||
* @return Rank|NULL
|
||||
*/
|
||||
public static function fromPoints( $points ): ?\IPS\core\Achievements\Rank
|
||||
public static function fromPoints( int $points ): ?Rank
|
||||
{
|
||||
$return = NULL;
|
||||
foreach ( static::getStore() as $rank )
|
||||
@@ -124,19 +154,24 @@ class _Rank extends \IPS\Node\Model
|
||||
* @note Return the class for the icon (e.g. 'globe', the 'fa fa-' is added automatically so you do not need this here)
|
||||
* @return mixed
|
||||
*/
|
||||
protected function get__icon()
|
||||
protected function get__icon(): mixed
|
||||
{
|
||||
if ( !$this->rank_use_image )
|
||||
{
|
||||
return CustomBadge::getRecordBadge( $this )->url();
|
||||
}
|
||||
|
||||
if ( $this->icon )
|
||||
{
|
||||
return \IPS\File::get( 'core_Ranks', $this->icon );
|
||||
return File::get( 'core_Ranks', $this->icon );
|
||||
}
|
||||
else
|
||||
{
|
||||
return \IPS\Theme::i()->resource( 'default_rank.png', 'core', 'global' );
|
||||
return Theme::i()->resource( 'default_rank.png', 'core', 'global' );
|
||||
}
|
||||
}
|
||||
|
||||
protected static $rankPositions = [];
|
||||
protected static array $rankPositions = [];
|
||||
/**
|
||||
* Fetch the rank position from all ranks
|
||||
*
|
||||
@@ -160,7 +195,7 @@ class _Rank extends \IPS\Node\Model
|
||||
|
||||
static::$rankPositions[ $this->id ] = [
|
||||
'pos' => $pos,
|
||||
'max' => \count( static::getStore() )
|
||||
'max' => count( static::getStore() )
|
||||
];
|
||||
}
|
||||
|
||||
@@ -176,36 +211,40 @@ class _Rank extends \IPS\Node\Model
|
||||
*/
|
||||
public function html( ?string $cssClass = NULL ): string
|
||||
{
|
||||
return \IPS\Theme::i()->getTemplate( 'global', 'core', 'global' )->rank( $this, $cssClass );
|
||||
return Theme::i()->getTemplate( 'global', 'core', 'global' )->rank( $this, $cssClass );
|
||||
}
|
||||
|
||||
/**
|
||||
* [Node] Get Node Description
|
||||
*
|
||||
* @return string
|
||||
* @return string|null
|
||||
*/
|
||||
protected function get__description()
|
||||
protected function get__description(): ?string
|
||||
{
|
||||
return \IPS\Member::loggedIn()->language()->addToStack( 'achievements_awards_points', FALSE, [ 'pluralize' => [ $this->points ] ] );
|
||||
return Member::loggedIn()->language()->addToStack( 'achievements_awards_points', FALSE, [ 'pluralize' => [ $this->points ] ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* [Node] Add/Edit Form
|
||||
*
|
||||
* @param \IPS\Helpers\Form $form The form
|
||||
* @param Form $form The form
|
||||
* @return void
|
||||
*/
|
||||
public function form( &$form )
|
||||
public function form( Form &$form ) : void
|
||||
{
|
||||
/* Allow SVGs without the obscure hash removing the file extension */
|
||||
\IPS\File::$safeFileExtensions[] = 'svg';
|
||||
File::$safeFileExtensions[] = 'svg';
|
||||
|
||||
$form->add( new \IPS\Helpers\Form\Translatable( 'member_ranks_word_custom', NULL, TRUE, array( 'app' => 'core', 'key' => ( $this->id ? "core_member_rank_{$this->id}" : NULL ) ) ) );
|
||||
$form->add( new \IPS\Helpers\Form\Number( 'member_ranks_points', $this->points ?: 0, TRUE, array( 'min' => 0 ) ) );
|
||||
$form->add( new \IPS\Helpers\Form\Upload( 'member_ranks_icon', $this->icon ? \IPS\File::get( 'core_Ranks', $this->icon ) : NULL, TRUE, array( 'obscure' => TRUE, 'allowedFileTypes' => array_merge( \IPS\Image::supportedExtensions(), ['svg'] ), 'checkImage' => TRUE, 'storageExtension' => 'core_Ranks' ), function( $val ) {
|
||||
$form->add( new Translatable( 'member_ranks_word_custom', NULL, TRUE, array( 'app' => 'core', 'key' => ( $this->id ? "core_member_rank_{$this->id}" : NULL ) ) ) );
|
||||
$form->add( new Number( 'member_ranks_points', $this->points ?: 0, TRUE, array( 'min' => 0 ) ) );
|
||||
|
||||
$badge = CustomBadge::getRecordBadge( $this );
|
||||
$badge->addBadgeFieldsToForm( $form, !$this->rank_use_image, ['member_ranks_icon'], 'rank', false );
|
||||
|
||||
$form->add( new Upload( 'member_ranks_icon', $this->icon ? File::get( 'core_Ranks', $this->icon ) : NULL, TRUE, array( 'obscure' => TRUE, 'allowedFileTypes' => array_merge( Image::supportedExtensions(), ['svg'] ), 'checkImage' => TRUE, 'storageExtension' => 'core_Ranks' ), function( $val ) {
|
||||
if( !$val )
|
||||
{
|
||||
throw new \DomainException('achievements_bad_image');
|
||||
throw new DomainException('achievements_bad_image');
|
||||
}
|
||||
|
||||
/* Good luck with your fancy SVG */
|
||||
@@ -214,14 +253,16 @@ class _Rank extends \IPS\Node\Model
|
||||
{
|
||||
try
|
||||
{
|
||||
$image = \IPS\Image::create( $val->contents() );
|
||||
$image = Image::create( $val->contents() );
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
catch ( Exception $e )
|
||||
{
|
||||
throw new \DomainException( 'achievements_bad_image' );
|
||||
throw new DomainException( 'achievements_bad_image' );
|
||||
}
|
||||
}
|
||||
}, NULL, NULL, 'member_ranks_icon' ) );
|
||||
|
||||
parent::form( $form );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -230,14 +271,14 @@ class _Rank extends \IPS\Node\Model
|
||||
* @param array $values Values from the form
|
||||
* @return array
|
||||
*/
|
||||
public function formatFormValues( $values )
|
||||
public function formatFormValues( array $values ): array
|
||||
{
|
||||
if ( !$this->id )
|
||||
{
|
||||
$this->save();
|
||||
}
|
||||
|
||||
\IPS\Lang::saveCustom( 'core', "core_member_rank_{$this->id}", $values['member_ranks_word_custom'] );
|
||||
Lang::saveCustom( 'core', "core_member_rank_{$this->id}", $values['member_ranks_word_custom'] );
|
||||
unset( $values['member_ranks_word_custom'] );
|
||||
|
||||
$_values = $values;
|
||||
@@ -248,11 +289,21 @@ class _Rank extends \IPS\Node\Model
|
||||
{
|
||||
$values[ mb_substr( $k, 13 ) ] = $v;
|
||||
}
|
||||
else if ( $k === 'rank_use_custom' )
|
||||
{
|
||||
$values[ 'rank_use_image' ] = !$v;
|
||||
}
|
||||
else
|
||||
{
|
||||
$values[ $k ] = $v;
|
||||
}
|
||||
}
|
||||
|
||||
/* Generate a custom SVG for this badge if we're not using a custom image */
|
||||
if ( ( !array_key_exists( 'rank_use_image', $values ) and !$this->rank_use_image ) or ( array_key_exists( 'rank_use_image', $values ) and !$values['rank_use_image'] ) )
|
||||
{
|
||||
CustomBadge::saveRecordBadge( $this, CustomBadge::generateBadgeFromFormValues( $values, 'rank' ) );
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
@@ -264,7 +315,7 @@ class _Rank extends \IPS\Node\Model
|
||||
*/
|
||||
public static function show(): bool
|
||||
{
|
||||
return !( ( \IPS\Settings::i()->achievements_rebuilding or !\IPS\Settings::i()->achievements_enabled ) );
|
||||
return !( ( Settings::i()->achievements_rebuilding or !Settings::i()->achievements_enabled ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -288,11 +339,11 @@ class _Rank extends \IPS\Node\Model
|
||||
{
|
||||
try
|
||||
{
|
||||
$image = \IPS\File::get( 'core_Ranks', $oldImage );
|
||||
$newImage = \IPS\File::create( 'core_Ranks', $image->originalFilename, $image->contents() );
|
||||
$image = File::get( 'core_Ranks', $oldImage );
|
||||
$newImage = File::create( 'core_Ranks', $image->originalFilename, $image->contents() );
|
||||
$this->icon = (string) $newImage;
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
catch ( Exception $e )
|
||||
{
|
||||
$this->icon = NULL;
|
||||
}
|
||||
@@ -304,17 +355,17 @@ class _Rank extends \IPS\Node\Model
|
||||
/**
|
||||
* Delete Record
|
||||
*
|
||||
* @return void
|
||||
* @return void
|
||||
*/
|
||||
public function delete()
|
||||
public function delete(): void
|
||||
{
|
||||
if ( $this->icon )
|
||||
{
|
||||
try
|
||||
{
|
||||
\IPS\File::get( 'core_Ranks', $this->icon )->delete();
|
||||
File::get( 'core_Ranks', $this->icon )->delete();
|
||||
}
|
||||
catch( \Exception $ex ) { }
|
||||
catch( Exception $ex ) { }
|
||||
}
|
||||
|
||||
parent::delete();
|
||||
@@ -323,19 +374,19 @@ class _Rank extends \IPS\Node\Model
|
||||
/**
|
||||
* Get output for API
|
||||
*
|
||||
* @param \IPS\Member|NULL $authorizedMember The member making the API request or NULL for API Key / client_credentials
|
||||
* @param Member|NULL $authorizedMember The member making the API request or NULL for API Key / client_credentials
|
||||
* @return array
|
||||
* @apiresponse int id ID number
|
||||
* @apiresponse string name Name
|
||||
* @apiresponse string url Path to the rank icon
|
||||
* @apiresponse int points Points
|
||||
*/
|
||||
public function apiOutput( \IPS\Member $authorizedMember = NULL )
|
||||
public function apiOutput( Member $authorizedMember = NULL ): array
|
||||
{
|
||||
return array(
|
||||
'id' => $this->id,
|
||||
'name' => $this->_title,
|
||||
'icon' => $this->icon ? (string) $this->_icon->url : $this->_icon,
|
||||
'icon' => ($this->rank_use_image and $this->icon) ? (string) $this->_icon->url : $this->_icon,
|
||||
'points' => $this->points
|
||||
);
|
||||
}
|
||||
@@ -348,20 +399,20 @@ class _Rank extends \IPS\Node\Model
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function importXml( $file, $option=NULL )
|
||||
public static function importXml( string $file, ?bool $option=NULL ) : void
|
||||
{
|
||||
/* Open XML file */
|
||||
$xml = \IPS\Xml\XMLReader::safeOpen( $file );
|
||||
|
||||
if ( ! @$xml->read() )
|
||||
{
|
||||
throw new \DomainException( 'xml_upload_invalid' );
|
||||
throw new DomainException( 'xml_upload_invalid' );
|
||||
}
|
||||
|
||||
/* Did we want to wipe first? */
|
||||
if ( $option == 'wipe' )
|
||||
{
|
||||
foreach( \IPS\core\Achievements\Rank::getStore() as $rank )
|
||||
foreach(Rank::getStore() as $rank )
|
||||
{
|
||||
$rank->delete();
|
||||
}
|
||||
@@ -370,7 +421,7 @@ class _Rank extends \IPS\Node\Model
|
||||
/* Start looping through each row */
|
||||
while ( $xml->read() and $xml->name == 'rank' )
|
||||
{
|
||||
if( $xml->nodeType != \XMLReader::ELEMENT )
|
||||
if( $xml->nodeType != XMLReader::ELEMENT )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -383,7 +434,7 @@ class _Rank extends \IPS\Node\Model
|
||||
|
||||
while ( $xml->read() and $xml->name != 'rank' )
|
||||
{
|
||||
if( $xml->nodeType != \XMLReader::ELEMENT )
|
||||
if( $xml->nodeType != XMLReader::ELEMENT )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -411,7 +462,7 @@ class _Rank extends \IPS\Node\Model
|
||||
/* Did we want to wipe existing ranks with the same points? */
|
||||
if ( $option == 'replace' )
|
||||
{
|
||||
foreach( \IPS\core\Achievements\Rank::getStore() as $rank )
|
||||
foreach(Rank::getStore() as $rank )
|
||||
{
|
||||
if ( $rank->points == $insert['points'] )
|
||||
{
|
||||
@@ -422,20 +473,20 @@ class _Rank extends \IPS\Node\Model
|
||||
|
||||
if ( ! empty( $insert['icon_name'] ) and ! empty( $insert['icon_data'] ) )
|
||||
{
|
||||
$insert['icon'] = (string) \IPS\File::create( 'core_Ranks', $insert['icon_name'], $insert['icon_data'], NULL, TRUE, NULL, FALSE );
|
||||
$insert['icon'] = (string) File::create( 'core_Ranks', $insert['icon_name'], $insert['icon_data'], NULL, TRUE, NULL, FALSE );
|
||||
|
||||
unset( $insert['icon_name'] );
|
||||
unset( $insert['icon_data'] );
|
||||
}
|
||||
|
||||
$insertId = \IPS\Db::i()->insert( 'core_member_ranks', $insert );
|
||||
$insertId = Db::i()->insert( 'core_member_ranks', $insert );
|
||||
|
||||
if ( ! empty( $insert['title'] ) )
|
||||
{
|
||||
\IPS\Lang::saveCustom( 'core', "core_member_rank_{$insertId}", $insert['title'] );
|
||||
Lang::saveCustom( 'core', "core_member_rank_{$insertId}", $insert['title'] );
|
||||
}
|
||||
}
|
||||
|
||||
unset( \IPS\Data\Store::i()->achievementRanks );
|
||||
unset( Store::i()->achievementRanks );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user