Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Orm rewrite #3

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions code/dataobjects/UniadsObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class UniadsObject extends DataObject {
'InternalPage' => 'Page',
);

private static $has_many = array(
'ImpressionDetails' => 'UniadsImpression'
);

private static $belongs_many_many = array(
'AdInPages' => 'Page',
);
Expand Down Expand Up @@ -203,4 +207,22 @@ public function getContent() {
return $this->AdContent;
}

/**
* Increases the impression counter if 'record_impressions' setting is true
* Creates a new UniadsImpression entry in DB if 'record_impressions_stats' is true
* @return UniadsObject
*/
public function increaseImpressions(){
$ad = clone($this);
if ($this->stat('record_impressions')) {
$ad->Impressions++;
$ad->write();
}
if ($this->stat('record_impressions_stats')) {
$imp = new UniadsImpression;
$imp->AdID = $ad->ID;
$imp->write();
}
return $ad;
}
}
18 changes: 18 additions & 0 deletions code/dataobjects/UniadsZone.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,22 @@ public function getCMSFields() {
return $fields;
}


/**
* @param string $title
* @return UniadsZone|null
*/
public static function getActiveZoneByTitle($title)
{
$zone = UniadsZone::get()
->filter(
array(
'Title' => $title,
'Active' => 1
)
)
->first();
return $zone;
}

}
254 changes: 161 additions & 93 deletions code/extensions/UniadsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ class UniadsExtension extends DataExtension {
'UseCampaign' => 'UniadsCampaign',
);

private static $filter_double_ads = true;

/**
* @var array cache for ads already shown on this page
*/
protected static $shown_ad_ids = array();

private function getListboxOptions($o) {
$list = new DataList($o);
return array('' => '') + $list->map()->toArray();
Expand All @@ -48,119 +55,180 @@ public function updateCMSFields(FieldList $fields) {
* @param zone of the advertisement
*/
public function DisplayAd($zone) {
$ad = null;

$output = '';
if ($zone) {
if (!is_object($zone)) {
$zone = UniadsZone::get()
->filter(array(
'Title' => $zone,
'Active' => 1
))
->first();
$zone = UniadsZone::getActiveZoneByTitle($zone);
}
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;
}
$adList = $this->getAdListForDisplaying($zone);
foreach ($adList as $ad) {
$output .= $ad->forTemplate();
self::$shown_ad_ids[] = $ad->ID;
}
}
}
return $output;
}

$page_related = "and not exists (select * from Page_Ads pa where pa.UniadsObjectID = UniadsObject.ID)";
$campaign = '';
if ($toUse) {
$page_related = "and (
exists (select * from Page_Ads pa where pa.UniadsObjectID = UniadsObject.ID and pa.PageID = ".$toUse->ID.")
or not exists (select * from Page_Ads pa where pa.UniadsObjectID = UniadsObject.ID)
)";
if ($toUse->UseCampaignID) {
$campaign = "and c.ID = '" . $toUse->UseCampaignID . "'";
}
/**
* Gets the ad for the current zone and all subzones
* @param UniadsZone $zone
* @retunr ArrayList with all ads
*/
public function getAdListForDisplaying(UniadsZone $zone){
$adList = ArrayList::create();

$ad = $this->getRandomAdByZone($zone);

if($ad) {
$ad = $ad->increaseImpressions();
}

if (!$ad) {
// Show an empty advert
$ad = new UniadsObject();
}

$adList->add($ad);

foreach ($zone->ChildZones()->sort('Order') as $child) {
if ($child->Active) {
$adList->merge($this->getAdListForDisplaying($child));
}
}

return $adList;

}

/**
* Scans over the owning page and all parent pages until it finds the one with the settings for displaying ads
* @return null|Page
*/
public function getPageWithSettingsForAds()
{
$settingsPage = $this->owner;
if ($settingsPage->InheritSettings) {
while ($settingsPage->ParentID) {
if (!$settingsPage->InheritSettings) {
break;
}
$settingsPage = $settingsPage->Parent();
}
if (!$settingsPage->ParentID && $settingsPage->InheritSettings) {
$settingsPage = null;
return $settingsPage;
}
return $settingsPage;
}
return $settingsPage;
}

$base_from = "
UniadsObject
left join UniadsCampaign c on c.ID = UniadsObject.CampaignID
";
$base_where = "
UniadsObject.ZoneID = '" . $zone->ID . "'
".$page_related."
and (c.ID is null or (
/**
* @param $zone
* @return DataList
*/
public function getBasicFilteredAdListByZone(UniadsZone $zone)
{
$adsSettingsPage = $this->getPageWithSettingsForAds();

$UniadsObject = UniadsObject::get()->filter(
array(
'ZoneID' => $zone->ID,
'Active' => 1
)
);



//page specific ads, use only them
if ($adsSettingsPage) {
$UniadsObject = $UniadsObject->where(
"(
exists (select * from Page_Ads pa where pa.UniadsObjectID = UniadsObject.ID and pa.PageID = " . $adsSettingsPage->ID . ")
or not exists (select * from Page_Ads pa where pa.UniadsObjectID = UniadsObject.ID)
)"
);
if ($adsSettingsPage->UseCampaignID) {
$UniadsObject = $UniadsObject->addFilter(array('CampaignID' => $adsSettingsPage->UseCampaignID));
}
} else {
//filter for Ads not exclusively associated with a page
//how to convert this to ORM filter?
$UniadsObject = $UniadsObject->where(
'not exists (select * from Page_Ads pa where pa.UniadsObjectID = UniadsObject.ID)'
);
}

$UniadsObject = $UniadsObject->leftJoin('UniadsCampaign', 'c.ID = UniadsObject.CampaignID', 'c');

//current ads and campaigns
$campaignFilter = "(c.ID is null or (
c.Active = '1'
and (c.Starts <= '" . date('Y-m-d') . "' or c.Starts = '' or c.Starts is null)
and (c.Expires >= '" . date('Y-m-d') . "' or c.Expires = '' or c.Expires is null)
".$campaign."
))
and (UniadsObject.Starts <= '" . date('Y-m-d') . "' or UniadsObject.Starts = '' or UniadsObject.Starts is null)
and (UniadsObject.Expires >= '" . date('Y-m-d') . "' or UniadsObject.Expires = '' or UniadsObject.Expires is null)
and UniadsObject.Active = '1'
";
$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 . "
and (UniadsObject.ImpressionLimit = 0 or UniadsObject.ImpressionLimit > UniadsObject.Impressions)
and UniadsObject.Weight >= (rand() * (
select max(UniadsObjectUniadsObject.Weight)
from UniadsObject as UniadsObjectUniadsObject
left join UniadsCampaign cc on cc.ID = UniadsObjectUniadsObject.CampaignID
where " . $subbase_where . "
))",
$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();
}
$UniadsObject = $UniadsObject->where($campaignFilter);
$sql = $UniadsObject->sql();
return $UniadsObject;
}

$output = $ad->forTemplate();
/**
* returns a DataList with all possible Ads in this zone.
* respects ImpressionLimit
*
* @param UniadsZone $zone
* @return DataList
*/
public function getAdsByZone(UniadsZone $zone){
$adList = $this->getBasicFilteredAdListByZone($zone)
->where('(UniadsObject.ImpressionLimit = 0 or UniadsObject.ImpressionLimit > UniadsObject.Impressions)');

if ($zone) {
foreach ($zone->ChildZones()->sort('Order') as $child) {
if ($child->Active) {
$output .= $this->DisplayAd($child);
}
}
return $adList;
}

/**
* @param UniadsZone $zone
* @todo: filter out already displayed ads or campaigns
* @return UniadsObject
*/
public function getRandomAdByZone(UniadsZone $zone)
{
$weight = rand(0, $this->getMaxWeightByZone($zone));

$randomString = DB::getConn()->random(); //e.g. rand() for mysql, random() for Sqlite3

$ad =$this->getAdsByZone($zone)
->filter(array('Weight:GreaterThanOrEqual' => $weight))
->sort($randomString);

if (Config::inst()->get('UniadsExtension', 'filter_double_ads')) {
$ad = $ad->exclude(
array('ID' => self::$shown_ad_ids)
);
}

return $output;
$this->owner->extend('UpdateRandomAdByZone', $ad);

return $ad->First();
}


/**
* @param UniadsZone $zone
* @return string
*/
public function getMaxWeightByZone(UniadsZone $zone){
$UniadsObject = $this->getBasicFilteredAdListByZone($zone);
$weight = $UniadsObject->max('Weight');

return $weight;
}


}
Loading