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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Model admin dataobject and translated site tree


Reply


2 Posts   734 Views

Avatar
Possibles

Community Member, 16 Posts

16 March 2012 at 8:59pm

Edited: 16/03/2012 10:55pm

Hello

(I wanted to move this post from general question to dataobject manager. sorry if i did something wrong)

Based on Aram great tutorial :
http://www.ssbits.com/tutorials/2010/dataobjects-as-pages-part-2-using-model-admin-and-url-segments-to-create-a-product-catalogue/

I'm building a website for a galery.

I have a main DataObject called 'Oeuvre' every 'Oeuvre' has title, images etc..
Then I can link any of them to a ArtistePage (see screenshot ):

$Artistes = DataObject::get('ArtistePage');
$f->addFieldToTab("Root.Artistes", new CheckboxsetField('Artistes', 'Artistes', $Artistes));

It works perfectly with the main language sitetree but if I translate an ArtistPage using the site tree translation the link is broken.

Is there a way not break the link between a translated page and the data object (best) or a way to relink them manualy.

Thanks a lot

Below the full code

<?php

class Oeuvre extends DataObject
{
static $db = array(
'Titre' => 'Varchar(255)',
'Annee' => 'Text',
'TechniqueFr' => 'Text',
'TechniqueEn' => 'Text',
'Editions' => 'Text',
'DescriptionFr' => 'Text',
'DescriptionEn' => 'Text',
'URLSegment' => 'Varchar(255)',
'MetaTitle' => 'Varchar(255)'
);

//Set our defaults
static $defaults = array(
'Title' => 'New Oeuvre',
'URLSegment' => 'new-Oeuvre'
);

static $has_many = array(
'Photos' => 'Photo'
);

//Relate to the Artiste and Exposition pages
static $belongs_many_many = array(
'Artistes' => 'ArtistePage',
'Expositions' => 'ExpositionPage'
);

//Fields to show in ModelAdmin table
static $summary_fields = array(
'Titre' => 'Titre',
'Artistes' => 'Artiste',
'Thumbnail' => 'Image'
//'Photos.CMSThumbnail.Tag'=> 'Photo'
);

//Thumbnail to show in ModelAdmin table
function getThumbnail()
{
if($gallery = DataObject::get_one("Photo", "OeuvreId = {$this->ID}"))
return ($img = $gallery->Attachment()) ? $img->CroppedImage(150,150) : "no image";
return "no gallery";
}
//Add an SQL index for the URLSegment
static $indexes = array(
"URLSegment" => true
);

//Fields to search in ModelAdmin
static $searchable_fields = array (
'Titre',
'URLSegment',
'Artistes.ID' => array(
'title' => 'Artiste'
),
'Expositions.ID' => array(
'title' => 'Exposition'
)
);

public function getCMSFields()
{
$f = parent::getCMSFields();
$manager = new FileDataObjectManager(
$this, // Controller
'Photos', // Source name
'Photo', // Source class
'Attachment', // File name on DataObject
array(
'Titre' => 'Titre',
'Annee' => 'Année',
'TechniqueFr' => 'Technique Français',
'TechniqueEn' => 'Technique English',
'Editions' => 'Editions',
'DescriptionFr' => 'Description Français',
'DescriptionEn' => 'Description English',
'URLSegment' => 'URL Segment',
'MetaTitle' => 'Meta Title'
), // Headings
'getCMSFields_forPopup' // Detail fields (function name or FieldSet object)
// Filter clause
// Sort clause
// Join clause
);
$manager->copyOnImport = false;
$f->addFieldToTab("Root.Photos", $manager);
//Artistes
$Artistes = DataObject::get('ArtistePage');
$f->addFieldToTab("Root.Artistes", new CheckboxsetField('Artistes', 'Artistes', $Artistes));

//Exposition
$Expositions = DataObject::get('ExpositionPage');
$f->addFieldToTab("Root.Expositions", new CheckboxsetField('Expositions', 'Expositions', $Expositions));
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-Oeuvre') && $this->Title != 'New Oeuvre')
{
$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 = "Oeuvre-$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 Oeuvre
function LookForExistingURLSegment($URLSegment)
{
return (DataObject::get_one('Oeuvre', "URLSegment = '" . $URLSegment ."' AND ID != " . $this->ID));
}

//Generate the link for this Oeuvre
function Link()
{
//if we are on a Artiste page return that
if(Director::CurrentPage()->ClassName == 'ArtistePage')
{
$Artiste = Director::CurrentPage();
}
//Otherwise just grab the first Artiste this Oeuvre is in
else
{
$Artiste = $this->Artistes()->First();
}
//Check we have a Artiste then return the link
if($Artiste)
{
return $Artiste->absoluteLink() . 'show/' . $this->URLSegment;
}
}

}

Avatar
Possibles

Community Member, 16 Posts

19 March 2012 at 9:44pm

Hello

I'm sorry to insist but I tried to find a solution to this with no sucess

I just want to know if a at least there is a way I could look (tutotrials recipies anything)

Thanks