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.

Data Model Questions /

Trying to build a "File Repository" Type system.


Reply


2 Posts   453 Views

Avatar
omnicurse

Community Member, 8 Posts

10 September 2013 at 2:13pm

Hi,

Pretty new to Silverstripe and I'm having a few issues getting to where I want to be. The goal is to have the CMS user upload a lot of documents, while entering information for each document as required (such as a document summary and category - Should note, I'm not looking for a bulk upload feature) and for that to then be displayed in a list with all other files that match the category.

I'm trying to create the document as a Dataobject, so the user will use the GridField to manage the displaying documents. My main issue at the moment is I can't work out how to access any of the file information from within HomePage in the CMS. Such things as the filename, file type, size, etc.

All googling and documentation I've found seems to either be with the Image class (I'm not dealing with images) or from 2009 and well out of date.

Any help on what I can do to better this would be appreciated.

class HomePage extends Page
{
static $has_many = array(
'Documents' => 'Document'
);

public function getCMSFields()
{
$fields = parent::getCMSFields();

$gridFieldConfig = GridFieldConfig_RecordEditor::create();
$gridFieldConfig->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
'Title' => 'Document Name',
'Summary' => 'Summary',
));

$resources = GridField::create('Documents', 'Resource Documents', $this->Documents(), $gridFieldConfig);
$fields->addFieldToTab("Root.Resources", $resources);
$fields->removeByName('Content');
$fields->removeByName('Metadata');
return $fields;
}

}


class Document extends DataObject
{
static $db = array(
'Title' => 'Varchar',
'Summary' => 'Text',
);
static $has_one = array(
'ResourceDocument' => 'File',
'HomePage' => 'HomePage',
);

function getCMSFields()
{
$fields = parent::getCMSFields();
$upload = new UploadField('ResourceDocument');
$upload->setConfig('allowedMaxFileNumber', 1);
$upload->allowedExtensions = array('doc', 'docx');
$upload->setCanAttachExisting(false);
$fields->addFieldToTab('Root.Main', $upload);

$upload->setFolderName('resources');

return $fields;
}
}

Avatar
Devlin

Community Member, 225 Posts

11 September 2013 at 1:47am

Edited: 11/09/2013 2:15am

I'm trying to create the document as a Dataobject

Explanation attempt: Every uploaded file is a DataObject.

My main issue at the moment is I can't work out how to access any of the file information from within HomePage in the CMS. Such things as the filename, file type, size, etc.

Hope this helps:

foreach($this->Documents() AS $document) {
   echo $document->Title; // title of the Document
   echo $document->ResourceDocumentID; // ID of the File
   $file = $document->ResourceDocument(); // fancy getter method
   echo $file->getFilename(); // filename of the File
   echo $file->getSize(); // size of the File
   echo $file->getExtension(); // suffix of the File
}


http://api.silverstripe.org/3.0/class-File.html

// edit
All googling and documentation I've found seems to either be with the Image class (I'm not dealing with images) or from 2009 and well out of date.

There weren't any major changes in the filesystem since '09 (except the new ORM layer) and the approach is still the same. The Image class is the File class plus some image specific methods.