some fixes
This commit is contained in:
@@ -11,18 +11,49 @@ class StoreWebsiteRequest extends FormRequest
|
||||
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
|
||||
{
|
||||
return [
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'domain' => ['required', 'string', 'max:255', 'regex:/^([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$/'],
|
||||
'name' => ['required', 'string', 'min:1', 'max:255'],
|
||||
'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
|
||||
{
|
||||
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.',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,11 +11,43 @@ class UpdateWebsiteRequest extends FormRequest
|
||||
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
|
||||
{
|
||||
$websiteId = $this->route('website')->id;
|
||||
|
||||
return [
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'domain' => ['required', 'string', 'max:255', 'regex:/^([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$/'],
|
||||
'name' => ['required', 'string', 'min:1', 'max:255'],
|
||||
'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'],
|
||||
];
|
||||
}
|
||||
@@ -23,7 +55,8 @@ class UpdateWebsiteRequest extends FormRequest
|
||||
public function messages(): array
|
||||
{
|
||||
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.',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ class Website extends Model
|
||||
'user_id',
|
||||
'name',
|
||||
'domain',
|
||||
'slug',
|
||||
'tracking_id',
|
||||
'is_active',
|
||||
'settings',
|
||||
@@ -35,9 +36,32 @@ class Website extends Model
|
||||
if (empty($website->tracking_id)) {
|
||||
$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
|
||||
{
|
||||
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');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -182,7 +182,17 @@
|
||||
responsive: true,
|
||||
maintainAspectRatio: true,
|
||||
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,
|
||||
callbacks: {
|
||||
label: (ctx) => {
|
||||
const countryName = ctx.raw?.feature?.properties?.name || 'Unknown';
|
||||
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`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
<div class="mb-6">
|
||||
<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" />
|
||||
<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')" />
|
||||
</div>
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
<div class="mb-6">
|
||||
<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 />
|
||||
<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')" />
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user