From 92a604f98836e163bb32d6cee3475a61ff3afae9 Mon Sep 17 00:00:00 2001 From: Remco Date: Mon, 19 Jan 2026 21:12:30 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=86=99=20More=20fixes=20=F0=9F=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Updated_Cms/app/Helpers/helper.php | 21 ++++++++++++- Updated_Cms/app/Models/Article.php | 48 +++++++++++++++++++----------- 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/Updated_Cms/app/Helpers/helper.php b/Updated_Cms/app/Helpers/helper.php index e92b991404..0674e7fd89 100644 --- a/Updated_Cms/app/Helpers/helper.php +++ b/Updated_Cms/app/Helpers/helper.php @@ -13,6 +13,25 @@ if (! function_exists('setting')) { } } +if (! function_exists('isDarkColor')) { + function isDarkColor(?string $hex): bool + { + if (! is_string($hex)) { + return false; + } + $hex = trim($hex); + if (! preg_match('/^#?[0-9a-fA-F]{6}$/', $hex)) { + return false; + } + $hex = ltrim($hex, '#'); + $r = hexdec(substr($hex, 0, 2)); + $g = hexdec(substr($hex, 2, 2)); + $b = hexdec(substr($hex, 4, 2)); + $luminance = (0.2126 * $r + 0.7152 * $g + 0.0722 * $b) / 255; + return $luminance < 0.5; + } +} + if (! function_exists('hasPermission')) { function hasPermission(string $permission): string { @@ -94,4 +113,4 @@ if (! function_exists('dropForeignKeyIfExists')) { } } } -} \ No newline at end of file +} diff --git a/Updated_Cms/app/Models/Article.php b/Updated_Cms/app/Models/Article.php index bc5b89ef40..c6a4930f90 100644 --- a/Updated_Cms/app/Models/Article.php +++ b/Updated_Cms/app/Models/Article.php @@ -6,12 +6,15 @@ use App\Models\Articles\WebsiteArticleComment as ArticleComment; use App\Models\Articles\WebsiteArticleReaction as ArticleReaction; use App\Models\Articles\Tag; use App\Models\Compositions\HasNotificationUrl; +use App\Models\User; use Auth; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\MorphToMany; use Str; class Article extends Model @@ -31,25 +34,21 @@ class Article extends Model static::creating(function (Article $article): void { $article->user_id = Auth::id(); $article->slug = Str::slug($article->title); - $article->predominant_color = getPredominantImageColor($article->image); }); static::updating(function (Article $article): void { $article->slug = Str::slug($article->title); - - if ($article->isDirty('image')) { - $article->predominant_color = getPredominantImageColor($article->image); - } }); } public function syncPaginatedComments(): void { - $this->setRelation('comments', - $this->comments()->defaultRelationships()->paginate(10)->fragment('comments'), - ); + $this->setRelation('comments', $this->comments()->paginate(10)->fragment('comments')); } + /** + * @return Builder
+ */ public static function fromIdAndSlug(string $id, string $slug, bool $withDefaultRelationships = true): Builder { return Article::valid() @@ -74,6 +73,9 @@ class Article extends Model return $article; } + /** + * @return Builder
+ */ public static function forIndex(int $limit): Builder { return Article::valid() @@ -84,38 +86,50 @@ class Article extends Model } #[\Illuminate\Database\Eloquent\Attributes\Scope] - protected function valid(Builder $query): void + protected function valid(Builder $query): Builder { - $query->whereVisible(true); + return $query->whereVisible(true); } #[\Illuminate\Database\Eloquent\Attributes\Scope] - protected function defaultRelationships(Builder $query): void + protected function defaultRelationships(Builder $query): Builder { - $query->with([ + return $query->with([ 'user:id,username,look,gender', 'tags', - 'reactions' => fn ($query) => $query->defaultRelationships(), + 'reactions', 'user.followers', ]); } + /** + * @return HasMany + */ public function comments(): HasMany { - return $this->hasMany(ArticleComment::class)->defaultBehavior(); + return $this->hasMany(ArticleComment::class); } + /** + * @return HasMany + */ public function reactions(): HasMany { - return $this->hasMany(ArticleReaction::class)->defaultBehavior(); + return $this->hasMany(ArticleReaction::class); } - public function user() + /** + * @return BelongsTo + */ + public function user(): BelongsTo { return $this->belongsTo(User::class); } - public function tags(): \Illuminate\Database\Eloquent\Relations\MorphToMany + /** + * @return MorphToMany + */ + public function tags(): MorphToMany { return $this->morphToMany(Tag::class, 'taggable'); }