nimuli/app/Livewire/Modals/UtmBuilder.php

90 lines
2.0 KiB
PHP

<?php
namespace App\Livewire\Modals;
use Illuminate\View\View;
use Livewire\Attributes\Computed;
use LivewireUI\Modal\ModalComponent;
/** @property-read string $fullUrl */
class UtmBuilder extends ModalComponent
{
public string $workspaceUlid = '';
public string $baseUrl = '';
public string $utmSource = '';
public string $utmMedium = '';
public string $utmCampaign = '';
public string $utmContent = '';
public string $utmTerm = '';
public bool $copied = false;
public function mount(string $workspaceUlid = ''): void
{
$this->workspaceUlid = $workspaceUlid;
}
#[Computed]
public function fullUrl(): string
{
if (! $this->baseUrl) {
return '';
}
$params = array_filter([
'utm_source' => $this->utmSource,
'utm_medium' => $this->utmMedium,
'utm_campaign' => $this->utmCampaign,
'utm_content' => $this->utmContent,
'utm_term' => $this->utmTerm,
]);
if (! $params) {
return $this->baseUrl;
}
$sep = str_contains($this->baseUrl, '?') ? '&' : '?';
return $this->baseUrl.$sep.http_build_query($params);
}
public function copyToClipboard(): void
{
$this->dispatch('copy-to-clipboard', text: $this->fullUrl);
$this->copied = true;
}
public function createLink(): void
{
$url = $this->fullUrl;
$wsUlid = $this->workspaceUlid;
if (! $wsUlid) {
$ws = current_workspace();
$wsUlid = $ws !== null ? $ws->ulid : '';
}
$this->closeModal();
if ($wsUlid) {
$this->dispatch('openModal', component: 'modals.create-link', arguments: [
'workspaceUlid' => $wsUlid,
'prefillUrl' => $url,
]);
}
}
public static function modalMaxWidth(): string
{
return 'lg';
}
public function render(): View
{
return view('livewire.modals.utm-builder');
}
}