89 lines
2.3 KiB
PHP
89 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class DisplayFooterContent extends Model
|
|
{
|
|
protected $fillable = [
|
|
'headline',
|
|
'subline',
|
|
'url',
|
|
'short_code',
|
|
'clicks',
|
|
'sort_order',
|
|
'is_active',
|
|
];
|
|
|
|
protected $casts = [
|
|
'is_active' => 'boolean',
|
|
'sort_order' => 'integer',
|
|
'clicks' => 'integer',
|
|
];
|
|
|
|
/**
|
|
* Boot-Methode für automatische Short-Code-Generierung
|
|
*/
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
static::creating(function ($model) {
|
|
// Short-Code nur generieren wenn URL vorhanden ist
|
|
if ($model->url && empty($model->short_code)) {
|
|
$model->short_code = self::generateUniqueShortCode();
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Generiert einen eindeutigen Short-Code
|
|
*/
|
|
public static function generateUniqueShortCode(): string
|
|
{
|
|
do {
|
|
// Generiere einen 6-stelligen alphanumerischen Code
|
|
$code = strtolower(substr(str_shuffle('abcdefghijklmnopqrstuvwxyz0123456789'), 0, 6));
|
|
} while (self::where('short_code', $code)->exists());
|
|
|
|
return $code;
|
|
}
|
|
|
|
/**
|
|
* Gibt die Short-URL zurück (dynamisch je nach Umgebung)
|
|
*/
|
|
public function getShortUrlAttribute(): string
|
|
{
|
|
$basePath = config('display.base_path', '_display-b2in-eu');
|
|
$subdomain = config('display.subdomain');
|
|
$domain = config('display.domain', 'b2in.eu');
|
|
|
|
// Wenn Subdomain konfiguriert ist, verwende diese (Live-Server)
|
|
if ($subdomain) {
|
|
// Nutze HTTPS für Produktion
|
|
$protocol = config('app.env') === 'production' ? 'https' : 'http';
|
|
return "{$protocol}://{$subdomain}.{$domain}/go.php?z={$this->short_code}";
|
|
}
|
|
|
|
// Ansonsten verwende APP_URL + Base-Path (Testserver)
|
|
$baseUrl = rtrim(config('app.url'), '/');
|
|
return "{$baseUrl}/{$basePath}/go.php?z={$this->short_code}";
|
|
}
|
|
|
|
/**
|
|
* Erhöht den Klick-Zähler
|
|
*/
|
|
public function incrementClicks(): void
|
|
{
|
|
$this->increment('clicks');
|
|
}
|
|
|
|
/**
|
|
* Scope für aktive Footer-Inhalte in sortierter Reihenfolge
|
|
*/
|
|
public function scopeActive($query)
|
|
{
|
|
return $query->where('is_active', true)->orderBy('sort_order');
|
|
}
|
|
}
|