Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Image upload from with Thumbnail Image.

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 939 Views
  • rog
    Avatar
    Community Member
    8 Posts

    Image upload from with Thumbnail Image. Link to this post

    Hi,

    I'm trying to build a form to allow users to attach a file to page. This more of less works but I can't get it to create the thumbnail image afterwards.

    Can someone please tell why?

    ArticlePage.php

    class ArticlePage extends Page {
       static $icon = "themes/tutorial/images/treeicons/news";
       static $defaults = array(
       'ProvideComments' => true
       );

       static $db = array(
          'Date' => 'Date',
          'Author' => 'Text'
       );
       static $has_one = array(
       );
       function getCMSFields() {
          $fields = parent::getCMSFields();
          $fields->addFieldToTab('Root.Content.Main', $dateField = new DateField('Date','Article Date (for example: 20/12/2010)'), 'Content');
          $dateField->setConfig('showcalendar', true);
          $dateField->setConfig('dateformat', 'dd/MM/YYYY');
          $fields->addFieldToTab('Root.Content.Main', new TextField('Author','Author Name'), 'Content');
          return $fields;
       }
    }

    class ArticlePage_Controller extends Page_Controller {

       public function ApplyForm() {
          $form = new Form (
             $this,
             "ApplyForm",
             new FieldSet (
                new TextField('ViewTitle', 'Title'),
                new TextareaField('ViewDescription', 'Description'),
                new HiddenField('ArticleTitle', _t('FileAttach.ARTICLETITLE'), $this->Title ),
                new HiddenField('ArticleID', _t('FileAttach.ARTICLEID'), $this->ID ),
                $file = new FileUploadField('Attachment', _t('FileAttach.ATTACHMENT','Upload a File'))
             ),
             new FieldSet (
             new FormAction('doApply', _t('FileAttach.ATTACHMENT','Add File'))
             ),
             new RequiredFields('ViewTitle')
          );
          $file->setFileTypes(array(
             'pdf',
             'jpg'
          ));
          return $form;
       }

       public function doApply($data, $form) {
          $form->saveInto($application = new FileAttach());
          $application->write();
          return array (
          'Application' => $application
       );
       }

       function GetFiles() {
          $id = $this->ID;
          $obj         = "FileAttach";
          $filter       = "ArticleID = $id";
          $sort         = "";
          $join         = "LEFT JOIN File ON (File.ID = FileAttach.AttachmentID)";
          $limit         = "";
          $data = DataObject::get($obj, $filter, $sort, $join, $limit);
          return $data;
       }

    }

    ArticlePage.ss

    <% include Menu2 %>
    <div id="Content" class="typography">
    <% include Breadcrumbs %>

    <h1>$Title</h1>
       $Content

    <div class="newsDetails">
    PAGE ID $ID Posted on $Date.Nice by $Author
    $ApplyForm
    <div>
    <% control GetFiles %>
          <div>$ID $ViewTitle $ViewDescription</div>
          $Attachment.CMSThumbnail
    <% end_control %>
    </div>
    </div>
    </div>

    FileAttach.php

    class FileAttach extends DataObject {

       static $db = array (
          'ViewTitle' => 'Text',
          'ViewDescription' => 'Text',
          'ArticleTitle' => 'Text',
          'ArticleID' => 'Varchar',
       );

       static $has_one = array (
          'Attachment' => 'File'
       );

       public function getCMSFields_forPopup() {
        return new FieldSet(
        new TextField('ViewTitle', 'Title'),
        new TextareaField('ViewDescription', 'Description'),
        new ImageField('File', 'File')

        );
       }

       public function getCMSFields() {
          $f = parent::getCMSFields();
          $manager = new ImageDataObjectManager(
          $this, // Controller
          'Attachment', // Source name
          'File', // Source class
          'ApplyForm', // File name on DataObject
          array(
          'Title' => 'Title',
          'Caption' => 'Caption'
          ), // Headings
          'getCMSFields_forPopup' // Detail fields
          // Filter clause
          // Sort clause
          // Join clause
          );
          $f->addFieldToTab("Root.Content.Gallery",$manager);
          return $f;
       }
    }

  • UncleCheese
    Avatar
    4085 Posts

    Re: Image upload from with Thumbnail Image. Link to this post

    The reason you're not getting thumbnails is because you've casted the file relation as "File" rather than "Image", so you're not getting any of the functions contained in the Image class.

    But there are some bigger problems going on with your datamodel. Your GetFiles() function is needlessly complex. Get rid of that all together.

    Add a has_many to your ArticlePage:

    static $has_many = array (
    'FileAttachments' => 'FileAttach'
    );

    And in your FileAttach class, you've put a foreign key in your DB array. That's no good. Use the ORM!!

    FileAttach.php

    $has_one = array (
    'Attachment' => 'Image',
    'Article' => 'ArticlePage'
    );

    Then on your template, you just need to do:

    <% control FileAttachments %>

    <% end_control %>

    Life is so much easier when you use the tools SS gives you!

    ---------------
    Silverstripe tips, tutorials, screencasts, and more. http://www.leftandmain.com

    939 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.