Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions /

Simple site-wide banner management .. Help, please?


Reply


8 Posts   1944 Views

Avatar
Imploosio

Community Member, 5 Posts

4 June 2011 at 4:02am

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!

Avatar
Willr

Forum Moderator, 5513 Posts

6 June 2011 at 3:39pm

You would be better adding the banner fields and complex table field on a single page (such as your HomePage if you have one) then in your template you would get the banner from the homepage. This would save you from having the relationship on every. single. page.

<% control Page(home) %>
<% control BannerLeft %>
<a href="$URL" title="$Name">$Image.SetSize(150,410)</a>
<% end_control %>
<% end_control %>

Avatar
Imploosio

Community Member, 5 Posts

6 June 2011 at 11:19pm

Edited: 06/06/2011 11:20pm

Oh, awesome. For some reason ComplexTableField returns nothing to templates, but HasOneComplexTableField works fine once I've 'checked' the radio buttons once. Anyhow, now that all the banners are in the same place and I don't have to manage them per page this is certainly something I can deal with. Thanks a lot Will!

Avatar
martimiz

Forum Moderator, 1132 Posts

9 June 2011 at 4:30am

Woudn't that be something for SiteConfig, where more sitewide things live?

Avatar
Willr

Forum Moderator, 5513 Posts

9 June 2011 at 4:15pm

martimiz - yes SiteConfig would be good for that but quite a few people have noted issues with ComplexTableFields and SiteConfig. I'm sure it would be able to be setup, just haven't attempted it myself.

Avatar
martimiz

Forum Moderator, 1132 Posts

11 June 2011 at 12:44am

Willr - I see, I spoke too soon... It seems the CTF doesn't acknowledge SiteConfig's ID. There's a discussion here that mostly concerns the DOM:
http://silverstripe.org/dataobjectmanager-module-forum/show/12048

Here you need to (tested, looks like it works):
- make sure the (banner)object has a 'has_one' = array('SiteConfig' => 'SiteConfig');
- in the SiteConfig decorator create a field like: $banners = new DataObjectManager($this->owner, ...
- add a custom sourceID: $banners->setSourceID($this->owner->ID);

A patch like this for the CTF would be nice. I think there's something about that in the thread as well...

Avatar
Willr

Forum Moderator, 5513 Posts

11 June 2011 at 12:16pm

Thanks good to know those steps. Perhaps the documentation needed to be appending with that / a tutorial on ssbits would be good that people could reference. Since CTF is deprecated for 3.0 a patch for CTF may not make it into the post 2.4 releases.

Avatar
Plato

Community Member, 26 Posts

1 March 2012 at 1:43pm

Edited: 01/03/2012 2:06pm

The issue lies with CTF relying on the parent form to have a field named ID.
The setSource functions available on a vanilla CTF did not work for me, and as such I found the solution lies in pushing a hidden field on to the siteconfig through it's decorator.

$fields->push(new HiddenField('ID', '', $this->owner->ID));

http://sspaste.com/paste/show/4f4ebfa33f39a

Setting the ID to the owner->ID was a previous fix I'd discovered, however it only worked for ImageDOM's.
However: neither of these work-arounds have been tested with Translatable / subsites.

Bumping old thread for benefit of future searchers. Enjoy.