Skip to content

Instantly share code, notes, and snippets.

@SpiritLevel
Last active October 8, 2015 02:47
Show Gist options
  • Save SpiritLevel/82dd4522f3a2c9689e60 to your computer and use it in GitHub Desktop.
Save SpiritLevel/82dd4522f3a2c9689e60 to your computer and use it in GitHub Desktop.
Gallery with photos stored in folder determined by Address
<?php
class GalleryPhoto extends DataObject {
private static $db = array (
'Caption' => 'Varchar'
);
private static $has_one = array (
'Photo' => 'Image',
'ProjectPage' => 'ProjectPage'
);
private static $summary_fields = array (
'GridThumbnail' => 'Photo Thumbnail',
'Caption' => 'Photo Caption',
'Photo.Filename' => 'Photo Filename'
);
public function getGridThumbnail() {
if($this->Photo()->exists()) {
return $this->Photo()->CroppedImage(50,50);
}
return '(no photo)';
}
public function getCMSFields() {
$fields = FieldList::create(
TextField::create('Caption'),
$photo = UploadField::create('Photo')
);
$photo->setFolderName($this->ProjectPage()->getField('Address'));
$photo->getValidator()->setAllowedExtensions(array('png','jpg','jpeg','gif'));
return $fields;
}
}
?>
<?php
class ProjectPage extends Page {
private static $db = array(
'Start' => 'Date',
'End' => 'Date',
'Address' => 'Varchar(100)',
'Description' => 'Text',
'Status' => "Enum('Future, Current, Recent, Past','Future')"
);
private static $has_one = array (
'Photo' => 'Image'
);
private static $has_many = array (
'Gallery' => 'GalleryPhoto'
);
private static $can_be_root = false;
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->removeFieldFromTab('Root.Main','Content');
$fields->removeFieldFromTab('Root.Main','MenuTitle');
$fields->removeFieldFromTab('Root.Main','Metadata');
$fields->addFieldToTab('Root.Main', TextField::create('Title','Project Title'),'URLSegment');
$fields->addFieldToTab('Root.Main', DateField::create('Start','Project Start Date')->setConfig('showcalendar',true),'');
$fields->addFieldToTab('Root.Main', DateField::create('End','Project End Date')->setConfig('showcalendar',true),'');
$fields->addFieldToTab('Root.Main', TextField::create('Address','Project Address'),'');
$fields->addFieldToTab('Root.Main', TextareaField::create('Description','Project Description'),'');
$fields->addFieldToTab('Root.Main', DropdownField::create('Status','Project Status', singleton('ProjectPage')->dbObject('Status')->enumValues()),'');
$fields->addFieldToTab('Root.Main', $photo = UploadField::create('Photo','Project Photo'),'');
$photo->getValidator()->setAllowedExtensions(array('png','jpg','jpeg','gif'));
$photo->setFolderName($this->getField('Address'));
$fields->addFieldToTab('Root.Main', GridField::create(
'Gallery',
'Project Gallery',
$this->Gallery(),
GridFieldConfig_RecordEditor::create()
));
return $fields;
}
}
class ProjectPage_Controller extends Page_Controller {
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment