Skip to content

Commit

Permalink
Abstracted Photo Gallery Features to an Extension
Browse files Browse the repository at this point in the history
  • Loading branch information
purplespider committed Sep 24, 2015
1 parent 13e192c commit 63ecdda
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 46 deletions.
5 changes: 4 additions & 1 deletion _config/galleries.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
LeftAndMain:
extra_requirements_css:
- basic-galleries/css/cms.css
- basic-galleries/css/cms.css
PhotoGalleryPage:
extensions:
- PhotoGalleryExtension
47 changes: 47 additions & 0 deletions code/PhotoGalleryExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

class PhotoGalleryExtension extends DataExtension {

// One gallery page has many gallery images
private static $has_many = array(
'PhotoGalleryImages' => 'PhotoGalleryImage'
);

public function updateCMSFields(FieldList $fields) {

$gridFieldConfig = GridFieldConfig_RecordEditor::create();
$gridFieldConfig->addComponent(new GridFieldBulkUpload());
$gridFieldConfig->addComponent(new GridFieldGalleryTheme('Image'));
$bulkUpload = $gridFieldConfig->getComponentByType('GridFieldBulkUpload');
$bulkUpload->setUfSetup('setFolderName', "Managed/PhotoGalleries/".$this->owner->ID."-".$this->owner->URLSegment);
$bulkUpload->setUfConfig('canAttachExisting',false);
$bulkUpload->setUfConfig('canPreviewFolder',false);

$gridFieldConfig->removeComponentsByType('GridFieldPaginator');
$gridFieldConfig->addComponent(new GridFieldSortableRows('SortOrder'));
$gridFieldConfig->addComponent(new GridFieldPaginator(100));
$gridFieldConfig->removeComponentsByType('GridFieldAddNewButton');

$gridfield = new GridField("PhotoGalleryImages", "Image Gallery", $this->owner->PhotoGalleryImages()->sort("SortOrder"), $gridFieldConfig);
$fields->addFieldToTab('Root.ImageGallery', $gridfield);

$fields->addFieldToTab('Root.ImageGallery', new LiteralField('help',"
<h2>To upload new images:</h2>
<ol>
<li>1. Click the <strong>From your computer</strong> button above.</li>
<li>2. <strong>Locate and select</strong> the image(s) you wish to upload.</li>
<li>3. Click on <strong>Open/Choose</strong> and the image(s) will begin to upload.</li>
<li>4. Click <strong>Finish</strong>.</li>
</ol>"));

$fields->renameField("Content", "Top Content");

return $fields;

}

public function GetGalleryImages() {
return $this->owner->PhotoGalleryImages()->sort("SortOrder");
}

}
2 changes: 1 addition & 1 deletion code/PhotoGalleryImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PhotoGalleryImage extends DataObject {

private static $has_one = array(
'Image' => 'Image',
'PhotoGalleryPage' => 'PhotoGalleryPage'
'PhotoGalleryPage' => 'Page'
);

private static $summary_fields = array(
Expand Down
55 changes: 11 additions & 44 deletions code/PhotoGalleryPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,25 @@ class PhotoGalleryPage extends Page {
private static $description = "A page including a photo gallery and the ability to bulk upload images";
private static $icon = 'basic-galleries/images/gallery';
private static $singular_name = "Photo Gallery";

// One gallery page has many gallery images
private static $has_many = array(
'PhotoGalleryImages' => 'PhotoGalleryImage'
);


private static $defaults = array(
'ShowInMenus' => false
);

public function getCMSFields() {

$fields = parent::getCMSFields();

// Makes Image Gallery Tab Default, if images exist
if ($this->PhotoGalleryImages()->exists()) {
$fields->insertBefore(new Tab('ImageGallery'), 'Main');
}

$gridFieldConfig = GridFieldConfig_RecordEditor::create();
$gridFieldConfig->addComponent(new GridFieldBulkUpload());
$gridFieldConfig->addComponent(new GridFieldGalleryTheme('Image'));
$bulkUpload = $gridFieldConfig->getComponentByType('GridFieldBulkUpload');
$bulkUpload->setUfSetup('setFolderName', "Managed/PhotoGalleries/".$this->ID."-".$this->URLSegment);
$bulkUpload->setUfConfig('canAttachExisting',false);
$bulkUpload->setUfConfig('canPreviewFolder',false);

$gridFieldConfig->removeComponentsByType('GridFieldPaginator');
$gridFieldConfig->addComponent(new GridFieldSortableRows('SortOrder'));
$gridFieldConfig->addComponent(new GridFieldPaginator(100));
$gridFieldConfig->removeComponentsByType('GridFieldAddNewButton');

$gridfield = new GridField("PhotoGalleryImages", "Image Gallery", $this->PhotoGalleryImages()->sort("SortOrder"), $gridFieldConfig);
$fields->addFieldToTab('Root.ImageGallery', $gridfield);
$this->beforeUpdateCMSFields(function($fields) {
// Makes Image Gallery Tab Default, if images exist
if ($this->PhotoGalleryImages()->exists()) {
$fields->insertBefore(new Tab('ImageGallery'), 'Main');
}
});

$fields->addFieldToTab('Root.ImageGallery', new LiteralField('help',"
<h2>To upload new images:</h2>
<ol>
<li>1. Click the <strong>From your computer</strong> button above.</li>
<li>2. <strong>Locate and select</strong> the image(s) you wish to upload.</li>
<li>3. Click on <strong>Open/Choose</strong> and the image(s) will begin to upload.</li>
<li>4. Click <strong>Finish</strong>.</li>
</ol>"));

$fields->renameField("Content", "Top Content");

$fields = parent::getCMSFields();

return $fields;



}

function onBeforeWrite() {
Expand All @@ -68,9 +38,6 @@ function onBeforeWrite() {
}

class PhotoGalleryPage_Controller extends Page_Controller {

public function GetGalleryImages() {
return $this->PhotoGalleryImages()->sort("SortOrder");
}


}

0 comments on commit 63ecdda

Please sign in to comment.