1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NovaAdDirector\Nova\Flexible\Layouts; |
4
|
|
|
|
5
|
|
|
use AdDirector\Facades\AdDirector; |
6
|
|
|
use AdDirector\GPT\AdGPT; |
7
|
|
|
use AdDirector\GPT\Slot; |
8
|
|
|
use Illuminate\Validation\Rule; |
9
|
|
|
use Laravel\Nova\Fields\Text; |
10
|
|
|
use NovaAdDirector\Contracts\ConfigurationLayout; |
11
|
|
|
use NovaFlexibleContent\Concerns\HasFlexible; |
12
|
|
|
use NovaFlexibleContent\Flexible; |
13
|
|
|
use NovaFlexibleContent\Layouts\Layout; |
14
|
|
|
use NovaFlexibleContent\Layouts\Preset; |
15
|
|
|
|
16
|
|
|
class GPTConfigurationLayout extends Layout implements ConfigurationLayout |
17
|
|
|
{ |
18
|
|
|
use HasFlexible; |
19
|
|
|
|
20
|
|
|
protected string $name = 'gpt'; |
21
|
|
|
|
22
|
|
|
protected string $title = 'gpt'; |
23
|
|
|
|
24
|
|
|
public function title(): string |
25
|
|
|
{ |
26
|
|
|
return trans("nova-ad-director::resource.layouts.{$this->title}"); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function targetsPreset() |
30
|
|
|
{ |
31
|
|
|
return Preset::withLayouts([ |
32
|
|
|
GPTTarget::class, |
33
|
|
|
]); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function fields(): array |
37
|
|
|
{ |
38
|
|
|
$allowedSizeKeys = array_keys(AdGPT::getSizes()); |
39
|
|
|
|
40
|
|
|
return [ |
41
|
|
|
Text::make( |
42
|
|
|
trans("nova-ad-director::resource.configuration.{$this->name}.adUnitPath"), |
43
|
|
|
'adUnitPath' |
44
|
|
|
) |
45
|
|
|
->placeholder('/1234567/Example_Leaderboard') |
46
|
|
|
->rules('required'), |
47
|
|
|
Text::make( |
48
|
|
|
trans("nova-ad-director::resource.configuration.{$this->name}.size"), |
49
|
|
|
'size' |
50
|
|
|
) |
51
|
|
|
->placeholder($allowedSizeKeys[0] ?? trans("nova-ad-director::resource.configuration.{$this->name}.size")) |
|
|
|
|
52
|
|
|
->rules('required', Rule::in($allowedSizeKeys)) |
53
|
|
|
->help(trans("nova-ad-director::resource.configuration.{$this->name}.help_size", [ |
|
|
|
|
54
|
|
|
'sizes' => !empty($allowedSizeKeys) ? implode(', ', $allowedSizeKeys) : '-', |
55
|
|
|
])), |
56
|
|
|
Text::make( |
57
|
|
|
trans("nova-ad-director::resource.configuration.{$this->name}.divId"), |
58
|
|
|
'divId' |
59
|
|
|
) |
60
|
|
|
->placeholder('div-gpt-ad-1234567891234-0') |
61
|
|
|
->rules('nullable') |
62
|
|
|
->help(trans("nova-ad-director::resource.configuration.{$this->name}.help_divId")), |
63
|
|
|
Flexible::make('Targets', 'targets') |
64
|
|
|
->preset($this->targetsPreset()) |
65
|
|
|
->button('Add target'), |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getFlexibleTargetsAttribute() |
70
|
|
|
{ |
71
|
|
|
return $this->flexible('targets', $this->targetsPreset()->layouts()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function applyConfiguration(string $locationName): static |
75
|
|
|
{ |
76
|
|
|
$slot = Slot::make( |
77
|
|
|
$this->adUnitPath, |
78
|
|
|
$this->size, |
79
|
|
|
$this->divId ?: null |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
if ($this->flexibleTargets) { |
83
|
|
|
/** |
84
|
|
|
* @var \NovaAdDirector\Nova\Flexible\Layouts\GPTTarget $target |
85
|
|
|
*/ |
86
|
|
|
foreach ($this->targets as $target) { |
87
|
|
|
if (($key = trim($target->target_key)) |
88
|
|
|
&& ($value = trim($target->target_value))) { |
89
|
|
|
$values = array_filter(array_map('trim', explode(',', $value))); |
90
|
|
|
if (!empty($values)) { |
91
|
|
|
$slot->addTarget($key, $values); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
AdDirector::gpt()->addLocation($slot, $locationName); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|