Fix views inconsistencies

This commit is contained in:
DariusIII
2026-08-09 14:37:39 +02:00
parent 73e5294132
commit f1912e5e74
117 changed files with 3587 additions and 3384 deletions
+53
View File
@@ -6,6 +6,7 @@ namespace App\Models;
use App\Enums\UserRole;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
/**
@@ -201,4 +202,56 @@ class Content extends Model
{
return $this->contenttype === self::TYPE_INDEX;
}
/**
* Resolve the content URL: external URLs are used as-is (domains without
* protocol get https://), internal paths are prefixed with the site URL.
*
* @return Attribute<string|null, never>
*/
protected function resolvedUrl(): Attribute
{
return Attribute::make(
get: function (): ?string {
$url = $this->url;
if (empty($url)) {
return null;
}
if (str_starts_with($url, 'http://') || str_starts_with($url, 'https://')) {
return $url;
}
// Domain pattern without protocol (e.g. google.com, chatgpt.ai)
if (! str_starts_with($url, '/') && preg_match('/^[a-zA-Z0-9][a-zA-Z0-9-]*\.[a-zA-Z]{2,}/', $url)) {
return 'https://'.$url;
}
return url($url);
}
);
}
/**
* Whether the content URL points to an external site.
*
* @return Attribute<bool, never>
*/
protected function isExternalUrl(): Attribute
{
return Attribute::make(
get: function (): bool {
$url = $this->url;
if (empty($url)) {
return false;
}
return str_starts_with($url, 'http://')
|| str_starts_with($url, 'https://')
|| (! str_starts_with($url, '/') && preg_match('/^[a-zA-Z0-9][a-zA-Z0-9-]*\.[a-zA-Z]{2,}/', $url));
}
);
}
}