New to SilverStripe, I'm trying to wrap my head around the whole data object and relationship thing, and what should probably be a pretty common feature - managing ad banners on my site. Yet I fail miserably trying to understand what's going on.
I have three fixed banner areas on my site, left and right column and a wider content banner. I've created a new tab 'Banners' to the admin page where I can name my banner, set a link and upload a picture. I've tried using ComplexTableField, HasOneComplexTableField and DataObjectManager to deal with the problem but I think I fail at understanding how the relationships between pages and data objects work.
Here's what I have so far,
Page.php
public static $has_one = array(
'BannerLeft' => 'BannerLeft',
'BannerRight' => 'BannerRight',
'BannerWide' => 'BannerWide'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$banner_left = new ComplexTableField(
$this,
'BannerLeft',
'BannerLeft',
array(
'Image' => 'Banner image',
'URL' => 'URL'
),
'getCMSFields_forPopup'
);
$banner_left->showPagination = false;
$banner_right = new ComplexTableField(
$this,
'BannerRight',
'BannerRight',
array(
'Image' => 'Banner image',
'URL' => 'URL'
),
'getCMSFields_forPopup'
);
$banner_right->showPagination = false;
$banner_wide = new ComplexTableField(
$this,
'BannerWide',
'BannerWide',
array(
'Image' => 'Banner image',
'URL' => 'URL'
),
'getCMSFields_forPopup'
);
$banner_wide->showPagination = false;
$fields->addFieldToTab('Root.Banners', $banner_left);
$fields->addFieldToTab('Root.Banners', $banner_right);
$fields->addFieldToTab('Root.Banners', $banner_wide);
return $fields;
}
For each banner data object (ie. BannerLeft.php)
class BannerLeft extends DataObject {
static $db = array(
"Name" => "Text",
"URL" => "Text"
);
static $has_one = array(
"Image" => "Image"
);
static $has_many = array(
"Page" => "Page"
);
public function getCMSFields_forPopup() {
return new FieldSet(
new TextField('Name'),
new TextField('URL'),
new FileIFrameField('Image')
);
}
}
In Page.ss I've tried to print out stuff with
<% control BannerLeft %>
<a href="$URL" title="$Name">$Image.SetSize(150,410)</a>
<% end_control %>
It produces the right kind of things in the admin views. I have a tab called 'Banners' in the top row where I can upload my banners and they seem to be shared throughout the site on every page, but unless I set the the field type to HasOneComplexTableField and click the radio button checked individually for each and every page, I don't get any visible results to front-end. Idea is to have just one banner at any given time in any of the banner areas. ComplexTableField (or DataObjectManager) should work out fine, right?
Also, would it be possible to just use a single Banner data object for all the areas, instead of having 3 clones for areas?
Any help and words of wisdom would be greatly appreciated!