Version 5.0.6
This commit is contained in:
1 parent
a3a4a9ccbd
commit
b89858b2ff
1214 files changed
+92828
-14859
No files matched your search
@@ -10,21 +10,14 @@
|
||||
|
||||
namespace IPS\core;
|
||||
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use IPS\Data\Store;
|
||||
use IPS\Db;
|
||||
use IPS\Helpers\Form;
|
||||
use IPS\File;
|
||||
use IPS\Http\Url;
|
||||
use IPS\Http\Url\Raw;
|
||||
use IPS\Member;
|
||||
use IPS\Output;
|
||||
use IPS\Patterns\ActiveRecord;
|
||||
use IPS\Settings;
|
||||
use IPS\Theme;
|
||||
use IPS\Xml\DOMDocument;
|
||||
use OutOfRangeException;
|
||||
use UnderflowException;
|
||||
use function is_array;
|
||||
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
@@ -32,90 +25,71 @@ if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
exit;
|
||||
}
|
||||
|
||||
class CustomBadge {
|
||||
class CustomBadge extends ActiveRecord
|
||||
{
|
||||
/**
|
||||
* @var array|null
|
||||
* @brief [ActiveRecord] Database table
|
||||
* @note This MUST be over-ridden
|
||||
*/
|
||||
protected ?array $icon = null;
|
||||
public static ?string $databaseTable = 'core_custom_badges';
|
||||
|
||||
protected int $numberOverlay = 0;
|
||||
/**
|
||||
* @brief [ActiveRecord] Multiton Store
|
||||
* @note This needs to be declared in any child classes as well, only declaring here for editor code-complete/error-check functionality
|
||||
*/
|
||||
protected static array $multitons = array();
|
||||
|
||||
protected int $sides = 5;
|
||||
/**
|
||||
* Set Default Values (overriding $defaultValues)
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function setDefaultValues() : void
|
||||
{
|
||||
$this->shape = 'circle';
|
||||
$this->sides = 5;
|
||||
$this->rotation = 0;
|
||||
$this->number_overlay = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function get_icon() : array
|
||||
{
|
||||
return isset( $this->_data['icon'] ) ? json_decode( $this->_data['icon'], true ) : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $val
|
||||
* @return void
|
||||
*/
|
||||
public function set_icon( mixed $val ) : void
|
||||
{
|
||||
$this->_data['icon'] = is_array( $val ) ? json_encode( $val ) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the saved SVG file
|
||||
*
|
||||
* @return File|null
|
||||
*/
|
||||
public function file() : ?File
|
||||
{
|
||||
if( $this->file )
|
||||
{
|
||||
try
|
||||
{
|
||||
return File::get( 'core_CustomBadges', $this->file );
|
||||
}
|
||||
catch( OutOfRangeException ){}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @var "square"|"circle"|"ngon"|"star"|"flower"
|
||||
*/
|
||||
protected string $shape = "circle";
|
||||
|
||||
protected string $foreground = '';
|
||||
|
||||
protected string $background = '';
|
||||
|
||||
protected string $border = '';
|
||||
|
||||
protected int $rotation = 0;
|
||||
|
||||
protected string $svgCache = '';
|
||||
|
||||
protected array $_data = [];
|
||||
|
||||
/**
|
||||
|
||||
* @param string $shape
|
||||
* @param string $foreground
|
||||
* @param string $background
|
||||
* @param string $border
|
||||
* @param string|array|null $icon
|
||||
* @param int $rotation
|
||||
* @param int $sides
|
||||
* @param int $numberOverlay
|
||||
* @param string $svg The svg
|
||||
*/
|
||||
public function __construct( string $shape, string $foreground, string $background, string $border, string|array|null $icon, int $rotation=0, int $sides=5, int $numberOverlay=0, string $svg = '' )
|
||||
{
|
||||
if ( is_string( $icon ) )
|
||||
{
|
||||
try
|
||||
{
|
||||
$icon = json_decode( $icon, true );
|
||||
}
|
||||
catch ( Exception ) {}
|
||||
}
|
||||
|
||||
if ( isset( $icon[0] ) AND is_array( $icon[0] ) )
|
||||
{
|
||||
$icon = $icon[0];
|
||||
}
|
||||
|
||||
if ( !is_array( $icon ) OR !isset( $icon['raw'] ) OR !isset( $icon['type'] ) )
|
||||
{
|
||||
$icon = null;
|
||||
}
|
||||
|
||||
$this->shape = $shape;
|
||||
$this->foreground = $foreground;
|
||||
$this->background = $background;
|
||||
$this->icon = $icon;
|
||||
$this->border = $border;
|
||||
$this->rotation = $rotation % 90; // since the minimum number of points/sides is 4, any rotation greater than 90 degrees can be simplified
|
||||
$this->sides = $sides;
|
||||
$this->numberOverlay = $numberOverlay;
|
||||
$this->svgCache = $svg;
|
||||
}
|
||||
|
||||
/**
|
||||
* The SVG url of this item
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString() : string
|
||||
{
|
||||
if ( !$this->svgCache )
|
||||
{
|
||||
$this->svgCache = static::generateSVG( $this->shape, $this->foreground, $this->background, $this->border, $this->compileIconElement(), $this->rotation, $this->sides, $this->numberOverlay );
|
||||
}
|
||||
return $this->svgCache;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@@ -125,298 +99,80 @@ class CustomBadge {
|
||||
*/
|
||||
public function url() : Url
|
||||
{
|
||||
if ( isset( $this->_data['id'] ) )
|
||||
{
|
||||
return Url::createFromString( rtrim( Settings::i()->base_url, '/' ) . '/applications/core/interface/icons/icons.php?icon=' . ( $this->_data['id'] ) . '&v=' . md5( json_encode( $this->_data['raw'] ) ) );
|
||||
}
|
||||
|
||||
return new Raw( (string) $this, Raw::TYPE_SVG );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ActiveRecord $record
|
||||
* @return CustomBadge
|
||||
*/
|
||||
public static function getRecordBadge( ActiveRecord $record ) : static
|
||||
{
|
||||
$class = trim( $record::class, '\\' );
|
||||
$idCol = $record::$databaseColumnId;
|
||||
$id = $record->$idCol;
|
||||
|
||||
try
|
||||
{
|
||||
$row = Db::i()->select( '*', 'core_custom_badges', ['class=? AND active_record_id=?', $class, $id] )->first();
|
||||
$badge = new static(
|
||||
$row['shape'],
|
||||
$row['foreground'],
|
||||
$row['background'],
|
||||
$row['border'],
|
||||
$row['icon'],
|
||||
$row['rotation'],
|
||||
$row['sides'],
|
||||
$row['number_overlay'],
|
||||
$row['raw']
|
||||
);
|
||||
|
||||
$badge->_data = $row;
|
||||
return $badge;
|
||||
}
|
||||
catch ( UnderflowException ) {}
|
||||
|
||||
return new static( 'circle', '#ffffff', '#eeb95f', '#f7d36f', null, 0, 5, 0 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a badge by the id in the core_custom_badges database
|
||||
*
|
||||
* @param int $id The id of the badge
|
||||
*
|
||||
* @return static
|
||||
*
|
||||
* @throws OutOfRangeException
|
||||
*/
|
||||
public static function load( int $id ) : static
|
||||
{
|
||||
try
|
||||
{
|
||||
$row = Db::i()->select( '*', 'core_custom_badges', [ 'id=?', $id ] )->first();
|
||||
$badge = new static (
|
||||
$row['shape'],
|
||||
$row['foreground'],
|
||||
$row['background'],
|
||||
$row['border'],
|
||||
$row['icon'],
|
||||
$row['rotation'],
|
||||
$row['sides'],
|
||||
$row['number_overlay'],
|
||||
$row['raw']
|
||||
);
|
||||
|
||||
$badge->_data = $row;
|
||||
return $badge;
|
||||
}
|
||||
catch ( UnderflowException )
|
||||
{
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ActiveRecord $record
|
||||
* @param CustomBadge $badge
|
||||
* @return void
|
||||
*/
|
||||
public static function saveRecordBadge( ActiveRecord $record, CustomBadge $badge ) : void
|
||||
{
|
||||
$class = trim( $record::class, '\\' );
|
||||
$idCol = $record::$databaseColumnId;
|
||||
$id = $record->$idCol;
|
||||
$data = (string) $badge;
|
||||
|
||||
$id = Db::i()->insert(
|
||||
'core_custom_badges',
|
||||
[
|
||||
'sides' => $badge->sides,
|
||||
'foreground' => $badge->foreground,
|
||||
'background' => $badge->background,
|
||||
'border' => $badge->border,
|
||||
'shape' => $badge->shape,
|
||||
'icon' => json_encode( $badge->icon ),
|
||||
'rotation' => $badge->rotation,
|
||||
'number_overlay' => $badge->numberOverlay,
|
||||
'raw' => $data,
|
||||
'class' => $class,
|
||||
'active_record_id' => $id
|
||||
],
|
||||
true
|
||||
);
|
||||
|
||||
$cacheKey = 'ips__custom_badge_' . $id;
|
||||
Store::i()->$cacheKey = $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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 bool $defaultEnabled
|
||||
* @param array $togglesOff
|
||||
* @param string $prefix
|
||||
* @param bool $includeNumberOverlay
|
||||
* @return void
|
||||
*/
|
||||
public function addBadgeFieldsToForm( Form $form, bool $defaultEnabled = false, array $togglesOff = [], string $prefix = 'badge', bool $includeNumberOverlay = true ) : void
|
||||
{
|
||||
if ( $prefix !== 'badge' )
|
||||
{
|
||||
foreach (['border', 'foreground', 'background', 'shape', 'icon', 'sides', 'rotation', 'number_overlay', 'use_custom'] as $key)
|
||||
{
|
||||
Member::loggedIn()->language()->words["{$prefix}_{$key}"] = Member::loggedIn()->language()->get( "badge_{$key}" );
|
||||
}
|
||||
}
|
||||
|
||||
$form->class .= " ipsBadgePicker--form";
|
||||
|
||||
$form->add( new Form\YesNo(
|
||||
"{$prefix}_use_custom",
|
||||
$defaultEnabled,
|
||||
true,
|
||||
[
|
||||
'togglesOff' => $togglesOff,
|
||||
'togglesOn' => [
|
||||
"{$form->id}_header_badge_preview",
|
||||
"{$prefix}_border",
|
||||
"{$prefix}_foreground",
|
||||
"{$prefix}_background",
|
||||
"{$prefix}_shape",
|
||||
"{$prefix}_icon",
|
||||
"{$prefix}_sides",
|
||||
"{$prefix}_rotation",
|
||||
"{$prefix}_number_overlay",
|
||||
'preview'
|
||||
]
|
||||
]
|
||||
));
|
||||
|
||||
$form->addHeader( 'badge_preview' );
|
||||
|
||||
Output::i()->addJsfiles( 'admin_badgepreview.js', 'core' );
|
||||
$form->addHtml( Theme::i()->getTemplate( 'achievements', 'core', 'admin' )->badgePreview( (string) $this, $prefix ) );
|
||||
|
||||
$form->add( new Form\Radio( "{$prefix}_shape", $this->shape, true, ['options' => [
|
||||
'square' => 'badge_square',
|
||||
'circle' => 'badge_circle',
|
||||
'ngon' => 'badge_n_gon',
|
||||
'star' => 'badge_star',
|
||||
'flower' => 'badge_flower'
|
||||
], 'toggles' => [
|
||||
'ngon' => ["{$prefix}_sides", "{$prefix}_rotation"],
|
||||
'star' => ["{$prefix}_sides", "{$prefix}_rotation"],
|
||||
'flower' => ["{$prefix}_sides", "{$prefix}_rotation"]
|
||||
], 'rowClasses' => ['ipsFieldRow--badge-creator ipsFieldRow--badge-creator-shape']], null, null, null, "{$prefix}_shape") );
|
||||
|
||||
$form->add( new Form\Icon( "{$prefix}_icon", $this->icon ? [$this->icon] : null, false, ['useSvgIcon' => true, 'rowClasses' => ['ipsFieldRow--badge-creator ipsFieldRow--badge-creator-icon']], null, null, null, "{$prefix}_icon" ) );
|
||||
|
||||
$form->add( new Form\Color( "{$prefix}_background", $this->background, true, ['rowClasses' => ['ipsFieldRow--badge-creator ipsFieldRow--badge-creator-background']], null, null, null, "{$prefix}_background" ) );
|
||||
$form->add( new Form\Color( "{$prefix}_border", $this->border, true, ['rowClasses' => ['ipsFieldRow--badge-creator ipsFieldRow--badge-creator-border']], null, null, null, "{$prefix}_border" ) );
|
||||
|
||||
$form->add( new Form\Color( "{$prefix}_foreground", $this->foreground, true, ['rgba' => true, 'swatches' => true, 'rowClasses' => ['ipsFieldRow--badge-creator ipsFieldRow--badge-creator-foreground']], null, null, null, "{$prefix}_foreground" ) );
|
||||
|
||||
/* The overlay is optional */
|
||||
if ( $includeNumberOverlay )
|
||||
{
|
||||
$form->add( new Form\Number( "{$prefix}_number_overlay", $this->numberOverlay, false, [ 'step' => 1, 'min' => 1, 'max' => 999, 'unlimited' => 0, 'unlimitedLang' => 'none', 'rowClasses' => ['ipsFieldRow--badge-creator'] ], null, null, null, "{$prefix}_number_overlay" ) );
|
||||
}
|
||||
|
||||
$form->add( new Form\Number( "{$prefix}_sides", $this->sides, true, ['step' => 1, 'min' => 4, 'max' => 12, 'range' => true, 'rowClasses' => ['ipsFieldRow--badge-creator']], null, null, null, "{$prefix}_sides" ) );
|
||||
$form->add( new Form\Number( "{$prefix}_rotation", $this->rotation, true, ['step' => 1, 'min' => 0, 'max' => 90, 'range' => true, 'rowClasses' => ['ipsFieldRow--badge-creator']], null, null, null, "{$prefix}_rotation" ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the SVG of a badge from the form values; to be used on the values of a form that was passed to Badge::addBadgeFieldsToForm()
|
||||
*
|
||||
* @param array $values
|
||||
* @param string $prefix
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function generateBadgeFromFormValues( array &$values, string $prefix = 'badge' ) : static
|
||||
{
|
||||
$badge = new static(
|
||||
$values["{$prefix}_shape"],
|
||||
$values["{$prefix}_foreground"],
|
||||
$values["{$prefix}_background"],
|
||||
$values["{$prefix}_border"],
|
||||
$values["{$prefix}_icon"][0] ?? null,
|
||||
$values["{$prefix}_rotation"],
|
||||
$values["{$prefix}_sides"],
|
||||
$values["{$prefix}_number_overlay"] ?? 0
|
||||
);
|
||||
|
||||
static::clearCustomBadgeFields( $values, $prefix );
|
||||
|
||||
return $badge;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the custom badge fields from the form values
|
||||
*
|
||||
* @param array $values
|
||||
* @param string $prefix
|
||||
* @return void
|
||||
*/
|
||||
public static function clearCustomBadgeFields( array &$values, string $prefix = 'badge' ) : void
|
||||
{
|
||||
foreach ( ['shape', 'foreground', 'background', 'border', 'icon', 'rotation', 'sides', 'number_overlay', 'use_custom'] as $key )
|
||||
{
|
||||
unset( $values["{$prefix}_{$key}"] );
|
||||
}
|
||||
return Url::createFromString( rtrim( Settings::i()->base_url, '/' ) . '/applications/core/interface/icons/icons.php?icon=' . ( $this->id ) . '&v=' . md5( json_encode( $this->raw ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the SVG for a badge
|
||||
*
|
||||
* @param "square"|"ngon"|"circle"|"star"|"flower" $shape The shape of the icon
|
||||
* @param string $foreground The color of the foreground
|
||||
* @param string $background The color of the background
|
||||
* @param string $border The border color
|
||||
* @param string $icon The icon to fill inside the svg
|
||||
* @param int $rotation The rotation of the icon
|
||||
* @param int $sides The number of sides
|
||||
* @param int $numberOverlay The overlay that is shown over the icon
|
||||
* @return string
|
||||
* @param bool $preview
|
||||
* @return void
|
||||
*/
|
||||
public static function generateSVG( string $shape, string $foreground, string $background, string $border, string $icon, int $rotation=0, int $sides=5, int $numberOverlay=0 ) : string
|
||||
public function generateSVG( bool $preview=false ) : void
|
||||
{
|
||||
$foreground = $foreground ?: 'currentColor';
|
||||
$numberOverlay = static::getBadgeNumberOverlay( $numberOverlay );
|
||||
$method = 'generateSVG_' . $this->shape;
|
||||
$this->raw = $this->$method();
|
||||
|
||||
return match ( $shape )
|
||||
/* If we're just doing a preview, don't save anything */
|
||||
if( $preview )
|
||||
{
|
||||
'square' => static::generateSVG_square( $foreground, $background, $border, $icon, $numberOverlay ),
|
||||
'ngon' => static::generateSVG_ngon( $foreground, $background, $border, $icon, $rotation, $sides, $numberOverlay ),
|
||||
'circle' => static::generateSVG_circle( $foreground, $background, $border, $icon, $numberOverlay ),
|
||||
'star' => static::generateSVG_star( $foreground, $background, $border, $icon, $rotation, $sides, $numberOverlay ),
|
||||
'flower' => static::generateSVG_flower( $foreground, $background, $border, $icon, $rotation, $sides, $numberOverlay ),
|
||||
default => throw new InvalidArgumentException( 'invalid_svg_shape' ),
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
/* If we already have a file, delete it */
|
||||
if( $file = $this->file() )
|
||||
{
|
||||
$file->delete();
|
||||
}
|
||||
|
||||
$this->file = (string) File::create( 'core_CustomBadges', 'custombadge-' . $this->id . '.svg', $this->raw );
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate an icon without the background
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function generateSVG_no_shape() : string
|
||||
{
|
||||
$foreground = $this->foreground ?: 'currentColor';
|
||||
$icon = $this->compileIconElement();
|
||||
$numberOverlay = static::getBadgeNumberOverlay( $this->number_overlay );
|
||||
|
||||
return <<<SVG
|
||||
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" height="48">
|
||||
<g color="{$foreground}" data-fgcolor-placeholder="color">
|
||||
{$icon}
|
||||
{$numberOverlay}
|
||||
</g>
|
||||
</svg>
|
||||
SVG;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a square icon
|
||||
*
|
||||
* @param string $foreground
|
||||
* @param string $background
|
||||
* @param string $border
|
||||
* @param string $icon
|
||||
* @param string $numberOverlay
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function generateSVG_square( string $foreground, string $background, string $border, string $icon, string $numberOverlay='' ) : string
|
||||
protected function generateSVG_square() : string
|
||||
{
|
||||
return static::generateSVG_ngon( $foreground, $background, $border, $icon, 45, 4, $numberOverlay );
|
||||
$this->rotation = 45;
|
||||
$this->sides = 4;
|
||||
return $this->generateSVG_ngon();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate an n-gon
|
||||
*
|
||||
* @param string $foreground
|
||||
* @param string $background
|
||||
* @param string $border
|
||||
* @param string $icon
|
||||
* @param int $rotation
|
||||
* @param int $sides
|
||||
* @param string $numberOverlay
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function generateSVG_ngon( string $foreground, string $background, string $border, string $icon, int $rotation=0, int $sides=5, string $numberOverlay='' ) : string
|
||||
protected function generateSVG_ngon() : string
|
||||
{
|
||||
$points = static::getRadialPoints( $sides, ($sides === 4 and $rotation % 90 === 45) ? sqrt( 47.4*47.4*2 ) : 47.4 );
|
||||
$points = static::getRadialPoints( $this->sides, ( $this->sides === 4 and $this->rotation % 90 === 45) ? sqrt( 47.4*47.4*2 ) : 47.4 );
|
||||
$icon = $this->compileIconElement();
|
||||
$numberOverlay = static::getBadgeNumberOverlay( $this->number_overlay );
|
||||
$foreground = $this->foreground ?: 'currentColor';
|
||||
|
||||
$path = "";
|
||||
foreach ( $points as $point )
|
||||
{
|
||||
@@ -426,7 +182,7 @@ class CustomBadge {
|
||||
return <<<SVG
|
||||
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" height="48">
|
||||
<g color="{$foreground}" data-fgcolor-placeholder="color">
|
||||
<path d="{$path} Z" stroke="{$border}" stroke-linejoin="round" stroke-width="5" fill="{$background}" transform="rotate({$rotation})" transform-origin="center" data-bgcolor-placeholder="fill" data-bordercolor-placeholder="stroke" />
|
||||
<path d="{$path} Z" stroke="{$this->border}" stroke-linejoin="round" stroke-width="5" fill="{$this->background}" transform="rotate({$this->rotation})" transform-origin="center" data-bgcolor-placeholder="fill" data-bordercolor-placeholder="stroke" />
|
||||
{$icon}
|
||||
{$numberOverlay}
|
||||
</g>
|
||||
@@ -437,20 +193,18 @@ SVG;
|
||||
/**
|
||||
* Generate a circle badge icon
|
||||
*
|
||||
* @param string $foreground
|
||||
* @param string $background
|
||||
* @param string $border
|
||||
* @param string $icon
|
||||
* @param string $numberOverlay
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function generateSVG_circle( string $foreground, string $background, string $border, string $icon, string $numberOverlay='' ) : string
|
||||
protected function generateSVG_circle() : string
|
||||
{
|
||||
$icon = $this->compileIconElement();
|
||||
$numberOverlay = static::getBadgeNumberOverlay( $this->number_overlay );
|
||||
$foreground = $this->foreground ?: 'currentColor';
|
||||
|
||||
return <<<SVG
|
||||
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" height="48">
|
||||
<g color="{$foreground}" data-fgcolor-placeholder="color">
|
||||
<circle cx="50" cy="50" r="47.4" stroke="{$border}" stroke-width="5" fill="{$background}" data-bgcolor-placeholder="fill" data-bordercolor-placeholder="stroke" />
|
||||
<circle cx="50" cy="50" r="47.4" stroke="{$this->border}" stroke-width="5" fill="{$this->background}" data-bgcolor-placeholder="fill" data-bordercolor-placeholder="stroke" />
|
||||
{$icon}
|
||||
{$numberOverlay}
|
||||
</g>
|
||||
@@ -462,22 +216,17 @@ SVG;
|
||||
/**
|
||||
* Generate a star svg badge
|
||||
*
|
||||
* @param string $foreground
|
||||
* @param string $background
|
||||
* @param string $border
|
||||
* @param string $icon
|
||||
* @param int $rotation
|
||||
* @param int $sides
|
||||
* @param string $numberOverlay
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function generateSVG_star( string $foreground, string $background, string $border, string $icon, int $rotation=0, int $sides=5, string $numberOverlay='' ) : string
|
||||
protected function generateSVG_star() : string
|
||||
{
|
||||
$innerPoints = static::getRadialPoints( $sides, (0.75 + (0.095 * ( $sides - 4 ) / 8)) * ( 45 * ( cos( pi() / $sides ) ) ), 0.5 );
|
||||
$outerPoints = static::getRadialPoints( $sides, 47.4 );
|
||||
$icon = $this->compileIconElement();
|
||||
$numberOverlay = static::getBadgeNumberOverlay( $this->number_overlay );
|
||||
$foreground = $this->foreground ?: 'currentColor';
|
||||
$innerPoints = static::getRadialPoints( $this->sides, (0.75 + (0.095 * ( $this->sides - 4 ) / 8)) * ( 45 * ( cos( pi() / $this->sides ) ) ), 0.5 );
|
||||
$outerPoints = static::getRadialPoints( $this->sides, 47.4 );
|
||||
$path = "";
|
||||
for ( $i = 0; $i < $sides; $i++ )
|
||||
for ( $i = 0; $i < $this->sides; $i++ )
|
||||
{
|
||||
$path .= !$path ? 'M ' : ' L ';
|
||||
$path .= "{$outerPoints[$i][0]} {$outerPoints[$i][1]} L {$innerPoints[$i][0]} {$innerPoints[$i][1]}";
|
||||
@@ -485,7 +234,7 @@ SVG;
|
||||
return <<<SVG
|
||||
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" height="48">
|
||||
<g color="{$foreground}" data-fgcolor-placeholder="color">
|
||||
<path d="{$path} Z" stroke="{$border}" stroke-width="5" stroke-linejoin="round" fill="{$background}" transform="rotate({$rotation})" transform-origin="center" data-bgcolor-placeholder="fill" data-bordercolor-placeholder="stroke" />
|
||||
<path d="{$path} Z" stroke="{$this->border}" stroke-width="5" stroke-linejoin="round" fill="{$this->background}" transform="rotate({$this->rotation})" transform-origin="center" data-bgcolor-placeholder="fill" data-bordercolor-placeholder="stroke" />
|
||||
{$icon}
|
||||
{$numberOverlay}
|
||||
</g>
|
||||
@@ -496,32 +245,27 @@ SVG;
|
||||
/**
|
||||
* Generate a flower svg badge
|
||||
*
|
||||
*
|
||||
* @param string $foreground
|
||||
* @param string $background
|
||||
* @param string $border
|
||||
* @param string $icon
|
||||
* @param int $rotation
|
||||
* @param int $sides
|
||||
* @param string $numberOverlay
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function generateSVG_flower( string $foreground, string $background, string $border, string $icon, int $rotation=0, int $sides=5, string $numberOverlay='' ) : string
|
||||
protected function generateSVG_flower() : string
|
||||
{
|
||||
$r = 45 * ((sin(pi() / $sides)) / (sin(pi() / $sides) + 1));
|
||||
$icon = $this->compileIconElement();
|
||||
$numberOverlay = static::getBadgeNumberOverlay( $this->number_overlay );
|
||||
$foreground = $this->foreground ?: 'currentColor';
|
||||
|
||||
$r = 45 * ((sin(pi() / $this->sides)) / (sin(pi() / $this->sides) + 1));
|
||||
$innerRadius = 45 - $r;
|
||||
$innerPoints = static::getRadialPoints( $sides, $innerRadius );
|
||||
$innerPoints = static::getRadialPoints( $this->sides, $innerRadius );
|
||||
$innerPoints[] = [$innerPoints[0][0], $innerPoints[0][1]];
|
||||
|
||||
|
||||
$ro = 51 * ((sin(pi() / $sides)) / (sin(pi() / $sides) + 1));
|
||||
$ro = 51 * ((sin(pi() / $this->sides)) / (sin(pi() / $this->sides) + 1));
|
||||
$outerRadius = 51 - $ro;
|
||||
$outerPoints = static::getRadialPoints( $sides, $outerRadius );
|
||||
$outerPoints = static::getRadialPoints( $this->sides, $outerRadius );
|
||||
$outerPoints[] = [$outerPoints[0][0], $outerPoints[0][1]];
|
||||
|
||||
$path = "";
|
||||
for ( $i = 0; $i < $sides + 1; $i++ )
|
||||
for ( $i = 0; $i < $this->sides + 1; $i++ )
|
||||
{
|
||||
if ( !$path )
|
||||
{
|
||||
@@ -534,7 +278,7 @@ SVG;
|
||||
}
|
||||
|
||||
$outerPath = "";
|
||||
for ( $i = 0; $i < $sides + 1; $i++ )
|
||||
for ( $i = 0; $i < $this->sides + 1; $i++ )
|
||||
{
|
||||
if ( !$outerPath )
|
||||
{
|
||||
@@ -549,8 +293,8 @@ SVG;
|
||||
return <<<SVG
|
||||
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" height="48">
|
||||
<g color="{$foreground}" data-fgcolor-placeholder="color">
|
||||
<path d="{$outerPath} Z" stroke="{$border}" stroke-width="1" stroke-join="miter" fill="{$border}" transform="rotate({$rotation})" transform-origin="center" data-bordercolor-placeholder="fill,stroke" />
|
||||
<path d="{$path} Z" fill="{$background}" transform="rotate({$rotation})" transform-origin="center" data-bgcolor-placeholder="fill" />
|
||||
<path d="{$outerPath} Z" stroke="{$this->border}" stroke-width="1" stroke-join="miter" fill="{$this->border}" transform="rotate({$this->rotation})" transform-origin="center" data-bordercolor-placeholder="fill,stroke" />
|
||||
<path d="{$path} Z" fill="{$this->background}" transform="rotate({$this->rotation})" transform-origin="center" data-bgcolor-placeholder="fill" />
|
||||
{$icon}
|
||||
{$numberOverlay}
|
||||
</g>
|
||||
@@ -568,11 +312,18 @@ SVG;
|
||||
$iconData = $this->icon;
|
||||
$foreground = $this->foreground;
|
||||
|
||||
if ( $iconData === null )
|
||||
if ( $iconData === null or ( !isset( $iconData['type'] ) and !isset( $iconData['raw'] ) ) )
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
$sizeOffset = (($this->icon_size - 1) * 3);
|
||||
if ( $this->shape === 'no_shape' )
|
||||
{
|
||||
$sizeOffset += 13;
|
||||
}
|
||||
|
||||
|
||||
if ( $iconData['type'] === 'fa' )
|
||||
{
|
||||
$doc = new DOMDocument();
|
||||
@@ -593,20 +344,26 @@ SVG;
|
||||
break;
|
||||
}
|
||||
$escapedIcon = rawurlencode( $doc->saveXML( $root ?? null ) );
|
||||
$size = 42 + round( $sizeOffset * 2, 2 );
|
||||
$offset = (100 - $size) / 2;
|
||||
return <<<SVG
|
||||
<image href="data:image/svg+xml,{$escapedIcon}" width="42" height="42" x="29" y="29" data-embedded-fg-color="" />
|
||||
<image href="data:image/svg+xml,{$escapedIcon}" width="{$size}" height="{$size}" x="{$offset}" y="{$offset}" data-embedded-fg-color="" />
|
||||
SVG;
|
||||
}
|
||||
else
|
||||
{
|
||||
// complicated, but emojis don't center well. This was tested on MacOS using Apple Emoji font and accounts for whether the badge uses a background shape or not. Might be worth adding a note that emojis might not look correct on all devices (as the font is device-specific)
|
||||
$size = 50 + round( $sizeOffset * 2, 2 );
|
||||
$offset = 50 + round( ($this->shape === "no_shape" ? 24 : 18) + (($this->iconSize - 1) * ($this->shape === 'no_shape' ? 12.5 : 8.75) / 4), 2 );
|
||||
|
||||
return <<<SVG
|
||||
<style>
|
||||
svg text.svg__text_icon:not(#x) {
|
||||
line-height: 50px;
|
||||
line-height: {$size}px;
|
||||
vertical-align: middle;
|
||||
}
|
||||
</style>
|
||||
<text text-anchor="middle" font-size="50" x="50" y="70" class="svg__text_icon" data-number-overlay="" >{$iconData['raw']}</text>
|
||||
<text text-anchor="middle" font-size="{$size}" x="50" y="{$offset}" class="svg__text_icon">{$iconData['raw']}</text>
|
||||
SVG;
|
||||
}
|
||||
}
|
||||
@@ -620,9 +377,7 @@ SVG;
|
||||
*/
|
||||
protected static function getBadgeNumberOverlay( int $number ) : string
|
||||
{
|
||||
if ( $number )
|
||||
{
|
||||
return <<<SVG
|
||||
return <<<SVG
|
||||
<style>
|
||||
svg text.svg__text_overlay:not(#x) {
|
||||
vertical-align: middle;
|
||||
@@ -633,8 +388,6 @@ SVG;
|
||||
<circle cx="75" cy="75" r="20" fill="#334155" stroke="#fff" stroke-width="3" data-number-overlay="" />
|
||||
<text class="svg__text_overlay" x="75" y="81.5" fill="#fff" text-anchor="middle" font-size="18" font-weight="bolder" font-family="sans-serif" data-number-overlay="" >{$number}</text>
|
||||
SVG;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
@@ -669,4 +422,30 @@ SVG;
|
||||
|
||||
return $points;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save Changed Columns
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function save(): void
|
||||
{
|
||||
$this->generateSVG();
|
||||
parent::save();
|
||||
}
|
||||
|
||||
/**
|
||||
* [ActiveRecord] Delete Record
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function delete() : void
|
||||
{
|
||||
parent::delete();
|
||||
|
||||
if( $file = $this->file() )
|
||||
{
|
||||
$file->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user