29/11/2011 12:30am

PHP | Problem - Extending Images Across All Pages


//The following code creates a CustomImage type which has
//a Caption and Location added to the object.
//I want to achieve a number of enhancements and
//outcomes with this code:
// 1. When "Add An Image" is clicked in the CMS a window
// (getCMSFields_forPopup) appears and an image can be
// uploaded and caption added. When the new image has
// been saved, and the popup closed I want to have the
// image automatically added to the page.
// 2. The Location value specifies where the image is
// displayed on a page. For example the Location "sidebar"
// would make the image appear in the sidebar or
// "below-article" would make the image appear below the
// article. I want to be able to specify the list of available
// locations so it relates to the Page type. For example a
// MapPage might have the locations "above-map" and
// "below-map" while NewsPage might have
// "top-right", "bottom-left" and "bottom-right". How do I
// specify the location options in the getCMSFields_forPopup
// so they can be set by each Page type?
// 3. A Page can have many CustomImages but only one
// CustomImage can be "Featured". What is the best way of
// setting up and controlling this relationship?
// 4. Currently all of the CustomImages are displayed in the
// CMS on every single page. Ideally I'd just like to display
// those CustomImages attached to the Page. If that isn't
// possible I'd like to sort the table with the attached images
// at the top.
// Any thoughts appreciated.

class CustomImagePageDecorator extends DataObjectDecorator {
function extraStatics() {
return array(
'many_many' => array(
'CustomImages' => 'CustomImage'

public function updateCMSFields(FieldSet &$fields) {

$tablefield = new HasManyComplexTableField(
'Thumbnail' => 'Thumbnail',
'Caption' => 'Caption',
'Location' => 'Location',
'Created' => 'Created',
'LastEdited' => 'LastEdited'
'LastEdited DESC'

$tablefield->setAddTitle( 'An Image' );

$fields->addFieldToTab("Root.Content.PageImages", new HeaderField("Page Images", 2));
$fields->addFieldToTab("Root.Content.PageImages", $tablefield);


class CustomImage extends DataObject {

static $db = array(
"Caption" => "Text",
"Location" => "Text"

static $has_one = array(
"ImageFile" => "Image",
"ImagePage" => "Page"

static $defaults = array(
"Location" => "default",

function getCMSFields_forPopup() {
$fields = new FieldSet();

$fields->push( new SimpleImageField( 'ImageFile', 'Image File' ) );
$fields->push( new TextField( 'Caption' ) );

return $fields;

public function getThumbnail(){
return $this->ImageFile()->SetHeight(100);