Skip to content

Commit

Permalink
Converted to PSR-2
Browse files Browse the repository at this point in the history
  • Loading branch information
helpfulrobot committed Dec 24, 2015
1 parent 64fa7d5 commit b9a51c2
Show file tree
Hide file tree
Showing 9 changed files with 665 additions and 657 deletions.
104 changes: 53 additions & 51 deletions code/controllers/UniadsAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,57 @@
* @author Marcus Nyeholt <[email protected]>
* @license BSD http://silverstripe.org/BSD-license
*/
class UniadsAdmin extends ModelAdmin {

private static $managed_models = array(
'UniadsObject',
'UniadsCampaign',
'UniadsClient',
'UniadsZone',
);

private static $allowed_actions = array(
'preview'
);

private static $url_rule = '/$ModelClass/$Action/$ID/$OtherID';

private static $url_segment = 'advrt';
private static $menu_title = 'Ads';
private static $menu_icon = '';


public function __construct() {
$this->config()->menu_icon = ADS_MODULE_DIR . '/images/icon-advrt.png';
parent::__construct();
}

/** 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);
}

class UniadsAdmin extends ModelAdmin
{

private static $managed_models = array(
'UniadsObject',
'UniadsCampaign',
'UniadsClient',
'UniadsZone',
);

private static $allowed_actions = array(
'preview'
);

private static $url_rule = '/$ModelClass/$Action/$ID/$OtherID';

private static $url_segment = 'advrt';
private static $menu_title = 'Ads';
private static $menu_icon = '';


public function __construct()
{
$this->config()->menu_icon = ADS_MODULE_DIR . '/images/icon-advrt.png';
parent::__construct();
}

/** 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);
}
}
75 changes: 39 additions & 36 deletions code/controllers/UniadsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,47 @@
* @author Marcus Nyeholt <[email protected]>
* @license BSD http://silverstripe.org/BSD-license
*/
class UniadsController extends Controller {
class UniadsController extends Controller
{

private static $allowed_actions = array(
'clk',
'go',
);
private static $allowed_actions = array(
'clk',
'go',
);

public function clk() {
$this->GetAdAndLogClick($this->request->requestVar('id'));
}
public function clk()
{
$this->GetAdAndLogClick($this->request->requestVar('id'));
}

public function go() {
$ad = $this->GetAdAndLogClick($this->request->param('ID'));
if ($ad) {
$target = $ad->getTarget();
$this->redirect($target ? $target : Director::baseURL());
}
}

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;
}
public function go()
{
$ad = $this->GetAdAndLogClick($this->request->param('ID'));
if ($ad) {
$target = $ad->getTarget();
$this->redirect($target ? $target : Director::baseURL());
}
}

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;
}
}
99 changes: 50 additions & 49 deletions code/dataobjects/UniadsCampaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,54 @@
* @author Marcus Nyeholt <[email protected]>
* @license BSD http://silverstripe.org/BSD-license
*/
class UniadsCampaign extends DataObject {

private static $db = array(
'Title' => 'Varchar',
'Starts' => 'Date',
'Expires' => 'Date',
'Active' => 'Boolean',
);

private static $summary_fields = array(
'Title' => 'Title',
'Starts' => 'Starts',
'Expires' => 'Expires',
'Active' => 'Active',
);

private static $has_many = array(
'Ads' => 'UniadsObject',
);

private static $has_one = array(
'Client' => 'UniadsClient',
);

public function getCMSFields() {
$fields = parent::getCMSFields();

$Starts = $fields->fieldByName('Root.Main.Starts');
$Starts->setConfig('showcalendar', true);
$Starts->setConfig('dateformat', i18n::get_date_format());
$Starts->setConfig('datavalueformat', 'yyyy-MM-dd');

$Expires = $fields->fieldByName('Root.Main.Expires');
$Expires->setConfig('showcalendar', true);
$Expires->setConfig('dateformat', i18n::get_date_format());
$Expires->setConfig('datavalueformat', 'yyyy-MM-dd');
$Expires->setConfig('min', date('Y-m-d', strtotime($this->Starts ? $this->Starts : '+1 days')));

$fields->changeFieldOrder(array(
'Title',
'ClientID',
'Starts',
'Expires',
'Active',
));

return $fields;
}

class UniadsCampaign extends DataObject
{

private static $db = array(
'Title' => 'Varchar',
'Starts' => 'Date',
'Expires' => 'Date',
'Active' => 'Boolean',
);

private static $summary_fields = array(
'Title' => 'Title',
'Starts' => 'Starts',
'Expires' => 'Expires',
'Active' => 'Active',
);

private static $has_many = array(
'Ads' => 'UniadsObject',
);

private static $has_one = array(
'Client' => 'UniadsClient',
);

public function getCMSFields()
{
$fields = parent::getCMSFields();

$Starts = $fields->fieldByName('Root.Main.Starts');
$Starts->setConfig('showcalendar', true);
$Starts->setConfig('dateformat', i18n::get_date_format());
$Starts->setConfig('datavalueformat', 'yyyy-MM-dd');

$Expires = $fields->fieldByName('Root.Main.Expires');
$Expires->setConfig('showcalendar', true);
$Expires->setConfig('dateformat', i18n::get_date_format());
$Expires->setConfig('datavalueformat', 'yyyy-MM-dd');
$Expires->setConfig('min', date('Y-m-d', strtotime($this->Starts ? $this->Starts : '+1 days')));

$fields->changeFieldOrder(array(
'Title',
'ClientID',
'Starts',
'Expires',
'Active',
));

return $fields;
}
}
4 changes: 2 additions & 2 deletions code/dataobjects/UniadsClick.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
* @author Marcus Nyeholt <[email protected]>
* @license BSD http://silverstripe.org/BSD-license
*/
class UniadsClick extends UniadsImpression {

class UniadsClick extends UniadsImpression
{
}
20 changes: 10 additions & 10 deletions code/dataobjects/UniadsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
* @author Marcus Nyeholt <[email protected]>
* @license BSD http://silverstripe.org/BSD-license
*/
class UniadsClient extends DataObject {
class UniadsClient extends DataObject
{

private static $db = array(
'Title' => 'Varchar(128)',
'ContactEmail' => 'Text',
);

private static $summary_fields = array(
'Title',
'ContactEmail',
);
private static $db = array(
'Title' => 'Varchar(128)',
'ContactEmail' => 'Text',
);

private static $summary_fields = array(
'Title',
'ContactEmail',
);
}
Loading

0 comments on commit b9a51c2

Please sign in to comment.