Hi Everyone:
I am following the tutorial on Aram's ssbits.org page.
I am trying to tweak it so that I can add a ImageDataObjectManager tab to each specific project. I have been fairly successful, but the issue is that when i upload the graphics in the tab and save them, they do the following:
1. sometimes dissapear (maybe a folder permissions issue?)
2. IMPORTANT: the images i saved in the previous project appear in any new project I create after that. IS there a away I can set this up so that the images stay specific to their project? Everything else seems to work fine but this. Any help would be greatly appreciated!
Project.php
<?php
class Project extends DataObject
{
static $db = array(
'Title' => 'Varchar(255)',
'Description' => 'HTMLText',
'URLSegment' => 'Varchar(255)',
'MetaTitle' => 'Varchar(255)'
);
//Set our defaults
static $defaults = array(
'Title' => 'New Project',
'URLSegment' => 'new-Project'
);
static $has_one = array(
'Image' => 'Image'
);
//Relate to the category pages
static $belongs_many_many = array(
'Categories' => 'CategoryPage'
);
//Fields to show in ModelAdmin table
static $summary_fields = array(
'Title' => 'Title',
'URLSegment' => 'URLSegment',
);
//Add an SQL index for the URLSegment
static $indexes = array(
"URLSegment" => true
);
//Fields to search in ModelAdmin
static $searchable_fields = array (
'Title',
'URLSegment',
'Description',
'Categories.ID' => array(
'title' => 'Category'
)
);
public function getCMSFields()
{
$f = parent::getCMSFields();
$manager = new ImageDataObjectManager(
$this, // Controller
'Images', // Source name
'ProjectImage', // Source class
'ProjectImageAttachment', // File name on DataObject
array(
'Title' => 'Title',
'Caption' => 'Caption'
), // Headings
'getCMSFields_forPopup' // Detail fields
// Filter clause
// Sort clause
// Join clause
);
$manager->setUploadFolder('ProjectImages');
$f->addFieldToTab("Root.AdditionalPhotos",$manager);
$f->addFieldToTab("Root.Main", new TextField('Title', 'Title'));
$f->addFieldToTab("Root.Main", new TextField('URLSegment', 'URL Segment'));
$f->addFieldToTab("Root.Main", new TextField('MetaTitle', 'Meta Title'));
$f->addFieldToTab("Root.Main", new HTMLEditorField('Description'));
//Categories
$Categories = DataObject::get('CategoryPage');
$f->addFieldToTab("Root.Categories", new CheckboxsetField('Categories', 'Categories', $Categories));
//Images
$f->addFieldToTab("Root.CalloutImage", new ImageField('Image', 'Image', Null, Null, Null, 'Uploads/category_callout'));
return $f;
}
//Set URLSegment to be unique on write
function onBeforeWrite()
{
// If there is no URLSegment set, generate one from Title
if((!$this->URLSegment || $this->URLSegment == 'new-Project') && $this->Title != 'New Project')
{
$this->URLSegment = SiteTree::generateURLSegment($this->Title);
}
else if($this->isChanged('URLSegment'))
{
// Make sure the URLSegment is valid for use in a URL
$segment = preg_replace('/[^A-Za-z0-9]+/','-',$this->URLSegment);
$segment = preg_replace('/-+/','-',$segment);
// If after sanitising there is no URLSegment, give it a reasonable default
if(!$segment) {
$segment = "Project-$this->ID";
}
$this->URLSegment = $segment;
}
// Ensure that this object has a non-conflicting URLSegment value.
$count = 2;
while($this->LookForExistingURLSegment($this->URLSegment))
{
$this->URLSegment = preg_replace('/-[0-9]+$/', null, $this->URLSegment) . '-' . $count;
$count++;
}
parent::onBeforeWrite();
}
//Test whether the URLSegment exists already on another Project
function LookForExistingURLSegment($URLSegment)
{
return (DataObject::get_one('Project', "URLSegment = '" . $URLSegment ."' AND ID != " . $this->ID));
}
//Generate the link for this Project
function Link()
{
//if we are on a category page return that
if(Director::CurrentPage()->ClassName == 'CategoryPage')
{
$Category = Director::CurrentPage();
}
//Otherwise just grab the first category this Project is in
else
{
$Category = $this->Categories()->First();
}
//Check we have a category then return the link
if($Category)
{
return $Category->absoluteLink() . 'show/' . $this->URLSegment;
}
}
}
catagorypage.php
<?php
class CategoryPage extends Page
{
static $has_one = array(
'CategoryBanner' => 'Image'
);
static $many_many = array(
'Projects' => 'Project'
);
static $allowed_children = array(
'none' => 'none'
);
function getCMSFields()
{
$fields = parent::getCMSFields();
//Banner Images
$fields->addFieldToTab("Root.Content.Banner", new ImageField('CategoryBanner', 'Banner', Null, Null, Null, 'Uploads/category_banners'));
return $fields;
}
}
class CategoryPage_Controller extends Page_Controller
{
static $allowed_actions = array(
'show'
);
public function init()
{
parent::init();
Requirements::css('Projects/css/Projects.css');
}
//Return the list of Projects for this category
public function getProjectsList()
{
return $this->Projects(Null, 'Title ASC');
}
//Get's the current Project from the URL, if any
public function getCurrentProject()
{
$Params = $this->getURLParams();
$URLSegment = Convert::raw2sql($Params['ID']);
if($URLSegment && $Project = DataObject::get_one('Project', "URLSegment = '" . $URLSegment . "'"))
{
return $Project;
}
}
//Shows the Project detail page
function show()
{
//Get the Project
if($Project = $this->getCurrentProject())
{
$Data = array(
'Project' => $Project,
'MetaTitle' => $Project->MetaTitle
);
//return our $Data array to use, rendering with the ProjectPage.ss template
return $this->customise($Data)->renderWith(array('ProjectPage', 'Page'));
}
else //Project not found
{
return $this->httpError(404, 'Sorry that Project could not be found');
}
}
//Generate out custom breadcrumbs
public function Breadcrumbs() {
//Get the default breadcrumbs
$Breadcrumbs = parent::Breadcrumbs();
if($Project = $this->getCurrentProject())
{
//Explode them into their individual parts
$Parts = explode(SiteTree::$breadcrumbs_delimiter, $Breadcrumbs);
//Count the parts
$NumOfParts = count($Parts);
//Change the last item to a link instead of just text
$Parts[$NumOfParts-1] = ('<a href="' . $this->Link() . '">' . $Parts[$NumOfParts-1] . '</a>');
//Add our extra piece on the end
$Parts[$NumOfParts] = $Project->Title;
//Return the imploded array
$Breadcrumbs = implode(SiteTree::$breadcrumbs_delimiter, $Parts);
}
return $Breadcrumbs;
}
}
projectimage.php
<?php
class ProjectImage extends DataObject
{
static $db = array (
'Title' => 'Text',
'Caption' => 'Text'
);
static $has_one = array (
'ProjectImageAttachment' => 'Image', //Needs to be an image
'Project' => 'Project'
);
public function getCMSFields_forPopup()
{
return new FieldSet(
new TextField('Title'),
new TextareaField('Caption'),
new FileIFrameField('ProjectImageAttachment')
);
}
}