Version 4.6.0
This commit is contained in:
1 parent
f79dcf067a
commit
517a5e1f70
2036 files changed
+110041
-26162
No files matched your search
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
/**
|
||||
* @brief Achievements Rule Model
|
||||
* @author <a href='https://www.invisioncommunity.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) Invision Power Services, Inc.
|
||||
* @license https://www.invisioncommunity.com/legal/standards/
|
||||
* @package Invision Community
|
||||
* @since 24 Feb 2021
|
||||
*/
|
||||
|
||||
namespace IPS\core\Achievements\Actions;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Base class for AchievementAction extensions
|
||||
*/
|
||||
abstract class _AbstractAchievementAction
|
||||
{
|
||||
/**
|
||||
* Can use this rule?
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function canUse(): bool
|
||||
{
|
||||
$exploded = explode( '\\', \get_called_class() );
|
||||
|
||||
return \IPS\Application::appIsEnabled( $exploded[1] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get filter form elements
|
||||
*
|
||||
* @param array|NULL $filters Current filter values (if editing)
|
||||
* @param \IPS\Http\Url $url The URL the form is being shown on
|
||||
* @return array
|
||||
*/
|
||||
public function filters( ?array $filters, \IPS\Http\Url $url ): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Format filter form values
|
||||
*
|
||||
* @param array $values The values from the form
|
||||
* @return array
|
||||
*/
|
||||
public function formatFilterValues( array $values ): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Work out if the filters applies for a given action
|
||||
*
|
||||
* Important note for milestones: consider the context. This method is called by \IPS\Member::achievementAction(). If your code
|
||||
* calls that BEFORE making its change in the database (or there is read/write separation), you will need to add
|
||||
* 1 to the value being considered for milestones
|
||||
*
|
||||
* @param \IPS\Member $subject The subject member
|
||||
* @param array $filters The value returned by formatFilterValues()
|
||||
* @param mixed $extra Any additional information about what is happening (e.g. if a post is being made: the post object)
|
||||
* @return bool
|
||||
*/
|
||||
public function filtersMatch( \IPS\Member $subject, array $filters, $extra = NULL ): bool
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the labels for the people this action might give awards to
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function awardOptions(): array
|
||||
{
|
||||
return [
|
||||
'subject' => '',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the "other" people we need to award =stuff to
|
||||
*
|
||||
* @param mixed $extra Any additional information about what is happening (e.g. if a post is being made: the post object)
|
||||
* @return array
|
||||
*/
|
||||
public function awardOther( $extra = NULL ): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get identifier to prevent the member being awarded points for the same action twice
|
||||
* Must be unique within within of this domain, must not exceed 32 chars.
|
||||
*
|
||||
* @param \IPS\Member $subject The subject member
|
||||
* @param mixed $extra Any additional information about what is happening (e.g. if a post is being made: the post object)
|
||||
* @return string
|
||||
*/
|
||||
abstract public function identifier( \IPS\Member $subject, $extra = NULL ): string;
|
||||
|
||||
/**
|
||||
* Return a description for this action to show in the log
|
||||
*
|
||||
* @param string $identifier The identifier as returned by identifier()
|
||||
* @param array $actor If the member was the "subject", "other", or both
|
||||
* @return string
|
||||
*/
|
||||
abstract public function logRow( string $identifier, array $actor ): string;
|
||||
|
||||
/**
|
||||
* Get "description" for rule
|
||||
*
|
||||
* @param \IPS\core\Achievements\Rule $rule The rule
|
||||
* @return string|null
|
||||
*/
|
||||
abstract public function ruleDescription( \IPS\core\Achievements\Rule $rule ): ?string;
|
||||
}
|
||||
@@ -0,0 +1,364 @@
|
||||
<?php
|
||||
/**
|
||||
* @brief Abstract Achievement Action Extension for content-related things
|
||||
* @author <a href='https://www.invisioncommunity.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) Invision Power Services, Inc.
|
||||
* @license https://www.invisioncommunity.com/legal/standards/
|
||||
* @since 24 Feb 2021
|
||||
*/
|
||||
|
||||
namespace IPS\core\Achievements\Actions;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract Achievement Action Extension for content-related things
|
||||
*/
|
||||
abstract class _AbstractContentAchievementAction extends AbstractAchievementAction
|
||||
{
|
||||
protected static $includeItems = TRUE;
|
||||
protected static $includeComments = TRUE;
|
||||
protected static $includeReviews = TRUE;
|
||||
|
||||
/**
|
||||
* Get filter form elements
|
||||
*
|
||||
* @param array|NULL $filters Current filter values (if editing)
|
||||
* @param \IPS\Http\Url $url The URL the form is being shown on
|
||||
* @return array
|
||||
*/
|
||||
public function filters( ?array $filters, \IPS\Http\Url $url ): array
|
||||
{
|
||||
$classKey = explode( '\\', \get_called_class() )[5];
|
||||
|
||||
$contentTypeOptions = [];
|
||||
$contentTypeToggles = [];
|
||||
$_return = [];
|
||||
|
||||
$defaultApp = NULL;
|
||||
foreach( \IPS\Application::applications() as $directory => $application )
|
||||
{
|
||||
if( $application->default )
|
||||
{
|
||||
$defaultApp = $directory;
|
||||
break;
|
||||
}
|
||||
}
|
||||
foreach ( \IPS\Application::allExtensions( 'core', 'ContentRouter', FALSE, $defaultApp ) as $extension )
|
||||
{
|
||||
foreach ( $extension->classes as $class )
|
||||
{
|
||||
if ( isset( $class::$databaseColumnMap['author'] ) )
|
||||
{
|
||||
$nodeFilterKey = NULL;
|
||||
$haveMatchingClass = FALSE;
|
||||
if ( isset( $class::$containerNodeClass ) )
|
||||
{
|
||||
$nodeFilterKey = 'achievement_subfilter_' . $classKey . '_type_' . str_replace( '\\', '-', $class );
|
||||
$contentTypeToggles[ $class ][] = $nodeFilterKey;
|
||||
}
|
||||
if ( static::$includeItems )
|
||||
{
|
||||
$contentTypeOptions[ $class ] = $class::$title;
|
||||
$contentTypeToggles[ $class ] = $nodeFilterKey ? [ $nodeFilterKey ] : [];
|
||||
$haveMatchingClass = TRUE;
|
||||
}
|
||||
if ( static::$includeComments and isset( $class::$commentClass ) )
|
||||
{
|
||||
$commentClass = $class::$commentClass;
|
||||
$contentTypeOptions[ $commentClass ] = $commentClass::$title;
|
||||
$contentTypeToggles[ $commentClass ] = $nodeFilterKey ? [ $nodeFilterKey ] : [];
|
||||
$haveMatchingClass = TRUE;
|
||||
}
|
||||
if ( static::$includeReviews and isset( $class::$reviewClass ) )
|
||||
{
|
||||
$reviewClass = $class::$reviewClass;
|
||||
$contentTypeOptions[ $reviewClass ] = $reviewClass::$title;
|
||||
$contentTypeToggles[ $reviewClass ] = $nodeFilterKey ? [ $nodeFilterKey ] : [];
|
||||
$haveMatchingClass = TRUE;
|
||||
}
|
||||
|
||||
if ( $nodeFilterKey and $haveMatchingClass )
|
||||
{
|
||||
try
|
||||
{
|
||||
$nodeTitle = \IPS\Member::loggedIn()->language()->get( ($class::$containerNodeClass)::$nodeTitle . '_sg_lc' );
|
||||
}
|
||||
catch( \UnderflowException $e )
|
||||
{
|
||||
$nodeTitle = ($class::$containerNodeClass)::$nodeTitle . '_sg';
|
||||
}
|
||||
|
||||
$nodeFilter = new \IPS\Helpers\Form\Node( $nodeFilterKey, ( $filters and isset( $filters[ 'nodes_' . str_replace( '\\', '-', $class ) ] ) and $filters[ 'nodes_' . str_replace( '\\', '-', $class ) ] ) ? $filters[ 'nodes_' . str_replace( '\\', '-', $class ) ] : 0, FALSE, [
|
||||
'url' => $url,
|
||||
'class' => $class::$containerNodeClass,
|
||||
'showAllNodes' => TRUE,
|
||||
'multiple' => TRUE,
|
||||
], NULL, \IPS\Member::loggedIn()->language()->addToStack( 'achievement_filter_NewContentItem_node_prefix', FALSE, [ 'sprintf' => [
|
||||
\IPS\Member::loggedIn()->language()->addToStack( $class::_definiteArticle(), FALSE ),
|
||||
\IPS\Member::loggedIn()->language()->addToStack( $nodeTitle, FALSE )
|
||||
] ] ) );
|
||||
$nodeFilter->label = \IPS\Member::loggedIn()->language()->addToStack( 'achievement_filter_NewContentItem_node', FALSE, [ 'sprintf' => [ \IPS\Member::loggedIn()->language()->addToStack( $nodeTitle, FALSE ) ] ] );
|
||||
$_return[ "nodes_" . str_replace( '\\', '-', $class ) ] = $nodeFilter;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$typeFilter = new \IPS\Helpers\Form\Select( 'achievement_filter_' . $classKey . '_type', ( $filters and isset( $filters['type'] ) and $filters['type'] ) ? $filters['type'] : NULL, FALSE, [ 'options' => $contentTypeOptions, 'toggles' => $contentTypeToggles ], NULL, \IPS\Member::loggedIn()->language()->addToStack('achievement_filter_NewContentItem_type_prefix') );
|
||||
$typeFilter->label = \IPS\Member::loggedIn()->language()->addToStack('achievement_filter_NewContentItem_type');
|
||||
|
||||
$nthFilter = new \IPS\Helpers\Form\Number( 'achievement_filter_' . $classKey . '_nth', ( $filters and isset( $filters['milestone'] ) and $filters['milestone'] ) ? $filters['milestone'] : 0, FALSE, [], NULL, \IPS\Member::loggedIn()->language()->addToStack('achievement_filter_nth_their'), \IPS\Member::loggedIn()->language()->addToStack('achievement_filter_' . $classKey . '_nth_suffix') );
|
||||
$nthFilter->label = \IPS\Member::loggedIn()->language()->addToStack('achievement_filter_NewContentItem_nth');
|
||||
|
||||
$return = [ 'type' => $typeFilter ];
|
||||
foreach ( $_return as $k => $v )
|
||||
{
|
||||
$return[ $k ] = $v;
|
||||
}
|
||||
$return['milestone'] = $nthFilter;
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format filter form values
|
||||
*
|
||||
* @param array $values The values from the form
|
||||
* @return array
|
||||
*/
|
||||
public function formatFilterValues( array $values ): array
|
||||
{
|
||||
$classKey = explode( '\\', \get_called_class() )[5];
|
||||
|
||||
$return = [];
|
||||
if ( isset( $values['achievement_filter_' . $classKey . '_type'] ) )
|
||||
{
|
||||
$return['type'] = $values['achievement_filter_' . $classKey . '_type'];
|
||||
|
||||
$itemClass = $return['type'];
|
||||
if ( \in_array( 'IPS\Content\Comment', class_parents( $return['type'] ) ) )
|
||||
{
|
||||
$itemClass = $return['type']::$itemClass;
|
||||
}
|
||||
|
||||
if ( isset( $values[ 'achievement_subfilter_' . $classKey . '_type_' . str_replace( '\\', '-', $itemClass ) ] ) )
|
||||
{
|
||||
$return[ 'nodes_' . str_replace( '\\', '-', $itemClass ) ] = array_keys( $values[ 'achievement_subfilter_' . $classKey . '_type_' . str_replace( '\\', '-', $itemClass ) ] );
|
||||
}
|
||||
}
|
||||
if ( isset( $values['achievement_filter_' . $classKey . '_nth'] ) )
|
||||
{
|
||||
$return['milestone'] = $values['achievement_filter_' . $classKey . '_nth'];
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Work out if the filters applies for a given action
|
||||
*
|
||||
* Important note for milestones: consider the context. This method is called by \IPS\Member::achievementAction(). If your code
|
||||
* calls that BEFORE making its change in the database (or there is read/write separation), you will need to add
|
||||
* 1 to the value being considered for milestones
|
||||
*
|
||||
* @param \IPS\Member $subject The subject member
|
||||
* @param array $filters The value returned by formatFilterValues()
|
||||
* @param mixed $extra Any additional information about what is happening (e.g. if a post is being made: the post object)
|
||||
* @return bool
|
||||
*/
|
||||
public function filtersMatch( \IPS\Member $subject, array $filters, $extra = NULL ): bool
|
||||
{
|
||||
if ( isset( $filters['type'] ) )
|
||||
{
|
||||
if ( !( $extra instanceof $filters['type'] ) )
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
$item = $extra;
|
||||
if ( \in_array( 'IPS\Content\Comment', class_parents( $filters['type'] ) ) )
|
||||
{
|
||||
$item = $extra->item();
|
||||
}
|
||||
|
||||
if ( isset( $filters[ 'nodes_' . str_replace( '\\', '-', \get_class( $item ) ) ] ) )
|
||||
{
|
||||
if ( !\in_array( $item->container()->_id, $filters[ 'nodes_' . str_replace( '\\', '-', \get_class( $item ) ) ] ) )
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get identifier to prevent the member being awarded points for the same action twice
|
||||
* Must be unique within within of this domain, must not exceed 32 chars.
|
||||
*
|
||||
* @param \IPS\Member $subject The subject member
|
||||
* @param mixed $extra Any additional information about what is happening (e.g. if a post is being made: the post object)
|
||||
* @return string
|
||||
*/
|
||||
public function identifier( \IPS\Member $subject, $extra = NULL ): string
|
||||
{
|
||||
return \get_class( $extra ) . ':' . $extra->{$extra::$databaseColumnId};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get "description" for rule (usually a description of the rule's filters)
|
||||
*
|
||||
* @param \IPS\core\Achievements\Rule $rule The rule
|
||||
* @return string|NULL
|
||||
*/
|
||||
protected function _nodeFilterDescription( \IPS\core\Achievements\Rule $rule ): ?string
|
||||
{
|
||||
if ( isset( $rule->filters['type'] ) )
|
||||
{
|
||||
$itemClass = $rule->filters['type'];
|
||||
if ( \in_array( 'IPS\Content\Comment', class_parents( $rule->filters['type'] ) ) )
|
||||
{
|
||||
$itemClass = $itemClass::$itemClass;
|
||||
}
|
||||
|
||||
if ( isset( $rule->filters[ 'nodes_' . str_replace( '\\', '-', $itemClass ) ] ) )
|
||||
{
|
||||
$nodeClass = $itemClass::$containerNodeClass;
|
||||
|
||||
$nodeNames = [];
|
||||
foreach ( $rule->filters[ 'nodes_' . str_replace( '\\', '-', $itemClass ) ] as $id )
|
||||
{
|
||||
try
|
||||
{
|
||||
$nodeNames[] = $nodeClass::load( $id )->_title;
|
||||
}
|
||||
catch ( \OutOfRangeException $e ) {}
|
||||
}
|
||||
if ( $nodeNames )
|
||||
{
|
||||
return \IPS\Member::loggedIn()->language()->addToStack( 'achievements_title_filter_location', FALSE, [
|
||||
'htmlsprintf' => [
|
||||
\IPS\Theme::i()->getTemplate( 'achievements' )->ruleDescriptionBadge( 'location',
|
||||
\count( $nodeNames ) === 1 ? $nodeNames[0] : \IPS\Member::loggedIn()->language()->addToStack( 'achievements_title_filter_location_val', FALSE, [ 'sprintf' => [
|
||||
\count( $nodeNames ),
|
||||
\IPS\Member::loggedIn()->language()->addToStack( $nodeClass::$nodeTitle, FALSE )
|
||||
] ] ),
|
||||
\count( $nodeNames ) === 1 ? NULL : $nodeNames
|
||||
)
|
||||
],
|
||||
] );
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get rebuild data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
static public function rebuildData()
|
||||
{
|
||||
$return = [];
|
||||
foreach ( \IPS\Application::allExtensions( 'core', 'ContentRouter', FALSE ) as $extension )
|
||||
{
|
||||
foreach ( $extension->classes as $class )
|
||||
{
|
||||
if ( isset( $class::$databaseColumnMap['author'] ) )
|
||||
{
|
||||
if ( static::$includeItems )
|
||||
{
|
||||
$return[] = [
|
||||
'table' => $class::$databaseTable,
|
||||
'pkey' => $class::$databasePrefix . $class::$databaseColumnId,
|
||||
'date' => ( isset( $class::$databaseColumnMap['date'] ) ) ? $class::$databasePrefix . $class::$databaseColumnMap['date'] : NULL,
|
||||
'where' => [],
|
||||
'class' => $class
|
||||
];
|
||||
}
|
||||
if ( static::$includeComments and isset( $class::$commentClass ) )
|
||||
{
|
||||
$commentClass = $class::$commentClass;
|
||||
$return[] = [
|
||||
'table' => $commentClass::$databaseTable,
|
||||
'pkey' => $commentClass::$databasePrefix . $commentClass::$databaseColumnId,
|
||||
'date' => ( isset( $commentClass::$databaseColumnMap['date'] ) ) ? $commentClass::$databasePrefix . $commentClass::$databaseColumnMap['date'] : NULL,
|
||||
'where' => [],
|
||||
'class' => $commentClass
|
||||
];
|
||||
}
|
||||
if ( static::$includeReviews and isset( $class::$reviewClass ) )
|
||||
{
|
||||
$reviewClass = $class::$reviewClass;
|
||||
$return[] = [
|
||||
'table' => $reviewClass::$databaseTable,
|
||||
'pkey' => $reviewClass::$databasePrefix . $reviewClass::$databaseColumnId,
|
||||
'date' => ( isset( $reviewClass::$databaseColumnMap['date'] ) ) ? $reviewClass::$databasePrefix . $reviewClass::$databaseColumnMap['date'] : NULL,
|
||||
'where' => [],
|
||||
'class' => $reviewClass
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a where clause to check based on the type/class
|
||||
*
|
||||
* @param array $filters The value returned by formatFilterValues()
|
||||
* @param string $class Class we are working with
|
||||
* @param \IPS\DateTime|null $time Any time limit to add
|
||||
* @return array
|
||||
*/
|
||||
protected function getWherePerType( $filters, $class, $time=NULL ): array
|
||||
{
|
||||
$where = ( $time ? [ [ $class::$databasePrefix . $class::$databaseColumnMap['date'] . ' >= ?', $time->getTimestamp() ] ] : [] );
|
||||
$itemClass = $class;
|
||||
|
||||
if ( \in_array( 'IPS\Content\Comment', class_parents( $class ) ) )
|
||||
{
|
||||
$itemClass = $class::$itemClass;
|
||||
}
|
||||
|
||||
if ( isset( $filters[ 'nodes_' . str_replace( '\\', '-', $itemClass ) ] ) )
|
||||
{
|
||||
if ( \in_array( 'IPS\Content\Comment', class_parents( $class ) ) )
|
||||
{
|
||||
$itemClass = $class::$itemClass;
|
||||
$where[] = [ $class::$databasePrefix . $class::$databaseColumnMap['item'] . ' IN(?)',
|
||||
\IPS\Db::i()->select( $itemClass::$databasePrefix . $itemClass::$databaseColumnId, $itemClass::$databaseTable, [ \IPS\Db::i()->in( $itemClass::$databasePrefix . $itemClass::$databaseColumnMap['container'], $filters[ 'nodes_' . str_replace( '\\', '-', $itemClass ) ] ) ] ) ];
|
||||
}
|
||||
else
|
||||
{
|
||||
$where[] = [ \IPS\Db::i()->in( $class::$databasePrefix . $class::$databaseColumnMap['container'], $filters[ 'nodes_' . str_replace( '\\', '-', $class ) ] ) ];
|
||||
}
|
||||
}
|
||||
|
||||
return $where;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the rebuild row
|
||||
*
|
||||
* @param array $row Row from database
|
||||
* @param array $data Data collected when starting rebuild [table, pkey...]
|
||||
* @return void
|
||||
*/
|
||||
public static function rebuildRow( $row, $data )
|
||||
{
|
||||
$classCalled = explode( '\\', \get_called_class() );
|
||||
$class = $data['class'];
|
||||
$item = $class::constructFromData( $row );
|
||||
$item->author()->achievementAction( 'core', array_pop( $classCalled ), $item );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
<?php
|
||||
/**
|
||||
* @brief Abstract Achievement Action Extension for node-related things
|
||||
* @author <a href='https://www.invisioncommunity.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) Invision Power Services, Inc.
|
||||
* @license https://www.invisioncommunity.com/legal/standards/
|
||||
* @since 24 Feb 2021
|
||||
*/
|
||||
|
||||
namespace IPS\core\Achievements\Actions;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract Achievement Action Extension for content-related things
|
||||
*/
|
||||
abstract class _AbstractNodeAchievementAction extends AbstractAchievementAction
|
||||
{
|
||||
/**
|
||||
* Get filter form elements
|
||||
*
|
||||
* @param array|NULL $filters Current filter values (if editing)
|
||||
* @param \IPS\Http\Url $url The URL the form is being shown on
|
||||
* @return array
|
||||
*/
|
||||
public function filters( ?array $filters, \IPS\Http\Url $url ): array
|
||||
{
|
||||
$contentTypeOptions = [];
|
||||
$contentTypeToggles = [];
|
||||
$_return = [];
|
||||
|
||||
$defaultApp = NULL;
|
||||
foreach( \IPS\Application::applications() as $directory => $application )
|
||||
{
|
||||
if( $application->default )
|
||||
{
|
||||
$defaultApp = $directory;
|
||||
break;
|
||||
}
|
||||
}
|
||||
foreach ( \IPS\Application::allExtensions( 'core', 'ContentRouter', FALSE, $defaultApp ) as $extension )
|
||||
{
|
||||
foreach ( $extension->classes as $class )
|
||||
{
|
||||
if ( isset( $class::$containerNodeClass ) )
|
||||
{
|
||||
$nodeClass = $class::$containerNodeClass;
|
||||
$nodeFilterKey = 'achievement_subfilter_Node_type_' . str_replace( '\\', '-', $nodeClass );
|
||||
$contentTypeToggles[ $nodeClass ][] = $nodeFilterKey;
|
||||
$contentTypeToggles[ $nodeClass ] = [ $nodeFilterKey ];
|
||||
$contentTypeOptions[ $nodeClass ] = $nodeClass::fullyQualifiedType();
|
||||
|
||||
$nodeFilter = new \IPS\Helpers\Form\Node( $nodeFilterKey, ( $filters and isset( $filters[ 'nodes_' . str_replace( '\\', '-', $nodeClass ) ] ) and $filters[ 'nodes_' . str_replace( '\\', '-', $nodeClass ) ] ) ? $filters[ 'nodes_' . str_replace( '\\', '-', $nodeClass ) ] : 0, FALSE, [
|
||||
'url' => $url,
|
||||
'class' => $class::$containerNodeClass,
|
||||
'showAllNodes' => TRUE,
|
||||
'multiple' => TRUE,
|
||||
], NULL, \IPS\Member::loggedIn()->language()->addToStack( 'achievement_filter_Node_node_prefix') );
|
||||
$nodeFilter->label = \IPS\Member::loggedIn()->language()->addToStack( 'achievement_filter_Node_node', FALSE, [ 'sprintf' => [ \IPS\Member::loggedIn()->language()->addToStack( ($class::$containerNodeClass)::$nodeTitle . '_sg', FALSE, [ 'strtolower' => TRUE ] ) ] ] );
|
||||
$_return[ "nodes_" . str_replace( '\\', '-', $nodeClass ) ] = $nodeFilter;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$typeFilter = new \IPS\Helpers\Form\Select( 'achievement_filter_Node_type', ( $filters and isset( $filters['type'] ) and $filters['type'] ) ? $filters['type'] : NULL, FALSE, [ 'options' => $contentTypeOptions, 'toggles' => $contentTypeToggles ], NULL, \IPS\Member::loggedIn()->language()->addToStack('achievement_filter_NewContentItem_type_prefix') );
|
||||
$typeFilter->label = \IPS\Member::loggedIn()->language()->addToStack('achievement_filter_NewContentItem_type');
|
||||
|
||||
$nthFilter = new \IPS\Helpers\Form\Number( 'achievement_filter_Node_nth', ( $filters and isset( $filters['milestone'] ) and $filters['milestone'] ) ? $filters['milestone'] : 0, FALSE, [], NULL, \IPS\Member::loggedIn()->language()->addToStack('achievement_filter_nth_their'), \IPS\Member::loggedIn()->language()->addToStack('achievement_filter_Node_nth_suffix') );
|
||||
$nthFilter->label = \IPS\Member::loggedIn()->language()->addToStack('achievement_filter_Node_nth');
|
||||
|
||||
$return = [ 'type' => $typeFilter ];
|
||||
foreach ( $_return as $k => $v )
|
||||
{
|
||||
$return[ $k ] = $v;
|
||||
}
|
||||
$return['milestone'] = $nthFilter;
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format filter form values
|
||||
*
|
||||
* @param array $values The values from the form
|
||||
* @return array
|
||||
*/
|
||||
public function formatFilterValues( array $values ): array
|
||||
{
|
||||
$return = [];
|
||||
if ( isset( $values['achievement_filter_Node_type'] ) )
|
||||
{
|
||||
$return['type'] = $values['achievement_filter_Node_type'];
|
||||
|
||||
$nodeClass = $return['type'];
|
||||
if ( isset( $values[ 'achievement_subfilter_Node_type_' . str_replace( '\\', '-', $nodeClass ) ] ) )
|
||||
{
|
||||
$return[ 'nodes_' . str_replace( '\\', '-', $nodeClass ) ] = array_keys( $values[ 'achievement_subfilter_Node_type_' . str_replace( '\\', '-', $nodeClass ) ] );
|
||||
}
|
||||
}
|
||||
if ( isset( $values['achievement_filter_Node_nth'] ) )
|
||||
{
|
||||
$return['milestone'] = $values['achievement_filter_Node_nth'];
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Work out if the filters applies for a given action
|
||||
*
|
||||
* Important note for milestones: consider the context. This method is called by \IPS\Member::achievementAction(). If your code
|
||||
* calls that BEFORE making its change in the database (or there is read/write separation), you will need to add
|
||||
* 1 to the value being considered for milestones
|
||||
*
|
||||
* @param \IPS\Member $subject The subject member
|
||||
* @param array $filters The value returned by formatFilterValues()
|
||||
* @param mixed $extra Any additional information about what is happening (e.g. if a post is being made: the post object)
|
||||
* @return bool
|
||||
*/
|
||||
public function filtersMatch( \IPS\Member $subject, array $filters, $extra = NULL ): bool
|
||||
{
|
||||
if ( isset( $filters['type'] ) )
|
||||
{
|
||||
if ( !( $extra instanceof $filters['type'] ) )
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if ( isset( $filters[ 'nodes_' . str_replace( '\\', '-', \get_class( $extra ) ) ] ) )
|
||||
{
|
||||
if ( !\in_array( $extra->_id, $filters[ 'nodes_' . str_replace( '\\', '-', \get_class( $extra ) ) ] ) )
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get identifier to prevent the member being awarded points for the same action twice
|
||||
* Must be unique within within of this domain, must not exceed 32 chars.
|
||||
*
|
||||
* @param \IPS\Member $subject The subject member
|
||||
* @param mixed $extra Any additional information about what is happening (e.g. if a post is being made: the post object)
|
||||
* @return string
|
||||
*/
|
||||
public function identifier( \IPS\Member $subject, $extra = NULL ): string
|
||||
{
|
||||
return \get_class( $extra ) . ':' . $extra->{$extra::$databaseColumnId} . ':' . $subject->member_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get "description" for rule (usually a description of the rule's filters)
|
||||
*
|
||||
* @param \IPS\core\Achievements\Rule $rule The rule
|
||||
* @return string|NULL
|
||||
*/
|
||||
protected function _nodeFilterDescription( \IPS\core\Achievements\Rule $rule ): ?string
|
||||
{
|
||||
if ( isset( $rule->filters['type'] ) )
|
||||
{
|
||||
$nodeClass = $rule->filters['type'];
|
||||
|
||||
if ( isset( $rule->filters[ 'nodes_' . str_replace( '\\', '-', $nodeClass ) ] ) )
|
||||
{
|
||||
$nodeNames = [];
|
||||
foreach ( $rule->filters[ 'nodes_' . str_replace( '\\', '-', $nodeClass ) ] as $id )
|
||||
{
|
||||
try
|
||||
{
|
||||
$nodeNames[] = $nodeClass::load( $id )->_title;
|
||||
}
|
||||
catch ( \OutOfRangeException $e ) {}
|
||||
}
|
||||
if ( $nodeNames )
|
||||
{
|
||||
return \IPS\Member::loggedIn()->language()->addToStack( 'achievements_title_filter_location', FALSE, [
|
||||
'htmlsprintf' => [
|
||||
\IPS\Theme::i()->getTemplate( 'achievements' )->ruleDescriptionBadge( 'location',
|
||||
\count( $nodeNames ) === 1 ? $nodeNames[0] : \IPS\Member::loggedIn()->language()->addToStack( 'achievements_title_filter_location_val', FALSE, [ 'sprintf' => [
|
||||
\count( $nodeNames ),
|
||||
\IPS\Member::loggedIn()->language()->addToStack( $nodeClass::$nodeTitle, FALSE, [ 'strtolower' => TRUE ] )
|
||||
] ] ),
|
||||
\count( $nodeNames ) === 1 ? NULL : $nodeNames
|
||||
)
|
||||
],
|
||||
] );
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,447 @@
|
||||
<?php
|
||||
/**
|
||||
* @brief Badge Model (as in, a representation of a badge a member *can* earn, not a badge a particular member *has* earned)
|
||||
* @author <a href='https://www.invisioncommunity.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) Invision Power Services, Inc.
|
||||
* @license https://www.invisioncommunity.com/legal/standards/
|
||||
* @package Invision Community
|
||||
* @since 22 Feb 2021
|
||||
*/
|
||||
|
||||
namespace IPS\core\Achievements;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Badge Model (as in, a representation of a badge a member *can* earn, not a badge a particular member *has* earned)
|
||||
*/
|
||||
class _Badge extends \IPS\Node\Model
|
||||
{
|
||||
/**
|
||||
* @brief Database Table
|
||||
*/
|
||||
public static $databaseTable = 'core_badges';
|
||||
|
||||
/**
|
||||
* @brief Multiton Store
|
||||
*/
|
||||
protected static $multitons;
|
||||
|
||||
/**
|
||||
* @brief [Node] Node Title
|
||||
*/
|
||||
public static $nodeTitle = 'menu__core_achievements_badges';
|
||||
|
||||
/**
|
||||
* @brief [Node] Title prefix. If specified, will look for a language key with "{$key}_title" as the key
|
||||
*/
|
||||
public static $titleLangPrefix = 'core_badges_';
|
||||
|
||||
/**
|
||||
* @brief [ActiveRecord] Database ID Fields
|
||||
* @note If using this, declare a static $multitonMap = array(); in the child class to prevent duplicate loading queries
|
||||
*/
|
||||
protected static $databaseIdFields = array('recognize');
|
||||
|
||||
/**
|
||||
* @brief [Node] ACP Restrictions
|
||||
* @code
|
||||
array(
|
||||
'app' => 'core', // The application key which holds the restrictrions
|
||||
'module' => 'foo', // The module key which holds the restrictions
|
||||
'map' => array( // [Optional] The key for each restriction - can alternatively use "prefix"
|
||||
'add' => 'foo_add',
|
||||
'edit' => 'foo_edit',
|
||||
'permissions' => 'foo_perms',
|
||||
'delete' => 'foo_delete'
|
||||
),
|
||||
'all' => 'foo_manage', // [Optional] The key to use for any restriction not provided in the map (only needed if not providing all 4)
|
||||
'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(
|
||||
'app' => 'core',
|
||||
'module' => 'achievements',
|
||||
'prefix' => 'badges_',
|
||||
);
|
||||
|
||||
/**
|
||||
* Construct ActiveRecord from database row
|
||||
*
|
||||
* @param array $data Row from database table
|
||||
* @param bool $updateMultitonStoreIfExists Replace current object in multiton store if it already exists there?
|
||||
* @return static
|
||||
*/
|
||||
public static function constructFromData( $data, $updateMultitonStoreIfExists = TRUE )
|
||||
{
|
||||
$obj = parent::constructFromData( $data, $updateMultitonStoreIfExists );
|
||||
|
||||
if ( isset( $obj->recognize ) and ! empty( $obj->recognize ) )
|
||||
{
|
||||
try
|
||||
{
|
||||
$obj->recognize = \IPS\core\Achievements\Recognize::load( $obj->recognize );
|
||||
}
|
||||
catch( \Exception $e )
|
||||
{
|
||||
/* Problem loading, so reset to 0 */
|
||||
$obj->recognize = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $obj->rule ) and ! empty( $obj->rule ) )
|
||||
{
|
||||
$obj->awardDescription = $obj->awardDescription( $obj->rule, $obj->actor );
|
||||
}
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* @var null|array
|
||||
*/
|
||||
static $badgeAwardLangBits = NULL;
|
||||
|
||||
/**
|
||||
* Show the public reason for this badge award
|
||||
*
|
||||
* @param int $ruleId
|
||||
* @param $actor
|
||||
* @return string|null
|
||||
*/
|
||||
public function awardDescription( int $ruleId, $actor ):? string
|
||||
{
|
||||
if ( static::$badgeAwardLangBits === NULL )
|
||||
{
|
||||
foreach( \IPS\Db::i()->select( 'word_key, word_default, word_custom', 'core_sys_lang_words', [ 'lang_id=? AND (word_key LIKE \'core_award_subject_badge_%\' OR word_key LIKE \'core_award_other_badge_%\')', \IPS\Member::loggedIn()->language()->id ] ) as $row )
|
||||
{
|
||||
static::$badgeAwardLangBits[ $row['word_key'] ] = $row['word_custom'] ?: $row['word_default'];
|
||||
}
|
||||
}
|
||||
|
||||
return static::$badgeAwardLangBits['core_award_' . $actor . '_badge_' . $ruleId] ?? NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* [Node] Add/Edit Form
|
||||
*
|
||||
* @param \IPS\Helpers\Form $form The form
|
||||
* @return void
|
||||
*/
|
||||
public function form( &$form )
|
||||
{
|
||||
$form->add( new \IPS\Helpers\Form\Translatable( 'badge_name', NULL, TRUE, array( 'app' => 'core', 'key' => ( $this->id ? "core_badges_{$this->id}" : NULL ) ) ) );
|
||||
$form->add( new \IPS\Helpers\Form\Upload( 'badge_image', $this->image ? \IPS\File::get( 'core_Badges', $this->image ) : NULL, TRUE, array( 'obscure' => FALSE, 'checkImage' => TRUE, 'allowedFileTypes' => array_merge( \IPS\Image::$imageExtensions, ['svg'] ), 'storageExtension' => 'core_Badges', 'allowStockPhotos' => FALSE ), function( $val ) {
|
||||
if( !$val )
|
||||
{
|
||||
throw new \DomainException('achievements_bad_image');
|
||||
}
|
||||
|
||||
/* Good luck with your fancy SVG */
|
||||
$ext = mb_substr( $val->originalFilename, ( mb_strrpos( $val->originalFilename, '.' ) + 1 ) );
|
||||
if( $ext !== 'svg' )
|
||||
{
|
||||
try
|
||||
{
|
||||
$image = \IPS\Image::create( $val->contents() );
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
{
|
||||
throw new \DomainException( 'achievements_bad_image' );
|
||||
}
|
||||
}
|
||||
} ) );
|
||||
|
||||
$form->add( new \IPS\Helpers\Form\YesNo( 'badge_manually_awarded', $this->manually_awarded ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* [Node] Format form values from add/edit form for save
|
||||
*
|
||||
* @param array $values Values from the form
|
||||
* @return array
|
||||
*/
|
||||
public function formatFormValues( $values )
|
||||
{
|
||||
if ( !$this->id )
|
||||
{
|
||||
$this->save();
|
||||
}
|
||||
|
||||
\IPS\Lang::saveCustom( 'core', "core_badges_{$this->id}", $values['badge_name'] );
|
||||
unset( $values['badge_name'] );
|
||||
|
||||
$_values = $values;
|
||||
$values = array();
|
||||
foreach ( $_values as $k => $v )
|
||||
{
|
||||
if( mb_substr( $k, 0, 6 ) === 'badge_' )
|
||||
{
|
||||
$values[ mb_substr( $k, 6 ) ] = $v;
|
||||
}
|
||||
else
|
||||
{
|
||||
$values[ $k ] = $v;
|
||||
}
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* [Node] Get Icon for tree
|
||||
*
|
||||
* @note Return the class for the icon (e.g. 'globe', the 'fa fa-' is added automatically so you do not need this here)
|
||||
* @return string|null
|
||||
*/
|
||||
protected function get__icon()
|
||||
{
|
||||
return \IPS\File::get( 'core_Badges', $this->image );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get badge HTML
|
||||
*
|
||||
* @param string|null $cssClass Additional CSS class to apply
|
||||
* @param bool|null $tooltip Whether or not to apply a tooltip to the badge
|
||||
* @return string
|
||||
*/
|
||||
public function html( ?string $cssClass = NULL, ?bool $tooltip = TRUE, ?bool $showRare = FALSE ): string
|
||||
{
|
||||
return \IPS\Theme::i()->getTemplate( 'global', 'core', 'global' )->badge( $this, $cssClass, $tooltip, $showRare );
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this badge rare?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isRare(): bool
|
||||
{
|
||||
if ( !\IPS\Settings::i()->rare_badge_percent )
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* Fetch the counts we need from the datastore. If they are old or non-existent, refresh and store in datastore for next time. */
|
||||
$stats = NULL;
|
||||
|
||||
if( isset( \IPS\Data\Store::i()->badgeRarityStats ) )
|
||||
{
|
||||
$stats = json_decode( \IPS\Data\Store::i()->badgeRarityStats, true );
|
||||
|
||||
if( !isset( $stats['expiration'] ) OR time() > $stats['expiration'] OR !isset( $stats['badgeCount'][ $this->id ] ) )
|
||||
{
|
||||
$stats = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if( $stats === NULL )
|
||||
{
|
||||
$stats = array(
|
||||
'expiration' => time() + 3600, // Cache for 60 minutes
|
||||
'memberCount' => \IPS\Db::i()->select( 'COUNT(*)', 'core_members', [ 'completed=?', true ] )->first(),
|
||||
'badgeCount' => array(),
|
||||
);
|
||||
|
||||
foreach( \IPS\Db::i()->select( 'id', 'core_badges' ) as $badgeId )
|
||||
{
|
||||
$stats['badgeCount'][ $badgeId ] = \IPS\Db::i()->select( 'COUNT(*)', 'core_member_badges', [ 'badge=?', $badgeId ] )->first();
|
||||
}
|
||||
|
||||
\IPS\Data\Store::i()->badgeRarityStats = json_encode( $stats );
|
||||
}
|
||||
|
||||
return ( 100 / $stats['memberCount'] * $stats['badgeCount'][ $this->id ] ) < \IPS\Settings::i()->rare_badge_percent;
|
||||
}
|
||||
|
||||
/**
|
||||
* [ActiveRecord] Duplicate
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __clone()
|
||||
{
|
||||
if ( $this->skipCloneDuplication === TRUE )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
$oldId = $this->id;
|
||||
$oldImage = $this->image;
|
||||
|
||||
parent::__clone();
|
||||
|
||||
\IPS\Lang::saveCustom( 'core', "core_badges_{$this->id}", iterator_to_array( \IPS\Db::i()->select( 'word_custom, lang_id', 'core_sys_lang_words', array( 'word_key=?', "core_badges_{$oldId}" ) )->setKeyField( 'lang_id' )->setValueField('word_custom') ) );
|
||||
|
||||
if ( $oldImage )
|
||||
{
|
||||
try
|
||||
{
|
||||
$image = \IPS\File::get( 'core_Badges', $oldImage );
|
||||
$newImage = \IPS\File::create( 'core_Badges', $image->originalFilename, $image->contents() );
|
||||
$this->image = (string) $newImage;
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
{
|
||||
$this->image = NULL;
|
||||
}
|
||||
|
||||
$this->save();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete Record
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
/* Remove from recognize where just the badge is used, and no points */
|
||||
\IPS\Db::i()->delete( 'core_member_recognize', [ 'r_points=0 and r_badge=?', $this->id ] );
|
||||
|
||||
/* Remove the badge from recognize, but leave points */
|
||||
\IPS\Db::i()->update( 'core_member_recognize', [ 'r_badge' => 0 ], [ 'r_badge=?', $this->id ] );
|
||||
|
||||
if ( $this->image )
|
||||
{
|
||||
try
|
||||
{
|
||||
\IPS\File::get( 'core_Badges', $this->image )->delete();
|
||||
}
|
||||
catch( \Exception $ex ) { }
|
||||
}
|
||||
|
||||
parent::delete();
|
||||
|
||||
\IPS\Db::i()->delete( 'core_member_badges', [ 'badge=?', $this->id ] );
|
||||
\IPS\Lang::deleteCustom( 'core', "core_badges_{$this->id}" );
|
||||
}
|
||||
|
||||
/**
|
||||
* Show badges on the front end?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function show(): bool
|
||||
{
|
||||
return !( ( \IPS\Settings::i()->achievements_rebuilding or !\IPS\Settings::i()->achievements_enabled ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch assigned (to rules) badge IDs
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getAssignedBadgeIds(): array
|
||||
{
|
||||
$assignedBadges = [];
|
||||
foreach( \IPS\Db::i()->select( 'badge_subject, badge_other', 'core_achievements_rules', [ 'badge_subject > 0 or badge_other > 0' ] ) as $row )
|
||||
{
|
||||
if ( $row['badge_subject'] )
|
||||
{
|
||||
$assignedBadges[] = $row['badge_subject'];
|
||||
}
|
||||
|
||||
if ( $row['badge_other'] )
|
||||
{
|
||||
$assignedBadges[] = $row['badge_other'];
|
||||
}
|
||||
}
|
||||
|
||||
return $assignedBadges;
|
||||
}
|
||||
|
||||
/**
|
||||
* Import from Xml
|
||||
*
|
||||
* @param string $file The file to import data from
|
||||
* @param boolean $deleteExisting Remove existing rules first?
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function importXml( $file, $deleteExisting=FALSE )
|
||||
{
|
||||
/* Open XML file */
|
||||
$xml = \IPS\Xml\XMLReader::safeOpen( $file );
|
||||
|
||||
if ( ! @$xml->read() )
|
||||
{
|
||||
throw new \DomainException( 'xml_upload_invalid' );
|
||||
}
|
||||
|
||||
/* Did we want to wipe first? */
|
||||
if ( $deleteExisting )
|
||||
{
|
||||
$assignedBadges = static::getAssignedBadgeIds();
|
||||
|
||||
$where = NULL;
|
||||
if ( \count( $assignedBadges ) )
|
||||
{
|
||||
$where = [ \IPS\Db::i()->in( '`id`', $assignedBadges, TRUE ) ];
|
||||
}
|
||||
|
||||
foreach( \IPS\Db::i()->select('*', 'core_badges', $where ) as $row )
|
||||
{
|
||||
static::constructFromData( $row )->delete();
|
||||
}
|
||||
}
|
||||
|
||||
/* Start looping through each row */
|
||||
while ( $xml->read() and $xml->name == 'badge' )
|
||||
{
|
||||
if( $xml->nodeType != \XMLReader::ELEMENT )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$insert = [];
|
||||
$title = NULL;
|
||||
|
||||
while ( $xml->read() and $xml->name != 'badge' )
|
||||
{
|
||||
if( $xml->nodeType != \XMLReader::ELEMENT )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
switch( $xml->name )
|
||||
{
|
||||
case 'manually_awarded':
|
||||
$insert[ $xml->name ] = (int) $xml->readString();
|
||||
break;
|
||||
case 'title':
|
||||
$title = $xml->readString();
|
||||
break;
|
||||
case 'icon_name':
|
||||
$insert['icon_name'] = $xml->readString();
|
||||
break;
|
||||
case 'icon_data':
|
||||
$insert['icon_data'] = base64_decode( $xml->readString() );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $insert['icon_name'] ) and ! empty( $insert['icon_data'] ) )
|
||||
{
|
||||
$insert['image'] = (string) \IPS\File::create( 'core_Badges', $insert['icon_name'], $insert['icon_data'], NULL, TRUE, NULL, FALSE );
|
||||
|
||||
unset( $insert['icon_name'] );
|
||||
unset( $insert['icon_data'] );
|
||||
}
|
||||
|
||||
$insertId = \IPS\Db::i()->insert( 'core_badges', $insert );
|
||||
|
||||
if ( ! empty( $title ) )
|
||||
{
|
||||
\IPS\Lang::saveCustom( 'core', "core_badges_{$insertId}", $title );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,438 @@
|
||||
<?php
|
||||
/**
|
||||
* @brief Rank Model
|
||||
* @author <a href='https://www.invisioncommunity.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) Invision Power Services, Inc.
|
||||
* @license https://www.invisioncommunity.com/legal/standards/
|
||||
* @package Invision Community
|
||||
* @since 3 Mar 2021
|
||||
*/
|
||||
|
||||
namespace IPS\core\Achievements;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rank Model
|
||||
*/
|
||||
class _Rank extends \IPS\Node\Model
|
||||
{
|
||||
/**
|
||||
* @brief Database Table
|
||||
*/
|
||||
public static $databaseTable = 'core_member_ranks';
|
||||
|
||||
/**
|
||||
* @brief [Node] Order Database Column
|
||||
*/
|
||||
public static $databaseColumnOrder = 'points';
|
||||
|
||||
/**
|
||||
* @brief [Node] Sortable?
|
||||
*/
|
||||
public static $nodeSortable = FALSE;
|
||||
|
||||
/**
|
||||
* @brief Multiton Store
|
||||
*/
|
||||
protected static $multitons;
|
||||
|
||||
/**
|
||||
* @brief [Node] Node Title
|
||||
*/
|
||||
public static $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_';
|
||||
|
||||
/**
|
||||
* @brief [Node] ACP Restrictions
|
||||
* @code
|
||||
array(
|
||||
'app' => 'core', // The application key which holds the restrictrions
|
||||
'module' => 'foo', // The module key which holds the restrictions
|
||||
'map' => array( // [Optional] The key for each restriction - can alternatively use "prefix"
|
||||
'add' => 'foo_add',
|
||||
'edit' => 'foo_edit',
|
||||
'permissions' => 'foo_perms',
|
||||
'delete' => 'foo_delete'
|
||||
),
|
||||
'all' => 'foo_manage', // [Optional] The key to use for any restriction not provided in the map (only needed if not providing all 4)
|
||||
'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(
|
||||
'app' => 'core',
|
||||
'module' => 'achievements',
|
||||
'prefix' => 'ranks_',
|
||||
);
|
||||
|
||||
/**
|
||||
* @brief [ActiveRecord] Caches
|
||||
* @note Defined cache keys will be cleared automatically as needed
|
||||
*/
|
||||
protected $caches = ['achievementRanks'];
|
||||
|
||||
/**
|
||||
* [ActiveRecord] Get cached rules
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getStore(): array
|
||||
{
|
||||
if ( !isset( \IPS\Data\Store::i()->achievementRanks ) )
|
||||
{
|
||||
\IPS\Data\Store::i()->achievementRanks = iterator_to_array( \IPS\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' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Work out the rank for a given number of points
|
||||
*
|
||||
* @param int $points Number of points
|
||||
* @return \IPS\core\Achievements\Rank|NULL
|
||||
*/
|
||||
public static function fromPoints( $points ): ?\IPS\core\Achievements\Rank
|
||||
{
|
||||
$return = NULL;
|
||||
foreach ( static::getStore() as $rank )
|
||||
{
|
||||
if ( $points >= $rank->points )
|
||||
{
|
||||
$return = $rank;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* [Node] Get Icon for tree
|
||||
*
|
||||
* @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()
|
||||
{
|
||||
if ( $this->icon )
|
||||
{
|
||||
return \IPS\File::get( 'core_Badges', $this->icon );
|
||||
}
|
||||
else
|
||||
{
|
||||
return \IPS\Theme::i()->resource( 'default_rank.png', 'core', 'global' );
|
||||
}
|
||||
}
|
||||
|
||||
protected static $rankPositions = [];
|
||||
/**
|
||||
* Fetch the rank position from all ranks
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rankPosition(): array
|
||||
{
|
||||
if ( ! isset( static::$rankPositions[ $this->id ] ) )
|
||||
{
|
||||
$pos = 1;
|
||||
|
||||
foreach( static::getStore() as $rank )
|
||||
{
|
||||
if ( $rank->id == $this->id )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
$pos++;
|
||||
}
|
||||
|
||||
static::$rankPositions[ $this->id ] = [
|
||||
'pos' => $pos,
|
||||
'max' => \count( static::getStore() )
|
||||
];
|
||||
}
|
||||
|
||||
return static::$rankPositions[ $this->id ];
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get rank image HTML
|
||||
*
|
||||
* @param string|NULL $cssClass Optional CSS class to apply
|
||||
* @return string
|
||||
*/
|
||||
public function html( ?string $cssClass = NULL ): string
|
||||
{
|
||||
return \IPS\Theme::i()->getTemplate( 'global', 'core', 'global' )->rank( $this, $cssClass );
|
||||
}
|
||||
|
||||
/**
|
||||
* [Node] Get Node Description
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get__description()
|
||||
{
|
||||
return \IPS\Member::loggedIn()->language()->addToStack( 'achievements_awards_points', FALSE, [ 'pluralize' => [ $this->points ] ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* [Node] Add/Edit Form
|
||||
*
|
||||
* @param \IPS\Helpers\Form $form The form
|
||||
* @return void
|
||||
*/
|
||||
public function form( &$form )
|
||||
{
|
||||
$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_Badges', $this->icon ) : NULL, TRUE, array( 'obscure' => FALSE, 'allowedFileTypes' => array_merge( \IPS\Image::$imageExtensions, ['svg'] ), 'checkImage' => TRUE, 'storageExtension' => 'core_Badges' ), function( $val ) {
|
||||
if( !$val )
|
||||
{
|
||||
throw new \DomainException('achievements_bad_image');
|
||||
}
|
||||
|
||||
/* Good luck with your fancy SVG */
|
||||
$ext = mb_substr( $val->originalFilename, ( mb_strrpos( $val->originalFilename, '.' ) + 1 ) );
|
||||
if( $ext !== 'svg' )
|
||||
{
|
||||
try
|
||||
{
|
||||
$image = \IPS\Image::create( $val->contents() );
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
{
|
||||
throw new \DomainException( 'achievements_bad_image' );
|
||||
}
|
||||
}
|
||||
}, NULL, NULL, 'member_ranks_icon' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* [Node] Format form values from add/edit form for save
|
||||
*
|
||||
* @param array $values Values from the form
|
||||
* @return array
|
||||
*/
|
||||
public function formatFormValues( $values )
|
||||
{
|
||||
if ( !$this->id )
|
||||
{
|
||||
$this->save();
|
||||
}
|
||||
|
||||
\IPS\Lang::saveCustom( 'core', "core_member_rank_{$this->id}", $values['member_ranks_word_custom'] );
|
||||
unset( $values['member_ranks_word_custom'] );
|
||||
|
||||
$_values = $values;
|
||||
$values = array();
|
||||
foreach ( $_values as $k => $v )
|
||||
{
|
||||
if( mb_substr( $k, 0, 13 ) === 'member_ranks_' )
|
||||
{
|
||||
$values[ mb_substr( $k, 13 ) ] = $v;
|
||||
}
|
||||
else
|
||||
{
|
||||
$values[ $k ] = $v;
|
||||
}
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show ranks on the community?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function show(): bool
|
||||
{
|
||||
return !( ( \IPS\Settings::i()->achievements_rebuilding or !\IPS\Settings::i()->achievements_enabled ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* [ActiveRecord] Duplicate
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __clone()
|
||||
{
|
||||
if ( $this->skipCloneDuplication === TRUE )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
$oldImage = $this->icon;
|
||||
|
||||
parent::__clone();
|
||||
|
||||
|
||||
if ( $oldImage )
|
||||
{
|
||||
try
|
||||
{
|
||||
$image = \IPS\File::get( 'core_Badges', $oldImage );
|
||||
$newImage = \IPS\File::create( 'core_Badges', $image->originalFilename, $image->contents() );
|
||||
$this->icon = (string) $newImage;
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
{
|
||||
$this->icon = NULL;
|
||||
}
|
||||
|
||||
$this->save();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete Record
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
if ( $this->icon )
|
||||
{
|
||||
try
|
||||
{
|
||||
\IPS\File::get( 'core_Badges', $this->icon )->delete();
|
||||
}
|
||||
catch( \Exception $ex ) { }
|
||||
}
|
||||
|
||||
parent::delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get output for API
|
||||
*
|
||||
* @param \IPS\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 )
|
||||
{
|
||||
return array(
|
||||
'id' => $this->id,
|
||||
'name' => $this->_title,
|
||||
'icon' => $this->icon ? $this->_icon->url : $this->_icon,
|
||||
'points' => $this->points
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Import from Xml
|
||||
*
|
||||
* @param string $file The file to import data from
|
||||
* @param boolean $option How to handle existing ranks
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function importXml( $file, $option=NULL )
|
||||
{
|
||||
/* Open XML file */
|
||||
$xml = \IPS\Xml\XMLReader::safeOpen( $file );
|
||||
|
||||
if ( ! @$xml->read() )
|
||||
{
|
||||
throw new \DomainException( 'xml_upload_invalid' );
|
||||
}
|
||||
|
||||
/* Did we want to wipe first? */
|
||||
if ( $option == 'wipe' )
|
||||
{
|
||||
foreach( \IPS\core\Achievements\Rank::getStore() as $rank )
|
||||
{
|
||||
$rank->delete();
|
||||
}
|
||||
}
|
||||
|
||||
/* Start looping through each row */
|
||||
while ( $xml->read() and $xml->name == 'rank' )
|
||||
{
|
||||
if( $xml->nodeType != \XMLReader::ELEMENT )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$insert = array(
|
||||
'points' => 0,
|
||||
'title' => NULL,
|
||||
'icon' => NULL
|
||||
);
|
||||
|
||||
while ( $xml->read() and $xml->name != 'rank' )
|
||||
{
|
||||
if( $xml->nodeType != \XMLReader::ELEMENT )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
switch( $xml->name )
|
||||
{
|
||||
case 'title':
|
||||
$insert['title'] = $xml->readString();
|
||||
break;
|
||||
|
||||
case 'points':
|
||||
$insert['points'] = $xml->readString();
|
||||
break;
|
||||
|
||||
case 'icon_name':
|
||||
$insert['icon_name'] = $xml->readString();
|
||||
break;
|
||||
|
||||
case 'icon_data':
|
||||
$insert['icon_data'] = base64_decode( $xml->readString() );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Did we want to wipe existing ranks with the same points? */
|
||||
if ( $option == 'replace' )
|
||||
{
|
||||
foreach( \IPS\core\Achievements\Rank::getStore() as $rank )
|
||||
{
|
||||
if ( $rank->points == $insert['points'] )
|
||||
{
|
||||
$rank->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $insert['icon_name'] ) and ! empty( $insert['icon_data'] ) )
|
||||
{
|
||||
$insert['icon'] = (string) \IPS\File::create( 'core_Badges', $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 );
|
||||
|
||||
if ( ! empty( $insert['title'] ) )
|
||||
{
|
||||
\IPS\Lang::saveCustom( 'core', "core_member_rank_{$insertId}", $insert['title'] );
|
||||
}
|
||||
}
|
||||
|
||||
unset( \IPS\Data\Store::i()->achievementRanks );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
<?php
|
||||
/**
|
||||
* @brief Badge Model (as in, a representation of a badge a member *can* earn, not a badge a particular member *has* earned)
|
||||
* @author <a href='https://www.invisioncommunity.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) Invision Power Services, Inc.
|
||||
* @license https://www.invisioncommunity.com/legal/standards/
|
||||
* @package Invision Community
|
||||
* @since 25 Mar 21
|
||||
*/
|
||||
|
||||
namespace IPS\core\Achievements;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recognize
|
||||
*/
|
||||
class _Recognize extends \IPS\Patterns\ActiveRecord
|
||||
{
|
||||
/**
|
||||
* @brief Database Table
|
||||
*/
|
||||
public static $databaseTable = 'core_member_recognize';
|
||||
|
||||
/**
|
||||
* @brief [ActiveRecord] Database Prefix
|
||||
*/
|
||||
public static $databasePrefix = 'r_';
|
||||
|
||||
/**
|
||||
* @brief Multiton Store
|
||||
*/
|
||||
protected static $multitons = array();
|
||||
|
||||
/**
|
||||
* Get member object
|
||||
*
|
||||
* @return \IPS\Member
|
||||
*/
|
||||
public function get__given_by(): \IPS\Member
|
||||
{
|
||||
return \IPS\Member::load( $this->given_by );
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a recognize row from the content item
|
||||
*
|
||||
* @param \IPS\Content $content Content item that has been rewarded
|
||||
* @return \IPS\core\Achievements\Recognize
|
||||
*/
|
||||
public static function loadFromContent( $content ): \IPS\core\Achievements\Recognize
|
||||
{
|
||||
$idField = $content::$databaseColumnId;
|
||||
|
||||
/* Let any exceptions bubble up */
|
||||
return static::constructFromData(
|
||||
\IPS\Db::i()->select( '*', 'core_member_recognize', [ 'r_content_class=? and r_content_id=?', \get_class( $content ), $content->$idField ] )->first()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new recognize entry
|
||||
*
|
||||
* @param \IPS\Content $content Content item that is being rewarded
|
||||
* @param \IPS\Member $member Member to award
|
||||
* @param int $points Number of points to add
|
||||
* @param \IPS\core\Achievements\Badge|NULL $badge Badge to assign (if any)
|
||||
* @param string $message Custom message (if any)
|
||||
* @param \IPS\Member $awardedBy Awarded by
|
||||
* @param bool $showPublicly Show this message to everyone
|
||||
*/
|
||||
public static function add( $content, \IPS\Member $member, $points, $badge, $message, \IPS\Member $awardedBy, $showPublicly=FALSE )
|
||||
{
|
||||
$idField = $content::$databaseColumnId;
|
||||
|
||||
/* Add to database */
|
||||
$recognize = new static;
|
||||
$recognize->member_id = $member->member_id;
|
||||
$recognize->content_class = \get_class( $content );
|
||||
$recognize->content_id = $content->$idField;
|
||||
$recognize->added = time();
|
||||
$recognize->points = $points;
|
||||
$recognize->badge = $badge ? $badge->_id : 0;
|
||||
$recognize->message = $message;
|
||||
$recognize->given_by = $awardedBy->member_id;
|
||||
$recognize->public = $showPublicly;
|
||||
$recognize->save();
|
||||
|
||||
if ( $badge )
|
||||
{
|
||||
$member->awardBadge( $badge, 0, 0, ['subject'], $recognize->id );
|
||||
$member->logHistory( 'core', 'badges', [ 'action' => 'manual', 'id' => $badge->_id, 'recognize' => $recognize->id ] );
|
||||
}
|
||||
if ( $points )
|
||||
{
|
||||
$member->logHistory( 'core', 'points', array('by' => 'manual', 'points' => $points, 'recognize' => $recognize->id ) );
|
||||
$member->awardPoints( $points, 0, [], ['subject'], $recognize->id );
|
||||
}
|
||||
|
||||
$notification = new \IPS\Notification( \IPS\Application::load( 'core' ), 'new_recognize', $member, [ $recognize ], [ $recognize->id ] );
|
||||
$notification->recipients->attach( $member );
|
||||
$notification->send();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the content object
|
||||
*
|
||||
* @return \IPS\Content
|
||||
*/
|
||||
public function content()
|
||||
{
|
||||
$class = $this->content_class;
|
||||
return $class::loadAndCheckPerms( $this->content_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper to get content.
|
||||
*
|
||||
* @return \IPS\Content\Item|NULL
|
||||
* @note This simply wraps content()
|
||||
*/
|
||||
public function contentWrapper()
|
||||
{
|
||||
/* Get container, if valid */
|
||||
$content = NULL;
|
||||
|
||||
try
|
||||
{
|
||||
$content = $this->content();
|
||||
}
|
||||
catch( \BadMethodCallException | \OutOfRangeException $e ) { }
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a badge, or null
|
||||
*
|
||||
* @return NULL|\IPS\core\Achievements\Badge
|
||||
*/
|
||||
public function badge()
|
||||
{
|
||||
try
|
||||
{
|
||||
return \IPS\core\Achievements\Badge::load( $this->badge );
|
||||
}
|
||||
catch( \Exception $e ) { }
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a member or NULL if the member no longer exists
|
||||
*
|
||||
* @return NULL|\IPS\Member
|
||||
*/
|
||||
public function awardedBy()
|
||||
{
|
||||
try
|
||||
{
|
||||
return \IPS\Member::load( $this->given_by );
|
||||
}
|
||||
catch( \Exception $e ) { }
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the recognize and remove points / badges earned
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
try
|
||||
{
|
||||
$author = \IPS\Member::load( $this->member_id );
|
||||
if ( $this->points )
|
||||
{
|
||||
/* Wade does it this way so I guess there's a reason. And let the reason be love */
|
||||
\IPS\Db::i()->update( 'core_members', "achievements_points = achievements_points - " . \intval( $this->points ), [ 'member_id=?', $this->member_id ] );
|
||||
}
|
||||
|
||||
if ( $this->badge )
|
||||
{
|
||||
\IPS\Db::i()->delete( 'core_member_badges', [ 'member_id=? and recognize_id=?', $this->member_id, $this->id ] );
|
||||
}
|
||||
}
|
||||
catch( \Exception $e ) { }
|
||||
|
||||
parent::delete();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,569 @@
|
||||
<?php
|
||||
/**
|
||||
* @brief Achievements Rule Model
|
||||
* @author <a href='https://www.invisioncommunity.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) Invision Power Services, Inc.
|
||||
* @license https://www.invisioncommunity.com/legal/standards/
|
||||
* @package Invision Community
|
||||
* @since 24 Feb 2021
|
||||
*/
|
||||
|
||||
namespace IPS\core\Achievements;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Achievements Rule Model
|
||||
*/
|
||||
class _Rule extends \IPS\Node\Model
|
||||
{
|
||||
/**
|
||||
* @brief Database Table
|
||||
*/
|
||||
public static $databaseTable = 'core_achievements_rules';
|
||||
|
||||
/**
|
||||
* @brief Multiton Store
|
||||
*/
|
||||
protected static $multitons;
|
||||
|
||||
/**
|
||||
* @brief [Node] Node Title
|
||||
*/
|
||||
public static $nodeTitle = 'achievements_rules';
|
||||
|
||||
/**
|
||||
* @brief [Node] ACP Restrictions
|
||||
* @code
|
||||
array(
|
||||
'app' => 'core', // The application key which holds the restrictrions
|
||||
'module' => 'foo', // The module key which holds the restrictions
|
||||
'map' => array( // [Optional] The key for each restriction - can alternatively use "prefix"
|
||||
'add' => 'foo_add',
|
||||
'edit' => 'foo_edit',
|
||||
'permissions' => 'foo_perms',
|
||||
'delete' => 'foo_delete'
|
||||
),
|
||||
'all' => 'foo_manage', // [Optional] The key to use for any restriction not provided in the map (only needed if not providing all 4)
|
||||
'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(
|
||||
'app' => 'core',
|
||||
'module' => 'achievements',
|
||||
'prefix' => 'rules_',
|
||||
);
|
||||
|
||||
/**
|
||||
* @brief [ActiveRecord] Caches
|
||||
* @note Defined cache keys will be cleared automatically as needed
|
||||
*/
|
||||
protected $caches = ['achievementRules'];
|
||||
|
||||
/**
|
||||
* [ActiveRecord] Get cached rules
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getStore(): array
|
||||
{
|
||||
if ( !isset( \IPS\Data\Store::i()->achievementRules ) )
|
||||
{
|
||||
$v = [];
|
||||
foreach ( static::roots() as $rule )
|
||||
{
|
||||
if ( !isset( $v[ $rule->action ] ) )
|
||||
{
|
||||
$v[ $rule->action ] = [];
|
||||
}
|
||||
$v[ $rule->action ][ $rule->id ] = [
|
||||
'filters' => $rule->filters,
|
||||
'points_subject'=> $rule->points_subject,
|
||||
'badge_subject' => $rule->badge_subject,
|
||||
'points_other' => $rule->points_other,
|
||||
'badge_other' => $rule->badge_other,
|
||||
'enabled' => $rule->enabled
|
||||
];
|
||||
}
|
||||
\IPS\Data\Store::i()->achievementRules = $v;
|
||||
}
|
||||
|
||||
return \IPS\Data\Store::i()->achievementRules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the extension
|
||||
*
|
||||
* @return \IPS\core\Achievements\Actions\AbstractAchievementAction
|
||||
*/
|
||||
public function extension(): \IPS\core\Achievements\Actions\AbstractAchievementAction
|
||||
{
|
||||
$exploded = explode( '_', $this->action );
|
||||
return \IPS\Application::load( $exploded[0] )->extensions( 'core', 'AchievementAction' )[ $exploded[1] ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get JSON-decoded filters
|
||||
*
|
||||
* @return array|NULL
|
||||
*/
|
||||
protected function get_filters()
|
||||
{
|
||||
if ( isset( $this->_data['filters'] ) and \is_array( $this->_data['filters'] ) )
|
||||
{
|
||||
return $this->_data['filters'];
|
||||
}
|
||||
|
||||
return ( isset( $this->_data['filters'] ) and $this->_data['filters'] ) ? json_decode( $this->_data['filters'], TRUE ) : NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* [Node] Add/Edit Form
|
||||
*
|
||||
* @param \IPS\Helpers\Form $form The form
|
||||
* @return void
|
||||
*/
|
||||
public function form( &$form )
|
||||
{
|
||||
$form->_activeFilters = [];
|
||||
|
||||
$options = [];
|
||||
$toggles = [];
|
||||
$extraElements = [];
|
||||
foreach ( \IPS\Application::allExtensions( 'core', 'AchievementAction' ) as $achievementAction )
|
||||
{
|
||||
if ( ! $achievementAction->canUse() )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$exploded = explode( '\\', \get_class( $achievementAction ) );
|
||||
$options[ "{$exploded[1]}_{$exploded[5]}" ] = "AchievementAction__{$exploded[5]}";
|
||||
|
||||
$toggles[ "{$exploded[1]}_{$exploded[5]}" ] = [ "{$exploded[1]}_{$exploded[5]}_award_subject" ];
|
||||
$toggles[ "{$exploded[1]}_{$exploded[5]}" ][] = "{$exploded[1]}_{$exploded[5]}_award_subject_badge";
|
||||
foreach ( $achievementAction->filters( $this->filters, \IPS\Http\Url::internal( $this->id ? "app=core&module=achievements&controller=rules&do=form&id={$this->id}" : 'app=core&module=achievements&controller=rules&do=form' ) ) as $filterKey => $filterElement )
|
||||
{
|
||||
if ( $this->filters and array_key_exists( $filterKey, $this->filters ) )
|
||||
{
|
||||
$form->_activeFilters[] = $filterElement->name;
|
||||
}
|
||||
if ( !$filterElement->htmlId )
|
||||
{
|
||||
$filterElement->htmlId = $filterElement->name;
|
||||
}
|
||||
$toggles[ "{$exploded[1]}_{$exploded[5]}" ][] = $filterElement->htmlId;
|
||||
$extraElements[] = $filterElement;
|
||||
}
|
||||
|
||||
$awardTo = $achievementAction->awardOptions();
|
||||
$awardSubjectField = new \IPS\Helpers\Form\Custom( "{$exploded[1]}_{$exploded[5]}_award_subject", [ 'points' => $this->points_subject, 'badge' => $this->badge_subject ], FALSE, [
|
||||
'getHtml' => function( $field ) {
|
||||
return \IPS\Theme::i()->getTemplate('achievements')->awardField( $field->name, $field->value );
|
||||
}
|
||||
], NULL, NULL, NULL, "{$exploded[1]}_{$exploded[5]}_award_subject" );
|
||||
if ( isset( $awardTo['subject'] ) )
|
||||
{
|
||||
$awardSubjectField->label = \IPS\Member::loggedIn()->language()->addToStack( $awardTo['subject'] );
|
||||
}
|
||||
$extraElements[] = $awardSubjectField;
|
||||
$extraElements[] = new \IPS\Helpers\Form\Translatable( "{$exploded[1]}_{$exploded[5]}_award_subject_badge", NULL, NULL, [ 'app' => 'core', 'key' => ( $this->id ) ? "core_award_subject_badge_" . $this->id : NULL ], NULL, NULL, NULL, "{$exploded[1]}_{$exploded[5]}_award_subject_badge" );
|
||||
|
||||
if ( isset( $awardTo['other'] ) )
|
||||
{
|
||||
$awardOtherField = new \IPS\Helpers\Form\Custom( "{$exploded[1]}_{$exploded[5]}_award_other", [ 'points' => $this->points_other, 'badge' => $this->badge_other ], FALSE, [
|
||||
'getHtml' => function( $field ) {
|
||||
return \IPS\Theme::i()->getTemplate('achievements')->awardField( $field->name, $field->value );
|
||||
}
|
||||
], NULL, NULL, NULL, "{$exploded[1]}_{$exploded[5]}_award_other" );
|
||||
$awardOtherField->label = \IPS\Member::loggedIn()->language()->addToStack( $awardTo['other'] );
|
||||
$extraElements[] = $awardOtherField;
|
||||
$extraElements[] = new \IPS\Helpers\Form\Translatable( "{$exploded[1]}_{$exploded[5]}_award_other_badge", NULL, NULL, [ 'app' => 'core', 'key' => ( $this->id ) ? "core_award_other_badge_" . $this->id : NULL ], NULL, NULL, NULL, "{$exploded[1]}_{$exploded[5]}_award_other_badge" );
|
||||
$toggles[ "{$exploded[1]}_{$exploded[5]}" ][] = "{$exploded[1]}_{$exploded[5]}_award_other";
|
||||
$toggles[ "{$exploded[1]}_{$exploded[5]}" ][] = "{$exploded[1]}_{$exploded[5]}_award_other_badge";
|
||||
}
|
||||
}
|
||||
|
||||
$form->add( new \IPS\Helpers\Form\Select( 'achievement_rule_action', $this->action, TRUE, [ 'options' => $options, 'toggles' => $toggles, 'sort' => TRUE ] ) );
|
||||
|
||||
foreach ( $extraElements as $element )
|
||||
{
|
||||
$form->add( $element );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* [Node] Format form values from add/edit form for save
|
||||
*
|
||||
* @param array $values Values from the form
|
||||
* @return array
|
||||
*/
|
||||
public function formatFormValues( $values )
|
||||
{
|
||||
if ( !$this->id )
|
||||
{
|
||||
$this->action = '';
|
||||
$this->filters = NULL;
|
||||
$this->points_other = 0;
|
||||
$this->points_subject = 0;
|
||||
$this->save();
|
||||
}
|
||||
|
||||
$exploded = explode( '_', $values['achievement_rule_action'] );
|
||||
$extension = \IPS\Application::load( $exploded[0] )->extensions( 'core', 'AchievementAction' )[ $exploded[1] ];
|
||||
|
||||
$filterValues = [];
|
||||
foreach ( \IPS\Request::i()->activeFilters ?: [] as $k => $v )
|
||||
{
|
||||
$filterValues[ $k ] = $values[ $k ];
|
||||
}
|
||||
$filters = $extension->formatFilterValues( $filterValues );
|
||||
|
||||
foreach( [ 'subject', 'other' ] as $type )
|
||||
{
|
||||
if ( isset( $values[$values['achievement_rule_action'] . '_award_' . $type . '_badge'] ) )
|
||||
{
|
||||
if ( $values[$values['achievement_rule_action'] . '_award_subject']['badge'] )
|
||||
{
|
||||
\IPS\Lang::saveCustom( 'core', "core_award_" . $type . "_badge_{$this->id}", $values[$values['achievement_rule_action'] . '_award_' . $type . '_badge'] );
|
||||
}
|
||||
else
|
||||
{
|
||||
\IPS\Lang::deleteCustom( 'core', "core_award_" . $type . "_badge_{$this->id}" );
|
||||
}
|
||||
|
||||
unset( $values[$values['achievement_rule_action'] . '_award_' . $type . '_badge'] );
|
||||
}
|
||||
}
|
||||
|
||||
$return = [
|
||||
'action' => $values['achievement_rule_action'],
|
||||
'filters' => $filters ? json_encode( $filters ) : NULL,
|
||||
'milestone' => isset( $filters['milestone'] ) ? $filters['milestone'] : NULL,
|
||||
'points_subject'=> \intval( $values[ $values['achievement_rule_action'] . '_award_subject' ]['points'] ),
|
||||
'badge_subject' => $values[ $values['achievement_rule_action'] . '_award_subject' ]['badge'] ? \intval( $values[ $values['achievement_rule_action'] . '_award_subject' ]['badge'] ): NULL,
|
||||
'points_other' => \intval( isset( $values[ $values['achievement_rule_action'] . '_award_other' ] ) ? $values[ $values['achievement_rule_action'] . '_award_other' ]['points'] : 0 ),
|
||||
'badge_other' => isset( $values[ $values['achievement_rule_action'] . '_award_other' ] ) ? ( $values[ $values['achievement_rule_action'] . '_award_other' ]['badge'] ? \intval( $values[ $values['achievement_rule_action'] . '_award_other' ]['badge'] ) : NULL ) : NULL,
|
||||
];
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* [Node] Get buttons to display in tree
|
||||
* Example code explains return value
|
||||
*
|
||||
* @param string $url Base URL
|
||||
* @param bool $subnode Is this a subnode?
|
||||
* @return array
|
||||
*/
|
||||
public function getButtons( $url, $subnode=FALSE )
|
||||
{
|
||||
$buttons = parent::getButtons( $url, $subnode );
|
||||
|
||||
/* Enable/Disable */
|
||||
$buttons['toggleEnabled'] = array(
|
||||
'icon' => ( $this->enabled ) ? 'pause-circle' : 'play-circle',
|
||||
'title' => ( $this->enabled ) ? 'acp_disable_rule' : 'acp_enable_rule',
|
||||
'link' => \IPS\Http\Url::internal( "app=core&module=achievements&controller=rules&do=toggleEnabled&id={$this->_id}&enable=" . ( $this->enabled ? 0 : 1 ) )->csrf(),
|
||||
'data' => [ 'data-confirm' => 'true' ]
|
||||
);
|
||||
|
||||
return $buttons;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get badge for subject
|
||||
*
|
||||
* @return \IPS\core\Achievements\Badge|NULL
|
||||
*/
|
||||
public function badgeSubject(): ?\IPS\core\Achievements\Badge
|
||||
{
|
||||
if ( $this->badge_subject )
|
||||
{
|
||||
try
|
||||
{
|
||||
return \IPS\core\Achievements\Badge::load( $this->badge_subject );
|
||||
}
|
||||
catch( \OutOfRangeException $e ) { }
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get badge for others
|
||||
*
|
||||
* @return \IPS\core\Achievements\Badge|NULL
|
||||
*/
|
||||
public function badgeOther(): ?\IPS\core\Achievements\Badge
|
||||
{
|
||||
if ( $this->badge_other )
|
||||
{
|
||||
try
|
||||
{
|
||||
return \IPS\core\Achievements\Badge::load( $this->badge_other );
|
||||
}
|
||||
catch( \OutOfRangeException $e ) { }
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete Record
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
parent::delete();
|
||||
|
||||
\IPS\Lang::deleteCustom( 'core', "core_award_subject_badge_{$this->id}" );
|
||||
\IPS\Lang::deleteCustom( 'core', "core_award_other_badge_{$this->id}" );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get details about the rebuild progress so far
|
||||
*
|
||||
* @return array|NULL
|
||||
*/
|
||||
public static function getRebuildProgress(): ?array
|
||||
{
|
||||
$return = [ 'count' => 0, 'processed' => 0 ];
|
||||
$rebuilding = FALSE;
|
||||
foreach( \IPS\Db::i()->select( '*', 'core_queue', [ [ '`key`=?', 'RebuildAchievements' ] ] ) as $rebuild )
|
||||
{
|
||||
$data = json_decode( $rebuild['data'], TRUE );
|
||||
|
||||
if ( isset( $data['processed'] ) and isset( $data['count'] ) )
|
||||
{
|
||||
$rebuilding = TRUE;
|
||||
$return['processed'] += (int) $data['processed'];
|
||||
$return['count'] += (int) $data['count'];
|
||||
}
|
||||
}
|
||||
|
||||
$return['percentage'] = ( $return['count'] ? ( round( 100 / $return['count'] * $return['processed'], 2 ) ) : 100 );
|
||||
|
||||
return $rebuilding ? $return : NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rebuild all cheeeeeevs
|
||||
*
|
||||
* @param \IPS\DateTime|null $time
|
||||
*/
|
||||
public static function rebuildAllAchievements( ?\IPS\DateTime $time )
|
||||
{
|
||||
\IPS\Db::i()->delete( 'core_achievements_log' );
|
||||
\IPS\Db::i()->delete( 'core_points_log' );
|
||||
\IPS\Db::i()->delete( 'core_member_badges', [ 'rule > 0' ] ); /* 0 is a manually awarded badge. Don't hate the coder, hate the player */
|
||||
\IPS\Db::i()->update( 'core_members', [ 'achievements_points' => 0 ] );
|
||||
|
||||
/* Remove all previous tasks */
|
||||
\IPS\Db::i()->delete( 'core_queue', [ '`key`=?', 'RebuildAchievements' ] );
|
||||
|
||||
foreach( \IPS\Application::applications() as $app )
|
||||
{
|
||||
foreach ( \IPS\Application::load( $app->directory )->extensions( 'core', 'AchievementAction' ) as $extension )
|
||||
{
|
||||
if ( method_exists( $extension, 'rebuildData' ) )
|
||||
{
|
||||
$bits = explode( '\\', \get_class( $extension ) );
|
||||
$className = array_pop( $bits );
|
||||
\IPS\Task::queue( 'core', 'RebuildAchievements', [
|
||||
'extension' => $app->directory . '_' . $className,
|
||||
'data' => $extension::rebuildData(),
|
||||
'time' => ( $time ) ? $time->getTimestamp() : NULL,
|
||||
], 4 );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Import from Xml
|
||||
*
|
||||
* @param string $file The file to import data from
|
||||
* @param boolean $deleteExisting Remove existing rules first?
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function importXml( $file, $deleteExisting=FALSE )
|
||||
{
|
||||
/* Open XML file */
|
||||
$xml = \IPS\Xml\XMLReader::safeOpen( $file );
|
||||
|
||||
if ( ! @$xml->read() )
|
||||
{
|
||||
throw new \DomainException( 'xml_upload_invalid' );
|
||||
}
|
||||
|
||||
/* Did we want to wipe first? */
|
||||
if ( $deleteExisting )
|
||||
{
|
||||
foreach( \IPS\core\Achievements\Rule::getStore() as $action => $rules )
|
||||
{
|
||||
foreach( $rules as $ruleId => $rule )
|
||||
{
|
||||
static::load( $ruleId )->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$allBadges = [];
|
||||
$badgeMap = [];
|
||||
|
||||
/* Rules: Start looping through each row */
|
||||
while ( $xml->read() )
|
||||
{
|
||||
if( $xml->nodeType != \XMLReader::ELEMENT )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( $xml->name == 'rule' )
|
||||
{
|
||||
$badgesToUpdate = [];
|
||||
$insert = array(
|
||||
'action' => NULL,
|
||||
'filters' => NULL,
|
||||
'milestone' => 0,
|
||||
'points_subject' => 0,
|
||||
'points_other' => 0,
|
||||
'badge_subject' => 0,
|
||||
'badge_other' => 0,
|
||||
'enabled' => 1,
|
||||
);
|
||||
|
||||
$awardSubject = NULL;
|
||||
$awardOther = NULL;
|
||||
|
||||
while ( $xml->read() and $xml->name != 'rule' )
|
||||
{
|
||||
if ( $xml->nodeType != \XMLReader::ELEMENT )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
switch ( $xml->name )
|
||||
{
|
||||
case 'action':
|
||||
case 'filters':
|
||||
$insert[$xml->name] = $xml->readString();
|
||||
break;
|
||||
case 'milestone':
|
||||
case 'points_subject':
|
||||
case 'points_other':
|
||||
case 'enabled':
|
||||
$insert[$xml->name] = (int)$xml->readString();
|
||||
break;
|
||||
case 'badge_subject':
|
||||
case 'badge_other':
|
||||
$badgesToUpdate[$xml->name] = $xml->readString();
|
||||
break;
|
||||
case 'award_subject_lang':
|
||||
$awardSubject = $xml->readString();
|
||||
break;
|
||||
case 'award_other_lang':
|
||||
$awardOther = $xml->readString();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$insertId = \IPS\Db::i()->insert( 'core_achievements_rules', $insert );
|
||||
$allBadges[ $insertId ] = $badgesToUpdate;
|
||||
|
||||
if ( ! empty( $awardSubject ) )
|
||||
{
|
||||
\IPS\Lang::saveCustom( 'core', "core_award_subject_badge_{$insertId}", $awardSubject );
|
||||
}
|
||||
|
||||
if ( ! empty( $awardOther ) )
|
||||
{
|
||||
\IPS\Lang::saveCustom( 'core', "core_award_other_badge_{$insertId}", $awardOther );
|
||||
}
|
||||
}
|
||||
/* Badges: Start looping through each row */
|
||||
else if ( $xml->name == 'badge' )
|
||||
{
|
||||
if( $xml->nodeType != \XMLReader::ELEMENT )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$insert = [];
|
||||
$title = NULL;
|
||||
$id = NULL;
|
||||
|
||||
while ( $xml->read() and $xml->name != 'badge' )
|
||||
{
|
||||
if( $xml->nodeType != \XMLReader::ELEMENT )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
switch( $xml->name )
|
||||
{
|
||||
case 'manually_awarded':
|
||||
$insert[ $xml->name ] = (int) $xml->readString();
|
||||
break;
|
||||
case 'title':
|
||||
$title = $xml->readString();
|
||||
break;
|
||||
case 'icon_name':
|
||||
$insert['icon_name'] = $xml->readString();
|
||||
break;
|
||||
case 'icon_data':
|
||||
$insert['icon_data'] = base64_decode( $xml->readString() );
|
||||
break;
|
||||
case 'id':
|
||||
$id = $xml->readString();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $insert['icon_name'] ) and ! empty( $insert['icon_data'] ) )
|
||||
{
|
||||
$insert['image'] = (string) \IPS\File::create( 'core_Badges', $insert['icon_name'], $insert['icon_data'], NULL, TRUE, NULL, FALSE );
|
||||
unset( $insert['icon_name'] );
|
||||
unset( $insert['icon_data'] );
|
||||
}
|
||||
|
||||
$insertId = \IPS\Db::i()->insert( 'core_badges', $insert );
|
||||
$badgeMap[ $id ] = $insertId;
|
||||
|
||||
if ( ! empty( $title ) )
|
||||
{
|
||||
\IPS\Lang::saveCustom( 'core', "core_badges_{$insertId}", $title );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( \count( $allBadges ) )
|
||||
{
|
||||
foreach( $allBadges as $ruleId => $data )
|
||||
{
|
||||
$update = [];
|
||||
foreach( [ 'badge_subject', 'badge_other' ] as $type )
|
||||
{
|
||||
if ( isset( $data[ $type ] ) and isset( $badgeMap[ $data[ $type ] ] ) )
|
||||
{
|
||||
$update[ $type ] = $badgeMap[ $data[ $type ] ];
|
||||
}
|
||||
}
|
||||
|
||||
if ( \count( $update ) )
|
||||
{
|
||||
\IPS\Db::i()->update( 'core_achievements_rules', $update, ['`id`=?', $ruleId ] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unset( \IPS\Data\Store::i()->achievementRules );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user