1 | <?php |
||
2 | |||
3 | class AdvertControllerExtension extends DataExtension |
||
0 ignored issues
–
show
|
|||
4 | { |
||
5 | /* |
||
6 | Store id to advert category to avoid multiple same queries |
||
7 | */ |
||
8 | private static $cachedcategories = array(); |
||
9 | |||
10 | /* |
||
11 | In order to prevent duplicate image adverts, stores those already served this request/response cycle |
||
12 | */ |
||
13 | private static $advertsalreadyserved = array(); |
||
14 | |||
15 | public static $ctr = 1; |
||
16 | |||
17 | /* |
||
18 | * @deprecated since version 1.0.0 |
||
19 | */ |
||
20 | public function RenderAdvert($cachekey, $adverttype, |
||
0 ignored issues
–
show
|
|||
21 | $template = 'InlineAdvert', $numberofads = 1, $showonajax = true) |
||
0 ignored issues
–
show
|
|||
22 | { |
||
0 ignored issues
–
show
|
|||
23 | return $this->RenderRandomAdvert($cachekey, $adverttype, $template, |
||
24 | $numberofads, $showonajax); |
||
25 | } |
||
26 | |||
27 | /* |
||
28 | Render a fixed advert by name. This is intended for either |
||
29 | i) An image ad that appears all the time |
||
30 | ii) An adserver ad that may or may not rotate |
||
31 | */ |
||
32 | public function RenderFixedAdvert($advertTitle, $template = 'InlineAdvert', |
||
0 ignored issues
–
show
|
|||
33 | $showonajax = true) { |
||
0 ignored issues
–
show
|
|||
34 | // If we are using ajax and showonajax is set to false, return no ad |
||
35 | if (Director::is_ajax()) { |
||
36 | if ($showonajax !== true) { |
||
37 | return ''; |
||
38 | } |
||
39 | } |
||
40 | |||
41 | $html = ''; |
||
42 | $advert = Advert::get()->filter('Title', $advertTitle)->first(); |
||
43 | if ($advert) { |
||
44 | $forTemplate = new ArrayData(array( |
||
45 | 'Advert' => $advert, |
||
46 | 'CacheKey' => $advertTitle, |
||
47 | )); |
||
48 | $html = $forTemplate->renderWith($template); |
||
49 | } |
||
50 | |||
51 | |||
52 | return $html; |
||
53 | } |
||
54 | |||
55 | /* |
||
56 | Work out the appropriate category and render a random advert from that category |
||
57 | @param #adverttype The type of advert, e.g. MPU or Skyscraper |
||
58 | @param $prefix HTML prefix to the advert, e.g an li wrapper |
||
59 | @param $suffix HTML suffix to the advert |
||
60 | @param $numberofads - the number of adverts to search for. Normally 1 but skyscraper needs 2 |
||
61 | @param $showonajax - set this to false to hide adverts on ajax |
||
62 | */ |
||
63 | public function RenderRandomAdvert($cachekey, $adverttype, |
||
0 ignored issues
–
show
|
|||
64 | $template = 'InlineAdvert', $numberofads = 1, $showonajax = true) |
||
0 ignored issues
–
show
|
|||
65 | { |
||
0 ignored issues
–
show
|
|||
66 | // If we are using ajax and showonajax is set to false, return no ad |
||
67 | if (Director::is_ajax()) { |
||
68 | if ($showonajax !== true) { |
||
69 | return ''; |
||
70 | } |
||
71 | } |
||
72 | |||
73 | // this is from the advert model extension so will be in place |
||
74 | $advertcategoryid = $this->owner->CalculateAdvertCategoryID(); |
||
75 | |||
76 | if (isset($cachedcategories[$advertcategoryid])) { |
||
77 | $advertcategory = $cachedcategories[$advertcategoryid]; |
||
78 | } |
||
79 | |||
80 | if (!isset($advertcategory)) { |
||
81 | $advertcategory = AdvertCategory::get()->byID($advertcategoryid); |
||
82 | $cachedcategories[$advertcategoryid] = $advertcategory; |
||
83 | } |
||
84 | |||
85 | // check if the category is enabled, if not return a blank |
||
86 | if (isset($advertcategory) && $advertcategory->Enabled) { |
||
87 | $where = "AdvertLayoutType.Title = '$adverttype'"; |
||
88 | |||
89 | if (isset($advertcategory)) { |
||
90 | $where .= ' AND Advert.AdvertCategoryID = '.$advertcategoryid; |
||
91 | $where .= ' AND (StartDate IS NULL OR !StartDate OR StartDate < NOW()) AND (FinishDate IS NULL OR !FinishDate OR NOW() < FinishDate)'; |
||
92 | |||
93 | if (count(self::$advertsalreadyserved) > 0) { |
||
94 | $csv = implode(',', array_keys(self::$advertsalreadyserved)); |
||
95 | $where .= " and Advert.ID not in ($csv)"; |
||
96 | } |
||
97 | } |
||
98 | |||
99 | $adverts = Advert::get()-> |
||
100 | innerJoin('AdvertLayoutType', 'Advert.AdvertLayoutTypeID = AdvertLayoutType.ID') |
||
101 | ->innerJoin('AdvertCategory', 'Advert.AdvertCategoryID = AdvertCategory.ID') |
||
102 | |||
103 | // filter does not work here, use where instead |
||
104 | ->where($where) |
||
105 | ->sort('RAND()')->limit($numberofads); |
||
106 | |||
107 | $firstad = null; |
||
108 | |||
109 | foreach ($adverts->getIterator() as $advert) { |
||
110 | self::$advertsalreadyserved[$advert->ID] = $advert->ID; |
||
111 | if ($firstad === null) { |
||
112 | $firstad = $advert; |
||
113 | } |
||
114 | } |
||
115 | |||
116 | $forTemplate = new ArrayData(array( |
||
117 | 'Adverts' => $adverts, |
||
118 | 'Advert' => $firstad, |
||
119 | 'CacheKey' => $cachekey, |
||
120 | )); |
||
121 | |||
122 | return $forTemplate->renderWith($template); |
||
123 | } else { |
||
124 | // return a blank if the cateogry is not enabled |
||
125 | return ''; |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 |
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.