Achso. Silverstripe 2.46 (uploadify und dataobject_manager -> letzte git repos)
mein Dataobject
<?php
class Projekt extends DataObject
{
static $db = array(
'Title' => 'Varchar(255)',
'Description' => 'HTMLText',
'Long' => 'Int',
'Lat' => 'Int',
'URLSegment' => 'Varchar(255)',
'LinkURL' => 'Varchar(255)',
'ProjectCat' => "Enum('Soziales, Strasse, Wirtschaft')",
'ProjectStatus' => "Enum('Planung, Durchfuehrung, Abgeschlossen')",
'StatusText' => 'Varchar(255)',
'DownloadItem' => 'Varchar(255)',
'MetaTitle' => 'Varchar(255)'
);
//Set our defaults
static $defaults = array(
'Title' => 'Neues Projekt',
'URLSegment' => 'neues-projekt'
);
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',
'ProjectStatus' => 'ProjectStatus',
'ProjectCat' => 'ProjectCat',
);
//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'
)
);
function getCMSFields()
{
$fields = parent::getCMSFields();
//Main Tab
$fields->addFieldToTab("Root.Main", new TextField('Title', 'Title'));
$fields->addFieldToTab("Root.Main", new TextField('MetaTitle', 'Untertitle'));
$fields->addFieldToTab("Root.Main", new TextField('URLSegment', 'URL des Projektes - Beispiel:alfred-kästner-schule - wichtig!'));
$fields->addFieldToTab("Root.Main", new HTMLEditorField('Description'));
$fields->addFieldToTab("Root.Main", new NumericField('LinkURL','URL einer zu verlinkenden Seite (TODO)'));
//Images
$fields->addFieldToTab("Root.Main", new ImageField('Image', 'Image', Null, Null, Null, 'Uploads/projekte_bilder'));
//Images
$fields->addFieldToTab("Root.Main", new NumericField('Long','Kartenbild: Bild-Pixel (X)'));
$fields->addFieldToTab("Root.Main", new NumericField('Lat','Kartenbild: Bild-Pixel (Y)'));
$statusValues = $this->dbObject('ProjectStatus')->enumValues();
$fields->addFieldToTab("Root.Categories", new DropdownField('ProjectStatus', 'Status: Stand des Projektes', $statusValues));
$fields->addFieldToTab("Root.Categories", new TextField('StatusText', 'Zusätzlicher Statustext'));
$catValues = $this->dbObject('ProjectCat')->enumValues();
$fields->addFieldToTab("Root.Categories", new DropdownField('ProjectCat', 'Projektkategorie(notwendig fur Farbgestaltung)', $catValues));
//Categories
$Categories = DataObject::get('CategoryPage');
$fields->addFieldToTab("Root.Categories", new CheckboxsetField('Categories', 'Categories', $Categories));
$fields->addFieldToTab("Root.Dateien", new TextField('DownloadItem', 'Download-Datei'));
return $fields;
}
//Set URLSegment to be unique on write
function onBeforeWrite()
{
// If there is no URLSegment set, generate one from Title
if((!$this->URLSegment || $this->URLSegment == 'neues-projekt') && $this->Title != 'Neues Projekt')
{
$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 = "projekt-$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 Projekt
function LookForExistingURLSegment($URLSegment)
{
return (DataObject::get_one('Projekt', "URLSegment = '" . $URLSegment ."' AND ID != " . $this->ID));
}
//Generate the link for this projekt
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 projekt is in
else
{
$Category = $this->Categories()->First();
}
//Check we have a category then return the link
if($Category)
{
return $Category->absoluteLink() . 'show/' . $this->URLSegment;
}
}
}
Nochmals danke. Pipifix