some fixes

This commit is contained in:
Doar Mihai
2026-03-25 20:39:10 +02:00
parent 4db544bcc2
commit b5f948433b
8 changed files with 164 additions and 10 deletions
+34 -3
View File
@@ -11,18 +11,49 @@ class StoreWebsiteRequest extends FormRequest
return true; return true;
} }
protected function prepareForValidation(): void
{
// Normalize domain: remove protocol and www, trim slashes
if ($this->has('domain')) {
$domain = $this->input('domain');
// Remove protocol (http://, https://)
$domain = preg_replace('#^https?://#i', '', $domain);
// Remove www.
$domain = preg_replace('#^www\.#i', '', $domain);
// Remove trailing slashes and paths
$domain = strtok($domain, '/');
// Trim whitespace
$domain = trim($domain);
$this->merge([
'domain' => $domain,
]);
}
}
public function rules(): array public function rules(): array
{ {
return [ return [
'name' => ['required', 'string', 'max:255'], 'name' => ['required', 'string', 'min:1', 'max:255'],
'domain' => ['required', 'string', 'max:255', 'regex:/^([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$/'], 'domain' => [
'required',
'string',
'max:255',
'regex:/^([a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/',
'unique:websites,domain,NULL,id,user_id,' . $this->user()->id,
],
]; ];
} }
public function messages(): array public function messages(): array
{ {
return [ return [
'domain.regex' => 'Please enter a valid domain (e.g. example.com).', 'domain.regex' => 'Please enter a valid domain (e.g. example.com or subdomain.example.com).',
'domain.unique' => 'You already have a website with this domain.',
]; ];
} }
} }
+36 -3
View File
@@ -11,11 +11,43 @@ class UpdateWebsiteRequest extends FormRequest
return true; return true;
} }
protected function prepareForValidation(): void
{
// Normalize domain: remove protocol and www, trim slashes
if ($this->has('domain')) {
$domain = $this->input('domain');
// Remove protocol (http://, https://)
$domain = preg_replace('#^https?://#i', '', $domain);
// Remove www.
$domain = preg_replace('#^www\.#i', '', $domain);
// Remove trailing slashes and paths
$domain = strtok($domain, '/');
// Trim whitespace
$domain = trim($domain);
$this->merge([
'domain' => $domain,
]);
}
}
public function rules(): array public function rules(): array
{ {
$websiteId = $this->route('website')->id;
return [ return [
'name' => ['required', 'string', 'max:255'], 'name' => ['required', 'string', 'min:1', 'max:255'],
'domain' => ['required', 'string', 'max:255', 'regex:/^([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$/'], 'domain' => [
'required',
'string',
'max:255',
'regex:/^([a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/',
'unique:websites,domain,' . $websiteId . ',id,user_id,' . $this->user()->id,
],
'is_active' => ['sometimes', 'boolean'], 'is_active' => ['sometimes', 'boolean'],
]; ];
} }
@@ -23,7 +55,8 @@ class UpdateWebsiteRequest extends FormRequest
public function messages(): array public function messages(): array
{ {
return [ return [
'domain.regex' => 'Please enter a valid domain (e.g. example.com).', 'domain.regex' => 'Please enter a valid domain (e.g. example.com or subdomain.example.com).',
'domain.unique' => 'You already have a website with this domain.',
]; ];
} }
} }
+24
View File
@@ -16,6 +16,7 @@ class Website extends Model
'user_id', 'user_id',
'name', 'name',
'domain', 'domain',
'slug',
'tracking_id', 'tracking_id',
'is_active', 'is_active',
'settings', 'settings',
@@ -35,9 +36,32 @@ class Website extends Model
if (empty($website->tracking_id)) { if (empty($website->tracking_id)) {
$website->tracking_id = 'TA-' . strtoupper(Str::random(10)); $website->tracking_id = 'TA-' . strtoupper(Str::random(10));
} }
// Auto-generate slug from name if not provided
if (empty($website->slug)) {
$baseSlug = Str::slug($website->name);
$slug = $baseSlug;
$counter = 1;
// Ensure slug uniqueness
while (static::where('slug', $slug)->exists()) {
$slug = $baseSlug . '-' . $counter;
$counter++;
}
$website->slug = $slug;
}
}); });
} }
/**
* Get the route key for the model.
*/
public function getRouteKeyName(): string
{
return 'slug';
}
public function user(): BelongsTo public function user(): BelongsTo
{ {
return $this->belongsTo(User::class); return $this->belongsTo(User::class);
@@ -0,0 +1,55 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('websites', function (Blueprint $table) {
$table->string('slug')->nullable()->after('domain');
$table->unique('slug');
$table->index('slug');
});
// Generate slugs for existing websites
$websites = DB::table('websites')->get();
foreach ($websites as $website) {
$baseSlug = Str::slug($website->name);
$slug = $baseSlug;
$counter = 1;
// Ensure uniqueness
while (DB::table('websites')->where('slug', $slug)->where('id', '!=', $website->id)->exists()) {
$slug = $baseSlug . '-' . $counter;
$counter++;
}
DB::table('websites')->where('id', $website->id)->update(['slug' => $slug]);
}
// Make slug non-nullable after populating
Schema::table('websites', function (Blueprint $table) {
$table->string('slug')->nullable(false)->change();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('websites', function (Blueprint $table) {
$table->dropUnique(['slug']);
$table->dropIndex(['slug']);
$table->dropColumn('slug');
});
}
};
+11 -1
View File
@@ -182,7 +182,17 @@
responsive: true, responsive: true,
maintainAspectRatio: true, maintainAspectRatio: true,
cutout: '65%', cutout: '65%',
plugins: { legend: { display: false }, tooltip: { backgroundColor: 'rgba(17,24,39,0.95)', padding: 10, cornerRadius: 8 } }, plugins: {
legend: { display: false },
tooltip: {
backgroundColor: 'rgba(17,24,39,0.95)',
padding: 10,
cornerRadius: 8,
callbacks: {
label: (ctx) => `${ctx.label}: ${ctx.parsed.toLocaleString()} sessions`
}
}
},
}, },
}); });
} }
@@ -76,8 +76,9 @@
cornerRadius: 8, cornerRadius: 8,
callbacks: { callbacks: {
label: (ctx) => { label: (ctx) => {
const countryName = ctx.raw?.feature?.properties?.name || 'Unknown';
const val = ctx.raw?.value || 0; const val = ctx.raw?.value || 0;
return val > 0 ? `${ctx.label}: ${val.toLocaleString()} visitors` : `${ctx.label}: 0`; return val > 0 ? `${countryName}: ${val.toLocaleString()} visitors` : `${countryName}: 0 visitors`;
} }
} }
} }
+1 -1
View File
@@ -21,7 +21,7 @@
<div class="mb-6"> <div class="mb-6">
<x-input-label for="domain" :value="__('Domain')" /> <x-input-label for="domain" :value="__('Domain')" />
<x-text-input id="domain" name="domain" type="text" class="mt-1 block w-full" :value="old('domain')" required placeholder="example.com" /> <x-text-input id="domain" name="domain" type="text" class="mt-1 block w-full" :value="old('domain')" required placeholder="example.com" />
<p class="mt-1 text-sm text-gray-500">{{ __('Enter the domain without http:// or https://') }}</p> <p class="mt-1 text-sm text-gray-500">{{ __('Enter your domain in any format (e.g., example.com, www.example.com, https://example.com)') }}</p>
<x-input-error class="mt-2" :messages="$errors->get('domain')" /> <x-input-error class="mt-2" :messages="$errors->get('domain')" />
</div> </div>
+1 -1
View File
@@ -22,7 +22,7 @@
<div class="mb-6"> <div class="mb-6">
<x-input-label for="domain" :value="__('Domain')" /> <x-input-label for="domain" :value="__('Domain')" />
<x-text-input id="domain" name="domain" type="text" class="mt-1 block w-full" :value="old('domain', $website->domain)" required /> <x-text-input id="domain" name="domain" type="text" class="mt-1 block w-full" :value="old('domain', $website->domain)" required />
<p class="mt-1 text-sm text-gray-500">{{ __('Enter the domain without http:// or https://') }}</p> <p class="mt-1 text-sm text-gray-500">{{ __('Enter your domain in any format (e.g., example.com, www.example.com, https://example.com)') }}</p>
<x-input-error class="mt-2" :messages="$errors->get('domain')" /> <x-input-error class="mt-2" :messages="$errors->get('domain')" />
</div> </div>