14/08/2012 1:22pm

PHP | Problem - SGallery


<?php


Class SGallery extends Page {

static $singular_name = 'SGallery';
static $plural_name = 'SGalleries';
static $icon = '';
static $db = array(
'GalleryDesc' => "HTMLText",
'AlbumFolderID' => 'Int',
'setPlugin' => 'Text',
'ItemsPerPage' => 'Int(2)',
'ThumbnailImageWidth' => 'Int(4)',
'ThumbnailImageHeight' => 'Int(4)',
'FullImageWidth' => 'Int(4)',
'FullImageHeight' => 'Int(4)',
'ResizeMethod' => "Enum('CroppedImage,SetWidth,Setheight','CroppedImage')"
);
static $has_one = array(
'Folder' => 'Folder',
'CoverImage' => 'Image'
);
public static $has_many = array(
'Images' => 'SGalleryImage'
);
public static $defaults = array(
'ItemsperPage' => '10',
'ThumbnailImageWidth' => '100',
'ThumbnailImageHeight' => '100',
'FullImageWidth' => '100',
'FullImageHeight' => '100'
);

//*
function getCMSFields() {
$fields = parent::getCMSFields();
/*
* Add extra basic fields to the CMS + Extra Tab
* $newFieldsArray = list of fields need to be added to tab
* $newFieldsArray MUST be used to add array of fields as fields need to be declared before they can be added to a tab !
*/

// Settings Tab
$newFieldsArray = array(
new UploadField("CoverImage"),
new TextareaField("GalleryDesc"),
new TextField("setPlugin"),
// new DropdownField('ResizeMethod', 'ResizeMethod', singleton($this)->dbObject('ResizeMethod')->enumValues()),
new TextField('ItemsPerPage'),
new TextField('ThumbnailImageWidth'),
new TextField('ThumbnailImageHeight'),
new TextField('FullImageWidth'),
new TextField('FullImageHeight')
);


/*
* Add Gallery Section to the page using has_many relation + GridFeild
*/
$gridFieldConfig = GridFieldConfig_RelationEditor::create();
$gridFieldConfig->addComponents(
new GridFieldSortableRows("SortID")
);
$gridField = new GridField("Images", "SGalleryImage", $this->Images(), $gridFieldConfig);




/* Customize the List Display in the CMS
*
$gridField->getConfig()->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(

));
*
*/

/* Auto list folders in SGallery/plugins/ */
/*
$album = Folder::get()->filter('Filename', 'assets/plugins/')->first();;
// var_dump($album);
// Debug::dump($album);die;

if ($album) {
$tree = new TreeDropdownField(
'AlbumFolderID',
_t(
'SGallery.CHOOSEALBUMFOLDER', 'Choose album folder (subfolder assets/plugins/)'
),
'Folder'
);
$tree->setTreeBaseID((int) $album->ID);
$fields->addFieldToTab("Root.Main", $tree, 'Content');
}
*
*/


/* Add Fields to tabs */
$fields->addFieldsToTab("Root.Settings", $newFieldsArray, "");
$fields->addFieldToTab("Root.GalleryImages", $gridField);


return $fields;
}

/*
* Attempt to create custom folder for each SGallerypage
*
function findOrMakeFolder() {
$Title = SiteTree::generateURLSegment($this->Title);
if ($this->Folder() && $this->Folder()->exists()) {
$folder = $this->Folder();
} else {
$folder = Folder::findOrMake($Title);
$folder->write();
}
$folder->Name =$Title;
$folder->Title = $Title;
$this->FolderID = $folder->ID;
$folder->write();
}
*
*/

function onBeforeWrite() {
parent::onBeforeWrite();
//$this->findOrMakeFolder();
}

function onBeforeDelete() {
parent::onBeforeDelete();
}

public static $default_values = array(
);

}

class SGallery_Controller extends Page_Controller {

function init() {
parent::init();
}
function getImagesByCategory(){

}
function RenderGallery($templatename="Default") {


// checks if setPlugin is changed in the CMS
if (isset($this->setPlugin)) {
$templatename = $this->setPlugin;
}
$renderWithArray = array(
"SGallery-" . $templatename
);

if (isset($templatename)) {
return $this->renderWith($renderWithArray);
}
}

public function Pagination() {
// return new PaginatedList($this->data()->Images(), $this->data()->Images()-ID);
$PaginatedList = new PaginatedList($this->data()->Images(), $this->request);
$PaginatedList->setPagelength($this->data()->ItemsPerPage);
return $PaginatedList;
// return $this->data()->Images();
}

public static function test() {
$newSortId = $this->Images()->Last()->SortID;
// return debug::dump($newSortId);
return $this->Images()->Last()->getnextSortID();
}

}



Post Comment