/** Preview an advertisement.
  */
 public function preview(SS_HTTPRequest $request)
 {
     $request->shift();
     $adID = (int) $request->param('ID');
     $ad = UniadsObject::get()->byID($adID);
     if (!$ad) {
         Controller::curr()->httpError(404);
         return;
     }
     // No impression and click tracking for previews
     $conf = UniadsObject::config();
     $conf->use_js_tracking = false;
     $conf->record_impressions = false;
     $conf->record_impressions_stats = false;
     // Block stylesheets and JS that are not required (using our own template)
     Requirements::clear();
     $template = new SSViewer('UniadsPreview');
     return $template->Process($ad);
 }
 private function GetAdAndLogClick($id)
 {
     $id = (int) $id;
     if ($id) {
         $ad = UniadsObject::get()->byID($id);
         if ($ad && $ad->exists()) {
             $conf = UniadsObject::config();
             if ($conf->record_clicks) {
                 $ad->Clicks++;
                 $ad->write();
             }
             if ($conf->record_clicks_stats) {
                 $clk = new UniadsClick();
                 $clk->AdID = $ad->ID;
                 $clk->write();
             }
             return $ad;
         }
     }
     return null;
 }
 /** Displays a randomly chosen advertisement of the specified dimensions.
  *
  * @param zone of the advertisement
  */
 public function DisplayAd($zone)
 {
     $ad = null;
     if ($zone) {
         if (!is_object($zone)) {
             $zone = UniadsZone::get()->filter(array('Title' => $zone, 'Active' => 1))->first();
         }
         if ($zone) {
             $toUse = $this->owner;
             if ($toUse->InheritSettings) {
                 while ($toUse->ParentID) {
                     if (!$toUse->InheritSettings) {
                         break;
                     }
                     $toUse = $toUse->Parent();
                 }
                 if (!$toUse->ParentID && $toUse->InheritSettings) {
                     $toUse = null;
                 }
             }
             $page_related = "and not exists (select * from Page_Ads pa where pa.UniadsObjectID = UniadsObject.ID)";
             $campaign = '';
             if ($toUse) {
                 $page_related = "and (\n\t\t\t\t\t\texists (select * from Page_Ads pa where pa.UniadsObjectID = UniadsObject.ID and pa.PageID = " . $toUse->ID . ")\n\t\t\t\t\t\tor not exists (select * from Page_Ads pa where pa.UniadsObjectID = UniadsObject.ID)\n\t\t\t\t\t)";
                 if ($toUse->UseCampaignID) {
                     $campaign = "and c.ID = '" . $toUse->UseCampaignID . "'";
                 }
             }
             $base_from = "\n\t\t\t\t\tUniadsObject\n\t\t\t\t\t\tleft join UniadsCampaign c on c.ID = UniadsObject.CampaignID\n\t\t\t\t";
             $base_where = "\n\t\t\t\t\tUniadsObject.ZoneID = '" . $zone->ID . "'\n\t\t\t\t\t" . $page_related . "\n\t\t\t\t\tand (c.ID is null or (\n\t\t\t\t\t\tc.Active = '1'\n\t\t\t\t\t\tand (c.Starts <= '" . date('Y-m-d') . "' or c.Starts = '' or c.Starts is null)\n\t\t\t\t\t\tand (c.Expires >= '" . date('Y-m-d') . "' or c.Expires = '' or c.Expires is null)\n\t\t\t\t\t\t" . $campaign . "\n\t\t\t\t\t))\n\t\t\t\t\tand (UniadsObject.Starts <= '" . date('Y-m-d') . "' or UniadsObject.Starts = '' or UniadsObject.Starts is null)\n\t\t\t\t\tand (UniadsObject.Expires >= '" . date('Y-m-d') . "' or UniadsObject.Expires = '' or UniadsObject.Expires is null)\n\t\t\t\t\tand UniadsObject.Active = '1'\n\t\t\t\t";
             $subbase_where = preg_replace_callback('/(?<!\\w)(UniadsObject|c)\\./', function ($m) {
                 return str_repeat($m[1], 2) . '.';
             }, $base_where);
             $sqlQuery = new SQLQuery($select = 'UniadsObject.ID', $from = array($base_from), $where = $base_where . "\n\t\t\t\t\t\tand (UniadsObject.ImpressionLimit = 0 or UniadsObject.ImpressionLimit > UniadsObject.Impressions)\n\t\t\t\t\t\tand UniadsObject.Weight >= (rand() * (\n\t\t\t\t\t\t\tselect max(UniadsObjectUniadsObject.Weight)\n\t\t\t\t\t\t\tfrom UniadsObject as UniadsObjectUniadsObject\n\t\t\t\t\t\t\t\tleft join UniadsCampaign cc on cc.ID = UniadsObjectUniadsObject.CampaignID\n\t\t\t\t\t\t\twhere " . $subbase_where . "\n\t\t\t\t\t\t))", $order = "rand()", $limit = 1);
             singleton('UniadsObject')->extend('augmentSQL', $sqlQuery);
             //echo $sqlQuery->sql();
             $result = $sqlQuery->execute();
             if ($result && count($result) > 0) {
                 $row = $result->First();
                 if (isset($row['ID']) && $row['ID'] !== '') {
                     $ad = UniadsObject::get()->byID($row['ID']);
                     // now we can log impression
                     $conf = UniadsObject::config();
                     if ($conf->record_impressions) {
                         $ad->Impressions++;
                         $ad->write();
                     }
                     if ($conf->record_impressions_stats) {
                         $imp = new UniadsImpression();
                         $imp->AdID = $ad->ID;
                         $imp->write();
                     }
                 }
             }
         }
     }
     if (!$ad) {
         // Show an empty advert
         $ad = new UniadsObject();
     }
     $output = $ad->forTemplate();
     if ($zone) {
         foreach ($zone->ChildZones()->sort('Order') as $child) {
             if ($child->Active) {
                 $output .= $this->DisplayAd($child);
             }
         }
     }
     return $output;
 }