1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Google DFP Slot Helper class |
5
|
|
|
* |
6
|
|
|
* |
7
|
|
|
* @package SilverStripe-Google-DFP |
8
|
|
|
* @copyright 2017 Fractas Labs |
9
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause |
10
|
|
|
* @link https://github.com/fractaslabs/silverstripe-google-dfp |
11
|
|
|
*/ |
12
|
|
|
class GoogleDfpSlotHelper extends Object |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
private static $id; |
|
|
|
|
15
|
|
|
|
16
|
|
|
private static $enable_in_dev = false; |
|
|
|
|
17
|
|
|
|
18
|
|
|
public function GoogleDfpID() |
19
|
|
|
{ |
20
|
|
|
return Config::inst()->get('GoogleDfpSlotHelper', 'publisher_id'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public static function enable_in_dev() |
24
|
|
|
{ |
25
|
|
|
return Config::inst()->get('GoogleDfpSlotHelper', 'enable_in_dev'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Check if ads should be enabled |
30
|
|
|
* By default they are enabled in "live" enviroment |
31
|
|
|
* |
32
|
|
|
* @return boolean |
33
|
|
|
*/ |
34
|
|
|
public static function enabled() |
35
|
|
|
{ |
36
|
|
|
if (Director::isLive()) { |
37
|
|
|
return true; |
38
|
|
|
} elseif (Director::isDev() && self::enable_in_dev()) { |
39
|
|
|
return true; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return false; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* |
47
|
|
|
* ID - unique identifier of banner |
48
|
|
|
* Layout - Controller ClassName where banner should be rendered |
49
|
|
|
* Alias - name of banner (used to distinguish ) |
50
|
|
|
* AdUnitPath - Full path of the ad unit with the network code and ad unit code. |
51
|
|
|
* Size - An exact size of banner creative |
52
|
|
|
* OutOfPage - if banner is "wallpaper" or "floater" type specify as "true" |
53
|
|
|
* DfpTargetPage - current Page ID |
54
|
|
|
* DfpTargetCategory - Parent Title of current Page ID |
55
|
|
|
* DfpTargetCategoryParent - GrandParent Title of current Page ID (if has) |
56
|
|
|
* |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
|
|
public static function slot_list() |
60
|
|
|
{ |
61
|
|
|
$list = Config::inst()->get('GoogleDfpSlotHelper', 'layouts'); |
62
|
|
|
if (!is_array($list)) { |
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$out = ArrayList::create(); |
67
|
|
|
$controller = Controller::curr(); |
68
|
|
|
$dfpTargetPage = $controller->ID; |
69
|
|
|
$dfpTargetCategory = $controller->Parent()->Title ? $controller->Parent()->Title : $controller->Title; |
|
|
|
|
70
|
|
|
$dfpTargetCategoryParent = $controller->Parent()->Parent()->Title ? $controller->Parent()->Parent()->Title : null; |
|
|
|
|
71
|
|
|
|
72
|
|
|
foreach ($list as $key => $item) { |
73
|
|
|
foreach ($item as $slotKey => $slot) { |
74
|
|
|
$out->add(ArrayData::create(array( |
75
|
|
|
'ID' => $slotKey, |
76
|
|
|
'Layout' => $key, |
77
|
|
|
'Alias' => isset($slot['alias']) ? $slot['alias'] : null, |
78
|
|
|
'AdUnitPath' => isset($slot['adUnitPath']) ? $slot['adUnitPath'] : null, |
79
|
|
|
'Size' => isset($slot['size']) ? $slot['size'] : null, |
80
|
|
|
'OutOfPage' => isset($slot['outOfPage']) ? $slot['outOfPage'] : false, |
81
|
|
|
'DfpTargetPage' => $dfpTargetPage, |
82
|
|
|
'DfpTargetCategory' => $dfpTargetCategory, |
83
|
|
|
'DfpTargetCategoryParent' => $dfpTargetCategoryParent, |
84
|
|
|
'DfpTargetRos' => $key, |
85
|
|
|
))); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $out; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get one banner by ID |
94
|
|
|
* |
95
|
|
|
* |
96
|
|
|
* @param $id |
97
|
|
|
*/ |
98
|
|
|
public static function slot_by_id($id = false) |
99
|
|
|
{ |
100
|
|
|
return self::slot_list() |
101
|
|
|
->filter(array('ID' => $id, 'Layout' => Controller::curr()->ClassName)) |
102
|
|
|
->first(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get one banner by Alias name |
107
|
|
|
* |
108
|
|
|
* |
109
|
|
|
* @param $alias |
110
|
|
|
*/ |
111
|
|
|
public static function slot_by_alias($alias = false) |
112
|
|
|
{ |
113
|
|
|
return self::slot_list() |
114
|
|
|
->filter(array('Alias' => $alias, 'Layout' => Controller::curr()->ClassName)) |
115
|
|
|
->first(); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.