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.

Customising the CMS /

trying to upload images on pages


Reply


684 Views

Avatar
Rishi

Community Member, 97 Posts

5 February 2010 at 8:55am

Edited: 06/02/2010 1:15am

Hello
i am trying to allow upload feature from admin panel on pages and display the images on the page,the code i have tried is giving me an error in admin panel and also dont no how to restrict the number of max upload images to 3 the code is

ImageAttachment.php
<?php

class ImageAttachment extends DataObject {
static $db = array(
'Title' => 'Text'
);

static $has_one = array(
'Image' => 'Image',
'ProjectPage' => 'ProjectPage'
);

static $field_names = array(
'Image' => 'Image',
'Title' => 'Title'
);

function getCMSFields_forPopup() {

$fields = new FieldSet();

$fields->push(new TextField('Title', 'Title'));
$fields->push(new ImageField('Image', 'Image'));

return $fields;
}
}

?>

ProjectPage.php

<?php
class ProjectPage extends Page {

static $db = array(
'ProjectChallenge' => 'Text'
,'ProjectSolution' => 'Text'
,'ProjectResults' => 'Text'
,'ImageMainTitle' => 'Text'
);

static $has_one = array(
'ImageMain' => 'Image',
'ThumbnailMain' => 'Image'
);

static $has_many = array(
'ImageAttachments' => 'ImageAttachment'
);

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

$fields->addFieldToTab("Root.Content.Main", new TextAreaField('ProjectChallenge', 'The Challenge'), 'Content');
$fields->addFieldToTab("Root.Content.Main", new TextAreaField('ProjectSolution', 'The Solution'), 'Content');
$fields->addFieldToTab("Root.Content.Main", new TextAreaField('ProjectResults', 'The Results'), 'Content');
$fields->addFieldToTab('Root.Content.Main', new ImageField('ImageMain', 'Main Image'), 'Content');
$fields->addFieldToTab('Root.Content.Main', new ImageField('ThumbnailMain', 'Main Thumbnail'), 'Content');
$fields->addFieldToTab('Root.Content.Main', new TextField('ImageMainTitle', 'Main Image Title'), 'Content');

$fields->removeFieldFromTab("Root.Content.Main","Content");

$imagetable = new ComplexTableField(
$this,
'ImageAttachments', // relation name
'ImageAttachment', // object class
ImageAttachment::$field_names, // fields to show in table
ImageAttachment::getCMSFields_forPopup(), // form that pops up for edit
"ProjectPageID = {$this->ID}", // a filter to only display item associated with this page
"Name ASC" // Sort by name
);
$fields->addFieldToTab('Root.Content.ImageAttachments', $imagetable);

return $fields;
}
}

?>

ProjectPage.ss:

<% control ImageAttachments %>
$Image
$Title
<% end_control %>

when i am trying to create a page of page type Projectpage its giving be a error
thank you in advance