Jump to:

3372 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Trying to build a "File Repository" Type system.

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

Page: 1
Go to End
Author Topic: 249 Views
  • omnicurse
    Avatar
    Community Member
    2 Posts

    Trying to build a "File Repository" Type system. Link to this post

    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;
    }
    }

  • Devlin
    Avatar
    Community Member
    186 Posts

    Re: Trying to build a "File Repository" Type system. Link to this post

    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.

    249 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.