Source of file AdvertControllerExtension.php
Size: 4,556 Bytes - Last Modified: 2021-12-23T10:58:29+00:00
/var/www/docs.ssmods.com/process/src/code/AdvertControllerExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 | <?php class AdvertControllerExtension extends DataExtension { /* Store id to advert category to avoid multiple same queries */ private static $cachedcategories = array(); /* In order to prevent duplicate image adverts, stores those already served this request/response cycle */ private static $advertsalreadyserved = array(); public static $ctr = 1; /* * @deprecated since version 1.0.0 */ public function RenderAdvert($cachekey, $adverttype, $template = 'InlineAdvert', $numberofads = 1, $showonajax = true) { return $this->RenderRandomAdvert($cachekey, $adverttype, $template, $numberofads, $showonajax); } /* Render a fixed advert by name. This is intended for either i) An image ad that appears all the time ii) An adserver ad that may or may not rotate */ public function RenderFixedAdvert($advertTitle, $template = 'InlineAdvert', $showonajax = true) { // If we are using ajax and showonajax is set to false, return no ad if (Director::is_ajax()) { if ($showonajax !== true) { return ''; } } $html = ''; $advert = Advert::get()->filter('Title', $advertTitle)->first(); if ($advert) { $forTemplate = new ArrayData(array( 'Advert' => $advert, 'CacheKey' => $advertTitle, )); $html = $forTemplate->renderWith($template); } return $html; } /* Work out the appropriate category and render a random advert from that category @param #adverttype The type of advert, e.g. MPU or Skyscraper @param $prefix HTML prefix to the advert, e.g an li wrapper @param $suffix HTML suffix to the advert @param $numberofads - the number of adverts to search for. Normally 1 but skyscraper needs 2 @param $showonajax - set this to false to hide adverts on ajax */ public function RenderRandomAdvert($cachekey, $adverttype, $template = 'InlineAdvert', $numberofads = 1, $showonajax = true) { // If we are using ajax and showonajax is set to false, return no ad if (Director::is_ajax()) { if ($showonajax !== true) { return ''; } } // this is from the advert model extension so will be in place $advertcategoryid = $this->owner->CalculateAdvertCategoryID(); if (isset($cachedcategories[$advertcategoryid])) { $advertcategory = $cachedcategories[$advertcategoryid]; } if (!isset($advertcategory)) { $advertcategory = AdvertCategory::get()->byID($advertcategoryid); $cachedcategories[$advertcategoryid] = $advertcategory; } // check if the category is enabled, if not return a blank if (isset($advertcategory) && $advertcategory->Enabled) { $where = "AdvertLayoutType.Title = '$adverttype'"; if (isset($advertcategory)) { $where .= ' AND Advert.AdvertCategoryID = '.$advertcategoryid; $where .= ' AND (StartDate IS NULL OR !StartDate OR StartDate < NOW()) AND (FinishDate IS NULL OR !FinishDate OR NOW() < FinishDate)'; if (count(self::$advertsalreadyserved) > 0) { $csv = implode(',', array_keys(self::$advertsalreadyserved)); $where .= " and Advert.ID not in ($csv)"; } } $adverts = Advert::get()-> innerJoin('AdvertLayoutType', 'Advert.AdvertLayoutTypeID = AdvertLayoutType.ID') ->innerJoin('AdvertCategory', 'Advert.AdvertCategoryID = AdvertCategory.ID') // filter does not work here, use where instead ->where($where) ->sort('RAND()')->limit($numberofads); $firstad = null; foreach ($adverts->getIterator() as $advert) { self::$advertsalreadyserved[$advert->ID] = $advert->ID; if ($firstad === null) { $firstad = $advert; } } $forTemplate = new ArrayData(array( 'Adverts' => $adverts, 'Advert' => $firstad, 'CacheKey' => $cachekey, )); return $forTemplate->renderWith($template); } else { // return a blank if the cateogry is not enabled return ''; } } } |