Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » HasManyFileManager: New CMS module/extension. Testers/review needed

Our old forums are still available as a read-only archive.

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

Page: 1 2 3 4 5 6 7 8
Go to End
Author Topic: 103788 Views
  • banal
    Avatar
    Community Member
    901 Posts

    HasManyFileManager: New CMS module/extension. Testers/review needed Link to this post

    Hello Forum

    I'm currently working on two websites, where i'm in need for a FormField that allows me to attach multiple Images/Files to a SiteTree instance. The last time i encountered this problem, i solved it like described in this post: http://www.silverstripe.com/site-builders-forum/flat/5853 (creating a folder for every page and put files in there).
    However, this solution doesn't satisfy my needs and lacks a possibility to easily sort the items attached to a Page. Therefore i decided to write my own custom FormField for my needs.

    I think the module (or CMS extension) has now reached a stage where i can show it to you and let you comment and test it.
    Since this is my first extension to the SilverStripe CMS and i'm not yet quite familiar with all the concepts and possibilities of the framework, the code may seem rather "hackish" here and there
    That's why i'm posting this here. To get as many feedback and testers as possible!

    Please note: This is a beta release at best. Use it in a productive environment at your own risk!

    Installation
    - Download the filemanager.zip
    - Unzip to <your_site_directory>/filemanager
    - Run /db/build?flush=1

    That should be all. When you enter the CMS, you'll have a new page type named "File Page". Create a new File Page. You should have two additional tabs named "Files" and "Images" in the CMS. Now you can add Files to these tabs. These items then "belong" to this Page.
    Items can be sorted using Drag'n'Drop.

    Have a look at the code/FilePage.php File to see an example of how to add the HasManyFileManager FormField to your own Pages.

    This module has been tested in Safari3 (mac), Firefox3 (mac), IE7 (win), IE6 (win). In IE6 there's a bug when dragging items (weird offset).

    And here's a screenshot how this looks in the CMS.

    Any feedback is welcome. Thanks for reading.

    Edit: fixed formatting...
    Edit: Ouch. File attachement didn't seem to work.
    Edit: Fixed some bugs. New sources to be found here: filemanager_0.2.zip

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: HasManyFileManager: New CMS module/extension. Testers/review needed Link to this post

    This looks awesome.

    It would be even better if you could add this onto any type of page type, instead of just a FilePage, so you could pretty much use it like a standard FormField in the CMS or even a form on the front end (possibly a lot more work to achieve this?). Alternatively even attaching it onto 'Page' using the DataObjectDecorator by default could be an option? This would mean you'd get the added benefit of being able to attach multiple images to already existing standard pages.

    It would mean you'd need to move those methods you've currently made on FilePage over to HasManyFileManager, e.g. "deleteAttached" and "Files" - or, it's possible they could go on the decorator class instead if they don't fit on HasManyFileManager.

    Nice work on it though, you appear to have touched on the more advanced concepts of SilverStripe, like the DataObjectDecorator, as seen with AttachedFile, so it's nice to see a solid implementation of it!

    Sean

  • banal
    Avatar
    Community Member
    901 Posts

    Re: HasManyFileManager: New CMS module/extension. Testers/review needed Link to this post

    Hi Sean

    Thanks for the Reply.
    You're right, i'll modify the FormField to make it work on any Type of Page inside the CMS. That was actually my intention
    Is it possible to have a onBeforeDelete method on a FormField, and will it be called when the parent SiteTree element is being deleted? Or how would you implement said methods?

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: HasManyFileManager: New CMS module/extension. Testers/review needed Link to this post

    Very good point. I don't know enough about the DataObjectDecorator to understand if that you call delete() on it's owner class it knows about/calls onBeforeDelete() on the decorator class. I guess you'd also need to call $this->owner instead of $this on the decorator if that works.

    And as for the FormField class, I don't believe it's possible to implement a check for deletion of it's SiteTree parent, because it doesn't have the responsibility to do so.

    Perhaps you could try adding onBeforeDelete() to your decorator, and see if it's called when you delete an instance of the decorated class?

  • banal
    Avatar
    Community Member
    901 Posts

    Re: HasManyFileManager: New CMS module/extension. Testers/review needed Link to this post

    Allright. Thanks!
    I'll look into it and post here what i come up with.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: HasManyFileManager: New CMS module/extension. Testers/review needed Link to this post

    I considered the input from Sean and modified my code accordingly. Some Bugs have been eliminated too.

    Get the latest sources: filemanager_0.3.zip

    Sadly, a onBeforeDelete Method on a Decorator doesn't apply to or overrides a existing implementation in the decorated class. That's why i now created a new Decorator for the SiteTree Object which adds 2 methods to any SiteTree instance. These two methods are:
    AttachedFiles A method you can call inside your Templates. Let's assume you have a HasManyFileManager FormField in your Page Class and you "named" it "MyImages". Now you could add the following Code to the Page.ss template to output files attached to the MyImages HasManyFileManager.

    <% control AttachedFiles(MyImages) %>
    ... item output. for example $CroppedImage(100,100)
    <% end_control %>

    deleteAttachedFiles Delete all files attached to this SiteTree instance. You are encouraged to add a call to this Method inside your Pages onBeforeDelete method.
    Example (in Page.php)

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

       /** @see SiteTreeFileHandler.php */
       $this->deleteAttachedFiles();
    }

    These changes make it quite simple to add the HasManyFileManager to your own Page Classes. Here's a very basic example for a custom Page that has a ManyFileManager. Let's call it ImagePage.php

    <?php
    class ImagePage extends Page
    {
       static $has_many = array(
          'Images' => 'Image'
       );

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

          $manager = new HasManyFileManager(
             $this,
             'MyImages', // name -> will be used for file grouping
             'Images' // relation name defined in $has_many
          );

          $fields->addFieldToTab('Root.Content.Images', $manager);
          return $fields;
       }

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

          /** @see SiteTreeFileHandler.php */
          $this->deleteAttachedFiles();
       }
    }

    That's all. Now you can add Images to this Page and output them as described above.
    Please note, that there is still a FilePage.php Class inside the filemanager/code Directory. You can safely delete it, it's just there for demonstration purposes.

  • Fuzz10
    Avatar
    Community Member
    786 Posts

    Re: HasManyFileManager: New CMS module/extension. Testers/review needed Link to this post

    Hi Banal,

    That is an awesome piece of work you did !

    I'm currently using your module in 1 of our projects , it works like a charm !

    Thank you for that ...

    I think this could very well be integrated into the main CMS system (without being a separate module) !

  • banal
    Avatar
    Community Member
    901 Posts

    Re: HasManyFileManager: New CMS module/extension. Testers/review needed Link to this post

    Hi Fuzz10

    Thanks for the feedback. Glad it's useful to you!
    It still has some quirks i wasn't yet able to resolve to my satisfaction. Mainly because the SilverStripe Image Class has some bugs, or let's call it "room for improvements".
    When you upload different files, but with the same Filename, the mini-thumbnails get messed up. The creation of folders and putting files into the right directory doesn't work as expected too... i filed that in the BugTracker.

    As of now, it works quite nicely here but i had to modify the Image Class (ss core).
    Of course it would be awesome, if the SilverStripe Team decides to put this "module" into a future release ;)

    103788 Views
Page: 1 2 3 4 5 6 7 8
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.