Invision Power Services, Inc. * @copyright (c) Invision Power Services, Inc. * @license https://www.invisioncommunity.com/legal/standards/ * @package Invision Community * @subpackage * @since 3/6/2025 */ namespace IPS\Node; /* To prevent PHP errors (extending class does not exist) revealing path */ use IPS\core\CustomBadge as CustomBadgeClass; use IPS\Db; use IPS\Helpers\Form; use IPS\Helpers\Form\Color; use IPS\Helpers\Form\Icon; use IPS\Helpers\Form\Number; use IPS\Helpers\Form\Radio; use IPS\Helpers\Form\YesNo; use IPS\Output; use IPS\Theme; use UnderflowException; use function get_called_class; if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) ) { header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' ); exit; } trait CustomBadge { /** * @var array */ protected static array $customBadgeStore = array(); /** * Get the custom badge linked to this class * * @return CustomBadgeClass|null */ public function getRecordBadge() : CustomBadgeClass|null { $class = get_called_class(); $idColumn = static::$databaseColumnId; if( !isset( static::$customBadgeStore[ $class ][ $this->$idColumn ] ) ) { try { $row = Db::i()->select( '*', 'core_custom_badges', [ 'class=? and active_record_id=?', $class, $this->$idColumn ] )->first(); static::$customBadgeStore[ $class ][ $this->$idColumn ] = CustomBadgeClass::constructFromData( $row ); } catch( UnderflowException ) { static::$customBadgeStore[ $class ][ $this->$idColumn ] = false; } } return static::$customBadgeStore[ $class ][ $this->$idColumn ] ?: null; } /** * Add elements to the form which are responsible for creating custom badges, and loads the JS and CSS that forms the interactive and grid-style UI * * @param Form $form * @param CustomBadgeClass|null $badge * @return void */ public function addBadgeFieldsToForm( Form $form, ?CustomBadgeClass $badge=null ) : void { $form->class .= " ipsBadgePicker--form"; $shapeToggles = ["custombadge_background", "custombadge_border"]; $dynamicShapeToggles = [ "custombadge_sides", "custombadge_rotation" ]; $form->add( new YesNo( "custombadge_use_custom", (bool) $badge, true, [ 'togglesOff' => static::$customBadgeToggles ?? [], 'togglesOn' => [ "{$form->id}_header_badge_preview", "custombadge_border", "custombadge_foreground", "custombadge_background", "custombadge_shape", "custombadge_icon", "custombadge_sides", "custombadge_rotation", "custombadge_number_overlay", "custombadge_icon_size", 'preview' ] ] )); $form->addHeader( 'custombadge_preview' ); Output::i()->jsFiles = array_merge( Output::i()->jsFiles, Output::i()->js( 'admin_badgepreview.js', 'core', 'admin' ) ); $form->addHtml( Theme::i()->getTemplate( 'achievements', 'core', 'admin' )->badgePreview( $badge ? $badge->raw : '' ) ); $form->add( new Radio( "custombadge_shape", $badge ? $badge->shape : 'circle', null, ['options' => [ 'square' => 'custombadge_square', 'circle' => 'custombadge_circle', 'ngon' => 'custombadge_n_gon', 'star' => 'custombadge_star', 'flower' => 'custombadge_flower', "no_shape" => "custombadge_no_shape", ], 'toggles' => [ 'ngon' => [...$dynamicShapeToggles, ...$shapeToggles], 'star' => [...$dynamicShapeToggles, ...$shapeToggles], 'flower' => [...$dynamicShapeToggles, ...$shapeToggles], "square" => $shapeToggles, "circle" => $shapeToggles ], 'rowClasses' => ['ipsFieldRow--badge-creator ipsFieldRow--badge-creator-shape']], null, null, null, "custombadge_shape") ); $form->add( new Icon( "custombadge_icon", ( $badge and $badge->icon ) ? [ $badge->icon ] : null, false, ['useSvgIcon' => true, 'rowClasses' => ['ipsFieldRow--badge-creator ipsFieldRow--badge-creator-icon']], null, null, null, "custombadge_icon" ) ); /* The overlay is optional */ if ( !isset( static::$customBadgeNumberOverlay ) or static::$customBadgeNumberOverlay ) { $form->add( new Number( "custombadge_number_overlay", ( $badge and $badge->number_overlay ) ? $badge->number_overlay : 0, false, [ 'step' => 1, 'min' => 1, 'max' => 999, 'unlimited' => 0, 'unlimitedLang' => 'none', 'rowClasses' => ['ipsFieldRow--badge-creator'] ], null, null, null, "custombadge_number_overlay" ) ); } $form->add( new Color( "custombadge_background", $badge ? $badge->background : '#eeb95f', null, ['rowClasses' => ['ipsFieldRow--badge-creator ipsFieldRow--badge-creator-background']], null, null, null, "custombadge_background" ) ); $form->add( new Color( "custombadge_border", $badge ? $badge->border : '#f7d36f', null, ['rowClasses' => ['ipsFieldRow--badge-creator ipsFieldRow--badge-creator-border']], null, null, null, "custombadge_border" ) ); $form->add( new Color( "custombadge_foreground", $badge ? $badge->foreground : '#ffffff', null, ['rgba' => true, 'swatches' => true, 'rowClasses' => ['ipsFieldRow--badge-creator ipsFieldRow--badge-creator-foreground']], null, null, null, "custombadge_foreground" ) ); $form->add( new Form\Number( "custombadge_icon_size", $badge ? $badge->icon_size : 3, null, ["min" => 1, "max" => 5, "step" => 1, "range" => true, "rowClasses" => ['ipsFieldRow--badge-creator', 'ipsFieldRow--badge-creator-icon-size']], id: "custombadge_icon_size" ) ); $form->add( new Number( "custombadge_sides", $badge ? $badge->sides : 5, null, ['step' => 1, 'min' => 4, 'max' => 12, 'range' => true, 'rowClasses' => ['ipsFieldRow--badge-creator']], null, null, null, "custombadge_sides" ) ); $form->add( new Number( "custombadge_rotation", $badge?->rotation, null, ['step' => 1, 'min' => 0, 'max' => 90, 'range' => true, 'rowClasses' => ['ipsFieldRow--badge-creator']], null, null, null, "custombadge_rotation" ) ); } /** * @param array $values * @return void */ public function saveRecordBadge( array &$values ) : void { $badge = $this->getRecordBadge(); /* Are we using a badge? */ if( $values['custombadge_use_custom'] ) { if( $badge === null ) { $badge = new CustomBadgeClass; $badge->class = get_called_class(); $idColumn = static::$databaseColumnId; $badge->active_record_id = $this->$idColumn; } $badge->shape = $values['custombadge_shape']; $badge->foreground = $values['custombadge_foreground']; $badge->background = $values['custombadge_background']; $badge->border = $values['custombadge_border']; $badge->icon = $values['custombadge_icon'][0] ?? null; $badge->rotation = $values['custombadge_rotation']; $badge->sides = $values['custombadge_sides']; $badge->number_overlay = $values['custombadge_number_overlay'] ?? 0; $badge->icon_size = $values['custombadge_icon_size'] ?? 3; $badge->save(); } elseif( $badge !== null ) { /* If we are not using a custom badge, and we already have one, delete it */ $badge->delete(); } /* Clear the form fields */ foreach ( ['shape', 'foreground', 'background', 'border', 'icon', 'rotation', 'sides', 'number_overlay', 'use_custom', 'icon_size'] as $key ) { unset( $values["custombadge_{$key}"] ); } } }