|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AdDirector\GPT; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\View; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
|
|
8
|
|
|
class Presenter |
|
9
|
|
|
{ |
|
10
|
|
|
public bool $importVendorScripts = true; |
|
11
|
|
|
|
|
12
|
|
|
public static string $jsMapPrefix = 'gptmap_'; |
|
13
|
|
|
|
|
14
|
|
|
public static string $jsSlotVariableName = 'gptSlot'; |
|
15
|
|
|
|
|
16
|
|
|
protected array $locationHtml = []; |
|
17
|
|
|
|
|
18
|
|
|
protected string $scriptSizeMaps = ''; |
|
19
|
|
|
|
|
20
|
|
|
protected string $scriptSlots = ''; |
|
21
|
|
|
|
|
22
|
|
|
protected string $scriptDisplayAds = ''; |
|
23
|
|
|
|
|
24
|
5 |
|
public function __construct(AdGPT $adManager) |
|
25
|
|
|
{ |
|
26
|
5 |
|
$this->importVendorScripts = $adManager->isImportVendorScripts(); |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string $sizeName |
|
30
|
|
|
* @var \AdDirector\GPT\Size $size |
|
31
|
|
|
*/ |
|
32
|
5 |
|
foreach ($adManager::getSizes() as $sizeName => $size) { |
|
33
|
5 |
|
if ($map = $size->map()) { |
|
34
|
5 |
|
$this->addSizeMapToScript($sizeName, $map); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string $locationName |
|
40
|
|
|
* @var \AdDirector\GPT\Slot $slot |
|
41
|
|
|
*/ |
|
42
|
5 |
|
foreach ($adManager->locations() as $locationName => $slot) { |
|
43
|
4 |
|
$this->locationHtml[$locationName] = View::make('ad-director::gpt.location-html', [ |
|
44
|
4 |
|
'locationName' => $locationName, |
|
45
|
4 |
|
'locationId' => $slot->divId(), |
|
46
|
4 |
|
])->render(); |
|
47
|
|
|
|
|
48
|
4 |
|
if (($size = $slot->size()) instanceof Size |
|
49
|
4 |
|
&& ($map = $size->map())) { |
|
50
|
1 |
|
$this->addSizeMapToScript(static::prepareSizeMapVariableName($slot->divId()), $map); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
4 |
|
$this->addSlotToScript($slot); |
|
54
|
4 |
|
$this->addDisplayToScript($slot); |
|
55
|
|
|
} |
|
56
|
5 |
|
} |
|
57
|
|
|
|
|
58
|
5 |
|
public function adLocation(string $name): string |
|
59
|
|
|
{ |
|
60
|
5 |
|
return $this->locationHtml[$name] ?? ''; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
5 |
|
protected function addSizeMapToScript(string $mapName, SizeMap $map): static |
|
64
|
|
|
{ |
|
65
|
5 |
|
$prefix = static::$jsMapPrefix; |
|
66
|
5 |
|
$this->scriptSizeMaps .= View::make('ad-director::gpt.define-size-map', [ |
|
67
|
5 |
|
'variableName' => "{$prefix}{$mapName}", |
|
68
|
5 |
|
'map' => $map->map(), |
|
69
|
5 |
|
])->render(); |
|
70
|
|
|
|
|
71
|
5 |
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
4 |
|
protected function addSlotToScript(Slot $slot): static |
|
75
|
|
|
{ |
|
76
|
4 |
|
$sizeName = static::prepareSizeMapVariableName($slot->divId()); |
|
77
|
4 |
|
$size = $slot->size(); |
|
78
|
4 |
|
if (is_string($size)) { |
|
79
|
3 |
|
$sizeName = $size; |
|
80
|
3 |
|
$size = AdGPT::getSize($sizeName); |
|
81
|
|
|
} |
|
82
|
4 |
|
if (!$size) { |
|
83
|
1 |
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
3 |
|
$this->scriptSlots .= View::make('ad-director::gpt.define-slot', [ |
|
87
|
3 |
|
'jsSlotVariableName' => static::$jsSlotVariableName, |
|
88
|
3 |
|
'mapVariableName' => $size->map()?(static::$jsMapPrefix.$sizeName):null, |
|
89
|
3 |
|
'adUnitPath' => $slot->adUnitPath(), |
|
90
|
3 |
|
'size' => json_encode($size->size()), |
|
91
|
3 |
|
'divId' => $slot->divId(), |
|
92
|
3 |
|
'targets' => $slot->targets(), |
|
93
|
3 |
|
])->render(); |
|
94
|
|
|
|
|
95
|
3 |
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
4 |
|
protected function addDisplayToScript(Slot $slot): static |
|
99
|
|
|
{ |
|
100
|
|
|
// language=JavaScript |
|
101
|
4 |
|
$script = "googletag.display('{$slot->divId()}');".PHP_EOL; |
|
102
|
|
|
|
|
103
|
4 |
|
$this->scriptDisplayAds .= $script; |
|
104
|
|
|
|
|
105
|
4 |
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
4 |
|
public function defineSizesMapsScript(): string |
|
109
|
|
|
{ |
|
110
|
4 |
|
return $this->scriptSizeMaps; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
4 |
|
public function defineSlotsScript(): string |
|
114
|
|
|
{ |
|
115
|
4 |
|
return $this->scriptSlots; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
4 |
|
public function displayAdsScript(): string |
|
119
|
|
|
{ |
|
120
|
4 |
|
return $this->scriptDisplayAds; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
4 |
|
public static function prepareSizeMapVariableName(string $name): string |
|
124
|
|
|
{ |
|
125
|
4 |
|
return Str::camel($name); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|